compat/snprintf.c: clarify SNPRINTF_SIZE_CORR
[alt-git.git] / strbuf.c
bloba6153dca278abe957254fa091fdcd8eb13b90b25
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 void strbuf_tolower(struct strbuf *sb)
96 int i;
97 for (i = 0; i < sb->len; i++)
98 sb->buf[i] = tolower(sb->buf[i]);
101 struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
103 int alloc = 2, pos = 0;
104 char *n, *p;
105 struct strbuf **ret;
106 struct strbuf *t;
108 ret = xcalloc(alloc, sizeof(struct strbuf *));
109 p = n = sb->buf;
110 while (n < sb->buf + sb->len) {
111 int len;
112 n = memchr(n, delim, sb->len - (n - sb->buf));
113 if (pos + 1 >= alloc) {
114 alloc = alloc * 2;
115 ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
117 if (!n)
118 n = sb->buf + sb->len - 1;
119 len = n - p + 1;
120 t = xmalloc(sizeof(struct strbuf));
121 strbuf_init(t, len);
122 strbuf_add(t, p, len);
123 ret[pos] = t;
124 ret[++pos] = NULL;
125 p = ++n;
127 return ret;
130 void strbuf_list_free(struct strbuf **sbs)
132 struct strbuf **s = sbs;
134 while (*s) {
135 strbuf_release(*s);
136 free(*s++);
138 free(sbs);
141 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
143 int len = a->len < b->len ? a->len: b->len;
144 int cmp = memcmp(a->buf, b->buf, len);
145 if (cmp)
146 return cmp;
147 return a->len < b->len ? -1: a->len != b->len;
150 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
151 const void *data, size_t dlen)
153 if (pos + len < pos)
154 die("you want to use way too much memory");
155 if (pos > sb->len)
156 die("`pos' is too far after the end of the buffer");
157 if (pos + len > sb->len)
158 die("`pos + len' is too far after the end of the buffer");
160 if (dlen >= len)
161 strbuf_grow(sb, dlen - len);
162 memmove(sb->buf + pos + dlen,
163 sb->buf + pos + len,
164 sb->len - pos - len);
165 memcpy(sb->buf + pos, data, dlen);
166 strbuf_setlen(sb, sb->len + dlen - len);
169 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
171 strbuf_splice(sb, pos, 0, data, len);
174 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
176 strbuf_splice(sb, pos, len, NULL, 0);
179 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
181 strbuf_grow(sb, len);
182 memcpy(sb->buf + sb->len, data, len);
183 strbuf_setlen(sb, sb->len + len);
186 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
188 strbuf_grow(sb, len);
189 memcpy(sb->buf + sb->len, sb->buf + pos, len);
190 strbuf_setlen(sb, sb->len + len);
193 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
195 int len;
196 va_list ap;
198 if (!strbuf_avail(sb))
199 strbuf_grow(sb, 64);
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 < 0)
204 die("your vsnprintf is broken");
205 if (len > strbuf_avail(sb)) {
206 strbuf_grow(sb, len);
207 va_start(ap, fmt);
208 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
209 va_end(ap);
210 if (len > strbuf_avail(sb)) {
211 die("this should not happen, your snprintf is broken");
214 strbuf_setlen(sb, sb->len + len);
217 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
218 void *context)
220 for (;;) {
221 const char *percent;
222 size_t consumed;
224 percent = strchrnul(format, '%');
225 strbuf_add(sb, format, percent - format);
226 if (!*percent)
227 break;
228 format = percent + 1;
230 consumed = fn(sb, format, context);
231 if (consumed)
232 format += consumed;
233 else
234 strbuf_addch(sb, '%');
238 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
239 void *context)
241 struct strbuf_expand_dict_entry *e = context;
242 size_t len;
244 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
245 if (!strncmp(placeholder, e->placeholder, len)) {
246 if (e->value)
247 strbuf_addstr(sb, e->value);
248 return len;
251 return 0;
254 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
256 size_t res;
257 size_t oldalloc = sb->alloc;
259 strbuf_grow(sb, size);
260 res = fread(sb->buf + sb->len, 1, size, f);
261 if (res > 0)
262 strbuf_setlen(sb, sb->len + res);
263 else if (oldalloc == 0)
264 strbuf_release(sb);
265 return res;
268 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
270 size_t oldlen = sb->len;
271 size_t oldalloc = sb->alloc;
273 strbuf_grow(sb, hint ? hint : 8192);
274 for (;;) {
275 ssize_t cnt;
277 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
278 if (cnt < 0) {
279 if (oldalloc == 0)
280 strbuf_release(sb);
281 else
282 strbuf_setlen(sb, oldlen);
283 return -1;
285 if (!cnt)
286 break;
287 sb->len += cnt;
288 strbuf_grow(sb, 8192);
291 sb->buf[sb->len] = '\0';
292 return sb->len - oldlen;
295 #define STRBUF_MAXLINK (2*PATH_MAX)
297 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
299 size_t oldalloc = sb->alloc;
301 if (hint < 32)
302 hint = 32;
304 while (hint < STRBUF_MAXLINK) {
305 int len;
307 strbuf_grow(sb, hint);
308 len = readlink(path, sb->buf, hint);
309 if (len < 0) {
310 if (errno != ERANGE)
311 break;
312 } else if (len < hint) {
313 strbuf_setlen(sb, len);
314 return 0;
317 /* .. the buffer was too small - try again */
318 hint *= 2;
320 if (oldalloc == 0)
321 strbuf_release(sb);
322 return -1;
325 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
327 int ch;
329 strbuf_grow(sb, 0);
330 if (feof(fp))
331 return EOF;
333 strbuf_reset(sb);
334 while ((ch = fgetc(fp)) != EOF) {
335 strbuf_grow(sb, 1);
336 sb->buf[sb->len++] = ch;
337 if (ch == term)
338 break;
340 if (ch == EOF && sb->len == 0)
341 return EOF;
343 sb->buf[sb->len] = '\0';
344 return 0;
347 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
349 if (strbuf_getwholeline(sb, fp, term))
350 return EOF;
351 if (sb->buf[sb->len-1] == term)
352 strbuf_setlen(sb, sb->len-1);
353 return 0;
356 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
358 int fd, len;
360 fd = open(path, O_RDONLY);
361 if (fd < 0)
362 return -1;
363 len = strbuf_read(sb, fd, hint);
364 close(fd);
365 if (len < 0)
366 return -1;
368 return len;
371 int strbuf_branchname(struct strbuf *sb, const char *name)
373 int len = strlen(name);
374 if (interpret_branch_name(name, sb) == len)
375 return 0;
376 strbuf_add(sb, name, len);
377 return len;
380 int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
382 strbuf_branchname(sb, name);
383 strbuf_splice(sb, 0, 0, "refs/heads/", 11);
384 return check_ref_format(sb->buf);