streambuf 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. // Stream buffer classes -*- C++ -*-
  2. // Copyright (C) 1997-2015 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/streambuf
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 27.5 Stream buffers
  25. //
  26. #ifndef _GLIBXX_STREAMBUF
  27. #define _GLIBXX_STREAMBUF 1
  28. #pragma GCC system_header
  29. #include <bits/c++config.h>
  30. #include <iosfwd>
  31. #include <bits/localefwd.h>
  32. #include <bits/ios_base.h>
  33. #include <bits/cpp_type_traits.h>
  34. #include <ext/type_traits.h>
  35. namespace std _GLIBCXX_VISIBILITY(default)
  36. {
  37. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  38. template<typename _CharT, typename _Traits>
  39. streamsize
  40. __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>*,
  41. basic_streambuf<_CharT, _Traits>*, bool&);
  42. /**
  43. * @brief The actual work of input and output (interface).
  44. * @ingroup io
  45. *
  46. * @tparam _CharT Type of character stream.
  47. * @tparam _Traits Traits for character type, defaults to
  48. * char_traits<_CharT>.
  49. *
  50. * This is a base class. Derived stream buffers each control a
  51. * pair of character sequences: one for input, and one for output.
  52. *
  53. * Section [27.5.1] of the standard describes the requirements and
  54. * behavior of stream buffer classes. That section (three paragraphs)
  55. * is reproduced here, for simplicity and accuracy.
  56. *
  57. * -# Stream buffers can impose various constraints on the sequences
  58. * they control. Some constraints are:
  59. * - The controlled input sequence can be not readable.
  60. * - The controlled output sequence can be not writable.
  61. * - The controlled sequences can be associated with the contents of
  62. * other representations for character sequences, such as external
  63. * files.
  64. * - The controlled sequences can support operations @e directly to or
  65. * from associated sequences.
  66. * - The controlled sequences can impose limitations on how the
  67. * program can read characters from a sequence, write characters to
  68. * a sequence, put characters back into an input sequence, or alter
  69. * the stream position.
  70. * .
  71. * -# Each sequence is characterized by three pointers which, if non-null,
  72. * all point into the same @c charT array object. The array object
  73. * represents, at any moment, a (sub)sequence of characters from the
  74. * sequence. Operations performed on a sequence alter the values
  75. * stored in these pointers, perform reads and writes directly to or
  76. * from associated sequences, and alter <em>the stream position</em> and
  77. * conversion state as needed to maintain this subsequence relationship.
  78. * The three pointers are:
  79. * - the <em>beginning pointer</em>, or lowest element address in the
  80. * array (called @e xbeg here);
  81. * - the <em>next pointer</em>, or next element address that is a
  82. * current candidate for reading or writing (called @e xnext here);
  83. * - the <em>end pointer</em>, or first element address beyond the
  84. * end of the array (called @e xend here).
  85. * .
  86. * -# The following semantic constraints shall always apply for any set
  87. * of three pointers for a sequence, using the pointer names given
  88. * immediately above:
  89. * - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
  90. * also be non-null pointers into the same @c charT array, as
  91. * described above; otherwise, @e xbeg and @e xend shall also be null.
  92. * - If @e xnext is not a null pointer and @e xnext < @e xend for an
  93. * output sequence, then a <em>write position</em> is available.
  94. * In this case, @e *xnext shall be assignable as the next element
  95. * to write (to put, or to store a character value, into the sequence).
  96. * - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
  97. * input sequence, then a <em>putback position</em> is available.
  98. * In this case, @e xnext[-1] shall have a defined value and is the
  99. * next (preceding) element to store a character that is put back
  100. * into the input sequence.
  101. * - If @e xnext is not a null pointer and @e xnext< @e xend for an
  102. * input sequence, then a <em>read position</em> is available.
  103. * In this case, @e *xnext shall have a defined value and is the
  104. * next element to read (to get, or to obtain a character value,
  105. * from the sequence).
  106. */
  107. template<typename _CharT, typename _Traits>
  108. class basic_streambuf
  109. {
  110. public:
  111. //@{
  112. /**
  113. * These are standard types. They permit a standardized way of
  114. * referring to names of (or names dependent on) the template
  115. * parameters, which are specific to the implementation.
  116. */
  117. typedef _CharT char_type;
  118. typedef _Traits traits_type;
  119. typedef typename traits_type::int_type int_type;
  120. typedef typename traits_type::pos_type pos_type;
  121. typedef typename traits_type::off_type off_type;
  122. //@}
  123. //@{
  124. /// This is a non-standard type.
  125. typedef basic_streambuf<char_type, traits_type> __streambuf_type;
  126. //@}
  127. friend class basic_ios<char_type, traits_type>;
  128. friend class basic_istream<char_type, traits_type>;
  129. friend class basic_ostream<char_type, traits_type>;
  130. friend class istreambuf_iterator<char_type, traits_type>;
  131. friend class ostreambuf_iterator<char_type, traits_type>;
  132. friend streamsize
  133. __copy_streambufs_eof<>(basic_streambuf*, basic_streambuf*, bool&);
  134. template<bool _IsMove, typename _CharT2>
  135. friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
  136. _CharT2*>::__type
  137. __copy_move_a2(istreambuf_iterator<_CharT2>,
  138. istreambuf_iterator<_CharT2>, _CharT2*);
  139. template<typename _CharT2>
  140. friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
  141. istreambuf_iterator<_CharT2> >::__type
  142. find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
  143. const _CharT2&);
  144. template<typename _CharT2, typename _Traits2>
  145. friend basic_istream<_CharT2, _Traits2>&
  146. operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*);
  147. template<typename _CharT2, typename _Traits2, typename _Alloc>
  148. friend basic_istream<_CharT2, _Traits2>&
  149. operator>>(basic_istream<_CharT2, _Traits2>&,
  150. basic_string<_CharT2, _Traits2, _Alloc>&);
  151. template<typename _CharT2, typename _Traits2, typename _Alloc>
  152. friend basic_istream<_CharT2, _Traits2>&
  153. getline(basic_istream<_CharT2, _Traits2>&,
  154. basic_string<_CharT2, _Traits2, _Alloc>&, _CharT2);
  155. protected:
  156. /*
  157. * This is based on _IO_FILE, just reordered to be more consistent,
  158. * and is intended to be the most minimal abstraction for an
  159. * internal buffer.
  160. * - get == input == read
  161. * - put == output == write
  162. */
  163. char_type* _M_in_beg; ///< Start of get area.
  164. char_type* _M_in_cur; ///< Current read area.
  165. char_type* _M_in_end; ///< End of get area.
  166. char_type* _M_out_beg; ///< Start of put area.
  167. char_type* _M_out_cur; ///< Current put area.
  168. char_type* _M_out_end; ///< End of put area.
  169. /// Current locale setting.
  170. locale _M_buf_locale;
  171. public:
  172. /// Destructor deallocates no buffer space.
  173. virtual
  174. ~basic_streambuf()
  175. { }
  176. // [27.5.2.2.1] locales
  177. /**
  178. * @brief Entry point for imbue().
  179. * @param __loc The new locale.
  180. * @return The previous locale.
  181. *
  182. * Calls the derived imbue(__loc).
  183. */
  184. locale
  185. pubimbue(const locale& __loc)
  186. {
  187. locale __tmp(this->getloc());
  188. this->imbue(__loc);
  189. _M_buf_locale = __loc;
  190. return __tmp;
  191. }
  192. /**
  193. * @brief Locale access.
  194. * @return The current locale in effect.
  195. *
  196. * If pubimbue(loc) has been called, then the most recent @c loc
  197. * is returned. Otherwise the global locale in effect at the time
  198. * of construction is returned.
  199. */
  200. locale
  201. getloc() const
  202. { return _M_buf_locale; }
  203. // [27.5.2.2.2] buffer management and positioning
  204. //@{
  205. /**
  206. * @brief Entry points for derived buffer functions.
  207. *
  208. * The public versions of @c pubfoo dispatch to the protected
  209. * derived @c foo member functions, passing the arguments (if any)
  210. * and returning the result unchanged.
  211. */
  212. basic_streambuf*
  213. pubsetbuf(char_type* __s, streamsize __n)
  214. { return this->setbuf(__s, __n); }
  215. /**
  216. * @brief Alters the stream position.
  217. * @param __off Offset.
  218. * @param __way Value for ios_base::seekdir.
  219. * @param __mode Value for ios_base::openmode.
  220. *
  221. * Calls virtual seekoff function.
  222. */
  223. pos_type
  224. pubseekoff(off_type __off, ios_base::seekdir __way,
  225. ios_base::openmode __mode = ios_base::in | ios_base::out)
  226. { return this->seekoff(__off, __way, __mode); }
  227. /**
  228. * @brief Alters the stream position.
  229. * @param __sp Position
  230. * @param __mode Value for ios_base::openmode.
  231. *
  232. * Calls virtual seekpos function.
  233. */
  234. pos_type
  235. pubseekpos(pos_type __sp,
  236. ios_base::openmode __mode = ios_base::in | ios_base::out)
  237. { return this->seekpos(__sp, __mode); }
  238. /**
  239. * @brief Calls virtual sync function.
  240. */
  241. int
  242. pubsync() { return this->sync(); }
  243. //@}
  244. // [27.5.2.2.3] get area
  245. /**
  246. * @brief Looking ahead into the stream.
  247. * @return The number of characters available.
  248. *
  249. * If a read position is available, returns the number of characters
  250. * available for reading before the buffer must be refilled.
  251. * Otherwise returns the derived @c showmanyc().
  252. */
  253. streamsize
  254. in_avail()
  255. {
  256. const streamsize __ret = this->egptr() - this->gptr();
  257. return __ret ? __ret : this->showmanyc();
  258. }
  259. /**
  260. * @brief Getting the next character.
  261. * @return The next character, or eof.
  262. *
  263. * Calls @c sbumpc(), and if that function returns
  264. * @c traits::eof(), so does this function. Otherwise, @c sgetc().
  265. */
  266. int_type
  267. snextc()
  268. {
  269. int_type __ret = traits_type::eof();
  270. if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
  271. __ret), true))
  272. __ret = this->sgetc();
  273. return __ret;
  274. }
  275. /**
  276. * @brief Getting the next character.
  277. * @return The next character, or eof.
  278. *
  279. * If the input read position is available, returns that character
  280. * and increments the read pointer, otherwise calls and returns
  281. * @c uflow().
  282. */
  283. int_type
  284. sbumpc()
  285. {
  286. int_type __ret;
  287. if (__builtin_expect(this->gptr() < this->egptr(), true))
  288. {
  289. __ret = traits_type::to_int_type(*this->gptr());
  290. this->gbump(1);
  291. }
  292. else
  293. __ret = this->uflow();
  294. return __ret;
  295. }
  296. /**
  297. * @brief Getting the next character.
  298. * @return The next character, or eof.
  299. *
  300. * If the input read position is available, returns that character,
  301. * otherwise calls and returns @c underflow(). Does not move the
  302. * read position after fetching the character.
  303. */
  304. int_type
  305. sgetc()
  306. {
  307. int_type __ret;
  308. if (__builtin_expect(this->gptr() < this->egptr(), true))
  309. __ret = traits_type::to_int_type(*this->gptr());
  310. else
  311. __ret = this->underflow();
  312. return __ret;
  313. }
  314. /**
  315. * @brief Entry point for xsgetn.
  316. * @param __s A buffer area.
  317. * @param __n A count.
  318. *
  319. * Returns xsgetn(__s,__n). The effect is to fill @a __s[0] through
  320. * @a __s[__n-1] with characters from the input sequence, if possible.
  321. */
  322. streamsize
  323. sgetn(char_type* __s, streamsize __n)
  324. { return this->xsgetn(__s, __n); }
  325. // [27.5.2.2.4] putback
  326. /**
  327. * @brief Pushing characters back into the input stream.
  328. * @param __c The character to push back.
  329. * @return The previous character, if possible.
  330. *
  331. * Similar to sungetc(), but @a __c is pushed onto the stream
  332. * instead of <em>the previous character.</em> If successful,
  333. * the next character fetched from the input stream will be @a
  334. * __c.
  335. */
  336. int_type
  337. sputbackc(char_type __c)
  338. {
  339. int_type __ret;
  340. const bool __testpos = this->eback() < this->gptr();
  341. if (__builtin_expect(!__testpos ||
  342. !traits_type::eq(__c, this->gptr()[-1]), false))
  343. __ret = this->pbackfail(traits_type::to_int_type(__c));
  344. else
  345. {
  346. this->gbump(-1);
  347. __ret = traits_type::to_int_type(*this->gptr());
  348. }
  349. return __ret;
  350. }
  351. /**
  352. * @brief Moving backwards in the input stream.
  353. * @return The previous character, if possible.
  354. *
  355. * If a putback position is available, this function decrements
  356. * the input pointer and returns that character. Otherwise,
  357. * calls and returns pbackfail(). The effect is to @a unget
  358. * the last character @a gotten.
  359. */
  360. int_type
  361. sungetc()
  362. {
  363. int_type __ret;
  364. if (__builtin_expect(this->eback() < this->gptr(), true))
  365. {
  366. this->gbump(-1);
  367. __ret = traits_type::to_int_type(*this->gptr());
  368. }
  369. else
  370. __ret = this->pbackfail();
  371. return __ret;
  372. }
  373. // [27.5.2.2.5] put area
  374. /**
  375. * @brief Entry point for all single-character output functions.
  376. * @param __c A character to output.
  377. * @return @a __c, if possible.
  378. *
  379. * One of two public output functions.
  380. *
  381. * If a write position is available for the output sequence (i.e.,
  382. * the buffer is not full), stores @a __c in that position, increments
  383. * the position, and returns @c traits::to_int_type(__c). If a write
  384. * position is not available, returns @c overflow(__c).
  385. */
  386. int_type
  387. sputc(char_type __c)
  388. {
  389. int_type __ret;
  390. if (__builtin_expect(this->pptr() < this->epptr(), true))
  391. {
  392. *this->pptr() = __c;
  393. this->pbump(1);
  394. __ret = traits_type::to_int_type(__c);
  395. }
  396. else
  397. __ret = this->overflow(traits_type::to_int_type(__c));
  398. return __ret;
  399. }
  400. /**
  401. * @brief Entry point for all single-character output functions.
  402. * @param __s A buffer read area.
  403. * @param __n A count.
  404. *
  405. * One of two public output functions.
  406. *
  407. *
  408. * Returns xsputn(__s,__n). The effect is to write @a __s[0] through
  409. * @a __s[__n-1] to the output sequence, if possible.
  410. */
  411. streamsize
  412. sputn(const char_type* __s, streamsize __n)
  413. { return this->xsputn(__s, __n); }
  414. protected:
  415. /**
  416. * @brief Base constructor.
  417. *
  418. * Only called from derived constructors, and sets up all the
  419. * buffer data to zero, including the pointers described in the
  420. * basic_streambuf class description. Note that, as a result,
  421. * - the class starts with no read nor write positions available,
  422. * - this is not an error
  423. */
  424. basic_streambuf()
  425. : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
  426. _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
  427. _M_buf_locale(locale())
  428. { }
  429. // [27.5.2.3.1] get area access
  430. //@{
  431. /**
  432. * @brief Access to the get area.
  433. *
  434. * These functions are only available to other protected functions,
  435. * including derived classes.
  436. *
  437. * - eback() returns the beginning pointer for the input sequence
  438. * - gptr() returns the next pointer for the input sequence
  439. * - egptr() returns the end pointer for the input sequence
  440. */
  441. char_type*
  442. eback() const { return _M_in_beg; }
  443. char_type*
  444. gptr() const { return _M_in_cur; }
  445. char_type*
  446. egptr() const { return _M_in_end; }
  447. //@}
  448. /**
  449. * @brief Moving the read position.
  450. * @param __n The delta by which to move.
  451. *
  452. * This just advances the read position without returning any data.
  453. */
  454. void
  455. gbump(int __n) { _M_in_cur += __n; }
  456. /**
  457. * @brief Setting the three read area pointers.
  458. * @param __gbeg A pointer.
  459. * @param __gnext A pointer.
  460. * @param __gend A pointer.
  461. * @post @a __gbeg == @c eback(), @a __gnext == @c gptr(), and
  462. * @a __gend == @c egptr()
  463. */
  464. void
  465. setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
  466. {
  467. _M_in_beg = __gbeg;
  468. _M_in_cur = __gnext;
  469. _M_in_end = __gend;
  470. }
  471. // [27.5.2.3.2] put area access
  472. //@{
  473. /**
  474. * @brief Access to the put area.
  475. *
  476. * These functions are only available to other protected functions,
  477. * including derived classes.
  478. *
  479. * - pbase() returns the beginning pointer for the output sequence
  480. * - pptr() returns the next pointer for the output sequence
  481. * - epptr() returns the end pointer for the output sequence
  482. */
  483. char_type*
  484. pbase() const { return _M_out_beg; }
  485. char_type*
  486. pptr() const { return _M_out_cur; }
  487. char_type*
  488. epptr() const { return _M_out_end; }
  489. //@}
  490. /**
  491. * @brief Moving the write position.
  492. * @param __n The delta by which to move.
  493. *
  494. * This just advances the write position without returning any data.
  495. */
  496. void
  497. pbump(int __n) { _M_out_cur += __n; }
  498. /**
  499. * @brief Setting the three write area pointers.
  500. * @param __pbeg A pointer.
  501. * @param __pend A pointer.
  502. * @post @a __pbeg == @c pbase(), @a __pbeg == @c pptr(), and
  503. * @a __pend == @c epptr()
  504. */
  505. void
  506. setp(char_type* __pbeg, char_type* __pend)
  507. {
  508. _M_out_beg = _M_out_cur = __pbeg;
  509. _M_out_end = __pend;
  510. }
  511. // [27.5.2.4] virtual functions
  512. // [27.5.2.4.1] locales
  513. /**
  514. * @brief Changes translations.
  515. * @param __loc A new locale.
  516. *
  517. * Translations done during I/O which depend on the current
  518. * locale are changed by this call. The standard adds,
  519. * <em>Between invocations of this function a class derived
  520. * from streambuf can safely cache results of calls to locale
  521. * functions and to members of facets so obtained.</em>
  522. *
  523. * @note Base class version does nothing.
  524. */
  525. virtual void
  526. imbue(const locale& __loc)
  527. { }
  528. // [27.5.2.4.2] buffer management and positioning
  529. /**
  530. * @brief Manipulates the buffer.
  531. *
  532. * Each derived class provides its own appropriate behavior. See
  533. * the next-to-last paragraph of
  534. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
  535. * for more on this function.
  536. *
  537. * @note Base class version does nothing, returns @c this.
  538. */
  539. virtual basic_streambuf<char_type,_Traits>*
  540. setbuf(char_type*, streamsize)
  541. { return this; }
  542. /**
  543. * @brief Alters the stream positions.
  544. *
  545. * Each derived class provides its own appropriate behavior.
  546. * @note Base class version does nothing, returns a @c pos_type
  547. * that represents an invalid stream position.
  548. */
  549. virtual pos_type
  550. seekoff(off_type, ios_base::seekdir,
  551. ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
  552. { return pos_type(off_type(-1)); }
  553. /**
  554. * @brief Alters the stream positions.
  555. *
  556. * Each derived class provides its own appropriate behavior.
  557. * @note Base class version does nothing, returns a @c pos_type
  558. * that represents an invalid stream position.
  559. */
  560. virtual pos_type
  561. seekpos(pos_type,
  562. ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
  563. { return pos_type(off_type(-1)); }
  564. /**
  565. * @brief Synchronizes the buffer arrays with the controlled sequences.
  566. * @return -1 on failure.
  567. *
  568. * Each derived class provides its own appropriate behavior,
  569. * including the definition of @a failure.
  570. * @note Base class version does nothing, returns zero.
  571. */
  572. virtual int
  573. sync() { return 0; }
  574. // [27.5.2.4.3] get area
  575. /**
  576. * @brief Investigating the data available.
  577. * @return An estimate of the number of characters available in the
  578. * input sequence, or -1.
  579. *
  580. * <em>If it returns a positive value, then successive calls to
  581. * @c underflow() will not return @c traits::eof() until at
  582. * least that number of characters have been supplied. If @c
  583. * showmanyc() returns -1, then calls to @c underflow() or @c
  584. * uflow() will fail.</em> [27.5.2.4.3]/1
  585. *
  586. * @note Base class version does nothing, returns zero.
  587. * @note The standard adds that <em>the intention is not only that the
  588. * calls [to underflow or uflow] will not return @c eof() but
  589. * that they will return immediately.</em>
  590. * @note The standard adds that <em>the morphemes of @c showmanyc are
  591. * @b es-how-many-see, not @b show-manic.</em>
  592. */
  593. virtual streamsize
  594. showmanyc() { return 0; }
  595. /**
  596. * @brief Multiple character extraction.
  597. * @param __s A buffer area.
  598. * @param __n Maximum number of characters to assign.
  599. * @return The number of characters assigned.
  600. *
  601. * Fills @a __s[0] through @a __s[__n-1] with characters from the input
  602. * sequence, as if by @c sbumpc(). Stops when either @a __n characters
  603. * have been copied, or when @c traits::eof() would be copied.
  604. *
  605. * It is expected that derived classes provide a more efficient
  606. * implementation by overriding this definition.
  607. */
  608. virtual streamsize
  609. xsgetn(char_type* __s, streamsize __n);
  610. /**
  611. * @brief Fetches more data from the controlled sequence.
  612. * @return The first character from the <em>pending sequence</em>.
  613. *
  614. * Informally, this function is called when the input buffer is
  615. * exhausted (or does not exist, as buffering need not actually be
  616. * done). If a buffer exists, it is @a refilled. In either case, the
  617. * next available character is returned, or @c traits::eof() to
  618. * indicate a null pending sequence.
  619. *
  620. * For a formal definition of the pending sequence, see a good text
  621. * such as Langer & Kreft, or [27.5.2.4.3]/7-14.
  622. *
  623. * A functioning input streambuf can be created by overriding only
  624. * this function (no buffer area will be used). For an example, see
  625. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html
  626. *
  627. * @note Base class version does nothing, returns eof().
  628. */
  629. virtual int_type
  630. underflow()
  631. { return traits_type::eof(); }
  632. /**
  633. * @brief Fetches more data from the controlled sequence.
  634. * @return The first character from the <em>pending sequence</em>.
  635. *
  636. * Informally, this function does the same thing as @c underflow(),
  637. * and in fact is required to call that function. It also returns
  638. * the new character, like @c underflow() does. However, this
  639. * function also moves the read position forward by one.
  640. */
  641. virtual int_type
  642. uflow()
  643. {
  644. int_type __ret = traits_type::eof();
  645. const bool __testeof = traits_type::eq_int_type(this->underflow(),
  646. __ret);
  647. if (!__testeof)
  648. {
  649. __ret = traits_type::to_int_type(*this->gptr());
  650. this->gbump(1);
  651. }
  652. return __ret;
  653. }
  654. // [27.5.2.4.4] putback
  655. /**
  656. * @brief Tries to back up the input sequence.
  657. * @param __c The character to be inserted back into the sequence.
  658. * @return eof() on failure, <em>some other value</em> on success
  659. * @post The constraints of @c gptr(), @c eback(), and @c pptr()
  660. * are the same as for @c underflow().
  661. *
  662. * @note Base class version does nothing, returns eof().
  663. */
  664. virtual int_type
  665. pbackfail(int_type __c = traits_type::eof())
  666. { return traits_type::eof(); }
  667. // Put area:
  668. /**
  669. * @brief Multiple character insertion.
  670. * @param __s A buffer area.
  671. * @param __n Maximum number of characters to write.
  672. * @return The number of characters written.
  673. *
  674. * Writes @a __s[0] through @a __s[__n-1] to the output sequence, as if
  675. * by @c sputc(). Stops when either @a n characters have been
  676. * copied, or when @c sputc() would return @c traits::eof().
  677. *
  678. * It is expected that derived classes provide a more efficient
  679. * implementation by overriding this definition.
  680. */
  681. virtual streamsize
  682. xsputn(const char_type* __s, streamsize __n);
  683. /**
  684. * @brief Consumes data from the buffer; writes to the
  685. * controlled sequence.
  686. * @param __c An additional character to consume.
  687. * @return eof() to indicate failure, something else (usually
  688. * @a __c, or not_eof())
  689. *
  690. * Informally, this function is called when the output buffer
  691. * is full (or does not exist, as buffering need not actually
  692. * be done). If a buffer exists, it is @a consumed, with
  693. * <em>some effect</em> on the controlled sequence.
  694. * (Typically, the buffer is written out to the sequence
  695. * verbatim.) In either case, the character @a c is also
  696. * written out, if @a __c is not @c eof().
  697. *
  698. * For a formal definition of this function, see a good text
  699. * such as Langer & Kreft, or [27.5.2.4.5]/3-7.
  700. *
  701. * A functioning output streambuf can be created by overriding only
  702. * this function (no buffer area will be used).
  703. *
  704. * @note Base class version does nothing, returns eof().
  705. */
  706. virtual int_type
  707. overflow(int_type __c = traits_type::eof())
  708. { return traits_type::eof(); }
  709. #if _GLIBCXX_USE_DEPRECATED
  710. // Annex D.6
  711. public:
  712. /**
  713. * @brief Tosses a character.
  714. *
  715. * Advances the read pointer, ignoring the character that would have
  716. * been read.
  717. *
  718. * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
  719. */
  720. void
  721. stossc()
  722. {
  723. if (this->gptr() < this->egptr())
  724. this->gbump(1);
  725. else
  726. this->uflow();
  727. }
  728. #endif
  729. // Also used by specializations for char and wchar_t in src.
  730. void
  731. __safe_gbump(streamsize __n) { _M_in_cur += __n; }
  732. void
  733. __safe_pbump(streamsize __n) { _M_out_cur += __n; }
  734. #if __cplusplus < 201103L
  735. private:
  736. #else
  737. protected:
  738. #endif
  739. basic_streambuf(const basic_streambuf&);
  740. basic_streambuf&
  741. operator=(const basic_streambuf&);
  742. #if __cplusplus >= 201103L
  743. void
  744. swap(basic_streambuf& __sb)
  745. {
  746. std::swap(_M_in_beg, __sb._M_in_beg);
  747. std::swap(_M_in_cur, __sb._M_in_cur);
  748. std::swap(_M_in_end, __sb._M_in_end);
  749. std::swap(_M_out_beg, __sb._M_out_beg);
  750. std::swap(_M_out_cur, __sb._M_out_cur);
  751. std::swap(_M_out_end, __sb._M_out_end);
  752. std::swap(_M_buf_locale, __sb._M_buf_locale);
  753. }
  754. #endif
  755. };
  756. #if __cplusplus >= 201103L
  757. template<typename _CharT, typename _Traits>
  758. std::basic_streambuf<_CharT, _Traits>::
  759. basic_streambuf(const basic_streambuf&) = default;
  760. template<typename _CharT, typename _Traits>
  761. std::basic_streambuf<_CharT, _Traits>&
  762. std::basic_streambuf<_CharT, _Traits>::
  763. operator=(const basic_streambuf&) = default;
  764. #endif
  765. // Explicit specialization declarations, defined in src/streambuf.cc.
  766. template<>
  767. streamsize
  768. __copy_streambufs_eof(basic_streambuf<char>* __sbin,
  769. basic_streambuf<char>* __sbout, bool& __ineof);
  770. #ifdef _GLIBCXX_USE_WCHAR_T
  771. template<>
  772. streamsize
  773. __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin,
  774. basic_streambuf<wchar_t>* __sbout, bool& __ineof);
  775. #endif
  776. _GLIBCXX_END_NAMESPACE_VERSION
  777. } // namespace
  778. #include <bits/streambuf.tcc>
  779. #endif /* _GLIBCXX_STREAMBUF */