3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2009, 2010
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file bits/streambuf_iterator.h
28 * This is an internal header file, included by other library headers.
29 * Do not attempt to use it directly. @headername{iterator}
32 #ifndef _STREAMBUF_ITERATOR_H
33 #define _STREAMBUF_ITERATOR_H 1
35 #pragma GCC system_header
38 #include <debug/debug.h>
40 _GLIBCXX_BEGIN_NAMESPACE(std
)
43 * @addtogroup iterators
47 // 24.5.3 Template class istreambuf_iterator
48 /// Provides input iterator semantics for streambufs.
49 template<typename _CharT
, typename _Traits
>
50 class istreambuf_iterator
51 : public iterator
<input_iterator_tag
, _CharT
, typename
_Traits::off_type
,
58 typedef _CharT char_type
;
59 typedef _Traits traits_type
;
60 typedef typename
_Traits::int_type int_type
;
61 typedef basic_streambuf
<_CharT
, _Traits
> streambuf_type
;
62 typedef basic_istream
<_CharT
, _Traits
> istream_type
;
65 template<typename _CharT2
>
66 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
67 ostreambuf_iterator
<_CharT2
> >::__type
68 copy(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
69 ostreambuf_iterator
<_CharT2
>);
71 template<bool _IsMove
, typename _CharT2
>
72 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
74 __copy_move_a2(istreambuf_iterator
<_CharT2
>,
75 istreambuf_iterator
<_CharT2
>, _CharT2
*);
77 template<typename _CharT2
>
78 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
79 istreambuf_iterator
<_CharT2
> >::__type
80 find(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
84 // 24.5.3 istreambuf_iterator
86 // If the end of stream is reached (streambuf_type::sgetc()
87 // returns traits_type::eof()), the iterator becomes equal to
88 // the "end of stream" iterator value.
89 // NB: This implementation assumes the "end of stream" value
91 mutable streambuf_type
* _M_sbuf
;
92 mutable int_type _M_c
;
95 /// Construct end of input stream iterator.
96 _GLIBCXX_CONSTEXPR
istreambuf_iterator() throw()
97 : _M_sbuf(0), _M_c(traits_type::eof()) { }
99 /// Construct start of input stream iterator.
100 istreambuf_iterator(istream_type
& __s
) throw()
101 : _M_sbuf(__s
.rdbuf()), _M_c(traits_type::eof()) { }
103 /// Construct start of streambuf iterator.
104 istreambuf_iterator(streambuf_type
* __s
) throw()
105 : _M_sbuf(__s
), _M_c(traits_type::eof()) { }
107 /// Return the current character pointed to by iterator. This returns
108 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
109 /// operator*() on an end of stream is undefined.
113 #ifdef _GLIBCXX_DEBUG_PEDANTIC
114 // Dereferencing a past-the-end istreambuf_iterator is a
115 // libstdc++ extension
116 __glibcxx_requires_cond(!_M_at_eof(),
117 _M_message(__gnu_debug::__msg_deref_istreambuf
)
118 ._M_iterator(*this));
120 return traits_type::to_char_type(_M_get());
123 /// Advance the iterator. Calls streambuf.sbumpc().
127 __glibcxx_requires_cond(!_M_at_eof(),
128 _M_message(__gnu_debug::__msg_inc_istreambuf
)
129 ._M_iterator(*this));
133 _M_c
= traits_type::eof();
138 /// Advance the iterator. Calls streambuf.sbumpc().
142 __glibcxx_requires_cond(!_M_at_eof(),
143 _M_message(__gnu_debug::__msg_inc_istreambuf
)
144 ._M_iterator(*this));
146 istreambuf_iterator __old
= *this;
149 __old
._M_c
= _M_sbuf
->sbumpc();
150 _M_c
= traits_type::eof();
155 // _GLIBCXX_RESOLVE_LIB_DEFECTS
156 // 110 istreambuf_iterator::equal not const
157 // NB: there is also number 111 (NAD, Future) pending on this function.
158 /// Return true both iterators are end or both are not end.
160 equal(const istreambuf_iterator
& __b
) const
161 { return _M_at_eof() == __b
._M_at_eof(); }
167 const int_type __eof
= traits_type::eof();
168 int_type __ret
= __eof
;
171 if (!traits_type::eq_int_type(_M_c
, __eof
))
173 else if (!traits_type::eq_int_type((__ret
= _M_sbuf
->sgetc()),
185 const int_type __eof
= traits_type::eof();
186 return traits_type::eq_int_type(_M_get(), __eof
);
190 template<typename _CharT
, typename _Traits
>
192 operator==(const istreambuf_iterator
<_CharT
, _Traits
>& __a
,
193 const istreambuf_iterator
<_CharT
, _Traits
>& __b
)
194 { return __a
.equal(__b
); }
196 template<typename _CharT
, typename _Traits
>
198 operator!=(const istreambuf_iterator
<_CharT
, _Traits
>& __a
,
199 const istreambuf_iterator
<_CharT
, _Traits
>& __b
)
200 { return !__a
.equal(__b
); }
202 /// Provides output iterator semantics for streambufs.
203 template<typename _CharT
, typename _Traits
>
204 class ostreambuf_iterator
205 : public iterator
<output_iterator_tag
, void, void, void, void>
211 typedef _CharT char_type
;
212 typedef _Traits traits_type
;
213 typedef basic_streambuf
<_CharT
, _Traits
> streambuf_type
;
214 typedef basic_ostream
<_CharT
, _Traits
> ostream_type
;
217 template<typename _CharT2
>
218 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
219 ostreambuf_iterator
<_CharT2
> >::__type
220 copy(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
221 ostreambuf_iterator
<_CharT2
>);
224 streambuf_type
* _M_sbuf
;
228 /// Construct output iterator from ostream.
229 ostreambuf_iterator(ostream_type
& __s
) throw ()
230 : _M_sbuf(__s
.rdbuf()), _M_failed(!_M_sbuf
) { }
232 /// Construct output iterator from streambuf.
233 ostreambuf_iterator(streambuf_type
* __s
) throw ()
234 : _M_sbuf(__s
), _M_failed(!_M_sbuf
) { }
236 /// Write character to streambuf. Calls streambuf.sputc().
238 operator=(_CharT __c
)
241 _Traits::eq_int_type(_M_sbuf
->sputc(__c
), _Traits::eof()))
261 /// Return true if previous operator=() failed.
263 failed() const throw()
264 { return _M_failed
; }
267 _M_put(const _CharT
* __ws
, streamsize __len
)
269 if (__builtin_expect(!_M_failed
, true)
270 && __builtin_expect(this->_M_sbuf
->sputn(__ws
, __len
) != __len
,
277 // Overloads for streambuf iterators.
278 template<typename _CharT
>
279 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
280 ostreambuf_iterator
<_CharT
> >::__type
281 copy(istreambuf_iterator
<_CharT
> __first
,
282 istreambuf_iterator
<_CharT
> __last
,
283 ostreambuf_iterator
<_CharT
> __result
)
285 if (__first
._M_sbuf
&& !__last
._M_sbuf
&& !__result
._M_failed
)
288 __copy_streambufs_eof(__first
._M_sbuf
, __result
._M_sbuf
, __ineof
);
290 __result
._M_failed
= true;
295 template<bool _IsMove
, typename _CharT
>
296 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
297 ostreambuf_iterator
<_CharT
> >::__type
298 __copy_move_a2(_CharT
* __first
, _CharT
* __last
,
299 ostreambuf_iterator
<_CharT
> __result
)
301 const streamsize __num
= __last
- __first
;
303 __result
._M_put(__first
, __num
);
307 template<bool _IsMove
, typename _CharT
>
308 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
309 ostreambuf_iterator
<_CharT
> >::__type
310 __copy_move_a2(const _CharT
* __first
, const _CharT
* __last
,
311 ostreambuf_iterator
<_CharT
> __result
)
313 const streamsize __num
= __last
- __first
;
315 __result
._M_put(__first
, __num
);
319 template<bool _IsMove
, typename _CharT
>
320 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
322 __copy_move_a2(istreambuf_iterator
<_CharT
> __first
,
323 istreambuf_iterator
<_CharT
> __last
, _CharT
* __result
)
325 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
326 typedef typename
__is_iterator_type::traits_type traits_type
;
327 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
328 typedef typename
traits_type::int_type int_type
;
330 if (__first
._M_sbuf
&& !__last
._M_sbuf
)
332 streambuf_type
* __sb
= __first
._M_sbuf
;
333 int_type __c
= __sb
->sgetc();
334 while (!traits_type::eq_int_type(__c
, traits_type::eof()))
336 const streamsize __n
= __sb
->egptr() - __sb
->gptr();
339 traits_type::copy(__result
, __sb
->gptr(), __n
);
342 __c
= __sb
->underflow();
346 *__result
++ = traits_type::to_char_type(__c
);
347 __c
= __sb
->snextc();
354 template<typename _CharT
>
355 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
356 istreambuf_iterator
<_CharT
> >::__type
357 find(istreambuf_iterator
<_CharT
> __first
,
358 istreambuf_iterator
<_CharT
> __last
, const _CharT
& __val
)
360 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
361 typedef typename
__is_iterator_type::traits_type traits_type
;
362 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
363 typedef typename
traits_type::int_type int_type
;
365 if (__first
._M_sbuf
&& !__last
._M_sbuf
)
367 const int_type __ival
= traits_type::to_int_type(__val
);
368 streambuf_type
* __sb
= __first
._M_sbuf
;
369 int_type __c
= __sb
->sgetc();
370 while (!traits_type::eq_int_type(__c
, traits_type::eof())
371 && !traits_type::eq_int_type(__c
, __ival
))
373 streamsize __n
= __sb
->egptr() - __sb
->gptr();
376 const _CharT
* __p
= traits_type::find(__sb
->gptr(),
379 __n
= __p
- __sb
->gptr();
384 __c
= __sb
->snextc();
387 if (!traits_type::eq_int_type(__c
, traits_type::eof()))
395 // @} group iterators
397 _GLIBCXX_END_NAMESPACE