streambuf 14 KB

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