3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
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 streambuf_iterator.h
28 * This is an internal header file, included by other library headers.
29 * You should not attempt to use it directly.
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)
42 // 24.5.3 Template class istreambuf_iterator
43 /// Provides input iterator semantics for streambufs.
44 template<typename _CharT, typename _Traits>
45 class istreambuf_iterator
46 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
53 typedef _CharT char_type;
54 typedef _Traits traits_type;
55 typedef typename _Traits::int_type int_type;
56 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
57 typedef basic_istream<_CharT, _Traits> istream_type;
60 template<typename _CharT2>
61 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
62 ostreambuf_iterator<_CharT2> >::__type
63 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
64 ostreambuf_iterator<_CharT2>);
66 template<bool _IsMove, typename _CharT2>
67 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
69 __copy_move_a2(istreambuf_iterator<_CharT2>,
70 istreambuf_iterator<_CharT2>, _CharT2*);
72 template<typename _CharT2>
73 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
74 istreambuf_iterator<_CharT2> >::__type
75 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
79 // 24.5.3 istreambuf_iterator
81 // If the end of stream is reached (streambuf_type::sgetc()
82 // returns traits_type::eof()), the iterator becomes equal to
83 // the "end of stream" iterator value.
84 // NB: This implementation assumes the "end of stream" value
86 mutable streambuf_type* _M_sbuf;
87 mutable int_type _M_c;
90 /// Construct end of input stream iterator.
91 istreambuf_iterator() throw()
92 : _M_sbuf(0), _M_c(traits_type::eof()) { }
94 /// Construct start of input stream iterator.
95 istreambuf_iterator(istream_type& __s) throw()
96 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
98 /// Construct start of streambuf iterator.
99 istreambuf_iterator(streambuf_type* __s) throw()
100 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
102 /// Return the current character pointed to by iterator. This returns
103 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
104 /// operator*() on an end of stream is undefined.
108 #ifdef _GLIBCXX_DEBUG_PEDANTIC
109 // Dereferencing a past-the-end istreambuf_iterator is a
110 // libstdc++ extension
111 __glibcxx_requires_cond(!_M_at_eof(),
112 _M_message(__gnu_debug::__msg_deref_istreambuf)
113 ._M_iterator(*this));
115 return traits_type::to_char_type(_M_get());
118 /// Advance the iterator. Calls streambuf.sbumpc().
122 __glibcxx_requires_cond(!_M_at_eof(),
123 _M_message(__gnu_debug::__msg_inc_istreambuf)
124 ._M_iterator(*this));
128 _M_c = traits_type::eof();
133 /// Advance the iterator. Calls streambuf.sbumpc().
137 __glibcxx_requires_cond(!_M_at_eof(),
138 _M_message(__gnu_debug::__msg_inc_istreambuf)
139 ._M_iterator(*this));
141 istreambuf_iterator __old = *this;
144 __old._M_c = _M_sbuf->sbumpc();
145 _M_c = traits_type::eof();
150 // _GLIBCXX_RESOLVE_LIB_DEFECTS
151 // 110 istreambuf_iterator::equal not const
152 // NB: there is also number 111 (NAD, Future) pending on this function.
153 /// Return true both iterators are end or both are not end.
155 equal(const istreambuf_iterator& __b) const
156 { return _M_at_eof() == __b._M_at_eof(); }
162 const int_type __eof = traits_type::eof();
163 int_type __ret = __eof;
166 if (!traits_type::eq_int_type(_M_c, __eof))
168 else if (!traits_type::eq_int_type((__ret = _M_sbuf->sgetc()),
180 const int_type __eof = traits_type::eof();
181 return traits_type::eq_int_type(_M_get(), __eof);
185 template<typename _CharT, typename _Traits>
187 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
188 const istreambuf_iterator<_CharT, _Traits>& __b)
189 { return __a.equal(__b); }
191 template<typename _CharT, typename _Traits>
193 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
194 const istreambuf_iterator<_CharT, _Traits>& __b)
195 { return !__a.equal(__b); }
197 /// Provides output iterator semantics for streambufs.
198 template<typename _CharT, typename _Traits>
199 class ostreambuf_iterator
200 : public iterator<output_iterator_tag, void, void, void, void>
206 typedef _CharT char_type;
207 typedef _Traits traits_type;
208 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
209 typedef basic_ostream<_CharT, _Traits> ostream_type;
212 template<typename _CharT2>
213 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
214 ostreambuf_iterator<_CharT2> >::__type
215 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
216 ostreambuf_iterator<_CharT2>);
219 streambuf_type* _M_sbuf;
223 /// Construct output iterator from ostream.
224 ostreambuf_iterator(ostream_type& __s) throw ()
225 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
227 /// Construct output iterator from streambuf.
228 ostreambuf_iterator(streambuf_type* __s) throw ()
229 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
231 /// Write character to streambuf. Calls streambuf.sputc().
233 operator=(_CharT __c)
236 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
256 /// Return true if previous operator=() failed.
258 failed() const throw()
259 { return _M_failed; }
262 _M_put(const _CharT* __ws, streamsize __len)
264 if (__builtin_expect(!_M_failed, true)
265 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
272 // Overloads for streambuf iterators.
273 template<typename _CharT>
274 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
275 ostreambuf_iterator<_CharT> >::__type
276 copy(istreambuf_iterator<_CharT> __first,
277 istreambuf_iterator<_CharT> __last,
278 ostreambuf_iterator<_CharT> __result)
280 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
283 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
285 __result._M_failed = true;
290 template<bool _IsMove, typename _CharT>
291 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
292 ostreambuf_iterator<_CharT> >::__type
293 __copy_move_a2(_CharT* __first, _CharT* __last,
294 ostreambuf_iterator<_CharT> __result)
296 const streamsize __num = __last - __first;
298 __result._M_put(__first, __num);
302 template<bool _IsMove, typename _CharT>
303 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
304 ostreambuf_iterator<_CharT> >::__type
305 __copy_move_a2(const _CharT* __first, const _CharT* __last,
306 ostreambuf_iterator<_CharT> __result)
308 const streamsize __num = __last - __first;
310 __result._M_put(__first, __num);
314 template<bool _IsMove, typename _CharT>
315 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
317 __copy_move_a2(istreambuf_iterator<_CharT> __first,
318 istreambuf_iterator<_CharT> __last, _CharT* __result)
320 typedef istreambuf_iterator<_CharT> __is_iterator_type;
321 typedef typename __is_iterator_type::traits_type traits_type;
322 typedef typename __is_iterator_type::streambuf_type streambuf_type;
323 typedef typename traits_type::int_type int_type;
325 if (__first._M_sbuf && !__last._M_sbuf)
327 streambuf_type* __sb = __first._M_sbuf;
328 int_type __c = __sb->sgetc();
329 while (!traits_type::eq_int_type(__c, traits_type::eof()))
331 const streamsize __n = __sb->egptr() - __sb->gptr();
334 traits_type::copy(__result, __sb->gptr(), __n);
337 __c = __sb->underflow();
341 *__result++ = traits_type::to_char_type(__c);
342 __c = __sb->snextc();
349 template<typename _CharT>
350 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
351 istreambuf_iterator<_CharT> >::__type
352 find(istreambuf_iterator<_CharT> __first,
353 istreambuf_iterator<_CharT> __last, const _CharT& __val)
355 typedef istreambuf_iterator<_CharT> __is_iterator_type;
356 typedef typename __is_iterator_type::traits_type traits_type;
357 typedef typename __is_iterator_type::streambuf_type streambuf_type;
358 typedef typename traits_type::int_type int_type;
360 if (__first._M_sbuf && !__last._M_sbuf)
362 const int_type __ival = traits_type::to_int_type(__val);
363 streambuf_type* __sb = __first._M_sbuf;
364 int_type __c = __sb->sgetc();
365 while (!traits_type::eq_int_type(__c, traits_type::eof())
366 && !traits_type::eq_int_type(__c, __ival))
368 streamsize __n = __sb->egptr() - __sb->gptr();
371 const _CharT* __p = traits_type::find(__sb->gptr(),
374 __n = __p - __sb->gptr();
379 __c = __sb->snextc();
382 if (!traits_type::eq_int_type(__c, traits_type::eof()))
390 _GLIBCXX_END_NAMESPACE