+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.
/* 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
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;
{
if (p == buffer || ferror (stream))
return NULL;
- if (p[-1] == '\n')
+ if (p[-1] == delimiter)
break;
- c = '\n';
+ c = delimiter;
}
if (p == end)
{
}
*p++ = c;
}
- while (c != '\n');
+ while (c != delimiter);
linebuffer->length = p - buffer;
return linebuffer;
/* 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
/* 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.