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