|
1 /* |
|
2 * Copyright (c) 1999 |
|
3 * Silicon Graphics Computer Systems, Inc. |
|
4 * |
|
5 * Copyright (c) 1999 |
|
6 * Boris Fomitchev |
|
7 * |
|
8 * This material is provided "as is", with absolutely no warranty expressed |
|
9 * or implied. Any use is at your own risk. |
|
10 * |
|
11 * Permission to use or copy this software for any purpose is hereby granted |
|
12 * without fee, provided the above notices are retained on all copies. |
|
13 * Permission to modify the code and to distribute modified code is granted, |
|
14 * provided the above notices are retained, and a notice that the code was |
|
15 * modified is included with the above copyright notice. |
|
16 * |
|
17 */ |
|
18 #ifndef _STLP_INTERNAL_ISTREAM |
|
19 #define _STLP_INTERNAL_ISTREAM |
|
20 |
|
21 // this block is included by _ostream.h, we include it here to lower #include level |
|
22 #if defined (_STLP_HAS_WCHAR_T) && !defined (_STLP_INTERNAL_CWCHAR) |
|
23 # include <stl/_cwchar.h> |
|
24 #endif |
|
25 |
|
26 #ifndef _STLP_INTERNAL_IOS_H |
|
27 # include <stl/_ios.h> // For basic_ios<>. Includes <iosfwd>. |
|
28 #endif |
|
29 |
|
30 #ifndef _STLP_INTERNAL_OSTREAM_H |
|
31 # include <stl/_ostream.h> // Needed as a base class of basic_iostream. |
|
32 #endif |
|
33 |
|
34 #ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H |
|
35 # include <stl/_istreambuf_iterator.h> |
|
36 #endif |
|
37 |
|
38 #include <stl/_ctraits_fns.h> // Helper functions that allow char traits |
|
39 // to be used as function objects. |
|
40 _STLP_BEGIN_NAMESPACE |
|
41 |
|
42 #if defined (_STLP_USE_TEMPLATE_EXPORT) |
|
43 template <class _CharT, class _Traits> |
|
44 class _Isentry; |
|
45 #endif |
|
46 |
|
47 struct _No_Skip_WS {}; // Dummy class used by sentry. |
|
48 |
|
49 template <class _CharT, class _Traits> |
|
50 bool _M_init_skip(basic_istream<_CharT, _Traits>& __istr); |
|
51 template <class _CharT, class _Traits> |
|
52 bool _M_init_noskip(basic_istream<_CharT, _Traits>& __istr); |
|
53 |
|
54 //---------------------------------------------------------------------- |
|
55 // Class basic_istream, a class that performs formatted input through |
|
56 // a stream buffer. |
|
57 |
|
58 // The second template parameter, _Traits, defaults to char_traits<_CharT>. |
|
59 // The default is declared in header <iosfwd>, and it isn't declared here |
|
60 // because C++ language rules do not allow it to be declared twice. |
|
61 |
|
62 template <class _CharT, class _Traits> |
|
63 class basic_istream : virtual public basic_ios<_CharT, _Traits> { |
|
64 typedef basic_istream<_CharT, _Traits> _Self; |
|
65 |
|
66 #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310) |
|
67 //explicitely defined as private to avoid warnings: |
|
68 basic_istream(_Self const&); |
|
69 _Self& operator = (_Self const&); |
|
70 #endif |
|
71 |
|
72 public: |
|
73 // Types |
|
74 typedef _CharT char_type; |
|
75 typedef typename _Traits::int_type int_type; |
|
76 typedef typename _Traits::pos_type pos_type; |
|
77 typedef typename _Traits::off_type off_type; |
|
78 typedef _Traits traits_type; |
|
79 typedef basic_ios<_CharT, _Traits> _Basic_ios; |
|
80 |
|
81 typedef basic_ios<_CharT, _Traits>& (_STLP_CALL *__ios_fn)(basic_ios<_CharT, _Traits>&); |
|
82 typedef ios_base& (_STLP_CALL *__ios_base_fn)(ios_base&); |
|
83 typedef _Self& (_STLP_CALL *__istream_fn)(_Self&); |
|
84 |
|
85 public: // Constructor and destructor. |
|
86 explicit basic_istream(basic_streambuf<_CharT, _Traits>* __buf) : |
|
87 basic_ios<_CharT, _Traits>(), _M_gcount(0) { |
|
88 this->init(__buf); |
|
89 } |
|
90 ~basic_istream() {}; |
|
91 |
|
92 public: // Nested sentry class. |
|
93 |
|
94 public: // Hooks for manipulators. The arguments are |
|
95 // function pointers. |
|
96 _Self& operator>> (__istream_fn __f) { return __f(*this); } |
|
97 _Self& operator>> (__ios_fn __f) { __f(*this); return *this; } |
|
98 _Self& operator>> (__ios_base_fn __f) { __f(*this); return *this; } |
|
99 |
|
100 public: // Formatted input of numbers. |
|
101 _Self& operator>> (short& __val); |
|
102 _Self& operator>> (int& __val); |
|
103 _Self& operator>> (unsigned short& __val); |
|
104 _Self& operator>> (unsigned int& __val); |
|
105 _Self& operator>> (long& __val); |
|
106 _Self& operator>> (unsigned long& __val); |
|
107 #ifdef _STLP_LONG_LONG |
|
108 _Self& operator>> (_STLP_LONG_LONG& __val); |
|
109 _Self& operator>> (unsigned _STLP_LONG_LONG& __val); |
|
110 #endif |
|
111 _Self& operator>> (float& __val); |
|
112 _Self& operator>> (double& __val); |
|
113 # ifndef _STLP_NO_LONG_DOUBLE |
|
114 _Self& operator>> (long double& __val); |
|
115 # endif |
|
116 # ifndef _STLP_NO_BOOL |
|
117 _Self& operator>> (bool& __val); |
|
118 # endif |
|
119 _Self& operator>> (void*& __val); |
|
120 |
|
121 public: // Copying characters into a streambuf. |
|
122 _Self& operator>>(basic_streambuf<_CharT, _Traits>*); |
|
123 |
|
124 public: // Unformatted input. |
|
125 streamsize gcount() const { return _M_gcount; } |
|
126 int_type peek(); |
|
127 |
|
128 public: // get() for single characters |
|
129 int_type get(); |
|
130 _Self& get(char_type& __c); |
|
131 |
|
132 public: // get() for character arrays. |
|
133 _Self& get(char_type* __s, streamsize __n, char_type __delim); |
|
134 _Self& get(char_type* __s, streamsize __n) |
|
135 { return get(__s, __n, this->widen('\n')); } |
|
136 |
|
137 public: // get() for streambufs |
|
138 _Self& get(basic_streambuf<_CharT, _Traits>& __buf, |
|
139 char_type __delim); |
|
140 _Self& get(basic_streambuf<_CharT, _Traits>& __buf) |
|
141 { return get(__buf, this->widen('\n')); } |
|
142 |
|
143 public: // getline() |
|
144 _Self& getline(char_type* __s, streamsize __n, char_type delim); |
|
145 _Self& getline(char_type* __s, streamsize __n) |
|
146 { return getline(__s, __n, this->widen('\n')); } |
|
147 |
|
148 public: // read(), readsome(), ignore() |
|
149 _Self& ignore(); |
|
150 _Self& ignore(streamsize __n); |
|
151 #if (defined (_STLP_MSVC) && _STLP_MSVC < 1200) |
|
152 inline |
|
153 #endif |
|
154 _Self& ignore(streamsize __n, int_type __delim); |
|
155 |
|
156 _Self& read(char_type* __s, streamsize __n); |
|
157 streamsize readsome(char_type* __s, streamsize __n); |
|
158 |
|
159 public: // putback |
|
160 _Self& putback(char_type __c); |
|
161 _Self& unget(); |
|
162 |
|
163 public: // Positioning and buffer control. |
|
164 int sync(); |
|
165 |
|
166 pos_type tellg(); |
|
167 _Self& seekg(pos_type __pos); |
|
168 _Self& seekg(off_type, ios_base::seekdir); |
|
169 |
|
170 public: // Helper functions for non-member extractors. |
|
171 void _M_formatted_get(_CharT& __c); |
|
172 void _M_formatted_get(_CharT* __s); |
|
173 void _M_skip_whitespace(bool __set_failbit); |
|
174 |
|
175 private: // Number of characters extracted by the |
|
176 streamsize _M_gcount; // most recent unformatted input function. |
|
177 |
|
178 public: |
|
179 |
|
180 #if defined (_STLP_USE_TEMPLATE_EXPORT) |
|
181 // If we are using DLL specs, we have not to use inner classes |
|
182 // end class declaration here |
|
183 typedef _Isentry<_CharT, _Traits> sentry; |
|
184 }; |
|
185 # define sentry _Isentry |
|
186 template <class _CharT, class _Traits> |
|
187 class _Isentry { |
|
188 typedef _Isentry<_CharT, _Traits> _Self; |
|
189 # else |
|
190 class sentry { |
|
191 typedef sentry _Self; |
|
192 #endif |
|
193 |
|
194 private: |
|
195 const bool _M_ok; |
|
196 // basic_streambuf<_CharT, _Traits>* _M_buf; |
|
197 |
|
198 public: |
|
199 typedef _Traits traits_type; |
|
200 |
|
201 explicit sentry(basic_istream<_CharT, _Traits>& __istr, |
|
202 bool __noskipws = false) : |
|
203 _M_ok((__noskipws || !(__istr.flags() & ios_base::skipws)) ? _M_init_noskip(__istr) : _M_init_skip(__istr) ) |
|
204 /* , _M_buf(__istr.rdbuf()) */ |
|
205 {} |
|
206 |
|
207 // Calling this constructor is the same as calling the previous one with |
|
208 // __noskipws = true, except that it doesn't require a runtime test. |
|
209 sentry(basic_istream<_CharT, _Traits>& __istr, _No_Skip_WS) : /* _M_buf(__istr.rdbuf()), */ |
|
210 _M_ok(_M_init_noskip(__istr)) {} |
|
211 |
|
212 ~sentry() {} |
|
213 |
|
214 operator bool() const { return _M_ok; } |
|
215 |
|
216 private: // Disable assignment and copy constructor. |
|
217 //Implementation is here only to avoid warning with some compilers. |
|
218 sentry(const _Self&) : _M_ok(false) {} |
|
219 _Self& operator=(const _Self&) { return *this; } |
|
220 }; |
|
221 |
|
222 # if defined (_STLP_USE_TEMPLATE_EXPORT) |
|
223 # undef sentry |
|
224 # else |
|
225 // close basic_istream class definition here |
|
226 }; |
|
227 # endif |
|
228 |
|
229 # if defined (_STLP_USE_TEMPLATE_EXPORT) |
|
230 _STLP_EXPORT_TEMPLATE_CLASS _Isentry<char, char_traits<char> >; |
|
231 _STLP_EXPORT_TEMPLATE_CLASS basic_istream<char, char_traits<char> >; |
|
232 # if ! defined (_STLP_NO_WCHAR_T) |
|
233 _STLP_EXPORT_TEMPLATE_CLASS _Isentry<wchar_t, char_traits<wchar_t> >; |
|
234 _STLP_EXPORT_TEMPLATE_CLASS basic_istream<wchar_t, char_traits<wchar_t> >; |
|
235 # endif |
|
236 # endif /* _STLP_USE_TEMPLATE_EXPORT */ |
|
237 |
|
238 // Non-member character and string extractor functions. |
|
239 template <class _CharT, class _Traits> |
|
240 inline basic_istream<_CharT, _Traits>& _STLP_CALL |
|
241 operator>>(basic_istream<_CharT, _Traits>& __in_str, _CharT& __c) { |
|
242 __in_str._M_formatted_get(__c); |
|
243 return __in_str; |
|
244 } |
|
245 |
|
246 template <class _Traits> |
|
247 inline basic_istream<char, _Traits>& _STLP_CALL |
|
248 operator>>(basic_istream<char, _Traits>& __in_str, unsigned char& __c) { |
|
249 __in_str._M_formatted_get(__REINTERPRET_CAST(char&,__c)); |
|
250 return __in_str; |
|
251 } |
|
252 |
|
253 template <class _Traits> |
|
254 inline basic_istream<char, _Traits>& _STLP_CALL |
|
255 operator>>(basic_istream<char, _Traits>& __in_str, signed char& __c) { |
|
256 __in_str._M_formatted_get(__REINTERPRET_CAST(char&,__c)); |
|
257 return __in_str; |
|
258 } |
|
259 |
|
260 template <class _CharT, class _Traits> |
|
261 inline basic_istream<_CharT, _Traits>& _STLP_CALL |
|
262 operator>>(basic_istream<_CharT, _Traits>& __in_str, _CharT* __s) { |
|
263 __in_str._M_formatted_get(__s); |
|
264 return __in_str; |
|
265 } |
|
266 |
|
267 template <class _Traits> |
|
268 inline basic_istream<char, _Traits>& _STLP_CALL |
|
269 operator>>(basic_istream<char, _Traits>& __in_str, unsigned char* __s) { |
|
270 __in_str._M_formatted_get(__REINTERPRET_CAST(char*,__s)); |
|
271 return __in_str; |
|
272 } |
|
273 |
|
274 template <class _Traits> |
|
275 inline basic_istream<char, _Traits>& _STLP_CALL |
|
276 operator>>(basic_istream<char, _Traits>& __in_str, signed char* __s) { |
|
277 __in_str._M_formatted_get(__REINTERPRET_CAST(char*,__s)); |
|
278 return __in_str; |
|
279 } |
|
280 |
|
281 //---------------------------------------------------------------------- |
|
282 // istream manipulator. |
|
283 template <class _CharT, class _Traits> |
|
284 basic_istream<_CharT, _Traits>& _STLP_CALL |
|
285 ws(basic_istream<_CharT, _Traits>& __istr) { |
|
286 if (!__istr.eof()) { |
|
287 typedef typename basic_istream<_CharT, _Traits>::sentry _Sentry; |
|
288 _Sentry __sentry(__istr, _No_Skip_WS()); // Don't skip whitespace. |
|
289 if (__sentry) |
|
290 __istr._M_skip_whitespace(false); |
|
291 } |
|
292 return __istr; |
|
293 } |
|
294 |
|
295 // Helper functions for istream<>::sentry constructor. |
|
296 template <class _CharT, class _Traits> |
|
297 inline bool _M_init_skip(basic_istream<_CharT, _Traits>& __istr) { |
|
298 if (__istr.good()) { |
|
299 if (__istr.tie()) |
|
300 __istr.tie()->flush(); |
|
301 |
|
302 __istr._M_skip_whitespace(true); |
|
303 } |
|
304 |
|
305 if (!__istr.good()) { |
|
306 __istr.setstate(ios_base::failbit); |
|
307 return false; |
|
308 } else |
|
309 return true; |
|
310 } |
|
311 |
|
312 template <class _CharT, class _Traits> |
|
313 inline bool _M_init_noskip(basic_istream<_CharT, _Traits>& __istr) { |
|
314 if (__istr.good()) { |
|
315 if (__istr.tie()) |
|
316 __istr.tie()->flush(); |
|
317 |
|
318 if (!__istr.rdbuf()) |
|
319 __istr.setstate(ios_base::badbit); |
|
320 } |
|
321 else |
|
322 __istr.setstate(ios_base::failbit); |
|
323 return __istr.good(); |
|
324 } |
|
325 |
|
326 //---------------------------------------------------------------------- |
|
327 // Class iostream. |
|
328 template <class _CharT, class _Traits> |
|
329 class basic_iostream |
|
330 : public basic_istream<_CharT, _Traits>, |
|
331 public basic_ostream<_CharT, _Traits> |
|
332 { |
|
333 public: |
|
334 typedef basic_ios<_CharT, _Traits> _Basic_ios; |
|
335 |
|
336 explicit basic_iostream(basic_streambuf<_CharT, _Traits>* __buf); |
|
337 virtual ~basic_iostream(); |
|
338 }; |
|
339 |
|
340 # if defined (_STLP_USE_TEMPLATE_EXPORT) |
|
341 _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<char, char_traits<char> >; |
|
342 |
|
343 # if ! defined (_STLP_NO_WCHAR_T) |
|
344 _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<wchar_t, char_traits<wchar_t> >; |
|
345 # endif |
|
346 # endif /* _STLP_USE_TEMPLATE_EXPORT */ |
|
347 |
|
348 template <class _CharT, class _Traits> |
|
349 basic_streambuf<_CharT, _Traits>* _STLP_CALL _M_get_istreambuf(basic_istream<_CharT, _Traits>& __istr) |
|
350 { return __istr.rdbuf(); } |
|
351 |
|
352 _STLP_END_NAMESPACE |
|
353 |
|
354 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION) |
|
355 # include <stl/_istream.c> |
|
356 #endif |
|
357 |
|
358 #endif /* _STLP_INTERNAL_ISTREAM */ |
|
359 |
|
360 // Local Variables: |
|
361 // mode:C++ |
|
362 // End: |