istream.tcc 31 KB

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