| 1 | // -*- C++ -*- |
| 2 | //===----------------------------------------------------------------------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef _LIBCPP_ISTREAM |
| 11 | #define _LIBCPP_ISTREAM |
| 12 | |
| 13 | /* |
| 14 | istream synopsis |
| 15 | |
| 16 | template <class charT, class traits = char_traits<charT> > |
| 17 | class basic_istream |
| 18 | : virtual public basic_ios<charT,traits> |
| 19 | { |
| 20 | public: |
| 21 | // types (inherited from basic_ios (27.5.4)): |
| 22 | typedef charT char_type; |
| 23 | typedef traits traits_type; |
| 24 | typedef typename traits_type::int_type int_type; |
| 25 | typedef typename traits_type::pos_type pos_type; |
| 26 | typedef typename traits_type::off_type off_type; |
| 27 | |
| 28 | // 27.7.1.1.1 Constructor/destructor: |
| 29 | explicit basic_istream(basic_streambuf<char_type, traits_type>* sb); |
| 30 | basic_istream(basic_istream&& rhs); |
| 31 | virtual ~basic_istream(); |
| 32 | |
| 33 | // 27.7.1.1.2 Assign/swap: |
| 34 | basic_istream& operator=(basic_istream&& rhs); |
| 35 | void swap(basic_istream& rhs); |
| 36 | |
| 37 | // 27.7.1.1.3 Prefix/suffix: |
| 38 | class sentry; |
| 39 | |
| 40 | // 27.7.1.2 Formatted input: |
| 41 | basic_istream& operator>>(basic_istream& (*pf)(basic_istream&)); |
| 42 | basic_istream& operator>>(basic_ios<char_type, traits_type>& |
| 43 | (*pf)(basic_ios<char_type, traits_type>&)); |
| 44 | basic_istream& operator>>(ios_base& (*pf)(ios_base&)); |
| 45 | basic_istream& operator>>(basic_streambuf<char_type, traits_type>* sb); |
| 46 | basic_istream& operator>>(bool& n); |
| 47 | basic_istream& operator>>(short& n); |
| 48 | basic_istream& operator>>(unsigned short& n); |
| 49 | basic_istream& operator>>(int& n); |
| 50 | basic_istream& operator>>(unsigned int& n); |
| 51 | basic_istream& operator>>(long& n); |
| 52 | basic_istream& operator>>(unsigned long& n); |
| 53 | basic_istream& operator>>(long long& n); |
| 54 | basic_istream& operator>>(unsigned long long& n); |
| 55 | basic_istream& operator>>(float& f); |
| 56 | basic_istream& operator>>(double& f); |
| 57 | basic_istream& operator>>(long double& f); |
| 58 | basic_istream& operator>>(void*& p); |
| 59 | |
| 60 | // 27.7.1.3 Unformatted input: |
| 61 | streamsize gcount() const; |
| 62 | int_type get(); |
| 63 | basic_istream& get(char_type& c); |
| 64 | basic_istream& get(char_type* s, streamsize n); |
| 65 | basic_istream& get(char_type* s, streamsize n, char_type delim); |
| 66 | basic_istream& get(basic_streambuf<char_type,traits_type>& sb); |
| 67 | basic_istream& get(basic_streambuf<char_type,traits_type>& sb, char_type delim); |
| 68 | |
| 69 | basic_istream& getline(char_type* s, streamsize n); |
| 70 | basic_istream& getline(char_type* s, streamsize n, char_type delim); |
| 71 | |
| 72 | basic_istream& ignore(streamsize n = 1, int_type delim = traits_type::eof()); |
| 73 | basic_istream& ignore(streamsize n, char_type delim); // Since C++26, implemented as a DR |
| 74 | int_type peek(); |
| 75 | basic_istream& read (char_type* s, streamsize n); |
| 76 | streamsize readsome(char_type* s, streamsize n); |
| 77 | |
| 78 | basic_istream& putback(char_type c); |
| 79 | basic_istream& unget(); |
| 80 | int sync(); |
| 81 | |
| 82 | pos_type tellg(); |
| 83 | basic_istream& seekg(pos_type); |
| 84 | basic_istream& seekg(off_type, ios_base::seekdir); |
| 85 | protected: |
| 86 | basic_istream(const basic_istream& rhs) = delete; |
| 87 | basic_istream(basic_istream&& rhs); |
| 88 | // 27.7.2.1.2 Assign/swap: |
| 89 | basic_istream& operator=(const basic_istream& rhs) = delete; |
| 90 | basic_istream& operator=(basic_istream&& rhs); |
| 91 | void swap(basic_istream& rhs); |
| 92 | }; |
| 93 | |
| 94 | // 27.7.1.2.3 character extraction templates: |
| 95 | template<class charT, class traits> |
| 96 | basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT&); |
| 97 | |
| 98 | template<class traits> |
| 99 | basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char&); |
| 100 | |
| 101 | template<class traits> |
| 102 | basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char&); |
| 103 | |
| 104 | template<class charT, class traits> |
| 105 | basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT*); |
| 106 | |
| 107 | template<class traits> |
| 108 | basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char*); |
| 109 | |
| 110 | template<class traits> |
| 111 | basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char*); |
| 112 | |
| 113 | template <class charT, class traits> |
| 114 | void |
| 115 | swap(basic_istream<charT, traits>& x, basic_istream<charT, traits>& y); |
| 116 | |
| 117 | typedef basic_istream<char> istream; |
| 118 | typedef basic_istream<wchar_t> wistream; |
| 119 | |
| 120 | template <class charT, class traits = char_traits<charT> > |
| 121 | class basic_iostream : |
| 122 | public basic_istream<charT,traits>, |
| 123 | public basic_ostream<charT,traits> |
| 124 | { |
| 125 | public: |
| 126 | // types: |
| 127 | typedef charT char_type; |
| 128 | typedef traits traits_type; |
| 129 | typedef typename traits_type::int_type int_type; |
| 130 | typedef typename traits_type::pos_type pos_type; |
| 131 | typedef typename traits_type::off_type off_type; |
| 132 | |
| 133 | // constructor/destructor |
| 134 | explicit basic_iostream(basic_streambuf<char_type, traits_type>* sb); |
| 135 | basic_iostream(basic_iostream&& rhs); |
| 136 | virtual ~basic_iostream(); |
| 137 | |
| 138 | // assign/swap |
| 139 | basic_iostream& operator=(basic_iostream&& rhs); |
| 140 | void swap(basic_iostream& rhs); |
| 141 | }; |
| 142 | |
| 143 | template <class charT, class traits> |
| 144 | void |
| 145 | swap(basic_iostream<charT, traits>& x, basic_iostream<charT, traits>& y); |
| 146 | |
| 147 | typedef basic_iostream<char> iostream; |
| 148 | typedef basic_iostream<wchar_t> wiostream; |
| 149 | |
| 150 | template <class charT, class traits> |
| 151 | basic_istream<charT,traits>& |
| 152 | ws(basic_istream<charT,traits>& is); |
| 153 | |
| 154 | // rvalue stream extraction |
| 155 | template <class Stream, class T> |
| 156 | Stream&& operator>>(Stream&& is, T&& x); |
| 157 | |
| 158 | } // std |
| 159 | |
| 160 | */ |
| 161 | |
| 162 | #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS) |
| 163 | # include <__cxx03/istream> |
| 164 | #else |
| 165 | # include <__config> |
| 166 | |
| 167 | # if _LIBCPP_HAS_LOCALIZATION |
| 168 | |
| 169 | # include <__fwd/istream.h> |
| 170 | # include <__iterator/istreambuf_iterator.h> |
| 171 | # include <__locale_dir/num.h> |
| 172 | # include <__ostream/basic_ostream.h> |
| 173 | # include <__type_traits/conjunction.h> |
| 174 | # include <__type_traits/enable_if.h> |
| 175 | # include <__type_traits/is_base_of.h> |
| 176 | # include <__type_traits/is_same.h> |
| 177 | # include <__type_traits/make_unsigned.h> |
| 178 | # include <__utility/declval.h> |
| 179 | # include <__utility/forward.h> |
| 180 | # include <bitset> |
| 181 | # include <ios> |
| 182 | # include <streambuf> |
| 183 | # include <version> |
| 184 | |
| 185 | # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 186 | # pragma GCC system_header |
| 187 | # endif |
| 188 | |
| 189 | _LIBCPP_PUSH_MACROS |
| 190 | # include <__undef_macros> |
| 191 | |
| 192 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 193 | |
| 194 | template <class _CharT, class _Traits> |
| 195 | class basic_istream : virtual public basic_ios<_CharT, _Traits> { |
| 196 | streamsize __gc_; |
| 197 | |
| 198 | _LIBCPP_HIDE_FROM_ABI void __inc_gcount() { |
| 199 | if (__gc_ < numeric_limits<streamsize>::max()) |
| 200 | ++__gc_; |
| 201 | } |
| 202 | |
| 203 | public: |
| 204 | // types (inherited from basic_ios (27.5.4)): |
| 205 | typedef _CharT char_type; |
| 206 | typedef _Traits traits_type; |
| 207 | typedef typename traits_type::int_type int_type; |
| 208 | typedef typename traits_type::pos_type pos_type; |
| 209 | typedef typename traits_type::off_type off_type; |
| 210 | |
| 211 | // 27.7.1.1.1 Constructor/destructor: |
| 212 | inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 explicit basic_istream(basic_streambuf<char_type, traits_type>* __sb) |
| 213 | : __gc_(0) { |
| 214 | this->init(__sb); |
| 215 | } |
| 216 | ~basic_istream() override; |
| 217 | |
| 218 | protected: |
| 219 | inline _LIBCPP_HIDE_FROM_ABI basic_istream(basic_istream&& __rhs); |
| 220 | |
| 221 | // 27.7.1.1.2 Assign/swap: |
| 222 | inline _LIBCPP_HIDE_FROM_ABI basic_istream& operator=(basic_istream&& __rhs); |
| 223 | |
| 224 | inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 void swap(basic_istream& __rhs) { |
| 225 | std::swap(__gc_, __rhs.__gc_); |
| 226 | basic_ios<char_type, traits_type>::swap(__rhs); |
| 227 | } |
| 228 | |
| 229 | public: |
| 230 | basic_istream(const basic_istream& __rhs) = delete; |
| 231 | basic_istream& operator=(const basic_istream& __rhs) = delete; |
| 232 | |
| 233 | // 27.7.1.1.3 Prefix/suffix: |
| 234 | class sentry; |
| 235 | |
| 236 | // 27.7.1.2 Formatted input: |
| 237 | inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&)) { |
| 238 | return __pf(*this); |
| 239 | } |
| 240 | |
| 241 | inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 basic_istream& |
| 242 | operator>>(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) { |
| 243 | __pf(*this); |
| 244 | return *this; |
| 245 | } |
| 246 | |
| 247 | inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 basic_istream& operator>>(ios_base& (*__pf)(ios_base&)) { |
| 248 | __pf(*this); |
| 249 | return *this; |
| 250 | } |
| 251 | |
| 252 | basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb); |
| 253 | basic_istream& operator>>(bool& __n); |
| 254 | basic_istream& operator>>(short& __n); |
| 255 | basic_istream& operator>>(unsigned short& __n); |
| 256 | basic_istream& operator>>(int& __n); |
| 257 | basic_istream& operator>>(unsigned int& __n); |
| 258 | basic_istream& operator>>(long& __n); |
| 259 | basic_istream& operator>>(unsigned long& __n); |
| 260 | basic_istream& operator>>(long long& __n); |
| 261 | basic_istream& operator>>(unsigned long long& __n); |
| 262 | basic_istream& operator>>(float& __f); |
| 263 | basic_istream& operator>>(double& __f); |
| 264 | basic_istream& operator>>(long double& __f); |
| 265 | basic_istream& operator>>(void*& __p); |
| 266 | |
| 267 | // 27.7.1.3 Unformatted input: |
| 268 | [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI streamsize gcount() const { return __gc_; } |
| 269 | int_type get(); |
| 270 | |
| 271 | inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 basic_istream& get(char_type& __c) { |
| 272 | int_type __ch = get(); |
| 273 | if (__ch != traits_type::eof()) |
| 274 | __c = traits_type::to_char_type(__ch); |
| 275 | return *this; |
| 276 | } |
| 277 | |
| 278 | inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 basic_istream& get(char_type* __s, streamsize __n) { |
| 279 | return get(__s, __n, this->widen('\n')); |
| 280 | } |
| 281 | |
| 282 | basic_istream& get(char_type* __s, streamsize __n, char_type __dlm); |
| 283 | |
| 284 | inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb) { |
| 285 | return get(__sb, this->widen('\n')); |
| 286 | } |
| 287 | |
| 288 | basic_istream& get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm); |
| 289 | |
| 290 | inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 basic_istream& getline(char_type* __s, streamsize __n) { |
| 291 | return getline(__s, __n, this->widen('\n')); |
| 292 | } |
| 293 | |
| 294 | basic_istream& getline(char_type* __s, streamsize __n, char_type __dlm); |
| 295 | |
| 296 | basic_istream& ignore(streamsize __n = 1, int_type __dlm = traits_type::eof()); |
| 297 | template <class _Tp = char_type, __enable_if_t<is_same<_Tp, char>::value, int> = 0> |
| 298 | _LIBCPP_HIDE_FROM_ABI basic_istream& ignore(streamsize __n, char_type __delim) { |
| 299 | return ignore(__n, traits_type::to_int_type(__delim)); |
| 300 | } |
| 301 | int_type peek(); |
| 302 | basic_istream& read(char_type* __s, streamsize __n); |
| 303 | streamsize readsome(char_type* __s, streamsize __n); |
| 304 | |
| 305 | basic_istream& putback(char_type __c); |
| 306 | basic_istream& unget(); |
| 307 | int sync(); |
| 308 | |
| 309 | [[__nodiscard__]] pos_type tellg(); |
| 310 | basic_istream& seekg(pos_type __pos); |
| 311 | basic_istream& seekg(off_type __off, ios_base::seekdir __dir); |
| 312 | }; |
| 313 | |
| 314 | template <class _CharT, class _Traits> |
| 315 | class basic_istream<_CharT, _Traits>::sentry { |
| 316 | bool __ok_; |
| 317 | |
| 318 | public: |
| 319 | explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false); |
| 320 | // ~sentry() = default; |
| 321 | |
| 322 | _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; } |
| 323 | |
| 324 | sentry(const sentry&) = delete; |
| 325 | sentry& operator=(const sentry&) = delete; |
| 326 | }; |
| 327 | |
| 328 | template <class _CharT, class _Traits> |
| 329 | basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws) : __ok_(false) { |
| 330 | if (__is.good()) { |
| 331 | if (__is.tie()) |
| 332 | __is.tie()->flush(); |
| 333 | if (!__noskipws && (__is.flags() & ios_base::skipws)) { |
| 334 | typedef istreambuf_iterator<_CharT, _Traits> _Ip; |
| 335 | const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc()); |
| 336 | _Ip __i(__is); |
| 337 | _Ip __eof; |
| 338 | for (; __i != __eof; ++__i) |
| 339 | if (!__ct.is(__ct.space, *__i)) |
| 340 | break; |
| 341 | if (__i == __eof) |
| 342 | __is.setstate(ios_base::failbit | ios_base::eofbit); |
| 343 | } |
| 344 | __ok_ = __is.good(); |
| 345 | } else |
| 346 | __is.setstate(ios_base::failbit); |
| 347 | } |
| 348 | |
| 349 | template <class _CharT, class _Traits> |
| 350 | basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs) : __gc_(__rhs.__gc_) { |
| 351 | __rhs.__gc_ = 0; |
| 352 | this->move(__rhs); |
| 353 | } |
| 354 | |
| 355 | template <class _CharT, class _Traits> |
| 356 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs) { |
| 357 | swap(__rhs); |
| 358 | return *this; |
| 359 | } |
| 360 | |
| 361 | template <class _CharT, class _Traits> |
| 362 | basic_istream<_CharT, _Traits>::~basic_istream() {} |
| 363 | |
| 364 | template <class _Tp, class _CharT, class _Traits> |
| 365 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 366 | __input_arithmetic(basic_istream<_CharT, _Traits>& __is, _Tp& __n) { |
| 367 | ios_base::iostate __state = ios_base::goodbit; |
| 368 | typename basic_istream<_CharT, _Traits>::sentry __s(__is); |
| 369 | if (__s) { |
| 370 | # if _LIBCPP_HAS_EXCEPTIONS |
| 371 | try { |
| 372 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 373 | typedef istreambuf_iterator<_CharT, _Traits> _Ip; |
| 374 | typedef num_get<_CharT, _Ip> _Fp; |
| 375 | std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __n); |
| 376 | # if _LIBCPP_HAS_EXCEPTIONS |
| 377 | } catch (...) { |
| 378 | __state |= ios_base::badbit; |
| 379 | __is.__setstate_nothrow(__state); |
| 380 | if (__is.exceptions() & ios_base::badbit) { |
| 381 | throw; |
| 382 | } |
| 383 | } |
| 384 | # endif |
| 385 | __is.setstate(__state); |
| 386 | } |
| 387 | return __is; |
| 388 | } |
| 389 | |
| 390 | template <class _CharT, class _Traits> |
| 391 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n) { |
| 392 | return std::__input_arithmetic<unsigned short>(*this, __n); |
| 393 | } |
| 394 | |
| 395 | template <class _CharT, class _Traits> |
| 396 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n) { |
| 397 | return std::__input_arithmetic<unsigned int>(*this, __n); |
| 398 | } |
| 399 | |
| 400 | template <class _CharT, class _Traits> |
| 401 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long& __n) { |
| 402 | return std::__input_arithmetic<long>(*this, __n); |
| 403 | } |
| 404 | |
| 405 | template <class _CharT, class _Traits> |
| 406 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n) { |
| 407 | return std::__input_arithmetic<unsigned long>(*this, __n); |
| 408 | } |
| 409 | |
| 410 | template <class _CharT, class _Traits> |
| 411 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long long& __n) { |
| 412 | return std::__input_arithmetic<long long>(*this, __n); |
| 413 | } |
| 414 | |
| 415 | template <class _CharT, class _Traits> |
| 416 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n) { |
| 417 | return std::__input_arithmetic<unsigned long long>(*this, __n); |
| 418 | } |
| 419 | |
| 420 | template <class _CharT, class _Traits> |
| 421 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(float& __n) { |
| 422 | return std::__input_arithmetic<float>(*this, __n); |
| 423 | } |
| 424 | |
| 425 | template <class _CharT, class _Traits> |
| 426 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(double& __n) { |
| 427 | return std::__input_arithmetic<double>(*this, __n); |
| 428 | } |
| 429 | |
| 430 | template <class _CharT, class _Traits> |
| 431 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long double& __n) { |
| 432 | return std::__input_arithmetic<long double>(*this, __n); |
| 433 | } |
| 434 | |
| 435 | template <class _CharT, class _Traits> |
| 436 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(bool& __n) { |
| 437 | return std::__input_arithmetic<bool>(*this, __n); |
| 438 | } |
| 439 | |
| 440 | template <class _CharT, class _Traits> |
| 441 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(void*& __n) { |
| 442 | return std::__input_arithmetic<void*>(*this, __n); |
| 443 | } |
| 444 | |
| 445 | template <class _Tp, class _CharT, class _Traits> |
| 446 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 447 | __input_arithmetic_with_numeric_limits(basic_istream<_CharT, _Traits>& __is, _Tp& __n) { |
| 448 | ios_base::iostate __state = ios_base::goodbit; |
| 449 | typename basic_istream<_CharT, _Traits>::sentry __s(__is); |
| 450 | if (__s) { |
| 451 | # if _LIBCPP_HAS_EXCEPTIONS |
| 452 | try { |
| 453 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 454 | typedef istreambuf_iterator<_CharT, _Traits> _Ip; |
| 455 | typedef num_get<_CharT, _Ip> _Fp; |
| 456 | long __temp; |
| 457 | std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __temp); |
| 458 | if (__temp < numeric_limits<_Tp>::min()) { |
| 459 | __state |= ios_base::failbit; |
| 460 | __n = numeric_limits<_Tp>::min(); |
| 461 | } else if (__temp > numeric_limits<_Tp>::max()) { |
| 462 | __state |= ios_base::failbit; |
| 463 | __n = numeric_limits<_Tp>::max(); |
| 464 | } else { |
| 465 | __n = static_cast<_Tp>(__temp); |
| 466 | } |
| 467 | # if _LIBCPP_HAS_EXCEPTIONS |
| 468 | } catch (...) { |
| 469 | __state |= ios_base::badbit; |
| 470 | __is.__setstate_nothrow(__state); |
| 471 | if (__is.exceptions() & ios_base::badbit) { |
| 472 | throw; |
| 473 | } |
| 474 | } |
| 475 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 476 | __is.setstate(__state); |
| 477 | } |
| 478 | return __is; |
| 479 | } |
| 480 | |
| 481 | template <class _CharT, class _Traits> |
| 482 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(short& __n) { |
| 483 | return std::__input_arithmetic_with_numeric_limits<short>(*this, __n); |
| 484 | } |
| 485 | |
| 486 | template <class _CharT, class _Traits> |
| 487 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(int& __n) { |
| 488 | return std::__input_arithmetic_with_numeric_limits<int>(*this, __n); |
| 489 | } |
| 490 | |
| 491 | template <class _CharT, class _Traits> |
| 492 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 493 | __input_c_string(basic_istream<_CharT, _Traits>& __is, _CharT* __p, size_t __n) { |
| 494 | ios_base::iostate __state = ios_base::goodbit; |
| 495 | typename basic_istream<_CharT, _Traits>::sentry __sen(__is); |
| 496 | if (__sen) { |
| 497 | # if _LIBCPP_HAS_EXCEPTIONS |
| 498 | try { |
| 499 | # endif |
| 500 | _CharT* __s = __p; |
| 501 | const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc()); |
| 502 | while (__s != __p + (__n - 1)) { |
| 503 | typename _Traits::int_type __i = __is.rdbuf()->sgetc(); |
| 504 | if (_Traits::eq_int_type(__i, _Traits::eof())) { |
| 505 | __state |= ios_base::eofbit; |
| 506 | break; |
| 507 | } |
| 508 | _CharT __ch = _Traits::to_char_type(__i); |
| 509 | if (__ct.is(__ct.space, __ch)) |
| 510 | break; |
| 511 | *__s++ = __ch; |
| 512 | __is.rdbuf()->sbumpc(); |
| 513 | } |
| 514 | *__s = _CharT(); |
| 515 | __is.width(0); |
| 516 | if (__s == __p) |
| 517 | __state |= ios_base::failbit; |
| 518 | # if _LIBCPP_HAS_EXCEPTIONS |
| 519 | } catch (...) { |
| 520 | __state |= ios_base::badbit; |
| 521 | __is.__setstate_nothrow(__state); |
| 522 | if (__is.exceptions() & ios_base::badbit) { |
| 523 | throw; |
| 524 | } |
| 525 | } |
| 526 | # endif |
| 527 | __is.setstate(__state); |
| 528 | } |
| 529 | return __is; |
| 530 | } |
| 531 | |
| 532 | # if _LIBCPP_STD_VER >= 20 |
| 533 | |
| 534 | template <class _CharT, class _Traits, size_t _Np> |
| 535 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 536 | operator>>(basic_istream<_CharT, _Traits>& __is, _CharT (&__buf)[_Np]) { |
| 537 | size_t __n = _Np; |
| 538 | if (__is.width() > 0) |
| 539 | __n = std::min(a: size_t(__is.width()), b: _Np); |
| 540 | return std::__input_c_string(__is, __buf, __n); |
| 541 | } |
| 542 | |
| 543 | template <class _Traits, size_t _Np> |
| 544 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>& |
| 545 | operator>>(basic_istream<char, _Traits>& __is, unsigned char (&__buf)[_Np]) { |
| 546 | return __is >> (char(&)[_Np])__buf; |
| 547 | } |
| 548 | |
| 549 | template <class _Traits, size_t _Np> |
| 550 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>& |
| 551 | operator>>(basic_istream<char, _Traits>& __is, signed char (&__buf)[_Np]) { |
| 552 | return __is >> (char(&)[_Np])__buf; |
| 553 | } |
| 554 | |
| 555 | # else |
| 556 | |
| 557 | template <class _CharT, class _Traits> |
| 558 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 559 | operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s) { |
| 560 | streamsize __n = __is.width(); |
| 561 | if (__n <= 0) |
| 562 | __n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1; |
| 563 | return std::__input_c_string(__is, __s, size_t(__n)); |
| 564 | } |
| 565 | |
| 566 | template <class _Traits> |
| 567 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>& |
| 568 | operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s) { |
| 569 | return __is >> (char*)__s; |
| 570 | } |
| 571 | |
| 572 | template <class _Traits> |
| 573 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>& |
| 574 | operator>>(basic_istream<char, _Traits>& __is, signed char* __s) { |
| 575 | return __is >> (char*)__s; |
| 576 | } |
| 577 | |
| 578 | # endif // _LIBCPP_STD_VER >= 20 |
| 579 | |
| 580 | template <class _CharT, class _Traits> |
| 581 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c) { |
| 582 | ios_base::iostate __state = ios_base::goodbit; |
| 583 | typename basic_istream<_CharT, _Traits>::sentry __sen(__is); |
| 584 | if (__sen) { |
| 585 | # if _LIBCPP_HAS_EXCEPTIONS |
| 586 | try { |
| 587 | # endif |
| 588 | typename _Traits::int_type __i = __is.rdbuf()->sbumpc(); |
| 589 | if (_Traits::eq_int_type(__i, _Traits::eof())) |
| 590 | __state |= ios_base::eofbit | ios_base::failbit; |
| 591 | else |
| 592 | __c = _Traits::to_char_type(__i); |
| 593 | # if _LIBCPP_HAS_EXCEPTIONS |
| 594 | } catch (...) { |
| 595 | __state |= ios_base::badbit; |
| 596 | __is.__setstate_nothrow(__state); |
| 597 | if (__is.exceptions() & ios_base::badbit) { |
| 598 | throw; |
| 599 | } |
| 600 | } |
| 601 | # endif |
| 602 | __is.setstate(__state); |
| 603 | } |
| 604 | return __is; |
| 605 | } |
| 606 | |
| 607 | template <class _Traits> |
| 608 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>& |
| 609 | operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c) { |
| 610 | return __is >> (char&)__c; |
| 611 | } |
| 612 | |
| 613 | template <class _Traits> |
| 614 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>& |
| 615 | operator>>(basic_istream<char, _Traits>& __is, signed char& __c) { |
| 616 | return __is >> (char&)__c; |
| 617 | } |
| 618 | |
| 619 | template <class _CharT, class _Traits> |
| 620 | basic_istream<_CharT, _Traits>& |
| 621 | basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_type>* __sb) { |
| 622 | ios_base::iostate __state = ios_base::goodbit; |
| 623 | __gc_ = 0; |
| 624 | sentry __s(*this, true); |
| 625 | if (__s) { |
| 626 | if (__sb) { |
| 627 | # if _LIBCPP_HAS_EXCEPTIONS |
| 628 | try { |
| 629 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 630 | while (true) { |
| 631 | typename traits_type::int_type __i = this->rdbuf()->sgetc(); |
| 632 | if (traits_type::eq_int_type(__i, _Traits::eof())) { |
| 633 | __state |= ios_base::eofbit; |
| 634 | break; |
| 635 | } |
| 636 | if (traits_type::eq_int_type(__sb->sputc(traits_type::to_char_type(__i)), traits_type::eof())) |
| 637 | break; |
| 638 | __inc_gcount(); |
| 639 | this->rdbuf()->sbumpc(); |
| 640 | } |
| 641 | if (__gc_ == 0) |
| 642 | __state |= ios_base::failbit; |
| 643 | # if _LIBCPP_HAS_EXCEPTIONS |
| 644 | } catch (...) { |
| 645 | __state |= ios_base::badbit; |
| 646 | if (__gc_ == 0) |
| 647 | __state |= ios_base::failbit; |
| 648 | |
| 649 | this->__setstate_nothrow(__state); |
| 650 | if (this->exceptions() & ios_base::failbit || this->exceptions() & ios_base::badbit) { |
| 651 | throw; |
| 652 | } |
| 653 | } |
| 654 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 655 | } else { |
| 656 | __state |= ios_base::failbit; |
| 657 | } |
| 658 | this->setstate(__state); |
| 659 | } |
| 660 | return *this; |
| 661 | } |
| 662 | |
| 663 | template <class _CharT, class _Traits> |
| 664 | typename basic_istream<_CharT, _Traits>::int_type basic_istream<_CharT, _Traits>::get() { |
| 665 | ios_base::iostate __state = ios_base::goodbit; |
| 666 | __gc_ = 0; |
| 667 | int_type __r = traits_type::eof(); |
| 668 | sentry __s(*this, true); |
| 669 | if (__s) { |
| 670 | # if _LIBCPP_HAS_EXCEPTIONS |
| 671 | try { |
| 672 | # endif |
| 673 | __r = this->rdbuf()->sbumpc(); |
| 674 | if (traits_type::eq_int_type(__r, traits_type::eof())) |
| 675 | __state |= ios_base::failbit | ios_base::eofbit; |
| 676 | else |
| 677 | __gc_ = 1; |
| 678 | # if _LIBCPP_HAS_EXCEPTIONS |
| 679 | } catch (...) { |
| 680 | this->__setstate_nothrow(this->rdstate() | ios_base::badbit); |
| 681 | if (this->exceptions() & ios_base::badbit) { |
| 682 | throw; |
| 683 | } |
| 684 | } |
| 685 | # endif |
| 686 | this->setstate(__state); |
| 687 | } |
| 688 | return __r; |
| 689 | } |
| 690 | |
| 691 | template <class _CharT, class _Traits> |
| 692 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __dlm) { |
| 693 | ios_base::iostate __state = ios_base::goodbit; |
| 694 | __gc_ = 0; |
| 695 | sentry __sen(*this, true); |
| 696 | if (__sen) { |
| 697 | if (__n > 0) { |
| 698 | # if _LIBCPP_HAS_EXCEPTIONS |
| 699 | try { |
| 700 | # endif |
| 701 | while (__gc_ < __n - 1) { |
| 702 | int_type __i = this->rdbuf()->sgetc(); |
| 703 | if (traits_type::eq_int_type(__i, traits_type::eof())) { |
| 704 | __state |= ios_base::eofbit; |
| 705 | break; |
| 706 | } |
| 707 | char_type __ch = traits_type::to_char_type(__i); |
| 708 | if (traits_type::eq(__ch, __dlm)) |
| 709 | break; |
| 710 | *__s++ = __ch; |
| 711 | __inc_gcount(); |
| 712 | this->rdbuf()->sbumpc(); |
| 713 | } |
| 714 | if (__gc_ == 0) |
| 715 | __state |= ios_base::failbit; |
| 716 | # if _LIBCPP_HAS_EXCEPTIONS |
| 717 | } catch (...) { |
| 718 | __state |= ios_base::badbit; |
| 719 | this->__setstate_nothrow(__state); |
| 720 | if (this->exceptions() & ios_base::badbit) { |
| 721 | if (__n > 0) |
| 722 | *__s = char_type(); |
| 723 | throw; |
| 724 | } |
| 725 | } |
| 726 | # endif |
| 727 | } else { |
| 728 | __state |= ios_base::failbit; |
| 729 | } |
| 730 | |
| 731 | if (__n > 0) |
| 732 | *__s = char_type(); |
| 733 | this->setstate(__state); |
| 734 | } |
| 735 | if (__n > 0) |
| 736 | *__s = char_type(); |
| 737 | return *this; |
| 738 | } |
| 739 | |
| 740 | template <class _CharT, class _Traits> |
| 741 | basic_istream<_CharT, _Traits>& |
| 742 | basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm) { |
| 743 | ios_base::iostate __state = ios_base::goodbit; |
| 744 | __gc_ = 0; |
| 745 | sentry __sen(*this, true); |
| 746 | if (__sen) { |
| 747 | # if _LIBCPP_HAS_EXCEPTIONS |
| 748 | try { |
| 749 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 750 | while (true) { |
| 751 | typename traits_type::int_type __i = this->rdbuf()->sgetc(); |
| 752 | if (traits_type::eq_int_type(__i, traits_type::eof())) { |
| 753 | __state |= ios_base::eofbit; |
| 754 | break; |
| 755 | } |
| 756 | char_type __ch = traits_type::to_char_type(__i); |
| 757 | if (traits_type::eq(__ch, __dlm)) |
| 758 | break; |
| 759 | if (traits_type::eq_int_type(__sb.sputc(__ch), traits_type::eof())) |
| 760 | break; |
| 761 | __inc_gcount(); |
| 762 | this->rdbuf()->sbumpc(); |
| 763 | } |
| 764 | # if _LIBCPP_HAS_EXCEPTIONS |
| 765 | } catch (...) { |
| 766 | __state |= ios_base::badbit; |
| 767 | // according to the spec, exceptions here are caught but not rethrown |
| 768 | } |
| 769 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 770 | if (__gc_ == 0) |
| 771 | __state |= ios_base::failbit; |
| 772 | this->setstate(__state); |
| 773 | } |
| 774 | return *this; |
| 775 | } |
| 776 | |
| 777 | template <class _CharT, class _Traits> |
| 778 | basic_istream<_CharT, _Traits>& |
| 779 | basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_type __dlm) { |
| 780 | ios_base::iostate __state = ios_base::goodbit; |
| 781 | __gc_ = 0; |
| 782 | sentry __sen(*this, true); |
| 783 | if (__sen) { |
| 784 | # if _LIBCPP_HAS_EXCEPTIONS |
| 785 | try { |
| 786 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 787 | while (true) { |
| 788 | typename traits_type::int_type __i = this->rdbuf()->sgetc(); |
| 789 | if (traits_type::eq_int_type(__i, traits_type::eof())) { |
| 790 | __state |= ios_base::eofbit; |
| 791 | break; |
| 792 | } |
| 793 | char_type __ch = traits_type::to_char_type(__i); |
| 794 | if (traits_type::eq(__ch, __dlm)) { |
| 795 | this->rdbuf()->sbumpc(); |
| 796 | __inc_gcount(); |
| 797 | break; |
| 798 | } |
| 799 | if (__gc_ >= __n - 1) { |
| 800 | __state |= ios_base::failbit; |
| 801 | break; |
| 802 | } |
| 803 | *__s++ = __ch; |
| 804 | this->rdbuf()->sbumpc(); |
| 805 | __inc_gcount(); |
| 806 | } |
| 807 | # if _LIBCPP_HAS_EXCEPTIONS |
| 808 | } catch (...) { |
| 809 | __state |= ios_base::badbit; |
| 810 | this->__setstate_nothrow(__state); |
| 811 | if (this->exceptions() & ios_base::badbit) { |
| 812 | if (__n > 0) |
| 813 | *__s = char_type(); |
| 814 | if (__gc_ == 0) |
| 815 | __state |= ios_base::failbit; |
| 816 | throw; |
| 817 | } |
| 818 | } |
| 819 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 820 | } |
| 821 | if (__n > 0) |
| 822 | *__s = char_type(); |
| 823 | if (__gc_ == 0) |
| 824 | __state |= ios_base::failbit; |
| 825 | this->setstate(__state); |
| 826 | return *this; |
| 827 | } |
| 828 | |
| 829 | template <class _CharT, class _Traits> |
| 830 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm) { |
| 831 | ios_base::iostate __state = ios_base::goodbit; |
| 832 | __gc_ = 0; |
| 833 | sentry __sen(*this, true); |
| 834 | if (__sen) { |
| 835 | # if _LIBCPP_HAS_EXCEPTIONS |
| 836 | try { |
| 837 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 838 | if (__n == numeric_limits<streamsize>::max()) { |
| 839 | while (true) { |
| 840 | typename traits_type::int_type __i = this->rdbuf()->sbumpc(); |
| 841 | if (traits_type::eq_int_type(__i, traits_type::eof())) { |
| 842 | __state |= ios_base::eofbit; |
| 843 | break; |
| 844 | } |
| 845 | __inc_gcount(); |
| 846 | if (traits_type::eq_int_type(__i, __dlm)) |
| 847 | break; |
| 848 | } |
| 849 | } else { |
| 850 | while (__gc_ < __n) { |
| 851 | typename traits_type::int_type __i = this->rdbuf()->sbumpc(); |
| 852 | if (traits_type::eq_int_type(__i, traits_type::eof())) { |
| 853 | __state |= ios_base::eofbit; |
| 854 | break; |
| 855 | } |
| 856 | __inc_gcount(); |
| 857 | if (traits_type::eq_int_type(__i, __dlm)) |
| 858 | break; |
| 859 | } |
| 860 | } |
| 861 | # if _LIBCPP_HAS_EXCEPTIONS |
| 862 | } catch (...) { |
| 863 | __state |= ios_base::badbit; |
| 864 | this->__setstate_nothrow(__state); |
| 865 | if (this->exceptions() & ios_base::badbit) { |
| 866 | throw; |
| 867 | } |
| 868 | } |
| 869 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 870 | this->setstate(__state); |
| 871 | } |
| 872 | return *this; |
| 873 | } |
| 874 | |
| 875 | template <class _CharT, class _Traits> |
| 876 | typename basic_istream<_CharT, _Traits>::int_type basic_istream<_CharT, _Traits>::peek() { |
| 877 | ios_base::iostate __state = ios_base::goodbit; |
| 878 | __gc_ = 0; |
| 879 | int_type __r = traits_type::eof(); |
| 880 | sentry __sen(*this, true); |
| 881 | if (__sen) { |
| 882 | # if _LIBCPP_HAS_EXCEPTIONS |
| 883 | try { |
| 884 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 885 | __r = this->rdbuf()->sgetc(); |
| 886 | if (traits_type::eq_int_type(__r, traits_type::eof())) |
| 887 | __state |= ios_base::eofbit; |
| 888 | # if _LIBCPP_HAS_EXCEPTIONS |
| 889 | } catch (...) { |
| 890 | __state |= ios_base::badbit; |
| 891 | this->__setstate_nothrow(__state); |
| 892 | if (this->exceptions() & ios_base::badbit) { |
| 893 | throw; |
| 894 | } |
| 895 | } |
| 896 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 897 | this->setstate(__state); |
| 898 | } |
| 899 | return __r; |
| 900 | } |
| 901 | |
| 902 | template <class _CharT, class _Traits> |
| 903 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n) { |
| 904 | ios_base::iostate __state = ios_base::goodbit; |
| 905 | __gc_ = 0; |
| 906 | sentry __sen(*this, true); |
| 907 | if (__sen) { |
| 908 | # if _LIBCPP_HAS_EXCEPTIONS |
| 909 | try { |
| 910 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 911 | __gc_ = this->rdbuf()->sgetn(__s, __n); |
| 912 | if (__gc_ != __n) |
| 913 | __state |= ios_base::failbit | ios_base::eofbit; |
| 914 | # if _LIBCPP_HAS_EXCEPTIONS |
| 915 | } catch (...) { |
| 916 | __state |= ios_base::badbit; |
| 917 | this->__setstate_nothrow(__state); |
| 918 | if (this->exceptions() & ios_base::badbit) { |
| 919 | throw; |
| 920 | } |
| 921 | } |
| 922 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 923 | } else { |
| 924 | __state |= ios_base::failbit; |
| 925 | } |
| 926 | this->setstate(__state); |
| 927 | return *this; |
| 928 | } |
| 929 | |
| 930 | template <class _CharT, class _Traits> |
| 931 | streamsize basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n) { |
| 932 | ios_base::iostate __state = ios_base::goodbit; |
| 933 | __gc_ = 0; |
| 934 | sentry __sen(*this, true); |
| 935 | if (__sen) { |
| 936 | # if _LIBCPP_HAS_EXCEPTIONS |
| 937 | try { |
| 938 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 939 | streamsize __c = this->rdbuf()->in_avail(); |
| 940 | switch (__c) { |
| 941 | case -1: |
| 942 | __state |= ios_base::eofbit; |
| 943 | break; |
| 944 | case 0: |
| 945 | break; |
| 946 | default: |
| 947 | __n = std::min(a: __c, b: __n); |
| 948 | __gc_ = this->rdbuf()->sgetn(__s, __n); |
| 949 | if (__gc_ != __n) |
| 950 | __state |= ios_base::failbit | ios_base::eofbit; |
| 951 | break; |
| 952 | } |
| 953 | # if _LIBCPP_HAS_EXCEPTIONS |
| 954 | } catch (...) { |
| 955 | __state |= ios_base::badbit; |
| 956 | this->__setstate_nothrow(__state); |
| 957 | if (this->exceptions() & ios_base::badbit) { |
| 958 | throw; |
| 959 | } |
| 960 | } |
| 961 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 962 | } else { |
| 963 | __state |= ios_base::failbit; |
| 964 | } |
| 965 | this->setstate(__state); |
| 966 | return __gc_; |
| 967 | } |
| 968 | |
| 969 | template <class _CharT, class _Traits> |
| 970 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::putback(char_type __c) { |
| 971 | ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit; |
| 972 | __gc_ = 0; |
| 973 | this->clear(__state); |
| 974 | sentry __sen(*this, true); |
| 975 | if (__sen) { |
| 976 | # if _LIBCPP_HAS_EXCEPTIONS |
| 977 | try { |
| 978 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 979 | if (this->rdbuf() == nullptr || this->rdbuf()->sputbackc(__c) == traits_type::eof()) |
| 980 | __state |= ios_base::badbit; |
| 981 | # if _LIBCPP_HAS_EXCEPTIONS |
| 982 | } catch (...) { |
| 983 | __state |= ios_base::badbit; |
| 984 | this->__setstate_nothrow(__state); |
| 985 | if (this->exceptions() & ios_base::badbit) { |
| 986 | throw; |
| 987 | } |
| 988 | } |
| 989 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 990 | } else { |
| 991 | __state |= ios_base::failbit; |
| 992 | } |
| 993 | this->setstate(__state); |
| 994 | return *this; |
| 995 | } |
| 996 | |
| 997 | template <class _CharT, class _Traits> |
| 998 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::unget() { |
| 999 | ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit; |
| 1000 | __gc_ = 0; |
| 1001 | this->clear(__state); |
| 1002 | sentry __sen(*this, true); |
| 1003 | if (__sen) { |
| 1004 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1005 | try { |
| 1006 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1007 | if (this->rdbuf() == nullptr || this->rdbuf()->sungetc() == traits_type::eof()) |
| 1008 | __state |= ios_base::badbit; |
| 1009 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1010 | } catch (...) { |
| 1011 | __state |= ios_base::badbit; |
| 1012 | this->__setstate_nothrow(__state); |
| 1013 | if (this->exceptions() & ios_base::badbit) { |
| 1014 | throw; |
| 1015 | } |
| 1016 | } |
| 1017 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1018 | } else { |
| 1019 | __state |= ios_base::failbit; |
| 1020 | } |
| 1021 | this->setstate(__state); |
| 1022 | return *this; |
| 1023 | } |
| 1024 | |
| 1025 | template <class _CharT, class _Traits> |
| 1026 | int basic_istream<_CharT, _Traits>::sync() { |
| 1027 | ios_base::iostate __state = ios_base::goodbit; |
| 1028 | sentry __sen(*this, true); |
| 1029 | if (this->rdbuf() == nullptr) |
| 1030 | return -1; |
| 1031 | |
| 1032 | int __r = 0; |
| 1033 | if (__sen) { |
| 1034 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1035 | try { |
| 1036 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1037 | if (this->rdbuf()->pubsync() == -1) { |
| 1038 | __state |= ios_base::badbit; |
| 1039 | __r = -1; |
| 1040 | } |
| 1041 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1042 | } catch (...) { |
| 1043 | __state |= ios_base::badbit; |
| 1044 | this->__setstate_nothrow(__state); |
| 1045 | if (this->exceptions() & ios_base::badbit) { |
| 1046 | throw; |
| 1047 | } |
| 1048 | } |
| 1049 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1050 | this->setstate(__state); |
| 1051 | } |
| 1052 | return __r; |
| 1053 | } |
| 1054 | |
| 1055 | template <class _CharT, class _Traits> |
| 1056 | typename basic_istream<_CharT, _Traits>::pos_type basic_istream<_CharT, _Traits>::tellg() { |
| 1057 | ios_base::iostate __state = ios_base::goodbit; |
| 1058 | pos_type __r(-1); |
| 1059 | sentry __sen(*this, true); |
| 1060 | if (__sen) { |
| 1061 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1062 | try { |
| 1063 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1064 | __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in); |
| 1065 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1066 | } catch (...) { |
| 1067 | __state |= ios_base::badbit; |
| 1068 | this->__setstate_nothrow(__state); |
| 1069 | if (this->exceptions() & ios_base::badbit) { |
| 1070 | throw; |
| 1071 | } |
| 1072 | } |
| 1073 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1074 | this->setstate(__state); |
| 1075 | } |
| 1076 | return __r; |
| 1077 | } |
| 1078 | |
| 1079 | template <class _CharT, class _Traits> |
| 1080 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::seekg(pos_type __pos) { |
| 1081 | ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit; |
| 1082 | this->clear(__state); |
| 1083 | sentry __sen(*this, true); |
| 1084 | if (__sen) { |
| 1085 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1086 | try { |
| 1087 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1088 | if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1)) |
| 1089 | __state |= ios_base::failbit; |
| 1090 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1091 | } catch (...) { |
| 1092 | __state |= ios_base::badbit; |
| 1093 | this->__setstate_nothrow(__state); |
| 1094 | if (this->exceptions() & ios_base::badbit) { |
| 1095 | throw; |
| 1096 | } |
| 1097 | } |
| 1098 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1099 | this->setstate(__state); |
| 1100 | } |
| 1101 | return *this; |
| 1102 | } |
| 1103 | |
| 1104 | template <class _CharT, class _Traits> |
| 1105 | basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir) { |
| 1106 | ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit; |
| 1107 | this->clear(__state); |
| 1108 | sentry __sen(*this, true); |
| 1109 | if (__sen) { |
| 1110 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1111 | try { |
| 1112 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1113 | if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::in) == pos_type(-1)) |
| 1114 | __state |= ios_base::failbit; |
| 1115 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1116 | } catch (...) { |
| 1117 | __state |= ios_base::badbit; |
| 1118 | this->__setstate_nothrow(__state); |
| 1119 | if (this->exceptions() & ios_base::badbit) { |
| 1120 | throw; |
| 1121 | } |
| 1122 | } |
| 1123 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1124 | this->setstate(__state); |
| 1125 | } |
| 1126 | return *this; |
| 1127 | } |
| 1128 | |
| 1129 | template <class _CharT, class _Traits> |
| 1130 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& ws(basic_istream<_CharT, _Traits>& __is) { |
| 1131 | ios_base::iostate __state = ios_base::goodbit; |
| 1132 | typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true); |
| 1133 | if (__sen) { |
| 1134 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1135 | try { |
| 1136 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1137 | const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc()); |
| 1138 | while (true) { |
| 1139 | typename _Traits::int_type __i = __is.rdbuf()->sgetc(); |
| 1140 | if (_Traits::eq_int_type(__i, _Traits::eof())) { |
| 1141 | __state |= ios_base::eofbit; |
| 1142 | break; |
| 1143 | } |
| 1144 | if (!__ct.is(__ct.space, _Traits::to_char_type(__i))) |
| 1145 | break; |
| 1146 | __is.rdbuf()->sbumpc(); |
| 1147 | } |
| 1148 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1149 | } catch (...) { |
| 1150 | __state |= ios_base::badbit; |
| 1151 | __is.__setstate_nothrow(__state); |
| 1152 | if (__is.exceptions() & ios_base::badbit) { |
| 1153 | throw; |
| 1154 | } |
| 1155 | } |
| 1156 | # endif // _LIBCPP_HAS_EXCEPTIONS |
| 1157 | __is.setstate(__state); |
| 1158 | } |
| 1159 | return __is; |
| 1160 | } |
| 1161 | |
| 1162 | template <class _Stream, class _Tp, class = void> |
| 1163 | struct __is_istreamable : false_type {}; |
| 1164 | |
| 1165 | template <class _Stream, class _Tp> |
| 1166 | struct __is_istreamable<_Stream, _Tp, decltype(std::declval<_Stream>() >> std::declval<_Tp>(), void())> : true_type {}; |
| 1167 | |
| 1168 | template <class _Stream, |
| 1169 | class _Tp, |
| 1170 | __enable_if_t< _And<is_base_of<ios_base, _Stream>, __is_istreamable<_Stream&, _Tp&&> >::value, int> = 0> |
| 1171 | _LIBCPP_HIDE_FROM_ABI _Stream&& operator>>(_Stream&& __is, _Tp&& __x) { |
| 1172 | __is >> std::forward<_Tp>(__x); |
| 1173 | return std::move(__is); |
| 1174 | } |
| 1175 | |
| 1176 | template <class _CharT, class _Traits> |
| 1177 | class basic_iostream : public basic_istream<_CharT, _Traits>, public basic_ostream<_CharT, _Traits> { |
| 1178 | public: |
| 1179 | // types: |
| 1180 | typedef _CharT char_type; |
| 1181 | typedef _Traits traits_type; |
| 1182 | typedef typename traits_type::int_type int_type; |
| 1183 | typedef typename traits_type::pos_type pos_type; |
| 1184 | typedef typename traits_type::off_type off_type; |
| 1185 | |
| 1186 | // constructor/destructor |
| 1187 | inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb) |
| 1188 | : basic_istream<_CharT, _Traits>(__sb) {} |
| 1189 | |
| 1190 | ~basic_iostream() override; |
| 1191 | |
| 1192 | protected: |
| 1193 | inline _LIBCPP_HIDE_FROM_ABI basic_iostream(basic_iostream&& __rhs); |
| 1194 | |
| 1195 | // assign/swap |
| 1196 | inline _LIBCPP_HIDE_FROM_ABI basic_iostream& operator=(basic_iostream&& __rhs); |
| 1197 | |
| 1198 | inline _LIBCPP_HIDE_FROM_ABI_SINCE_LLVM8 void swap(basic_iostream& __rhs) { |
| 1199 | basic_istream<char_type, traits_type>::swap(__rhs); |
| 1200 | } |
| 1201 | }; |
| 1202 | |
| 1203 | template <class _CharT, class _Traits> |
| 1204 | basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs) |
| 1205 | : basic_istream<_CharT, _Traits>(std::move(__rhs)) {} |
| 1206 | |
| 1207 | template <class _CharT, class _Traits> |
| 1208 | basic_iostream<_CharT, _Traits>& basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs) { |
| 1209 | swap(__rhs); |
| 1210 | return *this; |
| 1211 | } |
| 1212 | |
| 1213 | template <class _CharT, class _Traits> |
| 1214 | basic_iostream<_CharT, _Traits>::~basic_iostream() {} |
| 1215 | |
| 1216 | template <class _CharT, class _Traits, class _Allocator> |
| 1217 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 1218 | operator>>(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str) { |
| 1219 | ios_base::iostate __state = ios_base::goodbit; |
| 1220 | typename basic_istream<_CharT, _Traits>::sentry __sen(__is); |
| 1221 | if (__sen) { |
| 1222 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1223 | try { |
| 1224 | # endif |
| 1225 | __str.clear(); |
| 1226 | using _Size = typename basic_string<_CharT, _Traits, _Allocator>::size_type; |
| 1227 | streamsize const __width = __is.width(); |
| 1228 | _Size const __max_size = __str.max_size(); |
| 1229 | _Size __n; |
| 1230 | if (__width <= 0) { |
| 1231 | __n = __max_size; |
| 1232 | } else { |
| 1233 | __n = std::__to_unsigned_like(x: __width) < __max_size ? static_cast<_Size>(__width) : __max_size; |
| 1234 | } |
| 1235 | |
| 1236 | _Size __c = 0; |
| 1237 | const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc()); |
| 1238 | while (__c < __n) { |
| 1239 | typename _Traits::int_type __i = __is.rdbuf()->sgetc(); |
| 1240 | if (_Traits::eq_int_type(__i, _Traits::eof())) { |
| 1241 | __state |= ios_base::eofbit; |
| 1242 | break; |
| 1243 | } |
| 1244 | _CharT __ch = _Traits::to_char_type(__i); |
| 1245 | if (__ct.is(__ct.space, __ch)) |
| 1246 | break; |
| 1247 | __str.push_back(__ch); |
| 1248 | ++__c; |
| 1249 | __is.rdbuf()->sbumpc(); |
| 1250 | } |
| 1251 | __is.width(0); |
| 1252 | if (__c == 0) |
| 1253 | __state |= ios_base::failbit; |
| 1254 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1255 | } catch (...) { |
| 1256 | __state |= ios_base::badbit; |
| 1257 | __is.__setstate_nothrow(__state); |
| 1258 | if (__is.exceptions() & ios_base::badbit) { |
| 1259 | throw; |
| 1260 | } |
| 1261 | } |
| 1262 | # endif |
| 1263 | __is.setstate(__state); |
| 1264 | } |
| 1265 | return __is; |
| 1266 | } |
| 1267 | |
| 1268 | template <class _CharT, class _Traits, class _Allocator> |
| 1269 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 1270 | getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm) { |
| 1271 | ios_base::iostate __state = ios_base::goodbit; |
| 1272 | typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true); |
| 1273 | if (!__sen) |
| 1274 | return __is; |
| 1275 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1276 | try { |
| 1277 | # endif |
| 1278 | __str.clear(); |
| 1279 | |
| 1280 | auto& __buffer = *__is.rdbuf(); |
| 1281 | |
| 1282 | auto __next = __buffer.sgetc(); |
| 1283 | for (; !_Traits::eq_int_type(__next, _Traits::eof()); __next = __buffer.sgetc()) { |
| 1284 | const auto* __first = __buffer.gptr(); |
| 1285 | const auto* __last = __buffer.egptr(); |
| 1286 | _CharT __1buf; |
| 1287 | |
| 1288 | if (__first == __last) { |
| 1289 | __1buf = __next; |
| 1290 | __first = std::addressof(__1buf); |
| 1291 | __last = std::addressof(__1buf) + 1; |
| 1292 | } |
| 1293 | |
| 1294 | auto __bump_stream = [&](ptrdiff_t __diff) { |
| 1295 | if (__first == std::addressof(__1buf)) { |
| 1296 | _LIBCPP_ASSERT_INTERNAL(__diff == 0 || __diff == 1, "trying to bump stream further than buffer size" ); |
| 1297 | if (__diff != 0) |
| 1298 | __buffer.sbumpc(); |
| 1299 | } else { |
| 1300 | __buffer.__gbump_ptrdiff(__diff); |
| 1301 | } |
| 1302 | }; |
| 1303 | |
| 1304 | const auto* const __match = _Traits::find(__first, __last - __first, __dlm); |
| 1305 | if (__match) |
| 1306 | __last = __match; |
| 1307 | |
| 1308 | if (auto __cap = __str.max_size() - __str.size(); __cap > static_cast<size_t>(__last - __first)) { |
| 1309 | __str.append(__first, __last); |
| 1310 | __bump_stream(__last - __first); |
| 1311 | |
| 1312 | if (__match) { |
| 1313 | __bump_stream(1); // Remove the matched character |
| 1314 | break; |
| 1315 | } |
| 1316 | } else { |
| 1317 | __str.append(__first, __cap); |
| 1318 | __bump_stream(__cap); |
| 1319 | __state |= ios_base::failbit; |
| 1320 | break; |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | if (_Traits::eq_int_type(__next, _Traits::eof())) |
| 1325 | __state |= ios_base::eofbit | (__str.empty() ? ios_base::failbit : ios_base::goodbit); |
| 1326 | |
| 1327 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1328 | } catch (...) { |
| 1329 | __state |= ios_base::badbit; |
| 1330 | __is.__setstate_nothrow(__state); |
| 1331 | if (__is.exceptions() & ios_base::badbit) { |
| 1332 | throw; |
| 1333 | } |
| 1334 | } |
| 1335 | # endif |
| 1336 | __is.setstate(__state); |
| 1337 | return __is; |
| 1338 | } |
| 1339 | |
| 1340 | template <class _CharT, class _Traits, class _Allocator> |
| 1341 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 1342 | getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str) { |
| 1343 | return std::getline(__is, __str, __is.widen('\n')); |
| 1344 | } |
| 1345 | |
| 1346 | template <class _CharT, class _Traits, class _Allocator> |
| 1347 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 1348 | getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm) { |
| 1349 | return std::getline(__is, __str, __dlm); |
| 1350 | } |
| 1351 | |
| 1352 | template <class _CharT, class _Traits, class _Allocator> |
| 1353 | inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 1354 | getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str) { |
| 1355 | return std::getline(__is, __str, __is.widen('\n')); |
| 1356 | } |
| 1357 | |
| 1358 | template <class _CharT, class _Traits, size_t _Size> |
| 1359 | _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& |
| 1360 | operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x) { |
| 1361 | ios_base::iostate __state = ios_base::goodbit; |
| 1362 | typename basic_istream<_CharT, _Traits>::sentry __sen(__is); |
| 1363 | if (__sen) { |
| 1364 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1365 | try { |
| 1366 | # endif |
| 1367 | basic_string<_CharT, _Traits> __str; |
| 1368 | const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc()); |
| 1369 | size_t __c = 0; |
| 1370 | _CharT __zero = __ct.widen('0'); |
| 1371 | _CharT __one = __ct.widen('1'); |
| 1372 | while (__c != _Size) { |
| 1373 | typename _Traits::int_type __i = __is.rdbuf()->sgetc(); |
| 1374 | if (_Traits::eq_int_type(__i, _Traits::eof())) { |
| 1375 | __state |= ios_base::eofbit; |
| 1376 | break; |
| 1377 | } |
| 1378 | _CharT __ch = _Traits::to_char_type(__i); |
| 1379 | if (!_Traits::eq(__ch, __zero) && !_Traits::eq(__ch, __one)) |
| 1380 | break; |
| 1381 | __str.push_back(__ch); |
| 1382 | ++__c; |
| 1383 | __is.rdbuf()->sbumpc(); |
| 1384 | } |
| 1385 | __x = bitset<_Size>(__str); |
| 1386 | if (_Size > 0 && __c == 0) |
| 1387 | __state |= ios_base::failbit; |
| 1388 | # if _LIBCPP_HAS_EXCEPTIONS |
| 1389 | } catch (...) { |
| 1390 | __state |= ios_base::badbit; |
| 1391 | __is.__setstate_nothrow(__state); |
| 1392 | if (__is.exceptions() & ios_base::badbit) { |
| 1393 | throw; |
| 1394 | } |
| 1395 | } |
| 1396 | # endif |
| 1397 | __is.setstate(__state); |
| 1398 | } |
| 1399 | return __is; |
| 1400 | } |
| 1401 | |
| 1402 | extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<char>; |
| 1403 | # if _LIBCPP_HAS_WIDE_CHARACTERS |
| 1404 | extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<wchar_t>; |
| 1405 | # endif |
| 1406 | extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_iostream<char>; |
| 1407 | |
| 1408 | _LIBCPP_END_NAMESPACE_STD |
| 1409 | |
| 1410 | _LIBCPP_POP_MACROS |
| 1411 | |
| 1412 | # endif // _LIBCPP_HAS_LOCALIZATION |
| 1413 | |
| 1414 | # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 |
| 1415 | # include <concepts> |
| 1416 | # include <iosfwd> |
| 1417 | # include <ostream> |
| 1418 | # include <type_traits> |
| 1419 | # endif |
| 1420 | |
| 1421 | # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 23 |
| 1422 | # include <locale> |
| 1423 | # endif |
| 1424 | |
| 1425 | #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS) |
| 1426 | |
| 1427 | #endif // _LIBCPP_ISTREAM |
| 1428 | |