Skip to content

Commit c7e4f0d

Browse files
drafnelgitster
authored andcommitted
strbuf: add new function strbuf_getwholeline()
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 07a4a3b commit c7e4f0d

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

strbuf.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
322322
return -1;
323323
}
324324

325-
int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
325+
int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
326326
{
327327
int ch;
328328

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

333333
strbuf_reset(sb);
334334
while ((ch = fgetc(fp)) != EOF) {
335-
if (ch == term)
336-
break;
337335
strbuf_grow(sb, 1);
338336
sb->buf[sb->len++] = ch;
337+
if (ch == term)
338+
break;
339339
}
340340
if (ch == EOF && sb->len == 0)
341341
return EOF;
@@ -344,6 +344,15 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
344344
return 0;
345345
}
346346

347+
int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
348+
{
349+
if (strbuf_getwholeline(sb, fp, term))
350+
return EOF;
351+
if (sb->buf[sb->len-1] == term)
352+
strbuf_setlen(sb, sb->len-1);
353+
return 0;
354+
}
355+
347356
int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
348357
{
349358
int fd, len;

strbuf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
126126
extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
127127
extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
128128

129+
extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
129130
extern int strbuf_getline(struct strbuf *, FILE *, int);
130131

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

0 commit comments

Comments
 (0)