* conversion.c (test_float_to_integer): Make double precision tests
[official-gcc.git] / libio / isgetline.cc
blobadd26638b203df2e8d727e29eda4b45fdf7baf15
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(sb, buf, len - 1, delim, -1);
42 ch = sb->sbumpc();
43 if (ch == EOF)
44 set (_gcount == 0 ? (ios::failbit|ios::eofbit) : ios::eofbit);
45 else if (ch != (unsigned char) delim)
47 set(ios::failbit);
48 sb->sungetc(); // Leave delimiter unread.
51 else
52 ch = EOF;
53 buf[_gcount] = '\0';
54 if (ch == (unsigned char)delim)
55 _gcount++; // The delimiter is counted in the gcount().
56 return *this;
59 istream& istream::get(char* buf, int len, char delim)
61 _gcount = 0;
62 if (len <= 0)
64 set(ios::failbit);
65 return *this;
67 if (ipfx1())
69 streambuf *sbuf = rdbuf();
70 long count = _IO_getline(sbuf, buf, len - 1, delim, -1);
71 if (count == 0 && sbuf->sgetc() == EOF)
72 set(ios::failbit|ios::eofbit);
73 else
74 _gcount = count;
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(sb, buf, CHUNK_SIZE, terminator, -1);
96 ch = sb->sbumpc();
97 long old_total = total;
98 total += count;
99 if (ch != EOF && ch != terminator) {
100 total++; // Include ch in total.
101 ptr = _sb_readline(sb, total, terminator);
102 if (ptr) {
103 memcpy(ptr + old_total, buf, count);
104 ptr[old_total+count] = ch;
106 return ptr;
109 ptr = new char[total+1];
110 if (ptr != NULL) {
111 ptr[total] = '\0';
112 memcpy(ptr + total - count, buf, count);
114 return ptr;
117 /* Reads an arbitrarily long input line terminated by TERMINATOR.
118 This routine allocates its own memory, so the user should
119 only supply the address of a (char *). */
121 istream& istream::gets(char **s, char delim /* = '\n' */)
123 if (ipfx1()) {
124 long size = 0;
125 streambuf *sb = rdbuf();
126 *s = _sb_readline (sb, size, delim);
127 _gcount = *s ? size : 0;
128 if (sb->_flags & _IO_EOF_SEEN) {
129 set(ios::eofbit);
130 if (_gcount == 0)
131 set(ios::failbit);
134 else {
135 _gcount = 0;
136 *s = NULL;
138 return *this;