Commit | Line | Data |
---|---|---|
a666cab8 JA |
1 | #include <stdio.h> |
2 | #include <string.h> | |
3 | #include <stdlib.h> | |
4 | ||
5 | #include "output_buffer.h" | |
6 | #include "../log.h" | |
7 | ||
8 | #define BUF_INC 1024 | |
9 | ||
10 | void buf_output_init(struct buf_output *out, int index) | |
11 | { | |
12 | out->max_buflen = BUF_INC; | |
13 | out->buf = malloc(out->max_buflen); | |
14 | memset(out->buf, 0, out->max_buflen); | |
15 | out->buflen = 0; | |
16 | } | |
17 | ||
18 | void buf_output_free(struct buf_output *out) | |
19 | { | |
20 | free(out->buf); | |
21 | } | |
22 | ||
23 | void buf_output_add(struct buf_output *out, const char *buf, size_t len) | |
24 | { | |
25 | while (out->max_buflen - out->buflen < len) { | |
26 | size_t newlen = out->max_buflen + BUF_INC - out->buflen; | |
27 | ||
28 | out->buf = realloc(out->buf, out->max_buflen + BUF_INC); | |
29 | out->max_buflen += BUF_INC; | |
30 | memset(&out->buf[out->buflen], 0, newlen); | |
31 | } | |
32 | ||
33 | memcpy(&out->buf[out->buflen], buf, len); | |
34 | out->buflen += len; | |
35 | } | |
36 | ||
37 | void buf_output_flush(struct buf_output *out) | |
38 | { | |
39 | if (out->buflen) { | |
40 | log_local_buf(out->buf, out->buflen); | |
41 | memset(out->buf, 0, out->max_buflen); | |
42 | out->buflen = 0; | |
43 | } | |
44 | } |