* lib/linebuffer.c (readlinebuffer_delim): New function,
authorJim Meyering <jim@meyering.net>
Sat, 12 May 2007 15:59:59 +0000 (15:59 +0000)
committerJim Meyering <jim@meyering.net>
Sat, 12 May 2007 15:59:59 +0000 (15:59 +0000)
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
lib/linebuffer.c
lib/linebuffer.h

index d8d0e47..235043e 100644 (file)
--- 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.
index 3f1e45b..6317271 100644 (file)
@@ -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;
index 16b4b5a..dd62709 100644 (file)
@@ -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 @@ struct linebuffer
 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.  */