1 // Input streams -*- C++ -*- 2 3 // Copyright (C) 2004-2020 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 // 26 // ISO C++ 14882: 27.6.1 Input streams 27 // 28 29 #include <istream> 30 31 namespace std _GLIBCXX_VISIBILITY(default) 32 { 33 _GLIBCXX_BEGIN_NAMESPACE_VERSION 34 35 template<> 36 basic_istream<char>& 37 basic_istream<char>:: getline(char_type * __s,streamsize __n,char_type __delim)38 getline(char_type* __s, streamsize __n, char_type __delim) 39 { 40 _M_gcount = 0; 41 ios_base::iostate __err = ios_base::goodbit; 42 sentry __cerb(*this, true); 43 if (__cerb) 44 { 45 __try 46 { 47 const int_type __idelim = traits_type::to_int_type(__delim); 48 const int_type __eof = traits_type::eof(); 49 __streambuf_type* __sb = this->rdbuf(); 50 int_type __c = __sb->sgetc(); 51 52 while (_M_gcount + 1 < __n 53 && !traits_type::eq_int_type(__c, __eof) 54 && !traits_type::eq_int_type(__c, __idelim)) 55 { 56 streamsize __size = std::min(streamsize(__sb->egptr() 57 - __sb->gptr()), 58 streamsize(__n - _M_gcount 59 - 1)); 60 if (__size > 1) 61 { 62 const char_type* __p = traits_type::find(__sb->gptr(), 63 __size, 64 __delim); 65 if (__p) 66 __size = __p - __sb->gptr(); 67 traits_type::copy(__s, __sb->gptr(), __size); 68 __s += __size; 69 __sb->__safe_gbump(__size); 70 _M_gcount += __size; 71 __c = __sb->sgetc(); 72 } 73 else 74 { 75 *__s++ = traits_type::to_char_type(__c); 76 ++_M_gcount; 77 __c = __sb->snextc(); 78 } 79 } 80 81 if (traits_type::eq_int_type(__c, __eof)) 82 __err |= ios_base::eofbit; 83 else if (traits_type::eq_int_type(__c, __idelim)) 84 { 85 ++_M_gcount; 86 __sb->sbumpc(); 87 } 88 else 89 __err |= ios_base::failbit; 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 } 99 // _GLIBCXX_RESOLVE_LIB_DEFECTS 100 // 243. get and getline when sentry reports failure. 101 if (__n > 0) 102 *__s = char_type(); 103 if (!_M_gcount) 104 __err |= ios_base::failbit; 105 if (__err) 106 this->setstate(__err); 107 return *this; 108 } 109 110 template<> 111 basic_istream<char>& 112 basic_istream<char>:: ignore(streamsize __n,int_type __delim)113 ignore(streamsize __n, int_type __delim) 114 { 115 #ifdef _GLIBCXX_COMPAT_ 116 if (traits_type::eq_int_type(__delim, traits_type::eof())) 117 return ignore(__n); 118 #endif 119 120 _M_gcount = 0; 121 sentry __cerb(*this, true); 122 if (__n > 0 && __cerb) 123 { 124 ios_base::iostate __err = ios_base::goodbit; 125 __try 126 { 127 const char_type __cdelim = traits_type::to_char_type(__delim); 128 const int_type __eof = traits_type::eof(); 129 __streambuf_type* __sb = this->rdbuf(); 130 int_type __c = __sb->sgetc(); 131 132 bool __large_ignore = false; 133 while (true) 134 { 135 while (_M_gcount < __n 136 && !traits_type::eq_int_type(__c, __eof) 137 && !traits_type::eq_int_type(__c, __delim)) 138 { 139 streamsize __size = std::min(streamsize(__sb->egptr() 140 - __sb->gptr()), 141 streamsize(__n - _M_gcount)); 142 if (__size > 1) 143 { 144 const char_type* __p = traits_type::find(__sb->gptr(), 145 __size, 146 __cdelim); 147 if (__p) 148 __size = __p - __sb->gptr(); 149 __sb->__safe_gbump(__size); 150 _M_gcount += __size; 151 __c = __sb->sgetc(); 152 } 153 else 154 { 155 ++_M_gcount; 156 __c = __sb->snextc(); 157 } 158 } 159 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max 160 && !traits_type::eq_int_type(__c, __eof) 161 && !traits_type::eq_int_type(__c, __delim)) 162 { 163 _M_gcount = 164 __gnu_cxx::__numeric_traits<streamsize>::__min; 165 __large_ignore = true; 166 } 167 else 168 break; 169 } 170 171 if (__large_ignore) 172 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max; 173 174 if (traits_type::eq_int_type(__c, __eof)) 175 __err |= ios_base::eofbit; 176 else if (traits_type::eq_int_type(__c, __delim)) 177 { 178 if (_M_gcount 179 < __gnu_cxx::__numeric_traits<streamsize>::__max) 180 ++_M_gcount; 181 __sb->sbumpc(); 182 } 183 } 184 __catch(__cxxabiv1::__forced_unwind&) 185 { 186 this->_M_setstate(ios_base::badbit); 187 __throw_exception_again; 188 } 189 __catch(...) 190 { this->_M_setstate(ios_base::badbit); } 191 if (__err) 192 this->setstate(__err); 193 } 194 return *this; 195 } 196 197 template<> 198 basic_istream<char>& operator >>(basic_istream<char> & __in,char * __s)199 operator>>(basic_istream<char>& __in, char* __s) 200 { 201 typedef basic_istream<char> __istream_type; 202 typedef __istream_type::int_type __int_type; 203 typedef __istream_type::char_type __char_type; 204 typedef __istream_type::traits_type __traits_type; 205 typedef __istream_type::__streambuf_type __streambuf_type; 206 typedef __istream_type::__ctype_type __ctype_type; 207 208 streamsize __extracted = 0; 209 ios_base::iostate __err = ios_base::goodbit; 210 __istream_type::sentry __cerb(__in, false); 211 if (__cerb) 212 { 213 __try 214 { 215 // Figure out how many characters to extract. 216 streamsize __num = __in.width(); 217 if (__num <= 0) 218 __num = __gnu_cxx::__numeric_traits<streamsize>::__max; 219 220 const __ctype_type& __ct = use_facet<__ctype_type>(__in.getloc()); 221 222 const __int_type __eof = __traits_type::eof(); 223 __streambuf_type* __sb = __in.rdbuf(); 224 __int_type __c = __sb->sgetc(); 225 226 while (__extracted < __num - 1 227 && !__traits_type::eq_int_type(__c, __eof) 228 && !__ct.is(ctype_base::space, 229 __traits_type::to_char_type(__c))) 230 { 231 streamsize __size = std::min(streamsize(__sb->egptr() 232 - __sb->gptr()), 233 streamsize(__num - __extracted 234 - 1)); 235 if (__size > 1) 236 { 237 __size = (__ct.scan_is(ctype_base::space, 238 __sb->gptr() + 1, 239 __sb->gptr() + __size) 240 - __sb->gptr()); 241 __traits_type::copy(__s, __sb->gptr(), __size); 242 __s += __size; 243 __sb->__safe_gbump(__size); 244 __extracted += __size; 245 __c = __sb->sgetc(); 246 } 247 else 248 { 249 *__s++ = __traits_type::to_char_type(__c); 250 ++__extracted; 251 __c = __sb->snextc(); 252 } 253 } 254 255 if (__traits_type::eq_int_type(__c, __eof)) 256 __err |= ios_base::eofbit; 257 258 // _GLIBCXX_RESOLVE_LIB_DEFECTS 259 // 68. Extractors for char* should store null at end 260 *__s = __char_type(); 261 __in.width(0); 262 } 263 __catch(__cxxabiv1::__forced_unwind&) 264 { 265 __in._M_setstate(ios_base::badbit); 266 __throw_exception_again; 267 } 268 __catch(...) 269 { __in._M_setstate(ios_base::badbit); } 270 } 271 if (!__extracted) 272 __err |= ios_base::failbit; 273 if (__err) 274 __in.setstate(__err); 275 return __in; 276 } 277 278 #ifdef _GLIBCXX_USE_WCHAR_T 279 template<> 280 basic_istream<wchar_t>& 281 basic_istream<wchar_t>:: getline(char_type * __s,streamsize __n,char_type __delim)282 getline(char_type* __s, streamsize __n, char_type __delim) 283 { 284 _M_gcount = 0; 285 ios_base::iostate __err = ios_base::goodbit; 286 sentry __cerb(*this, true); 287 if (__cerb) 288 { 289 __try 290 { 291 const int_type __idelim = traits_type::to_int_type(__delim); 292 const int_type __eof = traits_type::eof(); 293 __streambuf_type* __sb = this->rdbuf(); 294 int_type __c = __sb->sgetc(); 295 296 while (_M_gcount + 1 < __n 297 && !traits_type::eq_int_type(__c, __eof) 298 && !traits_type::eq_int_type(__c, __idelim)) 299 { 300 streamsize __size = std::min(streamsize(__sb->egptr() 301 - __sb->gptr()), 302 streamsize(__n - _M_gcount 303 - 1)); 304 if (__size > 1) 305 { 306 const char_type* __p = traits_type::find(__sb->gptr(), 307 __size, 308 __delim); 309 if (__p) 310 __size = __p - __sb->gptr(); 311 traits_type::copy(__s, __sb->gptr(), __size); 312 __s += __size; 313 __sb->__safe_gbump(__size); 314 _M_gcount += __size; 315 __c = __sb->sgetc(); 316 } 317 else 318 { 319 *__s++ = traits_type::to_char_type(__c); 320 ++_M_gcount; 321 __c = __sb->snextc(); 322 } 323 } 324 325 if (traits_type::eq_int_type(__c, __eof)) 326 __err |= ios_base::eofbit; 327 else if (traits_type::eq_int_type(__c, __idelim)) 328 { 329 ++_M_gcount; 330 __sb->sbumpc(); 331 } 332 else 333 __err |= ios_base::failbit; 334 } 335 __catch(__cxxabiv1::__forced_unwind&) 336 { 337 this->_M_setstate(ios_base::badbit); 338 __throw_exception_again; 339 } 340 __catch(...) 341 { this->_M_setstate(ios_base::badbit); } 342 } 343 // _GLIBCXX_RESOLVE_LIB_DEFECTS 344 // 243. get and getline when sentry reports failure. 345 if (__n > 0) 346 *__s = char_type(); 347 if (!_M_gcount) 348 __err |= ios_base::failbit; 349 if (__err) 350 this->setstate(__err); 351 return *this; 352 } 353 354 template<> 355 basic_istream<wchar_t>& 356 basic_istream<wchar_t>:: ignore(streamsize __n,int_type __delim)357 ignore(streamsize __n, int_type __delim) 358 { 359 #ifdef _GLIBCXX_COMPAT_ 360 if (traits_type::eq_int_type(__delim, traits_type::eof())) 361 return ignore(__n); 362 #endif 363 364 _M_gcount = 0; 365 sentry __cerb(*this, true); 366 if (__n > 0 && __cerb) 367 { 368 ios_base::iostate __err = ios_base::goodbit; 369 __try 370 { 371 const char_type __cdelim = traits_type::to_char_type(__delim); 372 const int_type __eof = traits_type::eof(); 373 __streambuf_type* __sb = this->rdbuf(); 374 int_type __c = __sb->sgetc(); 375 376 bool __large_ignore = false; 377 while (true) 378 { 379 while (_M_gcount < __n 380 && !traits_type::eq_int_type(__c, __eof) 381 && !traits_type::eq_int_type(__c, __delim)) 382 { 383 streamsize __size = std::min(streamsize(__sb->egptr() 384 - __sb->gptr()), 385 streamsize(__n - _M_gcount)); 386 if (__size > 1) 387 { 388 const char_type* __p = traits_type::find(__sb->gptr(), 389 __size, 390 __cdelim); 391 if (__p) 392 __size = __p - __sb->gptr(); 393 __sb->__safe_gbump(__size); 394 _M_gcount += __size; 395 __c = __sb->sgetc(); 396 } 397 else 398 { 399 ++_M_gcount; 400 __c = __sb->snextc(); 401 } 402 } 403 if (__n == __gnu_cxx::__numeric_traits<streamsize>::__max 404 && !traits_type::eq_int_type(__c, __eof) 405 && !traits_type::eq_int_type(__c, __delim)) 406 { 407 _M_gcount = 408 __gnu_cxx::__numeric_traits<streamsize>::__min; 409 __large_ignore = true; 410 } 411 else 412 break; 413 } 414 415 if (__large_ignore) 416 _M_gcount = __gnu_cxx::__numeric_traits<streamsize>::__max; 417 418 if (traits_type::eq_int_type(__c, __eof)) 419 __err |= ios_base::eofbit; 420 else if (traits_type::eq_int_type(__c, __delim)) 421 { 422 if (_M_gcount 423 < __gnu_cxx::__numeric_traits<streamsize>::__max) 424 ++_M_gcount; 425 __sb->sbumpc(); 426 } 427 } 428 __catch(__cxxabiv1::__forced_unwind&) 429 { 430 this->_M_setstate(ios_base::badbit); 431 __throw_exception_again; 432 } 433 __catch(...) 434 { this->_M_setstate(ios_base::badbit); } 435 if (__err) 436 this->setstate(__err); 437 } 438 return *this; 439 } 440 #endif 441 442 443 _GLIBCXX_END_NAMESPACE_VERSION 444 } // namespace 445