Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/linebuffer.c @ 4663:f3ad8f03546c
Include <stdlib.h>.
(free): Remove decl.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 09 Sep 2003 21:51:00 +0000 |
parents | 4d31b2ff9bab |
children | c3e2b42bdca3 |
rev | line source |
---|---|
14 | 1 /* linebuffer.c -- read arbitrarily long lines |
4168
4d31b2ff9bab
(readlinebuffer): Renamed from readline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3678
diff
changeset
|
2 |
4d31b2ff9bab
(readlinebuffer): Renamed from readline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3678
diff
changeset
|
3 Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003 Free Software |
4d31b2ff9bab
(readlinebuffer): Renamed from readline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3678
diff
changeset
|
4 Foundation, Inc. |
14 | 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 | |
1464 | 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. */ | |
14 | 19 |
20 /* Written by Richard Stallman. */ | |
21 | |
1463
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
22 #ifdef HAVE_CONFIG_H |
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
23 # include <config.h> |
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
24 #endif |
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
25 |
14 | 26 #include <stdio.h> |
4663 | 27 #include <stdlib.h> |
1888
7bcd093998ec
Include <sys/types.h> now that linebuffer.h uses
Jim Meyering <jim@meyering.net>
parents:
1868
diff
changeset
|
28 #include <sys/types.h> |
14 | 29 #include "linebuffer.h" |
3618 | 30 #include "unlocked-io.h" |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
31 #include "xalloc.h" |
14 | 32 |
33 /* Initialize linebuffer LINEBUFFER for use. */ | |
34 | |
35 void | |
1465 | 36 initbuffer (struct linebuffer *linebuffer) |
14 | 37 { |
38 linebuffer->length = 0; | |
39 linebuffer->size = 200; | |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
40 linebuffer->buffer = xmalloc (linebuffer->size); |
14 | 41 } |
42 | |
43 /* Read an arbitrarily long line of text from STREAM into LINEBUFFER. | |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
44 Keep the newline; append a newline if it's the last line of a file |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
45 that ends in a non-newline character. Do not null terminate. |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
46 Therefore the stream can contain NUL bytes, and the length |
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
47 (including the newline) is returned in linebuffer->length. |
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
48 Return NULL upon error, or when STREAM is empty. |
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
49 Otherwise, return LINEBUFFER. */ |
14 | 50 struct linebuffer * |
4168
4d31b2ff9bab
(readlinebuffer): Renamed from readline.
Paul Eggert <eggert@cs.ucla.edu>
parents:
3678
diff
changeset
|
51 readlinebuffer (struct linebuffer *linebuffer, FILE *stream) |
14 | 52 { |
53 int c; | |
54 char *buffer = linebuffer->buffer; | |
55 char *p = linebuffer->buffer; | |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
56 char *end = buffer + linebuffer->size; /* Sentinel. */ |
14 | 57 |
1463
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
58 if (feof (stream) || ferror (stream)) |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
59 return NULL; |
14 | 60 |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
61 do |
14 | 62 { |
63 c = getc (stream); | |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
64 if (c == EOF) |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
65 { |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
66 if (p == buffer) |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
67 return NULL; |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
68 if (p[-1] == '\n') |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
69 break; |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
70 c = '\n'; |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
71 } |
14 | 72 if (p == end) |
73 { | |
74 linebuffer->size *= 2; | |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
75 buffer = xrealloc (buffer, linebuffer->size); |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
76 p = p - linebuffer->buffer + buffer; |
14 | 77 linebuffer->buffer = buffer; |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
78 end = buffer + linebuffer->size; |
14 | 79 } |
80 *p++ = c; | |
81 } | |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
82 while (c != '\n'); |
14 | 83 |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
84 linebuffer->length = p - buffer; |
14 | 85 return linebuffer; |
86 } | |
87 | |
88 /* Free linebuffer LINEBUFFER and its data, all allocated with malloc. */ | |
89 | |
90 void | |
1465 | 91 freebuffer (struct linebuffer *linebuffer) |
14 | 92 { |
93 free (linebuffer->buffer); | |
94 free (linebuffer); | |
95 } |