3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
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.
18 #ifndef _STLP_INTERNAL_ISTREAM
19 #define _STLP_INTERNAL_ISTREAM
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>
26 #ifndef _STLP_INTERNAL_IOS_H
27 # include <stl/_ios.h> // For basic_ios<>. Includes <iosfwd>.
30 #ifndef _STLP_INTERNAL_OSTREAM_H
31 # include <stl/_ostream.h> // Needed as a base class of basic_iostream.
34 #ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
35 # include <stl/_istreambuf_iterator.h>
38 #include <stl/_ctraits_fns.h> // Helper functions that allow char traits
39 // to be used as function objects.
42 #if defined (_STLP_USE_TEMPLATE_EXPORT)
43 template <class _CharT, class _Traits>
47 struct _No_Skip_WS {}; // Dummy class used by sentry.
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);
54 //----------------------------------------------------------------------
55 // Class basic_istream, a class that performs formatted input through
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.
62 template <class _CharT, class _Traits>
63 class basic_istream : virtual public basic_ios<_CharT, _Traits> {
64 typedef basic_istream<_CharT, _Traits> _Self;
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&);
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;
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&);
85 public: // Constructor and destructor.
86 explicit basic_istream(basic_streambuf<_CharT, _Traits>* __buf) :
87 basic_ios<_CharT, _Traits>(), _M_gcount(0) {
92 public: // Nested sentry class.
94 public: // Hooks for manipulators. The arguments are
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; }
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);
111 _Self& operator>> (float& __val);
112 _Self& operator>> (double& __val);
113 # ifndef _STLP_NO_LONG_DOUBLE
114 _Self& operator>> (long double& __val);
116 # ifndef _STLP_NO_BOOL
117 _Self& operator>> (bool& __val);
119 _Self& operator>> (void*& __val);
121 public: // Copying characters into a streambuf.
122 _Self& operator>>(basic_streambuf<_CharT, _Traits>*);
124 public: // Unformatted input.
125 streamsize gcount() const { return _M_gcount; }
128 public: // get() for single characters
130 _Self& get(char_type& __c);
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')); }
137 public: // get() for streambufs
138 _Self& get(basic_streambuf<_CharT, _Traits>& __buf,
140 _Self& get(basic_streambuf<_CharT, _Traits>& __buf)
141 { return get(__buf, this->widen('\n')); }
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')); }
148 public: // read(), readsome(), ignore()
150 _Self& ignore(streamsize __n);
151 #if (defined (_STLP_MSVC) && _STLP_MSVC < 1200)
154 _Self& ignore(streamsize __n, int_type __delim);
156 _Self& read(char_type* __s, streamsize __n);
157 streamsize readsome(char_type* __s, streamsize __n);
160 _Self& putback(char_type __c);
163 public: // Positioning and buffer control.
167 _Self& seekg(pos_type __pos);
168 _Self& seekg(off_type, ios_base::seekdir);
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);
175 private: // Number of characters extracted by the
176 streamsize _M_gcount; // most recent unformatted input function.
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;
185 # define sentry _Isentry
186 template <class _CharT, class _Traits>
188 typedef _Isentry<_CharT, _Traits> _Self;
191 typedef sentry _Self;
196 // basic_streambuf<_CharT, _Traits>* _M_buf;
199 typedef _Traits traits_type;
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()) */
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)) {}
214 operator bool() const { return _M_ok; }
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; }
222 # if defined (_STLP_USE_TEMPLATE_EXPORT)
225 // close basic_istream class definition here
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> >;
236 # endif /* _STLP_USE_TEMPLATE_EXPORT */
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);
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));
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));
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);
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));
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));
281 //----------------------------------------------------------------------
282 // istream manipulator.
283 template <class _CharT, class _Traits>
284 basic_istream<_CharT, _Traits>& _STLP_CALL
285 ws(basic_istream<_CharT, _Traits>& __istr) {
287 typedef typename basic_istream<_CharT, _Traits>::sentry _Sentry;
288 _Sentry __sentry(__istr, _No_Skip_WS()); // Don't skip whitespace.
290 __istr._M_skip_whitespace(false);
295 // Helper functions for istream<>::sentry constructor.
296 template <class _CharT, class _Traits>
297 inline bool _M_init_skip(basic_istream<_CharT, _Traits>& __istr) {
300 __istr.tie()->flush();
302 __istr._M_skip_whitespace(true);
305 if (!__istr.good()) {
306 __istr.setstate(ios_base::failbit);
312 template <class _CharT, class _Traits>
313 inline bool _M_init_noskip(basic_istream<_CharT, _Traits>& __istr) {
316 __istr.tie()->flush();
319 __istr.setstate(ios_base::badbit);
322 __istr.setstate(ios_base::failbit);
323 return __istr.good();
326 //----------------------------------------------------------------------
328 template <class _CharT, class _Traits>
330 : public basic_istream<_CharT, _Traits>,
331 public basic_ostream<_CharT, _Traits>
334 typedef basic_ios<_CharT, _Traits> _Basic_ios;
336 explicit basic_iostream(basic_streambuf<_CharT, _Traits>* __buf);
337 virtual ~basic_iostream();
340 # if defined (_STLP_USE_TEMPLATE_EXPORT)
341 _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<char, char_traits<char> >;
343 # if ! defined (_STLP_NO_WCHAR_T)
344 _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<wchar_t, char_traits<wchar_t> >;
346 # endif /* _STLP_USE_TEMPLATE_EXPORT */
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(); }
354 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
355 # include <stl/_istream.c>
358 #endif /* _STLP_INTERNAL_ISTREAM */