]> git.ipfire.org Git - thirdparty/git.git/blame - strbuf.c
[PATCH] Introduce diff-tree-helper.
[thirdparty/git.git] / strbuf.c
CommitLineData
d1df5743
JH
1#include <stdio.h>
2#include <stdlib.h>
3#include "strbuf.h"
4
5void strbuf_init(struct strbuf *sb) {
6 sb->buf = 0;
7 sb->eof = sb->alloc = sb->len = 0;
8}
9
10static void strbuf_begin(struct strbuf *sb) {
11 free(sb->buf);
12 strbuf_init(sb);
13}
14
15static void inline strbuf_add(struct strbuf *sb, int ch) {
16 if (sb->alloc <= sb->len) {
17 sb->alloc = sb->alloc * 3 / 2 + 16;
18 sb->buf = realloc(sb->buf, sb->alloc);
19 }
20 sb->buf[sb->len++] = ch;
21}
22
23static void strbuf_end(struct strbuf *sb) {
24 strbuf_add(sb, 0);
25}
26
27void read_line(struct strbuf *sb, FILE *fp, int term) {
28 int ch;
29 strbuf_begin(sb);
30 if (feof(fp)) {
31 sb->eof = 1;
32 return;
33 }
34 while ((ch = fgetc(fp)) != EOF) {
35 if (ch == term)
36 break;
37 strbuf_add(sb, ch);
38 }
39 if (sb->len == 0)
40 sb->eof = 1;
41 strbuf_end(sb);
42}
43