1#include "cache.h"
2
3int prefixcmp(const char *str, const char *prefix)
4{
5 for (; ; str++, prefix++)
6 if (!*prefix)
7 return 0;
8 else if (*str != *prefix)
9 return (unsigned char)*prefix - (unsigned char)*str;
10}
11
12/*
13 * Used as the default ->buf value, so that people can always assume
14 * buf is non NULL and ->buf is NUL terminated even for a freshly
15 * initialized strbuf.
16 */
17char strbuf_slopbuf[1];
18
19void strbuf_init(struct strbuf *sb, size_t hint)
20{
21 sb->alloc = sb->len = 0;
22 sb->buf = strbuf_slopbuf;
23 if (hint)
24 strbuf_grow(sb, hint);
25}
26
27void strbuf_release(struct strbuf *sb)
28{
29 if (sb->alloc) {
30 free(sb->buf);
31 strbuf_init(sb, 0);
32 }
33}
34
35char *strbuf_detach(struct strbuf *sb, size_t *sz)
36{
37 char *res = sb->alloc ? sb->buf : NULL;
38 if (sz)
39 *sz = sb->len;
40 strbuf_init(sb, 0);
41 return res;
42}
43
44void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
45{
46 strbuf_release(sb);
47 sb->buf = buf;
48 sb->len = len;
49 sb->alloc = alloc;
50 strbuf_grow(sb, 0);
51 sb->buf[sb->len] = '\0';
52}
53
54void strbuf_grow(struct strbuf *sb, size_t extra)
55{
56 if (sb->len + extra + 1 <= sb->len)
57 die("you want to use way too much memory");
58 if (!sb->alloc)
59 sb->buf = NULL;
60 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
61}
62
63void strbuf_trim(struct strbuf *sb)
64{
65 char *b = sb->buf;
66 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
67 sb->len--;
68 while (sb->len > 0 && isspace(*b)) {
69 b++;
70 sb->len--;
71 }
72 memmove(sb->buf, b, sb->len);
73 sb->buf[sb->len] = '\0';
74}
75void strbuf_rtrim(struct strbuf *sb)
76{
77 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
78 sb->len--;
79 sb->buf[sb->len] = '\0';
80}
81
82void strbuf_ltrim(struct strbuf *sb)
83{
84 char *b = sb->buf;
85 while (sb->len > 0 && isspace(*b)) {
86 b++;
87 sb->len--;
88 }
89 memmove(sb->buf, b, sb->len);
90 sb->buf[sb->len] = '\0';
91}
92
93void strbuf_tolower(struct strbuf *sb)
94{
95 int i;
96 for (i = 0; i < sb->len; i++)
97 sb->buf[i] = tolower(sb->buf[i]);
98}
99
100struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
101{
102 int alloc = 2, pos = 0;
103 char *n, *p;
104 struct strbuf **ret;
105 struct strbuf *t;
106
107 ret = xcalloc(alloc, sizeof(struct strbuf *));
108 p = n = sb->buf;
109 while (n < sb->buf + sb->len) {
110 int len;
111 n = memchr(n, delim, sb->len - (n - sb->buf));
112 if (pos + 1 >= alloc) {
113 alloc = alloc * 2;
114 ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
115 }
116 if (!n)
117 n = sb->buf + sb->len - 1;
118 len = n - p + 1;
119 t = xmalloc(sizeof(struct strbuf));
120 strbuf_init(t, len);
121 strbuf_add(t, p, len);
122 ret[pos] = t;
123 ret[++pos] = NULL;
124 p = ++n;
125 }
126 return ret;
127}
128
129void strbuf_list_free(struct strbuf **sbs)
130{
131 struct strbuf **s = sbs;
132
133 while (*s) {
134 strbuf_release(*s);
135 free(*s++);
136 }
137 free(sbs);
138}
139
140int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
141{
142 int cmp;
143 if (a->len < b->len) {
144 cmp = memcmp(a->buf, b->buf, a->len);
145 return cmp ? cmp : -1;
146 } else {
147 cmp = memcmp(a->buf, b->buf, b->len);
148 return cmp ? cmp : a->len != b->len;
149 }
150}
151
152void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
153 const void *data, size_t dlen)
154{
155 if (pos + len < pos)
156 die("you want to use way too much memory");
157 if (pos > sb->len)
158 die("`pos' is too far after the end of the buffer");
159 if (pos + len > sb->len)
160 die("`pos + len' is too far after the end of the buffer");
161
162 if (dlen >= len)
163 strbuf_grow(sb, dlen - len);
164 memmove(sb->buf + pos + dlen,
165 sb->buf + pos + len,
166 sb->len - pos - len);
167 memcpy(sb->buf + pos, data, dlen);
168 strbuf_setlen(sb, sb->len + dlen - len);
169}
170
171void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
172{
173 strbuf_splice(sb, pos, 0, data, len);
174}
175
176void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
177{
178 strbuf_splice(sb, pos, len, NULL, 0);
179}
180
181void strbuf_add(struct strbuf *sb, const void *data, size_t len)
182{
183 strbuf_grow(sb, len);
184 memcpy(sb->buf + sb->len, data, len);
185 strbuf_setlen(sb, sb->len + len);
186}
187
188void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
189{
190 strbuf_grow(sb, len);
191 memcpy(sb->buf + sb->len, sb->buf + pos, len);
192 strbuf_setlen(sb, sb->len + len);
193}
194
195void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
196{
197 int len;
198 va_list ap;
199
200 if (!strbuf_avail(sb))
201 strbuf_grow(sb, 64);
202 va_start(ap, fmt);
203 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
204 va_end(ap);
205 if (len < 0)
206 die("your vsnprintf is broken");
207 if (len > strbuf_avail(sb)) {
208 strbuf_grow(sb, len);
209 va_start(ap, fmt);
210 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
211 va_end(ap);
212 if (len > strbuf_avail(sb)) {
213 die("this should not happen, your snprintf is broken");
214 }
215 }
216 strbuf_setlen(sb, sb->len + len);
217}
218
219void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
220 void *context)
221{
222 for (;;) {
223 const char *percent;
224 size_t consumed;
225
226 percent = strchrnul(format, '%');
227 strbuf_add(sb, format, percent - format);
228 if (!*percent)
229 break;
230 format = percent + 1;
231
232 consumed = fn(sb, format, context);
233 if (consumed)
234 format += consumed;
235 else
236 strbuf_addch(sb, '%');
237 }
238}
239
240size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
241 void *context)
242{
243 struct strbuf_expand_dict_entry *e = context;
244 size_t len;
245
246 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
247 if (!strncmp(placeholder, e->placeholder, len)) {
248 if (e->value)
249 strbuf_addstr(sb, e->value);
250 return len;
251 }
252 }
253 return 0;
254}
255
256size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
257{
258 size_t res;
259 size_t oldalloc = sb->alloc;
260
261 strbuf_grow(sb, size);
262 res = fread(sb->buf + sb->len, 1, size, f);
263 if (res > 0)
264 strbuf_setlen(sb, sb->len + res);
265 else if (res < 0 && oldalloc == 0)
266 strbuf_release(sb);
267 return res;
268}
269
270ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
271{
272 size_t oldlen = sb->len;
273 size_t oldalloc = sb->alloc;
274
275 strbuf_grow(sb, hint ? hint : 8192);
276 for (;;) {
277 ssize_t cnt;
278
279 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
280 if (cnt < 0) {
281 if (oldalloc == 0)
282 strbuf_release(sb);
283 else
284 strbuf_setlen(sb, oldlen);
285 return -1;
286 }
287 if (!cnt)
288 break;
289 sb->len += cnt;
290 strbuf_grow(sb, 8192);
291 }
292
293 sb->buf[sb->len] = '\0';
294 return sb->len - oldlen;
295}
296
297#define STRBUF_MAXLINK (2*PATH_MAX)
298
299int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
300{
301 size_t oldalloc = sb->alloc;
302
303 if (hint < 32)
304 hint = 32;
305
306 while (hint < STRBUF_MAXLINK) {
307 int len;
308
309 strbuf_grow(sb, hint);
310 len = readlink(path, sb->buf, hint);
311 if (len < 0) {
312 if (errno != ERANGE)
313 break;
314 } else if (len < hint) {
315 strbuf_setlen(sb, len);
316 return 0;
317 }
318
319 /* .. the buffer was too small - try again */
320 hint *= 2;
321 }
322 if (oldalloc == 0)
323 strbuf_release(sb);
324 return -1;
325}
326
327int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
328{
329 int ch;
330
331 strbuf_grow(sb, 0);
332 if (feof(fp))
333 return EOF;
334
335 strbuf_reset(sb);
336 while ((ch = fgetc(fp)) != EOF) {
337 if (ch == term)
338 break;
339 strbuf_grow(sb, 1);
340 sb->buf[sb->len++] = ch;
341 }
342 if (ch == EOF && sb->len == 0)
343 return EOF;
344
345 sb->buf[sb->len] = '\0';
346 return 0;
347}
348
349int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
350{
351 int fd, len;
352
353 fd = open(path, O_RDONLY);
354 if (fd < 0)
355 return -1;
356 len = strbuf_read(sb, fd, hint);
357 close(fd);
358 if (len < 0)
359 return -1;
360
361 return len;
362}