2005-04-26 21:00:58 +02:00
|
|
|
#include "cache.h"
|
2006-12-19 23:34:12 +01:00
|
|
|
#include "strbuf.h"
|
2005-04-26 03:26:45 +02:00
|
|
|
|
|
|
|
void strbuf_init(struct strbuf *sb) {
|
2005-05-20 20:46:10 +02:00
|
|
|
sb->buf = NULL;
|
2005-04-26 03:26:45 +02:00
|
|
|
sb->eof = sb->alloc = sb->len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void strbuf_begin(struct strbuf *sb) {
|
|
|
|
free(sb->buf);
|
|
|
|
strbuf_init(sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void inline strbuf_add(struct strbuf *sb, int ch) {
|
|
|
|
if (sb->alloc <= sb->len) {
|
|
|
|
sb->alloc = sb->alloc * 3 / 2 + 16;
|
2005-04-26 21:00:58 +02:00
|
|
|
sb->buf = xrealloc(sb->buf, sb->alloc);
|
2005-04-26 03:26:45 +02:00
|
|
|
}
|
|
|
|
sb->buf[sb->len++] = ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void strbuf_end(struct strbuf *sb) {
|
|
|
|
strbuf_add(sb, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void read_line(struct strbuf *sb, FILE *fp, int term) {
|
|
|
|
int ch;
|
|
|
|
strbuf_begin(sb);
|
|
|
|
if (feof(fp)) {
|
|
|
|
sb->eof = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while ((ch = fgetc(fp)) != EOF) {
|
|
|
|
if (ch == term)
|
|
|
|
break;
|
|
|
|
strbuf_add(sb, ch);
|
|
|
|
}
|
2005-05-19 08:34:03 +02:00
|
|
|
if (ch == EOF && sb->len == 0)
|
|
|
|
sb->eof = 1;
|
2005-04-26 03:26:45 +02:00
|
|
|
strbuf_end(sb);
|
|
|
|
}
|