strbuf: add strbuf_vaddf
[git/git.git] / strbuf.c
CommitLineData
812666c8 1#include "cache.h"
a2fab531 2#include "refs.h"
d1df5743 3
698a68be
JH
4int 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
8cc5b290
AP
13int 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
b315c5c0
PH
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 */
27char strbuf_slopbuf[1];
28
917c9a71
PH
29void strbuf_init(struct strbuf *sb, size_t hint)
30{
b315c5c0
PH
31 sb->alloc = sb->len = 0;
32 sb->buf = strbuf_slopbuf;
f1696ee3
PH
33 if (hint)
34 strbuf_grow(sb, hint);
d1df5743
JH
35}
36
917c9a71
PH
37void strbuf_release(struct strbuf *sb)
38{
b315c5c0
PH
39 if (sb->alloc) {
40 free(sb->buf);
41 strbuf_init(sb, 0);
42 }
b449f4cf
PH
43}
44
b315c5c0 45char *strbuf_detach(struct strbuf *sb, size_t *sz)
917c9a71 46{
b315c5c0
PH
47 char *res = sb->alloc ? sb->buf : NULL;
48 if (sz)
49 *sz = sb->len;
f1696ee3 50 strbuf_init(sb, 0);
b449f4cf
PH
51 return res;
52}
53
917c9a71
PH
54void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
55{
56 strbuf_release(sb);
57 sb->buf = buf;
58 sb->len = len;
59 sb->alloc = alloc;
60 strbuf_grow(sb, 0);
61 sb->buf[sb->len] = '\0';
62}
63
64void strbuf_grow(struct strbuf *sb, size_t extra)
65{
1368f650
JN
66 if (unsigned_add_overflows(extra, 1) ||
67 unsigned_add_overflows(sb->len, extra + 1))
b449f4cf 68 die("you want to use way too much memory");
b315c5c0
PH
69 if (!sb->alloc)
70 sb->buf = NULL;
b449f4cf
PH
71 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
72}
73
eacd6dc5
LS
74void strbuf_trim(struct strbuf *sb)
75{
76 char *b = sb->buf;
77 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
78 sb->len--;
79 while (sb->len > 0 && isspace(*b)) {
80 b++;
81 sb->len--;
82 }
83 memmove(sb->buf, b, sb->len);
84 sb->buf[sb->len] = '\0';
85}
f1696ee3
PH
86void strbuf_rtrim(struct strbuf *sb)
87{
88 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
89 sb->len--;
90 sb->buf[sb->len] = '\0';
91}
92
eacd6dc5
LS
93void strbuf_ltrim(struct strbuf *sb)
94{
95 char *b = sb->buf;
96 while (sb->len > 0 && isspace(*b)) {
97 b++;
98 sb->len--;
99 }
100 memmove(sb->buf, b, sb->len);
101 sb->buf[sb->len] = '\0';
102}
103
eacd6dc5
LS
104struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
105{
106 int alloc = 2, pos = 0;
107 char *n, *p;
108 struct strbuf **ret;
109 struct strbuf *t;
110
111 ret = xcalloc(alloc, sizeof(struct strbuf *));
112 p = n = sb->buf;
113 while (n < sb->buf + sb->len) {
114 int len;
115 n = memchr(n, delim, sb->len - (n - sb->buf));
116 if (pos + 1 >= alloc) {
117 alloc = alloc * 2;
118 ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
119 }
120 if (!n)
121 n = sb->buf + sb->len - 1;
122 len = n - p + 1;
123 t = xmalloc(sizeof(struct strbuf));
124 strbuf_init(t, len);
125 strbuf_add(t, p, len);
126 ret[pos] = t;
127 ret[++pos] = NULL;
128 p = ++n;
129 }
130 return ret;
131}
132
133void strbuf_list_free(struct strbuf **sbs)
134{
135 struct strbuf **s = sbs;
136
137 while (*s) {
138 strbuf_release(*s);
139 free(*s++);
140 }
141 free(sbs);
142}
143
9b200fd6 144int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
45f66f64 145{
8f024655
AR
146 int len = a->len < b->len ? a->len: b->len;
147 int cmp = memcmp(a->buf, b->buf, len);
148 if (cmp)
149 return cmp;
150 return a->len < b->len ? -1: a->len != b->len;
45f66f64
PH
151}
152
917c9a71
PH
153void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
154 const void *data, size_t dlen)
155{
1368f650 156 if (unsigned_add_overflows(pos, len))
917c9a71
PH
157 die("you want to use way too much memory");
158 if (pos > sb->len)
159 die("`pos' is too far after the end of the buffer");
160 if (pos + len > sb->len)
161 die("`pos + len' is too far after the end of the buffer");
162
163 if (dlen >= len)
164 strbuf_grow(sb, dlen - len);
165 memmove(sb->buf + pos + dlen,
166 sb->buf + pos + len,
167 sb->len - pos - len);
168 memcpy(sb->buf + pos, data, dlen);
169 strbuf_setlen(sb, sb->len + dlen - len);
170}
171
c76689df
PH
172void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
173{
174 strbuf_splice(sb, pos, 0, data, len);
175}
176
177void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
178{
179 strbuf_splice(sb, pos, len, NULL, 0);
180}
181
917c9a71
PH
182void strbuf_add(struct strbuf *sb, const void *data, size_t len)
183{
b449f4cf
PH
184 strbuf_grow(sb, len);
185 memcpy(sb->buf + sb->len, data, len);
186 strbuf_setlen(sb, sb->len + len);
187}
188
91db267e
RS
189void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
190{
191 strbuf_grow(sb, len);
192 memcpy(sb->buf + sb->len, sb->buf + pos, len);
193 strbuf_setlen(sb, sb->len + len);
194}
195
917c9a71
PH
196void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
197{
b449f4cf 198 va_list ap;
ebeb6090
JK
199 va_start(ap, fmt);
200 strbuf_vaddf(sb, fmt, ap);
201 va_end(ap);
202}
203
204void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
205{
206 int len;
207 va_list cp;
b449f4cf 208
f141bd80
SP
209 if (!strbuf_avail(sb))
210 strbuf_grow(sb, 64);
ebeb6090
JK
211 va_copy(cp, ap);
212 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
213 va_end(cp);
f141bd80 214 if (len < 0)
ebeb6090 215 die("BUG: your vsnprintf is broken (returned %d)", len);
f1696ee3 216 if (len > strbuf_avail(sb)) {
b449f4cf 217 strbuf_grow(sb, len);
b449f4cf 218 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
ebeb6090
JK
219 if (len > strbuf_avail(sb))
220 die("BUG: your vsnprintf is broken (insatiable)");
b449f4cf
PH
221 }
222 strbuf_setlen(sb, sb->len + len);
d1df5743
JH
223}
224
c3a670de
MC
225void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
226 void *context)
cde75e59
RS
227{
228 for (;;) {
c3a670de
MC
229 const char *percent;
230 size_t consumed;
cde75e59
RS
231
232 percent = strchrnul(format, '%');
233 strbuf_add(sb, format, percent - format);
234 if (!*percent)
235 break;
236 format = percent + 1;
237
0a0416a3
JK
238 if (*format == '%') {
239 strbuf_addch(sb, '%');
240 format++;
241 continue;
242 }
243
c3a670de
MC
244 consumed = fn(sb, format, context);
245 if (consumed)
246 format += consumed;
247 else
cde75e59
RS
248 strbuf_addch(sb, '%');
249 }
250}
251
9b864e73
RS
252size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
253 void *context)
254{
255 struct strbuf_expand_dict_entry *e = context;
256 size_t len;
257
258 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
259 if (!strncmp(placeholder, e->placeholder, len)) {
260 if (e->value)
261 strbuf_addstr(sb, e->value);
262 return len;
263 }
264 }
265 return 0;
266}
267
361df5df
JK
268void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
269{
270 int i, len = src->len;
271
272 for (i = 0; i < len; i++) {
273 if (src->buf[i] == '%')
274 strbuf_addch(dst, '%');
275 strbuf_addch(dst, src->buf[i]);
276 }
277}
278
917c9a71
PH
279size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
280{
b449f4cf 281 size_t res;
2fc64700 282 size_t oldalloc = sb->alloc;
b449f4cf
PH
283
284 strbuf_grow(sb, size);
285 res = fread(sb->buf + sb->len, 1, size, f);
2fc64700 286 if (res > 0)
b449f4cf 287 strbuf_setlen(sb, sb->len + res);
6651c3f7 288 else if (oldalloc == 0)
2fc64700 289 strbuf_release(sb);
b449f4cf 290 return res;
d1df5743
JH
291}
292
f1696ee3 293ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
b449f4cf
PH
294{
295 size_t oldlen = sb->len;
2fc64700 296 size_t oldalloc = sb->alloc;
b449f4cf 297
f1696ee3 298 strbuf_grow(sb, hint ? hint : 8192);
b449f4cf
PH
299 for (;;) {
300 ssize_t cnt;
301
b449f4cf
PH
302 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
303 if (cnt < 0) {
2fc64700
RS
304 if (oldalloc == 0)
305 strbuf_release(sb);
306 else
307 strbuf_setlen(sb, oldlen);
b449f4cf
PH
308 return -1;
309 }
310 if (!cnt)
311 break;
312 sb->len += cnt;
f1696ee3 313 strbuf_grow(sb, 8192);
b449f4cf
PH
314 }
315
316 sb->buf[sb->len] = '\0';
317 return sb->len - oldlen;
d1df5743
JH
318}
319
b11b7e13
LT
320#define STRBUF_MAXLINK (2*PATH_MAX)
321
322int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
323{
2fc64700
RS
324 size_t oldalloc = sb->alloc;
325
b11b7e13
LT
326 if (hint < 32)
327 hint = 32;
328
329 while (hint < STRBUF_MAXLINK) {
330 int len;
331
332 strbuf_grow(sb, hint);
333 len = readlink(path, sb->buf, hint);
334 if (len < 0) {
335 if (errno != ERANGE)
336 break;
337 } else if (len < hint) {
338 strbuf_setlen(sb, len);
339 return 0;
340 }
341
342 /* .. the buffer was too small - try again */
343 hint *= 2;
344 }
2fc64700
RS
345 if (oldalloc == 0)
346 strbuf_release(sb);
b11b7e13
LT
347 return -1;
348}
349
c7e4f0d7 350int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
917c9a71 351{
d1df5743 352 int ch;
e6c019d0
PH
353
354 strbuf_grow(sb, 0);
355 if (feof(fp))
356 return EOF;
b449f4cf
PH
357
358 strbuf_reset(sb);
d1df5743 359 while ((ch = fgetc(fp)) != EOF) {
b449f4cf
PH
360 strbuf_grow(sb, 1);
361 sb->buf[sb->len++] = ch;
c7e4f0d7
BC
362 if (ch == term)
363 break;
d1df5743 364 }
e6c019d0
PH
365 if (ch == EOF && sb->len == 0)
366 return EOF;
b449f4cf 367
b449f4cf 368 sb->buf[sb->len] = '\0';
e6c019d0 369 return 0;
d1df5743 370}
a9390b9f 371
c7e4f0d7
BC
372int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
373{
374 if (strbuf_getwholeline(sb, fp, term))
375 return EOF;
376 if (sb->buf[sb->len-1] == term)
377 strbuf_setlen(sb, sb->len-1);
378 return 0;
379}
380
387e7e19 381int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
a9390b9f
KH
382{
383 int fd, len;
384
385 fd = open(path, O_RDONLY);
386 if (fd < 0)
387 return -1;
387e7e19 388 len = strbuf_read(sb, fd, hint);
a9390b9f
KH
389 close(fd);
390 if (len < 0)
391 return -1;
392
393 return len;
394}