streambuf 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. // -*- C++ -*-
  2. //===------------------------- streambuf ----------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_STEAMBUF
  11. #define _LIBCPP_STEAMBUF
  12. /*
  13. streambuf synopsis
  14. namespace std
  15. {
  16. template <class charT, class traits = char_traits<charT> >
  17. class basic_streambuf
  18. {
  19. public:
  20. // types:
  21. typedef charT char_type;
  22. typedef traits traits_type;
  23. typedef typename traits_type::int_type int_type;
  24. typedef typename traits_type::pos_type pos_type;
  25. typedef typename traits_type::off_type off_type;
  26. virtual ~basic_streambuf();
  27. // 27.6.2.2.1 locales:
  28. locale pubimbue(const locale& loc);
  29. locale getloc() const;
  30. // 27.6.2.2.2 buffer and positioning:
  31. basic_streambuf* pubsetbuf(char_type* s, streamsize n);
  32. pos_type pubseekoff(off_type off, ios_base::seekdir way,
  33. ios_base::openmode which = ios_base::in | ios_base::out);
  34. pos_type pubseekpos(pos_type sp,
  35. ios_base::openmode which = ios_base::in | ios_base::out);
  36. int pubsync();
  37. // Get and put areas:
  38. // 27.6.2.2.3 Get area:
  39. streamsize in_avail();
  40. int_type snextc();
  41. int_type sbumpc();
  42. int_type sgetc();
  43. streamsize sgetn(char_type* s, streamsize n);
  44. // 27.6.2.2.4 Putback:
  45. int_type sputbackc(char_type c);
  46. int_type sungetc();
  47. // 27.6.2.2.5 Put area:
  48. int_type sputc(char_type c);
  49. streamsize sputn(const char_type* s, streamsize n);
  50. protected:
  51. basic_streambuf();
  52. basic_streambuf(const basic_streambuf& rhs);
  53. basic_streambuf& operator=(const basic_streambuf& rhs);
  54. void swap(basic_streambuf& rhs);
  55. // 27.6.2.3.2 Get area:
  56. char_type* eback() const;
  57. char_type* gptr() const;
  58. char_type* egptr() const;
  59. void gbump(int n);
  60. void setg(char_type* gbeg, char_type* gnext, char_type* gend);
  61. // 27.6.2.3.3 Put area:
  62. char_type* pbase() const;
  63. char_type* pptr() const;
  64. char_type* epptr() const;
  65. void pbump(int n);
  66. void setp(char_type* pbeg, char_type* pend);
  67. // 27.6.2.4 virtual functions:
  68. // 27.6.2.4.1 Locales:
  69. virtual void imbue(const locale& loc);
  70. // 27.6.2.4.2 Buffer management and positioning:
  71. virtual basic_streambuf* setbuf(char_type* s, streamsize n);
  72. virtual pos_type seekoff(off_type off, ios_base::seekdir way,
  73. ios_base::openmode which = ios_base::in | ios_base::out);
  74. virtual pos_type seekpos(pos_type sp,
  75. ios_base::openmode which = ios_base::in | ios_base::out);
  76. virtual int sync();
  77. // 27.6.2.4.3 Get area:
  78. virtual streamsize showmanyc();
  79. virtual streamsize xsgetn(char_type* s, streamsize n);
  80. virtual int_type underflow();
  81. virtual int_type uflow();
  82. // 27.6.2.4.4 Putback:
  83. virtual int_type pbackfail(int_type c = traits_type::eof());
  84. // 27.6.2.4.5 Put area:
  85. virtual streamsize xsputn(const char_type* s, streamsize n);
  86. virtual int_type overflow (int_type c = traits_type::eof());
  87. };
  88. } // std
  89. */
  90. // Not supported in SGX.
  91. #include <__config>
  92. #if !defined(_LIBCPP_SGX_CONFIG)
  93. #include <iosfwd>
  94. #include <ios>
  95. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  96. #pragma GCC system_header
  97. #endif
  98. _LIBCPP_BEGIN_NAMESPACE_STD
  99. template <class _CharT, class _Traits>
  100. class _LIBCPP_TYPE_VIS_ONLY basic_streambuf
  101. {
  102. public:
  103. // types:
  104. typedef _CharT char_type;
  105. typedef _Traits traits_type;
  106. typedef typename traits_type::int_type int_type;
  107. typedef typename traits_type::pos_type pos_type;
  108. typedef typename traits_type::off_type off_type;
  109. virtual ~basic_streambuf();
  110. // 27.6.2.2.1 locales:
  111. locale pubimbue(const locale& __loc);
  112. locale getloc() const;
  113. // 27.6.2.2.2 buffer and positioning:
  114. basic_streambuf* pubsetbuf(char_type* __s, streamsize __n);
  115. pos_type pubseekoff(off_type __off, ios_base::seekdir __way,
  116. ios_base::openmode __which = ios_base::in | ios_base::out);
  117. pos_type pubseekpos(pos_type __sp,
  118. ios_base::openmode __which = ios_base::in | ios_base::out);
  119. int pubsync();
  120. // Get and put areas:
  121. // 27.6.2.2.3 Get area:
  122. streamsize in_avail();
  123. int_type snextc();
  124. int_type sbumpc();
  125. int_type sgetc();
  126. streamsize sgetn(char_type* __s, streamsize __n);
  127. // 27.6.2.2.4 Putback:
  128. int_type sputbackc(char_type __c);
  129. int_type sungetc();
  130. // 27.6.2.2.5 Put area:
  131. int_type sputc(char_type __c);
  132. streamsize sputn(const char_type* __s, streamsize __n);
  133. protected:
  134. basic_streambuf();
  135. basic_streambuf(const basic_streambuf& __rhs);
  136. basic_streambuf& operator=(const basic_streambuf& __rhs);
  137. void swap(basic_streambuf& __rhs);
  138. // 27.6.2.3.2 Get area:
  139. _LIBCPP_ALWAYS_INLINE char_type* eback() const {return __binp_;}
  140. _LIBCPP_ALWAYS_INLINE char_type* gptr() const {return __ninp_;}
  141. _LIBCPP_ALWAYS_INLINE char_type* egptr() const {return __einp_;}
  142. void gbump(int __n);
  143. void setg(char_type* __gbeg, char_type* __gnext, char_type* __gend);
  144. // 27.6.2.3.3 Put area:
  145. _LIBCPP_ALWAYS_INLINE char_type* pbase() const {return __bout_;}
  146. _LIBCPP_ALWAYS_INLINE char_type* pptr() const {return __nout_;}
  147. _LIBCPP_ALWAYS_INLINE char_type* epptr() const {return __eout_;}
  148. void pbump(int __n);
  149. void setp(char_type* __pbeg, char_type* __pend);
  150. // 27.6.2.4 virtual functions:
  151. // 27.6.2.4.1 Locales:
  152. virtual void imbue(const locale& __loc);
  153. // 27.6.2.4.2 Buffer management and positioning:
  154. virtual basic_streambuf* setbuf(char_type* __s, streamsize __n);
  155. virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
  156. ios_base::openmode __which = ios_base::in | ios_base::out);
  157. virtual pos_type seekpos(pos_type __sp,
  158. ios_base::openmode __which = ios_base::in | ios_base::out);
  159. virtual int sync();
  160. // 27.6.2.4.3 Get area:
  161. virtual streamsize showmanyc();
  162. virtual streamsize xsgetn(char_type* __s, streamsize __n);
  163. virtual int_type underflow();
  164. virtual int_type uflow();
  165. // 27.6.2.4.4 Putback:
  166. virtual int_type pbackfail(int_type __c = traits_type::eof());
  167. // 27.6.2.4.5 Put area:
  168. virtual streamsize xsputn(const char_type* __s, streamsize __n);
  169. virtual int_type overflow(int_type __c = traits_type::eof());
  170. private:
  171. locale __loc_;
  172. char_type* __binp_;
  173. char_type* __ninp_;
  174. char_type* __einp_;
  175. char_type* __bout_;
  176. char_type* __nout_;
  177. char_type* __eout_;
  178. };
  179. template <class _CharT, class _Traits>
  180. basic_streambuf<_CharT, _Traits>::~basic_streambuf()
  181. {
  182. }
  183. template <class _CharT, class _Traits>
  184. inline _LIBCPP_INLINE_VISIBILITY
  185. locale
  186. basic_streambuf<_CharT, _Traits>::pubimbue(const locale& __loc)
  187. {
  188. imbue(__loc);
  189. locale __r = __loc_;
  190. __loc_ = __loc;
  191. return __r;
  192. }
  193. template <class _CharT, class _Traits>
  194. inline _LIBCPP_INLINE_VISIBILITY
  195. locale
  196. basic_streambuf<_CharT, _Traits>::getloc() const
  197. {
  198. return __loc_;
  199. }
  200. template <class _CharT, class _Traits>
  201. inline _LIBCPP_INLINE_VISIBILITY
  202. basic_streambuf<_CharT, _Traits>*
  203. basic_streambuf<_CharT, _Traits>::pubsetbuf(char_type* __s, streamsize __n)
  204. {
  205. return setbuf(__s, __n);
  206. }
  207. template <class _CharT, class _Traits>
  208. inline _LIBCPP_INLINE_VISIBILITY
  209. typename basic_streambuf<_CharT, _Traits>::pos_type
  210. basic_streambuf<_CharT, _Traits>::pubseekoff(off_type __off,
  211. ios_base::seekdir __way,
  212. ios_base::openmode __which)
  213. {
  214. return seekoff(__off, __way, __which);
  215. }
  216. template <class _CharT, class _Traits>
  217. inline _LIBCPP_INLINE_VISIBILITY
  218. typename basic_streambuf<_CharT, _Traits>::pos_type
  219. basic_streambuf<_CharT, _Traits>::pubseekpos(pos_type __sp,
  220. ios_base::openmode __which)
  221. {
  222. return seekpos(__sp, __which);
  223. }
  224. template <class _CharT, class _Traits>
  225. inline _LIBCPP_INLINE_VISIBILITY
  226. int
  227. basic_streambuf<_CharT, _Traits>::pubsync()
  228. {
  229. return sync();
  230. }
  231. template <class _CharT, class _Traits>
  232. inline _LIBCPP_INLINE_VISIBILITY
  233. streamsize
  234. basic_streambuf<_CharT, _Traits>::in_avail()
  235. {
  236. if (__ninp_ < __einp_)
  237. return static_cast<streamsize>(__einp_ - __ninp_);
  238. return showmanyc();
  239. }
  240. template <class _CharT, class _Traits>
  241. inline _LIBCPP_INLINE_VISIBILITY
  242. typename basic_streambuf<_CharT, _Traits>::int_type
  243. basic_streambuf<_CharT, _Traits>::snextc()
  244. {
  245. if (sbumpc() == traits_type::eof())
  246. return traits_type::eof();
  247. return sgetc();
  248. }
  249. template <class _CharT, class _Traits>
  250. inline _LIBCPP_INLINE_VISIBILITY
  251. typename basic_streambuf<_CharT, _Traits>::int_type
  252. basic_streambuf<_CharT, _Traits>::sbumpc()
  253. {
  254. if (__ninp_ == __einp_)
  255. return uflow();
  256. return traits_type::to_int_type(*__ninp_++);
  257. }
  258. template <class _CharT, class _Traits>
  259. inline _LIBCPP_INLINE_VISIBILITY
  260. typename basic_streambuf<_CharT, _Traits>::int_type
  261. basic_streambuf<_CharT, _Traits>::sgetc()
  262. {
  263. if (__ninp_ == __einp_)
  264. return underflow();
  265. return traits_type::to_int_type(*__ninp_);
  266. }
  267. template <class _CharT, class _Traits>
  268. inline _LIBCPP_INLINE_VISIBILITY
  269. streamsize
  270. basic_streambuf<_CharT, _Traits>::sgetn(char_type* __s, streamsize __n)
  271. {
  272. return xsgetn(__s, __n);
  273. }
  274. template <class _CharT, class _Traits>
  275. inline _LIBCPP_INLINE_VISIBILITY
  276. typename basic_streambuf<_CharT, _Traits>::int_type
  277. basic_streambuf<_CharT, _Traits>::sputbackc(char_type __c)
  278. {
  279. if (__binp_ == __ninp_ || !traits_type::eq(__c, __ninp_[-1]))
  280. return pbackfail(traits_type::to_int_type(__c));
  281. return traits_type::to_int_type(*--__ninp_);
  282. }
  283. template <class _CharT, class _Traits>
  284. inline _LIBCPP_INLINE_VISIBILITY
  285. typename basic_streambuf<_CharT, _Traits>::int_type
  286. basic_streambuf<_CharT, _Traits>::sungetc()
  287. {
  288. if (__binp_ == __ninp_)
  289. return pbackfail();
  290. return traits_type::to_int_type(*--__ninp_);
  291. }
  292. template <class _CharT, class _Traits>
  293. inline _LIBCPP_INLINE_VISIBILITY
  294. typename basic_streambuf<_CharT, _Traits>::int_type
  295. basic_streambuf<_CharT, _Traits>::sputc(char_type __c)
  296. {
  297. if (__nout_ == __eout_)
  298. return overflow(traits_type::to_int_type(__c));
  299. *__nout_++ = __c;
  300. return traits_type::to_int_type(__c);
  301. }
  302. template <class _CharT, class _Traits>
  303. inline _LIBCPP_INLINE_VISIBILITY
  304. streamsize
  305. basic_streambuf<_CharT, _Traits>::sputn(const char_type* __s, streamsize __n)
  306. {
  307. return xsputn(__s, __n);
  308. }
  309. template <class _CharT, class _Traits>
  310. basic_streambuf<_CharT, _Traits>::basic_streambuf()
  311. : __binp_(0),
  312. __ninp_(0),
  313. __einp_(0),
  314. __bout_(0),
  315. __nout_(0),
  316. __eout_(0)
  317. {
  318. }
  319. template <class _CharT, class _Traits>
  320. basic_streambuf<_CharT, _Traits>::basic_streambuf(const basic_streambuf& __sb)
  321. : __loc_(__sb.__loc_),
  322. __binp_(__sb.__binp_),
  323. __ninp_(__sb.__ninp_),
  324. __einp_(__sb.__einp_),
  325. __bout_(__sb.__bout_),
  326. __nout_(__sb.__nout_),
  327. __eout_(__sb.__eout_)
  328. {
  329. }
  330. template <class _CharT, class _Traits>
  331. basic_streambuf<_CharT, _Traits>&
  332. basic_streambuf<_CharT, _Traits>::operator=(const basic_streambuf& __sb)
  333. {
  334. __loc_ = __sb.__loc_;
  335. __binp_ = __sb.__binp_;
  336. __ninp_ = __sb.__ninp_;
  337. __einp_ = __sb.__einp_;
  338. __bout_ = __sb.__bout_;
  339. __nout_ = __sb.__nout_;
  340. __eout_ = __sb.__eout_;
  341. return *this;
  342. }
  343. template <class _CharT, class _Traits>
  344. void
  345. basic_streambuf<_CharT, _Traits>::swap(basic_streambuf& __sb)
  346. {
  347. _VSTD::swap(__loc_, __sb.__loc_);
  348. _VSTD::swap(__binp_, __sb.__binp_);
  349. _VSTD::swap(__ninp_, __sb.__ninp_);
  350. _VSTD::swap(__einp_, __sb.__einp_);
  351. _VSTD::swap(__bout_, __sb.__bout_);
  352. _VSTD::swap(__nout_, __sb.__nout_);
  353. _VSTD::swap(__eout_, __sb.__eout_);
  354. }
  355. template <class _CharT, class _Traits>
  356. inline _LIBCPP_INLINE_VISIBILITY
  357. void
  358. basic_streambuf<_CharT, _Traits>::gbump(int __n)
  359. {
  360. __ninp_ += __n;
  361. }
  362. template <class _CharT, class _Traits>
  363. inline _LIBCPP_INLINE_VISIBILITY
  364. void
  365. basic_streambuf<_CharT, _Traits>::setg(char_type* __gbeg, char_type* __gnext,
  366. char_type* __gend)
  367. {
  368. __binp_ = __gbeg;
  369. __ninp_ = __gnext;
  370. __einp_ = __gend;
  371. }
  372. template <class _CharT, class _Traits>
  373. inline _LIBCPP_INLINE_VISIBILITY
  374. void
  375. basic_streambuf<_CharT, _Traits>::pbump(int __n)
  376. {
  377. __nout_ += __n;
  378. }
  379. template <class _CharT, class _Traits>
  380. inline _LIBCPP_INLINE_VISIBILITY
  381. void
  382. basic_streambuf<_CharT, _Traits>::setp(char_type* __pbeg, char_type* __pend)
  383. {
  384. __bout_ = __nout_ = __pbeg;
  385. __eout_ = __pend;
  386. }
  387. template <class _CharT, class _Traits>
  388. void
  389. basic_streambuf<_CharT, _Traits>::imbue(const locale&)
  390. {
  391. }
  392. template <class _CharT, class _Traits>
  393. basic_streambuf<_CharT, _Traits>*
  394. basic_streambuf<_CharT, _Traits>::setbuf(char_type*, streamsize)
  395. {
  396. return this;
  397. }
  398. template <class _CharT, class _Traits>
  399. typename basic_streambuf<_CharT, _Traits>::pos_type
  400. basic_streambuf<_CharT, _Traits>::seekoff(off_type, ios_base::seekdir,
  401. ios_base::openmode)
  402. {
  403. return pos_type(off_type(-1));
  404. }
  405. template <class _CharT, class _Traits>
  406. typename basic_streambuf<_CharT, _Traits>::pos_type
  407. basic_streambuf<_CharT, _Traits>::seekpos(pos_type, ios_base::openmode)
  408. {
  409. return pos_type(off_type(-1));
  410. }
  411. template <class _CharT, class _Traits>
  412. int
  413. basic_streambuf<_CharT, _Traits>::sync()
  414. {
  415. return 0;
  416. }
  417. template <class _CharT, class _Traits>
  418. streamsize
  419. basic_streambuf<_CharT, _Traits>::showmanyc()
  420. {
  421. return 0;
  422. }
  423. template <class _CharT, class _Traits>
  424. streamsize
  425. basic_streambuf<_CharT, _Traits>::xsgetn(char_type* __s, streamsize __n)
  426. {
  427. const int_type __eof = traits_type::eof();
  428. int_type __c;
  429. streamsize __i = 0;
  430. while(__i < __n)
  431. {
  432. if (__ninp_ < __einp_)
  433. {
  434. const streamsize __len = _VSTD::min(__einp_ - __ninp_, __n - __i);
  435. traits_type::copy(__s, __ninp_, __len);
  436. __s += __len;
  437. __i += __len;
  438. this->gbump(__len);
  439. }
  440. else if ((__c = uflow()) != __eof)
  441. {
  442. *__s = traits_type::to_char_type(__c);
  443. ++__s;
  444. ++__i;
  445. }
  446. else
  447. break;
  448. }
  449. return __i;
  450. }
  451. template <class _CharT, class _Traits>
  452. typename basic_streambuf<_CharT, _Traits>::int_type
  453. basic_streambuf<_CharT, _Traits>::underflow()
  454. {
  455. return traits_type::eof();
  456. }
  457. template <class _CharT, class _Traits>
  458. typename basic_streambuf<_CharT, _Traits>::int_type
  459. basic_streambuf<_CharT, _Traits>::uflow()
  460. {
  461. if (underflow() == traits_type::eof())
  462. return traits_type::eof();
  463. return traits_type::to_int_type(*__ninp_++);
  464. }
  465. template <class _CharT, class _Traits>
  466. typename basic_streambuf<_CharT, _Traits>::int_type
  467. basic_streambuf<_CharT, _Traits>::pbackfail(int_type)
  468. {
  469. return traits_type::eof();
  470. }
  471. template <class _CharT, class _Traits>
  472. streamsize
  473. basic_streambuf<_CharT, _Traits>::xsputn(const char_type* __s, streamsize __n)
  474. {
  475. streamsize __i = 0;
  476. int_type __eof = traits_type::eof();
  477. while( __i < __n)
  478. {
  479. if (__nout_ >= __eout_)
  480. {
  481. if (overflow(traits_type::to_int_type(*__s)) == __eof)
  482. break;
  483. ++__s;
  484. ++__i;
  485. }
  486. else
  487. {
  488. streamsize __chunk_size = _VSTD::min(__eout_ - __nout_, __n - __i);
  489. traits_type::copy(__nout_, __s, __chunk_size);
  490. __nout_ += __chunk_size;
  491. __s += __chunk_size;
  492. __i += __chunk_size;
  493. }
  494. }
  495. return __i;
  496. }
  497. template <class _CharT, class _Traits>
  498. typename basic_streambuf<_CharT, _Traits>::int_type
  499. basic_streambuf<_CharT, _Traits>::overflow(int_type)
  500. {
  501. return traits_type::eof();
  502. }
  503. _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS basic_streambuf<char>)
  504. _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS basic_streambuf<wchar_t>)
  505. _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS basic_ios<char>)
  506. _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS basic_ios<wchar_t>)
  507. _LIBCPP_END_NAMESPACE_STD
  508. #endif // !defined(_LIBCPP_SGX_CONFIG)
  509. #endif // _LIBCPP_STEAMBUF