-
Notifications
You must be signed in to change notification settings - Fork 0
/
buf.c
75 lines (65 loc) · 1.47 KB
/
buf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "buf.h"
int buf_alloc(struct buf *buf, size_t size, size_t padding) {
size += padding;
buf->buf = malloc(size);
if (!buf->buf) {
fprintf(stderr, "%s: OOM\n", __func__);
return -1;
}
buf->size = size;
buf->padding = padding;
buf->len = 0;
return 0;
}
int buf_vsprintf(struct buf *buf, const char *fmt, va_list ap) {
int len;
va_list a;
bool copied = false;
va_copy(a, ap);
while ((len = vsnprintf(&buf->buf[buf->padding + buf->len],
buf->size - buf->len - buf->padding, fmt, ap)) >=
buf->size - buf->len - buf->padding) {
int sz = 2*(buf->padding + buf->len + len + 1);
char *b = realloc(buf->buf, sz);
if (!b) {
fprintf(stderr, "%s: OOM\n", __func__);
return -1;
}
buf->buf = b;
buf->size = sz;
if (copied)
va_end(ap);
va_copy(ap, a);
copied = true;
}
va_end(a);
buf->len += len;
return len;
}
int buf_xsprintf(struct buf *buf, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
int ret = buf_vsprintf(buf, fmt, ap);
if (ret < 0)
exit(1);
va_end(ap);
return ret;
}
void buf_xcpy(struct buf *buf, void *src, size_t len) {
if (buf->size < buf->padding + buf->len + len) {
int sz = buf->padding + buf->len + len;
char *b = realloc(buf->buf, sz);
if (!b) {
fprintf(stderr, "%s: OOM\n", __func__);
exit(1);
}
buf->buf = b;
buf->size = sz;
}
memcpy(&buf->buf[buf->padding + buf->len], src, len);
buf->len += len;
}