0d35fc0ce3fd1e684cb559b53c055c0e00ce340d
[pspp] / lib / linebuffer.c
1 /* linebuffer.c -- read arbitrarily long lines
2
3    Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003 Free Software
4    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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 /* Written by Richard Stallman. */
21 \f
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include "linebuffer.h"
30 #include "unlocked-io.h"
31 #include "xalloc.h"
32
33 /* Initialize linebuffer LINEBUFFER for use. */
34
35 void
36 initbuffer (struct linebuffer *linebuffer)
37 {
38   memset (linebuffer, 0, sizeof *linebuffer);
39 }
40
41 /* Read an arbitrarily long line of text from STREAM into LINEBUFFER.
42    Keep the newline; append a newline if it's the last line of a file
43    that ends in a non-newline character.  Do not null terminate.
44    Therefore the stream can contain NUL bytes, and the length
45    (including the newline) is returned in linebuffer->length.
46    Return NULL when stream is empty.  Return NULL and set errno upon
47    error; callers can distinguish this case from the empty case by
48    invoking ferror (stream).
49    Otherwise, return LINEBUFFER.  */
50 struct linebuffer *
51 readlinebuffer (struct linebuffer *linebuffer, FILE *stream)
52 {
53   int c;
54   char *buffer = linebuffer->buffer;
55   char *p = linebuffer->buffer;
56   char *end = buffer + linebuffer->size; /* Sentinel. */
57
58   if (feof (stream))
59     return NULL;
60
61   do
62     {
63       c = getc (stream);
64       if (c == EOF)
65         {
66           if (p == buffer || ferror (stream))
67             return NULL;
68           if (p[-1] == '\n')
69             break;
70           c = '\n';
71         }
72       if (p == end)
73         {
74           size_t oldsize = linebuffer->size;
75           buffer = x2realloc (buffer, &linebuffer->size);
76           p = buffer + oldsize;
77           linebuffer->buffer = buffer;
78           end = buffer + linebuffer->size;
79         }
80       *p++ = c;
81     }
82   while (c != '\n');
83
84   linebuffer->length = p - buffer;
85   return linebuffer;
86 }
87
88 /* Free the buffer that was allocated for linebuffer LINEBUFFER.  */
89
90 void
91 freebuffer (struct linebuffer *linebuffer)
92 {
93   free (linebuffer->buffer);
94 }