Skip to content
Newer
Older
100644 491 lines (429 sloc) 9.74 KB
812666c [PATCH] introduce xmalloc and xrealloc
Christopher Li authored Apr 26, 2005
1 #include "cache.h"
a2fab53 @gitster strbuf_check_branch_ref(): a helper to check a refname for a branch
authored Mar 21, 2009
2 #include "refs.h"
d1df574 [PATCH] Introduce diff-tree-helper.
Junio C Hamano authored Apr 25, 2005
3
698a68b @gitster Uninline prefixcmp()
authored Jan 3, 2008
4 int 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
8cc5b29 @apenwarr git merge -X<option>
apenwarr authored Nov 25, 2009
13 int 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
b315c5c strbuf change: be sure ->buf is never ever NULL.
Pierre Habouzit authored Sep 27, 2007
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 */
27 char strbuf_slopbuf[1];
28
917c9a7 New strbuf APIs: splice and attach.
Pierre Habouzit authored Sep 15, 2007
29 void strbuf_init(struct strbuf *sb, size_t hint)
30 {
b315c5c strbuf change: be sure ->buf is never ever NULL.
Pierre Habouzit authored Sep 27, 2007
31 sb->alloc = sb->len = 0;
32 sb->buf = strbuf_slopbuf;
8c74ef1 @trast strbuf_grow(): maintain nul-termination even for new buffer
trast authored Aug 29, 2011
33 if (hint)
f1696ee Strbuf API extensions and fixes.
Pierre Habouzit authored Sep 10, 2007
34 strbuf_grow(sb, hint);
d1df574 [PATCH] Introduce diff-tree-helper.
Junio C Hamano authored Apr 26, 2005
35 }
36
917c9a7 New strbuf APIs: splice and attach.
Pierre Habouzit authored Sep 15, 2007
37 void strbuf_release(struct strbuf *sb)
38 {
b315c5c strbuf change: be sure ->buf is never ever NULL.
Pierre Habouzit authored Sep 27, 2007
39 if (sb->alloc) {
40 free(sb->buf);
41 strbuf_init(sb, 0);
42 }
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
43 }
44
b315c5c strbuf change: be sure ->buf is never ever NULL.
Pierre Habouzit authored Sep 27, 2007
45 char *strbuf_detach(struct strbuf *sb, size_t *sz)
917c9a7 New strbuf APIs: splice and attach.
Pierre Habouzit authored Sep 15, 2007
46 {
b315c5c strbuf change: be sure ->buf is never ever NULL.
Pierre Habouzit authored Sep 27, 2007
47 char *res = sb->alloc ? sb->buf : NULL;
48 if (sz)
49 *sz = sb->len;
f1696ee Strbuf API extensions and fixes.
Pierre Habouzit authored Sep 10, 2007
50 strbuf_init(sb, 0);
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
51 return res;
52 }
53
917c9a7 New strbuf APIs: splice and attach.
Pierre Habouzit authored Sep 15, 2007
54 void 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
64 void strbuf_grow(struct strbuf *sb, size_t extra)
65 {
8c74ef1 @trast strbuf_grow(): maintain nul-termination even for new buffer
trast authored Aug 29, 2011
66 int new_buf = !sb->alloc;
1368f65 @jrn compat: helper for detecting unsigned overflow
jrn authored Oct 10, 2010
67 if (unsigned_add_overflows(extra, 1) ||
68 unsigned_add_overflows(sb->len, extra + 1))
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
69 die("you want to use way too much memory");
8c74ef1 @trast strbuf_grow(): maintain nul-termination even for new buffer
trast authored Aug 29, 2011
70 if (new_buf)
b315c5c strbuf change: be sure ->buf is never ever NULL.
Pierre Habouzit authored Sep 27, 2007
71 sb->buf = NULL;
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
72 ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
8c74ef1 @trast strbuf_grow(): maintain nul-termination even for new buffer
trast authored Aug 29, 2011
73 if (new_buf)
74 sb->buf[0] = '\0';
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
75 }
76
eacd6dc Add some useful functions for strbuf manipulation.
Lukas Sandström authored Jul 13, 2008
77 void strbuf_trim(struct strbuf *sb)
78 {
79 char *b = sb->buf;
80 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
81 sb->len--;
82 while (sb->len > 0 && isspace(*b)) {
83 b++;
84 sb->len--;
85 }
86 memmove(sb->buf, b, sb->len);
87 sb->buf[sb->len] = '\0';
88 }
f1696ee Strbuf API extensions and fixes.
Pierre Habouzit authored Sep 10, 2007
89 void strbuf_rtrim(struct strbuf *sb)
90 {
91 while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
92 sb->len--;
93 sb->buf[sb->len] = '\0';
94 }
95
eacd6dc Add some useful functions for strbuf manipulation.
Lukas Sandström authored Jul 13, 2008
96 void strbuf_ltrim(struct strbuf *sb)
97 {
98 char *b = sb->buf;
99 while (sb->len > 0 && isspace(*b)) {
100 b++;
101 sb->len--;
102 }
103 memmove(sb->buf, b, sb->len);
104 sb->buf[sb->len] = '\0';
105 }
106
2f1d9e2 @peff strbuf: allow strbuf_split to work on non-strbufs
peff authored Jun 9, 2011
107 struct strbuf **strbuf_split_buf(const char *str, size_t slen, int delim, int max)
eacd6dc Add some useful functions for strbuf manipulation.
Lukas Sandström authored Jul 13, 2008
108 {
109 int alloc = 2, pos = 0;
2f1d9e2 @peff strbuf: allow strbuf_split to work on non-strbufs
peff authored Jun 9, 2011
110 const char *n, *p;
eacd6dc Add some useful functions for strbuf manipulation.
Lukas Sandström authored Jul 13, 2008
111 struct strbuf **ret;
112 struct strbuf *t;
113
114 ret = xcalloc(alloc, sizeof(struct strbuf *));
2f1d9e2 @peff strbuf: allow strbuf_split to work on non-strbufs
peff authored Jun 9, 2011
115 p = n = str;
116 while (n < str + slen) {
eacd6dc Add some useful functions for strbuf manipulation.
Lukas Sandström authored Jul 13, 2008
117 int len;
28fc3a6 @peff strbuf_split: add a max parameter
peff authored Jun 9, 2011
118 if (max <= 0 || pos + 1 < max)
2f1d9e2 @peff strbuf: allow strbuf_split to work on non-strbufs
peff authored Jun 9, 2011
119 n = memchr(n, delim, slen - (n - str));
28fc3a6 @peff strbuf_split: add a max parameter
peff authored Jun 9, 2011
120 else
121 n = NULL;
eacd6dc Add some useful functions for strbuf manipulation.
Lukas Sandström authored Jul 13, 2008
122 if (pos + 1 >= alloc) {
123 alloc = alloc * 2;
124 ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
125 }
126 if (!n)
2f1d9e2 @peff strbuf: allow strbuf_split to work on non-strbufs
peff authored Jun 9, 2011
127 n = str + slen - 1;
eacd6dc Add some useful functions for strbuf manipulation.
Lukas Sandström authored Jul 13, 2008
128 len = n - p + 1;
129 t = xmalloc(sizeof(struct strbuf));
130 strbuf_init(t, len);
131 strbuf_add(t, p, len);
132 ret[pos] = t;
133 ret[++pos] = NULL;
134 p = ++n;
135 }
136 return ret;
137 }
138
139 void strbuf_list_free(struct strbuf **sbs)
140 {
141 struct strbuf **s = sbs;
142
143 while (*s) {
144 strbuf_release(*s);
145 free(*s++);
146 }
147 free(sbs);
148 }
149
9b200fd Make some strbuf_*() struct strbuf arguments const.
Lukas Sandström authored Jul 13, 2008
150 int strbuf_cmp(const struct strbuf *a, const struct strbuf *b)
45f66f6 Add strbuf_cmp.
Pierre Habouzit authored Sep 24, 2007
151 {
8f02465 @raalkml Microoptimize strbuf_cmp
raalkml authored Mar 19, 2009
152 int len = a->len < b->len ? a->len: b->len;
153 int cmp = memcmp(a->buf, b->buf, len);
154 if (cmp)
155 return cmp;
156 return a->len < b->len ? -1: a->len != b->len;
45f66f6 Add strbuf_cmp.
Pierre Habouzit authored Sep 24, 2007
157 }
158
917c9a7 New strbuf APIs: splice and attach.
Pierre Habouzit authored Sep 15, 2007
159 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
160 const void *data, size_t dlen)
161 {
1368f65 @jrn compat: helper for detecting unsigned overflow
jrn authored Oct 11, 2010
162 if (unsigned_add_overflows(pos, len))
917c9a7 New strbuf APIs: splice and attach.
Pierre Habouzit authored Sep 15, 2007
163 die("you want to use way too much memory");
164 if (pos > sb->len)
165 die("`pos' is too far after the end of the buffer");
166 if (pos + len > sb->len)
167 die("`pos + len' is too far after the end of the buffer");
168
169 if (dlen >= len)
170 strbuf_grow(sb, dlen - len);
171 memmove(sb->buf + pos + dlen,
172 sb->buf + pos + len,
173 sb->len - pos - len);
174 memcpy(sb->buf + pos, data, dlen);
175 strbuf_setlen(sb, sb->len + dlen - len);
176 }
177
c76689d strbuf API additions and enhancements.
Pierre Habouzit authored Sep 20, 2007
178 void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
179 {
180 strbuf_splice(sb, pos, 0, data, len);
181 }
182
183 void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
184 {
185 strbuf_splice(sb, pos, len, NULL, 0);
186 }
187
917c9a7 New strbuf APIs: splice and attach.
Pierre Habouzit authored Sep 15, 2007
188 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
189 {
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
190 strbuf_grow(sb, len);
191 memcpy(sb->buf + sb->len, data, len);
192 strbuf_setlen(sb, sb->len + len);
193 }
194
91db267 add strbuf_adddup()
René Scharfe authored Nov 10, 2007
195 void strbuf_adddup(struct strbuf *sb, size_t pos, size_t len)
196 {
197 strbuf_grow(sb, len);
198 memcpy(sb->buf + sb->len, sb->buf + pos, len);
199 strbuf_setlen(sb, sb->len + len);
200 }
201
917c9a7 New strbuf APIs: splice and attach.
Pierre Habouzit authored Sep 15, 2007
202 void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
203 {
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
204 va_list ap;
ebeb609 @peff strbuf: add strbuf_vaddf
peff authored Feb 25, 2011
205 va_start(ap, fmt);
206 strbuf_vaddf(sb, fmt, ap);
207 va_end(ap);
208 }
209
210 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
211 {
212 int len;
213 va_list cp;
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
214
f141bd8 @spearce Handle broken vsnprintf implementations in strbuf
spearce authored Nov 13, 2007
215 if (!strbuf_avail(sb))
216 strbuf_grow(sb, 64);
ebeb609 @peff strbuf: add strbuf_vaddf
peff authored Feb 26, 2011
217 va_copy(cp, ap);
218 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, cp);
219 va_end(cp);
f141bd8 @spearce Handle broken vsnprintf implementations in strbuf
spearce authored Nov 13, 2007
220 if (len < 0)
ebeb609 @peff strbuf: add strbuf_vaddf
peff authored Feb 26, 2011
221 die("BUG: your vsnprintf is broken (returned %d)", len);
f1696ee Strbuf API extensions and fixes.
Pierre Habouzit authored Sep 10, 2007
222 if (len > strbuf_avail(sb)) {
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
223 strbuf_grow(sb, len);
224 len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
ebeb609 @peff strbuf: add strbuf_vaddf
peff authored Feb 26, 2011
225 if (len > strbuf_avail(sb))
226 die("BUG: your vsnprintf is broken (insatiable)");
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
227 }
228 strbuf_setlen(sb, sb->len + len);
d1df574 [PATCH] Introduce diff-tree-helper.
Junio C Hamano authored Apr 26, 2005
229 }
230
c3a670d @mcostalba Avoid a useless prefix lookup in strbuf_expand()
mcostalba authored Feb 9, 2008
231 void strbuf_expand(struct strbuf *sb, const char *format, expand_fn_t fn,
232 void *context)
cde75e5 --pretty=format: on-demand format expansion
René Scharfe authored Nov 9, 2007
233 {
234 for (;;) {
c3a670d @mcostalba Avoid a useless prefix lookup in strbuf_expand()
mcostalba authored Feb 9, 2008
235 const char *percent;
236 size_t consumed;
cde75e5 --pretty=format: on-demand format expansion
René Scharfe authored Nov 9, 2007
237
238 percent = strchrnul(format, '%');
239 strbuf_add(sb, format, percent - format);
240 if (!*percent)
241 break;
242 format = percent + 1;
243
0a0416a @peff strbuf_expand: convert "%%" to "%"
peff authored Jan 13, 2010
244 if (*format == '%') {
245 strbuf_addch(sb, '%');
246 format++;
247 continue;
248 }
249
c3a670d @mcostalba Avoid a useless prefix lookup in strbuf_expand()
mcostalba authored Feb 9, 2008
250 consumed = fn(sb, format, context);
251 if (consumed)
252 format += consumed;
253 else
cde75e5 --pretty=format: on-demand format expansion
René Scharfe authored Nov 9, 2007
254 strbuf_addch(sb, '%');
255 }
256 }
257
9b864e7 add strbuf_expand_dict_cb(), a helper for simple cases
René Scharfe authored Nov 23, 2008
258 size_t strbuf_expand_dict_cb(struct strbuf *sb, const char *placeholder,
259 void *context)
260 {
261 struct strbuf_expand_dict_entry *e = context;
262 size_t len;
263
264 for (; e->placeholder && (len = strlen(e->placeholder)); e++) {
265 if (!strncmp(placeholder, e->placeholder, len)) {
266 if (e->value)
267 strbuf_addstr(sb, e->value);
268 return len;
269 }
270 }
271 return 0;
272 }
273
361df5d @peff strbuf: add strbuf_addbuf_percentquote
peff authored Jan 13, 2010
274 void strbuf_addbuf_percentquote(struct strbuf *dst, const struct strbuf *src)
275 {
276 int i, len = src->len;
277
278 for (i = 0; i < len; i++) {
279 if (src->buf[i] == '%')
280 strbuf_addch(dst, '%');
281 strbuf_addch(dst, src->buf[i]);
282 }
283 }
284
917c9a7 New strbuf APIs: splice and attach.
Pierre Habouzit authored Sep 15, 2007
285 size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
286 {
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
287 size_t res;
2fc6470 strbuf: instate cleanup rule in case of non-memory errors
René Scharfe authored Jan 6, 2009
288 size_t oldalloc = sb->alloc;
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
289
290 strbuf_grow(sb, size);
291 res = fread(sb->buf + sb->len, 1, size, f);
2fc6470 strbuf: instate cleanup rule in case of non-memory errors
René Scharfe authored Jan 6, 2009
292 if (res > 0)
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
293 strbuf_setlen(sb, sb->len + res);
6651c3f @RoelKluin fread does not return negative on error
RoelKluin authored Jun 22, 2009
294 else if (oldalloc == 0)
2fc6470 strbuf: instate cleanup rule in case of non-memory errors
René Scharfe authored Jan 6, 2009
295 strbuf_release(sb);
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
296 return res;
d1df574 [PATCH] Introduce diff-tree-helper.
Junio C Hamano authored Apr 26, 2005
297 }
298
f1696ee Strbuf API extensions and fixes.
Pierre Habouzit authored Sep 10, 2007
299 ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
300 {
301 size_t oldlen = sb->len;
2fc6470 strbuf: instate cleanup rule in case of non-memory errors
René Scharfe authored Jan 6, 2009
302 size_t oldalloc = sb->alloc;
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
303
f1696ee Strbuf API extensions and fixes.
Pierre Habouzit authored Sep 10, 2007
304 strbuf_grow(sb, hint ? hint : 8192);
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
305 for (;;) {
306 ssize_t cnt;
307
308 cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
309 if (cnt < 0) {
2fc6470 strbuf: instate cleanup rule in case of non-memory errors
René Scharfe authored Jan 6, 2009
310 if (oldalloc == 0)
311 strbuf_release(sb);
312 else
313 strbuf_setlen(sb, oldlen);
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
314 return -1;
315 }
316 if (!cnt)
317 break;
318 sb->len += cnt;
f1696ee Strbuf API extensions and fixes.
Pierre Habouzit authored Sep 10, 2007
319 strbuf_grow(sb, 8192);
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
320 }
321
322 sb->buf[sb->len] = '\0';
323 return sb->len - oldlen;
d1df574 [PATCH] Introduce diff-tree-helper.
Junio C Hamano authored Apr 26, 2005
324 }
325
b11b7e1 @torvalds Add generic 'strbuf_readlink()' helper function
torvalds authored Dec 17, 2008
326 #define STRBUF_MAXLINK (2*PATH_MAX)
327
328 int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
329 {
2fc6470 strbuf: instate cleanup rule in case of non-memory errors
René Scharfe authored Jan 6, 2009
330 size_t oldalloc = sb->alloc;
331
b11b7e1 @torvalds Add generic 'strbuf_readlink()' helper function
torvalds authored Dec 17, 2008
332 if (hint < 32)
333 hint = 32;
334
335 while (hint < STRBUF_MAXLINK) {
336 int len;
337
338 strbuf_grow(sb, hint);
339 len = readlink(path, sb->buf, hint);
340 if (len < 0) {
341 if (errno != ERANGE)
342 break;
343 } else if (len < hint) {
344 strbuf_setlen(sb, len);
345 return 0;
346 }
347
348 /* .. the buffer was too small - try again */
349 hint *= 2;
350 }
2fc6470 strbuf: instate cleanup rule in case of non-memory errors
René Scharfe authored Jan 6, 2009
351 if (oldalloc == 0)
352 strbuf_release(sb);
b11b7e1 @torvalds Add generic 'strbuf_readlink()' helper function
torvalds authored Dec 17, 2008
353 return -1;
354 }
355
c7e4f0d @drafnel strbuf: add new function strbuf_getwholeline()
drafnel authored Aug 4, 2009
356 int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
917c9a7 New strbuf APIs: splice and attach.
Pierre Habouzit authored Sep 15, 2007
357 {
d1df574 [PATCH] Introduce diff-tree-helper.
Junio C Hamano authored Apr 26, 2005
358 int ch;
e6c019d Drop strbuf's 'eof' marker, and make read_line a first class citizen.
Pierre Habouzit authored Sep 17, 2007
359
360 if (feof(fp))
361 return EOF;
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
362
363 strbuf_reset(sb);
d1df574 [PATCH] Introduce diff-tree-helper.
Junio C Hamano authored Apr 26, 2005
364 while ((ch = fgetc(fp)) != EOF) {
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
365 strbuf_grow(sb, 1);
366 sb->buf[sb->len++] = ch;
c7e4f0d @drafnel strbuf: add new function strbuf_getwholeline()
drafnel authored Aug 5, 2009
367 if (ch == term)
368 break;
d1df574 [PATCH] Introduce diff-tree-helper.
Junio C Hamano authored Apr 26, 2005
369 }
e6c019d Drop strbuf's 'eof' marker, and make read_line a first class citizen.
Pierre Habouzit authored Sep 17, 2007
370 if (ch == EOF && sb->len == 0)
371 return EOF;
b449f4c Rework strbuf API and semantics.
Pierre Habouzit authored Sep 6, 2007
372
373 sb->buf[sb->len] = '\0';
e6c019d Drop strbuf's 'eof' marker, and make read_line a first class citizen.
Pierre Habouzit authored Sep 17, 2007
374 return 0;
d1df574 [PATCH] Introduce diff-tree-helper.
Junio C Hamano authored Apr 26, 2005
375 }
a9390b9 Add strbuf_read_file().
Kristian Høgsberg authored Sep 17, 2007
376
c7e4f0d @drafnel strbuf: add new function strbuf_getwholeline()
drafnel authored Aug 5, 2009
377 int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
378 {
379 if (strbuf_getwholeline(sb, fp, term))
380 return EOF;
381 if (sb->buf[sb->len-1] == term)
382 strbuf_setlen(sb, sb->len-1);
383 return 0;
384 }
385
5e8617f @trast bundle: put strbuf_readline_fd in strbuf.c with adjustments
trast authored Feb 22, 2012
386 int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term)
387 {
388 strbuf_reset(sb);
389
390 while (1) {
391 char ch;
392 ssize_t len = xread(fd, &ch, 1);
393 if (len <= 0)
394 return EOF;
395 strbuf_addch(sb, ch);
396 if (ch == term)
397 break;
398 }
399 return 0;
400 }
401
387e7e1 strbuf_read_file enhancement, and use it.
Pierre Habouzit authored Sep 27, 2007
402 int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
a9390b9 Add strbuf_read_file().
Kristian Høgsberg authored Sep 18, 2007
403 {
404 int fd, len;
405
406 fd = open(path, O_RDONLY);
407 if (fd < 0)
408 return -1;
387e7e1 strbuf_read_file enhancement, and use it.
Pierre Habouzit authored Sep 27, 2007
409 len = strbuf_read(sb, fd, hint);
a9390b9 Add strbuf_read_file().
Kristian Høgsberg authored Sep 18, 2007
410 close(fd);
411 if (len < 0)
412 return -1;
413
414 return len;
415 }
895680f @gitster fmt-merge-msg: Add contents of merged tag in the merge message
authored Nov 4, 2011
416
417 void strbuf_add_lines(struct strbuf *out, const char *prefix,
418 const char *buf, size_t size)
419 {
420 while (size) {
421 const char *next = memchr(buf, '\n', size);
422 next = next ? (next + 1) : (buf + size);
423 strbuf_addstr(out, prefix);
424 strbuf_add(out, buf, next - buf);
425 size -= next - buf;
426 buf = next;
427 }
428 strbuf_complete_line(out);
429 }
367d20e @gitster Merge branch 'jk/credentials'
authored Dec 19, 2011
430
c505116 @peff strbuf: add strbuf_add*_urlencode
peff authored Dec 10, 2011
431 static int is_rfc3986_reserved(char ch)
432 {
433 switch (ch) {
434 case '!': case '*': case '\'': case '(': case ')': case ';':
435 case ':': case '@': case '&': case '=': case '+': case '$':
436 case ',': case '/': case '?': case '#': case '[': case ']':
437 return 1;
438 }
439 return 0;
440 }
441
442 static int is_rfc3986_unreserved(char ch)
443 {
444 return isalnum(ch) ||
445 ch == '-' || ch == '_' || ch == '.' || ch == '~';
446 }
447
ea03a8e @gitster strbuf.c: mark a private file-scope symbol as static
authored Sep 15, 2012
448 static void strbuf_add_urlencode(struct strbuf *sb, const char *s, size_t len,
449 int reserved)
c505116 @peff strbuf: add strbuf_add*_urlencode
peff authored Dec 10, 2011
450 {
451 strbuf_grow(sb, len);
452 while (len--) {
453 char ch = *s++;
454 if (is_rfc3986_unreserved(ch) ||
455 (!reserved && is_rfc3986_reserved(ch)))
456 strbuf_addch(sb, ch);
457 else
458 strbuf_addf(sb, "%%%02x", ch);
459 }
460 }
461
462 void strbuf_addstr_urlencode(struct strbuf *sb, const char *s,
463 int reserved)
464 {
465 strbuf_add_urlencode(sb, s, strlen(s), reserved);
466 }
9a0a30a @pclouds strbuf: convenience format functions with \n automatically appended
pclouds authored Apr 23, 2012
467
468 int printf_ln(const char *fmt, ...)
469 {
470 int ret;
471 va_list ap;
472 va_start(ap, fmt);
473 ret = vprintf(fmt, ap);
474 va_end(ap);
475 if (ret < 0 || putchar('\n') == EOF)
476 return -1;
477 return ret + 1;
478 }
479
480 int fprintf_ln(FILE *fp, const char *fmt, ...)
481 {
482 int ret;
483 va_list ap;
484 va_start(ap, fmt);
485 ret = vfprintf(fp, fmt, ap);
486 va_end(ap);
487 if (ret < 0 || putc('\n', fp) == EOF)
488 return -1;
489 return ret + 1;
490 }
Something went wrong with that request. Please try again.