]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/streambuf_iterator.h
re PR testsuite/39696 (gcc.dg/tree-ssa/ssa-ccp-25.c scan-tree-dump doesn't work on...
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / streambuf_iterator.h
CommitLineData
725dc051
BK
1// Streambuf iterators
2
3c167a8b
PC
3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4// 2006, 2007
5c352a47 5// Free Software Foundation, Inc.
725dc051
BK
6//
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 2, or (at your option)
11// any later version.
12
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.
17
18// You should have received a copy of the GNU General Public License along
19// with this library; see the file COPYING. If not, write to the Free
83f51799 20// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
725dc051
BK
21// USA.
22
23// As a special exception, you may use this file as part of a free software
24// library without restriction. Specifically, if other files instantiate
25// templates or use macros or inline functions from this file, or you compile
26// this file and link it with other files to produce an executable, this
27// file does not by itself cause the resulting executable to be covered by
28// the GNU General Public License. This exception does not however
29// invalidate any other reasons why the executable file might be covered by
30// the GNU General Public License.
31
729e3d3f
PE
32/** @file streambuf_iterator.h
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
35 */
36
3d7c150e
BK
37#ifndef _STREAMBUF_ITERATOR_H
38#define _STREAMBUF_ITERATOR_H 1
725dc051 39
b0a85b86
GDR
40#pragma GCC system_header
41
2e2a38cd 42#include <streambuf>
285b36d6 43#include <debug/debug.h>
2e2a38cd 44
3cbc7af0 45_GLIBCXX_BEGIN_NAMESPACE(std)
0002d5d2 46
725dc051 47 // 24.5.3 Template class istreambuf_iterator
ffcec5c8 48 /// Provides input iterator semantics for streambufs.
39003c99 49 template<typename _CharT, typename _Traits>
725dc051
BK
50 class istreambuf_iterator
51 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
ed6814f7 52 _CharT*, _CharT&>
725dc051
BK
53 {
54 public:
725dc051 55 // Types:
ffcec5c8
JQ
56 //@{
57 /// Public typedefs
ed6814f7
BI
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;
ffcec5c8 63 //@}
725dc051 64
0002d5d2 65 template<typename _CharT2>
105c6331
BK
66 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
67 ostreambuf_iterator<_CharT2> >::__type
0002d5d2
PC
68 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
69 ostreambuf_iterator<_CharT2>);
70
f0112db9 71 template<bool _IsMove, typename _CharT2>
105c6331
BK
72 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
73 _CharT2*>::__type
f0112db9
PC
74 __copy_move_a2(istreambuf_iterator<_CharT2>,
75 istreambuf_iterator<_CharT2>, _CharT2*);
0002d5d2
PC
76
77 template<typename _CharT2>
105c6331
BK
78 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
79 istreambuf_iterator<_CharT2> >::__type
0002d5d2 80 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
976e25f4 81 const _CharT2&);
0002d5d2 82
39003c99 83 private:
ed6814f7
BI
84 // 24.5.3 istreambuf_iterator
85 // p 1
39003c99
BK
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
90 // is EOF, or -1.
ed6814f7 91 mutable streambuf_type* _M_sbuf;
15fb0dbe 92 mutable int_type _M_c;
39003c99
BK
93
94 public:
ffcec5c8 95 /// Construct end of input stream iterator.
ed6814f7 96 istreambuf_iterator() throw()
f13a69ec 97 : _M_sbuf(0), _M_c(traits_type::eof()) { }
ed6814f7 98
ffcec5c8 99 /// Construct start of input stream iterator.
725dc051 100 istreambuf_iterator(istream_type& __s) throw()
f13a69ec 101 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
725dc051 102
ffcec5c8 103 /// Construct start of streambuf iterator.
725dc051 104 istreambuf_iterator(streambuf_type* __s) throw()
f13a69ec 105 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
ed6814f7 106
ffcec5c8
JQ
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.
ed6814f7 110 char_type
725dc051 111 operator*() const
ed6814f7 112 {
285b36d6
BK
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)
ed6814f7 118 ._M_iterator(*this));
285b36d6 119#endif
ed6814f7 120 return traits_type::to_char_type(_M_get());
285b36d6 121 }
ffcec5c8
JQ
122
123 /// Advance the iterator. Calls streambuf.sbumpc().
ed6814f7 124 istreambuf_iterator&
725dc051 125 operator++()
ed6814f7 126 {
285b36d6
BK
127 __glibcxx_requires_cond(!_M_at_eof(),
128 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 129 ._M_iterator(*this));
eacf72d3
NM
130 if (_M_sbuf)
131 {
132 _M_sbuf->sbumpc();
133 _M_c = traits_type::eof();
134 }
ed6814f7 135 return *this;
725dc051
BK
136 }
137
ffcec5c8 138 /// Advance the iterator. Calls streambuf.sbumpc().
b581eaf7 139 istreambuf_iterator
725dc051
BK
140 operator++(int)
141 {
285b36d6
BK
142 __glibcxx_requires_cond(!_M_at_eof(),
143 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 144 ._M_iterator(*this));
285b36d6 145
b581eaf7 146 istreambuf_iterator __old = *this;
eacf72d3
NM
147 if (_M_sbuf)
148 {
149 __old._M_c = _M_sbuf->sbumpc();
150 _M_c = traits_type::eof();
151 }
ed6814f7 152 return __old;
725dc051 153 }
725dc051 154
f5677b15 155 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725dc051 156 // 110 istreambuf_iterator::equal not const
77cd227e 157 // NB: there is also number 111 (NAD, Future) pending on this function.
ffcec5c8 158 /// Return true both iterators are end or both are not end.
ed6814f7 159 bool
b581eaf7 160 equal(const istreambuf_iterator& __b) const
6bfcbf0d 161 { return _M_at_eof() == __b._M_at_eof(); }
9875ea05
BK
162
163 private:
ed6814f7 164 int_type
9875ea05 165 _M_get() const
ed6814f7 166 {
f13a69ec
BK
167 const int_type __eof = traits_type::eof();
168 int_type __ret = __eof;
9875ea05 169 if (_M_sbuf)
ed6814f7 170 {
50922820 171 if (!traits_type::eq_int_type(_M_c, __eof))
9875ea05 172 __ret = _M_c;
50922820
PC
173 else if (!traits_type::eq_int_type((__ret = _M_sbuf->sgetc()),
174 __eof))
175 _M_c = __ret;
15fb0dbe 176 else
15d72060 177 _M_sbuf = 0;
9875ea05
BK
178 }
179 return __ret;
180 }
285b36d6 181
ed6814f7 182 bool
285b36d6
BK
183 _M_at_eof() const
184 {
185 const int_type __eof = traits_type::eof();
186 return traits_type::eq_int_type(_M_get(), __eof);
187 }
725dc051
BK
188 };
189
190 template<typename _CharT, typename _Traits>
ed6814f7 191 inline bool
725dc051
BK
192 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
193 const istreambuf_iterator<_CharT, _Traits>& __b)
194 { return __a.equal(__b); }
195
196 template<typename _CharT, typename _Traits>
ed6814f7 197 inline bool
725dc051
BK
198 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
199 const istreambuf_iterator<_CharT, _Traits>& __b)
200 { return !__a.equal(__b); }
5c352a47 201
ffcec5c8 202 /// Provides output iterator semantics for streambufs.
5c352a47
BK
203 template<typename _CharT, typename _Traits>
204 class ostreambuf_iterator
205 : public iterator<output_iterator_tag, void, void, void, void>
206 {
207 public:
208 // Types:
ffcec5c8
JQ
209 //@{
210 /// Public typedefs
5c352a47
BK
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;
ffcec5c8 215 //@}
5c352a47 216
0002d5d2 217 template<typename _CharT2>
105c6331
BK
218 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
219 ostreambuf_iterator<_CharT2> >::__type
0002d5d2
PC
220 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
221 ostreambuf_iterator<_CharT2>);
222
5c352a47 223 private:
ed6814f7
BI
224 streambuf_type* _M_sbuf;
225 bool _M_failed;
5c352a47
BK
226
227 public:
ffcec5c8 228 /// Construct output iterator from ostream.
5c352a47
BK
229 ostreambuf_iterator(ostream_type& __s) throw ()
230 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
ed6814f7 231
ffcec5c8 232 /// Construct output iterator from streambuf.
5c352a47
BK
233 ostreambuf_iterator(streambuf_type* __s) throw ()
234 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
235
ffcec5c8 236 /// Write character to streambuf. Calls streambuf.sputc().
ed6814f7 237 ostreambuf_iterator&
2e2a38cd
BK
238 operator=(_CharT __c)
239 {
ed6814f7 240 if (!_M_failed &&
2e2a38cd
BK
241 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
242 _M_failed = true;
243 return *this;
244 }
5c352a47 245
ffcec5c8 246 /// Return *this.
ed6814f7 247 ostreambuf_iterator&
e0ec69c9 248 operator*()
5c352a47
BK
249 { return *this; }
250
ffcec5c8 251 /// Return *this.
ed6814f7 252 ostreambuf_iterator&
e0ec69c9 253 operator++(int)
5c352a47
BK
254 { return *this; }
255
ffcec5c8 256 /// Return *this.
ed6814f7 257 ostreambuf_iterator&
e0ec69c9 258 operator++()
5c352a47
BK
259 { return *this; }
260
ffcec5c8 261 /// Return true if previous operator=() failed.
ed6814f7 262 bool
5c352a47
BK
263 failed() const throw()
264 { return _M_failed; }
5c352a47 265
ed6814f7 266 ostreambuf_iterator&
2e2a38cd
BK
267 _M_put(const _CharT* __ws, streamsize __len)
268 {
53279c10 269 if (__builtin_expect(!_M_failed, true)
15d72060
PC
270 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
271 false))
53279c10 272 _M_failed = true;
2e2a38cd
BK
273 return *this;
274 }
275 };
3cbc7af0 276
0002d5d2
PC
277 // Overloads for streambuf iterators.
278 template<typename _CharT>
105c6331
BK
279 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
280 ostreambuf_iterator<_CharT> >::__type
0002d5d2
PC
281 copy(istreambuf_iterator<_CharT> __first,
282 istreambuf_iterator<_CharT> __last,
283 ostreambuf_iterator<_CharT> __result)
284 {
285 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
286 {
287 bool __ineof;
288 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
289 if (!__ineof)
290 __result._M_failed = true;
291 }
292 return __result;
293 }
294
f0112db9 295 template<bool _IsMove, typename _CharT>
105c6331
BK
296 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
297 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
298 __copy_move_a2(_CharT* __first, _CharT* __last,
299 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
300 {
301 const streamsize __num = __last - __first;
302 if (__num > 0)
303 __result._M_put(__first, __num);
304 return __result;
305 }
306
f0112db9 307 template<bool _IsMove, typename _CharT>
105c6331
BK
308 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
309 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
310 __copy_move_a2(const _CharT* __first, const _CharT* __last,
311 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
312 {
313 const streamsize __num = __last - __first;
314 if (__num > 0)
315 __result._M_put(__first, __num);
316 return __result;
317 }
318
f0112db9 319 template<bool _IsMove, typename _CharT>
105c6331
BK
320 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
321 _CharT*>::__type
f0112db9
PC
322 __copy_move_a2(istreambuf_iterator<_CharT> __first,
323 istreambuf_iterator<_CharT> __last, _CharT* __result)
0002d5d2
PC
324 {
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;
329
330 if (__first._M_sbuf && !__last._M_sbuf)
331 {
332 streambuf_type* __sb = __first._M_sbuf;
333 int_type __c = __sb->sgetc();
334 while (!traits_type::eq_int_type(__c, traits_type::eof()))
335 {
336 const streamsize __n = __sb->egptr() - __sb->gptr();
337 if (__n > 1)
338 {
339 traits_type::copy(__result, __sb->gptr(), __n);
340 __sb->gbump(__n);
341 __result += __n;
342 __c = __sb->underflow();
343 }
344 else
345 {
346 *__result++ = traits_type::to_char_type(__c);
347 __c = __sb->snextc();
348 }
349 }
350 }
351 return __result;
352 }
353
354 template<typename _CharT>
105c6331
BK
355 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
356 istreambuf_iterator<_CharT> >::__type
0002d5d2 357 find(istreambuf_iterator<_CharT> __first,
976e25f4 358 istreambuf_iterator<_CharT> __last, const _CharT& __val)
0002d5d2
PC
359 {
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;
364
365 if (__first._M_sbuf && !__last._M_sbuf)
366 {
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))
372 {
373 streamsize __n = __sb->egptr() - __sb->gptr();
374 if (__n > 1)
375 {
376 const _CharT* __p = traits_type::find(__sb->gptr(),
377 __n, __val);
378 if (__p)
379 __n = __p - __sb->gptr();
380 __sb->gbump(__n);
381 __c = __sb->sgetc();
382 }
383 else
384 __c = __sb->snextc();
385 }
386
387 if (!traits_type::eq_int_type(__c, traits_type::eof()))
388 __first._M_c = __c;
389 else
390 __first._M_sbuf = 0;
391 }
392 return __first;
393 }
394
3cbc7af0
BK
395_GLIBCXX_END_NAMESPACE
396
b85381b9 397#endif