streambuf 14 KB

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