From 526b71aa90c565b1986f188b920e8c7878c34320 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Sat, 12 May 2007 15:59:59 +0000 Subject: [PATCH] * lib/linebuffer.c (readlinebuffer_delim): New function, like readlinebuffer, but use a caller-specified delimiter. (readlinebuffer): Just call readlinebuffer_delim with '\n' as the delimiter. * lib/linebuffer.h (readlinebuffer_delim): Declare it. --- ChangeLog | 8 ++++++++ lib/linebuffer.c | 26 +++++++++++++++++--------- lib/linebuffer.h | 10 +++++++++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index d8d0e479a6..235043e355 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2007-05-12 James Youngman + + * lib/linebuffer.c (readlinebuffer_delim): New function, + like readlinebuffer, but use a caller-specified delimiter. + (readlinebuffer): Just call readlinebuffer_delim with '\n' + as the delimiter. + * lib/linebuffer.h (readlinebuffer_delim): Declare it. + 2007-05-12 Sergey Poznyakoff * m4/openat.m4 (gl_FUNC_OPENAT): Do not require openat-die. diff --git a/lib/linebuffer.c b/lib/linebuffer.c index 3f1e45b285..6317271c2f 100644 --- a/lib/linebuffer.c +++ b/lib/linebuffer.c @@ -1,7 +1,7 @@ /* linebuffer.c -- read arbitrarily long lines - Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003, 2004, 2006 Free - Software Foundation, Inc. + Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003, 2004, 2006, 2007 + Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -40,17 +40,25 @@ initbuffer (struct linebuffer *linebuffer) memset (linebuffer, 0, sizeof *linebuffer); } +struct linebuffer * +readlinebuffer (struct linebuffer *linebuffer, FILE *stream) +{ + return readlinebuffer_delim (linebuffer, stream, '\n'); +} + /* Read an arbitrarily long line of text from STREAM into LINEBUFFER. - Keep the newline; append a newline if it's the last line of a file - that ends in a non-newline character. Do not null terminate. + Consder lines to be terminated by DELIMITER. + Keep the delimiter; append DELIMITER if it's the last line of a file + that ends in a character other than DELIMITER. Do not null terminate. Therefore the stream can contain NUL bytes, and the length - (including the newline) is returned in linebuffer->length. + (including the delimiter) is returned in linebuffer->length. Return NULL when stream is empty. Return NULL and set errno upon error; callers can distinguish this case from the empty case by invoking ferror (stream). Otherwise, return LINEBUFFER. */ struct linebuffer * -readlinebuffer (struct linebuffer *linebuffer, FILE *stream) +readlinebuffer_delim (struct linebuffer *linebuffer, FILE *stream, + char delimiter) { int c; char *buffer = linebuffer->buffer; @@ -67,9 +75,9 @@ readlinebuffer (struct linebuffer *linebuffer, FILE *stream) { if (p == buffer || ferror (stream)) return NULL; - if (p[-1] == '\n') + if (p[-1] == delimiter) break; - c = '\n'; + c = delimiter; } if (p == end) { @@ -81,7 +89,7 @@ readlinebuffer (struct linebuffer *linebuffer, FILE *stream) } *p++ = c; } - while (c != '\n'); + while (c != delimiter); linebuffer->length = p - buffer; return linebuffer; diff --git a/lib/linebuffer.h b/lib/linebuffer.h index 16b4b5afd1..dd62709eab 100644 --- a/lib/linebuffer.h +++ b/lib/linebuffer.h @@ -1,6 +1,6 @@ /* linebuffer.h -- declarations for reading arbitrarily long lines - Copyright (C) 1986, 1991, 1998, 1999, 2002, 2003 Free Software + Copyright (C) 1986, 1991, 1998, 1999, 2002, 2003, 2007 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify @@ -34,6 +34,14 @@ struct linebuffer /* Initialize linebuffer LINEBUFFER for use. */ void initbuffer (struct linebuffer *linebuffer); +/* Read an arbitrarily long line of text from STREAM into LINEBUFFER. + Consider lines to be terminated by DELIMITER. + Keep the delimiter; append DELIMITER if we reach EOF and it wasn't + the last character in the file. Do not null terminate. + Return LINEBUFFER, except at end of file return 0. */ +struct linebuffer *readlinebuffer_delim (struct linebuffer *linebuffer, + FILE *stream, char delimiter); + /* Read an arbitrarily long line of text from STREAM into LINEBUFFER. Keep the newline; append a newline if it's the last line of a file that ends in a non-newline character. Do not null terminate. -- 2.30.2