summaryrefslogtreecommitdiff
path: root/ot_buf.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_buf.c')
-rw-r--r--ot_buf.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/ot_buf.c b/ot_buf.c
new file mode 100644
index 0000000..eb00edf
--- /dev/null
+++ b/ot_buf.c
@@ -0,0 +1,41 @@
1/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
2 It is considered beerware. Prost. Skol. Cheers or whatever.
3
4 $id$ */
5
6/* System */
7#include <stdlib.h>
8#include <string.h>
9
10/* Opentracker */
11#include "ot_buf.h"
12
13int buf_cat(ot_buf *buf, const void *src, size_t len) {
14 char *data;
15
16 if (!len)
17 return 0;
18
19 if (buf->size + len < buf->size)
20 return -1;
21
22 if (!(data = realloc(buf->data, buf->size + len)))
23 return -1;
24
25 buf->data = data;
26 memcpy(data + buf->size, src, len);
27 buf->size += len;
28
29 return 0;
30}
31
32void buf_truncate(ot_buf *buf, size_t len) {
33 if (len < buf->size)
34 buf->size = len;
35}
36
37void buf_reset(ot_buf *buf) {
38 free(buf->data);
39 buf->data = NULL;
40 buf->size = 0;
41}