3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
31 /** @file streambuf_iterator.h
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
36 #ifndef _STREAMBUF_ITERATOR_H
37 #define _STREAMBUF_ITERATOR_H 1
39 #pragma GCC system_header
42 #include <debug/debug.h>
44 _GLIBCXX_BEGIN_NAMESPACE(std)
46 // 24.5.3 Template class istreambuf_iterator
47 /// Provides input iterator semantics for streambufs.
48 template<typename _CharT, typename _Traits>
49 class istreambuf_iterator
50 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
57 typedef _CharT char_type;
58 typedef _Traits traits_type;
59 typedef typename _Traits::int_type int_type;
60 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
61 typedef basic_istream<_CharT, _Traits> istream_type;
64 template<typename _CharT2>
65 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
66 ostreambuf_iterator<_CharT2> >::__type
67 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
68 ostreambuf_iterator<_CharT2>);
70 template<typename _CharT2>
71 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
73 __copy_aux(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
76 template<typename _CharT2>
77 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
78 istreambuf_iterator<_CharT2> >::__type
79 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
83 // 24.5.3 istreambuf_iterator
85 // If the end of stream is reached (streambuf_type::sgetc()
86 // returns traits_type::eof()), the iterator becomes equal to
87 // the "end of stream" iterator value.
88 // NB: This implementation assumes the "end of stream" value
90 mutable streambuf_type* _M_sbuf;
91 mutable int_type _M_c;
94 /// Construct end of input stream iterator.
95 istreambuf_iterator() throw()
96 : _M_sbuf(0), _M_c(traits_type::eof()) { }
98 /// Construct start of input stream iterator.
99 istreambuf_iterator(istream_type& __s) throw()
100 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
102 /// Construct start of streambuf iterator.
103 istreambuf_iterator(streambuf_type* __s) throw()
104 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
106 /// Return the current character pointed to by iterator. This returns
107 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
108 /// operator*() on an end of stream is undefined.
112 #ifdef _GLIBCXX_DEBUG_PEDANTIC
113 // Dereferencing a past-the-end istreambuf_iterator is a
114 // libstdc++ extension
115 __glibcxx_requires_cond(!_M_at_eof(),
116 _M_message(__gnu_debug::__msg_deref_istreambuf)
117 ._M_iterator(*this));
119 return traits_type::to_char_type(_M_get());
122 /// Advance the iterator. Calls streambuf.sbumpc().
126 __glibcxx_requires_cond(!_M_at_eof(),
127 _M_message(__gnu_debug::__msg_inc_istreambuf)
128 ._M_iterator(*this));
132 _M_c = traits_type::eof();
137 /// Advance the iterator. Calls streambuf.sbumpc().
141 __glibcxx_requires_cond(!_M_at_eof(),
142 _M_message(__gnu_debug::__msg_inc_istreambuf)
143 ._M_iterator(*this));
145 istreambuf_iterator __old = *this;
148 __old._M_c = _M_sbuf->sbumpc();
149 _M_c = traits_type::eof();
154 // _GLIBCXX_RESOLVE_LIB_DEFECTS
155 // 110 istreambuf_iterator::equal not const
156 // NB: there is also number 111 (NAD, Future) pending on this function.
157 /// Return true both iterators are end or both are not end.
159 equal(const istreambuf_iterator& __b) const
161 const bool __thiseof = _M_at_eof();
162 const bool __beof = __b._M_at_eof();
163 return (__thiseof && __beof || (!__thiseof && !__beof));
170 const int_type __eof = traits_type::eof();
171 int_type __ret = __eof;
174 if (!traits_type::eq_int_type(_M_c, __eof))
176 else if (!traits_type::eq_int_type((__ret = _M_sbuf->sgetc()),
188 const int_type __eof = traits_type::eof();
189 return traits_type::eq_int_type(_M_get(), __eof);
193 template<typename _CharT, typename _Traits>
195 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
196 const istreambuf_iterator<_CharT, _Traits>& __b)
197 { return __a.equal(__b); }
199 template<typename _CharT, typename _Traits>
201 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
202 const istreambuf_iterator<_CharT, _Traits>& __b)
203 { return !__a.equal(__b); }
205 /// Provides output iterator semantics for streambufs.
206 template<typename _CharT, typename _Traits>
207 class ostreambuf_iterator
208 : public iterator<output_iterator_tag, void, void, void, void>
214 typedef _CharT char_type;
215 typedef _Traits traits_type;
216 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
217 typedef basic_ostream<_CharT, _Traits> ostream_type;
220 template<typename _CharT2>
221 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
222 ostreambuf_iterator<_CharT2> >::__type
223 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
224 ostreambuf_iterator<_CharT2>);
227 streambuf_type* _M_sbuf;
231 /// Construct output iterator from ostream.
232 ostreambuf_iterator(ostream_type& __s) throw ()
233 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
235 /// Construct output iterator from streambuf.
236 ostreambuf_iterator(streambuf_type* __s) throw ()
237 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
239 /// Write character to streambuf. Calls streambuf.sputc().
241 operator=(_CharT __c)
244 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
264 /// Return true if previous operator=() failed.
266 failed() const throw()
267 { return _M_failed; }
270 _M_put(const _CharT* __ws, streamsize __len)
272 if (__builtin_expect(!_M_failed, true)
273 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
280 // Overloads for streambuf iterators.
281 template<typename _CharT>
282 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
283 ostreambuf_iterator<_CharT> >::__type
284 copy(istreambuf_iterator<_CharT> __first,
285 istreambuf_iterator<_CharT> __last,
286 ostreambuf_iterator<_CharT> __result)
288 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
291 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
293 __result._M_failed = true;
298 template<typename _CharT>
299 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
300 ostreambuf_iterator<_CharT> >::__type
301 __copy_aux(_CharT* __first, _CharT* __last,
302 ostreambuf_iterator<_CharT> __result)
304 const streamsize __num = __last - __first;
306 __result._M_put(__first, __num);
310 template<typename _CharT>
311 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
312 ostreambuf_iterator<_CharT> >::__type
313 __copy_aux(const _CharT* __first, const _CharT* __last,
314 ostreambuf_iterator<_CharT> __result)
316 const streamsize __num = __last - __first;
318 __result._M_put(__first, __num);
322 template<typename _CharT>
323 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
325 __copy_aux(istreambuf_iterator<_CharT> __first,
326 istreambuf_iterator<_CharT> __last, _CharT* __result)
328 typedef istreambuf_iterator<_CharT> __is_iterator_type;
329 typedef typename __is_iterator_type::traits_type traits_type;
330 typedef typename __is_iterator_type::streambuf_type streambuf_type;
331 typedef typename traits_type::int_type int_type;
333 if (__first._M_sbuf && !__last._M_sbuf)
335 streambuf_type* __sb = __first._M_sbuf;
336 int_type __c = __sb->sgetc();
337 while (!traits_type::eq_int_type(__c, traits_type::eof()))
339 const streamsize __n = __sb->egptr() - __sb->gptr();
342 traits_type::copy(__result, __sb->gptr(), __n);
345 __c = __sb->underflow();
349 *__result++ = traits_type::to_char_type(__c);
350 __c = __sb->snextc();
357 template<typename _CharT>
358 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
359 istreambuf_iterator<_CharT> >::__type
360 find(istreambuf_iterator<_CharT> __first,
361 istreambuf_iterator<_CharT> __last, const _CharT& __val)
363 typedef istreambuf_iterator<_CharT> __is_iterator_type;
364 typedef typename __is_iterator_type::traits_type traits_type;
365 typedef typename __is_iterator_type::streambuf_type streambuf_type;
366 typedef typename traits_type::int_type int_type;
368 if (__first._M_sbuf && !__last._M_sbuf)
370 const int_type __ival = traits_type::to_int_type(__val);
371 streambuf_type* __sb = __first._M_sbuf;
372 int_type __c = __sb->sgetc();
373 while (!traits_type::eq_int_type(__c, traits_type::eof())
374 && !traits_type::eq_int_type(__c, __ival))
376 streamsize __n = __sb->egptr() - __sb->gptr();
379 const _CharT* __p = traits_type::find(__sb->gptr(),
382 __n = __p - __sb->gptr();
387 __c = __sb->snextc();
390 if (!traits_type::eq_int_type(__c, traits_type::eof()))
398 _GLIBCXX_END_NAMESPACE