00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef _CPP_BITS_STREAMBUF_ITERATOR_H
00037 #define _CPP_BITS_STREAMBUF_ITERATOR_H 1
00038
00039 #pragma GCC system_header
00040
00041 #include <streambuf>
00042
00043
00044
00045 namespace std
00046 {
00047
00048 template<typename _CharT, typename _Traits>
00049 class istreambuf_iterator
00050 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
00051 _CharT*, _CharT&>
00052 {
00053 public:
00054
00055 typedef _CharT char_type;
00056 typedef _Traits traits_type;
00057 typedef typename _Traits::int_type int_type;
00058 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
00059 typedef basic_istream<_CharT, _Traits> istream_type;
00060
00061 private:
00062
00063
00064
00065
00066
00067
00068
00069 mutable streambuf_type* _M_sbuf;
00070 int_type _M_c;
00071
00072 public:
00073 istreambuf_iterator() throw()
00074 : _M_sbuf(0), _M_c(traits_type::eof()) { }
00075
00076 istreambuf_iterator(istream_type& __s) throw()
00077 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
00078
00079 istreambuf_iterator(streambuf_type* __s) throw()
00080 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
00081
00082
00083 char_type
00084 operator*() const
00085 { return traits_type::to_char_type(_M_get()); }
00086
00087 istreambuf_iterator&
00088 operator++()
00089 {
00090 const int_type __eof = traits_type::eof();
00091 if (_M_sbuf && traits_type::eq_int_type(_M_sbuf->sbumpc(), __eof))
00092 _M_sbuf = 0;
00093 else
00094 _M_c = __eof;
00095 return *this;
00096 }
00097
00098 istreambuf_iterator
00099 operator++(int)
00100 {
00101 const int_type __eof = traits_type::eof();
00102 istreambuf_iterator __old = *this;
00103 if (_M_sbuf
00104 && traits_type::eq_int_type((__old._M_c = _M_sbuf->sbumpc()),
00105 __eof))
00106 _M_sbuf = 0;
00107 else
00108 _M_c = __eof;
00109 return __old;
00110 }
00111
00112 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00113
00114
00115 bool
00116 equal(const istreambuf_iterator& __b) const
00117 {
00118 const int_type __eof = traits_type::eof();
00119 bool __thiseof = traits_type::eq_int_type(_M_get(), __eof);
00120 bool __beof = traits_type::eq_int_type(__b._M_get(), __eof);
00121 return (__thiseof && __beof || (!__thiseof && !__beof));
00122 }
00123 #endif
00124
00125 private:
00126 int_type
00127 _M_get() const
00128 {
00129 const int_type __eof = traits_type::eof();
00130 int_type __ret = __eof;
00131 if (_M_sbuf)
00132 {
00133 if (!traits_type::eq_int_type(_M_c, __eof))
00134 __ret = _M_c;
00135 else
00136 if (traits_type::eq_int_type((__ret = _M_sbuf->sgetc()), __eof))
00137 _M_sbuf = 0;
00138 }
00139 return __ret;
00140 }
00141 };
00142
00143 template<typename _CharT, typename _Traits>
00144 inline bool
00145 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
00146 const istreambuf_iterator<_CharT, _Traits>& __b)
00147 { return __a.equal(__b); }
00148
00149 template<typename _CharT, typename _Traits>
00150 inline bool
00151 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
00152 const istreambuf_iterator<_CharT, _Traits>& __b)
00153 { return !__a.equal(__b); }
00154
00155 template<typename _CharT, typename _Traits>
00156 class ostreambuf_iterator
00157 : public iterator<output_iterator_tag, void, void, void, void>
00158 {
00159 public:
00160
00161 typedef _CharT char_type;
00162 typedef _Traits traits_type;
00163 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
00164 typedef basic_ostream<_CharT, _Traits> ostream_type;
00165
00166 private:
00167 streambuf_type* _M_sbuf;
00168 bool _M_failed;
00169
00170 public:
00171 ostreambuf_iterator(ostream_type& __s) throw ()
00172 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
00173
00174 ostreambuf_iterator(streambuf_type* __s) throw ()
00175 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
00176
00177 ostreambuf_iterator&
00178 operator=(_CharT __c)
00179 {
00180 if (!_M_failed &&
00181 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
00182 _M_failed = true;
00183 return *this;
00184 }
00185
00186 ostreambuf_iterator&
00187 operator*() throw()
00188 { return *this; }
00189
00190 ostreambuf_iterator&
00191 operator++(int) throw()
00192 { return *this; }
00193
00194 ostreambuf_iterator&
00195 operator++() throw()
00196 { return *this; }
00197
00198 bool
00199 failed() const throw()
00200 { return _M_failed; }
00201
00202 ostreambuf_iterator&
00203 _M_put(const _CharT* __ws, streamsize __len)
00204 {
00205 if (__builtin_expect(!_M_failed, true) &&
00206 __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len, false))
00207 _M_failed = true;
00208 return *this;
00209 }
00210 };
00211 }
00212 #endif