_istream.h

00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999 
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted 
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */ 
00018 #ifndef _STLP_INTERNAL_ISTREAM_H
00019 #define _STLP_INTERNAL_ISTREAM_H
00020 
00021 // this block is included by _ostream.h, we include it here to lower #include level
00022 # if defined (_STLP_HAS_WCHAR_T) && !defined (_STLP_CWCHAR)
00023 #  include <cwchar>
00024 # endif
00025 
00026 # ifndef _STLP_INTERNAL_IOS_H
00027 #  include <stl/_ios.h>                  // For basic_ios<>.  Includes <iosfwd>.
00028 # endif
00029 
00030 #ifndef _STLP_INTERNAL_OSTREAM_H
00031 # include <stl/_ostream.h>              // Needed as a base class of basic_iostream.
00032 #endif
00033 
00034 #ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
00035 # include <stl/_istreambuf_iterator.h>
00036 #endif
00037 
00038 #include <stl/_ctraits_fns.h>    // Helper functions that allow char traits
00039                                 // to be used as function objects.
00040 _STLP_BEGIN_NAMESPACE
00041 
00042 template <class _CharT, class _Traits, class _Number> 
00043 ios_base::iostate _STLP_CALL
00044 _M_get_num(basic_istream<_CharT, _Traits>& __that, _Number& __val);
00045 
00046 #if defined (_STLP_USE_TEMPLATE_EXPORT)
00047 template <class _CharT, class _Traits>
00048 class _Isentry;
00049 #endif
00050 
00051 struct _No_Skip_WS {};        // Dummy class used by sentry.
00052 
00053 template <class _CharT, class _Traits>
00054 bool _M_init_skip(basic_istream<_CharT, _Traits>& __is);
00055 template <class _CharT, class _Traits>
00056 bool _M_init_noskip(basic_istream<_CharT, _Traits>& __is);
00057 
00058 //----------------------------------------------------------------------
00059 // Class basic_istream, a class that performs formatted input through
00060 // a stream buffer.
00061 
00062 // The second template parameter, _Traits, defaults to char_traits<_CharT>.
00063 // The default is declared in header <iosfwd>, and it isn't declared here
00064 // because C++ language rules do not allow it to be declared twice. 
00065 
00066 template <class _CharT, class _Traits>
00067 class basic_istream : virtual public basic_ios<_CharT, _Traits> {
00068 public:
00069                          // Types
00070   typedef _CharT                     char_type;
00071   typedef typename _Traits::int_type int_type;
00072   typedef typename _Traits::pos_type pos_type;
00073   typedef typename _Traits::off_type off_type;
00074   typedef _Traits                    traits_type;
00075   typedef basic_ios<_CharT, _Traits>     _Basic_ios;
00076   typedef basic_istream<_CharT, _Traits> _Self;
00077 
00078   typedef basic_ios<_CharT, _Traits>& (_STLP_CALL *__ios_fn)(basic_ios<_CharT, _Traits>&);
00079   typedef ios_base& (_STLP_CALL *__ios_base_fn)(ios_base&);
00080   typedef _Self& (_STLP_CALL *__istream_fn)(_Self&);
00081 
00082 public:                         // Constructor and destructor.
00083   explicit basic_istream(basic_streambuf<_CharT, _Traits>* __buf) :
00084     basic_ios<_CharT, _Traits>(), _M_gcount(0) {
00085     this->init(__buf);
00086   }
00087   ~basic_istream() {};
00088 
00089 public:                         // Nested sentry class.
00090 
00091 public:                         // Hooks for manipulators.  The arguments are
00092                                 // function pointers.
00093   _Self& operator>> (__istream_fn __f) { return __f(*this); }
00094   _Self& operator>> (__ios_fn __f) {  __f(*this); return *this; }
00095   _Self& operator>> (__ios_base_fn __f) { __f(*this); return *this; }
00096 
00097 public:                         // Formatted input of numbers.
00098   _Self& operator>> (short& __val) {
00099     long __lval;
00100     unsigned short __uval;
00101    _M_get_num(*this, __lval);
00102     __val = __STATIC_CAST(short, __lval);
00103     __uval = __lval;
00104     // check if we lose digits
00105     //    if ((__val != __lval) && ((unsigned short)__val != __lval))
00106     if ((__val != __lval) && ((long)__uval != __lval))
00107       this->setstate(ios_base::failbit); 
00108     return *this; 
00109   }
00110   _Self& operator>> (int& __val) { 
00111     long __lval;
00112     unsigned int __uval;
00113     _M_get_num(*this, __lval);
00114     __val = __lval;
00115     __uval = __lval;
00116     // check if we lose digits
00117     //    if ((__val != __lval) && ((unsigned int)__val != __lval))
00118     if ((__val != __lval) && ((long)__uval != __lval))
00119       this->setstate(ios_base::failbit); 
00120     return *this;
00121   }
00122   _Self& operator>> (unsigned short& __val) { _M_get_num(*this, __val); return *this; }
00123   _Self& operator>> (unsigned int& __val) { _M_get_num(*this, __val); return *this; }
00124   _Self& operator>> (long& __val) { _M_get_num(*this, __val); return *this; }
00125   _Self& operator>> (unsigned long& __val) { _M_get_num(*this, __val); return *this; }
00126 #ifdef _STLP_LONG_LONG
00127   _Self& operator>> (_STLP_LONG_LONG& __val) { _M_get_num(*this, __val); return *this; }
00128   _Self& operator>> (unsigned _STLP_LONG_LONG& __val) { _M_get_num(*this, __val); return *this; }
00129 #endif 
00130   _Self& operator>> (float& __val)  { _M_get_num(*this, __val); return *this; }
00131   _Self& operator>> (double& __val) { _M_get_num(*this, __val); return *this; }
00132 # ifndef _STLP_NO_LONG_DOUBLE
00133   _Self& operator>> (long double& __val) { _M_get_num(*this, __val); return *this; }
00134 # endif
00135 # ifndef _STLP_NO_BOOL
00136   _Self& operator>> (bool& __val) { _M_get_num(*this, __val); return *this; }
00137 # endif
00138   _Self& operator>> (void*& __val) { _M_get_num(*this, __val); return *this; }
00139 
00140 public:                         // Copying characters into a streambuf.
00141   _Self& operator>>(basic_streambuf<_CharT, _Traits>*);
00142 
00143 public:                         // Unformatted input.
00144   streamsize gcount() const { return _M_gcount; }
00145   int_type peek();
00146 
00147 public:                         // get() for single characters
00148   int_type get();
00149   _Self& get(char_type& __c);
00150 
00151 public:                         // get() for character arrays.
00152   _Self& get(char_type* __s, streamsize __n, char_type __delim);
00153   _Self& get(char_type* __s, streamsize __n)
00154     { return get(__s, __n, this->widen('\n')); }
00155 
00156 public:                         // get() for streambufs
00157   _Self& get(basic_streambuf<_CharT, _Traits>& __buf,
00158                      char_type __delim);
00159   _Self& get(basic_streambuf<_CharT, _Traits>& __buf)
00160     { return get(__buf, this->widen('\n')); }
00161 
00162 public:                         // getline()
00163   _Self& getline(char_type* __s, streamsize __n, char_type delim);
00164   _Self& getline(char_type* __s, streamsize __n)
00165     { return getline(__s, __n, this->widen('\n')); }
00166 
00167 public:                         // read(), readsome(), ignore()
00168   _Self& ignore();
00169   _Self& ignore(streamsize __n);
00170 #if (defined (_STLP_MSVC) && _STLP_MSVC < 1200)
00171   inline
00172 #endif
00173   _Self& ignore(streamsize __n, int_type __delim);
00174 
00175   _Self& read(char_type* __s, streamsize __n);
00176   streamsize readsome(char_type* __s, streamsize __n);
00177 
00178 public:                         // putback
00179   _Self& putback(char_type __c);
00180   _Self& unget();
00181 
00182 public:                         // Positioning and buffer control.
00183   int sync();
00184 
00185   pos_type tellg();
00186   _Self& seekg(pos_type __pos);
00187   _Self& seekg(off_type, ios_base::seekdir);
00188 
00189 public:                         // Helper functions for non-member extractors.
00190   void _M_formatted_get(_CharT& __c);
00191   void _M_formatted_get(_CharT* __s);
00192   void _M_skip_whitespace(bool __set_failbit);
00193 
00194 private:                        // Number of characters extracted by the
00195   streamsize _M_gcount;         // most recent unformatted input function.
00196 
00197 public:
00198 
00199 #if defined (_STLP_USE_TEMPLATE_EXPORT)
00200   // If we are using DLL specs, we have not to use inner classes
00201   // end class declaration here
00202   typedef _Isentry<_CharT, _Traits>      sentry;
00203 };
00204 #  define sentry _Isentry
00205 template <class _CharT, class _Traits>
00206 class _Isentry {
00207   typedef _Isentry<_CharT, _Traits> _Self;
00208 # else
00209   class sentry {
00210     typedef sentry _Self;
00211 #endif
00212     
00213   private:
00214     const bool _M_ok;
00215     //    basic_streambuf<_CharT, _Traits>* _M_buf;
00216         
00217   public:
00218     typedef _Traits traits_type;
00219     
00220     explicit sentry(basic_istream<_CharT, _Traits>& __is,
00221                     bool __noskipws = false) : 
00222       _M_ok((__noskipws || !(__is.flags() & ios_base::skipws)) ? _M_init_noskip(__is) :  _M_init_skip(__is) )
00223       /* , _M_buf(__is.rdbuf()) */
00224       {}
00225     
00226     // Calling this constructor is the same as calling the previous one with 
00227     // __noskipws = true, except that it doesn't require a runtime test.
00228     sentry(basic_istream<_CharT, _Traits>& __is, _No_Skip_WS) : /* _M_buf(__is.rdbuf()), */
00229       _M_ok(_M_init_noskip(__is)) {}
00230     
00231     ~sentry() {}
00232     
00233     operator bool() const { return _M_ok; }
00234     
00235   private:                        // Disable assignment and copy constructor.
00236     sentry(const _Self&) : _M_ok(false) {}
00237     void operator=(const _Self&) {}
00238   };
00239   
00240 # if defined (_STLP_USE_TEMPLATE_EXPORT)
00241 #  undef sentry
00242 # else
00243   // close basic_istream class definition here
00244 };
00245 # endif
00246 
00247 # if defined (_STLP_USE_TEMPLATE_EXPORT)
00248 _STLP_EXPORT_TEMPLATE_CLASS _Isentry<char, char_traits<char> >;
00249 _STLP_EXPORT_TEMPLATE_CLASS basic_istream<char, char_traits<char> >;
00250 #  if ! defined (_STLP_NO_WCHAR_T)
00251 _STLP_EXPORT_TEMPLATE_CLASS _Isentry<wchar_t, char_traits<wchar_t> >;
00252 _STLP_EXPORT_TEMPLATE_CLASS basic_istream<wchar_t, char_traits<wchar_t> >;
00253 #  endif
00254 # endif /* _STLP_USE_TEMPLATE_EXPORT */
00255 
00256 // Non-member character and string extractor functions.
00257 
00258 template <class _CharT, class _Traits>
00259 inline basic_istream<_CharT, _Traits>& _STLP_CALL  
00260 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c) {
00261   __in._M_formatted_get(__c);
00262   return __in;
00263 }
00264 
00265 template <class _Traits>
00266 inline basic_istream<char, _Traits>& _STLP_CALL  
00267 operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c) {
00268   __in._M_formatted_get(__REINTERPRET_CAST(char&,__c));
00269   return __in;
00270 }
00271 
00272 template <class _Traits>
00273 inline basic_istream<char, _Traits>& _STLP_CALL 
00274 operator>>(basic_istream<char, _Traits>& __in, signed char& __c) {
00275   __in._M_formatted_get(__REINTERPRET_CAST(char&,__c));
00276   return __in;
00277 }
00278 
00279 template <class _CharT, class _Traits>
00280 inline basic_istream<_CharT, _Traits>& _STLP_CALL 
00281 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s) {
00282   __in._M_formatted_get(__s);
00283   return __in;
00284 }
00285 
00286 template <class _Traits>
00287 inline basic_istream<char, _Traits>& _STLP_CALL 
00288 operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s) {
00289   __in._M_formatted_get(__REINTERPRET_CAST(char*,__s));
00290   return __in;
00291 }
00292 
00293 template <class _Traits>
00294 inline basic_istream<char, _Traits>& _STLP_CALL 
00295 operator>>(basic_istream<char, _Traits>& __in, signed char* __s) {
00296   __in._M_formatted_get(__REINTERPRET_CAST(char*,__s));
00297   return __in;
00298 }
00299 
00300 //----------------------------------------------------------------------
00301 // istream manipulator.
00302 template <class _CharT, class _Traits>
00303 basic_istream<_CharT, _Traits>& _STLP_CALL
00304 ws(basic_istream<_CharT, _Traits>& __is);
00305 
00306 //----------------------------------------------------------------------
00307 // Class iostream.
00308 
00309 template <class _CharT, class _Traits>
00310 class basic_iostream 
00311   : public basic_istream<_CharT, _Traits>,
00312     public basic_ostream<_CharT, _Traits>
00313 {
00314 public:
00315   typedef basic_ios<_CharT, _Traits> _Basic_ios;
00316 
00317   explicit basic_iostream(basic_streambuf<_CharT, _Traits>* __buf);
00318   virtual ~basic_iostream();
00319 };
00320 
00321 # if defined (_STLP_USE_TEMPLATE_EXPORT)
00322 _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<char, char_traits<char> >;
00323 #  if ! defined (_STLP_NO_WCHAR_T)
00324 _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<wchar_t, char_traits<wchar_t> >;
00325 #  endif
00326 # endif /* _STLP_USE_TEMPLATE_EXPORT */
00327 
00328 template <class _CharT, class _Traits>
00329 basic_streambuf<_CharT, _Traits>* _STLP_CALL _M_get_istreambuf(basic_istream<_CharT, _Traits>& __is) 
00330 {
00331   return __is.rdbuf();
00332 }
00333 
00334 _STLP_END_NAMESPACE
00335 
00336 # if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
00337 #  include <stl/_istream.c>
00338 # endif
00339 
00340 #endif /* _STLP_INTERNAL_ISTREAM_H */
00341 
00342 // Local Variables:
00343 // mode:C++
00344 // End:

Generated on Mon Jun 5 10:20:46 2006 for Intelligence.kdevelop by  doxygen 1.4.6