3 * $Id: str.c,v 1.5 2000/10/08 09:50:11 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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.5 2000/10/08 09:50:11 mdw
34 * (str_qword): Remove redundant definition of @STRF_QUOTE@.
36 * Revision 1.4 2000/10/08 09:43:34 mdw
37 * New quoted string handling and simple pattern matching.
39 * Revision 1.3 1999/12/22 15:41:14 mdw
40 * Skip past trailing whitespace in str_getword.
42 * Revision 1.2 1999/05/26 20:52:57 mdw
43 * Add new `rest' argument for `str_split'.
45 * Revision 1.1 1999/05/17 20:37:01 mdw
46 * Some trivial string hacks.
50 /*----- Header files ------------------------------------------------------*/
59 /*----- Main code ---------------------------------------------------------*/
61 /* --- @str_qword@ --- *
63 * Arguments: @char **pp@ = address of pointer into string
64 * @unsigned f@ = various flags
66 * Returns: Pointer to the next space-separated possibly-quoted word from
67 * the string, or null.
69 * Use: Fetches the next word from a string. If the flag
70 * @STRF_QUOTE@ is set, the `\' character acts as an escape, and
71 * single and double quotes protect whitespace.
74 char *str_qword(char **pp
, unsigned f
)
76 char *p
= *pp
, *q
, *qq
;
79 /* --- Preliminaries --- */
83 while (isspace((unsigned char)*p
))
90 /* --- Main work --- */
92 for (q
= qq
= p
; *q
; q
++) {
108 if (isspace((unsigned char)*q
)) {
109 do q
++; while (*q
&& isspace((unsigned char)*q
));
111 } else if (!(f
& STRF_QUOTE
))
129 /* --- Finished --- */
137 /* --- @str_qsplit@ --- *
139 * Arguments: @char *p@ = pointer to string
140 * @char *v[]@ = pointer to array to fill in
141 * @size_t c@ = count of strings to fill in
142 * @char **rest@ = where to store the remainder of the string
143 * @unsigned f@ = flags for @str_qword@
145 * Returns: Number of strings filled in.
147 * Use: Fills an array with pointers to the individual words of a
148 * string. The string is modified in place to contain zero
149 * bytes at the word boundaries, and the words have leading
150 * and trailing space stripped off. No more than @c@ words
151 * are read; the actual number is returned as the value of the
152 * function. Unused slots in the array are populated with
153 * null bytes. If there's any string left, the address of the
154 * remainder is stored in @rest@ (if it's non-null); otherwise
155 * @rest@ is set to a null pointer.
158 size_t str_qsplit(char *p
, char *v
[], size_t c
, char **rest
, unsigned f
)
163 while (c
&& (q
= str_qword(&p
, f
)) != 0) {
177 /* --- @str_getword@ --- *
179 * Arguments: @char **pp@ = address of pointer into string
181 * Returns: Pointer to the next space-separated word from the string,
184 * Use: Parses off space-separated words from a string. This is a
185 * compatibility veneer over @str_qword@.
188 char *str_getword(char **pp
)
190 return (str_qword(pp
, 0));
193 /* --- @str_split@ --- *
195 * Arguments: @char *p@ = pointer to string
196 * @char *v[]@ = pointer to array to fill in
197 * @size_t c@ = count of strings to fill in
198 * @char **rest@ = where to store the remainder of the string
200 * Returns: Number of strings filled in.
202 * Use: Fills an array with pointers to the individual words of a
203 * string. This is a compatibility veneer over @str_qsplit@.
206 size_t str_split(char *p
, char *v
[], size_t c
, char **rest
)
208 return (str_qsplit(p
, v
, c
, rest
, 0));
211 /* --- @str_match@ --- *
213 * Arguments: @const char *p@ = pointer to pattern string
214 * @const char *s@ = string to compare with
216 * Returns: Nonzero if the pattern matches the string.
218 * Use: Does simple wildcard matching. This is quite nasty and more
219 * than a little slow. Supports metacharacters `*', `?' and
223 int str_match(const char *p
, const char *s
)
226 char pch
= *p
++, pche
, sch
;
250 if (pch
== '^' || pch
== '!') {
255 if (*p
== '-' && p
[1] && p
[1] != ']') {
258 if (pch
<= sch
&& sch
<= pche
)
260 } else if (pch
== sch
)
264 for (;; pch
= *p
++) {
265 if (!pch
|| pch
== ']')
267 if (*p
== '-' && p
[1] && p
[1] != ']') {
270 if (pch
<= sch
&& sch
<= pche
)
272 } else if (pch
== sch
)
284 if (*p
== '-' && p
[1] && p
[1] != ']')
305 /* --- @str_sanitize@ --- *
307 * Arguments: @char *d@ = destination buffer
308 * @const char *p@ = pointer to source string
309 * @size_t sz@ = size of destination buffer
313 * Use: Writes a string into a buffer, being careful not to overflow
314 * the buffer, to null terminate the result, and to prevent
315 * nasty nonprintable characters ending up in the buffer.
318 void str_sanitize(char *d
, const char *p
, size_t sz
)
325 if (!isgraph((unsigned char)ch
))
333 /*----- That's all, folks -------------------------------------------------*/