]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/streambuf_iterator.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / streambuf_iterator.h
CommitLineData
725dc051
BK
1// Streambuf iterators
2
99dee823 3// Copyright (C) 1997-2021 Free Software Foundation, Inc.
725dc051
BK
4//
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
748086b7 8// Free Software Foundation; either version 3, or (at your option)
725dc051
BK
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
748086b7
JJ
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
725dc051 24
f910786b 25/** @file bits/streambuf_iterator.h
729e3d3f 26 * This is an internal header file, included by other library headers.
f910786b 27 * Do not attempt to use it directly. @headername{iterator}
729e3d3f
PE
28 */
29
3d7c150e
BK
30#ifndef _STREAMBUF_ITERATOR_H
31#define _STREAMBUF_ITERATOR_H 1
725dc051 32
b0a85b86
GDR
33#pragma GCC system_header
34
2e2a38cd 35#include <streambuf>
285b36d6 36#include <debug/debug.h>
2e2a38cd 37
12ffa228
BK
38namespace std _GLIBCXX_VISIBILITY(default)
39{
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
e324f9cb 41
8e32aa11
BK
42 /**
43 * @addtogroup iterators
44 * @{
45 */
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,
7918cb93 52 _CharT*, _CharT>
725dc051
BK
53 {
54 public:
725dc051 55 // Types:
ffcec5c8
JQ
56 //@{
57 /// Public typedefs
7918cb93
JW
58#if __cplusplus < 201103L
59 typedef _CharT& reference; // Changed to _CharT by LWG 445
60#elif __cplusplus > 201703L
9aeb3bef
JW
61 // _GLIBCXX_RESOLVE_LIB_DEFECTS
62 // 3188. istreambuf_iterator::pointer should not be unspecified
63 using pointer = void;
64#endif
7918cb93 65
ed6814f7
BI
66 typedef _CharT char_type;
67 typedef _Traits traits_type;
68 typedef typename _Traits::int_type int_type;
69 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
70 typedef basic_istream<_CharT, _Traits> istream_type;
ffcec5c8 71 //@}
725dc051 72
0002d5d2 73 template<typename _CharT2>
105c6331 74 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
e324f9cb 75 ostreambuf_iterator<_CharT2> >::__type
0002d5d2
PC
76 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
77 ostreambuf_iterator<_CharT2>);
78
f0112db9 79 template<bool _IsMove, typename _CharT2>
e324f9cb 80 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
105c6331 81 _CharT2*>::__type
f0112db9
PC
82 __copy_move_a2(istreambuf_iterator<_CharT2>,
83 istreambuf_iterator<_CharT2>, _CharT2*);
0002d5d2 84
8ab38f6c 85 template<typename _CharT2, typename _Size>
4e05c918
FD
86 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
87 _CharT2*>::__type
88 __copy_n_a(istreambuf_iterator<_CharT2>, _Size, _CharT2*, bool);
8ab38f6c 89
0002d5d2 90 template<typename _CharT2>
105c6331 91 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
e324f9cb 92 istreambuf_iterator<_CharT2> >::__type
0002d5d2 93 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
976e25f4 94 const _CharT2&);
0002d5d2 95
e324f9cb
FD
96 template<typename _CharT2, typename _Distance>
97 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
98 void>::__type
99 advance(istreambuf_iterator<_CharT2>&, _Distance);
100
39003c99 101 private:
ed6814f7
BI
102 // 24.5.3 istreambuf_iterator
103 // p 1
39003c99
BK
104 // If the end of stream is reached (streambuf_type::sgetc()
105 // returns traits_type::eof()), the iterator becomes equal to
106 // the "end of stream" iterator value.
107 // NB: This implementation assumes the "end of stream" value
108 // is EOF, or -1.
ed6814f7 109 mutable streambuf_type* _M_sbuf;
4e914524 110 int_type _M_c;
39003c99
BK
111
112 public:
ffcec5c8 113 /// Construct end of input stream iterator.
23e4c4ee 114 _GLIBCXX_CONSTEXPR istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
f13a69ec 115 : _M_sbuf(0), _M_c(traits_type::eof()) { }
ed6814f7 116
07522ae9 117#if __cplusplus > 201703L && __cpp_lib_concepts
120e8734
JW
118 constexpr istreambuf_iterator(default_sentinel_t) noexcept
119 : istreambuf_iterator() { }
120#endif
121
734f5023 122#if __cplusplus >= 201103L
23e4c4ee
PC
123 istreambuf_iterator(const istreambuf_iterator&) noexcept = default;
124
125 ~istreambuf_iterator() = default;
126#endif
127
ffcec5c8 128 /// Construct start of input stream iterator.
23e4c4ee 129 istreambuf_iterator(istream_type& __s) _GLIBCXX_USE_NOEXCEPT
f13a69ec 130 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
725dc051 131
ffcec5c8 132 /// Construct start of streambuf iterator.
23e4c4ee 133 istreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
f13a69ec 134 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
ed6814f7 135
a1417556
JW
136#if __cplusplus >= 201103L
137 istreambuf_iterator&
138 operator=(const istreambuf_iterator&) noexcept = default;
139#endif
140
ffcec5c8
JQ
141 /// Return the current character pointed to by iterator. This returns
142 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
143 /// operator*() on an end of stream is undefined.
ed6814f7 144 char_type
725dc051 145 operator*() const
ed6814f7 146 {
4e914524
PO
147 int_type __c = _M_get();
148
285b36d6
BK
149#ifdef _GLIBCXX_DEBUG_PEDANTIC
150 // Dereferencing a past-the-end istreambuf_iterator is a
151 // libstdc++ extension
4e914524 152 __glibcxx_requires_cond(!_S_is_eof(__c),
285b36d6 153 _M_message(__gnu_debug::__msg_deref_istreambuf)
ed6814f7 154 ._M_iterator(*this));
285b36d6 155#endif
4e914524 156 return traits_type::to_char_type(__c);
285b36d6 157 }
ffcec5c8
JQ
158
159 /// Advance the iterator. Calls streambuf.sbumpc().
ed6814f7 160 istreambuf_iterator&
725dc051 161 operator++()
ed6814f7 162 {
4e914524
PO
163 __glibcxx_requires_cond(_M_sbuf &&
164 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
285b36d6 165 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 166 ._M_iterator(*this));
4e914524
PO
167
168 _M_sbuf->sbumpc();
169 _M_c = traits_type::eof();
ed6814f7 170 return *this;
725dc051
BK
171 }
172
ffcec5c8 173 /// Advance the iterator. Calls streambuf.sbumpc().
b581eaf7 174 istreambuf_iterator
725dc051
BK
175 operator++(int)
176 {
4e914524
PO
177 __glibcxx_requires_cond(_M_sbuf &&
178 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
285b36d6 179 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 180 ._M_iterator(*this));
285b36d6 181
b581eaf7 182 istreambuf_iterator __old = *this;
4e914524
PO
183 __old._M_c = _M_sbuf->sbumpc();
184 _M_c = traits_type::eof();
ed6814f7 185 return __old;
725dc051 186 }
725dc051 187
f5677b15 188 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725dc051 189 // 110 istreambuf_iterator::equal not const
0e7300bb 190 // NB: there is also number 111 (NAD) relevant to this function.
ffcec5c8 191 /// Return true both iterators are end or both are not end.
ed6814f7 192 bool
b581eaf7 193 equal(const istreambuf_iterator& __b) const
6bfcbf0d 194 { return _M_at_eof() == __b._M_at_eof(); }
9875ea05
BK
195
196 private:
ed6814f7 197 int_type
9875ea05 198 _M_get() const
ed6814f7 199 {
4e914524
PO
200 int_type __ret = _M_c;
201 if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->sgetc()))
202 _M_sbuf = 0;
9875ea05
BK
203 return __ret;
204 }
285b36d6 205
ed6814f7 206 bool
285b36d6 207 _M_at_eof() const
4e914524
PO
208 { return _S_is_eof(_M_get()); }
209
210 static bool
211 _S_is_eof(int_type __c)
285b36d6
BK
212 {
213 const int_type __eof = traits_type::eof();
4e914524 214 return traits_type::eq_int_type(__c, __eof);
285b36d6 215 }
120e8734 216
07522ae9 217#if __cplusplus > 201703L && __cpp_lib_concepts
120e8734
JW
218 friend bool
219 operator==(const istreambuf_iterator& __i, default_sentinel_t __s)
220 { return __i._M_at_eof(); }
221#endif
725dc051
BK
222 };
223
224 template<typename _CharT, typename _Traits>
ed6814f7 225 inline bool
725dc051
BK
226 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
227 const istreambuf_iterator<_CharT, _Traits>& __b)
228 { return __a.equal(__b); }
229
230 template<typename _CharT, typename _Traits>
ed6814f7 231 inline bool
725dc051
BK
232 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
233 const istreambuf_iterator<_CharT, _Traits>& __b)
234 { return !__a.equal(__b); }
5c352a47 235
ffcec5c8 236 /// Provides output iterator semantics for streambufs.
5c352a47
BK
237 template<typename _CharT, typename _Traits>
238 class ostreambuf_iterator
239 : public iterator<output_iterator_tag, void, void, void, void>
240 {
241 public:
242 // Types:
ffcec5c8
JQ
243 //@{
244 /// Public typedefs
9aeb3bef
JW
245#if __cplusplus > 201703L
246 using difference_type = ptrdiff_t;
247#endif
e324f9cb
FD
248 typedef _CharT char_type;
249 typedef _Traits traits_type;
5c352a47
BK
250 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
251 typedef basic_ostream<_CharT, _Traits> ostream_type;
ffcec5c8 252 //@}
5c352a47 253
0002d5d2 254 template<typename _CharT2>
105c6331 255 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
e324f9cb 256 ostreambuf_iterator<_CharT2> >::__type
0002d5d2
PC
257 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
258 ostreambuf_iterator<_CharT2>);
259
5c352a47 260 private:
ed6814f7
BI
261 streambuf_type* _M_sbuf;
262 bool _M_failed;
5c352a47
BK
263
264 public:
9aeb3bef
JW
265
266#if __cplusplus > 201703L
267 constexpr
268 ostreambuf_iterator() noexcept
269 : _M_sbuf(nullptr), _M_failed(true) { }
270#endif
271
ffcec5c8 272 /// Construct output iterator from ostream.
23e4c4ee 273 ostreambuf_iterator(ostream_type& __s) _GLIBCXX_USE_NOEXCEPT
5c352a47 274 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
ed6814f7 275
ffcec5c8 276 /// Construct output iterator from streambuf.
23e4c4ee 277 ostreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
5c352a47
BK
278 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
279
ffcec5c8 280 /// Write character to streambuf. Calls streambuf.sputc().
ed6814f7 281 ostreambuf_iterator&
2e2a38cd
BK
282 operator=(_CharT __c)
283 {
ed6814f7 284 if (!_M_failed &&
2e2a38cd
BK
285 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
286 _M_failed = true;
287 return *this;
288 }
5c352a47 289
ffcec5c8 290 /// Return *this.
ed6814f7 291 ostreambuf_iterator&
e0ec69c9 292 operator*()
5c352a47
BK
293 { return *this; }
294
ffcec5c8 295 /// Return *this.
ed6814f7 296 ostreambuf_iterator&
e0ec69c9 297 operator++(int)
5c352a47
BK
298 { return *this; }
299
ffcec5c8 300 /// Return *this.
ed6814f7 301 ostreambuf_iterator&
e0ec69c9 302 operator++()
5c352a47
BK
303 { return *this; }
304
ffcec5c8 305 /// Return true if previous operator=() failed.
ed6814f7 306 bool
23e4c4ee 307 failed() const _GLIBCXX_USE_NOEXCEPT
5c352a47 308 { return _M_failed; }
5c352a47 309
ed6814f7 310 ostreambuf_iterator&
2e2a38cd
BK
311 _M_put(const _CharT* __ws, streamsize __len)
312 {
53279c10 313 if (__builtin_expect(!_M_failed, true)
15d72060
PC
314 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
315 false))
53279c10 316 _M_failed = true;
2e2a38cd
BK
317 return *this;
318 }
319 };
3cbc7af0 320
0002d5d2
PC
321 // Overloads for streambuf iterators.
322 template<typename _CharT>
105c6331 323 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
e324f9cb 324 ostreambuf_iterator<_CharT> >::__type
0002d5d2
PC
325 copy(istreambuf_iterator<_CharT> __first,
326 istreambuf_iterator<_CharT> __last,
327 ostreambuf_iterator<_CharT> __result)
328 {
329 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
330 {
331 bool __ineof;
332 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
333 if (!__ineof)
334 __result._M_failed = true;
335 }
336 return __result;
337 }
338
f0112db9 339 template<bool _IsMove, typename _CharT>
e324f9cb
FD
340 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
341 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
342 __copy_move_a2(_CharT* __first, _CharT* __last,
343 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
344 {
345 const streamsize __num = __last - __first;
346 if (__num > 0)
347 __result._M_put(__first, __num);
348 return __result;
349 }
350
f0112db9 351 template<bool _IsMove, typename _CharT>
105c6331
BK
352 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
353 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
354 __copy_move_a2(const _CharT* __first, const _CharT* __last,
355 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
356 {
357 const streamsize __num = __last - __first;
358 if (__num > 0)
359 __result._M_put(__first, __num);
360 return __result;
361 }
362
f0112db9 363 template<bool _IsMove, typename _CharT>
e324f9cb
FD
364 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
365 _CharT*>::__type
f0112db9
PC
366 __copy_move_a2(istreambuf_iterator<_CharT> __first,
367 istreambuf_iterator<_CharT> __last, _CharT* __result)
0002d5d2 368 {
e324f9cb
FD
369 typedef istreambuf_iterator<_CharT> __is_iterator_type;
370 typedef typename __is_iterator_type::traits_type traits_type;
0002d5d2 371 typedef typename __is_iterator_type::streambuf_type streambuf_type;
e324f9cb 372 typedef typename traits_type::int_type int_type;
0002d5d2
PC
373
374 if (__first._M_sbuf && !__last._M_sbuf)
375 {
376 streambuf_type* __sb = __first._M_sbuf;
377 int_type __c = __sb->sgetc();
378 while (!traits_type::eq_int_type(__c, traits_type::eof()))
379 {
380 const streamsize __n = __sb->egptr() - __sb->gptr();
381 if (__n > 1)
382 {
383 traits_type::copy(__result, __sb->gptr(), __n);
1139a735 384 __sb->__safe_gbump(__n);
0002d5d2
PC
385 __result += __n;
386 __c = __sb->underflow();
387 }
388 else
389 {
390 *__result++ = traits_type::to_char_type(__c);
391 __c = __sb->snextc();
392 }
393 }
394 }
395 return __result;
396 }
397
8ab38f6c 398 template<typename _CharT, typename _Size>
4e05c918
FD
399 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
400 _CharT*>::__type
401 __copy_n_a(istreambuf_iterator<_CharT> __it, _Size __n, _CharT* __result,
402 bool __strict __attribute__((__unused__)))
8ab38f6c
FD
403 {
404 if (__n == 0)
405 return __result;
406
407 __glibcxx_requires_cond(__it._M_sbuf,
408 _M_message(__gnu_debug::__msg_inc_istreambuf)
409 ._M_iterator(__it));
410 _CharT* __beg = __result;
411 __result += __it._M_sbuf->sgetn(__beg, __n);
4e05c918 412 __glibcxx_requires_cond(!__strict || __result - __beg == __n,
8ab38f6c
FD
413 _M_message(__gnu_debug::__msg_inc_istreambuf)
414 ._M_iterator(__it));
415 return __result;
416 }
8ab38f6c 417
0002d5d2 418 template<typename _CharT>
105c6331
BK
419 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
420 istreambuf_iterator<_CharT> >::__type
0002d5d2 421 find(istreambuf_iterator<_CharT> __first,
976e25f4 422 istreambuf_iterator<_CharT> __last, const _CharT& __val)
0002d5d2 423 {
e324f9cb 424 typedef istreambuf_iterator<_CharT> __is_iterator_type;
0002d5d2
PC
425 typedef typename __is_iterator_type::traits_type traits_type;
426 typedef typename __is_iterator_type::streambuf_type streambuf_type;
e324f9cb 427 typedef typename traits_type::int_type int_type;
4e914524 428 const int_type __eof = traits_type::eof();
0002d5d2
PC
429
430 if (__first._M_sbuf && !__last._M_sbuf)
431 {
432 const int_type __ival = traits_type::to_int_type(__val);
433 streambuf_type* __sb = __first._M_sbuf;
434 int_type __c = __sb->sgetc();
4e914524 435 while (!traits_type::eq_int_type(__c, __eof)
0002d5d2
PC
436 && !traits_type::eq_int_type(__c, __ival))
437 {
438 streamsize __n = __sb->egptr() - __sb->gptr();
439 if (__n > 1)
440 {
441 const _CharT* __p = traits_type::find(__sb->gptr(),
442 __n, __val);
443 if (__p)
444 __n = __p - __sb->gptr();
1139a735 445 __sb->__safe_gbump(__n);
0002d5d2
PC
446 __c = __sb->sgetc();
447 }
448 else
449 __c = __sb->snextc();
450 }
451
4e914524 452 __first._M_c = __eof;
0002d5d2 453 }
4e914524 454
0002d5d2
PC
455 return __first;
456 }
457
e324f9cb
FD
458 template<typename _CharT, typename _Distance>
459 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
460 void>::__type
461 advance(istreambuf_iterator<_CharT>& __i, _Distance __n)
462 {
463 if (__n == 0)
464 return;
465
466 __glibcxx_assert(__n > 0);
467 __glibcxx_requires_cond(!__i._M_at_eof(),
468 _M_message(__gnu_debug::__msg_inc_istreambuf)
469 ._M_iterator(__i));
470
471 typedef istreambuf_iterator<_CharT> __is_iterator_type;
472 typedef typename __is_iterator_type::traits_type traits_type;
473 typedef typename __is_iterator_type::streambuf_type streambuf_type;
474 typedef typename traits_type::int_type int_type;
475 const int_type __eof = traits_type::eof();
476
477 streambuf_type* __sb = __i._M_sbuf;
478 while (__n > 0)
479 {
480 streamsize __size = __sb->egptr() - __sb->gptr();
481 if (__size > __n)
482 {
483 __sb->__safe_gbump(__n);
484 break;
485 }
486
487 __sb->__safe_gbump(__size);
488 __n -= __size;
489 if (traits_type::eq_int_type(__sb->underflow(), __eof))
490 {
491 __glibcxx_requires_cond(__n == 0,
492 _M_message(__gnu_debug::__msg_inc_istreambuf)
493 ._M_iterator(__i));
494 break;
495 }
496 }
497
498 __i._M_c = __eof;
499 }
500
8e32aa11
BK
501// @} group iterators
502
12ffa228
BK
503_GLIBCXX_END_NAMESPACE_VERSION
504} // namespace
3cbc7af0 505
b85381b9 506#endif