Mercurial > hg > nnchat
annotate th_string.c @ 377:9ad157feb99a
Sync th-libs.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 03 Oct 2011 00:04:33 +0300 |
parents | 7f9057e29af2 |
children | afbc3bfd3e03 |
rev | line source |
---|---|
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; | |
377 | 39 while (*s && i > 0) |
40 { | |
81 | 41 *(d++) = *(s++); |
42 i--; | |
43 } | |
0 | 44 |
81 | 45 /* Fill rest of space with zeros */ |
377 | 46 while (i > 0) |
47 { | |
81 | 48 *(d++) = 0; |
49 i--; | |
50 } | |
0 | 51 |
81 | 52 /* Ensure that last is always zero */ |
53 dst[n - 1] = 0; | |
0 | 54 |
81 | 55 return dst; |
0 | 56 } |
57 | |
58 | |
126 | 59 /* Simulate a sprintf() that allocates memory |
124 | 60 */ |
61 char * th_strdup_vprintf(const char *fmt, va_list args) | |
62 { | |
361 | 63 int size = 64; |
126 | 64 char *buf, *nbuf = NULL; |
124 | 65 |
126 | 66 if ((buf = th_malloc(size)) == NULL) |
67 return NULL; | |
68 | |
377 | 69 while (1) |
70 { | |
361 | 71 int n; |
72 va_list ap; | |
73 va_copy(ap, args); | |
74 n = vsnprintf(buf, size, fmt, ap); | |
75 va_end(ap); | |
76 | |
126 | 77 if (n > -1 && n < size) |
78 return buf; | |
79 if (n > -1) | |
80 size = n + 1; | |
81 else | |
82 size *= 2; | |
361 | 83 |
377 | 84 if ((nbuf = th_realloc(nbuf, size)) == NULL) |
85 { | |
126 | 86 th_free(buf); |
87 return NULL; | |
124 | 88 } |
126 | 89 |
90 buf = nbuf; | |
124 | 91 } |
92 } | |
93 | |
94 | |
95 char * th_strdup_printf(const char *fmt, ...) | |
96 { | |
97 char *res; | |
98 va_list ap; | |
99 | |
100 va_start(ap, fmt); | |
101 res = th_strdup_vprintf(fmt, ap); | |
102 va_end(ap); | |
103 | |
104 return res; | |
105 } | |
126 | 106 |
124 | 107 |
282
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
108 void th_pstr_vprintf(char **buf, const char *fmt, va_list ap) |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
109 { |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
110 char *tmp = th_strdup_vprintf(fmt, ap); |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
111 th_free(*buf); |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
112 *buf = tmp; |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
113 } |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
114 |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
115 |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
116 void th_pstr_printf(char **buf, const char *fmt, ...) |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
117 { |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
118 char *tmp; |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
119 va_list ap; |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
120 |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
121 va_start(ap, fmt); |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
122 tmp = th_strdup_vprintf(fmt, ap); |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
123 va_end(ap); |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
124 |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
125 th_free(*buf); |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
126 *buf = tmp; |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
127 } |
3dc86d8eb0a9
Added helper functions for easier string manipulation.
Matti Hamalainen <ccr@tnsp.org>
parents:
244
diff
changeset
|
128 |
124 | 129 /* Compare two strings ignoring case [strcasecmp, strncasecmp] |
130 */ | |
131 int th_strcasecmp(const char * str1, const char * str2) | |
132 { | |
133 const char *s1 = str1, *s2 = str2; | |
81 | 134 assert(str1 != NULL); |
135 assert(str2 != NULL); | |
0 | 136 |
81 | 137 if (str1 == str2) |
138 return 0; | |
0 | 139 |
377 | 140 while (*s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) |
141 { | |
81 | 142 s1++; |
143 s2++; | |
144 } | |
0 | 145 |
81 | 146 return (th_tolower(*s1) - th_tolower(*s2)); |
0 | 147 } |
148 | |
149 | |
124 | 150 int th_strncasecmp(const char * str1, const char * str2, size_t n) |
0 | 151 { |
124 | 152 const char *s1 = str1, *s2 = str2; |
81 | 153 assert(str1 != NULL); |
154 assert(str2 != NULL); | |
0 | 155 |
81 | 156 if (str1 == str2) |
157 return 0; | |
0 | 158 |
377 | 159 while (n > 0 && *s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) |
160 { | |
81 | 161 s1++; |
162 s2++; | |
163 n--; | |
164 } | |
0 | 165 |
124 | 166 return n > 0 ? (th_tolower(*s1) - th_tolower(*s2)) : 0; |
0 | 167 } |
168 | |
169 | |
170 /* Remove all occurences of control characters, in-place. | |
171 * Resulting string is always shorter or same length than original. | |
172 */ | |
11 | 173 void th_strip_ctrlchars(char * str) |
0 | 174 { |
81 | 175 char *i, *j; |
176 assert(str != NULL); | |
0 | 177 |
81 | 178 i = str; |
179 j = str; | |
377 | 180 while (*i) |
181 { | |
81 | 182 if (!th_iscntrl(*i)) |
183 *(j++) = *i; | |
184 i++; | |
185 } | |
0 | 186 |
81 | 187 *j = 0; |
0 | 188 } |
189 | |
190 | |
11 | 191 /* Copy a given string over in *result. |
0 | 192 */ |
124 | 193 int th_pstrcpy(char ** result, const char * str) |
0 | 194 { |
81 | 195 assert(result != NULL); |
0 | 196 |
81 | 197 if (str == NULL) |
198 return -1; | |
0 | 199 |
81 | 200 th_free(*result); |
124 | 201 if ((*result = th_malloc(strlen(str) + 1)) == NULL) |
81 | 202 return -2; |
0 | 203 |
81 | 204 strcpy(*result, str); |
205 return 0; | |
0 | 206 } |
207 | |
208 | |
11 | 209 /* Concatenates a given string into string pointed by *result. |
0 | 210 */ |
124 | 211 int th_pstrcat(char ** result, const char * str) |
0 | 212 { |
81 | 213 assert(result != NULL); |
0 | 214 |
81 | 215 if (str == NULL) |
216 return -1; | |
0 | 217 |
377 | 218 if (*result != NULL) |
219 { | |
124 | 220 *result = th_realloc(*result, strlen(*result) + strlen(str) + 1); |
81 | 221 if (*result == NULL) |
222 return -1; | |
0 | 223 |
81 | 224 strcat(*result, str); |
377 | 225 } |
226 else | |
227 { | |
124 | 228 *result = th_malloc(strlen(str) + 1); |
81 | 229 if (*result == NULL) |
230 return -1; | |
0 | 231 |
81 | 232 strcpy(*result, str); |
233 } | |
0 | 234 |
81 | 235 return 0; |
0 | 236 } |
237 | |
238 | |
239 /* Find next non-whitespace character in string. | |
240 * Updates iPos into the position of such character and | |
241 * returns pointer to the string. | |
242 */ | |
124 | 243 const char *th_findnext(const char * str, size_t * pos) |
0 | 244 { |
81 | 245 assert(str != NULL); |
0 | 246 |
81 | 247 /* Terminating NULL-character is not whitespace! */ |
124 | 248 while (th_isspace(str[*pos])) |
249 (*pos)++; | |
377 | 250 |
124 | 251 return &str[*pos]; |
0 | 252 } |
253 | |
254 | |
124 | 255 /* Find next sep-character from string |
0 | 256 */ |
124 | 257 const char *th_findsep(const char * str, size_t * pos, char sep) |
0 | 258 { |
81 | 259 assert(str != NULL); |
0 | 260 |
124 | 261 while (str[*pos] && str[*pos] != sep) |
262 (*pos)++; | |
263 | |
264 return &str[*pos]; | |
0 | 265 } |
266 | |
267 | |
124 | 268 /* Find next sep- or whitespace from string |
0 | 269 */ |
124 | 270 const char *th_findseporspace(const char * str, size_t * pos, char sep) |
0 | 271 { |
81 | 272 assert(str != NULL); |
0 | 273 |
124 | 274 while (!th_isspace(str[*pos]) && str[*pos] != sep) |
275 (*pos)++; | |
276 | |
277 return &str[*pos]; | |
0 | 278 } |
279 | |
280 | |
281 /* Compare a string to a pattern. Case-SENSITIVE version. | |
282 * The matching pattern can consist of any normal characters plus | |
283 * wildcards ? and *. "?" matches any character and "*" matches | |
284 * any number of characters. | |
285 */ | |
124 | 286 BOOL th_strmatch(const char * str, const char * pattern) |
0 | 287 { |
81 | 288 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; |
124 | 289 const char *tmpPattern = NULL; |
0 | 290 |
81 | 291 /* Check given pattern and string */ |
292 if (str == NULL || pattern == NULL) | |
293 return FALSE; | |
0 | 294 |
81 | 295 /* Start comparision */ |
296 do { | |
297 didMatch = FALSE; | |
377 | 298 switch (*pattern) |
299 { | |
81 | 300 case '?': |
301 /* Any single character matches */ | |
377 | 302 if (*str) |
303 { | |
81 | 304 didMatch = TRUE; |
305 pattern++; | |
306 str++; | |
307 } | |
308 break; | |
0 | 309 |
81 | 310 case '*': |
311 didMatch = TRUE; | |
312 pattern++; | |
313 if (!*pattern) | |
314 isEnd = TRUE; | |
315 isAnyMode = TRUE; | |
316 tmpPattern = pattern; | |
317 break; | |
0 | 318 |
81 | 319 case 0: |
377 | 320 if (isAnyMode) |
321 { | |
81 | 322 if (*str) |
323 str++; | |
324 else | |
325 isEnd = TRUE; | |
377 | 326 } |
327 else | |
328 { | |
329 if (*str) | |
330 { | |
331 if (tmpPattern) | |
332 { | |
81 | 333 isAnyMode = TRUE; |
334 pattern = tmpPattern; | |
377 | 335 } |
336 else | |
81 | 337 didMatch = FALSE; |
338 } else | |
339 isEnd = TRUE; | |
340 } | |
341 break; | |
342 default: | |
377 | 343 if (isAnyMode) |
344 { | |
345 if (*pattern == *str) | |
346 { | |
81 | 347 isAnyMode = FALSE; |
348 didMatch = TRUE; | |
377 | 349 } |
350 else | |
351 { | |
352 if (*str) | |
353 { | |
81 | 354 didMatch = TRUE; |
355 str++; | |
356 } | |
357 } | |
377 | 358 } |
359 else | |
360 { | |
361 if (*pattern == *str) | |
362 { | |
81 | 363 didMatch = TRUE; |
364 if (*pattern) | |
365 pattern++; | |
366 if (*str) | |
367 str++; | |
377 | 368 } |
369 else | |
370 { | |
371 if (tmpPattern) | |
372 { | |
81 | 373 didMatch = TRUE; |
374 isAnyMode = TRUE; | |
375 pattern = tmpPattern; | |
376 } | |
377 } | |
378 } | |
0 | 379 |
81 | 380 if (!*str && !*pattern) |
381 isEnd = TRUE; | |
382 break; | |
0 | 383 |
81 | 384 } /* switch */ |
0 | 385 |
81 | 386 } while (didMatch && !isEnd); |
0 | 387 |
81 | 388 return didMatch; |
0 | 389 } |
390 | |
391 | |
392 /* Compare a string to a pattern. Case-INSENSITIVE version. | |
393 */ | |
124 | 394 BOOL th_strcasematch(const char * str, const char * pattern) |
0 | 395 { |
81 | 396 BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; |
124 | 397 const char *tmpPattern = NULL; |
0 | 398 |
81 | 399 /* Check given pattern and string */ |
400 if (str == NULL || pattern == NULL) | |
401 return FALSE; | |
0 | 402 |
81 | 403 /* Start comparision */ |
404 do { | |
405 switch (*pattern) { | |
406 case '?': | |
407 /* Any single character matches */ | |
377 | 408 if (*str) |
409 { | |
81 | 410 pattern++; |
411 str++; | |
412 } else | |
413 didMatch = FALSE; | |
414 break; | |
0 | 415 |
81 | 416 case '*': |
417 pattern++; | |
418 if (!*pattern || *pattern == '?') | |
419 isEnd = TRUE; | |
420 isAnyMode = TRUE; | |
421 tmpPattern = pattern; | |
422 break; | |
0 | 423 |
81 | 424 case 0: |
377 | 425 if (isAnyMode) |
426 { | |
81 | 427 if (*str) |
428 str++; | |
429 else | |
430 isEnd = TRUE; | |
377 | 431 } |
432 else | |
433 { | |
434 if (*str) | |
435 { | |
436 if (tmpPattern) | |
437 { | |
81 | 438 isAnyMode = TRUE; |
439 pattern = tmpPattern; | |
377 | 440 } |
441 else | |
81 | 442 didMatch = FALSE; |
443 } else | |
444 isEnd = TRUE; | |
445 } | |
446 break; | |
0 | 447 |
81 | 448 default: |
377 | 449 if (isAnyMode) |
450 { | |
451 if (th_tolower(*pattern) == th_tolower(*str)) | |
452 { | |
81 | 453 isAnyMode = FALSE; |
377 | 454 } |
455 else | |
456 { | |
81 | 457 if (*str) |
458 str++; | |
459 else | |
460 didMatch = FALSE; | |
461 } | |
377 | 462 } |
463 else | |
464 { | |
465 if (th_tolower(*pattern) == th_tolower(*str)) | |
466 { | |
81 | 467 if (*pattern) |
468 pattern++; | |
469 if (*str) | |
470 str++; | |
377 | 471 } |
472 else | |
473 { | |
474 if (tmpPattern) | |
475 { | |
81 | 476 isAnyMode = TRUE; |
477 pattern = tmpPattern; | |
377 | 478 } |
479 else | |
81 | 480 didMatch = FALSE; |
481 } | |
482 } | |
0 | 483 |
81 | 484 if (!*str && !*pattern) |
485 isEnd = TRUE; | |
377 | 486 |
81 | 487 break; |
0 | 488 |
81 | 489 } /* switch */ |
0 | 490 |
81 | 491 } while (didMatch && !isEnd); |
0 | 492 |
81 | 493 return didMatch; |
0 | 494 } |
495 | |
131
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
496 |
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
497 int th_get_hex_triplet(const char *str) |
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
498 { |
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
499 const char *p = str; |
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
500 int len, val = 0; |
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
501 |
377 | 502 for (len = 0; *p && len < 6; p++, len++) |
503 { | |
504 if (*p >= '0' && *p <= '9') | |
505 { | |
506 val *= 16; | |
507 val += (*p - '0'); | |
508 } | |
509 else if (*p >= 'A' && *p <= 'F') | |
510 { | |
511 val *= 16; | |
512 val += (*p - 'A') + 10; | |
513 } | |
514 else if (*p >= 'a' && *p <= 'f') | |
515 { | |
516 val *= 16; | |
517 val += (*p - 'a') + 10; | |
518 } | |
519 else | |
131
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
520 return -1; |
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
521 } |
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
522 |
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
523 return (len == 6) ? val : -1; |
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
524 } |
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
525 |
3896861974ac
Added th_get_hex_triplet().
Matti Hamalainen <ccr@tnsp.org>
parents:
127
diff
changeset
|
526 |
327 | 527 BOOL th_growbuf(char **buf, size_t *bufsize, size_t *len, size_t grow) |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
528 { |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
529 if (*buf == NULL) |
287
eb74097b73f5
Make th_vputch() and th_vputs() return a boolean value for success and failure respectively. Also fix a grave bug in th_growbuf().
Matti Hamalainen <ccr@tnsp.org>
parents:
282
diff
changeset
|
530 *bufsize = *len = 0; |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
531 |
377 | 532 if (*buf == NULL || *len + grow >= *bufsize) |
533 { | |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
534 *bufsize += grow + TH_BUFGROW; |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
535 *buf = (char *) th_realloc(*buf, *bufsize); |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
536 if (*buf == NULL) |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
537 return FALSE; |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
538 } |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
539 return TRUE; |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
540 } |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
541 |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
542 |
287
eb74097b73f5
Make th_vputch() and th_vputs() return a boolean value for success and failure respectively. Also fix a grave bug in th_growbuf().
Matti Hamalainen <ccr@tnsp.org>
parents:
282
diff
changeset
|
543 BOOL th_vputch(char **buf, size_t *bufsize, size_t *len, const char ch) |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
544 { |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
545 if (!th_growbuf(buf, bufsize, len, 1)) |
287
eb74097b73f5
Make th_vputch() and th_vputs() return a boolean value for success and failure respectively. Also fix a grave bug in th_growbuf().
Matti Hamalainen <ccr@tnsp.org>
parents:
282
diff
changeset
|
546 return FALSE; |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
547 |
244
517c7b22b3c9
Buffer pointer handling was wrong in th_vaddch(), fixed.
Matti Hamalainen <ccr@tnsp.org>
parents:
243
diff
changeset
|
548 (*buf)[*len] = ch; |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
549 (*len)++; |
287
eb74097b73f5
Make th_vputch() and th_vputs() return a boolean value for success and failure respectively. Also fix a grave bug in th_growbuf().
Matti Hamalainen <ccr@tnsp.org>
parents:
282
diff
changeset
|
550 |
eb74097b73f5
Make th_vputch() and th_vputs() return a boolean value for success and failure respectively. Also fix a grave bug in th_growbuf().
Matti Hamalainen <ccr@tnsp.org>
parents:
282
diff
changeset
|
551 return TRUE; |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
552 } |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
553 |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
554 |
287
eb74097b73f5
Make th_vputch() and th_vputs() return a boolean value for success and failure respectively. Also fix a grave bug in th_growbuf().
Matti Hamalainen <ccr@tnsp.org>
parents:
282
diff
changeset
|
555 BOOL th_vputs(char **buf, size_t *bufsize, size_t *len, const char *str) |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
556 { |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
557 size_t slen; |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
558 if (str == NULL) |
287
eb74097b73f5
Make th_vputch() and th_vputs() return a boolean value for success and failure respectively. Also fix a grave bug in th_growbuf().
Matti Hamalainen <ccr@tnsp.org>
parents:
282
diff
changeset
|
559 return FALSE; |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
560 |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
561 slen = strlen(str); |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
562 if (!th_growbuf(buf, bufsize, len, slen)) |
287
eb74097b73f5
Make th_vputch() and th_vputs() return a boolean value for success and failure respectively. Also fix a grave bug in th_growbuf().
Matti Hamalainen <ccr@tnsp.org>
parents:
282
diff
changeset
|
563 return FALSE; |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
564 |
290
f1049f487987
Use strcpy() instead of strcat().
Matti Hamalainen <ccr@tnsp.org>
parents:
287
diff
changeset
|
565 strcpy(*buf + *len, str); |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
566 (*len) += slen; |
287
eb74097b73f5
Make th_vputch() and th_vputs() return a boolean value for success and failure respectively. Also fix a grave bug in th_growbuf().
Matti Hamalainen <ccr@tnsp.org>
parents:
282
diff
changeset
|
567 |
eb74097b73f5
Make th_vputch() and th_vputs() return a boolean value for success and failure respectively. Also fix a grave bug in th_growbuf().
Matti Hamalainen <ccr@tnsp.org>
parents:
282
diff
changeset
|
568 return TRUE; |
241
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
569 } |
a1ee6c76ca1c
Functions for growing a string buffer when needed.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
570 |