istream.tcc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. // istream classes -*- 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. /** @file istream.tcc
  23. * This is an internal header file, included by other library headers.
  24. * You should not attempt to use it directly.
  25. */
  26. //
  27. // ISO C++ 14882: 27.6.1 Input streams
  28. //
  29. #ifndef _ISTREAM_TCC
  30. #define _ISTREAM_TCC 1
  31. #pragma GCC system_header
  32. #include <cxxabi-forced.h>
  33. _GLIBCXX_BEGIN_NAMESPACE(std)
  34. template<typename _CharT, typename _Traits>
  35. basic_istream<_CharT, _Traits>::sentry::
  36. sentry(basic_istream<_CharT, _Traits>& __in, bool __noskip) : _M_ok(false)
  37. {
  38. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  39. if (__in.good())
  40. {
  41. if (__in.tie())
  42. __in.tie()->flush();
  43. if (!__noskip && bool(__in.flags() & ios_base::skipws))
  44. {
  45. const __int_type __eof = traits_type::eof();
  46. __streambuf_type* __sb = __in.rdbuf();
  47. __int_type __c = __sb->sgetc();
  48. const __ctype_type& __ct = __check_facet(__in._M_ctype);
  49. while (!traits_type::eq_int_type(__c, __eof)
  50. && __ct.is(ctype_base::space,
  51. traits_type::to_char_type(__c)))
  52. __c = __sb->snextc();
  53. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  54. // 195. Should basic_istream::sentry's constructor ever
  55. // set eofbit?
  56. if (traits_type::eq_int_type(__c, __eof))
  57. __err |= ios_base::eofbit;
  58. }
  59. }
  60. if (__in.good() && __err == ios_base::goodbit)
  61. _M_ok = true;
  62. else
  63. {
  64. __err |= ios_base::failbit;
  65. __in.setstate(__err);
  66. }
  67. }
  68. template<typename _CharT, typename _Traits>
  69. template<typename _ValueT>
  70. basic_istream<_CharT, _Traits>&
  71. basic_istream<_CharT, _Traits>::
  72. _M_extract(_ValueT& __v)
  73. {
  74. sentry __cerb(*this, false);
  75. if (__cerb)
  76. {
  77. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  78. __try
  79. {
  80. const __num_get_type& __ng = __check_facet(this->_M_num_get);
  81. __ng.get(*this, 0, *this, __err, __v);
  82. }
  83. __catch(__cxxabiv1::__forced_unwind&)
  84. {
  85. this->_M_setstate(ios_base::badbit);
  86. __throw_exception_again;
  87. }
  88. __catch(...)
  89. { this->_M_setstate(ios_base::badbit); }
  90. if (__err)
  91. this->setstate(__err);
  92. }
  93. return *this;
  94. }
  95. template<typename _CharT, typename _Traits>
  96. basic_istream<_CharT, _Traits>&
  97. basic_istream<_CharT, _Traits>::
  98. operator>>(short& __n)
  99. {
  100. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  101. // 118. basic_istream uses nonexistent num_get member functions.
  102. long __l;
  103. _M_extract(__l);
  104. if (!this->fail())
  105. {
  106. if (__gnu_cxx::__numeric_traits<short>::__min <= __l
  107. && __l <= __gnu_cxx::__numeric_traits<short>::__max)
  108. __n = short(__l);
  109. else
  110. this->setstate(ios_base::failbit);
  111. }
  112. return *this;
  113. }
  114. template<typename _CharT, typename _Traits>
  115. basic_istream<_CharT, _Traits>&
  116. basic_istream<_CharT, _Traits>::
  117. operator>>(int& __n)
  118. {
  119. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  120. // 118. basic_istream uses nonexistent num_get member functions.
  121. long __l;
  122. _M_extract(__l);
  123. if (!this->fail())
  124. {
  125. if (__gnu_cxx::__numeric_traits<int>::__min <= __l
  126. && __l <= __gnu_cxx::__numeric_traits<int>::__max)
  127. __n = int(__l);
  128. else
  129. this->setstate(ios_base::failbit);
  130. }
  131. return *this;
  132. }
  133. template<typename _CharT, typename _Traits>
  134. basic_istream<_CharT, _Traits>&
  135. basic_istream<_CharT, _Traits>::
  136. operator>>(__streambuf_type* __sbout)
  137. {
  138. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  139. sentry __cerb(*this, false);
  140. if (__cerb && __sbout)
  141. {
  142. __try
  143. {
  144. bool __ineof;
  145. if (!__copy_streambufs_eof(this->rdbuf(), __sbout, __ineof))
  146. __err |= ios_base::failbit;
  147. if (__ineof)
  148. __err |= ios_base::eofbit;
  149. }
  150. __catch(__cxxabiv1::__forced_unwind&)
  151. {
  152. this->_M_setstate(ios_base::failbit);
  153. __throw_exception_again;
  154. }
  155. __catch(...)
  156. { this->_M_setstate(ios_base::failbit); }
  157. }
  158. else if (!__sbout)
  159. __err |= ios_base::failbit;
  160. if (__err)
  161. this->setstate(__err);
  162. return *this;
  163. }
  164. template<typename _CharT, typename _Traits>
  165. typename basic_istream<_CharT, _Traits>::int_type
  166. basic_istream<_CharT, _Traits>::
  167. get(void)
  168. {
  169. const int_type __eof = traits_type::eof();
  170. int_type __c = __eof;
  171. _M_gcount = 0;
  172. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  173. sentry __cerb(*this, true);
  174. if (__cerb)
  175. {
  176. __try
  177. {
  178. __c = this->rdbuf()->sbumpc();
  179. // 27.6.1.1 paragraph 3
  180. if (!traits_type::eq_int_type(__c, __eof))
  181. _M_gcount = 1;
  182. else
  183. __err |= ios_base::eofbit;
  184. }
  185. __catch(__cxxabiv1::__forced_unwind&)
  186. {
  187. this->_M_setstate(ios_base::badbit);
  188. __throw_exception_again;
  189. }
  190. __catch(...)
  191. { this->_M_setstate(ios_base::badbit); }
  192. }
  193. if (!_M_gcount)
  194. __err |= ios_base::failbit;
  195. if (__err)
  196. this->setstate(__err);
  197. return __c;
  198. }
  199. template<typename _CharT, typename _Traits>
  200. basic_istream<_CharT, _Traits>&
  201. basic_istream<_CharT, _Traits>::
  202. get(char_type& __c)
  203. {
  204. _M_gcount = 0;
  205. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  206. sentry __cerb(*this, true);
  207. if (__cerb)
  208. {
  209. __try
  210. {
  211. const int_type __cb = this->rdbuf()->sbumpc();
  212. // 27.6.1.1 paragraph 3
  213. if (!traits_type::eq_int_type(__cb, traits_type::eof()))
  214. {
  215. _M_gcount = 1;
  216. __c = traits_type::to_char_type(__cb);
  217. }
  218. else
  219. __err |= ios_base::eofbit;
  220. }
  221. __catch(__cxxabiv1::__forced_unwind&)
  222. {
  223. this->_M_setstate(ios_base::badbit);
  224. __throw_exception_again;
  225. }
  226. __catch(...)
  227. { this->_M_setstate(ios_base::badbit); }
  228. }
  229. if (!_M_gcount)
  230. __err |= ios_base::failbit;
  231. if (__err)
  232. this->setstate(__err);
  233. return *this;
  234. }
  235. template<typename _CharT, typename _Traits>
  236. basic_istream<_CharT, _Traits>&
  237. basic_istream<_CharT, _Traits>::
  238. get(char_type* __s, streamsize __n, char_type __delim)
  239. {
  240. _M_gcount = 0;
  241. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  242. sentry __cerb(*this, true);
  243. if (__cerb)
  244. {
  245. __try
  246. {
  247. const int_type __idelim = traits_type::to_int_type(__delim);
  248. const int_type __eof = traits_type::eof();
  249. __streambuf_type* __sb = this->rdbuf();
  250. int_type __c = __sb->sgetc();
  251. while (_M_gcount + 1 < __n
  252. && !traits_type::eq_int_type(__c, __eof)
  253. && !traits_type::eq_int_type(__c, __idelim))
  254. {
  255. *__s++ = traits_type::to_char_type(__c);
  256. ++_M_gcount;
  257. __c = __sb->snextc();
  258. }
  259. if (traits_type::eq_int_type(__c, __eof))
  260. __err |= ios_base::eofbit;
  261. }
  262. __catch(__cxxabiv1::__forced_unwind&)
  263. {
  264. this->_M_setstate(ios_base::badbit);
  265. __throw_exception_again;
  266. }
  267. __catch(...)
  268. { this->_M_setstate(ios_base::badbit); }
  269. }
  270. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  271. // 243. get and getline when sentry reports failure.
  272. if (__n > 0)
  273. *__s = char_type();
  274. if (!_M_gcount)
  275. __err |= ios_base::failbit;
  276. if (__err)
  277. this->setstate(__err);
  278. return *this;
  279. }
  280. template<typename _CharT, typename _Traits>
  281. basic_istream<_CharT, _Traits>&
  282. basic_istream<_CharT, _Traits>::
  283. get(__streambuf_type& __sb, char_type __delim)
  284. {
  285. _M_gcount = 0;
  286. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  287. sentry __cerb(*this, true);
  288. if (__cerb)
  289. {
  290. __try
  291. {
  292. const int_type __idelim = traits_type::to_int_type(__delim);
  293. const int_type __eof = traits_type::eof();
  294. __streambuf_type* __this_sb = this->rdbuf();
  295. int_type __c = __this_sb->sgetc();
  296. char_type __c2 = traits_type::to_char_type(__c);
  297. while (!traits_type::eq_int_type(__c, __eof)
  298. && !traits_type::eq_int_type(__c, __idelim)
  299. && !traits_type::eq_int_type(__sb.sputc(__c2), __eof))
  300. {
  301. ++_M_gcount;
  302. __c = __this_sb->snextc();
  303. __c2 = traits_type::to_char_type(__c);
  304. }
  305. if (traits_type::eq_int_type(__c, __eof))
  306. __err |= ios_base::eofbit;
  307. }
  308. __catch(__cxxabiv1::__forced_unwind&)
  309. {
  310. this->_M_setstate(ios_base::badbit);
  311. __throw_exception_again;
  312. }
  313. __catch(...)
  314. { this->_M_setstate(ios_base::badbit); }
  315. }
  316. if (!_M_gcount)
  317. __err |= ios_base::failbit;
  318. if (__err)
  319. this->setstate(__err);
  320. return *this;
  321. }
  322. template<typename _CharT, typename _Traits>
  323. basic_istream<_CharT, _Traits>&
  324. basic_istream<_CharT, _Traits>::
  325. getline(char_type* __s, streamsize __n, char_type __delim)
  326. {
  327. _M_gcount = 0;
  328. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  329. sentry __cerb(*this, true);
  330. if (__cerb)
  331. {
  332. __try
  333. {
  334. const int_type __idelim = traits_type::to_int_type(__delim);
  335. const int_type __eof = traits_type::eof();
  336. __streambuf_type* __sb = this->rdbuf();
  337. int_type __c = __sb->sgetc();
  338. while (_M_gcount + 1 < __n
  339. && !traits_type::eq_int_type(__c, __eof)
  340. && !traits_type::eq_int_type(__c, __idelim))
  341. {
  342. *__s++ = traits_type::to_char_type(__c);
  343. __c = __sb->snextc();
  344. ++_M_gcount;
  345. }
  346. if (traits_type::eq_int_type(__c, __eof))
  347. __err |= ios_base::eofbit;
  348. else
  349. {
  350. if (traits_type::eq_int_type(__c, __idelim))
  351. {
  352. __sb->sbumpc();
  353. ++_M_gcount;
  354. }
  355. else
  356. __err |= ios_base::failbit;
  357. }
  358. }
  359. __catch(__cxxabiv1::__forced_unwind&)
  360. {
  361. this->_M_setstate(ios_base::badbit);
  362. __throw_exception_again;
  363. }
  364. __catch(...)
  365. { this->_M_setstate(ios_base::badbit); }
  366. }
  367. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  368. // 243. get and getline when sentry reports failure.
  369. if (__n > 0)
  370. *__s = char_type();
  371. if (!_M_gcount)
  372. __err |= ios_base::failbit;
  373. if (__err)
  374. this->setstate(__err);
  375. return *this;
  376. }
  377. // We provide three overloads, since the first two are much simpler
  378. // than the general case. Also, the latter two can thus adopt the
  379. // same "batchy" strategy used by getline above.
  380. template<typename _CharT, typename _Traits>
  381. basic_istream<_CharT, _Traits>&
  382. basic_istream<_CharT, _Traits>::
  383. ignore(void)
  384. {
  385. _M_gcount = 0;
  386. sentry __cerb(*this, true);
  387. if (__cerb)
  388. {
  389. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  390. __try
  391. {
  392. const int_type __eof = traits_type::eof();
  393. __streambuf_type* __sb = this->rdbuf();
  394. if (traits_type::eq_int_type(__sb->sbumpc(), __eof))
  395. __err |= ios_base::eofbit;
  396. else
  397. _M_gcount = 1;
  398. }
  399. __catch(__cxxabiv1::__forced_unwind&)
  400. {
  401. this->_M_setstate(ios_base::badbit);
  402. __throw_exception_again;
  403. }
  404. __catch(...)
  405. { this->_M_setstate(ios_base::badbit); }
  406. if (__err)
  407. this->setstate(__err);
  408. }
  409. return *this;
  410. }
  411. template<typename _CharT, typename _Traits>
  412. basic_istream<_CharT, _Traits>&
  413. basic_istream<_CharT, _Traits>::
  414. ignore(streamsize __n)
  415. {
  416. _M_gcount = 0;
  417. sentry __cerb(*this, true);
  418. if (__cerb && __n > 0)
  419. {
  420. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  421. __try
  422. {
  423. const int_type __eof = traits_type::eof();
  424. __streambuf_type* __sb = this->rdbuf();
  425. int_type __c = __sb->sgetc();
  426. // N.B. On LFS-enabled platforms streamsize is still 32 bits
  427. // wide: if we want to implement the standard mandated behavior
  428. // for n == max() (see 27.6.1.3/24) we are at risk of signed
  429. // integer overflow: thus these contortions. Also note that,
  430. // by definition, when more than 2G chars are actually ignored,
  431. // _M_gcount (the return value of gcount, that is) cannot be
  432. // really correct, being unavoidably too small.
  433. bool __large_ignore = false;
  434. while (true)
  435. {
  436. while (_M_gcount < __n
  437. && !traits_type::eq_int_type(__c, __eof))
  438. {
  439. ++_M_gcount;
  440. __c = __sb->snextc();
  441. }
  442. if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
  443. && !traits_type::eq_int_type(__c, __eof))
  444. {
  445. _M_gcount =
  446. __gnu_cxx::__numeric_traits<streamsize>::__min;
  447. __large_ignore = true;
  448. }
  449. else
  450. break;
  451. }
  452. if (__large_ignore)
  453. _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
  454. if (traits_type::eq_int_type(__c, __eof))
  455. __err |= ios_base::eofbit;
  456. }
  457. __catch(__cxxabiv1::__forced_unwind&)
  458. {
  459. this->_M_setstate(ios_base::badbit);
  460. __throw_exception_again;
  461. }
  462. __catch(...)
  463. { this->_M_setstate(ios_base::badbit); }
  464. if (__err)
  465. this->setstate(__err);
  466. }
  467. return *this;
  468. }
  469. template<typename _CharT, typename _Traits>
  470. basic_istream<_CharT, _Traits>&
  471. basic_istream<_CharT, _Traits>::
  472. ignore(streamsize __n, int_type __delim)
  473. {
  474. _M_gcount = 0;
  475. sentry __cerb(*this, true);
  476. if (__cerb && __n > 0)
  477. {
  478. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  479. __try
  480. {
  481. const int_type __eof = traits_type::eof();
  482. __streambuf_type* __sb = this->rdbuf();
  483. int_type __c = __sb->sgetc();
  484. // See comment above.
  485. bool __large_ignore = false;
  486. while (true)
  487. {
  488. while (_M_gcount < __n
  489. && !traits_type::eq_int_type(__c, __eof)
  490. && !traits_type::eq_int_type(__c, __delim))
  491. {
  492. ++_M_gcount;
  493. __c = __sb->snextc();
  494. }
  495. if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
  496. && !traits_type::eq_int_type(__c, __eof)
  497. && !traits_type::eq_int_type(__c, __delim))
  498. {
  499. _M_gcount =
  500. __gnu_cxx::__numeric_traits<streamsize>::__min;
  501. __large_ignore = true;
  502. }
  503. else
  504. break;
  505. }
  506. if (__large_ignore)
  507. _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
  508. if (traits_type::eq_int_type(__c, __eof))
  509. __err |= ios_base::eofbit;
  510. else if (traits_type::eq_int_type(__c, __delim))
  511. {
  512. if (_M_gcount
  513. < __gnu_cxx::__numeric_traits<streamsize>::__max)
  514. ++_M_gcount;
  515. __sb->sbumpc();
  516. }
  517. }
  518. __catch(__cxxabiv1::__forced_unwind&)
  519. {
  520. this->_M_setstate(ios_base::badbit);
  521. __throw_exception_again;
  522. }
  523. __catch(...)
  524. { this->_M_setstate(ios_base::badbit); }
  525. if (__err)
  526. this->setstate(__err);
  527. }
  528. return *this;
  529. }
  530. template<typename _CharT, typename _Traits>
  531. typename basic_istream<_CharT, _Traits>::int_type
  532. basic_istream<_CharT, _Traits>::
  533. peek(void)
  534. {
  535. int_type __c = traits_type::eof();
  536. _M_gcount = 0;
  537. sentry __cerb(*this, true);
  538. if (__cerb)
  539. {
  540. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  541. __try
  542. {
  543. __c = this->rdbuf()->sgetc();
  544. if (traits_type::eq_int_type(__c, traits_type::eof()))
  545. __err |= ios_base::eofbit;
  546. }
  547. __catch(__cxxabiv1::__forced_unwind&)
  548. {
  549. this->_M_setstate(ios_base::badbit);
  550. __throw_exception_again;
  551. }
  552. __catch(...)
  553. { this->_M_setstate(ios_base::badbit); }
  554. if (__err)
  555. this->setstate(__err);
  556. }
  557. return __c;
  558. }
  559. template<typename _CharT, typename _Traits>
  560. basic_istream<_CharT, _Traits>&
  561. basic_istream<_CharT, _Traits>::
  562. read(char_type* __s, streamsize __n)
  563. {
  564. _M_gcount = 0;
  565. sentry __cerb(*this, true);
  566. if (__cerb)
  567. {
  568. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  569. __try
  570. {
  571. _M_gcount = this->rdbuf()->sgetn(__s, __n);
  572. if (_M_gcount != __n)
  573. __err |= (ios_base::eofbit | ios_base::failbit);
  574. }
  575. __catch(__cxxabiv1::__forced_unwind&)
  576. {
  577. this->_M_setstate(ios_base::badbit);
  578. __throw_exception_again;
  579. }
  580. __catch(...)
  581. { this->_M_setstate(ios_base::badbit); }
  582. if (__err)
  583. this->setstate(__err);
  584. }
  585. return *this;
  586. }
  587. template<typename _CharT, typename _Traits>
  588. streamsize
  589. basic_istream<_CharT, _Traits>::
  590. readsome(char_type* __s, streamsize __n)
  591. {
  592. _M_gcount = 0;
  593. sentry __cerb(*this, true);
  594. if (__cerb)
  595. {
  596. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  597. __try
  598. {
  599. // Cannot compare int_type with streamsize generically.
  600. const streamsize __num = this->rdbuf()->in_avail();
  601. if (__num > 0)
  602. _M_gcount = this->rdbuf()->sgetn(__s, std::min(__num, __n));
  603. else if (__num == -1)
  604. __err |= ios_base::eofbit;
  605. }
  606. __catch(__cxxabiv1::__forced_unwind&)
  607. {
  608. this->_M_setstate(ios_base::badbit);
  609. __throw_exception_again;
  610. }
  611. __catch(...)
  612. { this->_M_setstate(ios_base::badbit); }
  613. if (__err)
  614. this->setstate(__err);
  615. }
  616. return _M_gcount;
  617. }
  618. template<typename _CharT, typename _Traits>
  619. basic_istream<_CharT, _Traits>&
  620. basic_istream<_CharT, _Traits>::
  621. putback(char_type __c)
  622. {
  623. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  624. // 60. What is a formatted input function?
  625. _M_gcount = 0;
  626. sentry __cerb(*this, true);
  627. if (__cerb)
  628. {
  629. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  630. __try
  631. {
  632. const int_type __eof = traits_type::eof();
  633. __streambuf_type* __sb = this->rdbuf();
  634. if (!__sb
  635. || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
  636. __err |= ios_base::badbit;
  637. }
  638. __catch(__cxxabiv1::__forced_unwind&)
  639. {
  640. this->_M_setstate(ios_base::badbit);
  641. __throw_exception_again;
  642. }
  643. __catch(...)
  644. { this->_M_setstate(ios_base::badbit); }
  645. if (__err)
  646. this->setstate(__err);
  647. }
  648. return *this;
  649. }
  650. template<typename _CharT, typename _Traits>
  651. basic_istream<_CharT, _Traits>&
  652. basic_istream<_CharT, _Traits>::
  653. unget(void)
  654. {
  655. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  656. // 60. What is a formatted input function?
  657. _M_gcount = 0;
  658. sentry __cerb(*this, true);
  659. if (__cerb)
  660. {
  661. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  662. __try
  663. {
  664. const int_type __eof = traits_type::eof();
  665. __streambuf_type* __sb = this->rdbuf();
  666. if (!__sb
  667. || traits_type::eq_int_type(__sb->sungetc(), __eof))
  668. __err |= ios_base::badbit;
  669. }
  670. __catch(__cxxabiv1::__forced_unwind&)
  671. {
  672. this->_M_setstate(ios_base::badbit);
  673. __throw_exception_again;
  674. }
  675. __catch(...)
  676. { this->_M_setstate(ios_base::badbit); }
  677. if (__err)
  678. this->setstate(__err);
  679. }
  680. return *this;
  681. }
  682. template<typename _CharT, typename _Traits>
  683. int
  684. basic_istream<_CharT, _Traits>::
  685. sync(void)
  686. {
  687. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  688. // DR60. Do not change _M_gcount.
  689. int __ret = -1;
  690. sentry __cerb(*this, true);
  691. if (__cerb)
  692. {
  693. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  694. __try
  695. {
  696. __streambuf_type* __sb = this->rdbuf();
  697. if (__sb)
  698. {
  699. if (__sb->pubsync() == -1)
  700. __err |= ios_base::badbit;
  701. else
  702. __ret = 0;
  703. }
  704. }
  705. __catch(__cxxabiv1::__forced_unwind&)
  706. {
  707. this->_M_setstate(ios_base::badbit);
  708. __throw_exception_again;
  709. }
  710. __catch(...)
  711. { this->_M_setstate(ios_base::badbit); }
  712. if (__err)
  713. this->setstate(__err);
  714. }
  715. return __ret;
  716. }
  717. template<typename _CharT, typename _Traits>
  718. typename basic_istream<_CharT, _Traits>::pos_type
  719. basic_istream<_CharT, _Traits>::
  720. tellg(void)
  721. {
  722. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  723. // DR60. Do not change _M_gcount.
  724. pos_type __ret = pos_type(-1);
  725. __try
  726. {
  727. if (!this->fail())
  728. __ret = this->rdbuf()->pubseekoff(0, ios_base::cur,
  729. ios_base::in);
  730. }
  731. __catch(__cxxabiv1::__forced_unwind&)
  732. {
  733. this->_M_setstate(ios_base::badbit);
  734. __throw_exception_again;
  735. }
  736. __catch(...)
  737. { this->_M_setstate(ios_base::badbit); }
  738. return __ret;
  739. }
  740. template<typename _CharT, typename _Traits>
  741. basic_istream<_CharT, _Traits>&
  742. basic_istream<_CharT, _Traits>::
  743. seekg(pos_type __pos)
  744. {
  745. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  746. // DR60. Do not change _M_gcount.
  747. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  748. __try
  749. {
  750. if (!this->fail())
  751. {
  752. // 136. seekp, seekg setting wrong streams?
  753. const pos_type __p = this->rdbuf()->pubseekpos(__pos,
  754. ios_base::in);
  755. // 129. Need error indication from seekp() and seekg()
  756. if (__p == pos_type(off_type(-1)))
  757. __err |= ios_base::failbit;
  758. }
  759. }
  760. __catch(__cxxabiv1::__forced_unwind&)
  761. {
  762. this->_M_setstate(ios_base::badbit);
  763. __throw_exception_again;
  764. }
  765. __catch(...)
  766. { this->_M_setstate(ios_base::badbit); }
  767. if (__err)
  768. this->setstate(__err);
  769. return *this;
  770. }
  771. template<typename _CharT, typename _Traits>
  772. basic_istream<_CharT, _Traits>&
  773. basic_istream<_CharT, _Traits>::
  774. seekg(off_type __off, ios_base::seekdir __dir)
  775. {
  776. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  777. // DR60. Do not change _M_gcount.
  778. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  779. __try
  780. {
  781. if (!this->fail())
  782. {
  783. // 136. seekp, seekg setting wrong streams?
  784. const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
  785. ios_base::in);
  786. // 129. Need error indication from seekp() and seekg()
  787. if (__p == pos_type(off_type(-1)))
  788. __err |= ios_base::failbit;
  789. }
  790. }
  791. __catch(__cxxabiv1::__forced_unwind&)
  792. {
  793. this->_M_setstate(ios_base::badbit);
  794. __throw_exception_again;
  795. }
  796. __catch(...)
  797. { this->_M_setstate(ios_base::badbit); }
  798. if (__err)
  799. this->setstate(__err);
  800. return *this;
  801. }
  802. // 27.6.1.2.3 Character extraction templates
  803. template<typename _CharT, typename _Traits>
  804. basic_istream<_CharT, _Traits>&
  805. operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
  806. {
  807. typedef basic_istream<_CharT, _Traits> __istream_type;
  808. typedef typename __istream_type::int_type __int_type;
  809. typename __istream_type::sentry __cerb(__in, false);
  810. if (__cerb)
  811. {
  812. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  813. __try
  814. {
  815. const __int_type __cb = __in.rdbuf()->sbumpc();
  816. if (!_Traits::eq_int_type(__cb, _Traits::eof()))
  817. __c = _Traits::to_char_type(__cb);
  818. else
  819. __err |= (ios_base::eofbit | ios_base::failbit);
  820. }
  821. __catch(__cxxabiv1::__forced_unwind&)
  822. {
  823. __in._M_setstate(ios_base::badbit);
  824. __throw_exception_again;
  825. }
  826. __catch(...)
  827. { __in._M_setstate(ios_base::badbit); }
  828. if (__err)
  829. __in.setstate(__err);
  830. }
  831. return __in;
  832. }
  833. template<typename _CharT, typename _Traits>
  834. basic_istream<_CharT, _Traits>&
  835. operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
  836. {
  837. typedef basic_istream<_CharT, _Traits> __istream_type;
  838. typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
  839. typedef typename _Traits::int_type int_type;
  840. typedef _CharT char_type;
  841. typedef ctype<_CharT> __ctype_type;
  842. streamsize __extracted = 0;
  843. ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
  844. typename __istream_type::sentry __cerb(__in, false);
  845. if (__cerb)
  846. {
  847. __try
  848. {
  849. // Figure out how many characters to extract.
  850. streamsize __num = __in.width();
  851. if (__num <= 0)
  852. __num = __gnu_cxx::__numeric_traits<streamsize>::__max;
  853. const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
  854. const int_type __eof = _Traits::eof();
  855. __streambuf_type* __sb = __in.rdbuf();
  856. int_type __c = __sb->sgetc();
  857. while (__extracted < __num - 1
  858. && !_Traits::eq_int_type(__c, __eof)
  859. && !__ct.is(ctype_base::space,
  860. _Traits::to_char_type(__c)))
  861. {
  862. *__s++ = _Traits::to_char_type(__c);
  863. ++__extracted;
  864. __c = __sb->snextc();
  865. }
  866. if (_Traits::eq_int_type(__c, __eof))
  867. __err |= ios_base::eofbit;
  868. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  869. // 68. Extractors for char* should store null at end
  870. *__s = char_type();
  871. __in.width(0);
  872. }
  873. __catch(__cxxabiv1::__forced_unwind&)
  874. {
  875. __in._M_setstate(ios_base::badbit);
  876. __throw_exception_again;
  877. }
  878. __catch(...)
  879. { __in._M_setstate(ios_base::badbit); }
  880. }
  881. if (!__extracted)
  882. __err |= ios_base::failbit;
  883. if (__err)
  884. __in.setstate(__err);
  885. return __in;
  886. }
  887. // 27.6.1.4 Standard basic_istream manipulators
  888. template<typename _CharT, typename _Traits>
  889. basic_istream<_CharT, _Traits>&
  890. ws(basic_istream<_CharT, _Traits>& __in)
  891. {
  892. typedef basic_istream<_CharT, _Traits> __istream_type;
  893. typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
  894. typedef typename __istream_type::int_type __int_type;
  895. typedef ctype<_CharT> __ctype_type;
  896. const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
  897. const __int_type __eof = _Traits::eof();
  898. __streambuf_type* __sb = __in.rdbuf();
  899. __int_type __c = __sb->sgetc();
  900. while (!_Traits::eq_int_type(__c, __eof)
  901. && __ct.is(ctype_base::space, _Traits::to_char_type(__c)))
  902. __c = __sb->snextc();
  903. if (_Traits::eq_int_type(__c, __eof))
  904. __in.setstate(ios_base::eofbit);
  905. return __in;
  906. }
  907. // Inhibit implicit instantiations for required instantiations,
  908. // which are defined via explicit instantiations elsewhere.
  909. // NB: This syntax is a GNU extension.
  910. #if _GLIBCXX_EXTERN_TEMPLATE
  911. extern template class basic_istream<char>;
  912. extern template istream& ws(istream&);
  913. extern template istream& operator>>(istream&, char&);
  914. extern template istream& operator>>(istream&, char*);
  915. extern template istream& operator>>(istream&, unsigned char&);
  916. extern template istream& operator>>(istream&, signed char&);
  917. extern template istream& operator>>(istream&, unsigned char*);
  918. extern template istream& operator>>(istream&, signed char*);
  919. extern template istream& istream::_M_extract(unsigned short&);
  920. extern template istream& istream::_M_extract(unsigned int&);
  921. extern template istream& istream::_M_extract(long&);
  922. extern template istream& istream::_M_extract(unsigned long&);
  923. extern template istream& istream::_M_extract(bool&);
  924. #ifdef _GLIBCXX_USE_LONG_LONG
  925. extern template istream& istream::_M_extract(long long&);
  926. extern template istream& istream::_M_extract(unsigned long long&);
  927. #endif
  928. extern template istream& istream::_M_extract(float&);
  929. extern template istream& istream::_M_extract(double&);
  930. extern template istream& istream::_M_extract(long double&);
  931. extern template istream& istream::_M_extract(void*&);
  932. extern template class basic_iostream<char>;
  933. #ifdef _GLIBCXX_USE_WCHAR_T
  934. extern template class basic_istream<wchar_t>;
  935. extern template wistream& ws(wistream&);
  936. extern template wistream& operator>>(wistream&, wchar_t&);
  937. extern template wistream& operator>>(wistream&, wchar_t*);
  938. extern template wistream& wistream::_M_extract(unsigned short&);
  939. extern template wistream& wistream::_M_extract(unsigned int&);
  940. extern template wistream& wistream::_M_extract(long&);
  941. extern template wistream& wistream::_M_extract(unsigned long&);
  942. extern template wistream& wistream::_M_extract(bool&);
  943. #ifdef _GLIBCXX_USE_LONG_LONG
  944. extern template wistream& wistream::_M_extract(long long&);
  945. extern template wistream& wistream::_M_extract(unsigned long long&);
  946. #endif
  947. extern template wistream& wistream::_M_extract(float&);
  948. extern template wistream& wistream::_M_extract(double&);
  949. extern template wistream& wistream::_M_extract(long double&);
  950. extern template wistream& wistream::_M_extract(void*&);
  951. extern template class basic_iostream<wchar_t>;
  952. #endif
  953. #endif
  954. _GLIBCXX_END_NAMESPACE
  955. #endif