Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/linebuffer.c @ 8816:b8edbad1b48a
Tweak doc.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Mon, 14 May 2007 21:36:29 +0000 |
parents | 28d0ef9a0a36 |
children | bbbbbf4cd1c5 |
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 |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
3 Copyright (C) 1986, 1991, 1998, 1999, 2001, 2003, 2004, 2006, 2007 |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
4 Free Software 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, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5318
diff
changeset
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
14 | 19 |
20 /* Written by Richard Stallman. */ | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
21 |
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
22 #include <config.h> |
1463
f8f00a1e2989
(readline): Return zero upon error as well as upon
Jim Meyering <jim@meyering.net>
parents:
14
diff
changeset
|
23 |
14 | 24 #include <stdio.h> |
4663 | 25 #include <stdlib.h> |
4835
a90f003e26e9
Include <string.h> for declaration of memset.
Jim Meyering <jim@meyering.net>
parents:
4827
diff
changeset
|
26 #include <string.h> |
1888
7bcd093998ec
Include <sys/types.h> now that linebuffer.h uses
Jim Meyering <jim@meyering.net>
parents:
1868
diff
changeset
|
27 #include <sys/types.h> |
14 | 28 #include "linebuffer.h" |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
29 #include "xalloc.h" |
14 | 30 |
5318
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
31 #if USE_UNLOCKED_IO |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
32 # include "unlocked-io.h" |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
33 #endif |
7c24a825b51d
Remove dependencies on unlocked-io.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4835
diff
changeset
|
34 |
14 | 35 /* Initialize linebuffer LINEBUFFER for use. */ |
36 | |
37 void | |
1465 | 38 initbuffer (struct linebuffer *linebuffer) |
14 | 39 { |
4827
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
40 memset (linebuffer, 0, sizeof *linebuffer); |
14 | 41 } |
42 | |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
43 struct linebuffer * |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
44 readlinebuffer (struct linebuffer *linebuffer, FILE *stream) |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
45 { |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
46 return readlinebuffer_delim (linebuffer, stream, '\n'); |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
47 } |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
48 |
14 | 49 /* Read an arbitrarily long line of text from STREAM into LINEBUFFER. |
8816 | 50 Consider lines to be terminated by DELIMITER. |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
51 Keep the delimiter; append DELIMITER if it's the last line of a file |
8816 | 52 that ends in a character other than DELIMITER. Do not NUL-terminate. |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
53 Therefore the stream can contain NUL bytes, and the length |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
54 (including the delimiter) is returned in linebuffer->length. |
4713
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
55 Return NULL when stream is empty. Return NULL and set errno upon |
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
56 error; callers can distinguish this case from the empty case by |
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
57 invoking ferror (stream). |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
58 Otherwise, return LINEBUFFER. */ |
14 | 59 struct linebuffer * |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
60 readlinebuffer_delim (struct linebuffer *linebuffer, FILE *stream, |
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
61 char delimiter) |
14 | 62 { |
63 int c; | |
64 char *buffer = linebuffer->buffer; | |
65 char *p = linebuffer->buffer; | |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
66 char *end = buffer + linebuffer->size; /* Sentinel. */ |
14 | 67 |
4713
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
68 if (feof (stream)) |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
69 return NULL; |
14 | 70 |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
71 do |
14 | 72 { |
73 c = getc (stream); | |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
74 if (c == EOF) |
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
75 { |
4713
c3e2b42bdca3
linebuffer.c (readlinebuffer): Return NULL immediately upon input error.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4663
diff
changeset
|
76 if (p == buffer || ferror (stream)) |
3678
72b3b19fece2
Remove explicit declarations of xmalloc and xrealloc,
Jim Meyering <jim@meyering.net>
parents:
3618
diff
changeset
|
77 return NULL; |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
78 if (p[-1] == delimiter) |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
79 break; |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
80 c = delimiter; |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
81 } |
14 | 82 if (p == end) |
83 { | |
4827
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
84 size_t oldsize = linebuffer->size; |
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
85 buffer = x2realloc (buffer, &linebuffer->size); |
a6d03da0fa67
Simplify the code by using new xalloc.h features.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4727
diff
changeset
|
86 p = buffer + oldsize; |
14 | 87 linebuffer->buffer = buffer; |
1991
e7ed29061994
(readline): Do not leave room for an extra
Jim Meyering <jim@meyering.net>
parents:
1888
diff
changeset
|
88 end = buffer + linebuffer->size; |
14 | 89 } |
90 *p++ = c; | |
91 } | |
8812
28d0ef9a0a36
* lib/linebuffer.c (readlinebuffer_delim): New function,
Jim Meyering <jim@meyering.net>
parents:
7302
diff
changeset
|
92 while (c != delimiter); |
14 | 93 |
1866
ac8a74ce72cd
(readline): Append trailing newline to line.
Jim Meyering <jim@meyering.net>
parents:
1465
diff
changeset
|
94 linebuffer->length = p - buffer; |
14 | 95 return linebuffer; |
96 } | |
97 | |
4727
9c493e421b7f
(freebuffer): Don't free the argument, just the buffer associated with
Paul Eggert <eggert@cs.ucla.edu>
parents:
4713
diff
changeset
|
98 /* Free the buffer that was allocated for linebuffer LINEBUFFER. */ |
14 | 99 |
100 void | |
1465 | 101 freebuffer (struct linebuffer *linebuffer) |
14 | 102 { |
103 free (linebuffer->buffer); | |
104 } |