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