/home/ntakagi/work/STLport-5.1.5/stlport/stl/_istream.h

Go to the documentation of this file.
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
00019 #define _STLP_INTERNAL_ISTREAM
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_INTERNAL_CWCHAR)
00023 #  include <stl/_cwchar.h>
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 #if defined (_STLP_USE_TEMPLATE_EXPORT)
00043 template <class _CharT, class _Traits>
00044 class _Isentry;
00045 #endif
00046 
00047 struct _No_Skip_WS {};        // Dummy class used by sentry.
00048 
00049 template <class _CharT, class _Traits>
00050 bool _M_init_skip(basic_istream<_CharT, _Traits>& __istr);
00051 template <class _CharT, class _Traits>
00052 bool _M_init_noskip(basic_istream<_CharT, _Traits>& __istr);
00053 
00054 //----------------------------------------------------------------------
00055 // Class basic_istream, a class that performs formatted input through
00056 // a stream buffer.
00057 
00058 // The second template parameter, _Traits, defaults to char_traits<_CharT>.
00059 // The default is declared in header <iosfwd>, and it isn't declared here
00060 // because C++ language rules do not allow it to be declared twice.
00061 
00062 template <class _CharT, class _Traits>
00063 class basic_istream : virtual public basic_ios<_CharT, _Traits> {
00064   typedef basic_istream<_CharT, _Traits> _Self;
00065 
00066 #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310)
00067   //explicitely defined as private to avoid warnings:
00068   basic_istream(_Self const&);
00069   _Self& operator = (_Self const&);
00070 #endif
00071 
00072 public:
00073                          // Types
00074   typedef _CharT                     char_type;
00075   typedef typename _Traits::int_type int_type;
00076   typedef typename _Traits::pos_type pos_type;
00077   typedef typename _Traits::off_type off_type;
00078   typedef _Traits                    traits_type;
00079   typedef basic_ios<_CharT, _Traits>     _Basic_ios;
00080 
00081   typedef basic_ios<_CharT, _Traits>& (_STLP_CALL *__ios_fn)(basic_ios<_CharT, _Traits>&);
00082   typedef ios_base& (_STLP_CALL *__ios_base_fn)(ios_base&);
00083   typedef _Self& (_STLP_CALL *__istream_fn)(_Self&);
00084 
00085 public:                         // Constructor and destructor.
00086   explicit basic_istream(basic_streambuf<_CharT, _Traits>* __buf) :
00087     basic_ios<_CharT, _Traits>(), _M_gcount(0) {
00088     this->init(__buf);
00089   }
00090   ~basic_istream() {};
00091 
00092 public:                         // Nested sentry class.
00093 
00094 public:                         // Hooks for manipulators.  The arguments are
00095                                 // function pointers.
00096   _Self& operator>> (__istream_fn __f) { return __f(*this); }
00097   _Self& operator>> (__ios_fn __f) {  __f(*this); return *this; }
00098   _Self& operator>> (__ios_base_fn __f) { __f(*this); return *this; }
00099 
00100 public:                         // Formatted input of numbers.
00101   _Self& operator>> (short& __val);
00102   _Self& operator>> (int& __val);
00103   _Self& operator>> (unsigned short& __val);
00104   _Self& operator>> (unsigned int& __val);
00105   _Self& operator>> (long& __val);
00106   _Self& operator>> (unsigned long& __val);
00107 #ifdef _STLP_LONG_LONG
00108   _Self& operator>> (_STLP_LONG_LONG& __val);
00109   _Self& operator>> (unsigned _STLP_LONG_LONG& __val);
00110 #endif
00111   _Self& operator>> (float& __val);
00112   _Self& operator>> (double& __val);
00113 # ifndef _STLP_NO_LONG_DOUBLE
00114   _Self& operator>> (long double& __val);
00115 # endif
00116 # ifndef _STLP_NO_BOOL
00117   _Self& operator>> (bool& __val);
00118 # endif
00119   _Self& operator>> (void*& __val);
00120 
00121 public:                         // Copying characters into a streambuf.
00122   _Self& operator>>(basic_streambuf<_CharT, _Traits>*);
00123 
00124 public:                         // Unformatted input.
00125   streamsize gcount() const { return _M_gcount; }
00126   int_type peek();
00127 
00128 public:                         // get() for single characters
00129   int_type get();
00130   _Self& get(char_type& __c);
00131 
00132 public:                         // get() for character arrays.
00133   _Self& get(char_type* __s, streamsize __n, char_type __delim);
00134   _Self& get(char_type* __s, streamsize __n)
00135     { return get(__s, __n, this->widen('\n')); }
00136 
00137 public:                         // get() for streambufs
00138   _Self& get(basic_streambuf<_CharT, _Traits>& __buf,
00139                      char_type __delim);
00140   _Self& get(basic_streambuf<_CharT, _Traits>& __buf)
00141     { return get(__buf, this->widen('\n')); }
00142 
00143 public:                         // getline()
00144   _Self& getline(char_type* __s, streamsize __n, char_type delim);
00145   _Self& getline(char_type* __s, streamsize __n)
00146     { return getline(__s, __n, this->widen('\n')); }
00147 
00148 public:                         // read(), readsome(), ignore()
00149   _Self& ignore();
00150   _Self& ignore(streamsize __n);
00151 #if (defined (_STLP_MSVC) && _STLP_MSVC < 1200)
00152   inline
00153 #endif
00154   _Self& ignore(streamsize __n, int_type __delim);
00155 
00156   _Self& read(char_type* __s, streamsize __n);
00157   streamsize readsome(char_type* __s, streamsize __n);
00158 
00159 public:                         // putback
00160   _Self& putback(char_type __c);
00161   _Self& unget();
00162 
00163 public:                         // Positioning and buffer control.
00164   int sync();
00165 
00166   pos_type tellg();
00167   _Self& seekg(pos_type __pos);
00168   _Self& seekg(off_type, ios_base::seekdir);
00169 
00170 public:                         // Helper functions for non-member extractors.
00171   void _M_formatted_get(_CharT& __c);
00172   void _M_formatted_get(_CharT* __s);
00173   void _M_skip_whitespace(bool __set_failbit);
00174 
00175 private:                        // Number of characters extracted by the
00176   streamsize _M_gcount;         // most recent unformatted input function.
00177 
00178 public:
00179 
00180 #if defined (_STLP_USE_TEMPLATE_EXPORT)
00181   // If we are using DLL specs, we have not to use inner classes
00182   // end class declaration here
00183   typedef _Isentry<_CharT, _Traits>      sentry;
00184 };
00185 #  define sentry _Isentry
00186 template <class _CharT, class _Traits>
00187 class _Isentry {
00188   typedef _Isentry<_CharT, _Traits> _Self;
00189 # else
00190   class sentry {
00191     typedef sentry _Self;
00192 #endif
00193 
00194   private:
00195     const bool _M_ok;
00196     //    basic_streambuf<_CharT, _Traits>* _M_buf;
00197 
00198   public:
00199     typedef _Traits traits_type;
00200 
00201     explicit sentry(basic_istream<_CharT, _Traits>& __istr,
00202                     bool __noskipws = false) :
00203       _M_ok((__noskipws || !(__istr.flags() & ios_base::skipws)) ? _M_init_noskip(__istr) : _M_init_skip(__istr) )
00204       /* , _M_buf(__istr.rdbuf()) */
00205       {}
00206 
00207     // Calling this constructor is the same as calling the previous one with
00208     // __noskipws = true, except that it doesn't require a runtime test.
00209     sentry(basic_istream<_CharT, _Traits>& __istr, _No_Skip_WS) : /* _M_buf(__istr.rdbuf()), */
00210       _M_ok(_M_init_noskip(__istr)) {}
00211 
00212     ~sentry() {}
00213 
00214     operator bool() const { return _M_ok; }
00215 
00216   private:                        // Disable assignment and copy constructor.
00217     //Implementation is here only to avoid warning with some compilers.
00218     sentry(const _Self&) : _M_ok(false) {}
00219     _Self& operator=(const _Self&) { return *this; }
00220   };
00221 
00222 # if defined (_STLP_USE_TEMPLATE_EXPORT)
00223 #  undef sentry
00224 # else
00225   // close basic_istream class definition here
00226 };
00227 # endif
00228 
00229 # if defined (_STLP_USE_TEMPLATE_EXPORT)
00230 _STLP_EXPORT_TEMPLATE_CLASS _Isentry<char, char_traits<char> >;
00231 _STLP_EXPORT_TEMPLATE_CLASS basic_istream<char, char_traits<char> >;
00232 #  if ! defined (_STLP_NO_WCHAR_T)
00233 _STLP_EXPORT_TEMPLATE_CLASS _Isentry<wchar_t, char_traits<wchar_t> >;
00234 _STLP_EXPORT_TEMPLATE_CLASS basic_istream<wchar_t, char_traits<wchar_t> >;
00235 #  endif
00236 # endif /* _STLP_USE_TEMPLATE_EXPORT */
00237 
00238 // Non-member character and string extractor functions.
00239 template <class _CharT, class _Traits>
00240 inline basic_istream<_CharT, _Traits>& _STLP_CALL
00241 operator>>(basic_istream<_CharT, _Traits>& __in_str, _CharT& __c) {
00242   __in_str._M_formatted_get(__c);
00243   return __in_str;
00244 }
00245 
00246 template <class _Traits>
00247 inline basic_istream<char, _Traits>& _STLP_CALL
00248 operator>>(basic_istream<char, _Traits>& __in_str, unsigned char& __c) {
00249   __in_str._M_formatted_get(__REINTERPRET_CAST(char&,__c));
00250   return __in_str;
00251 }
00252 
00253 template <class _Traits>
00254 inline basic_istream<char, _Traits>& _STLP_CALL
00255 operator>>(basic_istream<char, _Traits>& __in_str, signed char& __c) {
00256   __in_str._M_formatted_get(__REINTERPRET_CAST(char&,__c));
00257   return __in_str;
00258 }
00259 
00260 template <class _CharT, class _Traits>
00261 inline basic_istream<_CharT, _Traits>& _STLP_CALL
00262 operator>>(basic_istream<_CharT, _Traits>& __in_str, _CharT* __s) {
00263   __in_str._M_formatted_get(__s);
00264   return __in_str;
00265 }
00266 
00267 template <class _Traits>
00268 inline basic_istream<char, _Traits>& _STLP_CALL
00269 operator>>(basic_istream<char, _Traits>& __in_str, unsigned char* __s) {
00270   __in_str._M_formatted_get(__REINTERPRET_CAST(char*,__s));
00271   return __in_str;
00272 }
00273 
00274 template <class _Traits>
00275 inline basic_istream<char, _Traits>& _STLP_CALL
00276 operator>>(basic_istream<char, _Traits>& __in_str, signed char* __s) {
00277   __in_str._M_formatted_get(__REINTERPRET_CAST(char*,__s));
00278   return __in_str;
00279 }
00280 
00281 //----------------------------------------------------------------------
00282 // istream manipulator.
00283 template <class _CharT, class _Traits>
00284 basic_istream<_CharT, _Traits>& _STLP_CALL
00285 ws(basic_istream<_CharT, _Traits>& __istr) {
00286   if (!__istr.eof()) {
00287     typedef typename basic_istream<_CharT, _Traits>::sentry      _Sentry;
00288     _Sentry __sentry(__istr, _No_Skip_WS()); // Don't skip whitespace.
00289     if (__sentry)
00290       __istr._M_skip_whitespace(false);
00291   }
00292   return __istr;
00293 }
00294 
00295 // Helper functions for istream<>::sentry constructor.
00296 template <class _CharT, class _Traits>
00297 inline bool _M_init_skip(basic_istream<_CharT, _Traits>& __istr) {
00298   if (__istr.good()) {
00299     if (__istr.tie())
00300       __istr.tie()->flush();
00301 
00302     __istr._M_skip_whitespace(true);
00303   }
00304 
00305   if (!__istr.good()) {
00306     __istr.setstate(ios_base::failbit);
00307     return false;
00308   } else
00309     return true;
00310 }
00311 
00312 template <class _CharT, class _Traits>
00313 inline bool _M_init_noskip(basic_istream<_CharT, _Traits>& __istr) {
00314   if (__istr.good()) {
00315     if (__istr.tie())
00316       __istr.tie()->flush();
00317 
00318     if (!__istr.rdbuf())
00319       __istr.setstate(ios_base::badbit);
00320   }
00321   else
00322     __istr.setstate(ios_base::failbit);
00323   return __istr.good();
00324 }
00325 
00326 //----------------------------------------------------------------------
00327 // Class iostream.
00328 template <class _CharT, class _Traits>
00329 class basic_iostream
00330   : public basic_istream<_CharT, _Traits>,
00331     public basic_ostream<_CharT, _Traits>
00332 {
00333 public:
00334   typedef basic_ios<_CharT, _Traits> _Basic_ios;
00335 
00336   explicit basic_iostream(basic_streambuf<_CharT, _Traits>* __buf);
00337   virtual ~basic_iostream();
00338 };
00339 
00340 # if defined (_STLP_USE_TEMPLATE_EXPORT)
00341 _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<char, char_traits<char> >;
00342 
00343 #  if ! defined (_STLP_NO_WCHAR_T)
00344 _STLP_EXPORT_TEMPLATE_CLASS basic_iostream<wchar_t, char_traits<wchar_t> >;
00345 #  endif
00346 # endif /* _STLP_USE_TEMPLATE_EXPORT */
00347 
00348 template <class _CharT, class _Traits>
00349 basic_streambuf<_CharT, _Traits>* _STLP_CALL _M_get_istreambuf(basic_istream<_CharT, _Traits>& __istr)
00350 { return __istr.rdbuf(); }
00351 
00352 _STLP_END_NAMESPACE
00353 
00354 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
00355 #  include <stl/_istream.c>
00356 #endif
00357 
00358 #endif /* _STLP_INTERNAL_ISTREAM */
00359 
00360 // Local Variables:
00361 // mode:C++
00362 // End:



Generated on Mon Mar 10 15:32:25 2008 by  doxygen 1.5.1