]>
Commit | Line | Data |
---|---|---|
812666c8 | 1 | #include "cache.h" |
85023577 | 2 | #include "strbuf.h" |
d1df5743 JH |
3 | |
4 | void strbuf_init(struct strbuf *sb) { | |
e99d59ff | 5 | sb->buf = NULL; |
d1df5743 JH |
6 | sb->eof = sb->alloc = sb->len = 0; |
7 | } | |
8 | ||
9 | static void strbuf_begin(struct strbuf *sb) { | |
10 | free(sb->buf); | |
11 | strbuf_init(sb); | |
12 | } | |
13 | ||
14 | static void inline strbuf_add(struct strbuf *sb, int ch) { | |
15 | if (sb->alloc <= sb->len) { | |
16 | sb->alloc = sb->alloc * 3 / 2 + 16; | |
812666c8 | 17 | sb->buf = xrealloc(sb->buf, sb->alloc); |
d1df5743 JH |
18 | } |
19 | sb->buf[sb->len++] = ch; | |
20 | } | |
21 | ||
22 | static void strbuf_end(struct strbuf *sb) { | |
23 | strbuf_add(sb, 0); | |
24 | } | |
25 | ||
26 | void read_line(struct strbuf *sb, FILE *fp, int term) { | |
27 | int ch; | |
28 | strbuf_begin(sb); | |
29 | if (feof(fp)) { | |
30 | sb->eof = 1; | |
31 | return; | |
32 | } | |
33 | while ((ch = fgetc(fp)) != EOF) { | |
34 | if (ch == term) | |
35 | break; | |
36 | strbuf_add(sb, ch); | |
37 | } | |
9dc527ad JH |
38 | if (ch == EOF && sb->len == 0) |
39 | sb->eof = 1; | |
d1df5743 JH |
40 | strbuf_end(sb); |
41 | } | |
42 |