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