0
|
1 /*
|
|
2 * Miscellaneous string-handling related utility-functions
|
|
3 * Programmed and designed by Matti 'ccr' Hamalainen
|
|
4 * (C) Copyright 2002-2008 Tecnic Software productions (TNSP)
|
|
5 *
|
|
6 * Please read file 'COPYING' for information on license and distribution.
|
|
7 */
|
|
8 #ifdef HAVE_CONFIG_H
|
2
|
9 #include "config.h"
|
0
|
10 #endif
|
|
11 #include "th_string.h"
|
|
12
|
79
|
13 /* strdup with a NULL check
|
|
14 */
|
|
15 char *th_strdup(const char *s)
|
|
16 {
|
81
|
17 char *res;
|
|
18 if (s == NULL)
|
|
19 return NULL;
|
|
20
|
|
21 if ((res = th_malloc(strlen(s) + 1)) == NULL)
|
|
22 return NULL;
|
|
23
|
|
24 strcpy(res, s);
|
|
25 return res;
|
79
|
26 }
|
|
27
|
|
28
|
124
|
29 char *th_strncpy(char * dst, const char * src, const size_t n)
|
0
|
30 {
|
81
|
31 const char *s = src;
|
|
32 char *d = dst;
|
|
33 size_t i;
|
|
34 assert(src != NULL);
|
|
35 assert(dst != NULL);
|
0
|
36
|
81
|
37 /* Copy to the destination */
|
|
38 i = n;
|
124
|
39 while (*s && i > 0) {
|
81
|
40 *(d++) = *(s++);
|
|
41 i--;
|
|
42 }
|
0
|
43
|
81
|
44 /* Fill rest of space with zeros */
|
|
45 while (i > 0) {
|
|
46 *(d++) = 0;
|
|
47 i--;
|
|
48 }
|
0
|
49
|
81
|
50 /* Ensure that last is always zero */
|
|
51 dst[n - 1] = 0;
|
0
|
52
|
81
|
53 return dst;
|
0
|
54 }
|
|
55
|
|
56
|
126
|
57 /* Simulate a sprintf() that allocates memory
|
124
|
58 */
|
|
59 char * th_strdup_vprintf(const char *fmt, va_list args)
|
|
60 {
|
126
|
61 ssize_t size = 100;
|
|
62 char *buf, *nbuf = NULL;
|
124
|
63
|
126
|
64 if ((buf = th_malloc(size)) == NULL)
|
|
65 return NULL;
|
|
66
|
|
67 while (1) {
|
|
68 ssize_t n = vsnprintf(buf, size, fmt, args);
|
|
69 if (n > -1 && n < size)
|
|
70 return buf;
|
|
71 if (n > -1)
|
|
72 size = n + 1;
|
|
73 else
|
|
74 size *= 2;
|
|
75
|
|
76 if ((nbuf = th_realloc(nbuf, size)) == NULL) {
|
|
77 th_free(buf);
|
|
78 return NULL;
|
124
|
79 }
|
126
|
80
|
|
81 buf = nbuf;
|
124
|
82 }
|
|
83 }
|
|
84
|
|
85
|
|
86 char * th_strdup_printf(const char *fmt, ...)
|
|
87 {
|
|
88 char *res;
|
|
89 va_list ap;
|
|
90
|
|
91 va_start(ap, fmt);
|
|
92 res = th_strdup_vprintf(fmt, ap);
|
|
93 va_end(ap);
|
|
94
|
|
95 return res;
|
|
96 }
|
126
|
97
|
124
|
98
|
|
99 /* Compare two strings ignoring case [strcasecmp, strncasecmp]
|
|
100 */
|
|
101 int th_strcasecmp(const char * str1, const char * str2)
|
|
102 {
|
|
103 const char *s1 = str1, *s2 = str2;
|
81
|
104 assert(str1 != NULL);
|
|
105 assert(str2 != NULL);
|
0
|
106
|
81
|
107 if (str1 == str2)
|
|
108 return 0;
|
0
|
109
|
124
|
110 while (*s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) {
|
81
|
111 s1++;
|
|
112 s2++;
|
|
113 }
|
0
|
114
|
81
|
115 return (th_tolower(*s1) - th_tolower(*s2));
|
0
|
116 }
|
|
117
|
|
118
|
124
|
119 int th_strncasecmp(const char * str1, const char * str2, size_t n)
|
0
|
120 {
|
124
|
121 const char *s1 = str1, *s2 = str2;
|
81
|
122 assert(str1 != NULL);
|
|
123 assert(str2 != NULL);
|
0
|
124
|
81
|
125 if (str1 == str2)
|
|
126 return 0;
|
0
|
127
|
124
|
128 while (n > 0 && *s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) {
|
81
|
129 s1++;
|
|
130 s2++;
|
|
131 n--;
|
|
132 }
|
0
|
133
|
124
|
134 return n > 0 ? (th_tolower(*s1) - th_tolower(*s2)) : 0;
|
0
|
135 }
|
|
136
|
|
137
|
|
138 /* Remove all occurences of control characters, in-place.
|
|
139 * Resulting string is always shorter or same length than original.
|
|
140 */
|
11
|
141 void th_strip_ctrlchars(char * str)
|
0
|
142 {
|
81
|
143 char *i, *j;
|
|
144 assert(str != NULL);
|
0
|
145
|
81
|
146 i = str;
|
|
147 j = str;
|
|
148 while (*i) {
|
|
149 if (!th_iscntrl(*i))
|
|
150 *(j++) = *i;
|
|
151 i++;
|
|
152 }
|
0
|
153
|
81
|
154 *j = 0;
|
0
|
155 }
|
|
156
|
|
157
|
11
|
158 /* Copy a given string over in *result.
|
0
|
159 */
|
124
|
160 int th_pstrcpy(char ** result, const char * str)
|
0
|
161 {
|
81
|
162 assert(result != NULL);
|
0
|
163
|
81
|
164 if (str == NULL)
|
|
165 return -1;
|
0
|
166
|
81
|
167 th_free(*result);
|
124
|
168 if ((*result = th_malloc(strlen(str) + 1)) == NULL)
|
81
|
169 return -2;
|
0
|
170
|
81
|
171 strcpy(*result, str);
|
|
172 return 0;
|
0
|
173 }
|
|
174
|
|
175
|
11
|
176 /* Concatenates a given string into string pointed by *result.
|
0
|
177 */
|
124
|
178 int th_pstrcat(char ** result, const char * str)
|
0
|
179 {
|
81
|
180 assert(result != NULL);
|
0
|
181
|
81
|
182 if (str == NULL)
|
|
183 return -1;
|
0
|
184
|
81
|
185 if (*result != NULL) {
|
124
|
186 *result = th_realloc(*result, strlen(*result) + strlen(str) + 1);
|
81
|
187 if (*result == NULL)
|
|
188 return -1;
|
0
|
189
|
81
|
190 strcat(*result, str);
|
|
191 } else {
|
124
|
192 *result = th_malloc(strlen(str) + 1);
|
81
|
193 if (*result == NULL)
|
|
194 return -1;
|
0
|
195
|
81
|
196 strcpy(*result, str);
|
|
197 }
|
0
|
198
|
81
|
199 return 0;
|
0
|
200 }
|
|
201
|
|
202
|
|
203 /* Find next non-whitespace character in string.
|
|
204 * Updates iPos into the position of such character and
|
|
205 * returns pointer to the string.
|
|
206 */
|
124
|
207 const char *th_findnext(const char * str, size_t * pos)
|
0
|
208 {
|
81
|
209 assert(str != NULL);
|
0
|
210
|
81
|
211 /* Terminating NULL-character is not whitespace! */
|
124
|
212 while (th_isspace(str[*pos]))
|
|
213 (*pos)++;
|
|
214 return &str[*pos];
|
0
|
215 }
|
|
216
|
|
217
|
124
|
218 /* Find next sep-character from string
|
0
|
219 */
|
124
|
220 const char *th_findsep(const char * str, size_t * pos, char sep)
|
0
|
221 {
|
81
|
222 assert(str != NULL);
|
0
|
223
|
124
|
224 while (str[*pos] && str[*pos] != sep)
|
|
225 (*pos)++;
|
|
226
|
|
227 return &str[*pos];
|
0
|
228 }
|
|
229
|
|
230
|
124
|
231 /* Find next sep- or whitespace from string
|
0
|
232 */
|
124
|
233 const char *th_findseporspace(const char * str, size_t * pos, char sep)
|
0
|
234 {
|
81
|
235 assert(str != NULL);
|
0
|
236
|
124
|
237 while (!th_isspace(str[*pos]) && str[*pos] != sep)
|
|
238 (*pos)++;
|
|
239
|
|
240 return &str[*pos];
|
0
|
241 }
|
|
242
|
|
243
|
|
244 /* Compare a string to a pattern. Case-SENSITIVE version.
|
|
245 * The matching pattern can consist of any normal characters plus
|
|
246 * wildcards ? and *. "?" matches any character and "*" matches
|
|
247 * any number of characters.
|
|
248 */
|
124
|
249 BOOL th_strmatch(const char * str, const char * pattern)
|
0
|
250 {
|
81
|
251 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE;
|
124
|
252 const char *tmpPattern = NULL;
|
0
|
253
|
81
|
254 /* Check given pattern and string */
|
|
255 if (str == NULL || pattern == NULL)
|
|
256 return FALSE;
|
0
|
257
|
81
|
258 /* Start comparision */
|
|
259 do {
|
|
260 didMatch = FALSE;
|
|
261 switch (*pattern) {
|
|
262 case '?':
|
|
263 /* Any single character matches */
|
|
264 if (*str) {
|
|
265 didMatch = TRUE;
|
|
266 pattern++;
|
|
267 str++;
|
|
268 }
|
|
269 break;
|
0
|
270
|
81
|
271 case '*':
|
|
272 didMatch = TRUE;
|
|
273 pattern++;
|
|
274 if (!*pattern)
|
|
275 isEnd = TRUE;
|
|
276 isAnyMode = TRUE;
|
|
277 tmpPattern = pattern;
|
|
278 break;
|
0
|
279
|
81
|
280 case 0:
|
|
281 if (isAnyMode) {
|
|
282 if (*str)
|
|
283 str++;
|
|
284 else
|
|
285 isEnd = TRUE;
|
|
286 } else {
|
|
287 if (*str) {
|
|
288 if (tmpPattern) {
|
|
289 isAnyMode = TRUE;
|
|
290 pattern = tmpPattern;
|
|
291 } else
|
|
292 didMatch = FALSE;
|
|
293 } else
|
|
294 isEnd = TRUE;
|
|
295 }
|
|
296 break;
|
|
297 default:
|
|
298 if (isAnyMode) {
|
|
299 if (*pattern == *str) {
|
|
300 isAnyMode = FALSE;
|
|
301 didMatch = TRUE;
|
|
302 } else {
|
|
303 if (*str) {
|
|
304 didMatch = TRUE;
|
|
305 str++;
|
|
306 }
|
|
307 }
|
|
308 } else {
|
|
309 if (*pattern == *str) {
|
|
310 didMatch = TRUE;
|
|
311 if (*pattern)
|
|
312 pattern++;
|
|
313 if (*str)
|
|
314 str++;
|
|
315 } else {
|
|
316 if (tmpPattern) {
|
|
317 didMatch = TRUE;
|
|
318 isAnyMode = TRUE;
|
|
319 pattern = tmpPattern;
|
|
320 }
|
|
321 }
|
|
322 }
|
0
|
323
|
81
|
324 if (!*str && !*pattern)
|
|
325 isEnd = TRUE;
|
|
326 break;
|
0
|
327
|
81
|
328 } /* switch */
|
0
|
329
|
81
|
330 } while (didMatch && !isEnd);
|
0
|
331
|
81
|
332 return didMatch;
|
0
|
333 }
|
|
334
|
|
335
|
|
336 /* Compare a string to a pattern. Case-INSENSITIVE version.
|
|
337 */
|
124
|
338 BOOL th_strcasematch(const char * str, const char * pattern)
|
0
|
339 {
|
81
|
340 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE;
|
124
|
341 const char *tmpPattern = NULL;
|
0
|
342
|
81
|
343 /* Check given pattern and string */
|
|
344 if (str == NULL || pattern == NULL)
|
|
345 return FALSE;
|
0
|
346
|
81
|
347 /* Start comparision */
|
|
348 do {
|
|
349 switch (*pattern) {
|
|
350 case '?':
|
|
351 /* Any single character matches */
|
|
352 if (*str) {
|
|
353 pattern++;
|
|
354 str++;
|
|
355 } else
|
|
356 didMatch = FALSE;
|
|
357 break;
|
0
|
358
|
81
|
359 case '*':
|
|
360 pattern++;
|
|
361 if (!*pattern || *pattern == '?')
|
|
362 isEnd = TRUE;
|
|
363 isAnyMode = TRUE;
|
|
364 tmpPattern = pattern;
|
|
365 break;
|
0
|
366
|
81
|
367 case 0:
|
|
368 if (isAnyMode) {
|
|
369 if (*str)
|
|
370 str++;
|
|
371 else
|
|
372 isEnd = TRUE;
|
|
373 } else {
|
|
374 if (*str) {
|
|
375 if (tmpPattern) {
|
|
376 isAnyMode = TRUE;
|
|
377 pattern = tmpPattern;
|
|
378 } else
|
|
379 didMatch = FALSE;
|
|
380 } else
|
|
381 isEnd = TRUE;
|
|
382 }
|
|
383 break;
|
0
|
384
|
81
|
385 default:
|
|
386 if (isAnyMode) {
|
|
387 if (th_tolower(*pattern) == th_tolower(*str)) {
|
|
388 isAnyMode = FALSE;
|
|
389 } else {
|
|
390 if (*str)
|
|
391 str++;
|
|
392 else
|
|
393 didMatch = FALSE;
|
|
394 }
|
|
395 } else {
|
|
396 if (th_tolower(*pattern) == th_tolower(*str)) {
|
|
397 if (*pattern)
|
|
398 pattern++;
|
|
399 if (*str)
|
|
400 str++;
|
|
401 } else {
|
|
402 if (tmpPattern) {
|
|
403 isAnyMode = TRUE;
|
|
404 pattern = tmpPattern;
|
|
405 } else
|
|
406 didMatch = FALSE;
|
|
407 }
|
|
408 }
|
0
|
409
|
81
|
410 if (!*str && !*pattern)
|
|
411 isEnd = TRUE;
|
|
412 break;
|
0
|
413
|
81
|
414 } /* switch */
|
0
|
415
|
81
|
416 } while (didMatch && !isEnd);
|
0
|
417
|
81
|
418 return didMatch;
|
0
|
419 }
|
|
420
|