xbe | efe3422 | 2014-03-16 00:14:26 -0700 | [diff] [blame] | 1 | #include <stdbool.h> |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <stdarg.h> |
| 4 | #include <string.h> |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 5 | #include <assert.h> |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 6 | #include "misc.h" |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 7 | #include "mpconfig.h" |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 8 | |
| 9 | // returned value is always at least 1 greater than argument |
| 10 | #define ROUND_ALLOC(a) (((a) & ((~0) - 7)) + 8) |
| 11 | |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 12 | void vstr_init(vstr_t *vstr, int alloc) { |
Damien George | 8cd72bd | 2014-03-31 17:10:59 +0100 | [diff] [blame] | 13 | if (alloc < 2) { |
| 14 | // need at least 1 byte for the null byte at the end |
| 15 | alloc = 2; |
| 16 | } |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 17 | vstr->alloc = alloc; |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 18 | vstr->len = 0; |
| 19 | vstr->buf = m_new(char, vstr->alloc); |
| 20 | if (vstr->buf == NULL) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 21 | vstr->had_error = true; |
| 22 | return; |
| 23 | } |
| 24 | vstr->buf[0] = 0; |
| 25 | vstr->had_error = false; |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 26 | vstr->fixed_buf = false; |
| 27 | } |
| 28 | |
| 29 | void 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; |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void vstr_clear(vstr_t *vstr) { |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 40 | if (!vstr->fixed_buf) { |
| 41 | m_del(char, vstr->buf, vstr->alloc); |
| 42 | } |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 43 | vstr->buf = NULL; |
| 44 | } |
| 45 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 46 | vstr_t *vstr_new(void) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 47 | vstr_t *vstr = m_new(vstr_t, 1); |
| 48 | if (vstr == NULL) { |
| 49 | return NULL; |
| 50 | } |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 51 | vstr_init(vstr, 32); |
| 52 | return vstr; |
| 53 | } |
| 54 | |
| 55 | vstr_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); |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 61 | return vstr; |
| 62 | } |
| 63 | |
| 64 | void vstr_free(vstr_t *vstr) { |
| 65 | if (vstr != NULL) { |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 66 | if (!vstr->fixed_buf) { |
| 67 | m_del(char, vstr->buf, vstr->alloc); |
| 68 | } |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 69 | m_del_obj(vstr_t, vstr); |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | void vstr_reset(vstr_t *vstr) { |
| 74 | vstr->len = 0; |
| 75 | vstr->buf[0] = 0; |
| 76 | vstr->had_error = false; |
| 77 | } |
| 78 | |
| 79 | bool vstr_had_error(vstr_t *vstr) { |
| 80 | return vstr->had_error; |
| 81 | } |
| 82 | |
| 83 | char *vstr_str(vstr_t *vstr) { |
| 84 | if (vstr->had_error) { |
| 85 | return NULL; |
| 86 | } |
| 87 | return vstr->buf; |
| 88 | } |
| 89 | |
| 90 | int vstr_len(vstr_t *vstr) { |
| 91 | if (vstr->had_error) { |
| 92 | return 0; |
| 93 | } |
| 94 | return vstr->len; |
| 95 | } |
| 96 | |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 97 | // Extend vstr strictly to by requested size, return pointer to newly added chunk |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 98 | char *vstr_extend(vstr_t *vstr, int size) { |
| 99 | if (vstr->fixed_buf) { |
| 100 | return NULL; |
| 101 | } |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 102 | 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 |
| 114 | bool vstr_set_size(vstr_t *vstr, int size) { |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 115 | if (vstr->fixed_buf) { |
| 116 | return false; |
| 117 | } |
Paul Sokolovsky | 5d2499c | 2014-01-13 23:15:23 +0200 | [diff] [blame] | 118 | 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 |
| 129 | bool vstr_shrink(vstr_t *vstr) { |
| 130 | return vstr_set_size(vstr, vstr->len); |
| 131 | } |
| 132 | |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 133 | STATIC bool vstr_ensure_extra(vstr_t *vstr, int size) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 134 | if (vstr->len + size + 1 > vstr->alloc) { |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 135 | if (vstr->fixed_buf) { |
| 136 | return false; |
| 137 | } |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 138 | int new_alloc = ROUND_ALLOC((vstr->len + size + 1) * 2); |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 139 | char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc); |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 140 | 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 | |
| 150 | void 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 | |
| 157 | char *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 George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 167 | void vstr_add_byte(vstr_t *vstr, byte b) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 168 | byte *buf = (byte*)vstr_add_len(vstr, 1); |
| 169 | if (buf == NULL) { |
| 170 | return; |
| 171 | } |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 172 | buf[0] = b; |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void 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 | |
| 184 | void vstr_add_str(vstr_t *vstr, const char *str) { |
| 185 | vstr_add_strn(vstr, str, strlen(str)); |
| 186 | } |
| 187 | |
| 188 | void vstr_add_strn(vstr_t *vstr, const char *str, int len) { |
| 189 | if (vstr->had_error || !vstr_ensure_extra(vstr, len)) { |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 190 | // 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 | } |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 196 | return; |
| 197 | } |
Damien George | 354d15a | 2014-02-06 21:11:19 +0000 | [diff] [blame] | 198 | copy: |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 199 | memmove(vstr->buf + vstr->len, str, len); |
| 200 | vstr->len += len; |
| 201 | vstr->buf[vstr->len] = 0; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | void 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 | |
| 213 | void 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 George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 222 | char *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 George | ecd58ae | 2014-03-15 16:54:06 +0000 | [diff] [blame] | 235 | // 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 George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 237 | // increase the length |
| 238 | vstr->len += byte_len; |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 239 | } |
| 240 | return vstr->buf + byte_pos; |
| 241 | } |
| 242 | |
| 243 | void 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 George | ecd58ae | 2014-03-15 16:54:06 +0000 | [diff] [blame] | 250 | void vstr_ins_char(vstr_t *vstr, uint char_pos, unichar chr) { |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 251 | // TODO UNICODE |
Damien George | ecd58ae | 2014-03-15 16:54:06 +0000 | [diff] [blame] | 252 | char *s = vstr_ins_blank_bytes(vstr, char_pos, 1); |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 253 | if (s != NULL) { |
| 254 | *s = chr; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | void vstr_cut_head_bytes(vstr_t *vstr, uint bytes_to_cut) { |
| 259 | vstr_cut_out_bytes(vstr, 0, bytes_to_cut); |
| 260 | } |
| 261 | |
| 262 | void vstr_cut_tail_bytes(vstr_t *vstr, uint len) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 263 | if (vstr->had_error) { |
| 264 | return; |
| 265 | } |
| 266 | if (len > vstr->len) { |
| 267 | vstr->len = 0; |
| 268 | } else { |
| 269 | vstr->len -= len; |
| 270 | } |
Damien George | f64086f | 2014-01-22 23:18:50 +0000 | [diff] [blame] | 271 | vstr->buf[vstr->len] = 0; |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 272 | } |
| 273 | |
Damien George | 280e720 | 2014-03-15 14:33:09 +0000 | [diff] [blame] | 274 | void 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 | |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 287 | void vstr_printf(vstr_t *vstr, const char *fmt, ...) { |
Damien | 2f06c57 | 2013-11-03 18:20:56 +0000 | [diff] [blame] | 288 | va_list ap; |
| 289 | va_start(ap, fmt); |
| 290 | vstr_vprintf(vstr, fmt, ap); |
| 291 | va_end(ap); |
| 292 | } |
| 293 | |
| 294 | void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 295 | 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 George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 301 | // need to make a copy of the va_list because we may call vsnprintf multiple times |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 302 | int size = vstr->alloc - vstr->len; |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 303 | va_list ap2; |
| 304 | va_copy(ap2, ap); |
| 305 | int n = vsnprintf(vstr->buf + vstr->len, size, fmt, ap2); |
| 306 | va_end(ap2); |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 307 | |
| 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 | } |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 328 | |
| 329 | /** testing *****************************************************/ |
| 330 | |
| 331 | /* |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 332 | int main(void) { |
Damien | 68f59a9 | 2013-10-20 14:39:58 +0100 | [diff] [blame] | 333 | 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 | */ |