blob: 4fc4326e230059887cda1c7243156aee52cd6ed5 [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) {
Damien George8cd72bd2014-03-31 17:10:59 +010013 if (alloc < 2) {
14 // need at least 1 byte for the null byte at the end
15 alloc = 2;
16 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020017 vstr->alloc = alloc;
Damien68f59a92013-10-20 14:39:58 +010018 vstr->len = 0;
19 vstr->buf = m_new(char, vstr->alloc);
20 if (vstr->buf == NULL) {
Damien68f59a92013-10-20 14:39:58 +010021 vstr->had_error = true;
22 return;
23 }
24 vstr->buf[0] = 0;
25 vstr->had_error = false;
Damien George354d15a2014-02-06 21:11:19 +000026 vstr->fixed_buf = false;
27}
28
29void vstr_init_fixed_buf(vstr_t *vstr, int alloc, char *buf) {
30 assert(alloc > 0); // need at least room for the null byte
31 vstr->alloc = alloc;
32 vstr->len = 0;
33 vstr->buf = buf;
34 vstr->buf[0] = 0;
35 vstr->had_error = false;
36 vstr->fixed_buf = true;
Damien68f59a92013-10-20 14:39:58 +010037}
38
39void vstr_clear(vstr_t *vstr) {
Damien George354d15a2014-02-06 21:11:19 +000040 if (!vstr->fixed_buf) {
41 m_del(char, vstr->buf, vstr->alloc);
42 }
Damien68f59a92013-10-20 14:39:58 +010043 vstr->buf = NULL;
44}
45
Damien8b3a7c22013-10-23 20:20:17 +010046vstr_t *vstr_new(void) {
Damien68f59a92013-10-20 14:39:58 +010047 vstr_t *vstr = m_new(vstr_t, 1);
48 if (vstr == NULL) {
49 return NULL;
50 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020051 vstr_init(vstr, 32);
52 return vstr;
53}
54
55vstr_t *vstr_new_size(int alloc) {
56 vstr_t *vstr = m_new(vstr_t, 1);
57 if (vstr == NULL) {
58 return NULL;
59 }
60 vstr_init(vstr, alloc);
Damien68f59a92013-10-20 14:39:58 +010061 return vstr;
62}
63
64void vstr_free(vstr_t *vstr) {
65 if (vstr != NULL) {
Damien George354d15a2014-02-06 21:11:19 +000066 if (!vstr->fixed_buf) {
67 m_del(char, vstr->buf, vstr->alloc);
68 }
Damien732407f2013-12-29 19:33:23 +000069 m_del_obj(vstr_t, vstr);
Damien68f59a92013-10-20 14:39:58 +010070 }
71}
72
73void vstr_reset(vstr_t *vstr) {
74 vstr->len = 0;
75 vstr->buf[0] = 0;
76 vstr->had_error = false;
77}
78
79bool vstr_had_error(vstr_t *vstr) {
80 return vstr->had_error;
81}
82
83char *vstr_str(vstr_t *vstr) {
84 if (vstr->had_error) {
85 return NULL;
86 }
87 return vstr->buf;
88}
89
90int vstr_len(vstr_t *vstr) {
91 if (vstr->had_error) {
92 return 0;
93 }
94 return vstr->len;
95}
96
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020097// Extend vstr strictly to by requested size, return pointer to newly added chunk
Damien George354d15a2014-02-06 21:11:19 +000098char *vstr_extend(vstr_t *vstr, int size) {
99 if (vstr->fixed_buf) {
100 return NULL;
101 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200102 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
103 if (new_buf == NULL) {
104 vstr->had_error = true;
105 return NULL;
106 }
107 char *p = new_buf + vstr->alloc;
108 vstr->alloc += size;
109 vstr->buf = new_buf;
110 return p;
111}
112
113// Shrink vstr to be given size
114bool vstr_set_size(vstr_t *vstr, int size) {
Damien George354d15a2014-02-06 21:11:19 +0000115 if (vstr->fixed_buf) {
116 return false;
117 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200118 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, size);
119 if (new_buf == NULL) {
120 vstr->had_error = true;
121 return false;
122 }
123 vstr->buf = new_buf;
124 vstr->alloc = vstr->len = size;
125 return true;
126}
127
128// Shrink vstr allocation to its actual length
129bool vstr_shrink(vstr_t *vstr) {
130 return vstr_set_size(vstr, vstr->len);
131}
132
Paul Sokolovsky520e2f52014-02-12 18:31:30 +0200133STATIC bool vstr_ensure_extra(vstr_t *vstr, int size) {
Damien68f59a92013-10-20 14:39:58 +0100134 if (vstr->len + size + 1 > vstr->alloc) {
Damien George354d15a2014-02-06 21:11:19 +0000135 if (vstr->fixed_buf) {
136 return false;
137 }
Damien68f59a92013-10-20 14:39:58 +0100138 int new_alloc = ROUND_ALLOC((vstr->len + size + 1) * 2);
Damien732407f2013-12-29 19:33:23 +0000139 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
Damien68f59a92013-10-20 14:39:58 +0100140 if (new_buf == NULL) {
141 vstr->had_error = true;
142 return false;
143 }
144 vstr->alloc = new_alloc;
145 vstr->buf = new_buf;
146 }
147 return true;
148}
149
150void vstr_hint_size(vstr_t *vstr, int size) {
151 // it's not an error if we fail to allocate for the size hint
152 bool er = vstr->had_error;
153 vstr_ensure_extra(vstr, size);
154 vstr->had_error = er;
155}
156
157char *vstr_add_len(vstr_t *vstr, int len) {
158 if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
159 return NULL;
160 }
161 char *buf = vstr->buf + vstr->len;
162 vstr->len += len;
163 vstr->buf[vstr->len] = 0;
164 return buf;
165}
166
Damien George280e7202014-03-15 14:33:09 +0000167void vstr_add_byte(vstr_t *vstr, byte b) {
Damien68f59a92013-10-20 14:39:58 +0100168 byte *buf = (byte*)vstr_add_len(vstr, 1);
169 if (buf == NULL) {
170 return;
171 }
Damien George280e7202014-03-15 14:33:09 +0000172 buf[0] = b;
Damien68f59a92013-10-20 14:39:58 +0100173}
174
175void vstr_add_char(vstr_t *vstr, unichar c) {
176 // TODO UNICODE
177 byte *buf = (byte*)vstr_add_len(vstr, 1);
178 if (buf == NULL) {
179 return;
180 }
181 buf[0] = c;
182}
183
184void vstr_add_str(vstr_t *vstr, const char *str) {
185 vstr_add_strn(vstr, str, strlen(str));
186}
187
188void vstr_add_strn(vstr_t *vstr, const char *str, int len) {
189 if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
Damien George354d15a2014-02-06 21:11:19 +0000190 // if buf is fixed, we got here because there isn't enough room left
191 // so just try to copy as much as we can, with room for null byte
192 if (vstr->fixed_buf && vstr->len + 1 < vstr->alloc) {
193 len = vstr->alloc - vstr->len - 1;
194 goto copy;
195 }
Damien68f59a92013-10-20 14:39:58 +0100196 return;
197 }
Damien George354d15a2014-02-06 21:11:19 +0000198copy:
Damien68f59a92013-10-20 14:39:58 +0100199 memmove(vstr->buf + vstr->len, str, len);
200 vstr->len += len;
201 vstr->buf[vstr->len] = 0;
202}
203
204/*
205void vstr_add_le16(vstr_t *vstr, unsigned short v) {
206 byte *buf = (byte*)vstr_add_len(vstr, 2);
207 if (buf == NULL) {
208 return;
209 }
210 encode_le16(buf, v);
211}
212
213void vstr_add_le32(vstr_t *vstr, unsigned int v) {
214 byte *buf = (byte*)vstr_add_len(vstr, 4);
215 if (buf == NULL) {
216 return;
217 }
218 encode_le32(buf, v);
219}
220*/
221
Damien George280e7202014-03-15 14:33:09 +0000222char *vstr_ins_blank_bytes(vstr_t *vstr, uint byte_pos, uint byte_len) {
223 if (vstr->had_error) {
224 return NULL;
225 }
226 uint l = vstr->len;
227 if (byte_pos > l) {
228 byte_pos = l;
229 }
230 if (byte_len > 0) {
231 // ensure room for the new bytes
232 if (!vstr_ensure_extra(vstr, byte_len)) {
233 return NULL;
234 }
Damien Georgeecd58ae2014-03-15 16:54:06 +0000235 // copy up the string to make room for the new bytes; +1 for the null byte
236 memmove(vstr->buf + byte_pos + byte_len, vstr->buf + byte_pos, l - byte_pos + 1);
Damien George280e7202014-03-15 14:33:09 +0000237 // increase the length
238 vstr->len += byte_len;
Damien George280e7202014-03-15 14:33:09 +0000239 }
240 return vstr->buf + byte_pos;
241}
242
243void vstr_ins_byte(vstr_t *vstr, uint byte_pos, byte b) {
244 char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1);
245 if (s != NULL) {
246 *s = b;
247 }
248}
249
Damien Georgeecd58ae2014-03-15 16:54:06 +0000250void vstr_ins_char(vstr_t *vstr, uint char_pos, unichar chr) {
Damien George280e7202014-03-15 14:33:09 +0000251 // TODO UNICODE
Damien Georgeecd58ae2014-03-15 16:54:06 +0000252 char *s = vstr_ins_blank_bytes(vstr, char_pos, 1);
Damien George280e7202014-03-15 14:33:09 +0000253 if (s != NULL) {
254 *s = chr;
255 }
256}
257
258void vstr_cut_head_bytes(vstr_t *vstr, uint bytes_to_cut) {
259 vstr_cut_out_bytes(vstr, 0, bytes_to_cut);
260}
261
262void vstr_cut_tail_bytes(vstr_t *vstr, uint len) {
Damien68f59a92013-10-20 14:39:58 +0100263 if (vstr->had_error) {
264 return;
265 }
266 if (len > vstr->len) {
267 vstr->len = 0;
268 } else {
269 vstr->len -= len;
270 }
Damien Georgef64086f2014-01-22 23:18:50 +0000271 vstr->buf[vstr->len] = 0;
Damien68f59a92013-10-20 14:39:58 +0100272}
273
Damien George280e7202014-03-15 14:33:09 +0000274void vstr_cut_out_bytes(vstr_t *vstr, uint byte_pos, uint bytes_to_cut) {
275 if (vstr->had_error || byte_pos >= vstr->len) {
276 return;
277 } else if (byte_pos + bytes_to_cut >= vstr->len) {
278 vstr->len = byte_pos;
279 vstr->buf[vstr->len] = 0;
280 } else {
281 // move includes +1 for null byte at the end
282 memmove(vstr->buf + byte_pos, vstr->buf + byte_pos + bytes_to_cut, vstr->len - byte_pos - bytes_to_cut + 1);
283 vstr->len -= bytes_to_cut;
284 }
285}
286
Damien68f59a92013-10-20 14:39:58 +0100287void vstr_printf(vstr_t *vstr, const char *fmt, ...) {
Damien2f06c572013-11-03 18:20:56 +0000288 va_list ap;
289 va_start(ap, fmt);
290 vstr_vprintf(vstr, fmt, ap);
291 va_end(ap);
292}
293
294void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) {
Damien68f59a92013-10-20 14:39:58 +0100295 if (vstr->had_error || !vstr_ensure_extra(vstr, strlen(fmt))) {
296 return;
297 }
298
299 while (1) {
300 // try to print in the allocated space
Damien George66028ab2014-01-03 14:03:48 +0000301 // need to make a copy of the va_list because we may call vsnprintf multiple times
Damien68f59a92013-10-20 14:39:58 +0100302 int size = vstr->alloc - vstr->len;
Damien George66028ab2014-01-03 14:03:48 +0000303 va_list ap2;
304 va_copy(ap2, ap);
305 int n = vsnprintf(vstr->buf + vstr->len, size, fmt, ap2);
306 va_end(ap2);
Damien68f59a92013-10-20 14:39:58 +0100307
308 // if that worked, return
309 if (n > -1 && n < size) {
310 vstr->len += n;
311 return;
312 }
313
314 // else try again with more space
315 if (n > -1) { // glibc 2.1
316 // n + 1 is precisely what is needed
317 if (!vstr_ensure_extra(vstr, n + 1)) {
318 return;
319 }
320 } else { // glibc 2.0
321 // increase to twice the old size
322 if (!vstr_ensure_extra(vstr, size * 2)) {
323 return;
324 }
325 }
326 }
327}
Damien68f59a92013-10-20 14:39:58 +0100328
329/** testing *****************************************************/
330
331/*
Damien8b3a7c22013-10-23 20:20:17 +0100332int main(void) {
Damien68f59a92013-10-20 14:39:58 +0100333 vstr_t *vstr = vstr_new();
334 int i;
335 for (i = 0; i < 10; i++) {
336 vstr_printf(vstr, "%d) this is a test %d %s\n", i, 1234, "'a string'");
337 vstr_add_str(vstr, "-----");
338 vstr_printf(vstr, "this is another test %d %s\n", 1234, "'a second string'");
339 printf("%s", vstr->buf);
340 }
341}
342*/