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 _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
00049
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
00057
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;
00064
00065
00066
private:
00067
00068
00069
00070
00071
00072
00073
00074
mutable streambuf_type* _M_sbuf;
00075
int_type _M_c;
00076
00077
public:
00078
00079 istreambuf_iterator() throw()
00080 : _M_sbuf(0), _M_c(
traits_type::eof()) { }
00081
00082
00083 istreambuf_iterator(
istream_type& __s)
throw()
00084 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
00085
00086
00087 istreambuf_iterator(
streambuf_type* __s)
throw()
00088 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
00089
00090
00091
00092
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
00106
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
const int_type __eof = traits_type::eof();
00114
if (_M_sbuf && traits_type::eq_int_type(_M_sbuf->sbumpc(), __eof))
00115 _M_sbuf = 0;
00116
else
00117 _M_c = __eof;
00118
return *
this;
00119 }
00120
00121
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
const int_type __eof = traits_type::eof();
00130
istreambuf_iterator __old = *
this;
00131
if (_M_sbuf
00132 && traits_type::eq_int_type((__old._M_c = _M_sbuf->sbumpc()),
00133 __eof))
00134 _M_sbuf = 0;
00135
else
00136 _M_c = __eof;
00137
return __old;
00138 }
00139
00140
00141
00142
00143
00144
bool
00145 equal(
const istreambuf_iterator& __b)
const
00146
{
00147
const bool __thiseof = _M_at_eof();
00148
const bool __beof = __b._M_at_eof();
00149
return (__thiseof && __beof || (!__thiseof && !__beof));
00150 }
00151
00152
private:
00153 int_type
00154 _M_get()
const
00155
{
00156
const int_type __eof = traits_type::eof();
00157 int_type __ret = __eof;
00158
if (_M_sbuf)
00159 {
00160
if (!traits_type::eq_int_type(_M_c, __eof))
00161 __ret = _M_c;
00162
else if (traits_type::eq_int_type((__ret = _M_sbuf->sgetc()),
00163 __eof))
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
00189
00190
template<
typename _CharT,
typename _Traits>
00191 class ostreambuf_iterator
00192 :
public iterator<output_iterator_tag, void, void, void, void>
00193 {
00194
public:
00195
00196
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;
00202
00203
00204
private:
00205
streambuf_type* _M_sbuf;
00206
bool _M_failed;
00207
00208
public:
00209
00210 ostreambuf_iterator(
ostream_type& __s)
throw ()
00211 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
00212
00213
00214 ostreambuf_iterator(
streambuf_type* __s)
throw ()
00215 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
00216
00217
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
00227
00228
ostreambuf_iterator&
00229 operator*()
00230 {
return *
this; }
00231
00232
00233
ostreambuf_iterator&
00234 operator++(
int)
00235 {
return *
this; }
00236
00237
00238
ostreambuf_iterator&
00239 operator++()
00240 {
return *
this; }
00241
00242
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