1#include "cache.h"
2#include "strbuf.h"
3
4void strbuf_init(struct strbuf *sb) {
5 sb->buf = NULL;
6 sb->eof = sb->alloc = sb->len = 0;
7}
8
9static void strbuf_begin(struct strbuf *sb) {
10 free(sb->buf);
11 strbuf_init(sb);
12}
13
14static void inline strbuf_add(struct strbuf *sb, int ch) {
15 if (sb->alloc <= sb->len) {
16 sb->alloc = sb->alloc * 3 / 2 + 16;
17 sb->buf = xrealloc(sb->buf, sb->alloc);
18 }
19 sb->buf[sb->len++] = ch;
20}
21
22static void strbuf_end(struct strbuf *sb) {
23 strbuf_add(sb, 0);
24}
25
26void 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 }
38 if (ch == EOF && sb->len == 0)
39 sb->eof = 1;
40 strbuf_end(sb);
41}
42