comparison th_string.c @ 241:a1ee6c76ca1c

Functions for growing a string buffer when needed.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 20 Apr 2011 18:47:22 +0300
parents 3896861974ac
children 589f5e37dd91
comparison
equal deleted inserted replaced
240:e85d453e82eb 241:a1ee6c76ca1c
437 437
438 return (len == 6) ? val : -1; 438 return (len == 6) ? val : -1;
439 } 439 }
440 440
441 441
442 BOOL th_growbuf(char **buf, size_t *bufsize, size_t *len, size_t grow)
443 {
444 assert(buf != NULL);
445 assert(bufsize != NULL);
446 assert(len != NULL);
447
448 if (*buf == NULL)
449 *bufsize = 0;
450
451 if (*buf == NULL || *len + grow >= *bufsize) {
452 *bufsize += grow + TH_BUFGROW;
453 *buf = (char *) th_realloc(*buf, *bufsize);
454 if (*buf == NULL)
455 return FALSE;
456 }
457 return TRUE;
458 }
459
460
461 void th_vputch(char **buf, size_t *bufsize, size_t *len, const char ch)
462 {
463 if (!th_growbuf(buf, bufsize, len, 1))
464 return;
465
466 *buf[*len] = ch;
467 (*len)++;
468 }
469
470
471 void th_vputs(char **buf, size_t *bufsize, size_t *len, const char *str)
472 {
473 size_t slen;
474 if (str == NULL)
475 return;
476
477 slen = strlen(str);
478 if (!th_growbuf(buf, bufsize, len, slen))
479 return;
480
481 strcat(*buf + *len, str);
482 (*len) += slen;
483 }
484