changeset 8812:28d0ef9a0a36

* 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.
author Jim Meyering <jim@meyering.net>
date Sat, 12 May 2007 15:59:59 +0000
parents 10e5d47f0ac3
children dcdd75ded626
files ChangeLog lib/linebuffer.c lib/linebuffer.h
diffstat 3 files changed, 34 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2007-05-12  James Youngman  <jay@gnu.org>
+
+	* 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  <gray@gnu.org.ua>
 
 	* m4/openat.m4 (gl_FUNC_OPENAT): Do not require openat-die.
--- 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 @@
   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 @@
 	{
 	  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 @@
 	}
       *p++ = c;
     }
-  while (c != '\n');
+  while (c != delimiter);
 
   linebuffer->length = p - buffer;
   return linebuffer;
--- 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
@@ -35,6 +35,14 @@
 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.
    Return LINEBUFFER, except at end of file return 0.  */