Mercurial > hg > th-libs
changeset 111:cddfc43a4e45
More functions for growbuf.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 21 Jun 2014 22:44:56 +0300 |
parents | d739df7efba7 |
children | 5403f0d2d692 |
files | th_util.c th_util.h |
diffstat | 2 files changed, 45 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/th_util.c Sat Jun 21 21:12:27 2014 +0300 +++ b/th_util.c Sat Jun 21 22:44:56 2014 +0300 @@ -559,6 +559,35 @@ /* * Growing buffer implementations */ +void th_growbuf_init(th_growbuf_t *buf, const size_t mingrow) +{ + memset(buf, 0, sizeof(th_growbuf_t)); + buf->mingrow = mingrow; +} + + +th_growbuf_t *th_growbuf_new(const size_t mingrow) +{ + th_growbuf_t *buf; + + if ((buf = th_malloc(sizeof(th_growbuf_t))) == NULL) + return NULL; + + th_growbuf_init(buf, mingrow); + + return buf; +} + + +void th_growbuf_free(th_growbuf_t *buf) +{ + th_free(buf->data); + + if (buf->allocated) + th_free(buf); +} + + BOOL th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, size_t grow) { if (*buf == NULL) @@ -648,6 +677,21 @@ } +BOOL th_growbuf_put_str(th_growbuf_t *buf, const uint8_t *s, const size_t len) +{ + if (s == NULL) + return FALSE; + + if (!th_growbuf_grow(buf, len + 1)) + return FALSE; + + memcpy(buf->data + buf->len, s, len + 1); + buf->len += len; + + return TRUE; +} + + BOOL th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val) { if (!th_growbuf_grow(buf, sizeof(uint8_t)))
--- a/th_util.h Sat Jun 21 21:12:27 2014 +0300 +++ b/th_util.h Sat Jun 21 22:44:56 2014 +0300 @@ -217,6 +217,7 @@ BOOL th_growbuf_grow(th_growbuf_t *buf, const size_t grow); BOOL th_growbuf_puts(th_growbuf_t *buf, const char *str, BOOL eos); BOOL th_growbuf_putch(th_growbuf_t *buf, const char ch); +BOOL th_growbuf_put_str(th_growbuf_t *buf, const uint8_t *s, const size_t len); BOOL th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val); BOOL th_growbuf_put_u16_be(th_growbuf_t *buf, const uint16_t val); BOOL th_growbuf_put_u16_le(th_growbuf_t *buf, const uint16_t val);