streambuf 29 KB

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