istream.tcc 30 KB

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