istream 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. // Input streams -*- C++ -*-
  2. // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  3. // 2006, 2007, 2008, 2009
  4. // Free Software Foundation, Inc.
  5. //
  6. // This file is part of the GNU ISO C++ Library. This library is free
  7. // software; you can redistribute it and/or modify it under the
  8. // terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 3, or (at your option)
  10. // any later version.
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // Under Section 7 of GPL version 3, you are granted additional
  16. // permissions described in the GCC Runtime Library Exception, version
  17. // 3.1, as published by the Free Software Foundation.
  18. // You should have received a copy of the GNU General Public License and
  19. // a copy of the GCC Runtime Library Exception along with this program;
  20. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  21. // <http://www.gnu.org/licenses/>.
  22. //
  23. // ISO C++ 14882: 27.6.1 Input streams
  24. //
  25. /** @file istream
  26. * This is a Standard C++ Library header.
  27. */
  28. #ifndef _GLIBCXX_ISTREAM
  29. #define _GLIBCXX_ISTREAM 1
  30. #pragma GCC system_header
  31. #include <ios>
  32. #include <ostream>
  33. _GLIBCXX_BEGIN_NAMESPACE(std)
  34. // [27.6.1.1] Template class basic_istream
  35. /**
  36. * @brief Controlling input.
  37. * @ingroup io
  38. *
  39. * This is the base class for all input streams. It provides text
  40. * formatting of all builtin types, and communicates with any class
  41. * derived from basic_streambuf to do the actual input.
  42. */
  43. template<typename _CharT, typename _Traits>
  44. class basic_istream : virtual public basic_ios<_CharT, _Traits>
  45. {
  46. public:
  47. // Types (inherited from basic_ios (27.4.4)):
  48. typedef _CharT char_type;
  49. typedef typename _Traits::int_type int_type;
  50. typedef typename _Traits::pos_type pos_type;
  51. typedef typename _Traits::off_type off_type;
  52. typedef _Traits traits_type;
  53. // Non-standard Types:
  54. typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
  55. typedef basic_ios<_CharT, _Traits> __ios_type;
  56. typedef basic_istream<_CharT, _Traits> __istream_type;
  57. typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
  58. __num_get_type;
  59. typedef ctype<_CharT> __ctype_type;
  60. protected:
  61. // Data Members:
  62. /**
  63. * The number of characters extracted in the previous unformatted
  64. * function; see gcount().
  65. */
  66. streamsize _M_gcount;
  67. public:
  68. // [27.6.1.1.1] constructor/destructor
  69. /**
  70. * @brief Base constructor.
  71. *
  72. * This ctor is almost never called by the user directly, rather from
  73. * derived classes' initialization lists, which pass a pointer to
  74. * their own stream buffer.
  75. */
  76. explicit
  77. basic_istream(__streambuf_type* __sb)
  78. : _M_gcount(streamsize(0))
  79. { this->init(__sb); }
  80. /**
  81. * @brief Base destructor.
  82. *
  83. * This does very little apart from providing a virtual base dtor.
  84. */
  85. virtual
  86. ~basic_istream()
  87. { _M_gcount = streamsize(0); }
  88. // [27.6.1.1.2] prefix/suffix
  89. class sentry;
  90. friend class sentry;
  91. // [27.6.1.2] formatted input
  92. // [27.6.1.2.3] basic_istream::operator>>
  93. //@{
  94. /**
  95. * @brief Interface for manipulators.
  96. *
  97. * Manipulators such as @c std::ws and @c std::dec use these
  98. * functions in constructs like "std::cin >> std::ws". For more
  99. * information, see the iomanip header.
  100. */
  101. __istream_type&
  102. operator>>(__istream_type& (*__pf)(__istream_type&))
  103. { return __pf(*this); }
  104. __istream_type&
  105. operator>>(__ios_type& (*__pf)(__ios_type&))
  106. {
  107. __pf(*this);
  108. return *this;
  109. }
  110. __istream_type&
  111. operator>>(ios_base& (*__pf)(ios_base&))
  112. {
  113. __pf(*this);
  114. return *this;
  115. }
  116. //@}
  117. // [27.6.1.2.2] arithmetic extractors
  118. /**
  119. * @name Arithmetic Extractors
  120. *
  121. * All the @c operator>> functions (aka <em>formatted input
  122. * functions</em>) have some common behavior. Each starts by
  123. * constructing a temporary object of type std::basic_istream::sentry
  124. * with the second argument (noskipws) set to false. This has several
  125. * effects, concluding with the setting of a status flag; see the
  126. * sentry documentation for more.
  127. *
  128. * If the sentry status is good, the function tries to extract
  129. * whatever data is appropriate for the type of the argument.
  130. *
  131. * If an exception is thrown during extraction, ios_base::badbit
  132. * will be turned on in the stream's error state without causing an
  133. * ios_base::failure to be thrown. The original exception will then
  134. * be rethrown.
  135. */
  136. //@{
  137. /**
  138. * @brief Basic arithmetic extractors
  139. * @param A variable of builtin type.
  140. * @return @c *this if successful
  141. *
  142. * These functions use the stream's current locale (specifically, the
  143. * @c num_get facet) to parse the input data.
  144. */
  145. __istream_type&
  146. operator>>(bool& __n)
  147. { return _M_extract(__n); }
  148. __istream_type&
  149. operator>>(short& __n);
  150. __istream_type&
  151. operator>>(unsigned short& __n)
  152. { return _M_extract(__n); }
  153. __istream_type&
  154. operator>>(int& __n);
  155. __istream_type&
  156. operator>>(unsigned int& __n)
  157. { return _M_extract(__n); }
  158. __istream_type&
  159. operator>>(long& __n)
  160. { return _M_extract(__n); }
  161. __istream_type&
  162. operator>>(unsigned long& __n)
  163. { return _M_extract(__n); }
  164. #ifdef _GLIBCXX_USE_LONG_LONG
  165. __istream_type&
  166. operator>>(long long& __n)
  167. { return _M_extract(__n); }
  168. __istream_type&
  169. operator>>(unsigned long long& __n)
  170. { return _M_extract(__n); }
  171. #endif
  172. __istream_type&
  173. operator>>(float& __f)
  174. { return _M_extract(__f); }
  175. __istream_type&
  176. operator>>(double& __f)
  177. { return _M_extract(__f); }
  178. __istream_type&
  179. operator>>(long double& __f)
  180. { return _M_extract(__f); }
  181. __istream_type&
  182. operator>>(void*& __p)
  183. { return _M_extract(__p); }
  184. /**
  185. * @brief Extracting into another streambuf.
  186. * @param sb A pointer to a streambuf
  187. *
  188. * This function behaves like one of the basic arithmetic extractors,
  189. * in that it also constructs a sentry object and has the same error
  190. * handling behavior.
  191. *
  192. * If @a sb is NULL, the stream will set failbit in its error state.
  193. *
  194. * Characters are extracted from this stream and inserted into the
  195. * @a sb streambuf until one of the following occurs:
  196. *
  197. * - the input stream reaches end-of-file,
  198. * - insertion into the output buffer fails (in this case, the
  199. * character that would have been inserted is not extracted), or
  200. * - an exception occurs (and in this case is caught)
  201. *
  202. * If the function inserts no characters, failbit is set.
  203. */
  204. __istream_type&
  205. operator>>(__streambuf_type* __sb);
  206. //@}
  207. // [27.6.1.3] unformatted input
  208. /**
  209. * @brief Character counting
  210. * @return The number of characters extracted by the previous
  211. * unformatted input function dispatched for this stream.
  212. */
  213. streamsize
  214. gcount() const
  215. { return _M_gcount; }
  216. /**
  217. * @name Unformatted Input Functions
  218. *
  219. * All the unformatted input functions have some common behavior.
  220. * Each starts by constructing a temporary object of type
  221. * std::basic_istream::sentry with the second argument (noskipws)
  222. * set to true. This has several effects, concluding with the
  223. * setting of a status flag; see the sentry documentation for more.
  224. *
  225. * If the sentry status is good, the function tries to extract
  226. * whatever data is appropriate for the type of the argument.
  227. *
  228. * The number of characters extracted is stored for later retrieval
  229. * by gcount().
  230. *
  231. * If an exception is thrown during extraction, ios_base::badbit
  232. * will be turned on in the stream's error state without causing an
  233. * ios_base::failure to be thrown. The original exception will then
  234. * be rethrown.
  235. */
  236. //@{
  237. /**
  238. * @brief Simple extraction.
  239. * @return A character, or eof().
  240. *
  241. * Tries to extract a character. If none are available, sets failbit
  242. * and returns traits::eof().
  243. */
  244. int_type
  245. get();
  246. /**
  247. * @brief Simple extraction.
  248. * @param c The character in which to store data.
  249. * @return *this
  250. *
  251. * Tries to extract a character and store it in @a c. If none are
  252. * available, sets failbit and returns traits::eof().
  253. *
  254. * @note This function is not overloaded on signed char and
  255. * unsigned char.
  256. */
  257. __istream_type&
  258. get(char_type& __c);
  259. /**
  260. * @brief Simple multiple-character extraction.
  261. * @param s Pointer to an array.
  262. * @param n Maximum number of characters to store in @a s.
  263. * @param delim A "stop" character.
  264. * @return *this
  265. *
  266. * Characters are extracted and stored into @a s until one of the
  267. * following happens:
  268. *
  269. * - @c n-1 characters are stored
  270. * - the input sequence reaches EOF
  271. * - the next character equals @a delim, in which case the character
  272. * is not extracted
  273. *
  274. * If no characters are stored, failbit is set in the stream's error
  275. * state.
  276. *
  277. * In any case, a null character is stored into the next location in
  278. * the array.
  279. *
  280. * @note This function is not overloaded on signed char and
  281. * unsigned char.
  282. */
  283. __istream_type&
  284. get(char_type* __s, streamsize __n, char_type __delim);
  285. /**
  286. * @brief Simple multiple-character extraction.
  287. * @param s Pointer to an array.
  288. * @param n Maximum number of characters to store in @a s.
  289. * @return *this
  290. *
  291. * Returns @c get(s,n,widen('\n')).
  292. */
  293. __istream_type&
  294. get(char_type* __s, streamsize __n)
  295. { return this->get(__s, __n, this->widen('\n')); }
  296. /**
  297. * @brief Extraction into another streambuf.
  298. * @param sb A streambuf in which to store data.
  299. * @param delim A "stop" character.
  300. * @return *this
  301. *
  302. * Characters are extracted and inserted into @a sb until one of the
  303. * following happens:
  304. *
  305. * - the input sequence reaches EOF
  306. * - insertion into the output buffer fails (in this case, the
  307. * character that would have been inserted is not extracted)
  308. * - the next character equals @a delim (in this case, the character
  309. * is not extracted)
  310. * - an exception occurs (and in this case is caught)
  311. *
  312. * If no characters are stored, failbit is set in the stream's error
  313. * state.
  314. */
  315. __istream_type&
  316. get(__streambuf_type& __sb, char_type __delim);
  317. /**
  318. * @brief Extraction into another streambuf.
  319. * @param sb A streambuf in which to store data.
  320. * @return *this
  321. *
  322. * Returns @c get(sb,widen('\n')).
  323. */
  324. __istream_type&
  325. get(__streambuf_type& __sb)
  326. { return this->get(__sb, this->widen('\n')); }
  327. /**
  328. * @brief String extraction.
  329. * @param s A character array in which to store the data.
  330. * @param n Maximum number of characters to extract.
  331. * @param delim A "stop" character.
  332. * @return *this
  333. *
  334. * Extracts and stores characters into @a s until one of the
  335. * following happens. Note that these criteria are required to be
  336. * tested in the order listed here, to allow an input line to exactly
  337. * fill the @a s array without setting failbit.
  338. *
  339. * -# the input sequence reaches end-of-file, in which case eofbit
  340. * is set in the stream error state
  341. * -# the next character equals @c delim, in which case the character
  342. * is extracted (and therefore counted in @c gcount()) but not stored
  343. * -# @c n-1 characters are stored, in which case failbit is set
  344. * in the stream error state
  345. *
  346. * If no characters are extracted, failbit is set. (An empty line of
  347. * input should therefore not cause failbit to be set.)
  348. *
  349. * In any case, a null character is stored in the next location in
  350. * the array.
  351. */
  352. __istream_type&
  353. getline(char_type* __s, streamsize __n, char_type __delim);
  354. /**
  355. * @brief String extraction.
  356. * @param s A character array in which to store the data.
  357. * @param n Maximum number of characters to extract.
  358. * @return *this
  359. *
  360. * Returns @c getline(s,n,widen('\n')).
  361. */
  362. __istream_type&
  363. getline(char_type* __s, streamsize __n)
  364. { return this->getline(__s, __n, this->widen('\n')); }
  365. /**
  366. * @brief Discarding characters
  367. * @param n Number of characters to discard.
  368. * @param delim A "stop" character.
  369. * @return *this
  370. *
  371. * Extracts characters and throws them away until one of the
  372. * following happens:
  373. * - if @a n @c != @c std::numeric_limits<int>::max(), @a n
  374. * characters are extracted
  375. * - the input sequence reaches end-of-file
  376. * - the next character equals @a delim (in this case, the character
  377. * is extracted); note that this condition will never occur if
  378. * @a delim equals @c traits::eof().
  379. *
  380. * NB: Provide three overloads, instead of the single function
  381. * (with defaults) mandated by the Standard: this leads to a
  382. * better performing implementation, while still conforming to
  383. * the Standard.
  384. */
  385. __istream_type&
  386. ignore();
  387. __istream_type&
  388. ignore(streamsize __n);
  389. __istream_type&
  390. ignore(streamsize __n, int_type __delim);
  391. /**
  392. * @brief Looking ahead in the stream
  393. * @return The next character, or eof().
  394. *
  395. * If, after constructing the sentry object, @c good() is false,
  396. * returns @c traits::eof(). Otherwise reads but does not extract
  397. * the next input character.
  398. */
  399. int_type
  400. peek();
  401. /**
  402. * @brief Extraction without delimiters.
  403. * @param s A character array.
  404. * @param n Maximum number of characters to store.
  405. * @return *this
  406. *
  407. * If the stream state is @c good(), extracts characters and stores
  408. * them into @a s until one of the following happens:
  409. * - @a n characters are stored
  410. * - the input sequence reaches end-of-file, in which case the error
  411. * state is set to @c failbit|eofbit.
  412. *
  413. * @note This function is not overloaded on signed char and
  414. * unsigned char.
  415. */
  416. __istream_type&
  417. read(char_type* __s, streamsize __n);
  418. /**
  419. * @brief Extraction until the buffer is exhausted, but no more.
  420. * @param s A character array.
  421. * @param n Maximum number of characters to store.
  422. * @return The number of characters extracted.
  423. *
  424. * Extracts characters and stores them into @a s depending on the
  425. * number of characters remaining in the streambuf's buffer,
  426. * @c rdbuf()->in_avail(), called @c A here:
  427. * - if @c A @c == @c -1, sets eofbit and extracts no characters
  428. * - if @c A @c == @c 0, extracts no characters
  429. * - if @c A @c > @c 0, extracts @c min(A,n)
  430. *
  431. * The goal is to empty the current buffer, and to not request any
  432. * more from the external input sequence controlled by the streambuf.
  433. */
  434. streamsize
  435. readsome(char_type* __s, streamsize __n);
  436. /**
  437. * @brief Unextracting a single character.
  438. * @param c The character to push back into the input stream.
  439. * @return *this
  440. *
  441. * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
  442. *
  443. * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
  444. * the error state.
  445. *
  446. * @note Since no characters are extracted, the next call to
  447. * @c gcount() will return 0, as required by DR 60.
  448. */
  449. __istream_type&
  450. putback(char_type __c);
  451. /**
  452. * @brief Unextracting the previous character.
  453. * @return *this
  454. *
  455. * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
  456. *
  457. * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
  458. * the error state.
  459. *
  460. * @note Since no characters are extracted, the next call to
  461. * @c gcount() will return 0, as required by DR 60.
  462. */
  463. __istream_type&
  464. unget();
  465. /**
  466. * @brief Synchronizing the stream buffer.
  467. * @return 0 on success, -1 on failure
  468. *
  469. * If @c rdbuf() is a null pointer, returns -1.
  470. *
  471. * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
  472. * sets badbit and returns -1.
  473. *
  474. * Otherwise, returns 0.
  475. *
  476. * @note This function does not count the number of characters
  477. * extracted, if any, and therefore does not affect the next
  478. * call to @c gcount().
  479. */
  480. int
  481. sync();
  482. /**
  483. * @brief Getting the current read position.
  484. * @return A file position object.
  485. *
  486. * If @c fail() is not false, returns @c pos_type(-1) to indicate
  487. * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
  488. *
  489. * @note This function does not count the number of characters
  490. * extracted, if any, and therefore does not affect the next
  491. * call to @c gcount().
  492. */
  493. pos_type
  494. tellg();
  495. /**
  496. * @brief Changing the current read position.
  497. * @param pos A file position object.
  498. * @return *this
  499. *
  500. * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
  501. * that function fails, sets failbit.
  502. *
  503. * @note This function does not count the number of characters
  504. * extracted, if any, and therefore does not affect the next
  505. * call to @c gcount().
  506. */
  507. __istream_type&
  508. seekg(pos_type);
  509. /**
  510. * @brief Changing the current read position.
  511. * @param off A file offset object.
  512. * @param dir The direction in which to seek.
  513. * @return *this
  514. *
  515. * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
  516. * If that function fails, sets failbit.
  517. *
  518. * @note This function does not count the number of characters
  519. * extracted, if any, and therefore does not affect the next
  520. * call to @c gcount().
  521. */
  522. __istream_type&
  523. seekg(off_type, ios_base::seekdir);
  524. //@}
  525. protected:
  526. basic_istream()
  527. : _M_gcount(streamsize(0))
  528. { this->init(0); }
  529. template<typename _ValueT>
  530. __istream_type&
  531. _M_extract(_ValueT& __v);
  532. };
  533. // Explicit specialization declarations, defined in src/istream.cc.
  534. template<>
  535. basic_istream<char>&
  536. basic_istream<char>::
  537. getline(char_type* __s, streamsize __n, char_type __delim);
  538. template<>
  539. basic_istream<char>&
  540. basic_istream<char>::
  541. ignore(streamsize __n);
  542. template<>
  543. basic_istream<char>&
  544. basic_istream<char>::
  545. ignore(streamsize __n, int_type __delim);
  546. #ifdef _GLIBCXX_USE_WCHAR_T
  547. template<>
  548. basic_istream<wchar_t>&
  549. basic_istream<wchar_t>::
  550. getline(char_type* __s, streamsize __n, char_type __delim);
  551. template<>
  552. basic_istream<wchar_t>&
  553. basic_istream<wchar_t>::
  554. ignore(streamsize __n);
  555. template<>
  556. basic_istream<wchar_t>&
  557. basic_istream<wchar_t>::
  558. ignore(streamsize __n, int_type __delim);
  559. #endif
  560. /**
  561. * @brief Performs setup work for input streams.
  562. *
  563. * Objects of this class are created before all of the standard
  564. * extractors are run. It is responsible for "exception-safe prefix and
  565. * suffix operations," although only prefix actions are currently required
  566. * by the standard.
  567. */
  568. template<typename _CharT, typename _Traits>
  569. class basic_istream<_CharT, _Traits>::sentry
  570. {
  571. public:
  572. /// Easy access to dependant types.
  573. typedef _Traits traits_type;
  574. typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
  575. typedef basic_istream<_CharT, _Traits> __istream_type;
  576. typedef typename __istream_type::__ctype_type __ctype_type;
  577. typedef typename _Traits::int_type __int_type;
  578. /**
  579. * @brief The constructor performs all the work.
  580. * @param is The input stream to guard.
  581. * @param noskipws Whether to consume whitespace or not.
  582. *
  583. * If the stream state is good (@a is.good() is true), then the
  584. * following actions are performed, otherwise the sentry state is
  585. * false ("not okay") and failbit is set in the stream state.
  586. *
  587. * The sentry's preparatory actions are:
  588. *
  589. * -# if the stream is tied to an output stream, @c is.tie()->flush()
  590. * is called to synchronize the output sequence
  591. * -# if @a noskipws is false, and @c ios_base::skipws is set in
  592. * @c is.flags(), the sentry extracts and discards whitespace
  593. * characters from the stream. The currently imbued locale is
  594. * used to determine whether each character is whitespace.
  595. *
  596. * If the stream state is still good, then the sentry state becomes
  597. * true ("okay").
  598. */
  599. explicit
  600. sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
  601. /**
  602. * @brief Quick status checking.
  603. * @return The sentry state.
  604. *
  605. * For ease of use, sentries may be converted to booleans. The
  606. * return value is that of the sentry state (true == okay).
  607. */
  608. operator bool() const
  609. { return _M_ok; }
  610. private:
  611. bool _M_ok;
  612. };
  613. // [27.6.1.2.3] character extraction templates
  614. //@{
  615. /**
  616. * @brief Character extractors
  617. * @param in An input stream.
  618. * @param c A character reference.
  619. * @return in
  620. *
  621. * Behaves like one of the formatted arithmetic extractors described in
  622. * std::basic_istream. After constructing a sentry object with good
  623. * status, this function extracts a character (if one is available) and
  624. * stores it in @a c. Otherwise, sets failbit in the input stream.
  625. */
  626. template<typename _CharT, typename _Traits>
  627. basic_istream<_CharT, _Traits>&
  628. operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c);
  629. template<class _Traits>
  630. inline basic_istream<char, _Traits>&
  631. operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
  632. { return (__in >> reinterpret_cast<char&>(__c)); }
  633. template<class _Traits>
  634. inline basic_istream<char, _Traits>&
  635. operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
  636. { return (__in >> reinterpret_cast<char&>(__c)); }
  637. //@}
  638. //@{
  639. /**
  640. * @brief Character string extractors
  641. * @param in An input stream.
  642. * @param s A pointer to a character array.
  643. * @return in
  644. *
  645. * Behaves like one of the formatted arithmetic extractors described in
  646. * std::basic_istream. After constructing a sentry object with good
  647. * status, this function extracts up to @c n characters and stores them
  648. * into the array starting at @a s. @c n is defined as:
  649. *
  650. * - if @c width() is greater than zero, @c n is width()
  651. * - otherwise @c n is "the number of elements of the largest array of
  652. * @c char_type that can store a terminating @c eos." [27.6.1.2.3]/6
  653. *
  654. * Characters are extracted and stored until one of the following happens:
  655. * - @c n-1 characters are stored
  656. * - EOF is reached
  657. * - the next character is whitespace according to the current locale
  658. * - the next character is a null byte (i.e., @c charT() )
  659. *
  660. * @c width(0) is then called for the input stream.
  661. *
  662. * If no characters are extracted, sets failbit.
  663. */
  664. template<typename _CharT, typename _Traits>
  665. basic_istream<_CharT, _Traits>&
  666. operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s);
  667. // Explicit specialization declaration, defined in src/istream.cc.
  668. template<>
  669. basic_istream<char>&
  670. operator>>(basic_istream<char>& __in, char* __s);
  671. template<class _Traits>
  672. inline basic_istream<char, _Traits>&
  673. operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
  674. { return (__in >> reinterpret_cast<char*>(__s)); }
  675. template<class _Traits>
  676. inline basic_istream<char, _Traits>&
  677. operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
  678. { return (__in >> reinterpret_cast<char*>(__s)); }
  679. //@}
  680. // 27.6.1.5 Template class basic_iostream
  681. /**
  682. * @brief Merging istream and ostream capabilities.
  683. * @ingroup io
  684. *
  685. * This class multiply inherits from the input and output stream classes
  686. * simply to provide a single interface.
  687. */
  688. template<typename _CharT, typename _Traits>
  689. class basic_iostream
  690. : public basic_istream<_CharT, _Traits>,
  691. public basic_ostream<_CharT, _Traits>
  692. {
  693. public:
  694. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  695. // 271. basic_iostream missing typedefs
  696. // Types (inherited):
  697. typedef _CharT char_type;
  698. typedef typename _Traits::int_type int_type;
  699. typedef typename _Traits::pos_type pos_type;
  700. typedef typename _Traits::off_type off_type;
  701. typedef _Traits traits_type;
  702. // Non-standard Types:
  703. typedef basic_istream<_CharT, _Traits> __istream_type;
  704. typedef basic_ostream<_CharT, _Traits> __ostream_type;
  705. /**
  706. * @brief Constructor does nothing.
  707. *
  708. * Both of the parent classes are initialized with the same
  709. * streambuf pointer passed to this constructor.
  710. */
  711. explicit
  712. basic_iostream(basic_streambuf<_CharT, _Traits>* __sb)
  713. : __istream_type(__sb), __ostream_type(__sb) { }
  714. /**
  715. * @brief Destructor does nothing.
  716. */
  717. virtual
  718. ~basic_iostream() { }
  719. protected:
  720. basic_iostream()
  721. : __istream_type(), __ostream_type() { }
  722. };
  723. // [27.6.1.4] standard basic_istream manipulators
  724. /**
  725. * @brief Quick and easy way to eat whitespace
  726. *
  727. * This manipulator extracts whitespace characters, stopping when the
  728. * next character is non-whitespace, or when the input sequence is empty.
  729. * If the sequence is empty, @c eofbit is set in the stream, but not
  730. * @c failbit.
  731. *
  732. * The current locale is used to distinguish whitespace characters.
  733. *
  734. * Example:
  735. * @code
  736. * MyClass mc;
  737. *
  738. * std::cin >> std::ws >> mc;
  739. * @endcode
  740. * will skip leading whitespace before calling operator>> on cin and your
  741. * object. Note that the same effect can be achieved by creating a
  742. * std::basic_istream::sentry inside your definition of operator>>.
  743. */
  744. template<typename _CharT, typename _Traits>
  745. basic_istream<_CharT, _Traits>&
  746. ws(basic_istream<_CharT, _Traits>& __is);
  747. _GLIBCXX_END_NAMESPACE
  748. #ifndef _GLIBCXX_EXPORT_TEMPLATE
  749. # include <bits/istream.tcc>
  750. #endif
  751. #endif /* _GLIBCXX_ISTREAM */