Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

1101 lines
30KB

  1. // istream classes -*- C++ -*-
  2. // Copyright (C) 1997-2020 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. while (!traits_type::eq_int_type(__c, __eof)
  354. && !traits_type::eq_int_type(__c, __idelim)
  355. && !traits_type::eq_int_type(__sb.sputc(__c2), __eof))
  356. {
  357. ++_M_gcount;
  358. __c = __this_sb->snextc();
  359. __c2 = traits_type::to_char_type(__c);
  360. }
  361. if (traits_type::eq_int_type(__c, __eof))
  362. __err |= ios_base::eofbit;
  363. }
  364. __catch(__cxxabiv1::__forced_unwind&)
  365. {
  366. this->_M_setstate(ios_base::badbit);
  367. __throw_exception_again;
  368. }
  369. __catch(...)
  370. { this->_M_setstate(ios_base::badbit); }
  371. }
  372. if (!_M_gcount)
  373. __err |= ios_base::failbit;
  374. if (__err)
  375. this->setstate(__err);
  376. return *this;
  377. }
  378. template<typename _CharT, typename _Traits>
  379. basic_istream<_CharT, _Traits>&
  380. basic_istream<_CharT, _Traits>::
  381. getline(char_type* __s, streamsize __n, char_type __delim)
  382. {
  383. _M_gcount = 0;
  384. ios_base::iostate __err = ios_base::goodbit;
  385. sentry __cerb(*this, true);
  386. if (__cerb)
  387. {
  388. __try
  389. {
  390. const int_type __idelim = traits_type::to_int_type(__delim);
  391. const int_type __eof = traits_type::eof();
  392. __streambuf_type* __sb = this->rdbuf();
  393. int_type __c = __sb->sgetc();
  394. while (_M_gcount + 1 < __n
  395. && !traits_type::eq_int_type(__c, __eof)
  396. && !traits_type::eq_int_type(__c, __idelim))
  397. {
  398. *__s++ = traits_type::to_char_type(__c);
  399. __c = __sb->snextc();
  400. ++_M_gcount;
  401. }
  402. if (traits_type::eq_int_type(__c, __eof))
  403. __err |= ios_base::eofbit;
  404. else
  405. {
  406. if (traits_type::eq_int_type(__c, __idelim))
  407. {
  408. __sb->sbumpc();
  409. ++_M_gcount;
  410. }
  411. else
  412. __err |= ios_base::failbit;
  413. }
  414. }
  415. __catch(__cxxabiv1::__forced_unwind&)
  416. {
  417. this->_M_setstate(ios_base::badbit);
  418. __throw_exception_again;
  419. }
  420. __catch(...)
  421. { this->_M_setstate(ios_base::badbit); }
  422. }
  423. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  424. // 243. get and getline when sentry reports failure.
  425. if (__n > 0)
  426. *__s = char_type();
  427. if (!_M_gcount)
  428. __err |= ios_base::failbit;
  429. if (__err)
  430. this->setstate(__err);
  431. return *this;
  432. }
  433. // We provide three overloads, since the first two are much simpler
  434. // than the general case. Also, the latter two can thus adopt the
  435. // same "batchy" strategy used by getline above.
  436. template<typename _CharT, typename _Traits>
  437. basic_istream<_CharT, _Traits>&
  438. basic_istream<_CharT, _Traits>::
  439. ignore(void)
  440. {
  441. _M_gcount = 0;
  442. sentry __cerb(*this, true);
  443. if (__cerb)
  444. {
  445. ios_base::iostate __err = ios_base::goodbit;
  446. __try
  447. {
  448. const int_type __eof = traits_type::eof();
  449. __streambuf_type* __sb = this->rdbuf();
  450. if (traits_type::eq_int_type(__sb->sbumpc(), __eof))
  451. __err |= ios_base::eofbit;
  452. else
  453. _M_gcount = 1;
  454. }
  455. __catch(__cxxabiv1::__forced_unwind&)
  456. {
  457. this->_M_setstate(ios_base::badbit);
  458. __throw_exception_again;
  459. }
  460. __catch(...)
  461. { this->_M_setstate(ios_base::badbit); }
  462. if (__err)
  463. this->setstate(__err);
  464. }
  465. return *this;
  466. }
  467. template<typename _CharT, typename _Traits>
  468. basic_istream<_CharT, _Traits>&
  469. basic_istream<_CharT, _Traits>::
  470. ignore(streamsize __n)
  471. {
  472. _M_gcount = 0;
  473. sentry __cerb(*this, true);
  474. if (__cerb && __n > 0)
  475. {
  476. ios_base::iostate __err = ios_base::goodbit;
  477. __try
  478. {
  479. const int_type __eof = traits_type::eof();
  480. __streambuf_type* __sb = this->rdbuf();
  481. int_type __c = __sb->sgetc();
  482. // N.B. On LFS-enabled platforms streamsize is still 32 bits
  483. // wide: if we want to implement the standard mandated behavior
  484. // for n == max() (see 27.6.1.3/24) we are at risk of signed
  485. // integer overflow: thus these contortions. Also note that,
  486. // by definition, when more than 2G chars are actually ignored,
  487. // _M_gcount (the return value of gcount, that is) cannot be
  488. // really correct, being unavoidably too small.
  489. bool __large_ignore = false;
  490. while (true)
  491. {
  492. while (_M_gcount < __n
  493. && !traits_type::eq_int_type(__c, __eof))
  494. {
  495. ++_M_gcount;
  496. __c = __sb->snextc();
  497. }
  498. if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
  499. && !traits_type::eq_int_type(__c, __eof))
  500. {
  501. _M_gcount =
  502. __gnu_cxx::__numeric_traits<streamsize>::__min;
  503. __large_ignore = true;
  504. }
  505. else
  506. break;
  507. }
  508. if (__large_ignore)
  509. _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
  510. if (traits_type::eq_int_type(__c, __eof))
  511. __err |= ios_base::eofbit;
  512. }
  513. __catch(__cxxabiv1::__forced_unwind&)
  514. {
  515. this->_M_setstate(ios_base::badbit);
  516. __throw_exception_again;
  517. }
  518. __catch(...)
  519. { this->_M_setstate(ios_base::badbit); }
  520. if (__err)
  521. this->setstate(__err);
  522. }
  523. return *this;
  524. }
  525. template<typename _CharT, typename _Traits>
  526. basic_istream<_CharT, _Traits>&
  527. basic_istream<_CharT, _Traits>::
  528. ignore(streamsize __n, int_type __delim)
  529. {
  530. _M_gcount = 0;
  531. sentry __cerb(*this, true);
  532. if (__cerb && __n > 0)
  533. {
  534. ios_base::iostate __err = ios_base::goodbit;
  535. __try
  536. {
  537. const int_type __eof = traits_type::eof();
  538. __streambuf_type* __sb = this->rdbuf();
  539. int_type __c = __sb->sgetc();
  540. // See comment above.
  541. bool __large_ignore = false;
  542. while (true)
  543. {
  544. while (_M_gcount < __n
  545. && !traits_type::eq_int_type(__c, __eof)
  546. && !traits_type::eq_int_type(__c, __delim))
  547. {
  548. ++_M_gcount;
  549. __c = __sb->snextc();
  550. }
  551. if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max
  552. && !traits_type::eq_int_type(__c, __eof)
  553. && !traits_type::eq_int_type(__c, __delim))
  554. {
  555. _M_gcount =
  556. __gnu_cxx::__numeric_traits<streamsize>::__min;
  557. __large_ignore = true;
  558. }
  559. else
  560. break;
  561. }
  562. if (__large_ignore)
  563. _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max;
  564. if (traits_type::eq_int_type(__c, __eof))
  565. __err |= ios_base::eofbit;
  566. else if (traits_type::eq_int_type(__c, __delim))
  567. {
  568. if (_M_gcount
  569. < __gnu_cxx::__numeric_traits<streamsize>::__max)
  570. ++_M_gcount;
  571. __sb->sbumpc();
  572. }
  573. }
  574. __catch(__cxxabiv1::__forced_unwind&)
  575. {
  576. this->_M_setstate(ios_base::badbit);
  577. __throw_exception_again;
  578. }
  579. __catch(...)
  580. { this->_M_setstate(ios_base::badbit); }
  581. if (__err)
  582. this->setstate(__err);
  583. }
  584. return *this;
  585. }
  586. template<typename _CharT, typename _Traits>
  587. typename basic_istream<_CharT, _Traits>::int_type
  588. basic_istream<_CharT, _Traits>::
  589. peek(void)
  590. {
  591. int_type __c = traits_type::eof();
  592. _M_gcount = 0;
  593. sentry __cerb(*this, true);
  594. if (__cerb)
  595. {
  596. ios_base::iostate __err = ios_base::goodbit;
  597. __try
  598. {
  599. __c = this->rdbuf()->sgetc();
  600. if (traits_type::eq_int_type(__c, traits_type::eof()))
  601. __err |= ios_base::eofbit;
  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 __c;
  614. }
  615. template<typename _CharT, typename _Traits>
  616. basic_istream<_CharT, _Traits>&
  617. basic_istream<_CharT, _Traits>::
  618. read(char_type* __s, streamsize __n)
  619. {
  620. _M_gcount = 0;
  621. sentry __cerb(*this, true);
  622. if (__cerb)
  623. {
  624. ios_base::iostate __err = ios_base::goodbit;
  625. __try
  626. {
  627. _M_gcount = this->rdbuf()->sgetn(__s, __n);
  628. if (_M_gcount != __n)
  629. __err |= (ios_base::eofbit | ios_base::failbit);
  630. }
  631. __catch(__cxxabiv1::__forced_unwind&)
  632. {
  633. this->_M_setstate(ios_base::badbit);
  634. __throw_exception_again;
  635. }
  636. __catch(...)
  637. { this->_M_setstate(ios_base::badbit); }
  638. if (__err)
  639. this->setstate(__err);
  640. }
  641. return *this;
  642. }
  643. template<typename _CharT, typename _Traits>
  644. streamsize
  645. basic_istream<_CharT, _Traits>::
  646. readsome(char_type* __s, streamsize __n)
  647. {
  648. _M_gcount = 0;
  649. sentry __cerb(*this, true);
  650. if (__cerb)
  651. {
  652. ios_base::iostate __err = ios_base::goodbit;
  653. __try
  654. {
  655. // Cannot compare int_type with streamsize generically.
  656. const streamsize __num = this->rdbuf()->in_avail();
  657. if (__num > 0)
  658. _M_gcount = this->rdbuf()->sgetn(__s, std::min(__num, __n));
  659. else if (__num == -1)
  660. __err |= ios_base::eofbit;
  661. }
  662. __catch(__cxxabiv1::__forced_unwind&)
  663. {
  664. this->_M_setstate(ios_base::badbit);
  665. __throw_exception_again;
  666. }
  667. __catch(...)
  668. { this->_M_setstate(ios_base::badbit); }
  669. if (__err)
  670. this->setstate(__err);
  671. }
  672. return _M_gcount;
  673. }
  674. template<typename _CharT, typename _Traits>
  675. basic_istream<_CharT, _Traits>&
  676. basic_istream<_CharT, _Traits>::
  677. putback(char_type __c)
  678. {
  679. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  680. // 60. What is a formatted input function?
  681. _M_gcount = 0;
  682. // Clear eofbit per N3168.
  683. this->clear(this->rdstate() & ~ios_base::eofbit);
  684. sentry __cerb(*this, true);
  685. if (__cerb)
  686. {
  687. ios_base::iostate __err = ios_base::goodbit;
  688. __try
  689. {
  690. const int_type __eof = traits_type::eof();
  691. __streambuf_type* __sb = this->rdbuf();
  692. if (!__sb
  693. || traits_type::eq_int_type(__sb->sputbackc(__c), __eof))
  694. __err |= ios_base::badbit;
  695. }
  696. __catch(__cxxabiv1::__forced_unwind&)
  697. {
  698. this->_M_setstate(ios_base::badbit);
  699. __throw_exception_again;
  700. }
  701. __catch(...)
  702. { this->_M_setstate(ios_base::badbit); }
  703. if (__err)
  704. this->setstate(__err);
  705. }
  706. return *this;
  707. }
  708. template<typename _CharT, typename _Traits>
  709. basic_istream<_CharT, _Traits>&
  710. basic_istream<_CharT, _Traits>::
  711. unget(void)
  712. {
  713. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  714. // 60. What is a formatted input function?
  715. _M_gcount = 0;
  716. // Clear eofbit per N3168.
  717. this->clear(this->rdstate() & ~ios_base::eofbit);
  718. sentry __cerb(*this, true);
  719. if (__cerb)
  720. {
  721. ios_base::iostate __err = ios_base::goodbit;
  722. __try
  723. {
  724. const int_type __eof = traits_type::eof();
  725. __streambuf_type* __sb = this->rdbuf();
  726. if (!__sb
  727. || traits_type::eq_int_type(__sb->sungetc(), __eof))
  728. __err |= ios_base::badbit;
  729. }
  730. __catch(__cxxabiv1::__forced_unwind&)
  731. {
  732. this->_M_setstate(ios_base::badbit);
  733. __throw_exception_again;
  734. }
  735. __catch(...)
  736. { this->_M_setstate(ios_base::badbit); }
  737. if (__err)
  738. this->setstate(__err);
  739. }
  740. return *this;
  741. }
  742. template<typename _CharT, typename _Traits>
  743. int
  744. basic_istream<_CharT, _Traits>::
  745. sync(void)
  746. {
  747. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  748. // DR60. Do not change _M_gcount.
  749. int __ret = -1;
  750. sentry __cerb(*this, true);
  751. if (__cerb)
  752. {
  753. ios_base::iostate __err = ios_base::goodbit;
  754. __try
  755. {
  756. __streambuf_type* __sb = this->rdbuf();
  757. if (__sb)
  758. {
  759. if (__sb->pubsync() == -1)
  760. __err |= ios_base::badbit;
  761. else
  762. __ret = 0;
  763. }
  764. }
  765. __catch(__cxxabiv1::__forced_unwind&)
  766. {
  767. this->_M_setstate(ios_base::badbit);
  768. __throw_exception_again;
  769. }
  770. __catch(...)
  771. { this->_M_setstate(ios_base::badbit); }
  772. if (__err)
  773. this->setstate(__err);
  774. }
  775. return __ret;
  776. }
  777. template<typename _CharT, typename _Traits>
  778. typename basic_istream<_CharT, _Traits>::pos_type
  779. basic_istream<_CharT, _Traits>::
  780. tellg(void)
  781. {
  782. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  783. // DR60. Do not change _M_gcount.
  784. pos_type __ret = pos_type(-1);
  785. sentry __cerb(*this, true);
  786. if (__cerb)
  787. {
  788. __try
  789. {
  790. if (!this->fail())
  791. __ret = this->rdbuf()->pubseekoff(0, ios_base::cur,
  792. ios_base::in);
  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. }
  802. return __ret;
  803. }
  804. template<typename _CharT, typename _Traits>
  805. basic_istream<_CharT, _Traits>&
  806. basic_istream<_CharT, _Traits>::
  807. seekg(pos_type __pos)
  808. {
  809. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  810. // DR60. Do not change _M_gcount.
  811. // Clear eofbit per N3168.
  812. this->clear(this->rdstate() & ~ios_base::eofbit);
  813. sentry __cerb(*this, true);
  814. if (__cerb)
  815. {
  816. ios_base::iostate __err = ios_base::goodbit;
  817. __try
  818. {
  819. if (!this->fail())
  820. {
  821. // 136. seekp, seekg setting wrong streams?
  822. const pos_type __p = this->rdbuf()->pubseekpos(__pos,
  823. ios_base::in);
  824. // 129. Need error indication from seekp() and seekg()
  825. if (__p == pos_type(off_type(-1)))
  826. __err |= ios_base::failbit;
  827. }
  828. }
  829. __catch(__cxxabiv1::__forced_unwind&)
  830. {
  831. this->_M_setstate(ios_base::badbit);
  832. __throw_exception_again;
  833. }
  834. __catch(...)
  835. { this->_M_setstate(ios_base::badbit); }
  836. if (__err)
  837. this->setstate(__err);
  838. }
  839. return *this;
  840. }
  841. template<typename _CharT, typename _Traits>
  842. basic_istream<_CharT, _Traits>&
  843. basic_istream<_CharT, _Traits>::
  844. seekg(off_type __off, ios_base::seekdir __dir)
  845. {
  846. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  847. // DR60. Do not change _M_gcount.
  848. // Clear eofbit per N3168.
  849. this->clear(this->rdstate() & ~ios_base::eofbit);
  850. sentry __cerb(*this, true);
  851. if (__cerb)
  852. {
  853. ios_base::iostate __err = ios_base::goodbit;
  854. __try
  855. {
  856. if (!this->fail())
  857. {
  858. // 136. seekp, seekg setting wrong streams?
  859. const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
  860. ios_base::in);
  861. // 129. Need error indication from seekp() and seekg()
  862. if (__p == pos_type(off_type(-1)))
  863. __err |= ios_base::failbit;
  864. }
  865. }
  866. __catch(__cxxabiv1::__forced_unwind&)
  867. {
  868. this->_M_setstate(ios_base::badbit);
  869. __throw_exception_again;
  870. }
  871. __catch(...)
  872. { this->_M_setstate(ios_base::badbit); }
  873. if (__err)
  874. this->setstate(__err);
  875. }
  876. return *this;
  877. }
  878. // 27.6.1.2.3 Character extraction templates
  879. template<typename _CharT, typename _Traits>
  880. basic_istream<_CharT, _Traits>&
  881. operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
  882. {
  883. typedef basic_istream<_CharT, _Traits> __istream_type;
  884. typedef typename __istream_type::int_type __int_type;
  885. typename __istream_type::sentry __cerb(__in, false);
  886. if (__cerb)
  887. {
  888. ios_base::iostate __err = ios_base::goodbit;
  889. __try
  890. {
  891. const __int_type __cb = __in.rdbuf()->sbumpc();
  892. if (!_Traits::eq_int_type(__cb, _Traits::eof()))
  893. __c = _Traits::to_char_type(__cb);
  894. else
  895. __err |= (ios_base::eofbit | ios_base::failbit);
  896. }
  897. __catch(__cxxabiv1::__forced_unwind&)
  898. {
  899. __in._M_setstate(ios_base::badbit);
  900. __throw_exception_again;
  901. }
  902. __catch(...)
  903. { __in._M_setstate(ios_base::badbit); }
  904. if (__err)
  905. __in.setstate(__err);
  906. }
  907. return __in;
  908. }
  909. template<typename _CharT, typename _Traits>
  910. basic_istream<_CharT, _Traits>&
  911. operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
  912. {
  913. typedef basic_istream<_CharT, _Traits> __istream_type;
  914. typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
  915. typedef typename _Traits::int_type int_type;
  916. typedef _CharT char_type;
  917. typedef ctype<_CharT> __ctype_type;
  918. streamsize __extracted = 0;
  919. ios_base::iostate __err = ios_base::goodbit;
  920. typename __istream_type::sentry __cerb(__in, false);
  921. if (__cerb)
  922. {
  923. __try
  924. {
  925. // Figure out how many characters to extract.
  926. streamsize __num = __in.width();
  927. if (__num <= 0)
  928. __num = __gnu_cxx::__numeric_traits<streamsize>::__max;
  929. const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
  930. const int_type __eof = _Traits::eof();
  931. __streambuf_type* __sb = __in.rdbuf();
  932. int_type __c = __sb->sgetc();
  933. while (__extracted < __num - 1
  934. && !_Traits::eq_int_type(__c, __eof)
  935. && !__ct.is(ctype_base::space,
  936. _Traits::to_char_type(__c)))
  937. {
  938. *__s++ = _Traits::to_char_type(__c);
  939. ++__extracted;
  940. __c = __sb->snextc();
  941. }
  942. if (_Traits::eq_int_type(__c, __eof))
  943. __err |= ios_base::eofbit;
  944. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  945. // 68. Extractors for char* should store null at end
  946. *__s = char_type();
  947. __in.width(0);
  948. }
  949. __catch(__cxxabiv1::__forced_unwind&)
  950. {
  951. __in._M_setstate(ios_base::badbit);
  952. __throw_exception_again;
  953. }
  954. __catch(...)
  955. { __in._M_setstate(ios_base::badbit); }
  956. }
  957. if (!__extracted)
  958. __err |= ios_base::failbit;
  959. if (__err)
  960. __in.setstate(__err);
  961. return __in;
  962. }
  963. // 27.6.1.4 Standard basic_istream manipulators
  964. template<typename _CharT, typename _Traits>
  965. basic_istream<_CharT, _Traits>&
  966. ws(basic_istream<_CharT, _Traits>& __in)
  967. {
  968. typedef basic_istream<_CharT, _Traits> __istream_type;
  969. typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
  970. typedef typename __istream_type::int_type __int_type;
  971. typedef ctype<_CharT> __ctype_type;
  972. const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc());
  973. const __int_type __eof = _Traits::eof();
  974. __streambuf_type* __sb = __in.rdbuf();
  975. __int_type __c = __sb->sgetc();
  976. while (!_Traits::eq_int_type(__c, __eof)
  977. && __ct.is(ctype_base::space, _Traits::to_char_type(__c)))
  978. __c = __sb->snextc();
  979. if (_Traits::eq_int_type(__c, __eof))
  980. __in.setstate(ios_base::eofbit);
  981. return __in;
  982. }
  983. // Inhibit implicit instantiations for required instantiations,
  984. // which are defined via explicit instantiations elsewhere.
  985. #if _GLIBCXX_EXTERN_TEMPLATE
  986. extern template class basic_istream<char>;
  987. extern template istream& ws(istream&);
  988. extern template istream& operator>>(istream&, char&);
  989. extern template istream& operator>>(istream&, char*);
  990. extern template istream& operator>>(istream&, unsigned char&);
  991. extern template istream& operator>>(istream&, signed char&);
  992. extern template istream& operator>>(istream&, unsigned char*);
  993. extern template istream& operator>>(istream&, signed char*);
  994. extern template istream& istream::_M_extract(unsigned short&);
  995. extern template istream& istream::_M_extract(unsigned int&);
  996. extern template istream& istream::_M_extract(long&);
  997. extern template istream& istream::_M_extract(unsigned long&);
  998. extern template istream& istream::_M_extract(bool&);
  999. #ifdef _GLIBCXX_USE_LONG_LONG
  1000. extern template istream& istream::_M_extract(long long&);
  1001. extern template istream& istream::_M_extract(unsigned long long&);
  1002. #endif
  1003. extern template istream& istream::_M_extract(float&);
  1004. extern template istream& istream::_M_extract(double&);
  1005. extern template istream& istream::_M_extract(long double&);
  1006. extern template istream& istream::_M_extract(void*&);
  1007. extern template class basic_iostream<char>;
  1008. #ifdef _GLIBCXX_USE_WCHAR_T
  1009. extern template class basic_istream<wchar_t>;
  1010. extern template wistream& ws(wistream&);
  1011. extern template wistream& operator>>(wistream&, wchar_t&);
  1012. extern template wistream& operator>>(wistream&, wchar_t*);
  1013. extern template wistream& wistream::_M_extract(unsigned short&);
  1014. extern template wistream& wistream::_M_extract(unsigned int&);
  1015. extern template wistream& wistream::_M_extract(long&);
  1016. extern template wistream& wistream::_M_extract(unsigned long&);
  1017. extern template wistream& wistream::_M_extract(bool&);
  1018. #ifdef _GLIBCXX_USE_LONG_LONG
  1019. extern template wistream& wistream::_M_extract(long long&);
  1020. extern template wistream& wistream::_M_extract(unsigned long long&);
  1021. #endif
  1022. extern template wistream& wistream::_M_extract(float&);
  1023. extern template wistream& wistream::_M_extract(double&);
  1024. extern template wistream& wistream::_M_extract(long double&);
  1025. extern template wistream& wistream::_M_extract(void*&);
  1026. extern template class basic_iostream<wchar_t>;
  1027. #endif
  1028. #endif
  1029. _GLIBCXX_END_NAMESPACE_VERSION
  1030. } // namespace std
  1031. #endif