Commit | Line | Data |
---|---|---|
081e6815 | 1 | /* -*-c-*- |
2 | * | |
081e6815 | 3 | * Functions for hacking with strings |
4 | * | |
5 | * (c) 1999 Straylight/Edgeware | |
6 | */ | |
7 | ||
d4efbcd9 | 8 | /*----- Licensing notice --------------------------------------------------* |
081e6815 | 9 | * |
10 | * This file is part of the mLib utilities library. | |
11 | * | |
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. | |
d4efbcd9 | 16 | * |
081e6815 | 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. | |
d4efbcd9 | 21 | * |
081e6815 | 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, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
c6e0eaf0 | 28 | #ifndef MLIB_STR_H |
29 | #define MLIB_STR_H | |
081e6815 | 30 | |
31 | #ifdef __cplusplus | |
32 | extern "C" { | |
33 | #endif | |
34 | ||
35 | /*----- Header files ------------------------------------------------------*/ | |
36 | ||
37 | #include <stddef.h> | |
38 | ||
39 | /*----- Functions provided ------------------------------------------------*/ | |
40 | ||
efae42a6 | 41 | /* --- @str_qword@ --- * |
081e6815 | 42 | * |
43 | * Arguments: @char **pp@ = address of pointer into string | |
efae42a6 | 44 | * @unsigned f@ = various flags |
081e6815 | 45 | * |
efae42a6 | 46 | * Returns: Pointer to the next space-separated possibly-quoted word from |
47 | * the string, or null. | |
081e6815 | 48 | * |
efae42a6 | 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. | |
081e6815 | 52 | */ |
53 | ||
efae42a6 | 54 | #define STRF_QUOTE 1u |
081e6815 | 55 | |
efae42a6 | 56 | extern char *str_qword(char **/*pp*/, unsigned /*f*/); |
57 | ||
58 | /* --- @str_qsplit@ --- * | |
081e6815 | 59 | * |
60 | * Arguments: @char *p@ = pointer to string | |
61 | * @char *v[]@ = pointer to array to fill in | |
62 | * @size_t c@ = count of strings to fill in | |
f3a542e8 | 63 | * @char **rest@ = where to store the remainder of the string |
efae42a6 | 64 | * @unsigned f@ = flags for @str_qword@ |
081e6815 | 65 | * |
66 | * Returns: Number of strings filled in. | |
67 | * | |
68 | * Use: Fills an array with pointers to the individual words of a | |
69 | * string. The string is modified in place to contain zero | |
70 | * bytes at the word boundaries, and the words have leading | |
71 | * and trailing space stripped off. No more than @c@ words | |
72 | * are read; the actual number is returned as the value of the | |
73 | * function. Unused slots in the array are populated with | |
f3a542e8 | 74 | * null bytes. If there's any string left, the address of the |
75 | * remainder is stored in @rest@ (if it's non-null); otherwise | |
76 | * @rest@ is set to a null pointer. | |
081e6815 | 77 | */ |
78 | ||
efae42a6 | 79 | extern size_t str_qsplit(char */*p*/, char */*v*/[], size_t /*c*/, |
80 | char **/*rest*/, unsigned /*f*/); | |
81 | ||
82 | /* --- @str_getword@ --- * | |
83 | * | |
84 | * Arguments: @char **pp@ = address of pointer into string | |
85 | * | |
86 | * Returns: Pointer to the next space-separated word from the string, | |
87 | * or null. | |
88 | * | |
89 | * Use: Parses off space-separated words from a string. This is a | |
90 | * compatibility veneer over @str_qword@. | |
91 | */ | |
92 | ||
93 | extern char *str_getword(char **/*pp*/); | |
94 | ||
95 | /* --- @str_split@ --- * | |
96 | * | |
97 | * Arguments: @char *p@ = pointer to string | |
98 | * @char *v[]@ = pointer to array to fill in | |
99 | * @size_t c@ = count of strings to fill in | |
100 | * @char **rest@ = where to store the remainder of the string | |
101 | * | |
102 | * Returns: Number of strings filled in. | |
103 | * | |
104 | * Use: Fills an array with pointers to the individual words of a | |
105 | * string. This is a compatibility veneer over @str_qsplit@. | |
106 | */ | |
107 | ||
f3a542e8 | 108 | extern size_t str_split(char */*p*/, char */*v*/[], |
109 | size_t /*c*/, char **/*rest*/); | |
081e6815 | 110 | |
26f325c0 | 111 | /* --- @str_matchx@ --- * |
efae42a6 | 112 | * |
113 | * Arguments: @const char *p@ = pointer to pattern string | |
114 | * @const char *s@ = string to compare with | |
26f325c0 | 115 | * @unsigned f@ = various flags |
efae42a6 | 116 | * |
117 | * Returns: Nonzero if the pattern matches the string. | |
118 | * | |
119 | * Use: Does simple wildcard matching. This is quite nasty and more | |
120 | * than a little slow. Supports metacharacters `*', `?' and | |
121 | * '['. | |
122 | */ | |
123 | ||
26f325c0 MW |
124 | #define STRF_PREFIX 1u /* Accept if @s@ is exhausted during |
125 | * the attempted match */ | |
126 | ||
127 | extern int str_matchx(const char */*p*/, const char */*s*/, unsigned /*f*/); | |
128 | ||
129 | /* --- @str_match@ --- * | |
130 | * | |
131 | * Arguments: @const char *p@ = pointer to pattern string | |
132 | * @const char *s@ = string to compare with | |
133 | * | |
134 | * Returns: Nonzero if the pattern matches the string. | |
135 | * | |
136 | * Use: Does simple wildcard matching. Equivalent to @str_matchx@ | |
137 | * with zero flags word. | |
138 | */ | |
139 | ||
efae42a6 | 140 | extern int str_match(const char */*p*/, const char */*s*/); |
141 | ||
081e6815 | 142 | /* --- @str_sanitize@ --- * |
143 | * | |
144 | * Arguments: @char *d@ = destination buffer | |
145 | * @const char *p@ = pointer to source string | |
146 | * @size_t sz@ = size of destination buffer | |
147 | * | |
148 | * Returns: --- | |
149 | * | |
150 | * Use: Writes a string into a buffer, being careful not to overflow | |
151 | * the buffer, to null terminate the result, and to prevent | |
152 | * nasty nonprintable characters ending up in the buffer. | |
153 | */ | |
154 | ||
155 | extern void str_sanitize(char */*d*/, const char */*p*/, size_t /*sz*/); | |
156 | ||
157 | /*----- That's all, folks -------------------------------------------------*/ | |
158 | ||
159 | #ifdef __cplusplus | |
160 | } | |
161 | #endif | |
162 | ||
163 | #endif |