1 /* linebuffer.c -- read arbitrarily long lines
2 Copyright (C) 1986, 1991, 1998, 1999 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Richard Stallman. */
25 #include "linebuffer.h"
31 /* Initialize linebuffer LINEBUFFER for use. */
34 initbuffer (struct linebuffer *linebuffer)
36 linebuffer->length = 0;
37 linebuffer->size = 200;
38 linebuffer->buffer = (char *) xmalloc (linebuffer->size);
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 but leave room for an extra byte after the newline.
45 Return LINEBUFFER, except at end of file return 0. */
48 readline (struct linebuffer *linebuffer, FILE *stream)
51 char *buffer = linebuffer->buffer;
52 char *p = linebuffer->buffer;
53 char *end = buffer + linebuffer->size - 1; /* Sentinel. */
55 if (feof (stream) || ferror (stream))
71 linebuffer->size *= 2;
72 buffer = (char *) xrealloc (buffer, linebuffer->size);
73 p = p - linebuffer->buffer + buffer;
74 linebuffer->buffer = buffer;
75 end = buffer + linebuffer->size - 1;
81 linebuffer->length = p - buffer;
85 /* Free linebuffer LINEBUFFER and its data, all allocated with malloc. */
88 freebuffer (struct linebuffer *linebuffer)
90 free (linebuffer->buffer);