1 // This may look like C code, but it is really -*- C++ -*-
2 /*
3 Copyright (C) 1989 Free Software Foundation
4 written by Doug Lea (dl@rocky.oswego.edu)
5
6 This file is part of GNU CC.
7
8 GNU CC is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY. No author or distributor
10 accepts responsibility to anyone for the consequences of using it
11 or for whether it serves any particular purpose or works at all,
12 unless he says so in writing. Refer to the GNU CC General Public
13 License for full details.
14
15 Everyone is granted permission to copy, modify and redistribute
16 GNU CC, but only under the conditions described in the
17 GNU CC General Public License. A copy of this license is
18 supposed to have been given to you along with GNU CC so you
19 can know your rights and responsibilities. It should be in a
20 file named COPYING. Among other things, the copyright notice
21 and this notice must be preserved on all copies.
22 */
23
24 /* *** Version 1.2 -- nearly 100% AT&T 1.2 compatible *** */
25
26 /* istream.h now separately includable */
27
28 #ifndef _istream_h
29 #ifdef __GNUG__
30 #pragma once
31 #pragma interface
32 #endif
33 #define _istream_h 1
34
35
36 #include <File.h>
37 #include <streambuf.h>
38 #include <filebuf.h>
39 #include <Filebuf.h>
40
41 class whitespace // a class used only to input and
42 { // discard white space characters
43 char filler;
44 };
45
46 class ostream;
47
48 class istream
49 {
50 friend void eatwhite(istream& s);
51 protected:
52 streambuf* bp;
53 state_value state; // _good/_eof/_fail/_bad
54 ostream* tied_to;
55 char skipws;
56 char ownbuf;
57 void _flush();
58 char* readline (int chunk_number, char terminator);
59
60 public:
61 istream(const char* filename, io_mode m, access_mode a,
62 int sk=1, ostream* t = 0);
63 istream(const char* filename, const char* m,
64 int sk=1, ostream* t = 0);
65 istream(int filedesc, io_mode m, int sk=1, ostream* t = 0);
66 istream(FILE* fileptr, int sk=1, ostream* t = 0);
67 istream(int sz, char* buf, int sk=1, ostream* t = 0);
68 istream(int filedesc, int sk=1, ostream* t = 0);
69 istream(int filedesc, char* buf, int buflen,
70 int sk, ostream* t = 0);
71 istream(streambuf* s, int sk=1, ostream* t = 0);
72
73 ~istream();
74
75 istream& open(const char* filename, io_mode m, access_mode a);
76 istream& open(const char* filename, const char* m);
77 istream& open(int filedesc, io_mode m);
78 istream& open(FILE* fileptr);
79 istream& open(const char* filenam, open_mode m);
80
81 istream& close();
82
83 ostream* tie(ostream* s);
84 int skip(int);
85
86 // stream status
87
88 int rdstate();
89 int eof();
90 int fail();
91 int bad();
92 int good();
93
94 // other status queries
95
96 int readable();
97 int writable();
98 int is_open();
99
100 operator void*();
101 int operator !();
102
103 const char* name();
104
105 char* bufptr();
106
107 // error handling
108
109 void error();
110 void clear(state_value f = _good); // poorly named
111 void set(state_value f); // set corresponding bit
112 void unset(state_value f); // clear corresponding bit
113 istream& failif(int cond);
114
115 // unformatted IO
116
117 istream& get(char& c);
118 istream& unget(char c);
119 istream& putback(char c); // a synonym for unget
120
121 istream& get (char* s, int n, char terminator = '\n');
122 istream& getline(char* s, int n, char terminator = '\n');
123 istream& gets (char **s, char terminator = '\n');
124
125
126 istream& operator >> (char& c);
127 istream& operator >> (short& n);
128 istream& operator >> (unsigned short& n);
129 istream& operator >> (int& n);
130 istream& operator >> (unsigned int& n);
131 istream& operator >> (long& n);
132 istream& operator >> (unsigned long& n);
133 #ifdef __GNUG__
134 istream& operator >> (long long& n);
135 istream& operator >> (unsigned long long& n);
136 #endif
137 istream& operator >> (float& n);
138 istream& operator >> (double& n);
139 istream& operator >> (char* s);
140 istream& operator >> (whitespace& w);
141 };
142
143 // pre-declared streams
144
145 extern istream cin; // stdin
146
147 extern whitespace WS; // for convenience
148
149 #if defined(__OPTIMIZE__) || defined(USE_LIBGXX_INLINES)
150
151
clear(state_value flag)152 inline void istream::clear(state_value flag)
153 {
154 state = flag;
155 }
156
set(state_value flag)157 inline void istream::set(state_value flag)
158 {
159 state = state_value(int(state) | int(flag));
160 }
161
unset(state_value flag)162 inline void istream::unset(state_value flag)
163 {
164 state = state_value(int(state) & ~int(flag));
165 }
166
rdstate()167 inline int istream::rdstate()
168 {
169 return int(state);
170 }
171
good()172 inline int istream::good()
173 {
174 return state == _good;
175 }
176
eof()177 inline int istream::eof()
178 {
179 return int(state) & int(_eof);
180 }
181
fail()182 inline int istream::fail()
183 {
184 return int(state) & int(_fail);
185 }
186
bad()187 inline int istream::bad()
188 {
189 return int(state) & int(_bad);
190 }
191
192 inline istream::operator void*()
193 {
194 return (state == _good)? this : 0;
195 }
196
197 inline int istream::operator !()
198 {
199 return (state != _good);
200 }
201
failif(int cond)202 inline istream& istream::failif(int cond)
203 {
204 if (cond) set(_fail); return *this;
205 }
206
is_open()207 inline int istream::is_open()
208 {
209 return bp->is_open();
210 }
211
readable()212 inline int istream::readable()
213 {
214 return (bp != 0) && (bp->is_open()) && (state == _good);
215 }
216
writable()217 inline int istream::writable()
218 {
219 return 0;
220 }
221
222
bufptr()223 inline char* istream::bufptr()
224 {
225 return bp->base;
226 }
227
228
close()229 inline istream& istream::close()
230 {
231 bp->close(); return *this;
232 }
233
234
skip(int sk)235 inline int istream::skip(int sk)
236 {
237 int was = skipws; skipws = sk; return was;
238 }
239
240
unget(char c)241 inline istream& istream::unget(char c)
242 {
243 if (bp->sputbackc(c) == EOF) set(_fail); return *this;
244 }
245
putback(char c)246 inline istream& istream::putback(char c)
247 {
248 if (bp->sputbackc(c) == EOF) set(_fail); return *this;
249 }
250
eatwhite(istream & s)251 inline void eatwhite(istream& s)
252 {
253 s >> WS;
254 }
255
256 #endif
257
258
259 #endif
260