3 * $Id: str.c,v 1.6 2004/04/08 01:36:13 mdw Exp $
5 * Functions for hacking with strings
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
39 /*----- Main code ---------------------------------------------------------*/
41 /* --- @str_qword@ --- *
43 * Arguments: @char **pp@ = address of pointer into string
44 * @unsigned f@ = various flags
46 * Returns: Pointer to the next space-separated possibly-quoted word from
47 * the string, or null.
49 * Use: Fetches the next word from a string. If the flag
50 * @STRF_QUOTE@ is set, the `\' character acts as an escape, and
51 * single and double quotes protect whitespace.
54 char *str_qword(char **pp, unsigned f)
56 char *p = *pp, *q, *qq;
59 /* --- Preliminaries --- */
63 while (isspace((unsigned char)*p))
70 /* --- Main work --- */
72 for (q = qq = p; *q; q++) {
88 if (isspace((unsigned char)*q)) {
89 do q++; while (*q && isspace((unsigned char)*q));
91 } else if (!(f & STRF_QUOTE))
109 /* --- Finished --- */
117 /* --- @str_qsplit@ --- *
119 * Arguments: @char *p@ = pointer to string
120 * @char *v[]@ = pointer to array to fill in
121 * @size_t c@ = count of strings to fill in
122 * @char **rest@ = where to store the remainder of the string
123 * @unsigned f@ = flags for @str_qword@
125 * Returns: Number of strings filled in.
127 * Use: Fills an array with pointers to the individual words of a
128 * string. The string is modified in place to contain zero
129 * bytes at the word boundaries, and the words have leading
130 * and trailing space stripped off. No more than @c@ words
131 * are read; the actual number is returned as the value of the
132 * function. Unused slots in the array are populated with
133 * null bytes. If there's any string left, the address of the
134 * remainder is stored in @rest@ (if it's non-null); otherwise
135 * @rest@ is set to a null pointer.
138 size_t str_qsplit(char *p, char *v[], size_t c, char **rest, unsigned f)
143 while (c && (q = str_qword(&p, f)) != 0) {
157 /* --- @str_getword@ --- *
159 * Arguments: @char **pp@ = address of pointer into string
161 * Returns: Pointer to the next space-separated word from the string,
164 * Use: Parses off space-separated words from a string. This is a
165 * compatibility veneer over @str_qword@.
168 char *str_getword(char **pp) { return (str_qword(pp, 0)); }
170 /* --- @str_split@ --- *
172 * Arguments: @char *p@ = pointer to string
173 * @char *v[]@ = pointer to array to fill in
174 * @size_t c@ = count of strings to fill in
175 * @char **rest@ = where to store the remainder of the string
177 * Returns: Number of strings filled in.
179 * Use: Fills an array with pointers to the individual words of a
180 * string. This is a compatibility veneer over @str_qsplit@.
183 size_t str_split(char *p, char *v[], size_t c, char **rest)
184 { return (str_qsplit(p, v, c, rest, 0)); }
186 /* --- @str_matchx@ --- *
188 * Arguments: @const char *p@ = pointer to pattern string
189 * @const char *s@ = string to compare with
190 * @unsigned f@ = various flags
192 * Returns: Nonzero if the pattern matches the string.
194 * Use: Does simple wildcard matching. This is quite nasty and more
195 * than a little slow. Supports metacharacters `*', `?' and
199 int str_matchx(const char *p, const char *s, unsigned f)
202 char pch = *p++, pche, sch;
205 if ((f & STRF_PREFIX) && !*s)
214 if (!*p || (f & STRF_PREFIX))
228 if (pch == '^' || pch == '!') {
233 if (*p == '-' && p[1] && p[1] != ']') {
236 if (pch <= sch && sch <= pche)
238 } else if (pch == sch)
242 for (;; pch = *p++) {
243 if (!pch || pch == ']')
245 if (*p == '-' && p[1] && p[1] != ']') {
248 if (pch <= sch && sch <= pche)
250 } else if (pch == sch)
262 if (*p == '-' && p[1] && p[1] != ']')
283 /* --- @str_match@ --- *
285 * Arguments: @const char *p@ = pointer to pattern string
286 * @const char *s@ = string to compare with
288 * Returns: Nonzero if the pattern matches the string.
290 * Use: Does simple wildcard matching. Equivalent to @str_matchx@
291 * with zero flags word.
294 int str_match(const char *p, const char *s)
295 { return (str_matchx(p, s, 0)); }
297 /* --- @str_sanitize@ --- *
299 * Arguments: @char *d@ = destination buffer
300 * @const char *p@ = pointer to source string
301 * @size_t sz@ = size of destination buffer
305 * Use: Writes a string into a buffer, being careful not to overflow
306 * the buffer, to null terminate the result, and to prevent
307 * nasty nonprintable characters ending up in the buffer.
310 void str_sanitize(char *d, const char *p, size_t sz)
317 if (!isgraph((unsigned char)ch))
325 /*----- That's all, folks -------------------------------------------------*/