libstdc++
|
00001 // Streambuf iterators 00002 00003 // Copyright (C) 1997-2018 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 /// Return the current character pointed to by iterator. This returns 00125 /// streambuf.sgetc(). It cannot be assigned. NB: The result of 00126 /// operator*() on an end of stream is undefined. 00127 char_type 00128 operator*() const 00129 { 00130 int_type __c = _M_get(); 00131 00132 #ifdef _GLIBCXX_DEBUG_PEDANTIC 00133 // Dereferencing a past-the-end istreambuf_iterator is a 00134 // libstdc++ extension 00135 __glibcxx_requires_cond(!_S_is_eof(__c), 00136 _M_message(__gnu_debug::__msg_deref_istreambuf) 00137 ._M_iterator(*this)); 00138 #endif 00139 return traits_type::to_char_type(__c); 00140 } 00141 00142 /// Advance the iterator. Calls streambuf.sbumpc(). 00143 istreambuf_iterator& 00144 operator++() 00145 { 00146 __glibcxx_requires_cond(_M_sbuf && 00147 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())), 00148 _M_message(__gnu_debug::__msg_inc_istreambuf) 00149 ._M_iterator(*this)); 00150 00151 _M_sbuf->sbumpc(); 00152 _M_c = traits_type::eof(); 00153 return *this; 00154 } 00155 00156 /// Advance the iterator. Calls streambuf.sbumpc(). 00157 istreambuf_iterator 00158 operator++(int) 00159 { 00160 __glibcxx_requires_cond(_M_sbuf && 00161 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())), 00162 _M_message(__gnu_debug::__msg_inc_istreambuf) 00163 ._M_iterator(*this)); 00164 00165 istreambuf_iterator __old = *this; 00166 __old._M_c = _M_sbuf->sbumpc(); 00167 _M_c = traits_type::eof(); 00168 return __old; 00169 } 00170 00171 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00172 // 110 istreambuf_iterator::equal not const 00173 // NB: there is also number 111 (NAD) relevant to this function. 00174 /// Return true both iterators are end or both are not end. 00175 bool 00176 equal(const istreambuf_iterator& __b) const 00177 { return _M_at_eof() == __b._M_at_eof(); } 00178 00179 private: 00180 int_type 00181 _M_get() const 00182 { 00183 int_type __ret = _M_c; 00184 if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->sgetc())) 00185 _M_sbuf = 0; 00186 return __ret; 00187 } 00188 00189 bool 00190 _M_at_eof() const 00191 { return _S_is_eof(_M_get()); } 00192 00193 static bool 00194 _S_is_eof(int_type __c) 00195 { 00196 const int_type __eof = traits_type::eof(); 00197 return traits_type::eq_int_type(__c, __eof); 00198 } 00199 }; 00200 00201 template<typename _CharT, typename _Traits> 00202 inline bool 00203 operator==(const istreambuf_iterator<_CharT, _Traits>& __a, 00204 const istreambuf_iterator<_CharT, _Traits>& __b) 00205 { return __a.equal(__b); } 00206 00207 template<typename _CharT, typename _Traits> 00208 inline bool 00209 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a, 00210 const istreambuf_iterator<_CharT, _Traits>& __b) 00211 { return !__a.equal(__b); } 00212 00213 /// Provides output iterator semantics for streambufs. 00214 template<typename _CharT, typename _Traits> 00215 class ostreambuf_iterator 00216 : public iterator<output_iterator_tag, void, void, void, void> 00217 { 00218 public: 00219 // Types: 00220 //@{ 00221 /// Public typedefs 00222 typedef _CharT char_type; 00223 typedef _Traits traits_type; 00224 typedef basic_streambuf<_CharT, _Traits> streambuf_type; 00225 typedef basic_ostream<_CharT, _Traits> ostream_type; 00226 //@} 00227 00228 template<typename _CharT2> 00229 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value, 00230 ostreambuf_iterator<_CharT2> >::__type 00231 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>, 00232 ostreambuf_iterator<_CharT2>); 00233 00234 private: 00235 streambuf_type* _M_sbuf; 00236 bool _M_failed; 00237 00238 public: 00239 /// Construct output iterator from ostream. 00240 ostreambuf_iterator(ostream_type& __s) _GLIBCXX_USE_NOEXCEPT 00241 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { } 00242 00243 /// Construct output iterator from streambuf. 00244 ostreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT 00245 : _M_sbuf(__s), _M_failed(!_M_sbuf) { } 00246 00247 /// Write character to streambuf. Calls streambuf.sputc(). 00248 ostreambuf_iterator& 00249 operator=(_CharT __c) 00250 { 00251 if (!_M_failed && 00252 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof())) 00253 _M_failed = true; 00254 return *this; 00255 } 00256 00257 /// Return *this. 00258 ostreambuf_iterator& 00259 operator*() 00260 { return *this; } 00261 00262 /// Return *this. 00263 ostreambuf_iterator& 00264 operator++(int) 00265 { return *this; } 00266 00267 /// Return *this. 00268 ostreambuf_iterator& 00269 operator++() 00270 { return *this; } 00271 00272 /// Return true if previous operator=() failed. 00273 bool 00274 failed() const _GLIBCXX_USE_NOEXCEPT 00275 { return _M_failed; } 00276 00277 ostreambuf_iterator& 00278 _M_put(const _CharT* __ws, streamsize __len) 00279 { 00280 if (__builtin_expect(!_M_failed, true) 00281 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len, 00282 false)) 00283 _M_failed = true; 00284 return *this; 00285 } 00286 }; 00287 00288 // Overloads for streambuf iterators. 00289 template<typename _CharT> 00290 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00291 ostreambuf_iterator<_CharT> >::__type 00292 copy(istreambuf_iterator<_CharT> __first, 00293 istreambuf_iterator<_CharT> __last, 00294 ostreambuf_iterator<_CharT> __result) 00295 { 00296 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed) 00297 { 00298 bool __ineof; 00299 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof); 00300 if (!__ineof) 00301 __result._M_failed = true; 00302 } 00303 return __result; 00304 } 00305 00306 template<bool _IsMove, typename _CharT> 00307 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00308 ostreambuf_iterator<_CharT> >::__type 00309 __copy_move_a2(_CharT* __first, _CharT* __last, 00310 ostreambuf_iterator<_CharT> __result) 00311 { 00312 const streamsize __num = __last - __first; 00313 if (__num > 0) 00314 __result._M_put(__first, __num); 00315 return __result; 00316 } 00317 00318 template<bool _IsMove, typename _CharT> 00319 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00320 ostreambuf_iterator<_CharT> >::__type 00321 __copy_move_a2(const _CharT* __first, const _CharT* __last, 00322 ostreambuf_iterator<_CharT> __result) 00323 { 00324 const streamsize __num = __last - __first; 00325 if (__num > 0) 00326 __result._M_put(__first, __num); 00327 return __result; 00328 } 00329 00330 template<bool _IsMove, typename _CharT> 00331 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00332 _CharT*>::__type 00333 __copy_move_a2(istreambuf_iterator<_CharT> __first, 00334 istreambuf_iterator<_CharT> __last, _CharT* __result) 00335 { 00336 typedef istreambuf_iterator<_CharT> __is_iterator_type; 00337 typedef typename __is_iterator_type::traits_type traits_type; 00338 typedef typename __is_iterator_type::streambuf_type streambuf_type; 00339 typedef typename traits_type::int_type int_type; 00340 00341 if (__first._M_sbuf && !__last._M_sbuf) 00342 { 00343 streambuf_type* __sb = __first._M_sbuf; 00344 int_type __c = __sb->sgetc(); 00345 while (!traits_type::eq_int_type(__c, traits_type::eof())) 00346 { 00347 const streamsize __n = __sb->egptr() - __sb->gptr(); 00348 if (__n > 1) 00349 { 00350 traits_type::copy(__result, __sb->gptr(), __n); 00351 __sb->__safe_gbump(__n); 00352 __result += __n; 00353 __c = __sb->underflow(); 00354 } 00355 else 00356 { 00357 *__result++ = traits_type::to_char_type(__c); 00358 __c = __sb->snextc(); 00359 } 00360 } 00361 } 00362 return __result; 00363 } 00364 00365 template<typename _CharT> 00366 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00367 istreambuf_iterator<_CharT> >::__type 00368 find(istreambuf_iterator<_CharT> __first, 00369 istreambuf_iterator<_CharT> __last, const _CharT& __val) 00370 { 00371 typedef istreambuf_iterator<_CharT> __is_iterator_type; 00372 typedef typename __is_iterator_type::traits_type traits_type; 00373 typedef typename __is_iterator_type::streambuf_type streambuf_type; 00374 typedef typename traits_type::int_type int_type; 00375 const int_type __eof = traits_type::eof(); 00376 00377 if (__first._M_sbuf && !__last._M_sbuf) 00378 { 00379 const int_type __ival = traits_type::to_int_type(__val); 00380 streambuf_type* __sb = __first._M_sbuf; 00381 int_type __c = __sb->sgetc(); 00382 while (!traits_type::eq_int_type(__c, __eof) 00383 && !traits_type::eq_int_type(__c, __ival)) 00384 { 00385 streamsize __n = __sb->egptr() - __sb->gptr(); 00386 if (__n > 1) 00387 { 00388 const _CharT* __p = traits_type::find(__sb->gptr(), 00389 __n, __val); 00390 if (__p) 00391 __n = __p - __sb->gptr(); 00392 __sb->__safe_gbump(__n); 00393 __c = __sb->sgetc(); 00394 } 00395 else 00396 __c = __sb->snextc(); 00397 } 00398 00399 __first._M_c = __eof; 00400 } 00401 00402 return __first; 00403 } 00404 00405 template<typename _CharT, typename _Distance> 00406 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value, 00407 void>::__type 00408 advance(istreambuf_iterator<_CharT>& __i, _Distance __n) 00409 { 00410 if (__n == 0) 00411 return; 00412 00413 __glibcxx_assert(__n > 0); 00414 __glibcxx_requires_cond(!__i._M_at_eof(), 00415 _M_message(__gnu_debug::__msg_inc_istreambuf) 00416 ._M_iterator(__i)); 00417 00418 typedef istreambuf_iterator<_CharT> __is_iterator_type; 00419 typedef typename __is_iterator_type::traits_type traits_type; 00420 typedef typename __is_iterator_type::streambuf_type streambuf_type; 00421 typedef typename traits_type::int_type int_type; 00422 const int_type __eof = traits_type::eof(); 00423 00424 streambuf_type* __sb = __i._M_sbuf; 00425 while (__n > 0) 00426 { 00427 streamsize __size = __sb->egptr() - __sb->gptr(); 00428 if (__size > __n) 00429 { 00430 __sb->__safe_gbump(__n); 00431 break; 00432 } 00433 00434 __sb->__safe_gbump(__size); 00435 __n -= __size; 00436 if (traits_type::eq_int_type(__sb->underflow(), __eof)) 00437 { 00438 __glibcxx_requires_cond(__n == 0, 00439 _M_message(__gnu_debug::__msg_inc_istreambuf) 00440 ._M_iterator(__i)); 00441 break; 00442 } 00443 } 00444 00445 __i._M_c = __eof; 00446 } 00447 00448 // @} group iterators 00449 00450 _GLIBCXX_END_NAMESPACE_VERSION 00451 } // namespace 00452 00453 #endif