streambuf 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_STREAMBUF
  10. #define _LIBCPP_STREAMBUF
  11. /*
  12. streambuf synopsis
  13. namespace std
  14. {
  15. template <class charT, class traits = char_traits<charT> >
  16. class basic_streambuf
  17. {
  18. public:
  19. // types:
  20. typedef charT char_type;
  21. typedef traits traits_type;
  22. typedef typename traits_type::int_type int_type;
  23. typedef typename traits_type::pos_type pos_type;
  24. typedef typename traits_type::off_type off_type;
  25. virtual ~basic_streambuf();
  26. // 27.6.2.2.1 locales:
  27. locale pubimbue(const locale& loc);
  28. locale getloc() const;
  29. // 27.6.2.2.2 buffer and positioning:
  30. basic_streambuf* pubsetbuf(char_type* s, streamsize n);
  31. pos_type pubseekoff(off_type off, ios_base::seekdir way,
  32. ios_base::openmode which = ios_base::in | ios_base::out);
  33. pos_type pubseekpos(pos_type sp,
  34. ios_base::openmode which = ios_base::in | ios_base::out);
  35. int pubsync();
  36. // Get and put areas:
  37. // 27.6.2.2.3 Get area:
  38. streamsize in_avail();
  39. int_type snextc();
  40. int_type sbumpc();
  41. int_type sgetc();
  42. streamsize sgetn(char_type* s, streamsize n);
  43. // 27.6.2.2.4 Putback:
  44. int_type sputbackc(char_type c);
  45. int_type sungetc();
  46. // 27.6.2.2.5 Put area:
  47. int_type sputc(char_type c);
  48. streamsize sputn(const char_type* s, streamsize n);
  49. protected:
  50. basic_streambuf();
  51. basic_streambuf(const basic_streambuf& rhs);
  52. basic_streambuf& operator=(const basic_streambuf& rhs);
  53. void swap(basic_streambuf& rhs);
  54. // 27.6.2.3.2 Get area:
  55. char_type* eback() const;
  56. char_type* gptr() const;
  57. char_type* egptr() const;
  58. void gbump(int n);
  59. void setg(char_type* gbeg, char_type* gnext, char_type* gend);
  60. // 27.6.2.3.3 Put area:
  61. char_type* pbase() const;
  62. char_type* pptr() const;
  63. char_type* epptr() const;
  64. void pbump(int n);
  65. void setp(char_type* pbeg, char_type* pend);
  66. // 27.6.2.4 virtual functions:
  67. // 27.6.2.4.1 Locales:
  68. virtual void imbue(const locale& loc);
  69. // 27.6.2.4.2 Buffer management and positioning:
  70. virtual basic_streambuf* setbuf(char_type* s, streamsize n);
  71. virtual pos_type seekoff(off_type off, ios_base::seekdir way,
  72. ios_base::openmode which = ios_base::in | ios_base::out);
  73. virtual pos_type seekpos(pos_type sp,
  74. ios_base::openmode which = ios_base::in | ios_base::out);
  75. virtual int sync();
  76. // 27.6.2.4.3 Get area:
  77. virtual streamsize showmanyc();
  78. virtual streamsize xsgetn(char_type* s, streamsize n);
  79. virtual int_type underflow();
  80. virtual int_type uflow();
  81. // 27.6.2.4.4 Putback:
  82. virtual int_type pbackfail(int_type c = traits_type::eof());
  83. // 27.6.2.4.5 Put area:
  84. virtual streamsize xsputn(const char_type* s, streamsize n);
  85. virtual int_type overflow (int_type c = traits_type::eof());
  86. };
  87. } // std
  88. */
  89. #include <__assert> // all public C++ headers provide the assertion handler
  90. #include <__config>
  91. #include <__fwd/streambuf.h>
  92. #include <cstdint>
  93. #include <ios>
  94. #include <iosfwd>
  95. #include <version>
  96. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  97. # pragma GCC system_header
  98. #endif
  99. _LIBCPP_PUSH_MACROS
  100. #include <__undef_macros>
  101. _LIBCPP_BEGIN_NAMESPACE_STD
  102. template <class _CharT, class _Traits>
  103. class _LIBCPP_TEMPLATE_VIS basic_streambuf
  104. {
  105. public:
  106. // types:
  107. typedef _CharT char_type;
  108. typedef _Traits traits_type;
  109. typedef typename traits_type::int_type int_type;
  110. typedef typename traits_type::pos_type pos_type;
  111. typedef typename traits_type::off_type off_type;
  112. static_assert((is_same<_CharT, typename traits_type::char_type>::value),
  113. "traits_type::char_type must be the same type as CharT");
  114. virtual ~basic_streambuf();
  115. // 27.6.2.2.1 locales:
  116. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  117. locale pubimbue(const locale& __loc) {
  118. imbue(__loc);
  119. locale __r = __loc_;
  120. __loc_ = __loc;
  121. return __r;
  122. }
  123. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  124. locale getloc() const { return __loc_; }
  125. // 27.6.2.2.2 buffer and positioning:
  126. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  127. basic_streambuf* pubsetbuf(char_type* __s, streamsize __n)
  128. { return setbuf(__s, __n); }
  129. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  130. pos_type pubseekoff(off_type __off, ios_base::seekdir __way,
  131. ios_base::openmode __which = ios_base::in | ios_base::out)
  132. { return seekoff(__off, __way, __which); }
  133. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  134. pos_type pubseekpos(pos_type __sp,
  135. ios_base::openmode __which = ios_base::in | ios_base::out)
  136. { return seekpos(__sp, __which); }
  137. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  138. int pubsync() { return sync(); }
  139. // Get and put areas:
  140. // 27.6.2.2.3 Get area:
  141. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  142. streamsize in_avail() {
  143. if (__ninp_ < __einp_)
  144. return static_cast<streamsize>(__einp_ - __ninp_);
  145. return showmanyc();
  146. }
  147. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  148. int_type snextc() {
  149. if (sbumpc() == traits_type::eof())
  150. return traits_type::eof();
  151. return sgetc();
  152. }
  153. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  154. int_type sbumpc() {
  155. if (__ninp_ == __einp_)
  156. return uflow();
  157. return traits_type::to_int_type(*__ninp_++);
  158. }
  159. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  160. int_type sgetc() {
  161. if (__ninp_ == __einp_)
  162. return underflow();
  163. return traits_type::to_int_type(*__ninp_);
  164. }
  165. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  166. streamsize sgetn(char_type* __s, streamsize __n)
  167. { return xsgetn(__s, __n); }
  168. // 27.6.2.2.4 Putback:
  169. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  170. int_type sputbackc(char_type __c) {
  171. if (__binp_ == __ninp_ || !traits_type::eq(__c, __ninp_[-1]))
  172. return pbackfail(traits_type::to_int_type(__c));
  173. return traits_type::to_int_type(*--__ninp_);
  174. }
  175. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  176. int_type sungetc() {
  177. if (__binp_ == __ninp_)
  178. return pbackfail();
  179. return traits_type::to_int_type(*--__ninp_);
  180. }
  181. // 27.6.2.2.5 Put area:
  182. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  183. int_type sputc(char_type __c) {
  184. if (__nout_ == __eout_)
  185. return overflow(traits_type::to_int_type(__c));
  186. *__nout_++ = __c;
  187. return traits_type::to_int_type(__c);
  188. }
  189. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  190. streamsize sputn(const char_type* __s, streamsize __n)
  191. { return xsputn(__s, __n); }
  192. protected:
  193. basic_streambuf();
  194. basic_streambuf(const basic_streambuf& __rhs);
  195. basic_streambuf& operator=(const basic_streambuf& __rhs);
  196. void swap(basic_streambuf& __rhs);
  197. // 27.6.2.3.2 Get area:
  198. _LIBCPP_INLINE_VISIBILITY char_type* eback() const {return __binp_;}
  199. _LIBCPP_INLINE_VISIBILITY char_type* gptr() const {return __ninp_;}
  200. _LIBCPP_INLINE_VISIBILITY char_type* egptr() const {return __einp_;}
  201. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  202. void gbump(int __n) { __ninp_ += __n; }
  203. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  204. void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend) {
  205. __binp_ = __gbeg;
  206. __ninp_ = __gnext;
  207. __einp_ = __gend;
  208. }
  209. // 27.6.2.3.3 Put area:
  210. _LIBCPP_INLINE_VISIBILITY char_type* pbase() const {return __bout_;}
  211. _LIBCPP_INLINE_VISIBILITY char_type* pptr() const {return __nout_;}
  212. _LIBCPP_INLINE_VISIBILITY char_type* epptr() const {return __eout_;}
  213. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  214. void pbump(int __n) { __nout_ += __n; }
  215. _LIBCPP_INLINE_VISIBILITY
  216. void __pbump(streamsize __n) { __nout_ += __n; }
  217. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  218. void setp(char_type* __pbeg, char_type* __pend) {
  219. __bout_ = __nout_ = __pbeg;
  220. __eout_ = __pend;
  221. }
  222. // 27.6.2.4 virtual functions:
  223. // 27.6.2.4.1 Locales:
  224. virtual void imbue(const locale& __loc);
  225. // 27.6.2.4.2 Buffer management and positioning:
  226. virtual basic_streambuf* setbuf(char_type* __s, streamsize __n);
  227. virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
  228. ios_base::openmode __which = ios_base::in | ios_base::out);
  229. virtual pos_type seekpos(pos_type __sp,
  230. ios_base::openmode __which = ios_base::in | ios_base::out);
  231. virtual int sync();
  232. // 27.6.2.4.3 Get area:
  233. virtual streamsize showmanyc();
  234. virtual streamsize xsgetn(char_type* __s, streamsize __n);
  235. virtual int_type underflow();
  236. virtual int_type uflow();
  237. // 27.6.2.4.4 Putback:
  238. virtual int_type pbackfail(int_type __c = traits_type::eof());
  239. // 27.6.2.4.5 Put area:
  240. virtual streamsize xsputn(const char_type* __s, streamsize __n);
  241. virtual int_type overflow(int_type __c = traits_type::eof());
  242. private:
  243. locale __loc_;
  244. char_type* __binp_;
  245. char_type* __ninp_;
  246. char_type* __einp_;
  247. char_type* __bout_;
  248. char_type* __nout_;
  249. char_type* __eout_;
  250. };
  251. template <class _CharT, class _Traits>
  252. basic_streambuf<_CharT, _Traits>::~basic_streambuf()
  253. {
  254. }
  255. template <class _CharT, class _Traits>
  256. basic_streambuf<_CharT, _Traits>::basic_streambuf()
  257. : __binp_(nullptr),
  258. __ninp_(nullptr),
  259. __einp_(nullptr),
  260. __bout_(nullptr),
  261. __nout_(nullptr),
  262. __eout_(nullptr)
  263. {
  264. }
  265. template <class _CharT, class _Traits>
  266. basic_streambuf<_CharT, _Traits>::basic_streambuf(const basic_streambuf& __sb)
  267. : __loc_(__sb.__loc_),
  268. __binp_(__sb.__binp_),
  269. __ninp_(__sb.__ninp_),
  270. __einp_(__sb.__einp_),
  271. __bout_(__sb.__bout_),
  272. __nout_(__sb.__nout_),
  273. __eout_(__sb.__eout_)
  274. {
  275. }
  276. template <class _CharT, class _Traits>
  277. basic_streambuf<_CharT, _Traits>&
  278. basic_streambuf<_CharT, _Traits>::operator=(const basic_streambuf& __sb)
  279. {
  280. __loc_ = __sb.__loc_;
  281. __binp_ = __sb.__binp_;
  282. __ninp_ = __sb.__ninp_;
  283. __einp_ = __sb.__einp_;
  284. __bout_ = __sb.__bout_;
  285. __nout_ = __sb.__nout_;
  286. __eout_ = __sb.__eout_;
  287. return *this;
  288. }
  289. template <class _CharT, class _Traits>
  290. void
  291. basic_streambuf<_CharT, _Traits>::swap(basic_streambuf& __sb)
  292. {
  293. _VSTD::swap(__loc_, __sb.__loc_);
  294. _VSTD::swap(__binp_, __sb.__binp_);
  295. _VSTD::swap(__ninp_, __sb.__ninp_);
  296. _VSTD::swap(__einp_, __sb.__einp_);
  297. _VSTD::swap(__bout_, __sb.__bout_);
  298. _VSTD::swap(__nout_, __sb.__nout_);
  299. _VSTD::swap(__eout_, __sb.__eout_);
  300. }
  301. template <class _CharT, class _Traits>
  302. void
  303. basic_streambuf<_CharT, _Traits>::imbue(const locale&)
  304. {
  305. }
  306. template <class _CharT, class _Traits>
  307. basic_streambuf<_CharT, _Traits>*
  308. basic_streambuf<_CharT, _Traits>::setbuf(char_type*, streamsize)
  309. {
  310. return this;
  311. }
  312. template <class _CharT, class _Traits>
  313. typename basic_streambuf<_CharT, _Traits>::pos_type
  314. basic_streambuf<_CharT, _Traits>::seekoff(off_type, ios_base::seekdir,
  315. ios_base::openmode)
  316. {
  317. return pos_type(off_type(-1));
  318. }
  319. template <class _CharT, class _Traits>
  320. typename basic_streambuf<_CharT, _Traits>::pos_type
  321. basic_streambuf<_CharT, _Traits>::seekpos(pos_type, ios_base::openmode)
  322. {
  323. return pos_type(off_type(-1));
  324. }
  325. template <class _CharT, class _Traits>
  326. int
  327. basic_streambuf<_CharT, _Traits>::sync()
  328. {
  329. return 0;
  330. }
  331. template <class _CharT, class _Traits>
  332. streamsize
  333. basic_streambuf<_CharT, _Traits>::showmanyc()
  334. {
  335. return 0;
  336. }
  337. template <class _CharT, class _Traits>
  338. streamsize
  339. basic_streambuf<_CharT, _Traits>::xsgetn(char_type* __s, streamsize __n)
  340. {
  341. const int_type __eof = traits_type::eof();
  342. int_type __c;
  343. streamsize __i = 0;
  344. while(__i < __n)
  345. {
  346. if (__ninp_ < __einp_)
  347. {
  348. const streamsize __len = _VSTD::min(static_cast<streamsize>(INT_MAX),
  349. _VSTD::min(__einp_ - __ninp_, __n - __i));
  350. traits_type::copy(__s, __ninp_, __len);
  351. __s += __len;
  352. __i += __len;
  353. this->gbump(__len);
  354. }
  355. else if ((__c = uflow()) != __eof)
  356. {
  357. *__s = traits_type::to_char_type(__c);
  358. ++__s;
  359. ++__i;
  360. }
  361. else
  362. break;
  363. }
  364. return __i;
  365. }
  366. template <class _CharT, class _Traits>
  367. typename basic_streambuf<_CharT, _Traits>::int_type
  368. basic_streambuf<_CharT, _Traits>::underflow()
  369. {
  370. return traits_type::eof();
  371. }
  372. template <class _CharT, class _Traits>
  373. typename basic_streambuf<_CharT, _Traits>::int_type
  374. basic_streambuf<_CharT, _Traits>::uflow()
  375. {
  376. if (underflow() == traits_type::eof())
  377. return traits_type::eof();
  378. return traits_type::to_int_type(*__ninp_++);
  379. }
  380. template <class _CharT, class _Traits>
  381. typename basic_streambuf<_CharT, _Traits>::int_type
  382. basic_streambuf<_CharT, _Traits>::pbackfail(int_type)
  383. {
  384. return traits_type::eof();
  385. }
  386. template <class _CharT, class _Traits>
  387. streamsize
  388. basic_streambuf<_CharT, _Traits>::xsputn(const char_type* __s, streamsize __n)
  389. {
  390. streamsize __i = 0;
  391. int_type __eof = traits_type::eof();
  392. while( __i < __n)
  393. {
  394. if (__nout_ >= __eout_)
  395. {
  396. if (overflow(traits_type::to_int_type(*__s)) == __eof)
  397. break;
  398. ++__s;
  399. ++__i;
  400. }
  401. else
  402. {
  403. streamsize __chunk_size = _VSTD::min(__eout_ - __nout_, __n - __i);
  404. traits_type::copy(__nout_, __s, __chunk_size);
  405. __nout_ += __chunk_size;
  406. __s += __chunk_size;
  407. __i += __chunk_size;
  408. }
  409. }
  410. return __i;
  411. }
  412. template <class _CharT, class _Traits>
  413. typename basic_streambuf<_CharT, _Traits>::int_type
  414. basic_streambuf<_CharT, _Traits>::overflow(int_type)
  415. {
  416. return traits_type::eof();
  417. }
  418. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<char>;
  419. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  420. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<wchar_t>;
  421. #endif
  422. _LIBCPP_END_NAMESPACE_STD
  423. _LIBCPP_POP_MACROS
  424. #endif // _LIBCPP_STREAMBUF