streambuf_iterator.h 15 KB

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