summaryrefslogtreecommitdiff
path: root/buf.c
diff options
context:
space:
mode:
Diffstat (limited to 'buf.c')
-rw-r--r--buf.c156
1 files changed, 156 insertions, 0 deletions
diff --git a/buf.c b/buf.c
new file mode 100644
index 0000000..ac7e83a
--- /dev/null
+++ b/buf.c
@@ -0,0 +1,156 @@
+#include "buf.h"
+
+
+bbuf* bbuf_new(int size)
+{
+ bbuf *ret = NULL;
+ ret = malloc(sizeof(bbuf));
+ if (!ret)
+ {
+ return NULL;
+ }
+ ret->buf = malloc(size);
+ if (!ret->buf)
+ {
+ free(ret);
+ return NULL;
+ }
+ ret->buf_size = size;
+ ret->size = 0;
+ return ret;
+}
+
+//set buffer value
+int bbuf_set(bbuf *a, char *val, int size)
+{
+ if (!a)
+ return -1;
+
+ if (!a->buf)
+ return -1;
+
+ if (size<0)
+ return -1;
+
+ if (size > a->buf_size)
+ {
+ a->size = a->buf_size;
+ } else
+ {
+ a->size = size;
+ }
+ memcpy(a->buf, val, a->size);
+
+ return a->size;
+}
+
+
+//get copy of buffer
+bbuf *bbuf_copy(bbuf *buf)
+{
+ bbuf *ret = NULL;
+
+ if (!buf)
+ {
+ return NULL;
+ }
+
+ ret = malloc(sizeof(bbuf));
+ memcpy(ret, buf, sizeof(bbuf));
+ ret->buf = malloc(buf->buf_size);
+ if (!ret->buf)
+ {
+ free(ret);
+ return NULL;
+ }
+ memcpy(ret->buf, buf->buf, buf->buf_size);
+ ret->size = buf->size;
+ ret->buf_size = buf->buf_size;
+
+ return ret;
+}
+
+
+//get buffer from buffer, mapped pointer, plz dont modify
+int bbuf_get(bbuf *buf, char **val, int *size)
+{
+ if (!buf)
+ {
+ return -1;
+ }
+
+ if (!val)
+ {
+ return -1;
+ }
+
+ *size = buf->size;
+ *val = buf->buf;
+
+ return 0;
+}
+
+
+//resize buffer
+int bbuf_realloc(bbuf *buf, int size)
+{
+ int ret = -1;
+ char *ptr=NULL;
+ if (!buf)
+ return -1;
+ if (size < 0)
+ return -1;
+ if (buf->buf_size == size)
+ return size;
+
+ ret = buf->buf_size;
+ ptr = realloc(buf->buf, size);
+ if (ptr != NULL)
+ {
+ buf->buf = ptr;
+ ret = size;
+ }
+
+ return ret;
+}
+
+
+//increase buffer for size
+int bbuf_inc(bbuf *a, char *b, int size)
+{
+ printf("Not implemented\n");
+ return -1;
+}
+
+
+//decrease buffer for size
+int bbuf_dec(bbuf *a, char *b, int size)
+{
+ printf("Not implemented\n");
+ return -1;
+}
+
+
+//free buffer
+void bbuf_free(bbuf *buf)
+{
+ if (buf == NULL)
+ {
+ return;
+ }
+ if (buf->buf != NULL)
+ {
+ memset(buf->buf, 0, buf->buf_size);
+ free(buf->buf);
+ buf->buf = NULL;
+ memset(buf, 0, sizeof(bbuf));
+ free(buf);
+ }
+}
+
+int bbuf_concat(bbuf *a, bbuf *b)
+{
+ printf("Not implemented\n");
+ return -1;
+}
+