GIT v1.5.0-rc3
[git/mingw/4msysgit/kblees.git] / strbuf.c
blob7f14b0fb59bd7e14979b002f441ae84ff5b0e9a2
1 #include "cache.h"
2 #include "strbuf.h"
4 void strbuf_init(struct strbuf *sb) {
5 sb->buf = NULL;
6 sb->eof = sb->alloc = sb->len = 0;
9 static void strbuf_begin(struct strbuf *sb) {
10 free(sb->buf);
11 strbuf_init(sb);
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;
17 sb->buf = xrealloc(sb->buf, sb->alloc);
19 sb->buf[sb->len++] = ch;
22 static void strbuf_end(struct strbuf *sb) {
23 strbuf_add(sb, 0);
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;
33 while ((ch = fgetc(fp)) != EOF) {
34 if (ch == term)
35 break;
36 strbuf_add(sb, ch);
38 if (ch == EOF && sb->len == 0)
39 sb->eof = 1;
40 strbuf_end(sb);