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 <drafnel@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Brandon Casey committed
Aug 4, 2009 at 22:31 UTC
c7e4f0d78aaac33d5e3cec54cddd40439e77d16c
2 files changed
+13
-3
strbuf.c
+12
-3
@@ -322,7 +322,7 @@ int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint)
322
return -1;
323
}
324
325
-int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
325
+int strbuf_getwholeline(struct strbuf *sb, FILE *fp, int term)
326
{
327
int ch;
328
@@ -332,10 +332,10 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
332
333
strbuf_reset(sb);
334
while ((ch = fgetc(fp)) != EOF) {
335
- if (ch == term)
336
- break;
335
strbuf_grow(sb, 1);
336
sb->buf[sb->len++] = ch;
337
+ if (ch == term)
338
+ break;
339
}
340
if (ch == EOF && sb->len == 0)
341
return EOF;
@@ -344,6 +344,15 @@ int strbuf_getline(struct strbuf *sb, FILE *fp, int term)
344
return 0;
345
}
346
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
+
356
int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
357
{
358
int fd, len;
strbuf.h
+1
@@ -126,6 +126,7 @@ extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
126
extern int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint);
127
extern int strbuf_readlink(struct strbuf *sb, const char *path, size_t hint);
128
129
+extern int strbuf_getwholeline(struct strbuf *, FILE *, int);
130
extern int strbuf_getline(struct strbuf *, FILE *, int);
131
132
extern void stripspace(struct strbuf *buf, int skip_comments);