Skip to content

Commit

Permalink
strbuf: add new function strbuf_getwholeline()
Browse files Browse the repository at this point in the history
This function is just like strbuf_getline() except it retains the
line-termination character.  This function will be used by the mailinfo
and mailsplit builtins which require the entire line for parsing.

Signed-off-by: Brandon Casey <drafnel@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Brandon Casey authored and Junio C Hamano committed Aug 5, 2009
1 parent 07a4a3b commit c7e4f0d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
15 changes: 12 additions & 3 deletions strbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
return -1;
}

int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
{
int ch;

Expand All @@ -332,10 +332,10 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)

strbuf_reset(sb);
while ((ch = fgetc(fp)) != EOF) {
if (ch == term)
break;
strbuf_grow(sb, 1);
sb->buf[sb->len++] = ch;
if (ch == term)
break;
}
if (ch == EOF && sb->len == 0)
return EOF;
Expand All @@ -344,6 +344,15 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
return 0;
}

int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
{
if (strbuf_getwholeline(sb, fp, term))
return EOF;
if (sb->buf[sb->len-1] == term)
strbuf_setlen(sb, sb->len-1);
return 0;
}

int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
{
int fd, len;
Expand Down
1 change: 1 addition & 0 deletions strbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);

extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
extern int strbuf_getline(struct strbuf *, FILE *, int);

extern void stripspace(struct strbuf *buf, int skip_comments);
Expand Down

0 comments on commit c7e4f0d

Please sign in to comment.