_istreambuf_iterator.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 // WARNING: This is an internal header file, included by other C++
00019 // standard library headers.  You should not attempt to use this header
00020 // file directly.
00021 
00022 
00023 #ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
00024 #define _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
00025 
00026 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
00027 # include <stl/_iterator_base.h>
00028 #endif
00029 
00030 #ifndef _STLP_INTERNAL_STREAMBUF
00031 # include <stl/_streambuf.h>
00032 #endif
00033 
00034 _STLP_BEGIN_NAMESPACE
00035 
00036 // defined in _istream.h
00037 template <class _CharT, class _Traits>
00038 extern basic_streambuf<_CharT, _Traits>* _STLP_CALL _M_get_istreambuf(basic_istream<_CharT, _Traits>& ) ;
00039 
00040 // We do not read any characters until operator* is called. operator* calls sgetc 
00041 // unless the iterator is unchanged from the last call in which case a cached value is
00042 // used. Calls to operator++ use sbumpc.
00043 
00044 template<class _CharT, class _Traits>
00045 class istreambuf_iterator
00046 {
00047 public:
00048   typedef _CharT                           char_type;
00049   typedef _Traits                          traits_type;
00050   typedef typename _Traits::int_type       int_type;
00051   typedef basic_streambuf<_CharT, _Traits> streambuf_type;
00052   typedef basic_istream<_CharT, _Traits>   istream_type;
00053 
00054   typedef input_iterator_tag               iterator_category;
00055   typedef _CharT                           value_type;
00056   typedef typename _Traits::off_type       difference_type;
00057   typedef const _CharT*                    pointer;
00058   typedef const _CharT&                    reference;
00059 
00060 public:
00061   istreambuf_iterator(streambuf_type* __p = 0) { this->_M_init(__p); }
00062   //  istreambuf_iterator(basic_istream<_CharT, _Traits>& __is) { this->_M_init(_M_get_istreambuf(__is)); }
00063   inline istreambuf_iterator(basic_istream<_CharT, _Traits>& __is);
00064 
00065   char_type operator*() const { this->_M_getc(); return _M_c; }
00066   istreambuf_iterator<_CharT, _Traits>& operator++() { this->_M_bumpc(); return *this; }
00067   istreambuf_iterator<_CharT, _Traits>  operator++(int);
00068 
00069   bool equal(const istreambuf_iterator<_CharT, _Traits>& __i) const {
00070     if (this->_M_buf)
00071       this->_M_getc();
00072     if (__i._M_buf)
00073       __i._M_getc(); 
00074     return this->_M_eof == __i._M_eof;
00075   }
00076 
00077 private:
00078   void _M_init(streambuf_type* __p) {
00079     _M_buf = __p;
00080     _M_eof = !__p;
00081     //    _M_is_initialized = _M_eof;
00082     _M_have_c = false;
00083   }
00084 
00085   void _M_getc() const {
00086     if (_M_have_c)
00087       return;
00088     int_type __c = _M_buf->sgetc();
00089 # if !defined (_STLP_NEED_MUTABLE) /* && ! defined (__SUNPRO_CC) */
00090     _M_c = traits_type::to_char_type(__c);
00091     _M_eof = traits_type::eq_int_type(__c, traits_type::eof());
00092     _M_have_c = true;
00093 # else
00094     typedef istreambuf_iterator<_CharT,_Traits> _Self;
00095     _Self* __that = __CONST_CAST(_Self*, this);
00096     __that->_M_c = __STATIC_CAST(_CharT, traits_type::to_char_type(__c));
00097     __that->_M_eof = traits_type::eq_int_type(__c, traits_type::eof());
00098     __that->_M_have_c = true;
00099 # endif
00100   }
00101 
00102   void _M_bumpc() {
00103     _M_buf->sbumpc();
00104     _M_have_c = false;
00105   }
00106 
00107 private:
00108   streambuf_type* _M_buf;
00109   mutable _CharT _M_c;
00110   mutable unsigned char _M_eof;
00111   mutable unsigned char _M_have_c;
00112 };
00113 
00114 template<class _CharT, class _Traits>
00115 inline istreambuf_iterator<_CharT, _Traits>::istreambuf_iterator(basic_istream<_CharT, _Traits>& __is) 
00116 { this->_M_init(_M_get_istreambuf(__is)); }
00117 
00118 template<class _CharT, class _Traits>
00119 inline bool _STLP_CALL operator==(const istreambuf_iterator<_CharT, _Traits>& __x,
00120                                   const istreambuf_iterator<_CharT, _Traits>& __y) {
00121   return __x.equal(__y);
00122 }
00123 
00124 #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
00125 
00126 template<class _CharT, class _Traits>
00127 inline bool _STLP_CALL operator!=(const istreambuf_iterator<_CharT, _Traits>& __x,
00128                                   const istreambuf_iterator<_CharT, _Traits>& __y) {
00129   return !__x.equal(__y);
00130 }
00131 
00132 #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
00133 
00134 # if defined (_STLP_USE_TEMPLATE_EXPORT)
00135 _STLP_EXPORT_TEMPLATE_CLASS istreambuf_iterator<char, char_traits<char> >;
00136 #  if defined (INSTANTIATE_WIDE_STREAMS)
00137 _STLP_EXPORT_TEMPLATE_CLASS istreambuf_iterator<wchar_t, char_traits<wchar_t> >;
00138 #  endif
00139 # endif /* _STLP_USE_TEMPLATE_EXPORT */
00140 
00141 # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
00142 template <class _CharT, class _Traits>
00143 inline input_iterator_tag _STLP_CALL iterator_category(const istreambuf_iterator<_CharT, _Traits>&) { return input_iterator_tag(); }
00144 template <class _CharT, class _Traits>
00145 inline streamoff* _STLP_CALL 
00146 distance_type(const istreambuf_iterator<_CharT, _Traits>&) { return (streamoff*)0; }
00147 template <class _CharT, class _Traits>
00148 inline _CharT* _STLP_CALL value_type(const istreambuf_iterator<_CharT, _Traits>&) { return (_CharT*)0; }
00149 # endif
00150 
00151 template <class _CharT, class _Traits>
00152 istreambuf_iterator<_CharT, _Traits>
00153 istreambuf_iterator<_CharT, _Traits>::operator++(int) {
00154   istreambuf_iterator<_CharT, _Traits> __tmp = *this;
00155   this->_M_bumpc();
00156   this->_M_have_c = false;
00157   return __tmp;
00158 }
00159 
00160 _STLP_END_NAMESPACE
00161 
00162 #endif /* _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H */
00163 
00164 // Local Variables:
00165 // mode:C++
00166 // End:
00167 

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