3 * Functions for hacking with strings
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * mLib is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @str_qword@ --- *
42 * Arguments: @char **pp@ = address of pointer into string
43 * @unsigned f@ = various flags
45 * Returns: Pointer to the next space-separated possibly-quoted word from
46 * the string, or null.
48 * Use: Fetches the next word from a string. If the flag
49 * @STRF_QUOTE@ is set, the `\' character acts as an escape, and
50 * single and double quotes protect whitespace.
53 char *str_qword(char **pp, unsigned f)
55 char *p = *pp, *q, *qq;
58 /* --- Preliminaries --- */
69 /* --- Main work --- */
71 for (q = qq = p; *q; q++) {
88 do q++; while (*q && ISSPACE(*q));
90 } else if (!(f & STRF_QUOTE))
108 /* --- Finished --- */
116 /* --- @str_qsplit@ --- *
118 * Arguments: @char *p@ = pointer to string
119 * @char *v[]@ = pointer to array to fill in
120 * @size_t c@ = count of strings to fill in
121 * @char **rest@ = where to store the remainder of the string
122 * @unsigned f@ = flags for @str_qword@
124 * Returns: Number of strings filled in.
126 * Use: Fills an array with pointers to the individual words of a
127 * string. The string is modified in place to contain zero
128 * bytes at the word boundaries, and the words have leading
129 * and trailing space stripped off. No more than @c@ words
130 * are read; the actual number is returned as the value of the
131 * function. Unused slots in the array are populated with
132 * null bytes. If there's any string left, the address of the
133 * remainder is stored in @rest@ (if it's non-null); otherwise
134 * @rest@ is set to a null pointer.
137 size_t str_qsplit(char *p, char *v[], size_t c, char **rest, unsigned f)
142 while (c && (q = str_qword(&p, f)) != 0) {
156 /* --- @str_getword@ --- *
158 * Arguments: @char **pp@ = address of pointer into string
160 * Returns: Pointer to the next space-separated word from the string,
163 * Use: Parses off space-separated words from a string. This is a
164 * compatibility veneer over @str_qword@.
167 char *str_getword(char **pp) { return (str_qword(pp, 0)); }
169 /* --- @str_split@ --- *
171 * Arguments: @char *p@ = pointer to string
172 * @char *v[]@ = pointer to array to fill in
173 * @size_t c@ = count of strings to fill in
174 * @char **rest@ = where to store the remainder of the string
176 * Returns: Number of strings filled in.
178 * Use: Fills an array with pointers to the individual words of a
179 * string. This is a compatibility veneer over @str_qsplit@.
182 size_t str_split(char *p, char *v[], size_t c, char **rest)
183 { return (str_qsplit(p, v, c, rest, 0)); }
185 /* --- @str_matchx@ --- *
187 * Arguments: @const char *p@ = pointer to pattern string
188 * @const char *s@ = string to compare with
189 * @unsigned f@ = various flags
191 * Returns: Nonzero if the pattern matches the string.
193 * Use: Does simple wildcard matching. This is quite nasty and more
194 * than a little slow. Supports metacharacters `*', `?' and
198 int str_matchx(const char *p, const char *s, unsigned f)
201 char pch = *p++, pche, sch;
204 if ((f & STRF_PREFIX) && !*s)
213 if (!*p || (f & STRF_PREFIX))
227 if (pch == '^' || pch == '!') {
232 if (*p == '-' && p[1] && p[1] != ']') {
235 if (pch <= sch && sch <= pche)
237 } else if (pch == sch)
241 for (;; pch = *p++) {
242 if (!pch || pch == ']')
244 if (*p == '-' && p[1] && p[1] != ']') {
247 if (pch <= sch && sch <= pche)
249 } else if (pch == sch)
261 if (*p == '-' && p[1] && p[1] != ']')
282 /* --- @str_match@ --- *
284 * Arguments: @const char *p@ = pointer to pattern string
285 * @const char *s@ = string to compare with
287 * Returns: Nonzero if the pattern matches the string.
289 * Use: Does simple wildcard matching. Equivalent to @str_matchx@
290 * with zero flags word.
293 int str_match(const char *p, const char *s)
294 { return (str_matchx(p, s, 0)); }
296 /* --- @str_sanitize@ --- *
298 * Arguments: @char *d@ = destination buffer
299 * @const char *p@ = pointer to source string
300 * @size_t sz@ = size of destination buffer
304 * Use: Writes a string into a buffer, being careful not to overflow
305 * the buffer, to null terminate the result, and to prevent
306 * nasty nonprintable characters ending up in the buffer.
309 void str_sanitize(char *d, const char *p, size_t sz)
324 /*----- That's all, folks -------------------------------------------------*/