blob: 032acc61bcec27c3b1ad68736bc617f397620e00 [file] [log] [blame]
xbeefe34222014-03-16 00:14:26 -07001#include <stdbool.h>
Damien68f59a92013-10-20 14:39:58 +01002#include <stdio.h>
3#include <stdarg.h>
4#include <string.h>
Damien George354d15a2014-02-06 21:11:19 +00005#include <assert.h>
Damien68f59a92013-10-20 14:39:58 +01006#include "misc.h"
Paul Sokolovsky520e2f52014-02-12 18:31:30 +02007#include "mpconfig.h"
Damien68f59a92013-10-20 14:39:58 +01008
9// returned value is always at least 1 greater than argument
10#define ROUND_ALLOC(a) (((a) & ((~0) - 7)) + 8)
11
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020012void vstr_init(vstr_t *vstr, int alloc) {
13 vstr->alloc = alloc;
Damien68f59a92013-10-20 14:39:58 +010014 vstr->len = 0;
15 vstr->buf = m_new(char, vstr->alloc);
16 if (vstr->buf == NULL) {
Damien68f59a92013-10-20 14:39:58 +010017 vstr->had_error = true;
18 return;
19 }
20 vstr->buf[0] = 0;
21 vstr->had_error = false;
Damien George354d15a2014-02-06 21:11:19 +000022 vstr->fixed_buf = false;
23}
24
25void vstr_init_fixed_buf(vstr_t *vstr, int alloc, char *buf) {
26 assert(alloc > 0); // need at least room for the null byte
27 vstr->alloc = alloc;
28 vstr->len = 0;
29 vstr->buf = buf;
30 vstr->buf[0] = 0;
31 vstr->had_error = false;
32 vstr->fixed_buf = true;
Damien68f59a92013-10-20 14:39:58 +010033}
34
35void vstr_clear(vstr_t *vstr) {
Damien George354d15a2014-02-06 21:11:19 +000036 if (!vstr->fixed_buf) {
37 m_del(char, vstr->buf, vstr->alloc);
38 }
Damien68f59a92013-10-20 14:39:58 +010039 vstr->buf = NULL;
40}
41
Damien8b3a7c22013-10-23 20:20:17 +010042vstr_t *vstr_new(void) {
Damien68f59a92013-10-20 14:39:58 +010043 vstr_t *vstr = m_new(vstr_t, 1);
44 if (vstr == NULL) {
45 return NULL;
46 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020047 vstr_init(vstr, 32);
48 return vstr;
49}
50
51vstr_t *vstr_new_size(int alloc) {
52 vstr_t *vstr = m_new(vstr_t, 1);
53 if (vstr == NULL) {
54 return NULL;
55 }
56 vstr_init(vstr, alloc);
Damien68f59a92013-10-20 14:39:58 +010057 return vstr;
58}
59
60void vstr_free(vstr_t *vstr) {
61 if (vstr != NULL) {
Damien George354d15a2014-02-06 21:11:19 +000062 if (!vstr->fixed_buf) {
63 m_del(char, vstr->buf, vstr->alloc);
64 }
Damien732407f2013-12-29 19:33:23 +000065 m_del_obj(vstr_t, vstr);
Damien68f59a92013-10-20 14:39:58 +010066 }
67}
68
69void vstr_reset(vstr_t *vstr) {
70 vstr->len = 0;
71 vstr->buf[0] = 0;
72 vstr->had_error = false;
73}
74
75bool vstr_had_error(vstr_t *vstr) {
76 return vstr->had_error;
77}
78
79char *vstr_str(vstr_t *vstr) {
80 if (vstr->had_error) {
81 return NULL;
82 }
83 return vstr->buf;
84}
85
86int vstr_len(vstr_t *vstr) {
87 if (vstr->had_error) {
88 return 0;
89 }
90 return vstr->len;
91}
92
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020093// Extend vstr strictly to by requested size, return pointer to newly added chunk
Damien George354d15a2014-02-06 21:11:19 +000094char *vstr_extend(vstr_t *vstr, int size) {
95 if (vstr->fixed_buf) {
96 return NULL;
97 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020098 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
99 if (new_buf == NULL) {
100 vstr->had_error = true;
101 return NULL;
102 }
103 char *p = new_buf + vstr->alloc;
104 vstr->alloc += size;
105 vstr->buf = new_buf;
106 return p;
107}
108
109// Shrink vstr to be given size
110bool vstr_set_size(vstr_t *vstr, int size) {
Damien George354d15a2014-02-06 21:11:19 +0000111 if (vstr->fixed_buf) {
112 return false;
113 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200114 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, size);
115 if (new_buf == NULL) {
116 vstr->had_error = true;
117 return false;
118 }
119 vstr->buf = new_buf;
120 vstr->alloc = vstr->len = size;
121 return true;
122}
123
124// Shrink vstr allocation to its actual length
125bool vstr_shrink(vstr_t *vstr) {
126 return vstr_set_size(vstr, vstr->len);
127}
128
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200129STATIC bool vstr_ensure_extra(vstr_t *vstr, int size) {
Damien68f59a92013-10-20 14:39:58 +0100130 if (vstr->len + size + 1 > vstr->alloc) {
Damien George354d15a2014-02-06 21:11:19 +0000131 if (vstr->fixed_buf) {
132 return false;
133 }
Damien68f59a92013-10-20 14:39:58 +0100134 int new_alloc = ROUND_ALLOC((vstr->len + size + 1) * 2);
Damien732407f2013-12-29 19:33:23 +0000135 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
Damien68f59a92013-10-20 14:39:58 +0100136 if (new_buf == NULL) {
137 vstr->had_error = true;
138 return false;
139 }
140 vstr->alloc = new_alloc;
141 vstr->buf = new_buf;
142 }
143 return true;
144}
145
146void vstr_hint_size(vstr_t *vstr, int size) {
147 // it's not an error if we fail to allocate for the size hint
148 bool er = vstr->had_error;
149 vstr_ensure_extra(vstr, size);
150 vstr->had_error = er;
151}
152
153char *vstr_add_len(vstr_t *vstr, int len) {
154 if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
155 return NULL;
156 }
157 char *buf = vstr->buf + vstr->len;
158 vstr->len += len;
159 vstr->buf[vstr->len] = 0;
160 return buf;
161}
162
Damien George280e7202014-03-15 14:33:09 +0000163void vstr_add_byte(vstr_t *vstr, byte b) {
Damien68f59a92013-10-20 14:39:58 +0100164 byte *buf = (byte*)vstr_add_len(vstr, 1);
165 if (buf == NULL) {
166 return;
167 }
Damien George280e7202014-03-15 14:33:09 +0000168 buf[0] = b;
Damien68f59a92013-10-20 14:39:58 +0100169}
170
171void vstr_add_char(vstr_t *vstr, unichar c) {
172 // TODO UNICODE
173 byte *buf = (byte*)vstr_add_len(vstr, 1);
174 if (buf == NULL) {
175 return;
176 }
177 buf[0] = c;
178}
179
180void vstr_add_str(vstr_t *vstr, const char *str) {
181 vstr_add_strn(vstr, str, strlen(str));
182}
183
184void vstr_add_strn(vstr_t *vstr, const char *str, int len) {
185 if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
Damien George354d15a2014-02-06 21:11:19 +0000186 // if buf is fixed, we got here because there isn't enough room left
187 // so just try to copy as much as we can, with room for null byte
188 if (vstr->fixed_buf && vstr->len + 1 < vstr->alloc) {
189 len = vstr->alloc - vstr->len - 1;
190 goto copy;
191 }
Damien68f59a92013-10-20 14:39:58 +0100192 return;
193 }
Damien George354d15a2014-02-06 21:11:19 +0000194copy:
Damien68f59a92013-10-20 14:39:58 +0100195 memmove(vstr->buf + vstr->len, str, len);
196 vstr->len += len;
197 vstr->buf[vstr->len] = 0;
198}
199
200/*
201void vstr_add_le16(vstr_t *vstr, unsigned short v) {
202 byte *buf = (byte*)vstr_add_len(vstr, 2);
203 if (buf == NULL) {
204 return;
205 }
206 encode_le16(buf, v);
207}
208
209void vstr_add_le32(vstr_t *vstr, unsigned int v) {
210 byte *buf = (byte*)vstr_add_len(vstr, 4);
211 if (buf == NULL) {
212 return;
213 }
214 encode_le32(buf, v);
215}
216*/
217
Damien George280e7202014-03-15 14:33:09 +0000218char *vstr_ins_blank_bytes(vstr_t *vstr, uint byte_pos, uint byte_len) {
219 if (vstr->had_error) {
220 return NULL;
221 }
222 uint l = vstr->len;
223 if (byte_pos > l) {
224 byte_pos = l;
225 }
226 if (byte_len > 0) {
227 // ensure room for the new bytes
228 if (!vstr_ensure_extra(vstr, byte_len)) {
229 return NULL;
230 }
Damien Georgeecd58ae2014-03-15 16:54:06 +0000231 // copy up the string to make room for the new bytes; +1 for the null byte
232 memmove(vstr->buf + byte_pos + byte_len, vstr->buf + byte_pos, l - byte_pos + 1);
Damien George280e7202014-03-15 14:33:09 +0000233 // increase the length
234 vstr->len += byte_len;
Damien George280e7202014-03-15 14:33:09 +0000235 }
236 return vstr->buf + byte_pos;
237}
238
239void vstr_ins_byte(vstr_t *vstr, uint byte_pos, byte b) {
240 char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1);
241 if (s != NULL) {
242 *s = b;
243 }
244}
245
Damien Georgeecd58ae2014-03-15 16:54:06 +0000246void vstr_ins_char(vstr_t *vstr, uint char_pos, unichar chr) {
Damien George280e7202014-03-15 14:33:09 +0000247 // TODO UNICODE
Damien Georgeecd58ae2014-03-15 16:54:06 +0000248 char *s = vstr_ins_blank_bytes(vstr, char_pos, 1);
Damien George280e7202014-03-15 14:33:09 +0000249 if (s != NULL) {
250 *s = chr;
251 }
252}
253
254void vstr_cut_head_bytes(vstr_t *vstr, uint bytes_to_cut) {
255 vstr_cut_out_bytes(vstr, 0, bytes_to_cut);
256}
257
258void vstr_cut_tail_bytes(vstr_t *vstr, uint len) {
Damien68f59a92013-10-20 14:39:58 +0100259 if (vstr->had_error) {
260 return;
261 }
262 if (len > vstr->len) {
263 vstr->len = 0;
264 } else {
265 vstr->len -= len;
266 }
Damien Georgef64086f2014-01-22 23:18:50 +0000267 vstr->buf[vstr->len] = 0;
Damien68f59a92013-10-20 14:39:58 +0100268}
269
Damien George280e7202014-03-15 14:33:09 +0000270void vstr_cut_out_bytes(vstr_t *vstr, uint byte_pos, uint bytes_to_cut) {
271 if (vstr->had_error || byte_pos >= vstr->len) {
272 return;
273 } else if (byte_pos + bytes_to_cut >= vstr->len) {
274 vstr->len = byte_pos;
275 vstr->buf[vstr->len] = 0;
276 } else {
277 // move includes +1 for null byte at the end
278 memmove(vstr->buf + byte_pos, vstr->buf + byte_pos + bytes_to_cut, vstr->len - byte_pos - bytes_to_cut + 1);
279 vstr->len -= bytes_to_cut;
280 }
281}
282
Damien68f59a92013-10-20 14:39:58 +0100283void vstr_printf(vstr_t *vstr, const char *fmt, ...) {
Damien2f06c572013-11-03 18:20:56 +0000284 va_list ap;
285 va_start(ap, fmt);
286 vstr_vprintf(vstr, fmt, ap);
287 va_end(ap);
288}
289
290void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) {
Damien68f59a92013-10-20 14:39:58 +0100291 if (vstr->had_error || !vstr_ensure_extra(vstr, strlen(fmt))) {
292 return;
293 }
294
295 while (1) {
296 // try to print in the allocated space
Damien George66028ab2014-01-03 14:03:48 +0000297 // need to make a copy of the va_list because we may call vsnprintf multiple times
Damien68f59a92013-10-20 14:39:58 +0100298 int size = vstr->alloc - vstr->len;
Damien George66028ab2014-01-03 14:03:48 +0000299 va_list ap2;
300 va_copy(ap2, ap);
301 int n = vsnprintf(vstr->buf + vstr->len, size, fmt, ap2);
302 va_end(ap2);
Damien68f59a92013-10-20 14:39:58 +0100303
304 // if that worked, return
305 if (n > -1 && n < size) {
306 vstr->len += n;
307 return;
308 }
309
310 // else try again with more space
311 if (n > -1) { // glibc 2.1
312 // n + 1 is precisely what is needed
313 if (!vstr_ensure_extra(vstr, n + 1)) {
314 return;
315 }
316 } else { // glibc 2.0
317 // increase to twice the old size
318 if (!vstr_ensure_extra(vstr, size * 2)) {
319 return;
320 }
321 }
322 }
323}
Damien68f59a92013-10-20 14:39:58 +0100324
325/** testing *****************************************************/
326
327/*
Damien8b3a7c22013-10-23 20:20:17 +0100328int main(void) {
Damien68f59a92013-10-20 14:39:58 +0100329 vstr_t *vstr = vstr_new();
330 int i;
331 for (i = 0; i < 10; i++) {
332 vstr_printf(vstr, "%d) this is a test %d %s\n", i, 1234, "'a string'");
333 vstr_add_str(vstr, "-----");
334 vstr_printf(vstr, "this is another test %d %s\n", 1234, "'a second string'");
335 printf("%s", vstr->buf);
336 }
337}
338*/