streambuf 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 <cstdint>
  92. #include <ios>
  93. #include <iosfwd>
  94. #include <version>
  95. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  96. # pragma GCC system_header
  97. #endif
  98. _LIBCPP_PUSH_MACROS
  99. #include <__undef_macros>
  100. _LIBCPP_BEGIN_NAMESPACE_STD
  101. template <class _CharT, class _Traits>
  102. class _LIBCPP_TEMPLATE_VIS basic_streambuf
  103. {
  104. public:
  105. // types:
  106. typedef _CharT char_type;
  107. typedef _Traits traits_type;
  108. typedef typename traits_type::int_type int_type;
  109. typedef typename traits_type::pos_type pos_type;
  110. typedef typename traits_type::off_type off_type;
  111. static_assert((is_same<_CharT, typename traits_type::char_type>::value),
  112. "traits_type::char_type must be the same type as CharT");
  113. virtual ~basic_streambuf();
  114. // 27.6.2.2.1 locales:
  115. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  116. locale pubimbue(const locale& __loc) {
  117. imbue(__loc);
  118. locale __r = __loc_;
  119. __loc_ = __loc;
  120. return __r;
  121. }
  122. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  123. locale getloc() const { return __loc_; }
  124. // 27.6.2.2.2 buffer and positioning:
  125. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  126. basic_streambuf* pubsetbuf(char_type* __s, streamsize __n)
  127. { return setbuf(__s, __n); }
  128. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  129. pos_type pubseekoff(off_type __off, ios_base::seekdir __way,
  130. ios_base::openmode __which = ios_base::in | ios_base::out)
  131. { return seekoff(__off, __way, __which); }
  132. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  133. pos_type pubseekpos(pos_type __sp,
  134. ios_base::openmode __which = ios_base::in | ios_base::out)
  135. { return seekpos(__sp, __which); }
  136. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  137. int pubsync() { return sync(); }
  138. // Get and put areas:
  139. // 27.6.2.2.3 Get area:
  140. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  141. streamsize in_avail() {
  142. if (__ninp_ < __einp_)
  143. return static_cast<streamsize>(__einp_ - __ninp_);
  144. return showmanyc();
  145. }
  146. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  147. int_type snextc() {
  148. if (sbumpc() == traits_type::eof())
  149. return traits_type::eof();
  150. return sgetc();
  151. }
  152. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  153. int_type sbumpc() {
  154. if (__ninp_ == __einp_)
  155. return uflow();
  156. return traits_type::to_int_type(*__ninp_++);
  157. }
  158. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  159. int_type sgetc() {
  160. if (__ninp_ == __einp_)
  161. return underflow();
  162. return traits_type::to_int_type(*__ninp_);
  163. }
  164. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  165. streamsize sgetn(char_type* __s, streamsize __n)
  166. { return xsgetn(__s, __n); }
  167. // 27.6.2.2.4 Putback:
  168. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  169. int_type sputbackc(char_type __c) {
  170. if (__binp_ == __ninp_ || !traits_type::eq(__c, __ninp_[-1]))
  171. return pbackfail(traits_type::to_int_type(__c));
  172. return traits_type::to_int_type(*--__ninp_);
  173. }
  174. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  175. int_type sungetc() {
  176. if (__binp_ == __ninp_)
  177. return pbackfail();
  178. return traits_type::to_int_type(*--__ninp_);
  179. }
  180. // 27.6.2.2.5 Put area:
  181. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  182. int_type sputc(char_type __c) {
  183. if (__nout_ == __eout_)
  184. return overflow(traits_type::to_int_type(__c));
  185. *__nout_++ = __c;
  186. return traits_type::to_int_type(__c);
  187. }
  188. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  189. streamsize sputn(const char_type* __s, streamsize __n)
  190. { return xsputn(__s, __n); }
  191. protected:
  192. basic_streambuf();
  193. basic_streambuf(const basic_streambuf& __rhs);
  194. basic_streambuf& operator=(const basic_streambuf& __rhs);
  195. void swap(basic_streambuf& __rhs);
  196. // 27.6.2.3.2 Get area:
  197. _LIBCPP_INLINE_VISIBILITY char_type* eback() const {return __binp_;}
  198. _LIBCPP_INLINE_VISIBILITY char_type* gptr() const {return __ninp_;}
  199. _LIBCPP_INLINE_VISIBILITY char_type* egptr() const {return __einp_;}
  200. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  201. void gbump(int __n) { __ninp_ += __n; }
  202. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  203. void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend) {
  204. __binp_ = __gbeg;
  205. __ninp_ = __gnext;
  206. __einp_ = __gend;
  207. }
  208. // 27.6.2.3.3 Put area:
  209. _LIBCPP_INLINE_VISIBILITY char_type* pbase() const {return __bout_;}
  210. _LIBCPP_INLINE_VISIBILITY char_type* pptr() const {return __nout_;}
  211. _LIBCPP_INLINE_VISIBILITY char_type* epptr() const {return __eout_;}
  212. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  213. void pbump(int __n) { __nout_ += __n; }
  214. _LIBCPP_INLINE_VISIBILITY
  215. void __pbump(streamsize __n) { __nout_ += __n; }
  216. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
  217. void setp(char_type* __pbeg, char_type* __pend) {
  218. __bout_ = __nout_ = __pbeg;
  219. __eout_ = __pend;
  220. }
  221. // 27.6.2.4 virtual functions:
  222. // 27.6.2.4.1 Locales:
  223. virtual void imbue(const locale& __loc);
  224. // 27.6.2.4.2 Buffer management and positioning:
  225. virtual basic_streambuf* setbuf(char_type* __s, streamsize __n);
  226. virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
  227. ios_base::openmode __which = ios_base::in | ios_base::out);
  228. virtual pos_type seekpos(pos_type __sp,
  229. ios_base::openmode __which = ios_base::in | ios_base::out);
  230. virtual int sync();
  231. // 27.6.2.4.3 Get area:
  232. virtual streamsize showmanyc();
  233. virtual streamsize xsgetn(char_type* __s, streamsize __n);
  234. virtual int_type underflow();
  235. virtual int_type uflow();
  236. // 27.6.2.4.4 Putback:
  237. virtual int_type pbackfail(int_type __c = traits_type::eof());
  238. // 27.6.2.4.5 Put area:
  239. virtual streamsize xsputn(const char_type* __s, streamsize __n);
  240. virtual int_type overflow(int_type __c = traits_type::eof());
  241. private:
  242. locale __loc_;
  243. char_type* __binp_;
  244. char_type* __ninp_;
  245. char_type* __einp_;
  246. char_type* __bout_;
  247. char_type* __nout_;
  248. char_type* __eout_;
  249. };
  250. template <class _CharT, class _Traits>
  251. basic_streambuf<_CharT, _Traits>::~basic_streambuf()
  252. {
  253. }
  254. template <class _CharT, class _Traits>
  255. basic_streambuf<_CharT, _Traits>::basic_streambuf()
  256. : __binp_(nullptr),
  257. __ninp_(nullptr),
  258. __einp_(nullptr),
  259. __bout_(nullptr),
  260. __nout_(nullptr),
  261. __eout_(nullptr)
  262. {
  263. }
  264. template <class _CharT, class _Traits>
  265. basic_streambuf<_CharT, _Traits>::basic_streambuf(const basic_streambuf& __sb)
  266. : __loc_(__sb.__loc_),
  267. __binp_(__sb.__binp_),
  268. __ninp_(__sb.__ninp_),
  269. __einp_(__sb.__einp_),
  270. __bout_(__sb.__bout_),
  271. __nout_(__sb.__nout_),
  272. __eout_(__sb.__eout_)
  273. {
  274. }
  275. template <class _CharT, class _Traits>
  276. basic_streambuf<_CharT, _Traits>&
  277. basic_streambuf<_CharT, _Traits>::operator=(const basic_streambuf& __sb)
  278. {
  279. __loc_ = __sb.__loc_;
  280. __binp_ = __sb.__binp_;
  281. __ninp_ = __sb.__ninp_;
  282. __einp_ = __sb.__einp_;
  283. __bout_ = __sb.__bout_;
  284. __nout_ = __sb.__nout_;
  285. __eout_ = __sb.__eout_;
  286. return *this;
  287. }
  288. template <class _CharT, class _Traits>
  289. void
  290. basic_streambuf<_CharT, _Traits>::swap(basic_streambuf& __sb)
  291. {
  292. _VSTD::swap(__loc_, __sb.__loc_);
  293. _VSTD::swap(__binp_, __sb.__binp_);
  294. _VSTD::swap(__ninp_, __sb.__ninp_);
  295. _VSTD::swap(__einp_, __sb.__einp_);
  296. _VSTD::swap(__bout_, __sb.__bout_);
  297. _VSTD::swap(__nout_, __sb.__nout_);
  298. _VSTD::swap(__eout_, __sb.__eout_);
  299. }
  300. template <class _CharT, class _Traits>
  301. void
  302. basic_streambuf<_CharT, _Traits>::imbue(const locale&)
  303. {
  304. }
  305. template <class _CharT, class _Traits>
  306. basic_streambuf<_CharT, _Traits>*
  307. basic_streambuf<_CharT, _Traits>::setbuf(char_type*, streamsize)
  308. {
  309. return this;
  310. }
  311. template <class _CharT, class _Traits>
  312. typename basic_streambuf<_CharT, _Traits>::pos_type
  313. basic_streambuf<_CharT, _Traits>::seekoff(off_type, ios_base::seekdir,
  314. ios_base::openmode)
  315. {
  316. return pos_type(off_type(-1));
  317. }
  318. template <class _CharT, class _Traits>
  319. typename basic_streambuf<_CharT, _Traits>::pos_type
  320. basic_streambuf<_CharT, _Traits>::seekpos(pos_type, ios_base::openmode)
  321. {
  322. return pos_type(off_type(-1));
  323. }
  324. template <class _CharT, class _Traits>
  325. int
  326. basic_streambuf<_CharT, _Traits>::sync()
  327. {
  328. return 0;
  329. }
  330. template <class _CharT, class _Traits>
  331. streamsize
  332. basic_streambuf<_CharT, _Traits>::showmanyc()
  333. {
  334. return 0;
  335. }
  336. template <class _CharT, class _Traits>
  337. streamsize
  338. basic_streambuf<_CharT, _Traits>::xsgetn(char_type* __s, streamsize __n)
  339. {
  340. const int_type __eof = traits_type::eof();
  341. int_type __c;
  342. streamsize __i = 0;
  343. while(__i < __n)
  344. {
  345. if (__ninp_ < __einp_)
  346. {
  347. const streamsize __len = _VSTD::min(static_cast<streamsize>(INT_MAX),
  348. _VSTD::min(__einp_ - __ninp_, __n - __i));
  349. traits_type::copy(__s, __ninp_, __len);
  350. __s += __len;
  351. __i += __len;
  352. this->gbump(__len);
  353. }
  354. else if ((__c = uflow()) != __eof)
  355. {
  356. *__s = traits_type::to_char_type(__c);
  357. ++__s;
  358. ++__i;
  359. }
  360. else
  361. break;
  362. }
  363. return __i;
  364. }
  365. template <class _CharT, class _Traits>
  366. typename basic_streambuf<_CharT, _Traits>::int_type
  367. basic_streambuf<_CharT, _Traits>::underflow()
  368. {
  369. return traits_type::eof();
  370. }
  371. template <class _CharT, class _Traits>
  372. typename basic_streambuf<_CharT, _Traits>::int_type
  373. basic_streambuf<_CharT, _Traits>::uflow()
  374. {
  375. if (underflow() == traits_type::eof())
  376. return traits_type::eof();
  377. return traits_type::to_int_type(*__ninp_++);
  378. }
  379. template <class _CharT, class _Traits>
  380. typename basic_streambuf<_CharT, _Traits>::int_type
  381. basic_streambuf<_CharT, _Traits>::pbackfail(int_type)
  382. {
  383. return traits_type::eof();
  384. }
  385. template <class _CharT, class _Traits>
  386. streamsize
  387. basic_streambuf<_CharT, _Traits>::xsputn(const char_type* __s, streamsize __n)
  388. {
  389. streamsize __i = 0;
  390. int_type __eof = traits_type::eof();
  391. while( __i < __n)
  392. {
  393. if (__nout_ >= __eout_)
  394. {
  395. if (overflow(traits_type::to_int_type(*__s)) == __eof)
  396. break;
  397. ++__s;
  398. ++__i;
  399. }
  400. else
  401. {
  402. streamsize __chunk_size = _VSTD::min(__eout_ - __nout_, __n - __i);
  403. traits_type::copy(__nout_, __s, __chunk_size);
  404. __nout_ += __chunk_size;
  405. __s += __chunk_size;
  406. __i += __chunk_size;
  407. }
  408. }
  409. return __i;
  410. }
  411. template <class _CharT, class _Traits>
  412. typename basic_streambuf<_CharT, _Traits>::int_type
  413. basic_streambuf<_CharT, _Traits>::overflow(int_type)
  414. {
  415. return traits_type::eof();
  416. }
  417. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<char>;
  418. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ios<char>;
  419. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  420. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<wchar_t>;
  421. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ios<wchar_t>;
  422. #endif
  423. _LIBCPP_END_NAMESPACE_STD
  424. _LIBCPP_POP_MACROS
  425. #endif // _LIBCPP_STREAMBUF