Merge branch 'mg/cvsimport'
[alt-git.git] / strbuf.c
blob9b3c4457f229041784edfa65218aa09de7a5eff8
1 #include "cache.h"
2 #include "refs.h"
4 int prefixcmp(const char *str, const char *prefix)
6 for (; ; str++, prefix++)
7 if (!*prefix)
8 return 0;
9 else if (*str != *prefix)
10 return (unsigned char)*prefix - (unsigned char)*str;
13 int suffixcmp(const char *str, const char *suffix)
15 int len = strlen(str), suflen = strlen(suffix);
16 if (len < suflen)
17 return -1;
18 else
19 return strcmp(str + len - suflen, suffix);
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.
27 char strbuf_slopbuf[1];
29 void strbuf_init(struct strbuf *sb, size_t hint)
31 sb->alloc = sb->len = 0;
32 sb->buf = strbuf_slopbuf;
33 if (hint)
34 strbuf_grow(sb, hint);
37 void strbuf_release(struct strbuf *sb)
39 if (sb->alloc) {
40 free(sb->buf);
41 strbuf_init(sb, 0);
45 char *strbuf_detach(struct strbuf *sb, size_t *sz)
47 char *res = sb->alloc ? sb->buf : NULL;
48 if (sz)
49 *sz = sb->len;
50 strbuf_init(sb, 0);
51 return res;
54 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
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';
64 void strbuf_grow(struct strbuf *sb, size_t extra)
66 if (sb->len + extra + 1 <= sb->len)
67 die("you want to use way too much memory");
68 if (!sb->alloc)
69 sb->buf = NULL;
70 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
73 void strbuf_trim(struct strbuf *sb)
75 char *b = sb->buf;
76 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
77 sb->len--;
78 while (sb->len > 0 && isspace(*b)) {
79 b++;
80 sb->len--;
82 memmove(sb->buf, b, sb->len);
83 sb->buf[sb->len] = '\0';
85 void strbuf_rtrim(struct strbuf *sb)
87 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
88 sb->len--;
89 sb->buf[sb->len] = '\0';
92 void strbuf_ltrim(struct strbuf *sb)
94 char *b = sb->buf;
95 while (sb->len > 0 && isspace(*b)) {
96 b++;
97 sb->len--;
99 memmove(sb->buf, b, sb->len);
100 sb->buf[sb->len] = '\0';
103 struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
105 int alloc = 2, pos = 0;
106 char *n, *p;
107 struct strbuf **ret;
108 struct strbuf *t;
110 ret = xcalloc(alloc, sizeof(struct strbuf *));
111 p = n = sb->buf;
112 while (n < sb->buf + sb->len) {
113 int len;
114 n = memchr(n, delim, sb->len - (n - sb->buf));
115 if (pos + 1 >= alloc) {
116 alloc = alloc * 2;
117 ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
119 if (!n)
120 n = sb->buf + sb->len - 1;
121 len = n - p + 1;
122 t = xmalloc(sizeof(struct strbuf));
123 strbuf_init(t, len);
124 strbuf_add(t, p, len);
125 ret[pos] = t;
126 ret[++pos] = NULL;
127 p = ++n;
129 return ret;
132 void strbuf_list_free(struct strbuf **sbs)
134 struct strbuf **s = sbs;
136 while (*s) {
137 strbuf_release(*s);
138 free(*s++);
140 free(sbs);
143 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
145 int len = a->len < b->len ? a->len: b->len;
146 int cmp = memcmp(a->buf, b->buf, len);
147 if (cmp)
148 return cmp;
149 return a->len < b->len ? -1: a->len != b->len;
152 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
153 const void *data, size_t dlen)
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");
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);
171 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
173 strbuf_splice(sb, pos, 0, data, len);
176 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
178 strbuf_splice(sb, pos, len, NULL, 0);
181 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
183 strbuf_grow(sb, len);
184 memcpy(sb->buf + sb->len, data, len);
185 strbuf_setlen(sb, sb->len + len);
188 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
190 strbuf_grow(sb, len);
191 memcpy(sb->buf + sb->len, sb->buf + pos, len);
192 strbuf_setlen(sb, sb->len + len);
195 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
197 int len;
198 va_list ap;
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");
216 strbuf_setlen(sb, sb->len + len);
219 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
220 void *context)
222 for (;;) {
223 const char *percent;
224 size_t consumed;
226 percent = strchrnul(format, '%');
227 strbuf_add(sb, format, percent - format);
228 if (!*percent)
229 break;
230 format = percent + 1;
232 if (*format == '%') {
233 strbuf_addch(sb, '%');
234 format++;
235 continue;
238 consumed = fn(sb, format, context);
239 if (consumed)
240 format += consumed;
241 else
242 strbuf_addch(sb, '%');
246 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
247 void *context)
249 struct strbuf_expand_dict_entry *e = context;
250 size_t len;
252 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
253 if (!strncmp(placeholder, e->placeholder, len)) {
254 if (e->value)
255 strbuf_addstr(sb, e->value);
256 return len;
259 return 0;
262 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
264 int i, len = src->len;
266 for (i = 0; i < len; i++) {
267 if (src->buf[i] == '%')
268 strbuf_addch(dst, '%');
269 strbuf_addch(dst, src->buf[i]);
273 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
275 size_t res;
276 size_t oldalloc = sb->alloc;
278 strbuf_grow(sb, size);
279 res = fread(sb->buf + sb->len, 1, size, f);
280 if (res > 0)
281 strbuf_setlen(sb, sb->len + res);
282 else if (oldalloc == 0)
283 strbuf_release(sb);
284 return res;
287 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
289 size_t oldlen = sb->len;
290 size_t oldalloc = sb->alloc;
292 strbuf_grow(sb, hint ? hint : 8192);
293 for (;;) {
294 ssize_t cnt;
296 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
297 if (cnt < 0) {
298 if (oldalloc == 0)
299 strbuf_release(sb);
300 else
301 strbuf_setlen(sb, oldlen);
302 return -1;
304 if (!cnt)
305 break;
306 sb->len += cnt;
307 strbuf_grow(sb, 8192);
310 sb->buf[sb->len] = '\0';
311 return sb->len - oldlen;
314 #define STRBUF_MAXLINK (2*PATH_MAX)
316 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
318 size_t oldalloc = sb->alloc;
320 if (hint < 32)
321 hint = 32;
323 while (hint < STRBUF_MAXLINK) {
324 int len;
326 strbuf_grow(sb, hint);
327 len = readlink(path, sb->buf, hint);
328 if (len < 0) {
329 if (errno != ERANGE)
330 break;
331 } else if (len < hint) {
332 strbuf_setlen(sb, len);
333 return 0;
336 /* .. the buffer was too small - try again */
337 hint *= 2;
339 if (oldalloc == 0)
340 strbuf_release(sb);
341 return -1;
344 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
346 int ch;
348 strbuf_grow(sb, 0);
349 if (feof(fp))
350 return EOF;
352 strbuf_reset(sb);
353 while ((ch = fgetc(fp)) != EOF) {
354 strbuf_grow(sb, 1);
355 sb->buf[sb->len++] = ch;
356 if (ch == term)
357 break;
359 if (ch == EOF && sb->len == 0)
360 return EOF;
362 sb->buf[sb->len] = '\0';
363 return 0;
366 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
368 if (strbuf_getwholeline(sb, fp, term))
369 return EOF;
370 if (sb->buf[sb->len-1] == term)
371 strbuf_setlen(sb, sb->len-1);
372 return 0;
375 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
377 int fd, len;
379 fd = open(path, O_RDONLY);
380 if (fd < 0)
381 return -1;
382 len = strbuf_read(sb, fd, hint);
383 close(fd);
384 if (len < 0)
385 return -1;
387 return len;