changeset 109:fedad0ed894f

Remove th_growbuf, move to th_util.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 21 Jun 2014 20:42:06 +0300
parents 87f1caa659d4
children d739df7efba7
files th_growbuf.c th_growbuf.h th_util.c th_util.h
diffstat 4 files changed, 192 insertions(+), 219 deletions(-) [+]
line wrap: on
line diff
--- a/th_growbuf.c	Sat Jun 21 20:22:49 2014 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,161 +0,0 @@
-/*
- * Growing buffer implementations
- * Programmed and designed by Matti 'ccr' Hamalainen
- * (C) Copyright 2002-2014 Tecnic Software productions (TNSP)
- *
- * Please read file 'COPYING' for information on license and distribution.
- */
-#include "th_growbuf.h"
-
-
-
-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;
-}
-
-
-BOOL th_growbuf_grow(th_growbuf_t *buf, const size_t grow)
-{
-    if (buf == NULL)
-        return FALSE;
-
-    if (buf->data == NULL || buf->len + grow >= buf->size)
-    {
-        buf->size += grow + (buf->mingrow > 0 ? buf->mingrow : TH_BUFGROW);
-        buf->data = (uint8_t *) th_realloc(buf->data, buf->size);
-        if (buf->data == NULL)
-            return FALSE;
-    }
-    return TRUE;
-}
-
-
-BOOL th_growbuf_puts(th_growbuf_t *buf, const char *str, BOOL eos)
-{
-    size_t slen;
-    if (str == NULL)
-        return FALSE;
-
-    slen = strlen(str);
-    if (!th_growbuf_grow(buf, slen + 1))
-        return FALSE;
-
-    memcpy(buf->data + buf->len, str, slen + 1);
-    buf->len += eos ? (slen + 1) : slen;
-
-    return TRUE;
-}
-
-
-BOOL th_growbuf_putch(th_growbuf_t *buf, const char ch)
-{
-    if (!th_growbuf_grow(buf, sizeof(char)))
-        return FALSE;
-
-    buf->data[buf->len++] = (uint8_t) ch;
-
-    return TRUE;
-}
-
-
-BOOL th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val)
-{
-    if (!th_growbuf_grow(buf, sizeof(uint8_t)))
-        return FALSE;
-
-    buf->data[buf->len++] = val;
-
-    return TRUE;
-}
-
-
-BOOL th_growbuf_put_u16_be(th_growbuf_t *buf, const uint16_t val)
-{
-    if (!th_growbuf_grow(buf, sizeof(uint16_t)))
-        return FALSE;
-
-    buf->data[buf->len++] = (val >> 8) & 0xff;
-    buf->data[buf->len++] = val & 0xff;
-
-    return TRUE;
-}
-
-
-BOOL th_growbuf_put_u16_le(th_growbuf_t *buf, const uint16_t val)
-{
-    if (!th_growbuf_grow(buf, sizeof(uint16_t)))
-        return FALSE;
-
-    buf->data[buf->len++] = val & 0xff;
-    buf->data[buf->len++] = (val >> 8) & 0xff;
-
-    return TRUE;
-}
-
-
-BOOL th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val)
-{
-    if (!th_growbuf_grow(buf, sizeof(uint32_t)))
-        return FALSE;
-
-    buf->data[buf->len++] = (val >> 24) & 0xff;
-    buf->data[buf->len++] = (val >> 16) & 0xff;
-    buf->data[buf->len++] = (val >> 8) & 0xff;
-    buf->data[buf->len++] = val & 0xff;
-
-    return TRUE;
-}
-
-
-BOOL th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val)
-{
-    if (!th_growbuf_grow(buf, sizeof(uint32_t)))
-        return FALSE;
-
-    buf->data[buf->len++] = val & 0xff;
-    buf->data[buf->len++] = (val >> 8) & 0xff;
-    buf->data[buf->len++] = (val >> 16) & 0xff;
-    buf->data[buf->len++] = (val >> 24) & 0xff;
-
-    return TRUE;
-}
--- a/th_growbuf.h	Sat Jun 21 20:22:49 2014 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,58 +0,0 @@
-/*
- * Growing buffer implementations
- * Programmed and designed by Matti 'ccr' Hamalainen
- * (C) Copyright 2002-2014 Tecnic Software productions (TNSP)
- *
- * Please read file 'COPYING' for information on license and distribution.
- */
-#ifndef TH_GROWBUF_H
-#define TH_GROWBUF_H
-
-#include "th_util.h"
-
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
-#define TH_BUFGROW       (32)
-
-
-typedef struct
-{
-    BOOL allocated;
-    uint8_t *data;
-    size_t size, len, mingrow;
-} th_growbuf_t;
-
-
-
-/* Simple growing string buffer
- */
-BOOL    th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, const size_t grow);
-BOOL    th_strbuf_putch(char **buf, size_t *bufsize, size_t *len, const char ch);
-BOOL    th_strbuf_puts(char **buf, size_t *bufsize, size_t *len, const char *str);
-
-
-/* Growing byte buffer
- */
-void    th_growbuf_init(th_growbuf_t *buf, const size_t mingrow);
-th_growbuf_t *th_growbuf_new(const size_t mingrow);
-void    th_growbuf_free(th_growbuf_t *buf);
-
-
-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_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);
-BOOL    th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val);
-BOOL    th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val);
-
-
-#ifdef __cplusplus
-}
-#endif
-#endif /* TH_GROWBUF_H */
--- a/th_util.c	Sat Jun 21 20:22:49 2014 +0300
+++ b/th_util.c	Sat Jun 21 20:42:06 2014 +0300
@@ -554,3 +554,158 @@
     memmove(&(buf->data[0]), &(buf->data[1]), (buf->size - 1) * sizeof(void *));
     buf->data[buf->size - 1] = ptr;
 }
+
+
+/*
+ * Growing buffer implementations
+ */
+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;
+}
+
+
+BOOL th_growbuf_grow(th_growbuf_t *buf, const size_t grow)
+{
+    if (buf == NULL)
+        return FALSE;
+
+    if (buf->data == NULL || buf->len + grow >= buf->size)
+    {
+        buf->size += grow + (buf->mingrow > 0 ? buf->mingrow : TH_BUFGROW);
+        buf->data = (uint8_t *) th_realloc(buf->data, buf->size);
+        if (buf->data == NULL)
+            return FALSE;
+    }
+    return TRUE;
+}
+
+
+BOOL th_growbuf_puts(th_growbuf_t *buf, const char *str, BOOL eos)
+{
+    size_t slen;
+    if (str == NULL)
+        return FALSE;
+
+    slen = strlen(str);
+    if (!th_growbuf_grow(buf, slen + 1))
+        return FALSE;
+
+    memcpy(buf->data + buf->len, str, slen + 1);
+    buf->len += eos ? (slen + 1) : slen;
+
+    return TRUE;
+}
+
+
+BOOL th_growbuf_putch(th_growbuf_t *buf, const char ch)
+{
+    if (!th_growbuf_grow(buf, sizeof(char)))
+        return FALSE;
+
+    buf->data[buf->len++] = (uint8_t) ch;
+
+    return TRUE;
+}
+
+
+BOOL th_growbuf_put_u8(th_growbuf_t *buf, const uint8_t val)
+{
+    if (!th_growbuf_grow(buf, sizeof(uint8_t)))
+        return FALSE;
+
+    buf->data[buf->len++] = val;
+
+    return TRUE;
+}
+
+
+BOOL th_growbuf_put_u16_be(th_growbuf_t *buf, const uint16_t val)
+{
+    if (!th_growbuf_grow(buf, sizeof(uint16_t)))
+        return FALSE;
+
+    buf->data[buf->len++] = (val >> 8) & 0xff;
+    buf->data[buf->len++] = val & 0xff;
+
+    return TRUE;
+}
+
+
+BOOL th_growbuf_put_u16_le(th_growbuf_t *buf, const uint16_t val)
+{
+    if (!th_growbuf_grow(buf, sizeof(uint16_t)))
+        return FALSE;
+
+    buf->data[buf->len++] = val & 0xff;
+    buf->data[buf->len++] = (val >> 8) & 0xff;
+
+    return TRUE;
+}
+
+
+BOOL th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val)
+{
+    if (!th_growbuf_grow(buf, sizeof(uint32_t)))
+        return FALSE;
+
+    buf->data[buf->len++] = (val >> 24) & 0xff;
+    buf->data[buf->len++] = (val >> 16) & 0xff;
+    buf->data[buf->len++] = (val >> 8) & 0xff;
+    buf->data[buf->len++] = val & 0xff;
+
+    return TRUE;
+}
+
+
+BOOL th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val)
+{
+    if (!th_growbuf_grow(buf, sizeof(uint32_t)))
+        return FALSE;
+
+    buf->data[buf->len++] = val & 0xff;
+    buf->data[buf->len++] = (val >> 8) & 0xff;
+    buf->data[buf->len++] = (val >> 16) & 0xff;
+    buf->data[buf->len++] = (val >> 24) & 0xff;
+
+    return TRUE;
+}
--- a/th_util.h	Sat Jun 21 20:22:49 2014 +0300
+++ b/th_util.h	Sat Jun 21 20:42:06 2014 +0300
@@ -187,6 +187,43 @@
 void         th_ringbuf_add(qringbuf_t *buf, void *ptr);
 
 
+/* Growing buffers
+ */
+#define TH_BUFGROW       (32)
+
+
+typedef struct
+{
+    BOOL allocated;
+    uint8_t *data;
+    size_t size, len, mingrow;
+} th_growbuf_t;
+
+
+/* Simple growing string buffer
+ */
+BOOL    th_strbuf_grow(char **buf, size_t *bufsize, size_t *len, const size_t grow);
+BOOL    th_strbuf_putch(char **buf, size_t *bufsize, size_t *len, const char ch);
+BOOL    th_strbuf_puts(char **buf, size_t *bufsize, size_t *len, const char *str);
+
+
+/* Growing byte buffer
+ */
+void    th_growbuf_init(th_growbuf_t *buf, const size_t mingrow);
+th_growbuf_t *th_growbuf_new(const size_t mingrow);
+void    th_growbuf_free(th_growbuf_t *buf);
+
+
+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_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);
+BOOL    th_growbuf_put_u32_be(th_growbuf_t *buf, const uint32_t val);
+BOOL    th_growbuf_put_u32_le(th_growbuf_t *buf, const uint32_t val);
+
+
 #ifdef __cplusplus
 }
 #endif