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