libstdc++
|
00001 // Streambuf iterators 00002 00003 // Copyright (C) 1997-2019 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/streambuf_iterator.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{iterator} 00028 */ 00029 00030 #ifndef _STREAMBUF_ITERATOR_H 00031 #define _STREAMBUF_ITERATOR_H 1 00032 00033 #pragma GCC system_header 00034 00035 #include <streambuf> 00036 #include <debug/debug.h> 00037 00038 namespace std _GLIBCXX_VISIBILITY(default) 00039 { 00040 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00041 00042 /** 00043 * @addtogroup iterators 00044 * @{ 00045 */ 00046 00047 // 24.5.3 Template class istreambuf_iterator 00048 /// Provides input iterator semantics for streambufs. 00049 template<typename _CharT, typename _Traits> 00050 class istreambuf_iterator 00051 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type, 00052 _CharT*, 00053 #if __cplusplus >= 201103L 00054 // LWG 445. 00055 _CharT> 00056 #else 00057 _CharT&> 00058 #endif 00059 { 00060 public: 00061 // Types: 00062 //@{ 00063 /// Public typedefs 00064 typedef _CharT char_type; 00065 typedef _Traits traits_type; 00066 typedef typename _Traits::int_type int_type; 00067 typedef basic_streambuf<_CharT, _Traits> streambuf_type; 00068 typedef basic_istream<_CharT, _Traits> istream_type; 00069 //@} 00070 00071 template<typename _CharT2> 00072 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, 00073 ostreambuf_iterator<_CharT2> >::__type 00074 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>, 00075 ostreambuf_iterator<_CharT2>); 00076 00077 template<bool _IsMove, typename _CharT2> 00078 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, 00079 _CharT2*>::__type 00080 __copy_move_a2(istreambuf_iterator<_CharT2>, 00081 istreambuf_iterator<_CharT2>, _CharT2*); 00082 00083 template<typename _CharT2> 00084 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, 00085 istreambuf_iterator<_CharT2> >::__type 00086 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>, 00087 const _CharT2&); 00088 00089 template<typename _CharT2, typename _Distance> 00090 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, 00091 void>::__type 00092 advance(istreambuf_iterator<_CharT2>&, _Distance); 00093 00094 private: 00095 // 24.5.3 istreambuf_iterator 00096 // p 1 00097 // If the end of stream is reached (streambuf_type::sgetc() 00098 // returns traits_type::eof()), the iterator becomes equal to 00099 // the "end of stream" iterator value. 00100 // NB: This implementation assumes the "end of stream" value 00101 // is EOF, or -1. 00102 mutable streambuf_type* _M_sbuf; 00103 int_type _M_c; 00104 00105 public: 00106 /// Construct end of input stream iterator. 00107 _GLIBCXX_CONSTEXPR istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT 00108 : _M_sbuf(0), _M_c(traits_type::eof()) { } 00109 00110 #if __cplusplus >= 201103L 00111 istreambuf_iterator(const istreambuf_iterator&) noexcept = default; 00112 00113 ~istreambuf_iterator() = default; 00114 #endif 00115 00116 /// Construct start of input stream iterator. 00117 istreambuf_iterator(istream_type& __s) _GLIBCXX_USE_NOEXCEPT 00118 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { } 00119 00120 /// Construct start of streambuf iterator. 00121 istreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT 00122 : _M_sbuf(__s), _M_c(traits_type::eof()) { } 00123 00124 #if __cplusplus >= 201103L 00125 istreambuf_iterator& 00126 operator=(const istreambuf_iterator&) noexcept = default; 00127 #endif 00128 00129 /// Return the current character pointed to by iterator. This returns 00130 /// streambuf.sgetc(). It cannot be assigned. NB: The result of 00131 /// operator*() on an end of stream is undefined. 00132 char_type 00133 operator*() const 00134 { 00135 int_type __c = _M_get(); 00136 00137 #ifdef _GLIBCXX_DEBUG_PEDANTIC 00138 // Dereferencing a past-the-end istreambuf_iterator is a 00139 // libstdc++ extension 00140 __glibcxx_requires_cond(!_S_is_eof(__c), 00141 _M_message(__gnu_debug::__msg_deref_istreambuf) 00142 ._M_iterator(*this)); 00143 #endif 00144 return traits_type::to_char_type(__c); 00145 } 00146 00147 /// Advance the iterator. Calls streambuf.sbumpc(). 00148 istreambuf_iterator& 00149 operator++() 00150 { 00151 __glibcxx_requires_cond(_M_sbuf && 00152 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())), 00153 _M_message(__gnu_debug::__msg_inc_istreambuf) 00154 ._M_iterator(*this)); 00155 00156 _M_sbuf->sbumpc(); 00157 _M_c = traits_type::eof(); 00158 return *this; 00159 } 00160 00161 /// Advance the iterator. Calls streambuf.sbumpc(). 00162 istreambuf_iterator 00163 operator++(int) 00164 { 00165 __glibcxx_requires_cond(_M_sbuf && 00166 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())), 00167 _M_message(__gnu_debug::__msg_inc_istreambuf) 00168 ._M_iterator(*this)); 00169 00170 istreambuf_iterator __old = *this; 00171 __old._M_c = _M_sbuf->sbumpc(); 00172 _M_c = traits_type::eof(); 00173 return __old; 00174 } 00175 00176 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00177 // 110 istreambuf_iterator::equal not const 00178 // NB: there is also number 111 (NAD) relevant to this function. 00179 /// Return true both iterators are end or both are not end. 00180 bool 00181 equal(const istreambuf_iterator& __b) const 00182 { return _M_at_eof() == __b._M_at_eof(); } 00183 00184 private: 00185 int_type 00186 _M_get() const 00187 { 00188 int_type __ret = _M_c; 00189 if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->sgetc())) 00190 _M_sbuf = 0; 00191 return __ret; 00192 } 00193 00194 bool 00195 _M_at_eof() const 00196 { return _S_is_eof(_M_get()); } 00197 00198 static bool 00199 _S_is_eof(int_type __c) 00200 { 00201 const int_type __eof = traits_type::eof(); 00202 return traits_type::eq_int_type(__c, __eof); 00203 } 00204 }; 00205 00206 template<typename _CharT, typename _Traits> 00207 inline bool 00208 operator==(const istreambuf_iterator<_CharT, _Traits>& __a, 00209 const istreambuf_iterator<_CharT, _Traits>& __b) 00210 { return __a.equal(__b); } 00211 00212 template<typename _CharT, typename _Traits> 00213 inline bool 00214 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a, 00215 const istreambuf_iterator<_CharT, _Traits>& __b) 00216 { return !__a.equal(__b); } 00217 00218 /// Provides output iterator semantics for streambufs. 00219 template<typename _CharT, typename _Traits> 00220 class ostreambuf_iterator 00221 : public iterator<output_iterator_tag, void, void, void, void> 00222 { 00223 public: 00224 // Types: 00225 //@{ 00226 /// Public typedefs 00227 typedef _CharT char_type; 00228 typedef _Traits traits_type; 00229 typedef basic_streambuf<_CharT, _Traits> streambuf_type; 00230 typedef basic_ostream<_CharT, _Traits> ostream_type; 00231 //@} 00232 00233 template<typename _CharT2> 00234 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, 00235 ostreambuf_iterator<_CharT2> >::__type 00236 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>, 00237 ostreambuf_iterator<_CharT2>); 00238 00239 private: 00240 streambuf_type* _M_sbuf; 00241 bool _M_failed; 00242 00243 public: 00244 /// Construct output iterator from ostream. 00245 ostreambuf_iterator(ostream_type& __s) _GLIBCXX_USE_NOEXCEPT 00246 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { } 00247 00248 /// Construct output iterator from streambuf. 00249 ostreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT 00250 : _M_sbuf(__s), _M_failed(!_M_sbuf) { } 00251 00252 /// Write character to streambuf. Calls streambuf.sputc(). 00253 ostreambuf_iterator& 00254 operator=(_CharT __c) 00255 { 00256 if (!_M_failed && 00257 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof())) 00258 _M_failed = true; 00259 return *this; 00260 } 00261 00262 /// Return *this. 00263 ostreambuf_iterator& 00264 operator*() 00265 { return *this; } 00266 00267 /// Return *this. 00268 ostreambuf_iterator& 00269 operator++(int) 00270 { return *this; } 00271 00272 /// Return *this. 00273 ostreambuf_iterator& 00274 operator++() 00275 { return *this; } 00276 00277 /// Return true if previous operator=() failed. 00278 bool 00279 failed() const _GLIBCXX_USE_NOEXCEPT 00280 { return _M_failed; } 00281 00282 ostreambuf_iterator& 00283 _M_put(const _CharT* __ws, streamsize __len) 00284 { 00285 if (__builtin_expect(!_M_failed, true) 00286 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len, 00287 false)) 00288 _M_failed = true; 00289 return *this; 00290 } 00291 }; 00292 00293 // Overloads for streambuf iterators. 00294 template<typename _CharT> 00295 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00296 ostreambuf_iterator<_CharT> >::__type 00297 copy(istreambuf_iterator<_CharT> __first, 00298 istreambuf_iterator<_CharT> __last, 00299 ostreambuf_iterator<_CharT> __result) 00300 { 00301 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed) 00302 { 00303 bool __ineof; 00304 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof); 00305 if (!__ineof) 00306 __result._M_failed = true; 00307 } 00308 return __result; 00309 } 00310 00311 template<bool _IsMove, typename _CharT> 00312 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00313 ostreambuf_iterator<_CharT> >::__type 00314 __copy_move_a2(_CharT* __first, _CharT* __last, 00315 ostreambuf_iterator<_CharT> __result) 00316 { 00317 const streamsize __num = __last - __first; 00318 if (__num > 0) 00319 __result._M_put(__first, __num); 00320 return __result; 00321 } 00322 00323 template<bool _IsMove, typename _CharT> 00324 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00325 ostreambuf_iterator<_CharT> >::__type 00326 __copy_move_a2(const _CharT* __first, const _CharT* __last, 00327 ostreambuf_iterator<_CharT> __result) 00328 { 00329 const streamsize __num = __last - __first; 00330 if (__num > 0) 00331 __result._M_put(__first, __num); 00332 return __result; 00333 } 00334 00335 template<bool _IsMove, typename _CharT> 00336 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00337 _CharT*>::__type 00338 __copy_move_a2(istreambuf_iterator<_CharT> __first, 00339 istreambuf_iterator<_CharT> __last, _CharT* __result) 00340 { 00341 typedef istreambuf_iterator<_CharT> __is_iterator_type; 00342 typedef typename __is_iterator_type::traits_type traits_type; 00343 typedef typename __is_iterator_type::streambuf_type streambuf_type; 00344 typedef typename traits_type::int_type int_type; 00345 00346 if (__first._M_sbuf && !__last._M_sbuf) 00347 { 00348 streambuf_type* __sb = __first._M_sbuf; 00349 int_type __c = __sb->sgetc(); 00350 while (!traits_type::eq_int_type(__c, traits_type::eof())) 00351 { 00352 const streamsize __n = __sb->egptr() - __sb->gptr(); 00353 if (__n > 1) 00354 { 00355 traits_type::copy(__result, __sb->gptr(), __n); 00356 __sb->__safe_gbump(__n); 00357 __result += __n; 00358 __c = __sb->underflow(); 00359 } 00360 else 00361 { 00362 *__result++ = traits_type::to_char_type(__c); 00363 __c = __sb->snextc(); 00364 } 00365 } 00366 } 00367 return __result; 00368 } 00369 00370 template<typename _CharT> 00371 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00372 istreambuf_iterator<_CharT> >::__type 00373 find(istreambuf_iterator<_CharT> __first, 00374 istreambuf_iterator<_CharT> __last, const _CharT& __val) 00375 { 00376 typedef istreambuf_iterator<_CharT> __is_iterator_type; 00377 typedef typename __is_iterator_type::traits_type traits_type; 00378 typedef typename __is_iterator_type::streambuf_type streambuf_type; 00379 typedef typename traits_type::int_type int_type; 00380 const int_type __eof = traits_type::eof(); 00381 00382 if (__first._M_sbuf && !__last._M_sbuf) 00383 { 00384 const int_type __ival = traits_type::to_int_type(__val); 00385 streambuf_type* __sb = __first._M_sbuf; 00386 int_type __c = __sb->sgetc(); 00387 while (!traits_type::eq_int_type(__c, __eof) 00388 && !traits_type::eq_int_type(__c, __ival)) 00389 { 00390 streamsize __n = __sb->egptr() - __sb->gptr(); 00391 if (__n > 1) 00392 { 00393 const _CharT* __p = traits_type::find(__sb->gptr(), 00394 __n, __val); 00395 if (__p) 00396 __n = __p - __sb->gptr(); 00397 __sb->__safe_gbump(__n); 00398 __c = __sb->sgetc(); 00399 } 00400 else 00401 __c = __sb->snextc(); 00402 } 00403 00404 __first._M_c = __eof; 00405 } 00406 00407 return __first; 00408 } 00409 00410 template<typename _CharT, typename _Distance> 00411 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00412 void>::__type 00413 advance(istreambuf_iterator<_CharT>& __i, _Distance __n) 00414 { 00415 if (__n == 0) 00416 return; 00417 00418 __glibcxx_assert(__n > 0); 00419 __glibcxx_requires_cond(!__i._M_at_eof(), 00420 _M_message(__gnu_debug::__msg_inc_istreambuf) 00421 ._M_iterator(__i)); 00422 00423 typedef istreambuf_iterator<_CharT> __is_iterator_type; 00424 typedef typename __is_iterator_type::traits_type traits_type; 00425 typedef typename __is_iterator_type::streambuf_type streambuf_type; 00426 typedef typename traits_type::int_type int_type; 00427 const int_type __eof = traits_type::eof(); 00428 00429 streambuf_type* __sb = __i._M_sbuf; 00430 while (__n > 0) 00431 { 00432 streamsize __size = __sb->egptr() - __sb->gptr(); 00433 if (__size > __n) 00434 { 00435 __sb->__safe_gbump(__n); 00436 break; 00437 } 00438 00439 __sb->__safe_gbump(__size); 00440 __n -= __size; 00441 if (traits_type::eq_int_type(__sb->underflow(), __eof)) 00442 { 00443 __glibcxx_requires_cond(__n == 0, 00444 _M_message(__gnu_debug::__msg_inc_istreambuf) 00445 ._M_iterator(__i)); 00446 break; 00447 } 00448 } 00449 00450 __i._M_c = __eof; 00451 } 00452 00453 // @} group iterators 00454 00455 _GLIBCXX_END_NAMESPACE_VERSION 00456 } // namespace 00457 00458 #endif