strbuf_split_buf(): use ALLOC_GROW()
[git] / strbuf.c
1 #include "cache.h"
2 #include "refs.h"
3
4 int prefixcmp(const char *str, const char *prefix)
5 {
6         for (; ; str++, prefix++)
7                 if (!*prefix)
8                         return 0;
9                 else if (*str != *prefix)
10                         return (unsigned char)*prefix - (unsigned char)*str;
11 }
12
13 int suffixcmp(const char *str, const char *suffix)
14 {
15         int len = strlen(str), suflen = strlen(suffix);
16         if (len < suflen)
17                 return -1;
18         else
19                 return strcmp(str + len - suflen, suffix);
20 }
21
22 /*
23  * Used as the default ->buf value, so that people can always assume
24  * buf is non NULL and ->buf is NUL terminated even for a freshly
25  * initialized strbuf.
26  */
27 char strbuf_slopbuf[1];
28
29 void strbuf_init(struct strbuf *sb, size_t hint)
30 {
31         sb->alloc = sb->len = 0;
32         sb->buf = strbuf_slopbuf;
33         if (hint)
34                 strbuf_grow(sb, hint);
35 }
36
37 void strbuf_release(struct strbuf *sb)
38 {
39         if (sb->alloc) {
40                 free(sb->buf);
41                 strbuf_init(sb, 0);
42         }
43 }
44
45 char *strbuf_detach(struct strbuf *sb, size_t *sz)
46 {
47         char *res;
48         strbuf_grow(sb, 0);
49         res = sb->buf;
50         if (sz)
51                 *sz = sb->len;
52         strbuf_init(sb, 0);
53         return res;
54 }
55
56 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
57 {
58         strbuf_release(sb);
59         sb->buf   = buf;
60         sb->len   = len;
61         sb->alloc = alloc;
62         strbuf_grow(sb, 0);
63         sb->buf[sb->len] = '\0';
64 }
65
66 void strbuf_grow(struct strbuf *sb, size_t extra)
67 {
68         int new_buf = !sb->alloc;
69         if (unsigned_add_overflows(extra, 1) ||
70             unsigned_add_overflows(sb->len, extra + 1))
71                 die("you want to use way too much memory");
72         if (new_buf)
73                 sb->buf = NULL;
74         ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
75         if (new_buf)
76                 sb->buf[0] = '\0';
77 }
78
79 void strbuf_trim(struct strbuf *sb)
80 {
81         char *b = sb->buf;
82         while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
83                 sb->len--;
84         while (sb->len > 0 && isspace(*b)) {
85                 b++;
86                 sb->len--;
87         }
88         memmove(sb->buf, b, sb->len);
89         sb->buf[sb->len] = '\0';
90 }
91 void strbuf_rtrim(struct strbuf *sb)
92 {
93         while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
94                 sb->len--;
95         sb->buf[sb->len] = '\0';
96 }
97
98 void strbuf_ltrim(struct strbuf *sb)
99 {
100         char *b = sb->buf;
101         while (sb->len > 0 && isspace(*b)) {
102                 b++;
103                 sb->len--;
104         }
105         memmove(sb->buf, b, sb->len);
106         sb->buf[sb->len] = '\0';
107 }
108
109 struct strbuf **strbuf_split_buf(const char *str, size_t slen, int delim, int max)
110 {
111         struct strbuf **ret = NULL;
112         size_t nr = 0, alloc = 0;
113         const char *n, *p;
114         struct strbuf *t;
115
116         p = n = str;
117         while (n < str + slen) {
118                 int len;
119                 if (max <= 0 || nr + 1 < max)
120                         n = memchr(n, delim, slen - (n - str));
121                 else
122                         n = NULL;
123                 if (!n)
124                         n = str + slen - 1;
125                 len = n - p + 1;
126                 t = xmalloc(sizeof(struct strbuf));
127                 strbuf_init(t, len);
128                 strbuf_add(t, p, len);
129                 ALLOC_GROW(ret, nr + 2, alloc);
130                 ret[nr++] = t;
131                 p = ++n;
132         }
133         ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */
134         ret[nr] = NULL;
135         return ret;
136 }
137
138 void strbuf_list_free(struct strbuf **sbs)
139 {
140         struct strbuf **s = sbs;
141
142         while (*s) {
143                 strbuf_release(*s);
144                 free(*s++);
145         }
146         free(sbs);
147 }
148
149 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
150 {
151         int len = a->len < b->len ? a->len: b->len;
152         int cmp = memcmp(a->buf, b->buf, len);
153         if (cmp)
154                 return cmp;
155         return a->len < b->len ? -1: a->len != b->len;
156 }
157
158 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
159                                    const void *data, size_t dlen)
160 {
161         if (unsigned_add_overflows(pos, len))
162                 die("you want to use way too much memory");
163         if (pos > sb->len)
164                 die("`pos' is too far after the end of the buffer");
165         if (pos + len > sb->len)
166                 die("`pos + len' is too far after the end of the buffer");
167
168         if (dlen >= len)
169                 strbuf_grow(sb, dlen - len);
170         memmove(sb->buf + pos + dlen,
171                         sb->buf + pos + len,
172                         sb->len - pos - len);
173         memcpy(sb->buf + pos, data, dlen);
174         strbuf_setlen(sb, sb->len + dlen - len);
175 }
176
177 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
178 {
179         strbuf_splice(sb, pos, 0, data, len);
180 }
181
182 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
183 {
184         strbuf_splice(sb, pos, len, NULL, 0);
185 }
186
187 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
188 {
189         strbuf_grow(sb, len);
190         memcpy(sb->buf + sb->len, data, len);
191         strbuf_setlen(sb, sb->len + len);
192 }
193
194 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
195 {
196         strbuf_grow(sb, len);
197         memcpy(sb->buf + sb->len, sb->buf + pos, len);
198         strbuf_setlen(sb, sb->len + len);
199 }
200
201 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
202 {
203         va_list ap;
204         va_start(ap, fmt);
205         strbuf_vaddf(sb, fmt, ap);
206         va_end(ap);
207 }
208
209 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
210 {
211         int len;
212         va_list cp;
213
214         if (!strbuf_avail(sb))
215                 strbuf_grow(sb, 64);
216         va_copy(cp, ap);
217         len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
218         va_end(cp);
219         if (len < 0)
220                 die("BUG: your vsnprintf is broken (returned %d)", len);
221         if (len > strbuf_avail(sb)) {
222                 strbuf_grow(sb, len);
223                 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
224                 if (len > strbuf_avail(sb))
225                         die("BUG: your vsnprintf is broken (insatiable)");
226         }
227         strbuf_setlen(sb, sb->len + len);
228 }
229
230 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
231                    void *context)
232 {
233         for (;;) {
234                 const char *percent;
235                 size_t consumed;
236
237                 percent = strchrnul(format, '%');
238                 strbuf_add(sb, format, percent - format);
239                 if (!*percent)
240                         break;
241                 format = percent + 1;
242
243                 if (*format == '%') {
244                         strbuf_addch(sb, '%');
245                         format++;
246                         continue;
247                 }
248
249                 consumed = fn(sb, format, context);
250                 if (consumed)
251                         format += consumed;
252                 else
253                         strbuf_addch(sb, '%');
254         }
255 }
256
257 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
258                 void *context)
259 {
260         struct strbuf_expand_dict_entry *e = context;
261         size_t len;
262
263         for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
264                 if (!strncmp(placeholder, e->placeholder, len)) {
265                         if (e->value)
266                                 strbuf_addstr(sb, e->value);
267                         return len;
268                 }
269         }
270         return 0;
271 }
272
273 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
274 {
275         int i, len = src->len;
276
277         for (i = 0; i < len; i++) {
278                 if (src->buf[i] == '%')
279                         strbuf_addch(dst, '%');
280                 strbuf_addch(dst, src->buf[i]);
281         }
282 }
283
284 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
285 {
286         size_t res;
287         size_t oldalloc = sb->alloc;
288
289         strbuf_grow(sb, size);
290         res = fread(sb->buf + sb->len, 1, size, f);
291         if (res > 0)
292                 strbuf_setlen(sb, sb->len + res);
293         else if (oldalloc == 0)
294                 strbuf_release(sb);
295         return res;
296 }
297
298 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
299 {
300         size_t oldlen = sb->len;
301         size_t oldalloc = sb->alloc;
302
303         strbuf_grow(sb, hint ? hint : 8192);
304         for (;;) {
305                 ssize_t cnt;
306
307                 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
308                 if (cnt < 0) {
309                         if (oldalloc == 0)
310                                 strbuf_release(sb);
311                         else
312                                 strbuf_setlen(sb, oldlen);
313                         return -1;
314                 }
315                 if (!cnt)
316                         break;
317                 sb->len += cnt;
318                 strbuf_grow(sb, 8192);
319         }
320
321         sb->buf[sb->len] = '\0';
322         return sb->len - oldlen;
323 }
324
325 #define STRBUF_MAXLINK (2*PATH_MAX)
326
327 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
328 {
329         size_t oldalloc = sb->alloc;
330
331         if (hint < 32)
332                 hint = 32;
333
334         while (hint < STRBUF_MAXLINK) {
335                 int len;
336
337                 strbuf_grow(sb, hint);
338                 len = readlink(path, sb->buf, hint);
339                 if (len < 0) {
340                         if (errno != ERANGE)
341                                 break;
342                 } else if (len < hint) {
343                         strbuf_setlen(sb, len);
344                         return 0;
345                 }
346
347                 /* .. the buffer was too small - try again */
348                 hint *= 2;
349         }
350         if (oldalloc == 0)
351                 strbuf_release(sb);
352         return -1;
353 }
354
355 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
356 {
357         int ch;
358
359         if (feof(fp))
360                 return EOF;
361
362         strbuf_reset(sb);
363         while ((ch = fgetc(fp)) != EOF) {
364                 strbuf_grow(sb, 1);
365                 sb->buf[sb->len++] = ch;
366                 if (ch == term)
367                         break;
368         }
369         if (ch == EOF && sb->len == 0)
370                 return EOF;
371
372         sb->buf[sb->len] = '\0';
373         return 0;
374 }
375
376 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
377 {
378         if (strbuf_getwholeline(sb, fp, term))
379                 return EOF;
380         if (sb->buf[sb->len-1] == term)
381                 strbuf_setlen(sb, sb->len-1);
382         return 0;
383 }
384
385 int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
386 {
387         strbuf_reset(sb);
388
389         while (1) {
390                 char ch;
391                 ssize_t len = xread(fd, &ch, 1);
392                 if (len <= 0)
393                         return EOF;
394                 strbuf_addch(sb, ch);
395                 if (ch == term)
396                         break;
397         }
398         return 0;
399 }
400
401 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
402 {
403         int fd, len;
404
405         fd = open(path, O_RDONLY);
406         if (fd < 0)
407                 return -1;
408         len = strbuf_read(sb, fd, hint);
409         close(fd);
410         if (len < 0)
411                 return -1;
412
413         return len;
414 }
415
416 void strbuf_add_lines(struct strbuf *out, const char *prefix,
417                       const char *buf, size_t size)
418 {
419         while (size) {
420                 const char *next = memchr(buf, '\n', size);
421                 next = next ? (next + 1) : (buf + size);
422                 strbuf_addstr(out, prefix);
423                 strbuf_add(out, buf, next - buf);
424                 size -= next - buf;
425                 buf = next;
426         }
427         strbuf_complete_line(out);
428 }
429
430 static int is_rfc3986_reserved(char ch)
431 {
432         switch (ch) {
433                 case '!': case '*': case '\'': case '(': case ')': case ';':
434                 case ':': case '@': case '&': case '=': case '+': case '$':
435                 case ',': case '/': case '?': case '#': case '[': case ']':
436                         return 1;
437         }
438         return 0;
439 }
440
441 static int is_rfc3986_unreserved(char ch)
442 {
443         return isalnum(ch) ||
444                 ch == '-' || ch == '_' || ch == '.' || ch == '~';
445 }
446
447 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
448                                  int reserved)
449 {
450         strbuf_grow(sb, len);
451         while (len--) {
452                 char ch = *s++;
453                 if (is_rfc3986_unreserved(ch) ||
454                     (!reserved && is_rfc3986_reserved(ch)))
455                         strbuf_addch(sb, ch);
456                 else
457                         strbuf_addf(sb, "%%%02x", ch);
458         }
459 }
460
461 void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
462                              int reserved)
463 {
464         strbuf_add_urlencode(sb, s, strlen(s), reserved);
465 }
466
467 int printf_ln(const char *fmt, ...)
468 {
469         int ret;
470         va_list ap;
471         va_start(ap, fmt);
472         ret = vprintf(fmt, ap);
473         va_end(ap);
474         if (ret < 0 || putchar('\n') == EOF)
475                 return -1;
476         return ret + 1;
477 }
478
479 int fprintf_ln(FILE *fp, const char *fmt, ...)
480 {
481         int ret;
482         va_list ap;
483         va_start(ap, fmt);
484         ret = vfprintf(fp, fmt, ap);
485         va_end(ap);
486         if (ret < 0 || putc('\n', fp) == EOF)
487                 return -1;
488         return ret + 1;
489 }