xref: /reactos/sdk/include/c++/stlport/stl/_istream.h (revision c2c66aff)
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.
basic_istream(basic_streambuf<_CharT,_Traits> * __buf)86   explicit basic_istream(basic_streambuf<_CharT, _Traits>* __buf) :
87     basic_ios<_CharT, _Traits>(), _M_gcount(0) {
88     this->init(__buf);
89   }
~basic_istream()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.
gcount()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);
get(char_type * __s,streamsize __n)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);
get(basic_streambuf<_CharT,_Traits> & __buf)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);
getline(char_type * __s,streamsize __n)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   _Self& ignore(streamsize __n, int_type __delim);
152 
153   _Self& read(char_type* __s, streamsize __n);
154   streamsize readsome(char_type* __s, streamsize __n);
155 
156 public:                         // putback
157   _Self& putback(char_type __c);
158   _Self& unget();
159 
160 public:                         // Positioning and buffer control.
161   int sync();
162 
163   pos_type tellg();
164   _Self& seekg(pos_type __pos);
165   _Self& seekg(off_type, ios_base::seekdir);
166 
167 public:                         // Helper functions for non-member extractors.
168   void _M_formatted_get(_CharT& __c);
169   void _M_formatted_get(_CharT* __s);
170   void _M_skip_whitespace(bool __set_failbit);
171 
172 private:                        // Number of characters extracted by the
173   streamsize _M_gcount;         // most recent unformatted input function.
174 
175 public:
176 
177 #if defined (_STLP_USE_TEMPLATE_EXPORT)
178   // If we are using DLL specs, we have not to use inner classes
179   // end class declaration here
180   typedef _Isentry<_CharT, _Traits>      sentry;
181 };
182 #  define sentry _Isentry
183 template <class _CharT, class _Traits>
184 class _Isentry {
185   typedef _Isentry<_CharT, _Traits> _Self;
186 # else
187   class sentry {
188     typedef sentry _Self;
189 #endif
190 
191   private:
192     const bool _M_ok;
193     //    basic_streambuf<_CharT, _Traits>* _M_buf;
194 
195   public:
196     typedef _Traits traits_type;
197 
198     explicit sentry(basic_istream<_CharT, _Traits>& __istr,
199                     bool __noskipws = false) :
200       _M_ok((__noskipws || !(__istr.flags() & ios_base::skipws)) ? _M_init_noskip(__istr) : _M_init_skip(__istr) )
201       /* , _M_buf(__istr.rdbuf()) */
202       {}
203 
204     // Calling this constructor is the same as calling the previous one with
205     // __noskipws = true, except that it doesn't require a runtime test.
sentry(basic_istream<_CharT,_Traits> & __istr,_No_Skip_WS)206     sentry(basic_istream<_CharT, _Traits>& __istr, _No_Skip_WS) : /* _M_buf(__istr.rdbuf()), */
207       _M_ok(_M_init_noskip(__istr)) {}
208 
~sentry()209     ~sentry() {}
210 
211     operator bool() const { return _M_ok; }
212 
213   private:                        // Disable assignment and copy constructor.
214     //Implementation is here only to avoid warning with some compilers.
sentry(const _Self &)215     sentry(const _Self&) : _M_ok(false) {}
216     _Self& operator=(const _Self&) { return *this; }
217   };
218 
219 # if defined (_STLP_USE_TEMPLATE_EXPORT)
220 #  undef sentry
221 # else
222   // close basic_istream class definition here
223 };
224 # endif
225 
226 # if defined (_STLP_USE_TEMPLATE_EXPORT)
227 _STLP_EXPORT_TEMPLATE_CLASS _Isentry<char, char_traits<char> >;
228 _STLP_EXPORT_TEMPLATE_CLASS basic_istream<char, char_traits<char> >;
229 #  if ! defined (_STLP_NO_WCHAR_T)
230 _STLP_EXPORT_TEMPLATE_CLASS _Isentry<wchar_t, char_traits<wchar_t> >;
231 _STLP_EXPORT_TEMPLATE_CLASS basic_istream<wchar_t, char_traits<wchar_t> >;
232 #  endif
233 # endif /* _STLP_USE_TEMPLATE_EXPORT */
234 
235 // Non-member character and string extractor functions.
236 template <class _CharT, class _Traits>
237 inline basic_istream<_CharT, _Traits>& _STLP_CALL
238 operator>>(basic_istream<_CharT, _Traits>& __in_str, _CharT& __c) {
239   __in_str._M_formatted_get(__c);
240   return __in_str;
241 }
242 
243 template <class _Traits>
244 inline basic_istream<char, _Traits>& _STLP_CALL
245 operator>>(basic_istream<char, _Traits>& __in_str, unsigned char& __c) {
246   __in_str._M_formatted_get(__REINTERPRET_CAST(char&,__c));
247   return __in_str;
248 }
249 
250 template <class _Traits>
251 inline basic_istream<char, _Traits>& _STLP_CALL
252 operator>>(basic_istream<char, _Traits>& __in_str, signed char& __c) {
253   __in_str._M_formatted_get(__REINTERPRET_CAST(char&,__c));
254   return __in_str;
255 }
256 
257 template <class _CharT, class _Traits>
258 inline basic_istream<_CharT, _Traits>& _STLP_CALL
259 operator>>(basic_istream<_CharT, _Traits>& __in_str, _CharT* __s) {
260   __in_str._M_formatted_get(__s);
261   return __in_str;
262 }
263 
264 template <class _Traits>
265 inline basic_istream<char, _Traits>& _STLP_CALL
266 operator>>(basic_istream<char, _Traits>& __in_str, unsigned char* __s) {
267   __in_str._M_formatted_get(__REINTERPRET_CAST(char*,__s));
268   return __in_str;
269 }
270 
271 template <class _Traits>
272 inline basic_istream<char, _Traits>& _STLP_CALL
273 operator>>(basic_istream<char, _Traits>& __in_str, signed char* __s) {
274   __in_str._M_formatted_get(__REINTERPRET_CAST(char*,__s));
275   return __in_str;
276 }
277 
278 //----------------------------------------------------------------------
279 // istream manipulator.
280 template <class _CharT, class _Traits>
281 basic_istream<_CharT, _Traits>& _STLP_CALL
282 ws(basic_istream<_CharT, _Traits>& __istr) {
283   if (!__istr.eof()) {
284     typedef typename basic_istream<_CharT, _Traits>::sentry      _Sentry;
285     _Sentry __sentry(__istr, _No_Skip_WS()); // Don't skip whitespace.
286     if (__sentry)
287       __istr._M_skip_whitespace(false);
288   }
289   return __istr;
290 }
291 
292 // Helper functions for istream<>::sentry constructor.
293 template <class _CharT, class _Traits>
294 inline bool _M_init_skip(basic_istream<_CharT, _Traits>& __istr) {
295   if (__istr.good()) {
296     if (__istr.tie())
297       __istr.tie()->flush();
298 
299     __istr._M_skip_whitespace(true);
300   }
301 
302   if (!__istr.good()) {
303     __istr.setstate(ios_base::failbit);
304     return false;
305   } else
306     return true;
307 }
308 
309 template <class _CharT, class _Traits>
310 inline bool _M_init_noskip(basic_istream<_CharT, _Traits>& __istr) {
311   if (__istr.good()) {
312     if (__istr.tie())
313       __istr.tie()->flush();
314 
315     if (!__istr.rdbuf())
316       __istr.setstate(ios_base::badbit);
317   }
318   else
319     __istr.setstate(ios_base::failbit);
320   return __istr.good();
321 }
322 
323 //----------------------------------------------------------------------
324 // Class iostream.
325 template <class _CharT, class _Traits>
326 class basic_iostream
327   : public basic_istream<_CharT, _Traits>,
328     public basic_ostream<_CharT, _Traits>
329 {
330 public:
331   typedef basic_ios<_CharT, _Traits> _Basic_ios;
332 
333   explicit basic_iostream(basic_streambuf<_CharT, _Traits>* __buf);
334   virtual ~basic_iostream();
335 };
336 
337 # if defined (_STLP_USE_TEMPLATE_EXPORT)
338 _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<char, char_traits<char> >;
339 
340 #  if ! defined (_STLP_NO_WCHAR_T)
341 _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<wchar_t, char_traits<wchar_t> >;
342 #  endif
343 # endif /* _STLP_USE_TEMPLATE_EXPORT */
344 
345 template <class _CharT, class _Traits>
346 basic_streambuf<_CharT, _Traits>* _STLP_CALL _M_get_istreambuf(basic_istream<_CharT, _Traits>& __istr)
347 { return __istr.rdbuf(); }
348 
349 _STLP_END_NAMESPACE
350 
351 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
352 #  include <stl/_istream.c>
353 #endif
354 
355 #endif /* _STLP_INTERNAL_ISTREAM */
356 
357 // Local Variables:
358 // mode:C++
359 // End:
360