Mercurial > hg > th-libs
changeset 271:2f067fbf6f13
Oops, forgot to move th_strbuf_* functions into datastruct module.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 17 Feb 2016 14:27:47 +0200 |
parents | 6cfa9bc91ee6 |
children | 7f01ad43d257 |
files | th_datastruct.c |
diffstat | 1 files changed, 50 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/th_datastruct.c Wed Feb 17 14:22:33 2016 +0200 +++ b/th_datastruct.c Wed Feb 17 14:27:47 2016 +0200 @@ -538,3 +538,53 @@ return TRUE; } + + +/* + * Simple legacy string growing buffer + */ +BOOL th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, size_t grow) +{ + if (*buf == NULL) + *bufsize = *len = 0; + + if (*buf == NULL || *len + grow >= *bufsize) + { + *bufsize += grow + TH_BUFGROW; + *buf = th_realloc(*buf, *bufsize); + if (*buf == NULL) + return FALSE; + } + return TRUE; +} + + +BOOL th_strbuf_putch(char **buf, size_t *bufsize, size_t *len, const char ch) +{ + if (!th_strbuf_grow(buf, bufsize, len, 1)) + return FALSE; + + (*buf)[*len] = ch; + (*len)++; + + return TRUE; +} + + +BOOL th_strbuf_puts(char **buf, size_t *bufsize, size_t *len, const char *str) +{ + size_t slen; + if (str == NULL) + return FALSE; + + slen = strlen(str); + if (!th_strbuf_grow(buf, bufsize, len, slen + 1)) + return FALSE; + + memcpy(*buf + *len, str, slen + 1); + (*len) += slen; + + return TRUE; +} + +