streambuf_iterator.h 13 KB

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