1 /* getline.c -- Replacement for GNU C library function getline
3 Copyright (C) 1993, 1996, 2001-2002 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
30 /* Always add at least this many bytes when extending the buffer. */
33 /* Reads up to (and including) a TERMINATOR from STREAM into *LINEPTR + OFFSET
34 (and null-terminate it). *LINEPTR is a pointer returned from new [] (or
35 NULL), pointing to *N characters of space. It is realloc'd as
36 necessary. Returns the number of characters read (not including the
37 null terminator), or -1 on error or immediate EOF.
38 NOTE: There is another getstr() function declared in <curses.h>. */
41 getstr (char **lineptr, size_t *n, FILE *stream, char terminator, size_t offset)
43 size_t nchars_avail; /* Allocated but unused chars in *LINEPTR. */
44 char *read_pos; /* Where we're reading into *LINEPTR. */
46 if (!lineptr || !n || !stream)
52 *lineptr = new char[*n];
55 nchars_avail = *n - offset;
56 read_pos = *lineptr + offset;
60 int c = getc (stream);
62 /* We always want at least one char left in the buffer, since we
63 always (unless we get an error while reading the first char)
64 NUL-terminate the line buffer. */
66 assert (*n - nchars_avail == (size_t) (read_pos - *lineptr));
74 nchars_avail = *n + *lineptr - read_pos;
75 char *new_line = new char[*n];
78 memcpy (new_line, *lineptr, read_pos - *lineptr);
82 read_pos = *n - nchars_avail + *lineptr;
83 assert (*n - nchars_avail == (size_t) (read_pos - *lineptr));
86 if (c == EOF || ferror (stream))
88 /* Return partial line, if any. */
89 if (read_pos == *lineptr)
99 /* Return the line. */
103 /* Done - NUL terminate and return the number of chars read. */
106 return read_pos - (*lineptr + offset);
110 get_line (char **lineptr, size_t *n, FILE *stream)
112 return getstr (lineptr, n, stream, '\n', 0);
116 get_delim (char **lineptr, size_t *n, int delimiter, FILE *stream)
118 return getstr (lineptr, n, stream, delimiter, 0);