-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbuf.c
143 lines (122 loc) · 2.62 KB
/
dbuf.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* dbuf.c
* Copyright (C) 2020 Adrian Perez de Castro <[email protected]>
*
* Distributed under terms of the MIT license.
*/
#include "dbuf.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
enum {
CHUNKSIZE = 512, /* bytes */
};
/* Wraps malloc(), realloc(), and free(). */
void*
mrealloc(void *ptr, size_t size)
{
if (size) {
ptr = ptr ? realloc(ptr, size) : malloc(size);
} else if (ptr) {
free(ptr);
ptr = NULL;
}
return ptr;
}
/* Reallocates a buffer and sets the .alloc member. */
static inline void
brealloc(struct dbuf *b, size_t size)
{
if (size) {
const size_t new_size = CHUNKSIZE * ((size / CHUNKSIZE) + 1) + 1;
assert(new_size >= size);
if (new_size != b->alloc)
b->data = mrealloc(b->data, (b->alloc = new_size));
} else if (b->data) {
free(b->data);
b->data = NULL;
b->alloc = 0;
} else {
assert(b->alloc == 0);
}
}
/* Calls brealloc() and sets the .size member. */
static inline void
bresize(struct dbuf *b, size_t size)
{
brealloc(b, size);
b->size = size;
}
struct dbuf*
dbuf_new(size_t prealloc)
{
struct dbuf *b = mrealloc(NULL, sizeof(struct dbuf));
*b = DBUF_INIT;
bresize(b, prealloc);
return b;
}
void
dbuf_free(struct dbuf *b)
{
assert(b);
dbuf_clear(b);
mrealloc(b, 0);
}
void
dbuf_resize(struct dbuf *b, size_t size)
{
assert(b);
bresize(b, size);
}
void
dbuf_clear(struct dbuf *b)
{
assert(b);
bresize(b, 0);
}
void
dbuf_addmem(struct dbuf *b, const void *data, size_t size)
{
assert(b);
assert(data);
const size_t bsize = b->size;
bresize(b, bsize + size);
memcpy(b->data + bsize, data, size);
}
void
dbuf_addstr(struct dbuf *b, const char *s)
{
assert(b);
assert(s);
const size_t bsize = b->size;
const size_t slen = strlen(s);
bresize(b, bsize + slen);
memcpy(b->data + bsize, s, slen);
}
void
dbuf_addfmtv(struct dbuf *b, const char *format, va_list args)
{
assert(b);
assert(format);
va_list saved;
va_copy(saved, args);
const size_t available = (b->alloc - b->size);
const size_t needed = available
? vsnprintf((char*) b->data + b->size, available - 1, format, args)
: vsnprintf(NULL, 0, format, args);
if (needed >= available) {
brealloc(b, b->alloc + needed);
vsnprintf((char*) b->data + b->size, b->alloc - 1, format, saved);
}
b->size += needed;
va_end(saved);
}
char*
dbuf_str(struct dbuf *b)
{
assert(b);
if (!b->alloc)
brealloc(b, 1);
b->data[b->size] = '\0';
return (char*) b->data;
}