RISC-V: Add --specs=nosys.specs support.
[official-gcc.git] / libstdc++-v3 / include / bits / streambuf_iterator.h
blob292ef3a5335445575ec1e24a540f29a8ec131a20
1 // Streambuf iterators
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.
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.
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.
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/>.
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}
30 #ifndef _STREAMBUF_ITERATOR_H
31 #define _STREAMBUF_ITERATOR_H 1
33 #pragma GCC system_header
35 #include <streambuf>
36 #include <debug/debug.h>
38 namespace std _GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 /**
43 * @addtogroup iterators
44 * @{
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
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 //@}
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>);
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*);
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&);
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);
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;
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()) { }
110 #if __cplusplus >= 201103L
111 istreambuf_iterator(const istreambuf_iterator&) noexcept = default;
113 ~istreambuf_iterator() = default;
114 #endif
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()) { }
120 /// Construct start of streambuf iterator.
121 istreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
122 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
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
130 int_type __c = _M_get();
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);
142 /// Advance the iterator. Calls streambuf.sbumpc().
143 istreambuf_iterator&
144 operator++()
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));
151 _M_sbuf->sbumpc();
152 _M_c = traits_type::eof();
153 return *this;
156 /// Advance the iterator. Calls streambuf.sbumpc().
157 istreambuf_iterator
158 operator++(int)
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));
165 istreambuf_iterator __old = *this;
166 __old._M_c = _M_sbuf->sbumpc();
167 _M_c = traits_type::eof();
168 return __old;
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(); }
179 private:
180 int_type
181 _M_get() const
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;
189 bool
190 _M_at_eof() const
191 { return _S_is_eof(_M_get()); }
193 static bool
194 _S_is_eof(int_type __c)
196 const int_type __eof = traits_type::eof();
197 return traits_type::eq_int_type(__c, __eof);
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); }
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); }
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>
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 //@}
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>);
234 private:
235 streambuf_type* _M_sbuf;
236 bool _M_failed;
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) { }
243 /// Construct output iterator from streambuf.
244 ostreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
245 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
247 /// Write character to streambuf. Calls streambuf.sputc().
248 ostreambuf_iterator&
249 operator=(_CharT __c)
251 if (!_M_failed &&
252 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
253 _M_failed = true;
254 return *this;
257 /// Return *this.
258 ostreambuf_iterator&
259 operator*()
260 { return *this; }
262 /// Return *this.
263 ostreambuf_iterator&
264 operator++(int)
265 { return *this; }
267 /// Return *this.
268 ostreambuf_iterator&
269 operator++()
270 { return *this; }
272 /// Return true if previous operator=() failed.
273 bool
274 failed() const _GLIBCXX_USE_NOEXCEPT
275 { return _M_failed; }
277 ostreambuf_iterator&
278 _M_put(const _CharT* __ws, streamsize __len)
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;
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)
296 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
298 bool __ineof;
299 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
300 if (!__ineof)
301 __result._M_failed = true;
303 return __result;
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)
312 const streamsize __num = __last - __first;
313 if (__num > 0)
314 __result._M_put(__first, __num);
315 return __result;
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)
324 const streamsize __num = __last - __first;
325 if (__num > 0)
326 __result._M_put(__first, __num);
327 return __result;
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)
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;
341 if (__first._M_sbuf && !__last._M_sbuf)
343 streambuf_type* __sb = __first._M_sbuf;
344 int_type __c = __sb->sgetc();
345 while (!traits_type::eq_int_type(__c, traits_type::eof()))
347 const streamsize __n = __sb->egptr() - __sb->gptr();
348 if (__n > 1)
350 traits_type::copy(__result, __sb->gptr(), __n);
351 __sb->__safe_gbump(__n);
352 __result += __n;
353 __c = __sb->underflow();
355 else
357 *__result++ = traits_type::to_char_type(__c);
358 __c = __sb->snextc();
362 return __result;
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)
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();
377 if (__first._M_sbuf && !__last._M_sbuf)
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))
385 streamsize __n = __sb->egptr() - __sb->gptr();
386 if (__n > 1)
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();
395 else
396 __c = __sb->snextc();
399 __first._M_c = __eof;
402 return __first;
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)
410 if (__n == 0)
411 return;
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));
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();
424 streambuf_type* __sb = __i._M_sbuf;
425 while (__n > 0)
427 streamsize __size = __sb->egptr() - __sb->gptr();
428 if (__size > __n)
430 __sb->__safe_gbump(__n);
431 break;
434 __sb->__safe_gbump(__size);
435 __n -= __size;
436 if (traits_type::eq_int_type(__sb->underflow(), __eof))
438 __glibcxx_requires_cond(__n == 0,
439 _M_message(__gnu_debug::__msg_inc_istreambuf)
440 ._M_iterator(__i));
441 break;
445 __i._M_c = __eof;
448 // @} group iterators
450 _GLIBCXX_END_NAMESPACE_VERSION
451 } // namespace
453 #endif