streambuf 27 KB

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