* lib/linebuffer.c (readlinebuffer_delim): New function,
[gnulib.git] / lib / linebuffer.c
1 /* linebuffer.c -- read arbitrarily long lines
2
3    Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003, 2004, 2006, 2007
4    Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* Written by Richard Stallman. */
21
22 #include <config.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include "linebuffer.h"
29 #include "xalloc.h"
30
31 #if USE_UNLOCKED_IO
32 # include "unlocked-io.h"
33 #endif
34
35 /* Initialize linebuffer LINEBUFFER for use. */
36
37 void
38 initbuffer (struct linebuffer *linebuffer)
39 {
40   memset (linebuffer, 0, sizeof *linebuffer);
41 }
42
43 struct linebuffer *
44 readlinebuffer (struct linebuffer *linebuffer, FILE *stream)
45 {
46   return readlinebuffer_delim (linebuffer, stream, '\n');
47 }
48
49 /* Read an arbitrarily long line of text from STREAM into LINEBUFFER.
50    Consder lines to be terminated by DELIMITER.
51    Keep the delimiter; append DELIMITER if it's the last line of a file
52    that ends in a character other than DELIMITER.  Do not null terminate.
53    Therefore the stream can contain NUL bytes, and the length
54    (including the delimiter) is returned in linebuffer->length.
55    Return NULL when stream is empty.  Return NULL and set errno upon
56    error; callers can distinguish this case from the empty case by
57    invoking ferror (stream).
58    Otherwise, return LINEBUFFER.  */
59 struct linebuffer *
60 readlinebuffer_delim (struct linebuffer *linebuffer, FILE *stream,
61                       char delimiter)
62 {
63   int c;
64   char *buffer = linebuffer->buffer;
65   char *p = linebuffer->buffer;
66   char *end = buffer + linebuffer->size; /* Sentinel. */
67
68   if (feof (stream))
69     return NULL;
70
71   do
72     {
73       c = getc (stream);
74       if (c == EOF)
75         {
76           if (p == buffer || ferror (stream))
77             return NULL;
78           if (p[-1] == delimiter)
79             break;
80           c = delimiter;
81         }
82       if (p == end)
83         {
84           size_t oldsize = linebuffer->size;
85           buffer = x2realloc (buffer, &linebuffer->size);
86           p = buffer + oldsize;
87           linebuffer->buffer = buffer;
88           end = buffer + linebuffer->size;
89         }
90       *p++ = c;
91     }
92   while (c != delimiter);
93
94   linebuffer->length = p - buffer;
95   return linebuffer;
96 }
97
98 /* Free the buffer that was allocated for linebuffer LINEBUFFER.  */
99
100 void
101 freebuffer (struct linebuffer *linebuffer)
102 {
103   free (linebuffer->buffer);
104 }