Merge branch 'nd/status-partial-refresh'
[git/git-svn.git] / strbuf.c
blob67448b7b07aa9d07b0c8cc250e3e0fad64c8fb5e
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;
14 * Used as the default ->buf value, so that people can always assume
15 * buf is non NULL and ->buf is NUL terminated even for a freshly
16 * initialized strbuf.
18 char strbuf_slopbuf[1];
20 void strbuf_init(struct strbuf *sb, size_t hint)
22 sb->alloc = sb->len = 0;
23 sb->buf = strbuf_slopbuf;
24 if (hint)
25 strbuf_grow(sb, hint);
28 void strbuf_release(struct strbuf *sb)
30 if (sb->alloc) {
31 free(sb->buf);
32 strbuf_init(sb, 0);
36 char *strbuf_detach(struct strbuf *sb, size_t *sz)
38 char *res = sb->alloc ? sb->buf : NULL;
39 if (sz)
40 *sz = sb->len;
41 strbuf_init(sb, 0);
42 return res;
45 void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
47 strbuf_release(sb);
48 sb->buf = buf;
49 sb->len = len;
50 sb->alloc = alloc;
51 strbuf_grow(sb, 0);
52 sb->buf[sb->len] = '\0';
55 void strbuf_grow(struct strbuf *sb, size_t extra)
57 if (sb->len + extra + 1 <= sb->len)
58 die("you want to use way too much memory");
59 if (!sb->alloc)
60 sb->buf = NULL;
61 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
64 void strbuf_trim(struct strbuf *sb)
66 char *b = sb->buf;
67 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
68 sb->len--;
69 while (sb->len > 0 && isspace(*b)) {
70 b++;
71 sb->len--;
73 memmove(sb->buf, b, sb->len);
74 sb->buf[sb->len] = '\0';
76 void strbuf_rtrim(struct strbuf *sb)
78 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
79 sb->len--;
80 sb->buf[sb->len] = '\0';
83 void strbuf_ltrim(struct strbuf *sb)
85 char *b = sb->buf;
86 while (sb->len > 0 && isspace(*b)) {
87 b++;
88 sb->len--;
90 memmove(sb->buf, b, sb->len);
91 sb->buf[sb->len] = '\0';
94 struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
96 int alloc = 2, pos = 0;
97 char *n, *p;
98 struct strbuf **ret;
99 struct strbuf *t;
101 ret = xcalloc(alloc, sizeof(struct strbuf *));
102 p = n = sb->buf;
103 while (n < sb->buf + sb->len) {
104 int len;
105 n = memchr(n, delim, sb->len - (n - sb->buf));
106 if (pos + 1 >= alloc) {
107 alloc = alloc * 2;
108 ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
110 if (!n)
111 n = sb->buf + sb->len - 1;
112 len = n - p + 1;
113 t = xmalloc(sizeof(struct strbuf));
114 strbuf_init(t, len);
115 strbuf_add(t, p, len);
116 ret[pos] = t;
117 ret[++pos] = NULL;
118 p = ++n;
120 return ret;
123 void strbuf_list_free(struct strbuf **sbs)
125 struct strbuf **s = sbs;
127 while (*s) {
128 strbuf_release(*s);
129 free(*s++);
131 free(sbs);
134 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
136 int len = a->len < b->len ? a->len: b->len;
137 int cmp = memcmp(a->buf, b->buf, len);
138 if (cmp)
139 return cmp;
140 return a->len < b->len ? -1: a->len != b->len;
143 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
144 const void *data, size_t dlen)
146 if (pos + len < pos)
147 die("you want to use way too much memory");
148 if (pos > sb->len)
149 die("`pos' is too far after the end of the buffer");
150 if (pos + len > sb->len)
151 die("`pos + len' is too far after the end of the buffer");
153 if (dlen >= len)
154 strbuf_grow(sb, dlen - len);
155 memmove(sb->buf + pos + dlen,
156 sb->buf + pos + len,
157 sb->len - pos - len);
158 memcpy(sb->buf + pos, data, dlen);
159 strbuf_setlen(sb, sb->len + dlen - len);
162 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
164 strbuf_splice(sb, pos, 0, data, len);
167 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
169 strbuf_splice(sb, pos, len, NULL, 0);
172 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
174 strbuf_grow(sb, len);
175 memcpy(sb->buf + sb->len, data, len);
176 strbuf_setlen(sb, sb->len + len);
179 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
181 strbuf_grow(sb, len);
182 memcpy(sb->buf + sb->len, sb->buf + pos, len);
183 strbuf_setlen(sb, sb->len + len);
186 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
188 int len;
189 va_list ap;
191 if (!strbuf_avail(sb))
192 strbuf_grow(sb, 64);
193 va_start(ap, fmt);
194 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
195 va_end(ap);
196 if (len < 0)
197 die("your vsnprintf is broken");
198 if (len > strbuf_avail(sb)) {
199 strbuf_grow(sb, len);
200 va_start(ap, fmt);
201 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
202 va_end(ap);
203 if (len > strbuf_avail(sb)) {
204 die("this should not happen, your snprintf is broken");
207 strbuf_setlen(sb, sb->len + len);
210 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
211 void *context)
213 for (;;) {
214 const char *percent;
215 size_t consumed;
217 percent = strchrnul(format, '%');
218 strbuf_add(sb, format, percent - format);
219 if (!*percent)
220 break;
221 format = percent + 1;
223 if (*format == '%') {
224 strbuf_addch(sb, '%');
225 format++;
226 continue;
229 consumed = fn(sb, format, context);
230 if (consumed)
231 format += consumed;
232 else
233 strbuf_addch(sb, '%');
237 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
238 void *context)
240 struct strbuf_expand_dict_entry *e = context;
241 size_t len;
243 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
244 if (!strncmp(placeholder, e->placeholder, len)) {
245 if (e->value)
246 strbuf_addstr(sb, e->value);
247 return len;
250 return 0;
253 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
255 int i, len = src->len;
257 for (i = 0; i < len; i++) {
258 if (src->buf[i] == '%')
259 strbuf_addch(dst, '%');
260 strbuf_addch(dst, src->buf[i]);
264 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
266 size_t res;
267 size_t oldalloc = sb->alloc;
269 strbuf_grow(sb, size);
270 res = fread(sb->buf + sb->len, 1, size, f);
271 if (res > 0)
272 strbuf_setlen(sb, sb->len + res);
273 else if (oldalloc == 0)
274 strbuf_release(sb);
275 return res;
278 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
280 size_t oldlen = sb->len;
281 size_t oldalloc = sb->alloc;
283 strbuf_grow(sb, hint ? hint : 8192);
284 for (;;) {
285 ssize_t cnt;
287 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
288 if (cnt < 0) {
289 if (oldalloc == 0)
290 strbuf_release(sb);
291 else
292 strbuf_setlen(sb, oldlen);
293 return -1;
295 if (!cnt)
296 break;
297 sb->len += cnt;
298 strbuf_grow(sb, 8192);
301 sb->buf[sb->len] = '\0';
302 return sb->len - oldlen;
305 #define STRBUF_MAXLINK (2*PATH_MAX)
307 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
309 size_t oldalloc = sb->alloc;
311 if (hint < 32)
312 hint = 32;
314 while (hint < STRBUF_MAXLINK) {
315 int len;
317 strbuf_grow(sb, hint);
318 len = readlink(path, sb->buf, hint);
319 if (len < 0) {
320 if (errno != ERANGE)
321 break;
322 } else if (len < hint) {
323 strbuf_setlen(sb, len);
324 return 0;
327 /* .. the buffer was too small - try again */
328 hint *= 2;
330 if (oldalloc == 0)
331 strbuf_release(sb);
332 return -1;
335 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
337 int ch;
339 strbuf_grow(sb, 0);
340 if (feof(fp))
341 return EOF;
343 strbuf_reset(sb);
344 while ((ch = fgetc(fp)) != EOF) {
345 strbuf_grow(sb, 1);
346 sb->buf[sb->len++] = ch;
347 if (ch == term)
348 break;
350 if (ch == EOF && sb->len == 0)
351 return EOF;
353 sb->buf[sb->len] = '\0';
354 return 0;
357 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
359 if (strbuf_getwholeline(sb, fp, term))
360 return EOF;
361 if (sb->buf[sb->len-1] == term)
362 strbuf_setlen(sb, sb->len-1);
363 return 0;
366 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
368 int fd, len;
370 fd = open(path, O_RDONLY);
371 if (fd < 0)
372 return -1;
373 len = strbuf_read(sb, fd, hint);
374 close(fd);
375 if (len < 0)
376 return -1;
378 return len;
381 int strbuf_branchname(struct strbuf *sb, const char *name)
383 int len = strlen(name);
384 if (interpret_branch_name(name, sb) == len)
385 return 0;
386 strbuf_add(sb, name, len);
387 return len;
390 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
392 strbuf_branchname(sb, name);
393 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
394 return check_ref_format(sb->buf);