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