]> 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
7adcbafe 3// Copyright (C) 1997-2022 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:
f0b88346 56 ///@{
ffcec5c8 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;
f0b88346 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.
240b01b0 144 _GLIBCXX_NODISCARD
ed6814f7 145 char_type
725dc051 146 operator*() const
ed6814f7 147 {
4e914524
PO
148 int_type __c = _M_get();
149
285b36d6
BK
150#ifdef _GLIBCXX_DEBUG_PEDANTIC
151 // Dereferencing a past-the-end istreambuf_iterator is a
152 // libstdc++ extension
4e914524 153 __glibcxx_requires_cond(!_S_is_eof(__c),
285b36d6 154 _M_message(__gnu_debug::__msg_deref_istreambuf)
ed6814f7 155 ._M_iterator(*this));
285b36d6 156#endif
4e914524 157 return traits_type::to_char_type(__c);
285b36d6 158 }
ffcec5c8
JQ
159
160 /// Advance the iterator. Calls streambuf.sbumpc().
ed6814f7 161 istreambuf_iterator&
725dc051 162 operator++()
ed6814f7 163 {
4e914524
PO
164 __glibcxx_requires_cond(_M_sbuf &&
165 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
285b36d6 166 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 167 ._M_iterator(*this));
4e914524
PO
168
169 _M_sbuf->sbumpc();
170 _M_c = traits_type::eof();
ed6814f7 171 return *this;
725dc051
BK
172 }
173
ffcec5c8 174 /// Advance the iterator. Calls streambuf.sbumpc().
b581eaf7 175 istreambuf_iterator
725dc051
BK
176 operator++(int)
177 {
4e914524
PO
178 __glibcxx_requires_cond(_M_sbuf &&
179 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
285b36d6 180 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 181 ._M_iterator(*this));
285b36d6 182
b581eaf7 183 istreambuf_iterator __old = *this;
4e914524
PO
184 __old._M_c = _M_sbuf->sbumpc();
185 _M_c = traits_type::eof();
ed6814f7 186 return __old;
725dc051 187 }
725dc051 188
f5677b15 189 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725dc051 190 // 110 istreambuf_iterator::equal not const
0e7300bb 191 // NB: there is also number 111 (NAD) relevant to this function.
ffcec5c8 192 /// Return true both iterators are end or both are not end.
240b01b0 193 _GLIBCXX_NODISCARD
ed6814f7 194 bool
b581eaf7 195 equal(const istreambuf_iterator& __b) const
6bfcbf0d 196 { return _M_at_eof() == __b._M_at_eof(); }
9875ea05
BK
197
198 private:
ed6814f7 199 int_type
9875ea05 200 _M_get() const
ed6814f7 201 {
4e914524
PO
202 int_type __ret = _M_c;
203 if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->sgetc()))
204 _M_sbuf = 0;
9875ea05
BK
205 return __ret;
206 }
285b36d6 207
ed6814f7 208 bool
285b36d6 209 _M_at_eof() const
4e914524
PO
210 { return _S_is_eof(_M_get()); }
211
212 static bool
213 _S_is_eof(int_type __c)
285b36d6
BK
214 {
215 const int_type __eof = traits_type::eof();
4e914524 216 return traits_type::eq_int_type(__c, __eof);
285b36d6 217 }
120e8734 218
07522ae9 219#if __cplusplus > 201703L && __cpp_lib_concepts
240b01b0 220 [[nodiscard]]
120e8734
JW
221 friend bool
222 operator==(const istreambuf_iterator& __i, default_sentinel_t __s)
223 { return __i._M_at_eof(); }
224#endif
725dc051
BK
225 };
226
227 template<typename _CharT, typename _Traits>
240b01b0 228 _GLIBCXX_NODISCARD
ed6814f7 229 inline bool
725dc051
BK
230 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
231 const istreambuf_iterator<_CharT, _Traits>& __b)
232 { return __a.equal(__b); }
233
240b01b0 234#if __cpp_impl_three_way_comparison < 201907L
725dc051 235 template<typename _CharT, typename _Traits>
240b01b0 236 _GLIBCXX_NODISCARD
ed6814f7 237 inline bool
725dc051
BK
238 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
239 const istreambuf_iterator<_CharT, _Traits>& __b)
240 { return !__a.equal(__b); }
240b01b0 241#endif
5c352a47 242
ffcec5c8 243 /// Provides output iterator semantics for streambufs.
5c352a47
BK
244 template<typename _CharT, typename _Traits>
245 class ostreambuf_iterator
246 : public iterator<output_iterator_tag, void, void, void, void>
247 {
248 public:
249 // Types:
f0b88346 250 ///@{
ffcec5c8 251 /// Public typedefs
9aeb3bef
JW
252#if __cplusplus > 201703L
253 using difference_type = ptrdiff_t;
254#endif
e324f9cb
FD
255 typedef _CharT char_type;
256 typedef _Traits traits_type;
5c352a47
BK
257 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
258 typedef basic_ostream<_CharT, _Traits> ostream_type;
f0b88346 259 ///@}
5c352a47 260
0002d5d2 261 template<typename _CharT2>
105c6331 262 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
e324f9cb 263 ostreambuf_iterator<_CharT2> >::__type
0002d5d2
PC
264 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
265 ostreambuf_iterator<_CharT2>);
266
5c352a47 267 private:
ed6814f7
BI
268 streambuf_type* _M_sbuf;
269 bool _M_failed;
5c352a47
BK
270
271 public:
9aeb3bef
JW
272
273#if __cplusplus > 201703L
274 constexpr
275 ostreambuf_iterator() noexcept
276 : _M_sbuf(nullptr), _M_failed(true) { }
277#endif
278
ffcec5c8 279 /// Construct output iterator from ostream.
23e4c4ee 280 ostreambuf_iterator(ostream_type& __s) _GLIBCXX_USE_NOEXCEPT
5c352a47 281 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
ed6814f7 282
ffcec5c8 283 /// Construct output iterator from streambuf.
23e4c4ee 284 ostreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
5c352a47
BK
285 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
286
ffcec5c8 287 /// Write character to streambuf. Calls streambuf.sputc().
ed6814f7 288 ostreambuf_iterator&
2e2a38cd
BK
289 operator=(_CharT __c)
290 {
ed6814f7 291 if (!_M_failed &&
2e2a38cd
BK
292 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
293 _M_failed = true;
294 return *this;
295 }
5c352a47 296
ffcec5c8 297 /// Return *this.
240b01b0 298 _GLIBCXX_NODISCARD
ed6814f7 299 ostreambuf_iterator&
e0ec69c9 300 operator*()
5c352a47
BK
301 { return *this; }
302
ffcec5c8 303 /// Return *this.
ed6814f7 304 ostreambuf_iterator&
e0ec69c9 305 operator++(int)
5c352a47
BK
306 { return *this; }
307
ffcec5c8 308 /// Return *this.
ed6814f7 309 ostreambuf_iterator&
e0ec69c9 310 operator++()
5c352a47
BK
311 { return *this; }
312
ffcec5c8 313 /// Return true if previous operator=() failed.
240b01b0 314 _GLIBCXX_NODISCARD
ed6814f7 315 bool
23e4c4ee 316 failed() const _GLIBCXX_USE_NOEXCEPT
5c352a47 317 { return _M_failed; }
5c352a47 318
ed6814f7 319 ostreambuf_iterator&
2e2a38cd
BK
320 _M_put(const _CharT* __ws, streamsize __len)
321 {
53279c10 322 if (__builtin_expect(!_M_failed, true)
15d72060
PC
323 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
324 false))
53279c10 325 _M_failed = true;
2e2a38cd
BK
326 return *this;
327 }
328 };
3cbc7af0 329
0002d5d2
PC
330 // Overloads for streambuf iterators.
331 template<typename _CharT>
105c6331 332 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
e324f9cb 333 ostreambuf_iterator<_CharT> >::__type
0002d5d2
PC
334 copy(istreambuf_iterator<_CharT> __first,
335 istreambuf_iterator<_CharT> __last,
336 ostreambuf_iterator<_CharT> __result)
337 {
338 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
339 {
340 bool __ineof;
341 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
342 if (!__ineof)
343 __result._M_failed = true;
344 }
345 return __result;
346 }
347
f0112db9 348 template<bool _IsMove, typename _CharT>
e324f9cb
FD
349 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
350 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
351 __copy_move_a2(_CharT* __first, _CharT* __last,
352 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
353 {
354 const streamsize __num = __last - __first;
355 if (__num > 0)
356 __result._M_put(__first, __num);
357 return __result;
358 }
359
f0112db9 360 template<bool _IsMove, typename _CharT>
105c6331
BK
361 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
362 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
363 __copy_move_a2(const _CharT* __first, const _CharT* __last,
364 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
365 {
366 const streamsize __num = __last - __first;
367 if (__num > 0)
368 __result._M_put(__first, __num);
369 return __result;
370 }
371
f0112db9 372 template<bool _IsMove, typename _CharT>
e324f9cb
FD
373 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
374 _CharT*>::__type
f0112db9
PC
375 __copy_move_a2(istreambuf_iterator<_CharT> __first,
376 istreambuf_iterator<_CharT> __last, _CharT* __result)
0002d5d2 377 {
e324f9cb
FD
378 typedef istreambuf_iterator<_CharT> __is_iterator_type;
379 typedef typename __is_iterator_type::traits_type traits_type;
0002d5d2 380 typedef typename __is_iterator_type::streambuf_type streambuf_type;
e324f9cb 381 typedef typename traits_type::int_type int_type;
0002d5d2
PC
382
383 if (__first._M_sbuf && !__last._M_sbuf)
384 {
385 streambuf_type* __sb = __first._M_sbuf;
386 int_type __c = __sb->sgetc();
387 while (!traits_type::eq_int_type(__c, traits_type::eof()))
388 {
389 const streamsize __n = __sb->egptr() - __sb->gptr();
390 if (__n > 1)
391 {
392 traits_type::copy(__result, __sb->gptr(), __n);
1139a735 393 __sb->__safe_gbump(__n);
0002d5d2
PC
394 __result += __n;
395 __c = __sb->underflow();
396 }
397 else
398 {
399 *__result++ = traits_type::to_char_type(__c);
400 __c = __sb->snextc();
401 }
402 }
403 }
404 return __result;
405 }
406
8ab38f6c 407 template<typename _CharT, typename _Size>
4e05c918
FD
408 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
409 _CharT*>::__type
410 __copy_n_a(istreambuf_iterator<_CharT> __it, _Size __n, _CharT* __result,
411 bool __strict __attribute__((__unused__)))
8ab38f6c
FD
412 {
413 if (__n == 0)
414 return __result;
415
416 __glibcxx_requires_cond(__it._M_sbuf,
417 _M_message(__gnu_debug::__msg_inc_istreambuf)
418 ._M_iterator(__it));
419 _CharT* __beg = __result;
420 __result += __it._M_sbuf->sgetn(__beg, __n);
4e05c918 421 __glibcxx_requires_cond(!__strict || __result - __beg == __n,
8ab38f6c
FD
422 _M_message(__gnu_debug::__msg_inc_istreambuf)
423 ._M_iterator(__it));
424 return __result;
425 }
8ab38f6c 426
0002d5d2 427 template<typename _CharT>
105c6331
BK
428 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
429 istreambuf_iterator<_CharT> >::__type
0002d5d2 430 find(istreambuf_iterator<_CharT> __first,
976e25f4 431 istreambuf_iterator<_CharT> __last, const _CharT& __val)
0002d5d2 432 {
e324f9cb 433 typedef istreambuf_iterator<_CharT> __is_iterator_type;
0002d5d2
PC
434 typedef typename __is_iterator_type::traits_type traits_type;
435 typedef typename __is_iterator_type::streambuf_type streambuf_type;
e324f9cb 436 typedef typename traits_type::int_type int_type;
4e914524 437 const int_type __eof = traits_type::eof();
0002d5d2
PC
438
439 if (__first._M_sbuf && !__last._M_sbuf)
440 {
441 const int_type __ival = traits_type::to_int_type(__val);
442 streambuf_type* __sb = __first._M_sbuf;
443 int_type __c = __sb->sgetc();
4e914524 444 while (!traits_type::eq_int_type(__c, __eof)
0002d5d2
PC
445 && !traits_type::eq_int_type(__c, __ival))
446 {
447 streamsize __n = __sb->egptr() - __sb->gptr();
448 if (__n > 1)
449 {
450 const _CharT* __p = traits_type::find(__sb->gptr(),
451 __n, __val);
452 if (__p)
453 __n = __p - __sb->gptr();
1139a735 454 __sb->__safe_gbump(__n);
0002d5d2
PC
455 __c = __sb->sgetc();
456 }
457 else
458 __c = __sb->snextc();
459 }
460
4e914524 461 __first._M_c = __eof;
0002d5d2 462 }
4e914524 463
0002d5d2
PC
464 return __first;
465 }
466
e324f9cb
FD
467 template<typename _CharT, typename _Distance>
468 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
469 void>::__type
470 advance(istreambuf_iterator<_CharT>& __i, _Distance __n)
471 {
472 if (__n == 0)
473 return;
474
475 __glibcxx_assert(__n > 0);
476 __glibcxx_requires_cond(!__i._M_at_eof(),
477 _M_message(__gnu_debug::__msg_inc_istreambuf)
478 ._M_iterator(__i));
479
480 typedef istreambuf_iterator<_CharT> __is_iterator_type;
481 typedef typename __is_iterator_type::traits_type traits_type;
482 typedef typename __is_iterator_type::streambuf_type streambuf_type;
483 typedef typename traits_type::int_type int_type;
484 const int_type __eof = traits_type::eof();
485
486 streambuf_type* __sb = __i._M_sbuf;
487 while (__n > 0)
488 {
489 streamsize __size = __sb->egptr() - __sb->gptr();
490 if (__size > __n)
491 {
492 __sb->__safe_gbump(__n);
493 break;
494 }
495
496 __sb->__safe_gbump(__size);
497 __n -= __size;
498 if (traits_type::eq_int_type(__sb->underflow(), __eof))
499 {
500 __glibcxx_requires_cond(__n == 0,
501 _M_message(__gnu_debug::__msg_inc_istreambuf)
502 ._M_iterator(__i));
503 break;
504 }
505 }
506
507 __i._M_c = __eof;
508 }
509
72ddd861 510/// @} group iterators
8e32aa11 511
12ffa228
BK
512_GLIBCXX_END_NAMESPACE_VERSION
513} // namespace
3cbc7af0 514
b85381b9 515#endif