Whole document tree istream.tccGo to the documentation of this file.00001 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc. 00002 // 00003 // This file is part of the GNU ISO C++ Library. This library is free 00004 // software; you can redistribute it and/or modify it under the 00005 // terms of the GNU General Public License as published by the 00006 // Free Software Foundation; either version 2, or (at your option) 00007 // any later version. 00008 00009 // This library is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 00014 // You should have received a copy of the GNU General Public License along 00015 // with this library; see the file COPYING. If not, write to the Free 00016 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00017 // USA. 00018 00019 // As a special exception, you may use this file as part of a free software 00020 // library without restriction. Specifically, if other files instantiate 00021 // templates or use macros or inline functions from this file, or you compile 00022 // this file and link it with other files to produce an executable, this 00023 // file does not by itself cause the resulting executable to be covered by 00024 // the GNU General Public License. This exception does not however 00025 // invalidate any other reasons why the executable file might be covered by 00026 // the GNU General Public License. 00027 00028 // 00029 // ISO C++ 14882: 27.6.2 Output streams 00030 // 00031 00032 #include <bits/std_locale.h> 00033 #include <bits/std_ostream.h> // for flush() 00034 00035 namespace std 00036 { 00037 template<typename _CharT, typename _Traits> 00038 basic_istream<_CharT, _Traits>::sentry:: 00039 sentry(basic_istream<_CharT, _Traits>& __in, bool __noskipws) 00040 { 00041 if (__in.good()) 00042 { 00043 if (__in.tie()) 00044 __in.tie()->flush(); 00045 if (!__noskipws && (__in.flags() & ios_base::skipws)) 00046 { 00047 const __int_type __eof = traits_type::eof(); 00048 const __ctype_type* __ctype = __in._M_get_fctype_ios(); 00049 __streambuf_type* __sb = __in.rdbuf(); 00050 __int_type __c = __sb->sgetc(); 00051 00052 while (__c != __eof && __ctype->is(ctype_base::space, __c)) 00053 __c = __sb->snextc(); 00054 00055 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 00056 //195. Should basic_istream::sentry's constructor ever set eofbit? 00057 if (__c == __eof) 00058 __in.setstate(ios_base::eofbit); 00059 #endif 00060 } 00061 } 00062 00063 if (__in.good()) 00064 _M_ok = true; 00065 else 00066 { 00067 _M_ok = false; 00068 __in.setstate(ios_base::failbit); 00069 } 00070 } 00071 00072 template<typename _CharT, typename _Traits> 00073 basic_istream<_CharT, _Traits>& 00074 basic_istream<_CharT, _Traits>:: 00075 operator>>(__istream_type& (*__pf)(__istream_type&)) 00076 { 00077 __pf(*this); 00078 return *this; 00079 } 00080 00081 template<typename _CharT, typename _Traits> 00082 basic_istream<_CharT, _Traits>& 00083 basic_istream<_CharT, _Traits>:: 00084 operator>>(__ios_type& (*__pf)(__ios_type&)) 00085 { 00086 __pf(*this); 00087 return *this; 00088 } 00089 00090 template<typename _CharT, typename _Traits> 00091 basic_istream<_CharT, _Traits>& 00092 basic_istream<_CharT, _Traits>:: 00093 operator>>(ios_base& (*__pf)(ios_base&)) 00094 { 00095 __pf(*this); 00096 return *this; 00097 } 00098 00099 template<typename _CharT, typename _Traits> 00100 basic_istream<_CharT, _Traits>& 00101 basic_istream<_CharT, _Traits>:: 00102 operator>>(bool& __n) 00103 { 00104 sentry __cerb(*this, false); 00105 if (__cerb) 00106 { 00107 try 00108 { 00109 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00110 if (_M_check_facet(_M_fnumget)) 00111 _M_fnumget->get(*this, 0, *this, __err, __n); 00112 this->setstate(__err); 00113 } 00114 catch(exception& __fail) 00115 { 00116 // 27.6.1.2.1 Common requirements. 00117 // Turn this on without causing an ios::failure to be thrown. 00118 this->setstate(ios_base::badbit); 00119 if ((this->exceptions() & ios_base::badbit) != 0) 00120 __throw_exception_again; 00121 } 00122 } 00123 return *this; 00124 } 00125 00126 template<typename _CharT, typename _Traits> 00127 basic_istream<_CharT, _Traits>& 00128 basic_istream<_CharT, _Traits>:: 00129 operator>>(short& __n) 00130 { 00131 sentry __cerb(*this, false); 00132 if (__cerb) 00133 { 00134 try 00135 { 00136 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00137 if (_M_check_facet(_M_fnumget)) 00138 _M_fnumget->get(*this, 0, *this, __err, __n); 00139 this->setstate(__err); 00140 } 00141 catch(exception& __fail) 00142 { 00143 // 27.6.1.2.1 Common requirements. 00144 // Turn this on without causing an ios::failure to be thrown. 00145 this->setstate(ios_base::badbit); 00146 if ((this->exceptions() & ios_base::badbit) != 0) 00147 __throw_exception_again; 00148 } 00149 } 00150 return *this; 00151 } 00152 00153 template<typename _CharT, typename _Traits> 00154 basic_istream<_CharT, _Traits>& 00155 basic_istream<_CharT, _Traits>:: 00156 operator>>(unsigned short& __n) 00157 { 00158 sentry __cerb(*this, false); 00159 if (__cerb) 00160 { 00161 try 00162 { 00163 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00164 if (_M_check_facet(_M_fnumget)) 00165 _M_fnumget->get(*this, 0, *this, __err, __n); 00166 this->setstate(__err); 00167 } 00168 catch(exception& __fail) 00169 { 00170 // 27.6.1.2.1 Common requirements. 00171 // Turn this on without causing an ios::failure to be thrown. 00172 this->setstate(ios_base::badbit); 00173 if ((this->exceptions() & ios_base::badbit) != 0) 00174 __throw_exception_again; 00175 } 00176 } 00177 return *this; 00178 } 00179 00180 template<typename _CharT, typename _Traits> 00181 basic_istream<_CharT, _Traits>& 00182 basic_istream<_CharT, _Traits>:: 00183 operator>>(int& __n) 00184 { 00185 sentry __cerb(*this, false); 00186 if (__cerb) 00187 { 00188 try 00189 { 00190 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00191 if (_M_check_facet(_M_fnumget)) 00192 _M_fnumget->get(*this, 0, *this, __err, __n); 00193 this->setstate(__err); 00194 } 00195 catch(exception& __fail) 00196 { 00197 // 27.6.1.2.1 Common requirements. 00198 // Turn this on without causing an ios::failure to be thrown. 00199 this->setstate(ios_base::badbit); 00200 if ((this->exceptions() & ios_base::badbit) != 0) 00201 __throw_exception_again; 00202 } 00203 } 00204 return *this; 00205 } 00206 00207 template<typename _CharT, typename _Traits> 00208 basic_istream<_CharT, _Traits>& 00209 basic_istream<_CharT, _Traits>:: 00210 operator>>(unsigned int& __n) 00211 { 00212 sentry __cerb(*this, false); 00213 if (__cerb) 00214 { 00215 try 00216 { 00217 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00218 if (_M_check_facet(_M_fnumget)) 00219 _M_fnumget->get(*this, 0, *this, __err, __n); 00220 this->setstate(__err); 00221 } 00222 catch(exception& __fail) 00223 { 00224 // 27.6.1.2.1 Common requirements. 00225 // Turn this on without causing an ios::failure to be thrown. 00226 this->setstate(ios_base::badbit); 00227 if ((this->exceptions() & ios_base::badbit) != 0) 00228 __throw_exception_again; 00229 } 00230 } 00231 return *this; 00232 } 00233 00234 template<typename _CharT, typename _Traits> 00235 basic_istream<_CharT, _Traits>& 00236 basic_istream<_CharT, _Traits>:: 00237 operator>>(long& __n) 00238 { 00239 sentry __cerb(*this, false); 00240 if (__cerb) 00241 { 00242 try 00243 { 00244 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00245 if (_M_check_facet(_M_fnumget)) 00246 _M_fnumget->get(*this, 0, *this, __err, __n); 00247 this->setstate(__err); 00248 } 00249 catch(exception& __fail) 00250 { 00251 // 27.6.1.2.1 Common requirements. 00252 // Turn this on without causing an ios::failure to be thrown. 00253 this->setstate(ios_base::badbit); 00254 if ((this->exceptions() & ios_base::badbit) != 0) 00255 __throw_exception_again; 00256 } 00257 } 00258 return *this; 00259 } 00260 00261 template<typename _CharT, typename _Traits> 00262 basic_istream<_CharT, _Traits>& 00263 basic_istream<_CharT, _Traits>:: 00264 operator>>(unsigned long& __n) 00265 { 00266 sentry __cerb(*this, false); 00267 if (__cerb) 00268 { 00269 try 00270 { 00271 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00272 if (_M_check_facet(_M_fnumget)) 00273 _M_fnumget->get(*this, 0, *this, __err, __n); 00274 this->setstate(__err); 00275 } 00276 catch(exception& __fail) 00277 { 00278 // 27.6.1.2.1 Common requirements. 00279 // Turn this on without causing an ios::failure to be thrown. 00280 this->setstate(ios_base::badbit); 00281 if ((this->exceptions() & ios_base::badbit) != 0) 00282 __throw_exception_again; 00283 } 00284 } 00285 return *this; 00286 } 00287 00288 #ifdef _GLIBCPP_USE_LONG_LONG 00289 template<typename _CharT, typename _Traits> 00290 basic_istream<_CharT, _Traits>& 00291 basic_istream<_CharT, _Traits>:: 00292 operator>>(long long& __n) 00293 { 00294 sentry __cerb(*this, false); 00295 if (__cerb) 00296 { 00297 try 00298 { 00299 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00300 if (_M_check_facet(_M_fnumget)) 00301 _M_fnumget->get(*this, 0, *this, __err, __n); 00302 this->setstate(__err); 00303 } 00304 catch(exception& __fail) 00305 { 00306 // 27.6.1.2.1 Common requirements. 00307 // Turn this on without causing an ios::failure to be thrown. 00308 this->setstate(ios_base::badbit); 00309 if ((this->exceptions() & ios_base::badbit) != 0) 00310 __throw_exception_again; 00311 } 00312 } 00313 return *this; 00314 } 00315 00316 template<typename _CharT, typename _Traits> 00317 basic_istream<_CharT, _Traits>& 00318 basic_istream<_CharT, _Traits>:: 00319 operator>>(unsigned long long& __n) 00320 { 00321 sentry __cerb(*this, false); 00322 if (__cerb) 00323 { 00324 try 00325 { 00326 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00327 if (_M_check_facet(_M_fnumget)) 00328 _M_fnumget->get(*this, 0, *this, __err, __n); 00329 this->setstate(__err); 00330 } 00331 catch(exception& __fail) 00332 { 00333 // 27.6.1.2.1 Common requirements. 00334 // Turn this on without causing an ios::failure to be thrown. 00335 this->setstate(ios_base::badbit); 00336 if ((this->exceptions() & ios_base::badbit) != 0) 00337 __throw_exception_again; 00338 } 00339 } 00340 return *this; 00341 } 00342 #endif 00343 00344 template<typename _CharT, typename _Traits> 00345 basic_istream<_CharT, _Traits>& 00346 basic_istream<_CharT, _Traits>:: 00347 operator>>(float& __n) 00348 { 00349 sentry __cerb(*this, false); 00350 if (__cerb) 00351 { 00352 try 00353 { 00354 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00355 if (_M_check_facet(_M_fnumget)) 00356 _M_fnumget->get(*this, 0, *this, __err, __n); 00357 this->setstate(__err); 00358 } 00359 catch(exception& __fail) 00360 { 00361 // 27.6.1.2.1 Common requirements. 00362 // Turn this on without causing an ios::failure to be thrown. 00363 this->setstate(ios_base::badbit); 00364 if ((this->exceptions() & ios_base::badbit) != 0) 00365 __throw_exception_again; 00366 } 00367 } 00368 return *this; 00369 } 00370 00371 template<typename _CharT, typename _Traits> 00372 basic_istream<_CharT, _Traits>& 00373 basic_istream<_CharT, _Traits>:: 00374 operator>>(double& __n) 00375 { 00376 sentry __cerb(*this, false); 00377 if (__cerb) 00378 { 00379 try 00380 { 00381 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00382 if (_M_check_facet(_M_fnumget)) 00383 _M_fnumget->get(*this, 0, *this, __err, __n); 00384 this->setstate(__err); 00385 } 00386 catch(exception& __fail) 00387 { 00388 // 27.6.1.2.1 Common requirements. 00389 // Turn this on without causing an ios::failure to be thrown. 00390 this->setstate(ios_base::badbit); 00391 if ((this->exceptions() & ios_base::badbit) != 0) 00392 __throw_exception_again; 00393 } 00394 } 00395 return *this; 00396 } 00397 00398 template<typename _CharT, typename _Traits> 00399 basic_istream<_CharT, _Traits>& 00400 basic_istream<_CharT, _Traits>:: 00401 operator>>(long double& __n) 00402 { 00403 sentry __cerb(*this, false); 00404 if (__cerb) 00405 { 00406 try 00407 { 00408 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00409 if (_M_check_facet(_M_fnumget)) 00410 _M_fnumget->get(*this, 0, *this, __err, __n); 00411 this->setstate(__err); 00412 } 00413 catch(exception& __fail) 00414 { 00415 // 27.6.1.2.1 Common requirements. 00416 // Turn this on without causing an ios::failure to be thrown. 00417 this->setstate(ios_base::badbit); 00418 if ((this->exceptions() & ios_base::badbit) != 0) 00419 __throw_exception_again; 00420 } 00421 } 00422 return *this; 00423 } 00424 00425 template<typename _CharT, typename _Traits> 00426 basic_istream<_CharT, _Traits>& 00427 basic_istream<_CharT, _Traits>:: 00428 operator>>(void*& __n) 00429 { 00430 sentry __cerb(*this, false); 00431 if (__cerb) 00432 { 00433 try 00434 { 00435 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit); 00436 if (_M_check_facet(_M_fnumget)) 00437 _M_fnumget->get(*this, 0, *this, __err, __n); 00438 this->setstate(__err); 00439 } 00440 catch(exception& __fail) 00441 { 00442 // 27.6.1.2.1 Common requirements. 00443 // Turn this on without causing an ios::failure to be thrown. 00444 this->setstate(ios_base::badbit); 00445 if ((this->exceptions() & ios_base::badbit) != 0) 00446 __throw_exception_again; 00447 } 00448 } 00449 return *this; 00450 } 00451 00452 template<typename _CharT, typename _Traits> 00453 basic_istream<_CharT, _Traits>& 00454 basic_istream<_CharT, _Traits>:: 00455 operator>>(__streambuf_type* __sbout) 00456 { 00457 streamsize __xtrct = 0; 00458 __streambuf_type* __sbin = this->rdbuf(); 00459 sentry __cerb(*this, false); 00460 if (__sbout && __cerb) 00461 __xtrct = __copy_streambufs(*this, __sbin, __sbout); 00462 if (!__sbout || !__xtrct) 00463 this->setstate(ios_base::failbit); 00464 return *this; 00465 } 00466 00467 template<typename _CharT, typename _Traits> 00468 typename basic_istream<_CharT, _Traits>::int_type 00469 basic_istream<_CharT, _Traits>:: 00470 get(void) 00471 { 00472 const int_type __eof = traits_type::eof(); 00473 int_type __c = __eof; 00474 _M_gcount = 0; 00475 sentry __cerb(*this, true); 00476 if (__cerb) 00477 { 00478 try 00479 { 00480 __c = this->rdbuf()->sbumpc(); 00481 // 27.6.1.1 paragraph 3 00482 if (__c != __eof) 00483 _M_gcount = 1; 00484 else 00485 this->setstate(ios_base::eofbit | ios_base::failbit); 00486 } 00487 catch(exception& __fail) 00488 { 00489 // 27.6.1.3 paragraph 1 00490 // Turn this on without causing an ios::failure to be thrown. 00491 this->setstate(ios_base::badbit); 00492 if ((this->exceptions() & ios_base::badbit) != 0) 00493 __throw_exception_again; 00494 } 00495 } 00496 return __c; 00497 } 00498 00499 template<typename _CharT, typename _Traits> 00500 basic_istream<_CharT, _Traits>& 00501 basic_istream<_CharT, _Traits>:: 00502 get(char_type& __c) 00503 { 00504 _M_gcount = 0; 00505 sentry __cerb(*this, true); 00506 if (__cerb) 00507 { 00508 try 00509 { 00510 const int_type __eof = traits_type::eof(); 00511 int_type __bufval = this->rdbuf()->sbumpc(); 00512 // 27.6.1.1 paragraph 3 00513 if (__bufval != __eof) 00514 { 00515 _M_gcount = 1; 00516 __c = traits_type::to_char_type(__bufval); 00517 } 00518 else 00519 this->setstate(ios_base::eofbit | ios_base::failbit); 00520 } 00521 catch(exception& __fail) 00522 { 00523 // 27.6.1.3 paragraph 1 00524 // Turn this on without causing an ios::failure to be thrown. 00525 this->setstate(ios_base::badbit); 00526 if ((this->exceptions() & ios_base::badbit) != 0) 00527 __throw_exception_again; 00528 } 00529 } 00530 return *this; 00531 } 00532 00533 template<typename _CharT, typename _Traits> 00534 basic_istream<_CharT, _Traits>& 00535 basic_istream<_CharT, _Traits>:: 00536 get(char_type* __s, streamsize __n, char_type __delim) 00537 { 00538 _M_gcount = 0; 00539 sentry __cerb(*this, true); 00540 if (__cerb && __n > 1) 00541 { 00542 try 00543 { 00544 const int_type __idelim = traits_type::to_int_type(__delim); 00545 const int_type __eof = traits_type::eof(); 00546 __streambuf_type* __sb = this->rdbuf(); 00547 int_type __c = __sb->sbumpc(); 00548 bool __testdelim = __c == __idelim; 00549 bool __testeof = __c == __eof; 00550 00551 while (_M_gcount < __n - 1 && !__testeof && !__testdelim) 00552 { 00553 *__s++ = traits_type::to_char_type(__c); 00554 ++_M_gcount; 00555 __c = __sb->sbumpc(); 00556 __testeof = __c == __eof; 00557 __testdelim = __c == __idelim; 00558 } 00559 if (__testdelim || _M_gcount == __n - 1) 00560 __sb->sputbackc(__c); 00561 if (__testeof) 00562 this->setstate(ios_base::eofbit); 00563 } 00564 catch(exception& __fail) 00565 { 00566 // 27.6.1.3 paragraph 1 00567 // Turn this on without causing an ios::failure to be thrown. 00568 this->setstate(ios_base::badbit); 00569 if ((this->exceptions() & ios_base::badbit) != 0) 00570 __throw_exception_again; 00571 } 00572 } 00573 *__s = char_type(); 00574 if (!_M_gcount) 00575 this->setstate(ios_base::failbit); 00576 return *this; 00577 } 00578 00579 template<typename _CharT, typename _Traits> 00580 basic_istream<_CharT, _Traits>& 00581 basic_istream<_CharT, _Traits>:: 00582 get(__streambuf_type& __sb, char_type __delim) 00583 { 00584 _M_gcount = 0; 00585 sentry __cerb(*this, true); 00586 if (__cerb) 00587 { 00588 int_type __c; 00589 __streambuf_type* __this_sb = this->rdbuf(); 00590 try 00591 { 00592 const int_type __idelim = traits_type::to_int_type(__delim); 00593 const int_type __eof = traits_type::eof(); 00594 __c = __this_sb->sbumpc(); 00595 bool __testdelim = __c == __idelim; 00596 bool __testeof = __c == __eof; 00597 bool __testput = true; 00598 00599 while (!__testeof && !__testdelim 00600 && (__testput = __sb.sputc(traits_type::to_char_type(__c)) 00601 != __eof)) 00602 { 00603 ++_M_gcount; 00604 __c = __this_sb->sbumpc(); 00605 __testeof = __c == __eof; 00606 __testdelim = __c == __idelim; 00607 } 00608 if (__testdelim || !__testput) 00609 __this_sb->sputbackc(traits_type::to_char_type(__c)); 00610 if (__testeof) 00611 this->setstate(ios_base::eofbit); 00612 } 00613 catch(exception& __fail) 00614 { 00615 // Exception may result from sputc->overflow. 00616 __this_sb->sputbackc(traits_type::to_char_type(__c)); 00617 } 00618 } 00619 if (!_M_gcount) 00620 this->setstate(ios_base::failbit); 00621 return *this; 00622 } 00623 00624 template<typename _CharT, typename _Traits> 00625 basic_istream<_CharT, _Traits>& 00626 basic_istream<_CharT, _Traits>:: 00627 getline(char_type* __s, streamsize __n, char_type __delim) 00628 { 00629 _M_gcount = 0; 00630 sentry __cerb(*this, true); 00631 if (__cerb) 00632 { 00633 try 00634 { 00635 __streambuf_type* __sb = this->rdbuf(); 00636 int_type __c = __sb->sbumpc(); 00637 ++_M_gcount; 00638 const int_type __idelim = traits_type::to_int_type(__delim); 00639 const int_type __eof = traits_type::eof(); 00640 bool __testdelim = __c == __idelim; 00641 bool __testeof = __c == __eof; 00642 00643 while (_M_gcount < __n && !__testeof && !__testdelim) 00644 { 00645 *__s++ = traits_type::to_char_type(__c); 00646 __c = __sb->sbumpc(); 00647 ++_M_gcount; 00648 __testeof = __c == __eof; 00649 __testdelim = __c == __idelim; 00650 } 00651 00652 if (__testeof) 00653 { 00654 --_M_gcount; 00655 this->setstate(ios_base::eofbit); 00656 } 00657 else if (!__testdelim) 00658 { 00659 --_M_gcount; 00660 __sb->sputbackc(traits_type::to_char_type(__c)); 00661 this->setstate(ios_base::failbit); 00662 } 00663 } 00664 catch(exception& __fail) 00665 { 00666 // 27.6.1.3 paragraph 1 00667 // Turn this on without causing an ios::failure to be thrown. 00668 this->setstate(ios_base::badbit); 00669 if ((this->exceptions() & ios_base::badbit) != 0) 00670 __throw_exception_again; 00671 } 00672 } 00673 *__s = char_type(); 00674 if (!_M_gcount) 00675 this->setstate(ios_base::failbit); 00676 return *this; 00677 } 00678 00679 template<typename _CharT, typename _Traits> 00680 basic_istream<_CharT, _Traits>& 00681 basic_istream<_CharT, _Traits>:: 00682 ignore(streamsize __n, int_type __delim) 00683 { 00684 _M_gcount = 0; 00685 sentry __cerb(*this, true); 00686 if (__cerb && __n > 0) 00687 { 00688 try 00689 { 00690 const int_type __idelim = traits_type::to_int_type(__delim); 00691 const int_type __eof = traits_type::eof(); 00692 __streambuf_type* __sb = this->rdbuf(); 00693 int_type __c = __sb->sbumpc(); 00694 bool __testdelim = __c == __idelim; 00695 bool __testeof = __c == __eof; 00696 00697 __n = min(__n, numeric_limits<streamsize>::max()); 00698 while (_M_gcount < __n - 1 && !__testeof && !__testdelim) 00699 { 00700 ++_M_gcount; 00701 __c = __sb->sbumpc(); 00702 __testeof = __c == __eof; 00703 __testdelim = __c == __idelim; 00704 } 00705 if ((_M_gcount == __n - 1 && !__testeof) || __testdelim) 00706 ++_M_gcount; 00707 if (__testeof) 00708 this->setstate(ios_base::eofbit); 00709 } 00710 catch(exception& __fail) 00711 { 00712 // 27.6.1.3 paragraph 1 00713 // Turn this on without causing an ios::failure to be thrown. 00714 this->setstate(ios_base::badbit); 00715 if ((this->exceptions() & ios_base::badbit) != 0) 00716 __throw_exception_again; 00717 } 00718 } 00719 return *this; 00720 } 00721 00722 template<typename _CharT, typename _Traits> 00723 typename basic_istream<_CharT, _Traits>::int_type 00724 basic_istream<_CharT, _Traits>:: 00725 peek(void) 00726 { 00727 int_type __c = traits_type::eof(); 00728 _M_gcount = 0; 00729 sentry __cerb(*this, true); 00730 if (__cerb) 00731 { 00732 try 00733 { __c = this->rdbuf()->sgetc(); } 00734 catch(exception& __fail) 00735 { 00736 // 27.6.1.3 paragraph 1 00737 // Turn this on without causing an ios::failure to be thrown. 00738 this->setstate(ios_base::badbit); 00739 if ((this->exceptions() & ios_base::badbit) != 0) 00740 __throw_exception_again; 00741 } 00742 } 00743 return __c; 00744 } 00745 00746 template<typename _CharT, typename _Traits> 00747 basic_istream<_CharT, _Traits>& 00748 basic_istream<_CharT, _Traits>:: 00749 read(char_type* __s, streamsize __n) 00750 { 00751 _M_gcount = 0; 00752 sentry __cerb(*this, true); 00753 if (__cerb) 00754 { 00755 if (__n > 0) 00756 { 00757 try 00758 { 00759 const int_type __eof = traits_type::eof(); 00760 __streambuf_type* __sb = this->rdbuf(); 00761 int_type __c = __sb->sbumpc(); 00762 bool __testeof = __c == __eof; 00763 00764 while (_M_gcount < __n - 1 && !__testeof) 00765 { 00766 *__s++ = traits_type::to_char_type(__c); 00767 ++_M_gcount; 00768 __c = __sb->sbumpc(); 00769 __testeof = __c == __eof; 00770 } 00771 if (__testeof) 00772 this->setstate(ios_base::eofbit | ios_base::failbit); 00773 else 00774 { 00775 // _M_gcount == __n - 1 00776 *__s++ = traits_type::to_char_type(__c); 00777 ++_M_gcount; 00778 } 00779 } 00780 catch(exception& __fail) 00781 { 00782 // 27.6.1.3 paragraph 1 00783 // Turn this on without causing an ios::failure to be thrown. 00784 this->setstate(ios_base::badbit); 00785 if ((this->exceptions() & ios_base::badbit) != 0) 00786 __throw_exception_again; 00787 } 00788 } 00789 } 00790 else 00791 this->setstate(ios_base::failbit); 00792 return *this; 00793 } 00794 00795 template<typename _CharT, typename _Traits> 00796 streamsize 00797 basic_istream<_CharT, _Traits>:: 00798 readsome(char_type* __s, streamsize __n) 00799 { 00800 const int_type __eof = traits_type::eof(); 00801 _M_gcount = 0; 00802 sentry __cerb(*this, true); 00803 if (__cerb) 00804 { 00805 if (__n > 0) 00806 { 00807 try 00808 { 00809 streamsize __num = this->rdbuf()->in_avail(); 00810 if (__num != static_cast<streamsize>(__eof)) 00811 { 00812 __num = min(__num, __n); 00813 _M_gcount = this->rdbuf()->sgetn(__s, __num); 00814 } 00815 else 00816 this->setstate(ios_base::eofbit); 00817 } 00818 catch(exception& __fail) 00819 { 00820 // 27.6.1.3 paragraph 1 00821 // Turn this on without causing an ios::failure to be thrown. 00822 this->setstate(ios_base::badbit); 00823 if ((this->exceptions() & ios_base::badbit) != 0) 00824 __throw_exception_again; 00825 } 00826 } 00827 } 00828 else 00829 this->setstate(ios_base::failbit); 00830 return _M_gcount; 00831 } 00832 00833 template<typename _CharT, typename _Traits> 00834 basic_istream<_CharT, _Traits>& 00835 basic_istream<_CharT, _Traits>:: 00836 putback(char_type __c) 00837 { 00838 sentry __cerb(*this, true); 00839 if (__cerb) 00840 { 00841 try 00842 { 00843 const int_type __eof = traits_type::eof(); 00844 __streambuf_type* __sb = this->rdbuf(); 00845 if (!__sb || __sb->sputbackc(__c) == __eof) 00846 this->setstate(ios_base::badbit); 00847 } 00848 catch(exception& __fail) 00849 { 00850 // 27.6.1.3 paragraph 1 00851 // Turn this on without causing an ios::failure to be thrown. 00852 this->setstate(ios_base::badbit); 00853 if ((this->exceptions() & ios_base::badbit) != 0) 00854 __throw_exception_again; 00855 } 00856 } 00857 else 00858 this->setstate(ios_base::failbit); 00859 return *this; 00860 } 00861 00862 template<typename _CharT, typename _Traits> 00863 basic_istream<_CharT, _Traits>& 00864 basic_istream<_CharT, _Traits>:: 00865 unget(void) 00866 { 00867 _M_gcount = 0; 00868 sentry __cerb(*this, true); 00869 if (__cerb) 00870 { 00871 try 00872 { 00873 const int_type __eof = traits_type::eof(); 00874 __streambuf_type* __sb = this->rdbuf(); 00875 if (!__sb || __eof == __sb->sungetc()) 00876 this->setstate(ios_base::badbit); 00877 } 00878 catch(exception& __fail) 00879 { 00880 // 27.6.1.3 paragraph 1 00881 // Turn this on without causing an ios::failure to be thrown. 00882 this->setstate(ios_base::badbit); 00883 if ((this->exceptions() & ios_base::badbit) != 0) 00884 __throw_exception_again; 00885 } 00886 } 00887 else 00888 this->setstate(ios_base::failbit); 00889 return *this; 00890 } 00891 00892 template<typename _CharT, typename _Traits> 00893 int 00894 basic_istream<_CharT, _Traits>:: 00895 sync(void) 00896 { 00897 int __ret = traits_type::eof(); 00898 _M_gcount = 0; 00899 sentry __cerb(*this, true); 00900 if (__cerb) 00901 { 00902 try 00903 { 00904 __streambuf_type* __sb = this->rdbuf(); 00905 if (!__sb || __ret == __sb->pubsync()) 00906 this->setstate(ios_base::badbit); 00907 else 00908 __ret = 0; 00909 } 00910 catch(exception& __fail) 00911 { 00912 // 27.6.1.3 paragraph 1 00913 // Turn this on without causing an ios::failure to be thrown. 00914 this->setstate(ios_base::badbit); 00915 if ((this->exceptions() & ios_base::badbit) != 0) 00916 __throw_exception_again; 00917 } 00918 } 00919 return __ret; 00920 } 00921 00922 template<typename _CharT, typename _Traits> 00923 typename basic_istream<_CharT, _Traits>::pos_type 00924 basic_istream<_CharT, _Traits>:: 00925 tellg(void) 00926 { 00927 pos_type __ret = pos_type(-1); 00928 _M_gcount = 0; 00929 sentry __cerb(*this, true); 00930 if (__cerb) 00931 { 00932 try 00933 { 00934 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in); 00935 } 00936 catch(exception& __fail) 00937 { 00938 // 27.6.1.3 paragraph 1 00939 // Turn this on without causing an ios::failure to be thrown. 00940 this->setstate(ios_base::badbit); 00941 if ((this->exceptions() & ios_base::badbit) != 0) 00942 __throw_exception_again; 00943 } 00944 } 00945 return __ret; 00946 } 00947 00948 00949 template<typename _CharT, typename _Traits> 00950 basic_istream<_CharT, _Traits>& 00951 basic_istream<_CharT, _Traits>:: 00952 seekg(pos_type __pos) 00953 { 00954 _M_gcount = 0; 00955 sentry __cerb(*this, true); 00956 if (__cerb) 00957 { 00958 try 00959 { 00960 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 00961 // 136. seekp, seekg setting wrong streams? 00962 pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::in); 00963 00964 // 129. Need error indication from seekp() and seekg() 00965 if (__err == pos_type(off_type(-1))) 00966 this->setstate(failbit); 00967 #endif 00968 } 00969 catch(exception& __fail) 00970 { 00971 // 27.6.1.3 paragraph 1 00972 // Turn this on without causing an ios::failure to be thrown. 00973 this->setstate(ios_base::badbit); 00974 if ((this->exceptions() & ios_base::badbit) != 0) 00975 __throw_exception_again; 00976 } 00977 } 00978 return *this; 00979 } 00980 00981 template<typename _CharT, typename _Traits> 00982 basic_istream<_CharT, _Traits>& 00983 basic_istream<_CharT, _Traits>:: 00984 seekg(off_type __off, ios_base::seekdir __dir) 00985 { 00986 _M_gcount = 0; 00987 sentry __cerb(*this, true); 00988 if (__cerb) 00989 { 00990 try 00991 { 00992 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 00993 // 136. seekp, seekg setting wrong streams? 00994 pos_type __err = this->rdbuf()->pubseekoff(__off, __dir, 00995 ios_base::in); 00996 00997 // 129. Need error indication from seekp() and seekg() 00998 if (__err == pos_type(off_type(-1))) 00999 this->setstate(failbit); 01000 #endif 01001 } 01002 catch(exception& __fail) 01003 { 01004 // 27.6.1.3 paragraph 1 01005 // Turn this on without causing an ios::failure to be thrown. 01006 this->setstate(ios_base::badbit); 01007 if ((this->exceptions() & ios_base::badbit) != 0) 01008 __throw_exception_again; 01009 } 01010 } 01011 return *this; 01012 } 01013 01014 // 27.6.1.2.3 Character extraction templates 01015 template<typename _CharT, typename _Traits> 01016 basic_istream<_CharT, _Traits>& 01017 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c) 01018 { 01019 typedef basic_istream<_CharT, _Traits> __istream_type; 01020 typename __istream_type::sentry __cerb(__in, false); 01021 if (__cerb) 01022 { 01023 try 01024 { __in.get(__c); } 01025 catch(exception& __fail) 01026 { 01027 // 27.6.1.2.1 Common requirements. 01028 // Turn this on without causing an ios::failure to be thrown. 01029 __in.setstate(ios_base::badbit); 01030 if ((__in.exceptions() & ios_base::badbit) != 0) 01031 __throw_exception_again; 01032 } 01033 } 01034 else 01035 __in.setstate(ios_base::failbit); 01036 return __in; 01037 } 01038 01039 template<typename _CharT, typename _Traits> 01040 basic_istream<_CharT, _Traits>& 01041 operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s) 01042 { 01043 typedef basic_istream<_CharT, _Traits> __istream_type; 01044 typedef typename __istream_type::__streambuf_type __streambuf_type; 01045 typedef typename _Traits::int_type int_type; 01046 typedef _CharT char_type; 01047 typedef ctype<_CharT> __ctype_type; 01048 streamsize __extracted = 0; 01049 01050 typename __istream_type::sentry __cerb(__in, false); 01051 if (__cerb) 01052 { 01053 try 01054 { 01055 // Figure out how many characters to extract. 01056 streamsize __num = __in.width(); 01057 if (__num == 0) 01058 __num = numeric_limits<streamsize>::max(); 01059 01060 __streambuf_type* __sb = __in.rdbuf(); 01061 const __ctype_type* __ctype = __in._M_get_fctype_ios(); 01062 int_type __c = __sb->sbumpc(); 01063 const int_type __eof = _Traits::eof(); 01064 bool __testsp = __ctype->is(ctype_base::space, __c); 01065 bool __testeof = __c == __eof; 01066 01067 while (__extracted < __num - 1 && !__testeof && !__testsp) 01068 { 01069 *__s++ = __c; 01070 ++__extracted; 01071 __c = __sb->sbumpc(); 01072 __testeof = __c == __eof; 01073 __testsp = __ctype->is(ctype_base::space, __c); 01074 } 01075 01076 if (!__testeof) 01077 __sb->sputbackc(__c); 01078 else 01079 __in.setstate(ios_base::eofbit); 01080 01081 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 01082 //68. Extractors for char* should store null at end 01083 *__s = char_type(); 01084 #endif 01085 __in.width(0); 01086 } 01087 catch(exception& __fail) 01088 { 01089 // 27.6.1.2.1 Common requirements. 01090 // Turn this on without causing an ios::failure to be thrown. 01091 __in.setstate(ios_base::badbit); 01092 if ((__in.exceptions() & ios_base::badbit) != 0) 01093 __throw_exception_again; 01094 } 01095 } 01096 if (!__extracted) 01097 __in.setstate(ios_base::failbit); 01098 return __in; 01099 } 01100 01101 // 27.6.1.4 Standard basic_istream manipulators 01102 template<typename _CharT, typename _Traits> 01103 basic_istream<_CharT,_Traits>& 01104 ws(basic_istream<_CharT,_Traits>& __in) 01105 { 01106 typedef basic_istream<_CharT, _Traits> __istream_type; 01107 typedef typename __istream_type::__streambuf_type __streambuf_type; 01108 typedef typename __istream_type::__ctype_type __ctype_type; 01109 typedef typename __istream_type::int_type __int_type; 01110 typedef typename __istream_type::char_type __char_type; 01111 01112 __streambuf_type* __sb = __in.rdbuf(); 01113 const __ctype_type* __ctype = __in._M_get_fctype_ios(); 01114 const __int_type __eof = _Traits::eof(); 01115 __int_type __c; 01116 bool __testeof; 01117 bool __testsp; 01118 01119 do 01120 { 01121 __c = __sb->sbumpc(); 01122 __testeof = __c == __eof; 01123 __testsp = __ctype->is(ctype_base::space, __c); 01124 } 01125 while (!__testeof && __testsp); 01126 01127 if (!__testeof && !__testsp) 01128 __sb->sputbackc(__c); 01129 else 01130 __in.setstate(ios_base::eofbit); 01131 01132 return __in; 01133 } 01134 01135 // 21.3.7.9 basic_string::getline and operators 01136 template<typename _CharT, typename _Traits, typename _Alloc> 01137 basic_istream<_CharT, _Traits>& 01138 operator>>(basic_istream<_CharT, _Traits>& __in, 01139 basic_string<_CharT, _Traits, _Alloc>& __str) 01140 { 01141 typedef basic_istream<_CharT, _Traits> __istream_type; 01142 typedef typename __istream_type::int_type __int_type; 01143 typedef typename __istream_type::__streambuf_type __streambuf_type; 01144 typedef typename __istream_type::__ctype_type __ctype_type; 01145 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 01146 typedef typename __string_type::size_type __size_type; 01147 __size_type __extracted = 0; 01148 01149 typename __istream_type::sentry __cerb(__in, false); 01150 if (__cerb) 01151 { 01152 __str.erase(); 01153 streamsize __w = __in.width(); 01154 __size_type __n; 01155 __n = __w > 0 ? static_cast<__size_type>(__w) : __str.max_size(); 01156 01157 __streambuf_type* __sb = __in.rdbuf(); 01158 const __ctype_type* __ctype = __in._M_get_fctype_ios(); 01159 __int_type __c = __sb->sbumpc(); 01160 const __int_type __eof = _Traits::eof(); 01161 bool __testsp = __ctype->is(ctype_base::space, __c); 01162 bool __testeof = __c == __eof; 01163 01164 while (__extracted < __n && !__testeof && !__testsp) 01165 { 01166 __str += _Traits::to_char_type(__c); 01167 ++__extracted; 01168 __c = __sb->sbumpc(); 01169 __testeof = __c == __eof; 01170 __testsp = __ctype->is(ctype_base::space, __c); 01171 } 01172 if (!__testeof) 01173 __sb->sputbackc(__c); 01174 else 01175 __in.setstate(ios_base::eofbit); 01176 __in.width(0); 01177 } 01178 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 01179 // 2000-02-01 Number to be determined 01180 if (!__extracted) 01181 __in.setstate (ios_base::failbit); 01182 #endif 01183 return __in; 01184 } 01185 01186 template<typename _CharT, typename _Traits, typename _Alloc> 01187 basic_istream<_CharT, _Traits>& 01188 getline(basic_istream<_CharT, _Traits>& __in, 01189 basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim) 01190 { 01191 typedef basic_istream<_CharT, _Traits> __istream_type; 01192 typedef typename __istream_type::int_type __int_type; 01193 typedef typename __istream_type::__streambuf_type __streambuf_type; 01194 typedef typename __istream_type::__ctype_type __ctype_type; 01195 typedef basic_string<_CharT, _Traits, _Alloc> __string_type; 01196 typedef typename __string_type::size_type __size_type; 01197 01198 __size_type __extracted = 0; 01199 bool __testdelim = false; 01200 typename __istream_type::sentry __cerb(__in, true); 01201 if (__cerb) 01202 { 01203 __str.erase(); 01204 __size_type __n = __str.max_size(); 01205 01206 __int_type __idelim = _Traits::to_int_type(__delim); 01207 __streambuf_type* __sb = __in.rdbuf(); 01208 __int_type __c = __sb->sbumpc(); 01209 const __int_type __eof = _Traits::eof(); 01210 __testdelim = __c == __idelim; 01211 bool __testeof = __c == __eof; 01212 01213 while (__extracted <= __n && !__testeof && !__testdelim) 01214 { 01215 __str += _Traits::to_char_type(__c); 01216 ++__extracted; 01217 __c = __sb->sbumpc(); 01218 __testeof = __c == __eof; 01219 __testdelim = __c == __idelim; 01220 } 01221 if (__testeof) 01222 __in.setstate(ios_base::eofbit); 01223 } 01224 if (!__extracted && !__testdelim) 01225 __in.setstate(ios_base::failbit); 01226 return __in; 01227 } 01228 01229 template<class _CharT, class _Traits, class _Alloc> 01230 inline basic_istream<_CharT,_Traits>& 01231 getline(basic_istream<_CharT, _Traits>& __in, 01232 basic_string<_CharT,_Traits,_Alloc>& __str) 01233 { return getline(__in, __str, __in.widen('\n')); } 01234 } // namespace std 01235 01236 // Local Variables: 01237 // mode:C++ 01238 // End: 01239 Generated on Mon Apr 8 03:11:27 2002 for libstdc++-v3 Source by ![]() |