3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2009, 2010, 2011
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 namespace std _GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 * @addtogroup iterators
49 // 24.5.3 Template class istreambuf_iterator
50 /// Provides input iterator semantics for streambufs.
51 template<typename _CharT, typename _Traits>
52 class istreambuf_iterator
53 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
60 typedef _CharT char_type;
61 typedef _Traits traits_type;
62 typedef typename _Traits::int_type int_type;
63 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
64 typedef basic_istream<_CharT, _Traits> istream_type;
67 template<typename _CharT2>
68 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
69 ostreambuf_iterator<_CharT2> >::__type
70 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
71 ostreambuf_iterator<_CharT2>);
73 template<bool _IsMove, typename _CharT2>
74 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
76 __copy_move_a2(istreambuf_iterator<_CharT2>,
77 istreambuf_iterator<_CharT2>, _CharT2*);
79 template<typename _CharT2>
80 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
81 istreambuf_iterator<_CharT2> >::__type
82 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
86 // 24.5.3 istreambuf_iterator
88 // If the end of stream is reached (streambuf_type::sgetc()
89 // returns traits_type::eof()), the iterator becomes equal to
90 // the "end of stream" iterator value.
91 // NB: This implementation assumes the "end of stream" value
93 mutable streambuf_type* _M_sbuf;
94 mutable int_type _M_c;
97 /// Construct end of input stream iterator.
98 _GLIBCXX_CONSTEXPR istreambuf_iterator() throw()
99 : _M_sbuf(0), _M_c(traits_type::eof()) { }
101 /// Construct start of input stream iterator.
102 istreambuf_iterator(istream_type& __s) throw()
103 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
105 /// Construct start of streambuf iterator.
106 istreambuf_iterator(streambuf_type* __s) throw()
107 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
109 /// Return the current character pointed to by iterator. This returns
110 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
111 /// operator*() on an end of stream is undefined.
115 #ifdef _GLIBCXX_DEBUG_PEDANTIC
116 // Dereferencing a past-the-end istreambuf_iterator is a
117 // libstdc++ extension
118 __glibcxx_requires_cond(!_M_at_eof(),
119 _M_message(__gnu_debug::__msg_deref_istreambuf)
120 ._M_iterator(*this));
122 return traits_type::to_char_type(_M_get());
125 /// Advance the iterator. Calls streambuf.sbumpc().
129 __glibcxx_requires_cond(!_M_at_eof(),
130 _M_message(__gnu_debug::__msg_inc_istreambuf)
131 ._M_iterator(*this));
135 _M_c = traits_type::eof();
140 /// Advance the iterator. Calls streambuf.sbumpc().
144 __glibcxx_requires_cond(!_M_at_eof(),
145 _M_message(__gnu_debug::__msg_inc_istreambuf)
146 ._M_iterator(*this));
148 istreambuf_iterator __old = *this;
151 __old._M_c = _M_sbuf->sbumpc();
152 _M_c = traits_type::eof();
157 // _GLIBCXX_RESOLVE_LIB_DEFECTS
158 // 110 istreambuf_iterator::equal not const
159 // NB: there is also number 111 (NAD, Future) pending on this function.
160 /// Return true both iterators are end or both are not end.
162 equal(const istreambuf_iterator& __b) const
163 { return _M_at_eof() == __b._M_at_eof(); }
169 const int_type __eof = traits_type::eof();
170 int_type __ret = __eof;
173 if (!traits_type::eq_int_type(_M_c, __eof))
175 else if (!traits_type::eq_int_type((__ret = _M_sbuf->sgetc()),
187 const int_type __eof = traits_type::eof();
188 return traits_type::eq_int_type(_M_get(), __eof);
192 template<typename _CharT, typename _Traits>
194 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
195 const istreambuf_iterator<_CharT, _Traits>& __b)
196 { return __a.equal(__b); }
198 template<typename _CharT, typename _Traits>
200 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
201 const istreambuf_iterator<_CharT, _Traits>& __b)
202 { return !__a.equal(__b); }
204 /// Provides output iterator semantics for streambufs.
205 template<typename _CharT, typename _Traits>
206 class ostreambuf_iterator
207 : public iterator<output_iterator_tag, void, void, void, void>
213 typedef _CharT char_type;
214 typedef _Traits traits_type;
215 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
216 typedef basic_ostream<_CharT, _Traits> ostream_type;
219 template<typename _CharT2>
220 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
221 ostreambuf_iterator<_CharT2> >::__type
222 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
223 ostreambuf_iterator<_CharT2>);
226 streambuf_type* _M_sbuf;
230 /// Construct output iterator from ostream.
231 ostreambuf_iterator(ostream_type& __s) throw ()
232 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
234 /// Construct output iterator from streambuf.
235 ostreambuf_iterator(streambuf_type* __s) throw ()
236 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
238 /// Write character to streambuf. Calls streambuf.sputc().
240 operator=(_CharT __c)
243 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
263 /// Return true if previous operator=() failed.
265 failed() const throw()
266 { return _M_failed; }
269 _M_put(const _CharT* __ws, streamsize __len)
271 if (__builtin_expect(!_M_failed, true)
272 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
279 // Overloads for streambuf iterators.
280 template<typename _CharT>
281 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
282 ostreambuf_iterator<_CharT> >::__type
283 copy(istreambuf_iterator<_CharT> __first,
284 istreambuf_iterator<_CharT> __last,
285 ostreambuf_iterator<_CharT> __result)
287 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
290 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
292 __result._M_failed = true;
297 template<bool _IsMove, typename _CharT>
298 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
299 ostreambuf_iterator<_CharT> >::__type
300 __copy_move_a2(_CharT* __first, _CharT* __last,
301 ostreambuf_iterator<_CharT> __result)
303 const streamsize __num = __last - __first;
305 __result._M_put(__first, __num);
309 template<bool _IsMove, typename _CharT>
310 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
311 ostreambuf_iterator<_CharT> >::__type
312 __copy_move_a2(const _CharT* __first, const _CharT* __last,
313 ostreambuf_iterator<_CharT> __result)
315 const streamsize __num = __last - __first;
317 __result._M_put(__first, __num);
321 template<bool _IsMove, typename _CharT>
322 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
324 __copy_move_a2(istreambuf_iterator<_CharT> __first,
325 istreambuf_iterator<_CharT> __last, _CharT* __result)
327 typedef istreambuf_iterator<_CharT> __is_iterator_type;
328 typedef typename __is_iterator_type::traits_type traits_type;
329 typedef typename __is_iterator_type::streambuf_type streambuf_type;
330 typedef typename traits_type::int_type int_type;
332 if (__first._M_sbuf && !__last._M_sbuf)
334 streambuf_type* __sb = __first._M_sbuf;
335 int_type __c = __sb->sgetc();
336 while (!traits_type::eq_int_type(__c, traits_type::eof()))
338 const streamsize __n = __sb->egptr() - __sb->gptr();
341 traits_type::copy(__result, __sb->gptr(), __n);
342 __sb->__safe_gbump(__n);
344 __c = __sb->underflow();
348 *__result++ = traits_type::to_char_type(__c);
349 __c = __sb->snextc();
356 template<typename _CharT>
357 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
358 istreambuf_iterator<_CharT> >::__type
359 find(istreambuf_iterator<_CharT> __first,
360 istreambuf_iterator<_CharT> __last, const _CharT& __val)
362 typedef istreambuf_iterator<_CharT> __is_iterator_type;
363 typedef typename __is_iterator_type::traits_type traits_type;
364 typedef typename __is_iterator_type::streambuf_type streambuf_type;
365 typedef typename traits_type::int_type int_type;
367 if (__first._M_sbuf && !__last._M_sbuf)
369 const int_type __ival = traits_type::to_int_type(__val);
370 streambuf_type* __sb = __first._M_sbuf;
371 int_type __c = __sb->sgetc();
372 while (!traits_type::eq_int_type(__c, traits_type::eof())
373 && !traits_type::eq_int_type(__c, __ival))
375 streamsize __n = __sb->egptr() - __sb->gptr();
378 const _CharT* __p = traits_type::find(__sb->gptr(),
381 __n = __p - __sb->gptr();
382 __sb->__safe_gbump(__n);
386 __c = __sb->snextc();
389 if (!traits_type::eq_int_type(__c, traits_type::eof()))
397 // @} group iterators
399 _GLIBCXX_END_NAMESPACE_VERSION