xref: /haiku/headers/cpp/iostream.h (revision c237c4ce593ee823d9867fd997e51e4c447f5623)
1 /*  This is part of libio/iostream, providing -*- C++ -*- input/output.
2 Copyright (C) 1993 Free Software Foundation
3 
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.
9 
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.
14 
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.
18 
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. */
24 
25 #ifndef _IOSTREAM_H
26 #ifdef __GNUG__
27 #pragma interface
28 #endif
29 #define _IOSTREAM_H
30 
31 #include <streambuf.h>
32 
33 extern "C++" {
34 class istream; class ostream;
35 typedef ios& (*__manip)(ios&);
36 typedef istream& (*__imanip)(istream&);
37 typedef ostream& (*__omanip)(ostream&);
38 
39 extern istream& ws(istream& ins);
40 extern ostream& flush(ostream& outs);
41 extern ostream& endl(ostream& outs);
42 extern ostream& ends(ostream& outs);
43 
44 class ostream : virtual public ios
45 {
46     // NOTE: If fields are changed, you must fix _fake_ostream in stdstreams.C!
47     void do_osfx();
48   public:
49     ostream() { }
50     ostream(streambuf* sb, ostream* tied=NULL);
51     int opfx() {
52 	if (!good()) return 0;
53 	else { if (_tie) _tie->flush(); _IO_flockfile(_strbuf); return 1;} }
54     void osfx() { _IO_funlockfile(_strbuf);
55 		  if (flags() & (ios::unitbuf|ios::stdio))
56 		      do_osfx(); }
57     ostream& flush();
58     ostream& put(char c) { _strbuf->sputc(c); return *this; }
59 #ifdef _STREAM_COMPAT
60     /* Temporary binary compatibility.  REMOVE IN NEXT RELEASE. */
61     ostream& put(unsigned char c) { return put((char)c); }
62     ostream& put(signed char c) { return put((char)c); }
63 #endif
64     ostream& write(const char *s, streamsize n);
65     ostream& write(const unsigned char *s, streamsize n)
66       { return write((const char*)s, n);}
67     ostream& write(const signed char *s, streamsize n)
68       { return write((const char*)s, n);}
69     ostream& write(const void *s, streamsize n)
70       { return write((const char*)s, n);}
71 #ifdef _STREAM_COMPAT
72     // [zooey]: added for R5-compatibility with bdb,
73     // these can't be inlined as they wouldn't end up in the lib then.
74     ostream& write(const char *s, int n);
75     ostream& write(const unsigned char *s, int n);
76     ostream& write(const signed char *s, int n);
77     ostream& write(const void *s, int n);
78 #endif
79     ostream& seekp(streampos);
80     ostream& seekp(streamoff, _seek_dir);
81     streampos tellp();
82     ostream& form(const char *format ...);
83     ostream& vform(const char *format, _IO_va_list args);
84 
85     ostream& operator<<(char c);
86     ostream& operator<<(unsigned char c) { return (*this) << (char)c; }
87     ostream& operator<<(signed char c) { return (*this) << (char)c; }
88     ostream& operator<<(const char *s);
89     ostream& operator<<(const unsigned char *s)
90 	{ return (*this) << (const char*)s; }
91     ostream& operator<<(const signed char *s)
92 	{ return (*this) << (const char*)s; }
93     ostream& operator<<(const void *p);
94     ostream& operator<<(int n);
95     ostream& operator<<(unsigned int n);
96     ostream& operator<<(long n);
97     ostream& operator<<(unsigned long n);
98 #if defined(__GNUC__)
99     __extension__ ostream& operator<<(long long n);
100     __extension__ ostream& operator<<(unsigned long long n);
101 #endif
102     ostream& operator<<(short n) {return operator<<((int)n);}
103     ostream& operator<<(unsigned short n) {return operator<<((unsigned int)n);}
104 #if _G_HAVE_BOOL
105     ostream& operator<<(bool b) { return operator<<((int)b); }
106 #endif
107     ostream& operator<<(double n);
108     ostream& operator<<(float n) { return operator<<((double)n); }
109 #if _G_HAVE_LONG_DOUBLE_IO
110     ostream& operator<<(long double n);
111 #else
112     ostream& operator<<(long double n) { return operator<<((double)n); }
113 #endif
114     ostream& operator<<(__omanip func) { return (*func)(*this); }
115     ostream& operator<<(__manip func) {(*func)(*this); return *this;}
116     ostream& operator<<(streambuf*);
117 #ifdef _STREAM_COMPAT
118     streambuf* ostreambuf() const { return _strbuf; }
119 #endif
120 };
121 
122 class istream : virtual public ios
123 {
124     // NOTE: If fields are changed, you must fix _fake_istream in stdstreams.C!
125 protected:
126     _IO_size_t _gcount;
127 
128     int _skip_ws();
129   public:
130     istream(): _gcount (0) { }
131     istream(streambuf* sb, ostream*tied=NULL);
132     istream& get(char* ptr, int len, char delim = '\n');
133     istream& get(unsigned char* ptr, int len, char delim = '\n')
134 	{ return get((char*)ptr, len, delim); }
135     istream& get(char& c);
136     istream& get(unsigned char& c) { return get((char&)c); }
137     istream& getline(char* ptr, int len, char delim = '\n');
138     istream& getline(unsigned char* ptr, int len, char delim = '\n')
139 	{ return getline((char*)ptr, len, delim); }
140     istream& get(signed char& c)  { return get((char&)c); }
141     istream& get(signed char* ptr, int len, char delim = '\n')
142 	{ return get((char*)ptr, len, delim); }
143     istream& getline(signed char* ptr, int len, char delim = '\n')
144 	{ return getline((char*)ptr, len, delim); }
145     istream& read(char *ptr, streamsize n);
146     istream& read(unsigned char *ptr, streamsize n)
147       { return read((char*)ptr, n); }
148     istream& read(signed char *ptr, streamsize n)
149       { return read((char*)ptr, n); }
150     istream& read(void *ptr, streamsize n)
151       { return read((char*)ptr, n); }
152 #ifdef _STREAM_COMPAT
153     // [zooey]: added for R5-compatibility with bdb,
154     // these can't be inlined as they wouldn't end up in the lib then.
155     istream& read(char *ptr, int n);
156     istream& read(unsigned char *ptr, int n);
157     istream& read(signed char *ptr, int n);
158     istream& read(void *ptr, int n);
159 #endif
160     istream& get(streambuf& sb, char delim = '\n');
161     istream& gets(char **s, char delim = '\n');
162     int ipfx(int need = 0) {
163 	if (!good()) { set(ios::failbit); return 0; }
164 	else {
165 	  _IO_flockfile(_strbuf);
166 	  if (_tie && (need == 0 || rdbuf()->in_avail() < need)) _tie->flush();
167 	  if (!need && (flags() & ios::skipws)) return _skip_ws();
168 	  else return 1;
169 	}
170     }
171     int ipfx0() { // Optimized version of ipfx(0).
172 	if (!good()) { set(ios::failbit); return 0; }
173 	else {
174 	  _IO_flockfile(_strbuf);
175 	  if (_tie) _tie->flush();
176 	  if (flags() & ios::skipws) return _skip_ws();
177 	  else return 1;
178 	}
179     }
180     int ipfx1() { // Optimized version of ipfx(1).
181 	if (!good()) { set(ios::failbit); return 0; }
182 	else {
183 	  _IO_flockfile(_strbuf);
184 	  if (_tie && rdbuf()->in_avail() == 0) _tie->flush();
185 	  return 1;
186 	}
187     }
188     void isfx() { _IO_funlockfile(_strbuf); }
189     int get() { if (!ipfx1()) return EOF;
190 		else { int ch = _strbuf->sbumpc();
191 		       if (ch == EOF) set(ios::eofbit);
192 		       isfx();
193 		       return ch;
194 		     } }
195     int peek();
196     _IO_size_t gcount() { return _gcount; }
197     istream& ignore(int n=1, int delim = EOF);
198     int sync ();
199     istream& seekg(streampos);
200     istream& seekg(streamoff, _seek_dir);
201     streampos tellg();
202     istream& putback(char ch) {
203 	if (good() && _strbuf->sputbackc(ch) == EOF) clear(ios::badbit);
204 	return *this;}
205     istream& unget() {
206 	if (good() && _strbuf->sungetc() == EOF) clear(ios::badbit);
207 	return *this;}
208     istream& scan(const char *format ...);
209     istream& vscan(const char *format, _IO_va_list args);
210 #ifdef _STREAM_COMPAT
211     istream& unget(char ch) { return putback(ch); }
212     int skip(int i);
213     streambuf* istreambuf() const { return _strbuf; }
214 #endif
215 
216     istream& operator>>(char*);
217     istream& operator>>(unsigned char* p) { return operator>>((char*)p); }
218     istream& operator>>(signed char*p) { return operator>>((char*)p); }
219     istream& operator>>(char& c);
220     istream& operator>>(unsigned char& c) {return operator>>((char&)c);}
221     istream& operator>>(signed char& c) {return operator>>((char&)c);}
222     istream& operator>>(int&);
223     istream& operator>>(long&);
224 #if defined(__GNUC__)
225     __extension__ istream& operator>>(long long&);
226     __extension__ istream& operator>>(unsigned long long&);
227 #endif
228     istream& operator>>(short&);
229     istream& operator>>(unsigned int&);
230     istream& operator>>(unsigned long&);
231     istream& operator>>(unsigned short&);
232 #if _G_HAVE_BOOL
233     istream& operator>>(bool&);
234 #endif
235     istream& operator>>(float&);
236     istream& operator>>(double&);
237     istream& operator>>(long double&);
238     istream& operator>>( __manip func) {(*func)(*this); return *this;}
239     istream& operator>>(__imanip func) { return (*func)(*this); }
240     istream& operator>>(streambuf*);
241 };
242 
243 class iostream : public istream, public ostream
244 {
245   public:
246     iostream() { }
247     iostream(streambuf* sb, ostream*tied=NULL);
248 };
249 
250 class _IO_istream_withassign : public istream {
251 public:
252   _IO_istream_withassign& operator=(istream&);
253   _IO_istream_withassign& operator=(_IO_istream_withassign& rhs)
254     { return operator= (static_cast<istream&> (rhs)); }
255 };
256 
257 class _IO_ostream_withassign : public ostream {
258 public:
259   _IO_ostream_withassign& operator=(ostream&);
260   _IO_ostream_withassign& operator=(_IO_ostream_withassign& rhs)
261     { return operator= (static_cast<ostream&> (rhs)); }
262 };
263 
264 extern _IO_istream_withassign cin;
265 // clog->rdbuf() == cerr->rdbuf()
266 extern _IO_ostream_withassign cout, cerr;
267 
268 extern _IO_ostream_withassign clog
269 #if _G_CLOG_CONFLICT
270 __asm__ ("__IO_clog")
271 #endif
272 ;
273 
274 struct Iostream_init { } ;  // Compatibility hack for AT&T library.
275 
276 inline ios& dec(ios& i)
277 { i.setf(ios::dec, ios::dec|ios::hex|ios::oct); return i; }
278 inline ios& hex(ios& i)
279 { i.setf(ios::hex, ios::dec|ios::hex|ios::oct); return i; }
280 inline ios& oct(ios& i)
281 { i.setf(ios::oct, ios::dec|ios::hex|ios::oct); return i; }
282 } // extern "C++"
283 
284 #endif /*!_IOSTREAM_H*/
285