istream.tcc 31 KB

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