Drop strbuf's 'eof' marker, and make read_line a first class citizen.

read_line is now strbuf_getline, and is a first class citizen, it returns 0
when reading a line worked, EOF else.

The ->eof marker was used non-locally by fast-import.c, mimic the same
behaviour using a static int in "read_next_command", that now returns -1 on
EOF, and avoids to call strbuf_getline when it's in EOF state.

Also no longer automagically strbuf_release the buffer, it's counter
intuitive and breaks fast-import in a very subtle way.

Note: being at EOF implies that command_buf.len == 0.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Pierre Habouzit
2007-09-17 11:19:04 +02:00
committed by Junio C Hamano
parent 8b6087fb25
commit e6c019d0b0
7 changed files with 39 additions and 41 deletions

View File

@ -17,7 +17,6 @@ void strbuf_reset(struct strbuf *sb)
{
if (sb->len)
strbuf_setlen(sb, 0);
sb->eof = 0;
}
char *strbuf_detach(struct strbuf *sb)
@ -145,14 +144,13 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
return sb->len - oldlen;
}
void read_line(struct strbuf *sb, FILE *fp, int term)
int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
{
int ch;
if (feof(fp)) {
strbuf_release(sb);
sb->eof = 1;
return;
}
strbuf_grow(sb, 0);
if (feof(fp))
return EOF;
strbuf_reset(sb);
while ((ch = fgetc(fp)) != EOF) {
@ -161,11 +159,9 @@ void read_line(struct strbuf *sb, FILE *fp, int term)
strbuf_grow(sb, 1);
sb->buf[sb->len++] = ch;
}
if (ch == EOF && sb->len == 0) {
strbuf_release(sb);
sb->eof = 1;
}
if (ch == EOF && sb->len == 0)
return EOF;
strbuf_grow(sb, 1);
sb->buf[sb->len] = '\0';
return 0;
}