Add initial varray support infrastructure
[official-gcc.git] / libio / isgetline.cc
blob02b34927eb8fa49ead3a9e23b48619eeeeb04ca4
1 /* This is part of libio/iostream, providing -*- C++ -*- input/output.
2 Copyright (C) 1993 Free Software Foundation
4 This file is part of the GNU IO Library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this library; see the file COPYING. If not, write to the Free
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
25 #include <libioP.h>
26 #include "iostream.h"
27 #include <string.h>
29 istream& istream::getline(char* buf, int len, char delim)
31 _gcount = 0;
32 if (len <= 0)
34 set(ios::failbit);
35 return *this;
37 int ch;
38 if (ipfx1())
40 streambuf *sb = rdbuf();
41 _gcount = _IO_getline_info(sb, buf, len - 1, delim, -1, &ch);
42 if (ch != EOF)
43 ch = sb->sbumpc();
44 if (ch == EOF)
45 set (_gcount == 0 ? (ios::failbit|ios::eofbit) : ios::eofbit);
46 else if (ch != (unsigned char) delim)
48 set(ios::failbit);
49 sb->sungetc(); // Leave delimiter unread.
52 else
53 ch = EOF;
54 buf[_gcount] = '\0';
55 if (ch == (unsigned char)delim)
56 _gcount++; // The delimiter is counted in the gcount().
57 return *this;
60 istream& istream::get(char* buf, int len, char delim)
62 _gcount = 0;
63 if (len <= 0)
65 set(ios::failbit);
66 return *this;
68 if (ipfx1())
70 streambuf *sbuf = rdbuf();
71 int ch;
72 _gcount = _IO_getline_info(sbuf, buf, len - 1, delim, -1, &ch);
73 if (_gcount == 0 && ch == EOF)
74 set(ios::failbit|ios::eofbit);
76 buf[_gcount] = '\0';
77 return *this;
81 // from Doug Schmidt
83 #define CHUNK_SIZE 512
85 /* Reads an arbitrarily long input line terminated by a user-specified
86 TERMINATOR. Super-nifty trick using recursion avoids unnecessary calls
87 to NEW! */
89 char *_sb_readline (streambuf *sb, long& total, char terminator)
91 char buf[CHUNK_SIZE];
92 char *ptr;
93 int ch;
95 _IO_size_t count = _IO_getline_info(sb, buf, CHUNK_SIZE, terminator,
96 -1, &ch);
97 if (ch != EOF)
98 ch = sb->sbumpc();
99 long old_total = total;
100 total += count;
101 if (ch != EOF && ch != terminator) {
102 total++; // Include ch in total.
103 ptr = _sb_readline(sb, total, terminator);
104 if (ptr) {
105 memcpy(ptr + old_total, buf, count);
106 ptr[old_total+count] = ch;
108 return ptr;
111 ptr = new char[total+1];
112 if (ptr != NULL) {
113 ptr[total] = '\0';
114 memcpy(ptr + total - count, buf, count);
116 return ptr;
119 /* Reads an arbitrarily long input line terminated by TERMINATOR.
120 This routine allocates its own memory, so the user should
121 only supply the address of a (char *). */
123 istream& istream::gets(char **s, char delim /* = '\n' */)
125 if (ipfx1()) {
126 long size = 0;
127 streambuf *sb = rdbuf();
128 *s = _sb_readline (sb, size, delim);
129 _gcount = *s ? size : 0;
130 if (sb->_flags & _IO_EOF_SEEN) {
131 set(ios::eofbit);
132 if (_gcount == 0)
133 set(ios::failbit);
136 else {
137 _gcount = 0;
138 *s = NULL;
140 return *this;