1*404b540aSrobert // Streambuf iterators
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction. Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License. This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert
31*404b540aSrobert /** @file streambuf_iterator.h
32*404b540aSrobert * This is an internal header file, included by other library headers.
33*404b540aSrobert * You should not attempt to use it directly.
34*404b540aSrobert */
35*404b540aSrobert
36*404b540aSrobert #ifndef _STREAMBUF_ITERATOR_H
37*404b540aSrobert #define _STREAMBUF_ITERATOR_H 1
38*404b540aSrobert
39*404b540aSrobert #pragma GCC system_header
40*404b540aSrobert
41*404b540aSrobert #include <streambuf>
42*404b540aSrobert #include <debug/debug.h>
43*404b540aSrobert
_GLIBCXX_BEGIN_NAMESPACE(std)44*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
45*404b540aSrobert
46*404b540aSrobert // 24.5.3 Template class istreambuf_iterator
47*404b540aSrobert /// Provides input iterator semantics for streambufs.
48*404b540aSrobert template<typename _CharT, typename _Traits>
49*404b540aSrobert class istreambuf_iterator
50*404b540aSrobert : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
51*404b540aSrobert _CharT*, _CharT&>
52*404b540aSrobert {
53*404b540aSrobert public:
54*404b540aSrobert // Types:
55*404b540aSrobert //@{
56*404b540aSrobert /// Public typedefs
57*404b540aSrobert typedef _CharT char_type;
58*404b540aSrobert typedef _Traits traits_type;
59*404b540aSrobert typedef typename _Traits::int_type int_type;
60*404b540aSrobert typedef basic_streambuf<_CharT, _Traits> streambuf_type;
61*404b540aSrobert typedef basic_istream<_CharT, _Traits> istream_type;
62*404b540aSrobert //@}
63*404b540aSrobert
64*404b540aSrobert template<typename _CharT2>
65*404b540aSrobert friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
66*404b540aSrobert ostreambuf_iterator<_CharT2> >::__type
67*404b540aSrobert copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
68*404b540aSrobert ostreambuf_iterator<_CharT2>);
69*404b540aSrobert
70*404b540aSrobert template<typename _CharT2>
71*404b540aSrobert friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
72*404b540aSrobert _CharT2*>::__type
73*404b540aSrobert __copy_aux(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
74*404b540aSrobert _CharT2*);
75*404b540aSrobert
76*404b540aSrobert template<typename _CharT2>
77*404b540aSrobert friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
78*404b540aSrobert istreambuf_iterator<_CharT2> >::__type
79*404b540aSrobert find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
80*404b540aSrobert const _CharT2&);
81*404b540aSrobert
82*404b540aSrobert private:
83*404b540aSrobert // 24.5.3 istreambuf_iterator
84*404b540aSrobert // p 1
85*404b540aSrobert // If the end of stream is reached (streambuf_type::sgetc()
86*404b540aSrobert // returns traits_type::eof()), the iterator becomes equal to
87*404b540aSrobert // the "end of stream" iterator value.
88*404b540aSrobert // NB: This implementation assumes the "end of stream" value
89*404b540aSrobert // is EOF, or -1.
90*404b540aSrobert mutable streambuf_type* _M_sbuf;
91*404b540aSrobert mutable int_type _M_c;
92*404b540aSrobert
93*404b540aSrobert public:
94*404b540aSrobert /// Construct end of input stream iterator.
95*404b540aSrobert istreambuf_iterator() throw()
96*404b540aSrobert : _M_sbuf(0), _M_c(traits_type::eof()) { }
97*404b540aSrobert
98*404b540aSrobert /// Construct start of input stream iterator.
99*404b540aSrobert istreambuf_iterator(istream_type& __s) throw()
100*404b540aSrobert : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
101*404b540aSrobert
102*404b540aSrobert /// Construct start of streambuf iterator.
103*404b540aSrobert istreambuf_iterator(streambuf_type* __s) throw()
104*404b540aSrobert : _M_sbuf(__s), _M_c(traits_type::eof()) { }
105*404b540aSrobert
106*404b540aSrobert /// Return the current character pointed to by iterator. This returns
107*404b540aSrobert /// streambuf.sgetc(). It cannot be assigned. NB: The result of
108*404b540aSrobert /// operator*() on an end of stream is undefined.
109*404b540aSrobert char_type
110*404b540aSrobert operator*() const
111*404b540aSrobert {
112*404b540aSrobert #ifdef _GLIBCXX_DEBUG_PEDANTIC
113*404b540aSrobert // Dereferencing a past-the-end istreambuf_iterator is a
114*404b540aSrobert // libstdc++ extension
115*404b540aSrobert __glibcxx_requires_cond(!_M_at_eof(),
116*404b540aSrobert _M_message(__gnu_debug::__msg_deref_istreambuf)
117*404b540aSrobert ._M_iterator(*this));
118*404b540aSrobert #endif
119*404b540aSrobert return traits_type::to_char_type(_M_get());
120*404b540aSrobert }
121*404b540aSrobert
122*404b540aSrobert /// Advance the iterator. Calls streambuf.sbumpc().
123*404b540aSrobert istreambuf_iterator&
124*404b540aSrobert operator++()
125*404b540aSrobert {
126*404b540aSrobert __glibcxx_requires_cond(!_M_at_eof(),
127*404b540aSrobert _M_message(__gnu_debug::__msg_inc_istreambuf)
128*404b540aSrobert ._M_iterator(*this));
129*404b540aSrobert if (_M_sbuf)
130*404b540aSrobert {
131*404b540aSrobert _M_sbuf->sbumpc();
132*404b540aSrobert _M_c = traits_type::eof();
133*404b540aSrobert }
134*404b540aSrobert return *this;
135*404b540aSrobert }
136*404b540aSrobert
137*404b540aSrobert /// Advance the iterator. Calls streambuf.sbumpc().
138*404b540aSrobert istreambuf_iterator
139*404b540aSrobert operator++(int)
140*404b540aSrobert {
141*404b540aSrobert __glibcxx_requires_cond(!_M_at_eof(),
142*404b540aSrobert _M_message(__gnu_debug::__msg_inc_istreambuf)
143*404b540aSrobert ._M_iterator(*this));
144*404b540aSrobert
145*404b540aSrobert istreambuf_iterator __old = *this;
146*404b540aSrobert if (_M_sbuf)
147*404b540aSrobert {
148*404b540aSrobert __old._M_c = _M_sbuf->sbumpc();
149*404b540aSrobert _M_c = traits_type::eof();
150*404b540aSrobert }
151*404b540aSrobert return __old;
152*404b540aSrobert }
153*404b540aSrobert
154*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
155*404b540aSrobert // 110 istreambuf_iterator::equal not const
156*404b540aSrobert // NB: there is also number 111 (NAD, Future) pending on this function.
157*404b540aSrobert /// Return true both iterators are end or both are not end.
158*404b540aSrobert bool
159*404b540aSrobert equal(const istreambuf_iterator& __b) const
160*404b540aSrobert {
161*404b540aSrobert const bool __thiseof = _M_at_eof();
162*404b540aSrobert const bool __beof = __b._M_at_eof();
163*404b540aSrobert return (__thiseof && __beof || (!__thiseof && !__beof));
164*404b540aSrobert }
165*404b540aSrobert
166*404b540aSrobert private:
167*404b540aSrobert int_type
168*404b540aSrobert _M_get() const
169*404b540aSrobert {
170*404b540aSrobert const int_type __eof = traits_type::eof();
171*404b540aSrobert int_type __ret = __eof;
172*404b540aSrobert if (_M_sbuf)
173*404b540aSrobert {
174*404b540aSrobert if (!traits_type::eq_int_type(_M_c, __eof))
175*404b540aSrobert __ret = _M_c;
176*404b540aSrobert else if (!traits_type::eq_int_type((__ret = _M_sbuf->sgetc()),
177*404b540aSrobert __eof))
178*404b540aSrobert _M_c = __ret;
179*404b540aSrobert else
180*404b540aSrobert _M_sbuf = 0;
181*404b540aSrobert }
182*404b540aSrobert return __ret;
183*404b540aSrobert }
184*404b540aSrobert
185*404b540aSrobert bool
186*404b540aSrobert _M_at_eof() const
187*404b540aSrobert {
188*404b540aSrobert const int_type __eof = traits_type::eof();
189*404b540aSrobert return traits_type::eq_int_type(_M_get(), __eof);
190*404b540aSrobert }
191*404b540aSrobert };
192*404b540aSrobert
193*404b540aSrobert template<typename _CharT, typename _Traits>
194*404b540aSrobert inline bool
195*404b540aSrobert operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
196*404b540aSrobert const istreambuf_iterator<_CharT, _Traits>& __b)
197*404b540aSrobert { return __a.equal(__b); }
198*404b540aSrobert
199*404b540aSrobert template<typename _CharT, typename _Traits>
200*404b540aSrobert inline bool
201*404b540aSrobert operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
202*404b540aSrobert const istreambuf_iterator<_CharT, _Traits>& __b)
203*404b540aSrobert { return !__a.equal(__b); }
204*404b540aSrobert
205*404b540aSrobert /// Provides output iterator semantics for streambufs.
206*404b540aSrobert template<typename _CharT, typename _Traits>
207*404b540aSrobert class ostreambuf_iterator
208*404b540aSrobert : public iterator<output_iterator_tag, void, void, void, void>
209*404b540aSrobert {
210*404b540aSrobert public:
211*404b540aSrobert // Types:
212*404b540aSrobert //@{
213*404b540aSrobert /// Public typedefs
214*404b540aSrobert typedef _CharT char_type;
215*404b540aSrobert typedef _Traits traits_type;
216*404b540aSrobert typedef basic_streambuf<_CharT, _Traits> streambuf_type;
217*404b540aSrobert typedef basic_ostream<_CharT, _Traits> ostream_type;
218*404b540aSrobert //@}
219*404b540aSrobert
220*404b540aSrobert template<typename _CharT2>
221*404b540aSrobert friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
222*404b540aSrobert ostreambuf_iterator<_CharT2> >::__type
223*404b540aSrobert copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
224*404b540aSrobert ostreambuf_iterator<_CharT2>);
225*404b540aSrobert
226*404b540aSrobert private:
227*404b540aSrobert streambuf_type* _M_sbuf;
228*404b540aSrobert bool _M_failed;
229*404b540aSrobert
230*404b540aSrobert public:
231*404b540aSrobert /// Construct output iterator from ostream.
ostreambuf_iterator(ostream_type & __s)232*404b540aSrobert ostreambuf_iterator(ostream_type& __s) throw ()
233*404b540aSrobert : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
234*404b540aSrobert
235*404b540aSrobert /// Construct output iterator from streambuf.
throw()236*404b540aSrobert ostreambuf_iterator(streambuf_type* __s) throw ()
237*404b540aSrobert : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
238*404b540aSrobert
239*404b540aSrobert /// Write character to streambuf. Calls streambuf.sputc().
240*404b540aSrobert ostreambuf_iterator&
241*404b540aSrobert operator=(_CharT __c)
242*404b540aSrobert {
243*404b540aSrobert if (!_M_failed &&
244*404b540aSrobert _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
245*404b540aSrobert _M_failed = true;
246*404b540aSrobert return *this;
247*404b540aSrobert }
248*404b540aSrobert
249*404b540aSrobert /// Return *this.
250*404b540aSrobert ostreambuf_iterator&
251*404b540aSrobert operator*()
252*404b540aSrobert { return *this; }
253*404b540aSrobert
254*404b540aSrobert /// Return *this.
255*404b540aSrobert ostreambuf_iterator&
256*404b540aSrobert operator++(int)
257*404b540aSrobert { return *this; }
258*404b540aSrobert
259*404b540aSrobert /// Return *this.
260*404b540aSrobert ostreambuf_iterator&
261*404b540aSrobert operator++()
262*404b540aSrobert { return *this; }
263*404b540aSrobert
264*404b540aSrobert /// Return true if previous operator=() failed.
265*404b540aSrobert bool
failed()266*404b540aSrobert failed() const throw()
267*404b540aSrobert { return _M_failed; }
268*404b540aSrobert
269*404b540aSrobert ostreambuf_iterator&
_M_put(const _CharT * __ws,streamsize __len)270*404b540aSrobert _M_put(const _CharT* __ws, streamsize __len)
271*404b540aSrobert {
272*404b540aSrobert if (__builtin_expect(!_M_failed, true)
273*404b540aSrobert && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
274*404b540aSrobert false))
275*404b540aSrobert _M_failed = true;
276*404b540aSrobert return *this;
277*404b540aSrobert }
278*404b540aSrobert };
279*404b540aSrobert
280*404b540aSrobert // Overloads for streambuf iterators.
281*404b540aSrobert template<typename _CharT>
282*404b540aSrobert typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
283*404b540aSrobert ostreambuf_iterator<_CharT> >::__type
copy(istreambuf_iterator<_CharT> __first,istreambuf_iterator<_CharT> __last,ostreambuf_iterator<_CharT> __result)284*404b540aSrobert copy(istreambuf_iterator<_CharT> __first,
285*404b540aSrobert istreambuf_iterator<_CharT> __last,
286*404b540aSrobert ostreambuf_iterator<_CharT> __result)
287*404b540aSrobert {
288*404b540aSrobert if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
289*404b540aSrobert {
290*404b540aSrobert bool __ineof;
291*404b540aSrobert __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
292*404b540aSrobert if (!__ineof)
293*404b540aSrobert __result._M_failed = true;
294*404b540aSrobert }
295*404b540aSrobert return __result;
296*404b540aSrobert }
297*404b540aSrobert
298*404b540aSrobert template<typename _CharT>
299*404b540aSrobert typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
300*404b540aSrobert ostreambuf_iterator<_CharT> >::__type
__copy_aux(_CharT * __first,_CharT * __last,ostreambuf_iterator<_CharT> __result)301*404b540aSrobert __copy_aux(_CharT* __first, _CharT* __last,
302*404b540aSrobert ostreambuf_iterator<_CharT> __result)
303*404b540aSrobert {
304*404b540aSrobert const streamsize __num = __last - __first;
305*404b540aSrobert if (__num > 0)
306*404b540aSrobert __result._M_put(__first, __num);
307*404b540aSrobert return __result;
308*404b540aSrobert }
309*404b540aSrobert
310*404b540aSrobert template<typename _CharT>
311*404b540aSrobert typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
312*404b540aSrobert ostreambuf_iterator<_CharT> >::__type
__copy_aux(const _CharT * __first,const _CharT * __last,ostreambuf_iterator<_CharT> __result)313*404b540aSrobert __copy_aux(const _CharT* __first, const _CharT* __last,
314*404b540aSrobert ostreambuf_iterator<_CharT> __result)
315*404b540aSrobert {
316*404b540aSrobert const streamsize __num = __last - __first;
317*404b540aSrobert if (__num > 0)
318*404b540aSrobert __result._M_put(__first, __num);
319*404b540aSrobert return __result;
320*404b540aSrobert }
321*404b540aSrobert
322*404b540aSrobert template<typename _CharT>
323*404b540aSrobert typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
324*404b540aSrobert _CharT*>::__type
__copy_aux(istreambuf_iterator<_CharT> __first,istreambuf_iterator<_CharT> __last,_CharT * __result)325*404b540aSrobert __copy_aux(istreambuf_iterator<_CharT> __first,
326*404b540aSrobert istreambuf_iterator<_CharT> __last, _CharT* __result)
327*404b540aSrobert {
328*404b540aSrobert typedef istreambuf_iterator<_CharT> __is_iterator_type;
329*404b540aSrobert typedef typename __is_iterator_type::traits_type traits_type;
330*404b540aSrobert typedef typename __is_iterator_type::streambuf_type streambuf_type;
331*404b540aSrobert typedef typename traits_type::int_type int_type;
332*404b540aSrobert
333*404b540aSrobert if (__first._M_sbuf && !__last._M_sbuf)
334*404b540aSrobert {
335*404b540aSrobert streambuf_type* __sb = __first._M_sbuf;
336*404b540aSrobert int_type __c = __sb->sgetc();
337*404b540aSrobert while (!traits_type::eq_int_type(__c, traits_type::eof()))
338*404b540aSrobert {
339*404b540aSrobert const streamsize __n = __sb->egptr() - __sb->gptr();
340*404b540aSrobert if (__n > 1)
341*404b540aSrobert {
342*404b540aSrobert traits_type::copy(__result, __sb->gptr(), __n);
343*404b540aSrobert __sb->gbump(__n);
344*404b540aSrobert __result += __n;
345*404b540aSrobert __c = __sb->underflow();
346*404b540aSrobert }
347*404b540aSrobert else
348*404b540aSrobert {
349*404b540aSrobert *__result++ = traits_type::to_char_type(__c);
350*404b540aSrobert __c = __sb->snextc();
351*404b540aSrobert }
352*404b540aSrobert }
353*404b540aSrobert }
354*404b540aSrobert return __result;
355*404b540aSrobert }
356*404b540aSrobert
357*404b540aSrobert template<typename _CharT>
358*404b540aSrobert typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
359*404b540aSrobert istreambuf_iterator<_CharT> >::__type
find(istreambuf_iterator<_CharT> __first,istreambuf_iterator<_CharT> __last,const _CharT & __val)360*404b540aSrobert find(istreambuf_iterator<_CharT> __first,
361*404b540aSrobert istreambuf_iterator<_CharT> __last, const _CharT& __val)
362*404b540aSrobert {
363*404b540aSrobert typedef istreambuf_iterator<_CharT> __is_iterator_type;
364*404b540aSrobert typedef typename __is_iterator_type::traits_type traits_type;
365*404b540aSrobert typedef typename __is_iterator_type::streambuf_type streambuf_type;
366*404b540aSrobert typedef typename traits_type::int_type int_type;
367*404b540aSrobert
368*404b540aSrobert if (__first._M_sbuf && !__last._M_sbuf)
369*404b540aSrobert {
370*404b540aSrobert const int_type __ival = traits_type::to_int_type(__val);
371*404b540aSrobert streambuf_type* __sb = __first._M_sbuf;
372*404b540aSrobert int_type __c = __sb->sgetc();
373*404b540aSrobert while (!traits_type::eq_int_type(__c, traits_type::eof())
374*404b540aSrobert && !traits_type::eq_int_type(__c, __ival))
375*404b540aSrobert {
376*404b540aSrobert streamsize __n = __sb->egptr() - __sb->gptr();
377*404b540aSrobert if (__n > 1)
378*404b540aSrobert {
379*404b540aSrobert const _CharT* __p = traits_type::find(__sb->gptr(),
380*404b540aSrobert __n, __val);
381*404b540aSrobert if (__p)
382*404b540aSrobert __n = __p - __sb->gptr();
383*404b540aSrobert __sb->gbump(__n);
384*404b540aSrobert __c = __sb->sgetc();
385*404b540aSrobert }
386*404b540aSrobert else
387*404b540aSrobert __c = __sb->snextc();
388*404b540aSrobert }
389*404b540aSrobert
390*404b540aSrobert if (!traits_type::eq_int_type(__c, traits_type::eof()))
391*404b540aSrobert __first._M_c = __c;
392*404b540aSrobert else
393*404b540aSrobert __first._M_sbuf = 0;
394*404b540aSrobert }
395*404b540aSrobert return __first;
396*404b540aSrobert }
397*404b540aSrobert
398*404b540aSrobert _GLIBCXX_END_NAMESPACE
399*404b540aSrobert
400*404b540aSrobert #endif
401