1 /* Correctly reads an arbitrarily size string.
3 Copyright (C) 1989-1998 Free Software Foundation, Inc.
4 written by Douglas C. Schmidt (schmidt@ics.uci.edu)
6 This file is part of GNU GPERF.
8 GNU GPERF is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 1, or (at your option)
13 GNU GPERF is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU GPERF; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
22 #include "read-line.h"
25 #include <string.h> /* declares memcpy() */
29 /* Recursively fills up the buffer. */
31 #define CHUNK_SIZE 4096
33 /* CHUNKS is the number of chunks (each of size CHUNK_SIZE) which have
34 already been read and which are temporarily stored on the stack.
35 This function reads the remainder of the line, allocates a buffer
36 for the entire line, fills the part beyond &buffer[chunks*CHUNK_SIZE],
37 and returns &buffer[chunks*CHUNK_SIZE]. */
40 Read_Line::readln_aux (int chunks
)
42 T (Trace
t ("Read_Line::readln_aux");)
46 // Note: we don't use new, because that invokes a custom operator new.
47 char *buf
= (char*)malloc(CHUNK_SIZE
);
55 while (c
= getc (fp
), c
!= EOF
&& c
!= '\n') /* fill the current buffer */
58 if (bufptr
- buf
== CHUNK_SIZE
)
60 if ((ptr
= readln_aux (chunks
+ 1)) != NULL
)
62 /* prepend remainder to ptr buffer */
65 memcpy (ptr
, buf
, CHUNK_SIZE
);
71 if (c
== EOF
&& bufptr
== buf
&& chunks
== 0)
75 size_t s1
= chunks
* CHUNK_SIZE
;
76 size_t s2
= bufptr
- buf
;
78 ptr
= new char[s1
+s2
+1];
81 memcpy (ptr
, buf
, s2
);
93 #define INLINE /* not inline */
94 #include "read-line.icc"
97 #endif /* not defined __OPTIMIZE__ */