[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Implement readlinebuffer_delim, generalizing readlinebuffer
From: |
James Youngman |
Subject: |
Implement readlinebuffer_delim, generalizing readlinebuffer |
Date: |
Sat, 12 May 2007 11:43:17 +0100 |
I have just implemented "uniq -z" (analogous with sort -z) but needed
to generalize readlinebuffer() to do so.
2007-05-12 James Youngman <address@hidden>
* lib/linebuffer.c (readlinebuffer_delim): Like
readlinebuffer, but use the delimiter the caller
specifies.
(readlinebuffer): Just call readlinebuffer_delim with '\n'
as the delimiter.
* lib/linebuffer.h (readlinebuffer_delim): Declare it.
Index: lib/linebuffer.c
===================================================================
RCS file: /sources/gnulib/gnulib/lib/linebuffer.c,v
retrieving revision 1.21
diff -u -r1.21 linebuffer.c
--- lib/linebuffer.c 13 Sep 2006 22:38:14 -0000 1.21
+++ lib/linebuffer.c 12 May 2007 10:35:19 -0000
@@ -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;
Index: lib/linebuffer.h
===================================================================
RCS file: /sources/gnulib/gnulib/lib/linebuffer.h,v
retrieving revision 1.15
diff -u -r1.15 linebuffer.h
--- lib/linebuffer.h 14 May 2005 06:03:58 -0000 1.15
+++ lib/linebuffer.h 12 May 2007 10:35:19 -0000
@@ -35,6 +35,13 @@
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. */
- Implement readlinebuffer_delim, generalizing readlinebuffer,
James Youngman <=