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
00036 #ifndef _STREAMBUF_ITERATOR_H
00037 #define _STREAMBUF_ITERATOR_H 1
00038
00039 #pragma GCC system_header
00040
00041 #include <streambuf>
00042 #include <debug/debug.h>
00043
00044
00045
00046 namespace std
00047 {
00048
00050 template<typename _CharT, typename _Traits>
00051 class istreambuf_iterator
00052 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
00053 _CharT*, _CharT&>
00054 {
00055 public:
00056
00058
00059 typedef _CharT char_type;
00060 typedef _Traits traits_type;
00061 typedef typename _Traits::int_type int_type;
00062 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
00063 typedef basic_istream<_CharT, _Traits> istream_type;
00065
00066 private:
00067
00068
00069
00070
00071
00072
00073
00074 mutable streambuf_type* _M_sbuf;
00075 mutable int_type _M_c;
00076
00077 public:
00079 istreambuf_iterator() throw()
00080 : _M_sbuf(0), _M_c(traits_type::eof()) { }
00081
00083 istreambuf_iterator(istream_type& __s) throw()
00084 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
00085
00087 istreambuf_iterator(streambuf_type* __s) throw()
00088 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
00089
00093 char_type
00094 operator*() const
00095 {
00096 #ifdef _GLIBCXX_DEBUG_PEDANTIC
00097
00098
00099 __glibcxx_requires_cond(!_M_at_eof(),
00100 _M_message(__gnu_debug::__msg_deref_istreambuf)
00101 ._M_iterator(*this));
00102 #endif
00103 return traits_type::to_char_type(_M_get());
00104 }
00105
00107 istreambuf_iterator&
00108 operator++()
00109 {
00110 __glibcxx_requires_cond(!_M_at_eof(),
00111 _M_message(__gnu_debug::__msg_inc_istreambuf)
00112 ._M_iterator(*this));
00113 if (_M_sbuf)
00114 {
00115 _M_sbuf->sbumpc();
00116 _M_c = traits_type::eof();
00117 }
00118 return *this;
00119 }
00120
00122 istreambuf_iterator
00123 operator++(int)
00124 {
00125 __glibcxx_requires_cond(!_M_at_eof(),
00126 _M_message(__gnu_debug::__msg_inc_istreambuf)
00127 ._M_iterator(*this));
00128
00129 istreambuf_iterator __old = *this;
00130 if (_M_sbuf)
00131 {
00132 __old._M_c = _M_sbuf->sbumpc();
00133 _M_c = traits_type::eof();
00134 }
00135 return __old;
00136 }
00137
00138
00139
00140
00142 bool
00143 equal(const istreambuf_iterator& __b) const
00144 {
00145 const bool __thiseof = _M_at_eof();
00146 const bool __beof = __b._M_at_eof();
00147 return (__thiseof && __beof || (!__thiseof && !__beof));
00148 }
00149
00150 private:
00151 int_type
00152 _M_get() const
00153 {
00154 const int_type __eof = traits_type::eof();
00155 int_type __ret = __eof;
00156 if (_M_sbuf)
00157 {
00158 if (!traits_type::eq_int_type(_M_c, __eof))
00159 __ret = _M_c;
00160 else if (!traits_type::eq_int_type((__ret = _M_sbuf->sgetc()),
00161 __eof))
00162 _M_c = __ret;
00163 else
00164 _M_sbuf = 0;
00165 }
00166 return __ret;
00167 }
00168
00169 bool
00170 _M_at_eof() const
00171 {
00172 const int_type __eof = traits_type::eof();
00173 return traits_type::eq_int_type(_M_get(), __eof);
00174 }
00175 };
00176
00177 template<typename _CharT, typename _Traits>
00178 inline bool
00179 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
00180 const istreambuf_iterator<_CharT, _Traits>& __b)
00181 { return __a.equal(__b); }
00182
00183 template<typename _CharT, typename _Traits>
00184 inline bool
00185 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
00186 const istreambuf_iterator<_CharT, _Traits>& __b)
00187 { return !__a.equal(__b); }
00188
00190 template<typename _CharT, typename _Traits>
00191 class ostreambuf_iterator
00192 : public iterator<output_iterator_tag, void, void, void, void>
00193 {
00194 public:
00195
00197
00198 typedef _CharT char_type;
00199 typedef _Traits traits_type;
00200 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
00201 typedef basic_ostream<_CharT, _Traits> ostream_type;
00203
00204 private:
00205 streambuf_type* _M_sbuf;
00206 bool _M_failed;
00207
00208 public:
00210 ostreambuf_iterator(ostream_type& __s) throw ()
00211 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
00212
00214 ostreambuf_iterator(streambuf_type* __s) throw ()
00215 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
00216
00218 ostreambuf_iterator&
00219 operator=(_CharT __c)
00220 {
00221 if (!_M_failed &&
00222 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
00223 _M_failed = true;
00224 return *this;
00225 }
00226
00228 ostreambuf_iterator&
00229 operator*()
00230 { return *this; }
00231
00233 ostreambuf_iterator&
00234 operator++(int)
00235 { return *this; }
00236
00238 ostreambuf_iterator&
00239 operator++()
00240 { return *this; }
00241
00243 bool
00244 failed() const throw()
00245 { return _M_failed; }
00246
00247 ostreambuf_iterator&
00248 _M_put(const _CharT* __ws, streamsize __len)
00249 {
00250 if (__builtin_expect(!_M_failed, true)
00251 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
00252 false))
00253 _M_failed = true;
00254 return *this;
00255 }
00256 };
00257 }
00258 #endif