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 ------------------------------------------------------*/
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @str_qword@ --- *
40 * Arguments: @char **pp@ = address of pointer into string
41 * @unsigned f@ = various flags
43 * Returns: Pointer to the next space-separated possibly-quoted word from
44 * the string, or null.
46 * Use: Fetches the next word from a string. If the flag
47 * @STRF_QUOTE@ is set, the `\' character acts as an escape, and
48 * single and double quotes protect whitespace.
51 char *str_qword(char **pp, unsigned f)
53 char *p = *pp, *q, *qq;
56 /* --- Preliminaries --- */
67 /* --- Main work --- */
69 for (q = qq = p; *q; q++) {
86 do q++; while (*q && ISSPACE(*q));
88 } else if (!(f & STRF_QUOTE))
106 /* --- Finished --- */
114 /* --- @str_qsplit@ --- *
116 * Arguments: @char *p@ = pointer to string
117 * @char *v[]@ = pointer to array to fill in
118 * @size_t c@ = count of strings to fill in
119 * @char **rest@ = where to store the remainder of the string
120 * @unsigned f@ = flags for @str_qword@
122 * Returns: Number of strings filled in.
124 * Use: Fills an array with pointers to the individual words of a
125 * string. The string is modified in place to contain zero
126 * bytes at the word boundaries, and the words have leading
127 * and trailing space stripped off. No more than @c@ words
128 * are read; the actual number is returned as the value of the
129 * function. Unused slots in the array are populated with
130 * null bytes. If there's any string left, the address of the
131 * remainder is stored in @rest@ (if it's non-null); otherwise
132 * @rest@ is set to a null pointer.
135 size_t str_qsplit(char *p, char *v[], size_t c, char **rest, unsigned f)
140 while (c && (q = str_qword(&p, f)) != 0) {
154 /* --- @str_getword@ --- *
156 * Arguments: @char **pp@ = address of pointer into string
158 * Returns: Pointer to the next space-separated word from the string,
161 * Use: Parses off space-separated words from a string. This is a
162 * compatibility veneer over @str_qword@.
165 char *str_getword(char **pp) { return (str_qword(pp, 0)); }
167 /* --- @str_split@ --- *
169 * Arguments: @char *p@ = pointer to string
170 * @char *v[]@ = pointer to array to fill in
171 * @size_t c@ = count of strings to fill in
172 * @char **rest@ = where to store the remainder of the string
174 * Returns: Number of strings filled in.
176 * Use: Fills an array with pointers to the individual words of a
177 * string. This is a compatibility veneer over @str_qsplit@.
180 size_t str_split(char *p, char *v[], size_t c, char **rest)
181 { return (str_qsplit(p, v, c, rest, 0)); }
183 /* --- @str_matchx@ --- *
185 * Arguments: @const char *p@ = pointer to pattern string
186 * @const char *s@ = string to compare with
187 * @unsigned f@ = various flags
189 * Returns: Nonzero if the pattern matches the string.
191 * Use: Does simple wildcard matching. This is quite nasty and more
192 * than a little slow. Supports metacharacters `*', `?' and
196 int str_matchx(const char *p, const char *s, unsigned f)
199 char pch = *p++, pche, sch;
202 if ((f & STRF_PREFIX) && !*s)
211 if (!*p || (f & STRF_PREFIX))
225 if (pch == '^' || pch == '!') {
230 if (*p == '-' && p[1] && p[1] != ']') {
233 if (pch <= sch && sch <= pche)
235 } else if (pch == sch)
239 for (;; pch = *p++) {
240 if (!pch || pch == ']')
242 if (*p == '-' && p[1] && p[1] != ']') {
245 if (pch <= sch && sch <= pche)
247 } else if (pch == sch)
259 if (*p == '-' && p[1] && p[1] != ']')
280 /* --- @str_match@ --- *
282 * Arguments: @const char *p@ = pointer to pattern string
283 * @const char *s@ = string to compare with
285 * Returns: Nonzero if the pattern matches the string.
287 * Use: Does simple wildcard matching. Equivalent to @str_matchx@
288 * with zero flags word.
291 int str_match(const char *p, const char *s)
292 { return (str_matchx(p, s, 0)); }
294 /* --- @str_sanitize@ --- *
296 * Arguments: @char *d@ = destination buffer
297 * @const char *p@ = pointer to source string
298 * @size_t sz@ = size of destination buffer
302 * Use: Writes a string into a buffer, being careful not to overflow
303 * the buffer, to null terminate the result, and to prevent
304 * nasty nonprintable characters ending up in the buffer.
307 void str_sanitize(char *d, const char *p, size_t sz)
322 /*----- That's all, folks -------------------------------------------------*/