streambuf_iterator.h 14 KB

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