streambuf 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 <__fwd/streambuf.h>
  91. #include <__type_traits/is_same.h>
  92. #include <climits>
  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. 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 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 locale getloc() const { return __loc_; }
  122. // 27.6.2.2.2 buffer and positioning:
  123. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_streambuf* pubsetbuf(char_type* __s, streamsize __n) {
  124. return setbuf(__s, __n);
  125. }
  126. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type
  127. pubseekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which = ios_base::in | ios_base::out) {
  128. return seekoff(__off, __way, __which);
  129. }
  130. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type
  131. pubseekpos(pos_type __sp, ios_base::openmode __which = ios_base::in | ios_base::out) {
  132. return seekpos(__sp, __which);
  133. }
  134. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 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 streamsize in_avail() {
  138. if (__ninp_ < __einp_)
  139. return static_cast<streamsize>(__einp_ - __ninp_);
  140. return showmanyc();
  141. }
  142. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type snextc() {
  143. if (sbumpc() == traits_type::eof())
  144. return traits_type::eof();
  145. return sgetc();
  146. }
  147. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sbumpc() {
  148. if (__ninp_ == __einp_)
  149. return uflow();
  150. return traits_type::to_int_type(*__ninp_++);
  151. }
  152. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sgetc() {
  153. if (__ninp_ == __einp_)
  154. return underflow();
  155. return traits_type::to_int_type(*__ninp_);
  156. }
  157. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize sgetn(char_type* __s, streamsize __n) { return xsgetn(__s, __n); }
  158. // 27.6.2.2.4 Putback:
  159. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sputbackc(char_type __c) {
  160. if (__binp_ == __ninp_ || !traits_type::eq(__c, __ninp_[-1]))
  161. return pbackfail(traits_type::to_int_type(__c));
  162. return traits_type::to_int_type(*--__ninp_);
  163. }
  164. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sungetc() {
  165. if (__binp_ == __ninp_)
  166. return pbackfail();
  167. return traits_type::to_int_type(*--__ninp_);
  168. }
  169. // 27.6.2.2.5 Put area:
  170. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sputc(char_type __c) {
  171. if (__nout_ == __eout_)
  172. return overflow(traits_type::to_int_type(__c));
  173. *__nout_++ = __c;
  174. return traits_type::to_int_type(__c);
  175. }
  176. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize sputn(const char_type* __s, streamsize __n) {
  177. return xsputn(__s, __n);
  178. }
  179. protected:
  180. basic_streambuf();
  181. basic_streambuf(const basic_streambuf& __rhs);
  182. basic_streambuf& operator=(const basic_streambuf& __rhs);
  183. void swap(basic_streambuf& __rhs);
  184. // 27.6.2.3.2 Get area:
  185. _LIBCPP_HIDE_FROM_ABI char_type* eback() const { return __binp_; }
  186. _LIBCPP_HIDE_FROM_ABI char_type* gptr() const { return __ninp_; }
  187. _LIBCPP_HIDE_FROM_ABI char_type* egptr() const { return __einp_; }
  188. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void gbump(int __n) { __ninp_ += __n; }
  189. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend) {
  190. __binp_ = __gbeg;
  191. __ninp_ = __gnext;
  192. __einp_ = __gend;
  193. }
  194. // 27.6.2.3.3 Put area:
  195. _LIBCPP_HIDE_FROM_ABI char_type* pbase() const { return __bout_; }
  196. _LIBCPP_HIDE_FROM_ABI char_type* pptr() const { return __nout_; }
  197. _LIBCPP_HIDE_FROM_ABI char_type* epptr() const { return __eout_; }
  198. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void pbump(int __n) { __nout_ += __n; }
  199. _LIBCPP_HIDE_FROM_ABI void __pbump(streamsize __n) { __nout_ += __n; }
  200. inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void setp(char_type* __pbeg, char_type* __pend) {
  201. __bout_ = __nout_ = __pbeg;
  202. __eout_ = __pend;
  203. }
  204. // 27.6.2.4 virtual functions:
  205. // 27.6.2.4.1 Locales:
  206. virtual void imbue(const locale& __loc);
  207. // 27.6.2.4.2 Buffer management and positioning:
  208. virtual basic_streambuf* setbuf(char_type* __s, streamsize __n);
  209. virtual pos_type
  210. seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __which = ios_base::in | ios_base::out);
  211. virtual pos_type seekpos(pos_type __sp, ios_base::openmode __which = ios_base::in | ios_base::out);
  212. virtual int sync();
  213. // 27.6.2.4.3 Get area:
  214. virtual streamsize showmanyc();
  215. virtual streamsize xsgetn(char_type* __s, streamsize __n);
  216. virtual int_type underflow();
  217. virtual int_type uflow();
  218. // 27.6.2.4.4 Putback:
  219. virtual int_type pbackfail(int_type __c = traits_type::eof());
  220. // 27.6.2.4.5 Put area:
  221. virtual streamsize xsputn(const char_type* __s, streamsize __n);
  222. virtual int_type overflow(int_type __c = traits_type::eof());
  223. private:
  224. locale __loc_;
  225. char_type* __binp_;
  226. char_type* __ninp_;
  227. char_type* __einp_;
  228. char_type* __bout_;
  229. char_type* __nout_;
  230. char_type* __eout_;
  231. };
  232. template <class _CharT, class _Traits>
  233. basic_streambuf<_CharT, _Traits>::~basic_streambuf() {}
  234. template <class _CharT, class _Traits>
  235. basic_streambuf<_CharT, _Traits>::basic_streambuf()
  236. : __binp_(nullptr), __ninp_(nullptr), __einp_(nullptr), __bout_(nullptr), __nout_(nullptr), __eout_(nullptr) {}
  237. template <class _CharT, class _Traits>
  238. basic_streambuf<_CharT, _Traits>::basic_streambuf(const basic_streambuf& __sb)
  239. : __loc_(__sb.__loc_),
  240. __binp_(__sb.__binp_),
  241. __ninp_(__sb.__ninp_),
  242. __einp_(__sb.__einp_),
  243. __bout_(__sb.__bout_),
  244. __nout_(__sb.__nout_),
  245. __eout_(__sb.__eout_) {}
  246. template <class _CharT, class _Traits>
  247. basic_streambuf<_CharT, _Traits>& basic_streambuf<_CharT, _Traits>::operator=(const basic_streambuf& __sb) {
  248. __loc_ = __sb.__loc_;
  249. __binp_ = __sb.__binp_;
  250. __ninp_ = __sb.__ninp_;
  251. __einp_ = __sb.__einp_;
  252. __bout_ = __sb.__bout_;
  253. __nout_ = __sb.__nout_;
  254. __eout_ = __sb.__eout_;
  255. return *this;
  256. }
  257. template <class _CharT, class _Traits>
  258. void basic_streambuf<_CharT, _Traits>::swap(basic_streambuf& __sb) {
  259. std::swap(__loc_, __sb.__loc_);
  260. std::swap(__binp_, __sb.__binp_);
  261. std::swap(__ninp_, __sb.__ninp_);
  262. std::swap(__einp_, __sb.__einp_);
  263. std::swap(__bout_, __sb.__bout_);
  264. std::swap(__nout_, __sb.__nout_);
  265. std::swap(__eout_, __sb.__eout_);
  266. }
  267. template <class _CharT, class _Traits>
  268. void basic_streambuf<_CharT, _Traits>::imbue(const locale&) {}
  269. template <class _CharT, class _Traits>
  270. basic_streambuf<_CharT, _Traits>* basic_streambuf<_CharT, _Traits>::setbuf(char_type*, streamsize) {
  271. return this;
  272. }
  273. template <class _CharT, class _Traits>
  274. typename basic_streambuf<_CharT, _Traits>::pos_type
  275. basic_streambuf<_CharT, _Traits>::seekoff(off_type, ios_base::seekdir, ios_base::openmode) {
  276. return pos_type(off_type(-1));
  277. }
  278. template <class _CharT, class _Traits>
  279. typename basic_streambuf<_CharT, _Traits>::pos_type
  280. basic_streambuf<_CharT, _Traits>::seekpos(pos_type, ios_base::openmode) {
  281. return pos_type(off_type(-1));
  282. }
  283. template <class _CharT, class _Traits>
  284. int basic_streambuf<_CharT, _Traits>::sync() {
  285. return 0;
  286. }
  287. template <class _CharT, class _Traits>
  288. streamsize basic_streambuf<_CharT, _Traits>::showmanyc() {
  289. return 0;
  290. }
  291. template <class _CharT, class _Traits>
  292. streamsize basic_streambuf<_CharT, _Traits>::xsgetn(char_type* __s, streamsize __n) {
  293. const int_type __eof = traits_type::eof();
  294. int_type __c;
  295. streamsize __i = 0;
  296. while (__i < __n) {
  297. if (__ninp_ < __einp_) {
  298. const streamsize __len = std::min(static_cast<streamsize>(INT_MAX), std::min(__einp_ - __ninp_, __n - __i));
  299. traits_type::copy(__s, __ninp_, __len);
  300. __s += __len;
  301. __i += __len;
  302. this->gbump(__len);
  303. } else if ((__c = uflow()) != __eof) {
  304. *__s = traits_type::to_char_type(__c);
  305. ++__s;
  306. ++__i;
  307. } else
  308. break;
  309. }
  310. return __i;
  311. }
  312. template <class _CharT, class _Traits>
  313. typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::underflow() {
  314. return traits_type::eof();
  315. }
  316. template <class _CharT, class _Traits>
  317. typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::uflow() {
  318. if (underflow() == traits_type::eof())
  319. return traits_type::eof();
  320. return traits_type::to_int_type(*__ninp_++);
  321. }
  322. template <class _CharT, class _Traits>
  323. typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::pbackfail(int_type) {
  324. return traits_type::eof();
  325. }
  326. template <class _CharT, class _Traits>
  327. streamsize basic_streambuf<_CharT, _Traits>::xsputn(const char_type* __s, streamsize __n) {
  328. streamsize __i = 0;
  329. int_type __eof = traits_type::eof();
  330. while (__i < __n) {
  331. if (__nout_ >= __eout_) {
  332. if (overflow(traits_type::to_int_type(*__s)) == __eof)
  333. break;
  334. ++__s;
  335. ++__i;
  336. } else {
  337. streamsize __chunk_size = std::min(__eout_ - __nout_, __n - __i);
  338. traits_type::copy(__nout_, __s, __chunk_size);
  339. __nout_ += __chunk_size;
  340. __s += __chunk_size;
  341. __i += __chunk_size;
  342. }
  343. }
  344. return __i;
  345. }
  346. template <class _CharT, class _Traits>
  347. typename basic_streambuf<_CharT, _Traits>::int_type basic_streambuf<_CharT, _Traits>::overflow(int_type) {
  348. return traits_type::eof();
  349. }
  350. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<char>;
  351. #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
  352. extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_streambuf<wchar_t>;
  353. #endif
  354. _LIBCPP_END_NAMESPACE_STD
  355. _LIBCPP_POP_MACROS
  356. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  357. # include <cstdint>
  358. #endif
  359. #endif // _LIBCPP_STREAMBUF