1// Streambuf iterators
2
3// Copyright (C) 1997-2018 Free Software Foundation, Inc.
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
8// Free Software Foundation; either version 3, or (at your option)
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
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/>.
24
25/** @file bits/streambuf_iterator.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{iterator}
28 */
29
30#ifndef _STREAMBUF_ITERATOR_H
31#define _STREAMBUF_ITERATOR_H 1
32
33#pragma GCC system_header
34
35#include <streambuf>
36#include <debug/debug.h>
37
38namespace std _GLIBCXX_VISIBILITY(default)
39{
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
41
42 /**
43 * @addtogroup iterators
44 * @{
45 */
46
47 // 24.5.3 Template class istreambuf_iterator
48 /// Provides input iterator semantics for streambufs.
49 template<typename _CharT, typename _Traits>
50 class istreambuf_iterator
51 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
52 _CharT*,
53#if __cplusplus >= 201103L
54 // LWG 445.
55 _CharT>
56#else
57 _CharT&>
58#endif
59 {
60 public:
61 // Types:
62 //@{
63 /// Public typedefs
64 typedef _CharT char_type;
65 typedef _Traits traits_type;
66 typedef typename _Traits::int_type int_type;
67 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
68 typedef basic_istream<_CharT, _Traits> istream_type;
69 //@}
70
71 template<typename _CharT2>
72 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
73 ostreambuf_iterator<_CharT2> >::__type
74 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
75 ostreambuf_iterator<_CharT2>);
76
77 template<bool _IsMove, typename _CharT2>
78 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
79 _CharT2*>::__type
80 __copy_move_a2(istreambuf_iterator<_CharT2>,
81 istreambuf_iterator<_CharT2>, _CharT2*);
82
83 template<typename _CharT2>
84 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
85 istreambuf_iterator<_CharT2> >::__type
86 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
87 const _CharT2&);
88
89 template<typename _CharT2, typename _Distance>
90 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
91 void>::__type
92 advance(istreambuf_iterator<_CharT2>&, _Distance);
93
94 private:
95 // 24.5.3 istreambuf_iterator
96 // p 1
97 // If the end of stream is reached (streambuf_type::sgetc()
98 // returns traits_type::eof()), the iterator becomes equal to
99 // the "end of stream" iterator value.
100 // NB: This implementation assumes the "end of stream" value
101 // is EOF, or -1.
102 mutable streambuf_type* _M_sbuf;
103 int_type _M_c;
104
105 public:
106 /// Construct end of input stream iterator.
107 _GLIBCXX_CONSTEXPR istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
108 : _M_sbuf(0), _M_c(traits_type::eof()) { }
109
110#if __cplusplus >= 201103L
111 istreambuf_iterator(const istreambuf_iterator&) noexcept = default;
112
113 ~istreambuf_iterator() = default;
114#endif
115
116 /// Construct start of input stream iterator.
117 istreambuf_iterator(istream_type& __s) _GLIBCXX_USE_NOEXCEPT
118 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
119
120 /// Construct start of streambuf iterator.
121 istreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
122 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
123
124 /// Return the current character pointed to by iterator. This returns
125 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
126 /// operator*() on an end of stream is undefined.
127 char_type
128 operator*() const
129 {
130 int_type __c = _M_get();
131
132#ifdef _GLIBCXX_DEBUG_PEDANTIC
133 // Dereferencing a past-the-end istreambuf_iterator is a
134 // libstdc++ extension
135 __glibcxx_requires_cond(!_S_is_eof(__c),
136 _M_message(__gnu_debug::__msg_deref_istreambuf)
137 ._M_iterator(*this));
138#endif
139 return traits_type::to_char_type(__c);
140 }
141
142 /// Advance the iterator. Calls streambuf.sbumpc().
143 istreambuf_iterator&
144 operator++()
145 {
146 __glibcxx_requires_cond(_M_sbuf &&
147 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
148 _M_message(__gnu_debug::__msg_inc_istreambuf)
149 ._M_iterator(*this));
150
151 _M_sbuf->sbumpc();
152 _M_c = traits_type::eof();
153 return *this;
154 }
155
156 /// Advance the iterator. Calls streambuf.sbumpc().
157 istreambuf_iterator
158 operator++(int)
159 {
160 __glibcxx_requires_cond(_M_sbuf &&
161 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
162 _M_message(__gnu_debug::__msg_inc_istreambuf)
163 ._M_iterator(*this));
164
165 istreambuf_iterator __old = *this;
166 __old._M_c = _M_sbuf->sbumpc();
167 _M_c = traits_type::eof();
168 return __old;
169 }
170
171 // _GLIBCXX_RESOLVE_LIB_DEFECTS
172 // 110 istreambuf_iterator::equal not const
173 // NB: there is also number 111 (NAD) relevant to this function.
174 /// Return true both iterators are end or both are not end.
175 bool
176 equal(const istreambuf_iterator& __b) const
177 { return _M_at_eof() == __b._M_at_eof(); }
178
179 private:
180 int_type
181 _M_get() const
182 {
183 int_type __ret = _M_c;
184 if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->sgetc()))
185 _M_sbuf = 0;
186 return __ret;
187 }
188
189 bool
190 _M_at_eof() const
191 { return _S_is_eof(_M_get()); }
192
193 static bool
194 _S_is_eof(int_type __c)
195 {
196 const int_type __eof = traits_type::eof();
197 return traits_type::eq_int_type(__c, __eof);
198 }
199 };
200
201 template<typename _CharT, typename _Traits>
202 inline bool
203 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
204 const istreambuf_iterator<_CharT, _Traits>& __b)
205 { return __a.equal(__b); }
206
207 template<typename _CharT, typename _Traits>
208 inline bool
209 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
210 const istreambuf_iterator<_CharT, _Traits>& __b)
211 { return !__a.equal(__b); }
212
213 /// Provides output iterator semantics for streambufs.
214 template<typename _CharT, typename _Traits>
215 class ostreambuf_iterator
216 : public iterator<output_iterator_tag, void, void, void, void>
217 {
218 public:
219 // Types:
220 //@{
221 /// Public typedefs
222 typedef _CharT char_type;
223 typedef _Traits traits_type;
224 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
225 typedef basic_ostream<_CharT, _Traits> ostream_type;
226 //@}
227
228 template<typename _CharT2>
229 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
230 ostreambuf_iterator<_CharT2> >::__type
231 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
232 ostreambuf_iterator<_CharT2>);
233
234 private:
235 streambuf_type* _M_sbuf;
236 bool _M_failed;
237
238 public:
239 /// Construct output iterator from ostream.
240 ostreambuf_iterator(ostream_type& __s) _GLIBCXX_USE_NOEXCEPT
241 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
242
243 /// Construct output iterator from streambuf.
244 ostreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
245 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
246
247 /// Write character to streambuf. Calls streambuf.sputc().
248 ostreambuf_iterator&
249 operator=(_CharT __c)
250 {
251 if (!_M_failed &&
252 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
253 _M_failed = true;
254 return *this;
255 }
256
257 /// Return *this.
258 ostreambuf_iterator&
259 operator*()
260 { return *this; }
261
262 /// Return *this.
263 ostreambuf_iterator&
264 operator++(int)
265 { return *this; }
266
267 /// Return *this.
268 ostreambuf_iterator&
269 operator++()
270 { return *this; }
271
272 /// Return true if previous operator=() failed.
273 bool
274 failed() const _GLIBCXX_USE_NOEXCEPT
275 { return _M_failed; }
276
277 ostreambuf_iterator&
278 _M_put(const _CharT* __ws, streamsize __len)
279 {
280 if (__builtin_expect(!_M_failed, true)
281 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
282 false))
283 _M_failed = true;
284 return *this;
285 }
286 };
287
288 // Overloads for streambuf iterators.
289 template<typename _CharT>
290 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
291 ostreambuf_iterator<_CharT> >::__type
292 copy(istreambuf_iterator<_CharT> __first,
293 istreambuf_iterator<_CharT> __last,
294 ostreambuf_iterator<_CharT> __result)
295 {
296 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
297 {
298 bool __ineof;
299 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
300 if (!__ineof)
301 __result._M_failed = true;
302 }
303 return __result;
304 }
305
306 template<bool _IsMove, typename _CharT>
307 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
308 ostreambuf_iterator<_CharT> >::__type
309 __copy_move_a2(_CharT* __first, _CharT* __last,
310 ostreambuf_iterator<_CharT> __result)
311 {
312 const streamsize __num = __last - __first;
313 if (__num > 0)
314 __result._M_put(__first, __num);
315 return __result;
316 }
317
318 template<bool _IsMove, typename _CharT>
319 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
320 ostreambuf_iterator<_CharT> >::__type
321 __copy_move_a2(const _CharT* __first, const _CharT* __last,
322 ostreambuf_iterator<_CharT> __result)
323 {
324 const streamsize __num = __last - __first;
325 if (__num > 0)
326 __result._M_put(__first, __num);
327 return __result;
328 }
329
330 template<bool _IsMove, typename _CharT>
331 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
332 _CharT*>::__type
333 __copy_move_a2(istreambuf_iterator<_CharT> __first,
334 istreambuf_iterator<_CharT> __last, _CharT* __result)
335 {
336 typedef istreambuf_iterator<_CharT> __is_iterator_type;
337 typedef typename __is_iterator_type::traits_type traits_type;
338 typedef typename __is_iterator_type::streambuf_type streambuf_type;
339 typedef typename traits_type::int_type int_type;
340
341 if (__first._M_sbuf && !__last._M_sbuf)
342 {
343 streambuf_type* __sb = __first._M_sbuf;
344 int_type __c = __sb->sgetc();
345 while (!traits_type::eq_int_type(__c, traits_type::eof()))
346 {
347 const streamsize __n = __sb->egptr() - __sb->gptr();
348 if (__n > 1)
349 {
350 traits_type::copy(__result, __sb->gptr(), __n);
351 __sb->__safe_gbump(__n);
352 __result += __n;
353 __c = __sb->underflow();
354 }
355 else
356 {
357 *__result++ = traits_type::to_char_type(__c);
358 __c = __sb->snextc();
359 }
360 }
361 }
362 return __result;
363 }
364
365 template<typename _CharT>
366 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
367 istreambuf_iterator<_CharT> >::__type
368 find(istreambuf_iterator<_CharT> __first,
369 istreambuf_iterator<_CharT> __last, const _CharT& __val)
370 {
371 typedef istreambuf_iterator<_CharT> __is_iterator_type;
372 typedef typename __is_iterator_type::traits_type traits_type;
373 typedef typename __is_iterator_type::streambuf_type streambuf_type;
374 typedef typename traits_type::int_type int_type;
375 const int_type __eof = traits_type::eof();
376
377 if (__first._M_sbuf && !__last._M_sbuf)
378 {
379 const int_type __ival = traits_type::to_int_type(__val);
380 streambuf_type* __sb = __first._M_sbuf;
381 int_type __c = __sb->sgetc();
382 while (!traits_type::eq_int_type(__c, __eof)
383 && !traits_type::eq_int_type(__c, __ival))
384 {
385 streamsize __n = __sb->egptr() - __sb->gptr();
386 if (__n > 1)
387 {
388 const _CharT* __p = traits_type::find(__sb->gptr(),
389 __n, __val);
390 if (__p)
391 __n = __p - __sb->gptr();
392 __sb->__safe_gbump(__n);
393 __c = __sb->sgetc();
394 }
395 else
396 __c = __sb->snextc();
397 }
398
399 __first._M_c = __eof;
400 }
401
402 return __first;
403 }
404
405 template<typename _CharT, typename _Distance>
406 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
407 void>::__type
408 advance(istreambuf_iterator<_CharT>& __i, _Distance __n)
409 {
410 if (__n == 0)
411 return;
412
413 __glibcxx_assert(__n > 0);
414 __glibcxx_requires_cond(!__i._M_at_eof(),
415 _M_message(__gnu_debug::__msg_inc_istreambuf)
416 ._M_iterator(__i));
417
418 typedef istreambuf_iterator<_CharT> __is_iterator_type;
419 typedef typename __is_iterator_type::traits_type traits_type;
420 typedef typename __is_iterator_type::streambuf_type streambuf_type;
421 typedef typename traits_type::int_type int_type;
422 const int_type __eof = traits_type::eof();
423
424 streambuf_type* __sb = __i._M_sbuf;
425 while (__n > 0)
426 {
427 streamsize __size = __sb->egptr() - __sb->gptr();
428 if (__size > __n)
429 {
430 __sb->__safe_gbump(__n);
431 break;
432 }
433
434 __sb->__safe_gbump(__size);
435 __n -= __size;
436 if (traits_type::eq_int_type(__sb->underflow(), __eof))
437 {
438 __glibcxx_requires_cond(__n == 0,
439 _M_message(__gnu_debug::__msg_inc_istreambuf)
440 ._M_iterator(__i));
441 break;
442 }
443 }
444
445 __i._M_c = __eof;
446 }
447
448// @} group iterators
449
450_GLIBCXX_END_NAMESPACE_VERSION
451} // namespace
452
453#endif
454