home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _istream.h < prev    next >
C/C++ Source or Header  |  2001-05-03  |  12KB  |  345 lines

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