1// -*- C++ -*- 2//===------------------------- fstream ------------------------------------===// 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_FSTREAM 11#define _LIBCPP_FSTREAM 12 13/* 14 fstream synopsis 15 16template <class charT, class traits = char_traits<charT> > 17class basic_filebuf 18 : public basic_streambuf<charT, traits> 19{ 20public: 21 typedef charT char_type; 22 typedef traits traits_type; 23 typedef typename traits_type::int_type int_type; 24 typedef typename traits_type::pos_type pos_type; 25 typedef typename traits_type::off_type off_type; 26 27 // 27.9.1.2 Constructors/destructor: 28 basic_filebuf(); 29 basic_filebuf(basic_filebuf&& rhs); 30 virtual ~basic_filebuf(); 31 32 // 27.9.1.3 Assign/swap: 33 basic_filebuf& operator=(basic_filebuf&& rhs); 34 void swap(basic_filebuf& rhs); 35 36 // 27.9.1.4 Members: 37 bool is_open() const; 38 basic_filebuf* open(const char* s, ios_base::openmode mode); 39 basic_filebuf* open(const string& s, ios_base::openmode mode); 40 basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17 41 basic_filebuf* close(); 42 43protected: 44 // 27.9.1.5 Overridden virtual functions: 45 virtual streamsize showmanyc(); 46 virtual int_type underflow(); 47 virtual int_type uflow(); 48 virtual int_type pbackfail(int_type c = traits_type::eof()); 49 virtual int_type overflow (int_type c = traits_type::eof()); 50 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n); 51 virtual pos_type seekoff(off_type off, ios_base::seekdir way, 52 ios_base::openmode which = ios_base::in | ios_base::out); 53 virtual pos_type seekpos(pos_type sp, 54 ios_base::openmode which = ios_base::in | ios_base::out); 55 virtual int sync(); 56 virtual void imbue(const locale& loc); 57}; 58 59template <class charT, class traits> 60 void 61 swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y); 62 63typedef basic_filebuf<char> filebuf; 64typedef basic_filebuf<wchar_t> wfilebuf; 65 66template <class charT, class traits = char_traits<charT> > 67class basic_ifstream 68 : public basic_istream<charT,traits> 69{ 70public: 71 typedef charT char_type; 72 typedef traits traits_type; 73 typedef typename traits_type::int_type int_type; 74 typedef typename traits_type::pos_type pos_type; 75 typedef typename traits_type::off_type off_type; 76 77 basic_ifstream(); 78 explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in); 79 explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in); 80 explicit basic_ifstream(const filesystem::path& p, 81 ios_base::openmode mode = ios_base::in); // C++17 82 basic_ifstream(basic_ifstream&& rhs); 83 84 basic_ifstream& operator=(basic_ifstream&& rhs); 85 void swap(basic_ifstream& rhs); 86 87 basic_filebuf<char_type, traits_type>* rdbuf() const; 88 bool is_open() const; 89 void open(const char* s, ios_base::openmode mode = ios_base::in); 90 void open(const string& s, ios_base::openmode mode = ios_base::in); 91 void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17 92 93 void close(); 94}; 95 96template <class charT, class traits> 97 void 98 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y); 99 100typedef basic_ifstream<char> ifstream; 101typedef basic_ifstream<wchar_t> wifstream; 102 103template <class charT, class traits = char_traits<charT> > 104class basic_ofstream 105 : public basic_ostream<charT,traits> 106{ 107public: 108 typedef charT char_type; 109 typedef traits traits_type; 110 typedef typename traits_type::int_type int_type; 111 typedef typename traits_type::pos_type pos_type; 112 typedef typename traits_type::off_type off_type; 113 114 basic_ofstream(); 115 explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out); 116 explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out); 117 explicit basic_ofstream(const filesystem::path& p, 118 ios_base::openmode mode = ios_base::out); // C++17 119 basic_ofstream(basic_ofstream&& rhs); 120 121 basic_ofstream& operator=(basic_ofstream&& rhs); 122 void swap(basic_ofstream& rhs); 123 124 basic_filebuf<char_type, traits_type>* rdbuf() const; 125 bool is_open() const; 126 void open(const char* s, ios_base::openmode mode = ios_base::out); 127 void open(const string& s, ios_base::openmode mode = ios_base::out); 128 void open(const filesystem::path& p, 129 ios_base::openmode mode = ios_base::out); // C++17 130 131 void close(); 132}; 133 134template <class charT, class traits> 135 void 136 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y); 137 138typedef basic_ofstream<char> ofstream; 139typedef basic_ofstream<wchar_t> wofstream; 140 141template <class charT, class traits=char_traits<charT> > 142class basic_fstream 143 : public basic_iostream<charT,traits> 144{ 145public: 146 typedef charT char_type; 147 typedef traits traits_type; 148 typedef typename traits_type::int_type int_type; 149 typedef typename traits_type::pos_type pos_type; 150 typedef typename traits_type::off_type off_type; 151 152 basic_fstream(); 153 explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out); 154 explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out); 155 explicit basic_fstream(const filesystem::path& p, 156 ios_base::openmode mode = ios_base::in|ios_base::out); C++17 157 basic_fstream(basic_fstream&& rhs); 158 159 basic_fstream& operator=(basic_fstream&& rhs); 160 void swap(basic_fstream& rhs); 161 162 basic_filebuf<char_type, traits_type>* rdbuf() const; 163 bool is_open() const; 164 void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out); 165 void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out); 166 void open(const filesystem::path& s, 167 ios_base::openmode mode = ios_base::in|ios_base::out); // C++17 168 169 void close(); 170}; 171 172template <class charT, class traits> 173 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y); 174 175typedef basic_fstream<char> fstream; 176typedef basic_fstream<wchar_t> wfstream; 177 178} // std 179 180*/ 181 182#include <__config> 183#include <__availability> 184#include <ostream> 185#include <istream> 186#include <__locale> 187#include <cstdio> 188#include <cstdlib> 189 190#if !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY) 191# include <filesystem> 192#endif 193 194#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 195#pragma GCC system_header 196#endif 197 198_LIBCPP_PUSH_MACROS 199#include <__undef_macros> 200 201#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION) 202# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS 203#endif 204 205_LIBCPP_BEGIN_NAMESPACE_STD 206 207template <class _CharT, class _Traits> 208class _LIBCPP_TEMPLATE_VIS basic_filebuf 209 : public basic_streambuf<_CharT, _Traits> 210{ 211public: 212 typedef _CharT char_type; 213 typedef _Traits traits_type; 214 typedef typename traits_type::int_type int_type; 215 typedef typename traits_type::pos_type pos_type; 216 typedef typename traits_type::off_type off_type; 217 typedef typename traits_type::state_type state_type; 218 219 // 27.9.1.2 Constructors/destructor: 220 basic_filebuf(); 221#ifndef _LIBCPP_CXX03_LANG 222 basic_filebuf(basic_filebuf&& __rhs); 223#endif 224 virtual ~basic_filebuf(); 225 226 // 27.9.1.3 Assign/swap: 227#ifndef _LIBCPP_CXX03_LANG 228 _LIBCPP_INLINE_VISIBILITY 229 basic_filebuf& operator=(basic_filebuf&& __rhs); 230#endif 231 void swap(basic_filebuf& __rhs); 232 233 // 27.9.1.4 Members: 234 _LIBCPP_INLINE_VISIBILITY 235 bool is_open() const; 236#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 237 basic_filebuf* open(const char* __s, ios_base::openmode __mode); 238#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 239 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode); 240#endif 241 _LIBCPP_INLINE_VISIBILITY 242 basic_filebuf* open(const string& __s, ios_base::openmode __mode); 243 244#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY) 245 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY 246 basic_filebuf* open(const _VSTD_FS::path& __p, ios_base::openmode __mode) { 247 return open(__p.c_str(), __mode); 248 } 249#endif 250 inline _LIBCPP_INLINE_VISIBILITY 251 basic_filebuf* __open(int __fd, ios_base::openmode __mode); 252#endif 253 basic_filebuf* close(); 254 255 _LIBCPP_INLINE_VISIBILITY 256 inline static const char* 257 __make_mdstring(ios_base::openmode __mode) _NOEXCEPT; 258 259 protected: 260 // 27.9.1.5 Overridden virtual functions: 261 virtual int_type underflow(); 262 virtual int_type pbackfail(int_type __c = traits_type::eof()); 263 virtual int_type overflow (int_type __c = traits_type::eof()); 264 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n); 265 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way, 266 ios_base::openmode __wch = ios_base::in | ios_base::out); 267 virtual pos_type seekpos(pos_type __sp, 268 ios_base::openmode __wch = ios_base::in | ios_base::out); 269 virtual int sync(); 270 virtual void imbue(const locale& __loc); 271 272private: 273 char* __extbuf_; 274 const char* __extbufnext_; 275 const char* __extbufend_; 276 char __extbuf_min_[8]; 277 size_t __ebs_; 278 char_type* __intbuf_; 279 size_t __ibs_; 280 FILE* __file_; 281 const codecvt<char_type, char, state_type>* __cv_; 282 state_type __st_; 283 state_type __st_last_; 284 ios_base::openmode __om_; 285 ios_base::openmode __cm_; 286 bool __owns_eb_; 287 bool __owns_ib_; 288 bool __always_noconv_; 289 290 bool __read_mode(); 291 void __write_mode(); 292}; 293 294template <class _CharT, class _Traits> 295basic_filebuf<_CharT, _Traits>::basic_filebuf() 296 : __extbuf_(nullptr), 297 __extbufnext_(nullptr), 298 __extbufend_(nullptr), 299 __ebs_(0), 300 __intbuf_(nullptr), 301 __ibs_(0), 302 __file_(nullptr), 303 __cv_(nullptr), 304 __st_(), 305 __st_last_(), 306 __om_(0), 307 __cm_(0), 308 __owns_eb_(false), 309 __owns_ib_(false), 310 __always_noconv_(false) 311{ 312 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc())) 313 { 314 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc()); 315 __always_noconv_ = __cv_->always_noconv(); 316 } 317 setbuf(nullptr, 4096); 318} 319 320#ifndef _LIBCPP_CXX03_LANG 321 322template <class _CharT, class _Traits> 323basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) 324 : basic_streambuf<_CharT, _Traits>(__rhs) 325{ 326 if (__rhs.__extbuf_ == __rhs.__extbuf_min_) 327 { 328 __extbuf_ = __extbuf_min_; 329 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_); 330 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_); 331 } 332 else 333 { 334 __extbuf_ = __rhs.__extbuf_; 335 __extbufnext_ = __rhs.__extbufnext_; 336 __extbufend_ = __rhs.__extbufend_; 337 } 338 __ebs_ = __rhs.__ebs_; 339 __intbuf_ = __rhs.__intbuf_; 340 __ibs_ = __rhs.__ibs_; 341 __file_ = __rhs.__file_; 342 __cv_ = __rhs.__cv_; 343 __st_ = __rhs.__st_; 344 __st_last_ = __rhs.__st_last_; 345 __om_ = __rhs.__om_; 346 __cm_ = __rhs.__cm_; 347 __owns_eb_ = __rhs.__owns_eb_; 348 __owns_ib_ = __rhs.__owns_ib_; 349 __always_noconv_ = __rhs.__always_noconv_; 350 if (__rhs.pbase()) 351 { 352 if (__rhs.pbase() == __rhs.__intbuf_) 353 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase())); 354 else 355 this->setp((char_type*)__extbuf_, 356 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase())); 357 this->__pbump(__rhs. pptr() - __rhs.pbase()); 358 } 359 else if (__rhs.eback()) 360 { 361 if (__rhs.eback() == __rhs.__intbuf_) 362 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), 363 __intbuf_ + (__rhs.egptr() - __rhs.eback())); 364 else 365 this->setg((char_type*)__extbuf_, 366 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()), 367 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback())); 368 } 369 __rhs.__extbuf_ = nullptr; 370 __rhs.__extbufnext_ = nullptr; 371 __rhs.__extbufend_ = nullptr; 372 __rhs.__ebs_ = 0; 373 __rhs.__intbuf_ = 0; 374 __rhs.__ibs_ = 0; 375 __rhs.__file_ = nullptr; 376 __rhs.__st_ = state_type(); 377 __rhs.__st_last_ = state_type(); 378 __rhs.__om_ = 0; 379 __rhs.__cm_ = 0; 380 __rhs.__owns_eb_ = false; 381 __rhs.__owns_ib_ = false; 382 __rhs.setg(0, 0, 0); 383 __rhs.setp(0, 0); 384} 385 386template <class _CharT, class _Traits> 387inline 388basic_filebuf<_CharT, _Traits>& 389basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) 390{ 391 close(); 392 swap(__rhs); 393 return *this; 394} 395 396#endif // _LIBCPP_CXX03_LANG 397 398template <class _CharT, class _Traits> 399basic_filebuf<_CharT, _Traits>::~basic_filebuf() 400{ 401#ifndef _LIBCPP_NO_EXCEPTIONS 402 try 403 { 404#endif // _LIBCPP_NO_EXCEPTIONS 405 close(); 406#ifndef _LIBCPP_NO_EXCEPTIONS 407 } 408 catch (...) 409 { 410 } 411#endif // _LIBCPP_NO_EXCEPTIONS 412 if (__owns_eb_) 413 delete [] __extbuf_; 414 if (__owns_ib_) 415 delete [] __intbuf_; 416} 417 418template <class _CharT, class _Traits> 419void 420basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) 421{ 422 basic_streambuf<char_type, traits_type>::swap(__rhs); 423 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) 424 { 425 _VSTD::swap(__extbuf_, __rhs.__extbuf_); 426 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_); 427 _VSTD::swap(__extbufend_, __rhs.__extbufend_); 428 } 429 else 430 { 431 ptrdiff_t __ln = __extbufnext_ - __extbuf_; 432 ptrdiff_t __le = __extbufend_ - __extbuf_; 433 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_; 434 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_; 435 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) 436 { 437 __extbuf_ = __rhs.__extbuf_; 438 __rhs.__extbuf_ = __rhs.__extbuf_min_; 439 } 440 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) 441 { 442 __rhs.__extbuf_ = __extbuf_; 443 __extbuf_ = __extbuf_min_; 444 } 445 __extbufnext_ = __extbuf_ + __rn; 446 __extbufend_ = __extbuf_ + __re; 447 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln; 448 __rhs.__extbufend_ = __rhs.__extbuf_ + __le; 449 } 450 _VSTD::swap(__ebs_, __rhs.__ebs_); 451 _VSTD::swap(__intbuf_, __rhs.__intbuf_); 452 _VSTD::swap(__ibs_, __rhs.__ibs_); 453 _VSTD::swap(__file_, __rhs.__file_); 454 _VSTD::swap(__cv_, __rhs.__cv_); 455 _VSTD::swap(__st_, __rhs.__st_); 456 _VSTD::swap(__st_last_, __rhs.__st_last_); 457 _VSTD::swap(__om_, __rhs.__om_); 458 _VSTD::swap(__cm_, __rhs.__cm_); 459 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_); 460 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_); 461 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_); 462 if (this->eback() == (char_type*)__rhs.__extbuf_min_) 463 { 464 ptrdiff_t __n = this->gptr() - this->eback(); 465 ptrdiff_t __e = this->egptr() - this->eback(); 466 this->setg((char_type*)__extbuf_min_, 467 (char_type*)__extbuf_min_ + __n, 468 (char_type*)__extbuf_min_ + __e); 469 } 470 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) 471 { 472 ptrdiff_t __n = this->pptr() - this->pbase(); 473 ptrdiff_t __e = this->epptr() - this->pbase(); 474 this->setp((char_type*)__extbuf_min_, 475 (char_type*)__extbuf_min_ + __e); 476 this->__pbump(__n); 477 } 478 if (__rhs.eback() == (char_type*)__extbuf_min_) 479 { 480 ptrdiff_t __n = __rhs.gptr() - __rhs.eback(); 481 ptrdiff_t __e = __rhs.egptr() - __rhs.eback(); 482 __rhs.setg((char_type*)__rhs.__extbuf_min_, 483 (char_type*)__rhs.__extbuf_min_ + __n, 484 (char_type*)__rhs.__extbuf_min_ + __e); 485 } 486 else if (__rhs.pbase() == (char_type*)__extbuf_min_) 487 { 488 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase(); 489 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase(); 490 __rhs.setp((char_type*)__rhs.__extbuf_min_, 491 (char_type*)__rhs.__extbuf_min_ + __e); 492 __rhs.__pbump(__n); 493 } 494} 495 496template <class _CharT, class _Traits> 497inline _LIBCPP_INLINE_VISIBILITY 498void 499swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) 500{ 501 __x.swap(__y); 502} 503 504template <class _CharT, class _Traits> 505inline 506bool 507basic_filebuf<_CharT, _Traits>::is_open() const 508{ 509 return __file_ != nullptr; 510} 511 512template <class _CharT, class _Traits> 513const char* basic_filebuf<_CharT, _Traits>::__make_mdstring( 514 ios_base::openmode __mode) _NOEXCEPT { 515 switch (__mode & ~ios_base::ate) { 516 case ios_base::out: 517 case ios_base::out | ios_base::trunc: 518 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE; 519 case ios_base::out | ios_base::app: 520 case ios_base::app: 521 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE; 522 case ios_base::in: 523 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE; 524 case ios_base::in | ios_base::out: 525 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE; 526 case ios_base::in | ios_base::out | ios_base::trunc: 527 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE; 528 case ios_base::in | ios_base::out | ios_base::app: 529 case ios_base::in | ios_base::app: 530 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE; 531 case ios_base::out | ios_base::binary: 532 case ios_base::out | ios_base::trunc | ios_base::binary: 533 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE; 534 case ios_base::out | ios_base::app | ios_base::binary: 535 case ios_base::app | ios_base::binary: 536 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE; 537 case ios_base::in | ios_base::binary: 538 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE; 539 case ios_base::in | ios_base::out | ios_base::binary: 540 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE; 541 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary: 542 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE; 543 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary: 544 case ios_base::in | ios_base::app | ios_base::binary: 545 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE; 546 default: 547 return nullptr; 548 } 549 _LIBCPP_UNREACHABLE(); 550} 551 552#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 553template <class _CharT, class _Traits> 554basic_filebuf<_CharT, _Traits>* 555basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) 556{ 557 basic_filebuf<_CharT, _Traits>* __rt = nullptr; 558 if (__file_ == nullptr) 559 { 560 if (const char* __mdstr = __make_mdstring(__mode)) { 561 __rt = this; 562 __file_ = fopen(__s, __mdstr); 563 if (__file_) { 564 __om_ = __mode; 565 if (__mode & ios_base::ate) { 566 if (fseek(__file_, 0, SEEK_END)) { 567 fclose(__file_); 568 __file_ = nullptr; 569 __rt = nullptr; 570 } 571 } 572 } else 573 __rt = nullptr; 574 } 575 } 576 return __rt; 577} 578 579template <class _CharT, class _Traits> 580inline _LIBCPP_INLINE_VISIBILITY 581basic_filebuf<_CharT, _Traits>* 582basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) { 583 basic_filebuf<_CharT, _Traits>* __rt = nullptr; 584 if (__file_ == nullptr) { 585 if (const char* __mdstr = __make_mdstring(__mode)) { 586 __rt = this; 587 __file_ = fdopen(__fd, __mdstr); 588 if (__file_) { 589 __om_ = __mode; 590 if (__mode & ios_base::ate) { 591 if (fseek(__file_, 0, SEEK_END)) { 592 fclose(__file_); 593 __file_ = nullptr; 594 __rt = nullptr; 595 } 596 } 597 } else 598 __rt = nullptr; 599 } 600 } 601 return __rt; 602} 603 604#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 605// This is basically the same as the char* overload except that it uses _wfopen 606// and long mode strings. 607template <class _CharT, class _Traits> 608basic_filebuf<_CharT, _Traits>* 609basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) 610{ 611 basic_filebuf<_CharT, _Traits>* __rt = nullptr; 612 if (__file_ == nullptr) 613 { 614 __rt = this; 615 const wchar_t* __mdstr; 616 switch (__mode & ~ios_base::ate) 617 { 618 case ios_base::out: 619 case ios_base::out | ios_base::trunc: 620 __mdstr = L"w"; 621 break; 622 case ios_base::out | ios_base::app: 623 case ios_base::app: 624 __mdstr = L"a"; 625 break; 626 case ios_base::in: 627 __mdstr = L"r"; 628 break; 629 case ios_base::in | ios_base::out: 630 __mdstr = L"r+"; 631 break; 632 case ios_base::in | ios_base::out | ios_base::trunc: 633 __mdstr = L"w+"; 634 break; 635 case ios_base::in | ios_base::out | ios_base::app: 636 case ios_base::in | ios_base::app: 637 __mdstr = L"a+"; 638 break; 639 case ios_base::out | ios_base::binary: 640 case ios_base::out | ios_base::trunc | ios_base::binary: 641 __mdstr = L"wb"; 642 break; 643 case ios_base::out | ios_base::app | ios_base::binary: 644 case ios_base::app | ios_base::binary: 645 __mdstr = L"ab"; 646 break; 647 case ios_base::in | ios_base::binary: 648 __mdstr = L"rb"; 649 break; 650 case ios_base::in | ios_base::out | ios_base::binary: 651 __mdstr = L"r+b"; 652 break; 653 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary: 654 __mdstr = L"w+b"; 655 break; 656 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary: 657 case ios_base::in | ios_base::app | ios_base::binary: 658 __mdstr = L"a+b"; 659 break; 660 default: 661 __rt = nullptr; 662 break; 663 } 664 if (__rt) 665 { 666 __file_ = _wfopen(__s, __mdstr); 667 if (__file_) 668 { 669 __om_ = __mode; 670 if (__mode & ios_base::ate) 671 { 672 if (fseek(__file_, 0, SEEK_END)) 673 { 674 fclose(__file_); 675 __file_ = nullptr; 676 __rt = nullptr; 677 } 678 } 679 } 680 else 681 __rt = nullptr; 682 } 683 } 684 return __rt; 685} 686#endif 687 688template <class _CharT, class _Traits> 689inline 690basic_filebuf<_CharT, _Traits>* 691basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) 692{ 693 return open(__s.c_str(), __mode); 694} 695#endif 696 697template <class _CharT, class _Traits> 698basic_filebuf<_CharT, _Traits>* 699basic_filebuf<_CharT, _Traits>::close() 700{ 701 basic_filebuf<_CharT, _Traits>* __rt = nullptr; 702 if (__file_) 703 { 704 __rt = this; 705 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose); 706 if (sync()) 707 __rt = nullptr; 708 if (fclose(__h.release())) 709 __rt = nullptr; 710 __file_ = nullptr; 711 setbuf(0, 0); 712 } 713 return __rt; 714} 715 716template <class _CharT, class _Traits> 717typename basic_filebuf<_CharT, _Traits>::int_type 718basic_filebuf<_CharT, _Traits>::underflow() 719{ 720 if (__file_ == nullptr) 721 return traits_type::eof(); 722 bool __initial = __read_mode(); 723 char_type __1buf; 724 if (this->gptr() == nullptr) 725 this->setg(&__1buf, &__1buf+1, &__1buf+1); 726 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4); 727 int_type __c = traits_type::eof(); 728 if (this->gptr() == this->egptr()) 729 { 730 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type)); 731 if (__always_noconv_) 732 { 733 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz); 734 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_); 735 if (__nmemb != 0) 736 { 737 this->setg(this->eback(), 738 this->eback() + __unget_sz, 739 this->eback() + __unget_sz + __nmemb); 740 __c = traits_type::to_int_type(*this->gptr()); 741 } 742 } 743 else 744 { 745 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" ); 746 if (__extbufend_ != __extbufnext_) 747 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_); 748 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_); 749 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_); 750 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz), 751 static_cast<size_t>(__extbufend_ - __extbufnext_)); 752 codecvt_base::result __r; 753 __st_last_ = __st_; 754 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_); 755 if (__nr != 0) 756 { 757 if (!__cv_) 758 __throw_bad_cast(); 759 760 __extbufend_ = __extbufnext_ + __nr; 761 char_type* __inext; 762 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_, 763 this->eback() + __unget_sz, 764 this->eback() + __ibs_, __inext); 765 if (__r == codecvt_base::noconv) 766 { 767 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, 768 (char_type*)const_cast<char *>(__extbufend_)); 769 __c = traits_type::to_int_type(*this->gptr()); 770 } 771 else if (__inext != this->eback() + __unget_sz) 772 { 773 this->setg(this->eback(), this->eback() + __unget_sz, __inext); 774 __c = traits_type::to_int_type(*this->gptr()); 775 } 776 } 777 } 778 } 779 else 780 __c = traits_type::to_int_type(*this->gptr()); 781 if (this->eback() == &__1buf) 782 this->setg(nullptr, nullptr, nullptr); 783 return __c; 784} 785 786template <class _CharT, class _Traits> 787typename basic_filebuf<_CharT, _Traits>::int_type 788basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) 789{ 790 if (__file_ && this->eback() < this->gptr()) 791 { 792 if (traits_type::eq_int_type(__c, traits_type::eof())) 793 { 794 this->gbump(-1); 795 return traits_type::not_eof(__c); 796 } 797 if ((__om_ & ios_base::out) || 798 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) 799 { 800 this->gbump(-1); 801 *this->gptr() = traits_type::to_char_type(__c); 802 return __c; 803 } 804 } 805 return traits_type::eof(); 806} 807 808template <class _CharT, class _Traits> 809typename basic_filebuf<_CharT, _Traits>::int_type 810basic_filebuf<_CharT, _Traits>::overflow(int_type __c) 811{ 812 if (__file_ == nullptr) 813 return traits_type::eof(); 814 __write_mode(); 815 char_type __1buf; 816 char_type* __pb_save = this->pbase(); 817 char_type* __epb_save = this->epptr(); 818 if (!traits_type::eq_int_type(__c, traits_type::eof())) 819 { 820 if (this->pptr() == nullptr) 821 this->setp(&__1buf, &__1buf+1); 822 *this->pptr() = traits_type::to_char_type(__c); 823 this->pbump(1); 824 } 825 if (this->pptr() != this->pbase()) 826 { 827 if (__always_noconv_) 828 { 829 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase()); 830 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb) 831 return traits_type::eof(); 832 } 833 else 834 { 835 char* __extbe = __extbuf_; 836 codecvt_base::result __r; 837 do 838 { 839 if (!__cv_) 840 __throw_bad_cast(); 841 842 const char_type* __e; 843 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, 844 __extbuf_, __extbuf_ + __ebs_, __extbe); 845 if (__e == this->pbase()) 846 return traits_type::eof(); 847 if (__r == codecvt_base::noconv) 848 { 849 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase()); 850 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb) 851 return traits_type::eof(); 852 } 853 else if (__r == codecvt_base::ok || __r == codecvt_base::partial) 854 { 855 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_); 856 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb) 857 return traits_type::eof(); 858 if (__r == codecvt_base::partial) 859 { 860 this->setp(const_cast<char_type*>(__e), this->pptr()); 861 this->__pbump(this->epptr() - this->pbase()); 862 } 863 } 864 else 865 return traits_type::eof(); 866 } while (__r == codecvt_base::partial); 867 } 868 this->setp(__pb_save, __epb_save); 869 } 870 return traits_type::not_eof(__c); 871} 872 873template <class _CharT, class _Traits> 874basic_streambuf<_CharT, _Traits>* 875basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) 876{ 877 this->setg(nullptr, nullptr, nullptr); 878 this->setp(nullptr, nullptr); 879 if (__owns_eb_) 880 delete [] __extbuf_; 881 if (__owns_ib_) 882 delete [] __intbuf_; 883 __ebs_ = __n; 884 if (__ebs_ > sizeof(__extbuf_min_)) 885 { 886 if (__always_noconv_ && __s) 887 { 888 __extbuf_ = (char*)__s; 889 __owns_eb_ = false; 890 } 891 else 892 { 893 __extbuf_ = new char[__ebs_]; 894 __owns_eb_ = true; 895 } 896 } 897 else 898 { 899 __extbuf_ = __extbuf_min_; 900 __ebs_ = sizeof(__extbuf_min_); 901 __owns_eb_ = false; 902 } 903 if (!__always_noconv_) 904 { 905 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_)); 906 if (__s && __ibs_ >= sizeof(__extbuf_min_)) 907 { 908 __intbuf_ = __s; 909 __owns_ib_ = false; 910 } 911 else 912 { 913 __intbuf_ = new char_type[__ibs_]; 914 __owns_ib_ = true; 915 } 916 } 917 else 918 { 919 __ibs_ = 0; 920 __intbuf_ = nullptr; 921 __owns_ib_ = false; 922 } 923 return this; 924} 925 926template <class _CharT, class _Traits> 927typename basic_filebuf<_CharT, _Traits>::pos_type 928basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, 929 ios_base::openmode) 930{ 931 if (!__cv_) 932 __throw_bad_cast(); 933 934 int __width = __cv_->encoding(); 935 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync()) 936 return pos_type(off_type(-1)); 937 // __width > 0 || __off == 0 938 int __whence; 939 switch (__way) 940 { 941 case ios_base::beg: 942 __whence = SEEK_SET; 943 break; 944 case ios_base::cur: 945 __whence = SEEK_CUR; 946 break; 947 case ios_base::end: 948 __whence = SEEK_END; 949 break; 950 default: 951 return pos_type(off_type(-1)); 952 } 953#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS) 954 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence)) 955 return pos_type(off_type(-1)); 956 pos_type __r = ftell(__file_); 957#else 958 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence)) 959 return pos_type(off_type(-1)); 960 pos_type __r = ftello(__file_); 961#endif 962 __r.state(__st_); 963 return __r; 964} 965 966template <class _CharT, class _Traits> 967typename basic_filebuf<_CharT, _Traits>::pos_type 968basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) 969{ 970 if (__file_ == nullptr || sync()) 971 return pos_type(off_type(-1)); 972#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS) 973 if (fseek(__file_, __sp, SEEK_SET)) 974 return pos_type(off_type(-1)); 975#else 976 if (fseeko(__file_, __sp, SEEK_SET)) 977 return pos_type(off_type(-1)); 978#endif 979 __st_ = __sp.state(); 980 return __sp; 981} 982 983template <class _CharT, class _Traits> 984int 985basic_filebuf<_CharT, _Traits>::sync() 986{ 987 if (__file_ == nullptr) 988 return 0; 989 if (!__cv_) 990 __throw_bad_cast(); 991 992 if (__cm_ & ios_base::out) 993 { 994 if (this->pptr() != this->pbase()) 995 if (overflow() == traits_type::eof()) 996 return -1; 997 codecvt_base::result __r; 998 do 999 { 1000 char* __extbe; 1001 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe); 1002 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_); 1003 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb) 1004 return -1; 1005 } while (__r == codecvt_base::partial); 1006 if (__r == codecvt_base::error) 1007 return -1; 1008 if (fflush(__file_)) 1009 return -1; 1010 } 1011 else if (__cm_ & ios_base::in) 1012 { 1013 off_type __c; 1014 state_type __state = __st_last_; 1015 bool __update_st = false; 1016 if (__always_noconv_) 1017 __c = this->egptr() - this->gptr(); 1018 else 1019 { 1020 int __width = __cv_->encoding(); 1021 __c = __extbufend_ - __extbufnext_; 1022 if (__width > 0) 1023 __c += __width * (this->egptr() - this->gptr()); 1024 else 1025 { 1026 if (this->gptr() != this->egptr()) 1027 { 1028 const int __off = __cv_->length(__state, __extbuf_, 1029 __extbufnext_, 1030 this->gptr() - this->eback()); 1031 __c += __extbufnext_ - __extbuf_ - __off; 1032 __update_st = true; 1033 } 1034 } 1035 } 1036#if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS) 1037 if (fseek(__file_, -__c, SEEK_CUR)) 1038 return -1; 1039#else 1040 if (fseeko(__file_, -__c, SEEK_CUR)) 1041 return -1; 1042#endif 1043 if (__update_st) 1044 __st_ = __state; 1045 __extbufnext_ = __extbufend_ = __extbuf_; 1046 this->setg(nullptr, nullptr, nullptr); 1047 __cm_ = 0; 1048 } 1049 return 0; 1050} 1051 1052template <class _CharT, class _Traits> 1053void 1054basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) 1055{ 1056 sync(); 1057 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc); 1058 bool __old_anc = __always_noconv_; 1059 __always_noconv_ = __cv_->always_noconv(); 1060 if (__old_anc != __always_noconv_) 1061 { 1062 this->setg(nullptr, nullptr, nullptr); 1063 this->setp(nullptr, nullptr); 1064 // invariant, char_type is char, else we couldn't get here 1065 if (__always_noconv_) // need to dump __intbuf_ 1066 { 1067 if (__owns_eb_) 1068 delete [] __extbuf_; 1069 __owns_eb_ = __owns_ib_; 1070 __ebs_ = __ibs_; 1071 __extbuf_ = (char*)__intbuf_; 1072 __ibs_ = 0; 1073 __intbuf_ = nullptr; 1074 __owns_ib_ = false; 1075 } 1076 else // need to obtain an __intbuf_. 1077 { // If __extbuf_ is user-supplied, use it, else new __intbuf_ 1078 if (!__owns_eb_ && __extbuf_ != __extbuf_min_) 1079 { 1080 __ibs_ = __ebs_; 1081 __intbuf_ = (char_type*)__extbuf_; 1082 __owns_ib_ = false; 1083 __extbuf_ = new char[__ebs_]; 1084 __owns_eb_ = true; 1085 } 1086 else 1087 { 1088 __ibs_ = __ebs_; 1089 __intbuf_ = new char_type[__ibs_]; 1090 __owns_ib_ = true; 1091 } 1092 } 1093 } 1094} 1095 1096template <class _CharT, class _Traits> 1097bool 1098basic_filebuf<_CharT, _Traits>::__read_mode() 1099{ 1100 if (!(__cm_ & ios_base::in)) 1101 { 1102 this->setp(nullptr, nullptr); 1103 if (__always_noconv_) 1104 this->setg((char_type*)__extbuf_, 1105 (char_type*)__extbuf_ + __ebs_, 1106 (char_type*)__extbuf_ + __ebs_); 1107 else 1108 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_); 1109 __cm_ = ios_base::in; 1110 return true; 1111 } 1112 return false; 1113} 1114 1115template <class _CharT, class _Traits> 1116void 1117basic_filebuf<_CharT, _Traits>::__write_mode() 1118{ 1119 if (!(__cm_ & ios_base::out)) 1120 { 1121 this->setg(nullptr, nullptr, nullptr); 1122 if (__ebs_ > sizeof(__extbuf_min_)) 1123 { 1124 if (__always_noconv_) 1125 this->setp((char_type*)__extbuf_, 1126 (char_type*)__extbuf_ + (__ebs_ - 1)); 1127 else 1128 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1)); 1129 } 1130 else 1131 this->setp(nullptr, nullptr); 1132 __cm_ = ios_base::out; 1133 } 1134} 1135 1136// basic_ifstream 1137 1138template <class _CharT, class _Traits> 1139class _LIBCPP_TEMPLATE_VIS basic_ifstream 1140 : public basic_istream<_CharT, _Traits> 1141{ 1142public: 1143 typedef _CharT char_type; 1144 typedef _Traits traits_type; 1145 typedef typename traits_type::int_type int_type; 1146 typedef typename traits_type::pos_type pos_type; 1147 typedef typename traits_type::off_type off_type; 1148 1149 _LIBCPP_INLINE_VISIBILITY 1150 basic_ifstream(); 1151#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 1152 _LIBCPP_INLINE_VISIBILITY 1153 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in); 1154#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1155 _LIBCPP_INLINE_VISIBILITY 1156 explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in); 1157#endif 1158 _LIBCPP_INLINE_VISIBILITY 1159 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in); 1160#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY) 1161 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY 1162 explicit basic_ifstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) 1163 : basic_ifstream(__p.c_str(), __mode) {} 1164#endif // _LIBCPP_STD_VER >= 17 1165#endif 1166#ifndef _LIBCPP_CXX03_LANG 1167 _LIBCPP_INLINE_VISIBILITY 1168 basic_ifstream(basic_ifstream&& __rhs); 1169 1170 _LIBCPP_INLINE_VISIBILITY 1171 basic_ifstream& operator=(basic_ifstream&& __rhs); 1172#endif 1173 _LIBCPP_INLINE_VISIBILITY 1174 void swap(basic_ifstream& __rhs); 1175 1176 _LIBCPP_INLINE_VISIBILITY 1177 basic_filebuf<char_type, traits_type>* rdbuf() const; 1178 _LIBCPP_INLINE_VISIBILITY 1179 bool is_open() const; 1180#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 1181 void open(const char* __s, ios_base::openmode __mode = ios_base::in); 1182#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1183 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in); 1184#endif 1185 void open(const string& __s, ios_base::openmode __mode = ios_base::in); 1186#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY) 1187 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY 1188 void open(const filesystem::path& __p, 1189 ios_base::openmode __mode = ios_base::in) { 1190 return open(__p.c_str(), __mode); 1191 } 1192#endif // _LIBCPP_STD_VER >= 17 1193 1194 _LIBCPP_INLINE_VISIBILITY 1195 void __open(int __fd, ios_base::openmode __mode); 1196#endif 1197 _LIBCPP_INLINE_VISIBILITY 1198 void close(); 1199 1200private: 1201 basic_filebuf<char_type, traits_type> __sb_; 1202}; 1203 1204template <class _CharT, class _Traits> 1205inline 1206basic_ifstream<_CharT, _Traits>::basic_ifstream() 1207 : basic_istream<char_type, traits_type>(&__sb_) 1208{ 1209} 1210 1211#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 1212template <class _CharT, class _Traits> 1213inline 1214basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode) 1215 : basic_istream<char_type, traits_type>(&__sb_) 1216{ 1217 if (__sb_.open(__s, __mode | ios_base::in) == nullptr) 1218 this->setstate(ios_base::failbit); 1219} 1220 1221#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1222template <class _CharT, class _Traits> 1223inline 1224basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode) 1225 : basic_istream<char_type, traits_type>(&__sb_) 1226{ 1227 if (__sb_.open(__s, __mode | ios_base::in) == nullptr) 1228 this->setstate(ios_base::failbit); 1229} 1230#endif 1231 1232template <class _CharT, class _Traits> 1233inline 1234basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode) 1235 : basic_istream<char_type, traits_type>(&__sb_) 1236{ 1237 if (__sb_.open(__s, __mode | ios_base::in) == nullptr) 1238 this->setstate(ios_base::failbit); 1239} 1240#endif 1241 1242#ifndef _LIBCPP_CXX03_LANG 1243 1244template <class _CharT, class _Traits> 1245inline 1246basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs) 1247 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)), 1248 __sb_(_VSTD::move(__rhs.__sb_)) 1249{ 1250 this->set_rdbuf(&__sb_); 1251} 1252 1253template <class _CharT, class _Traits> 1254inline 1255basic_ifstream<_CharT, _Traits>& 1256basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) 1257{ 1258 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs)); 1259 __sb_ = _VSTD::move(__rhs.__sb_); 1260 return *this; 1261} 1262 1263#endif // _LIBCPP_CXX03_LANG 1264 1265template <class _CharT, class _Traits> 1266inline 1267void 1268basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) 1269{ 1270 basic_istream<char_type, traits_type>::swap(__rhs); 1271 __sb_.swap(__rhs.__sb_); 1272} 1273 1274template <class _CharT, class _Traits> 1275inline _LIBCPP_INLINE_VISIBILITY 1276void 1277swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) 1278{ 1279 __x.swap(__y); 1280} 1281 1282template <class _CharT, class _Traits> 1283inline 1284basic_filebuf<_CharT, _Traits>* 1285basic_ifstream<_CharT, _Traits>::rdbuf() const 1286{ 1287 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_); 1288} 1289 1290template <class _CharT, class _Traits> 1291inline 1292bool 1293basic_ifstream<_CharT, _Traits>::is_open() const 1294{ 1295 return __sb_.is_open(); 1296} 1297 1298#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 1299template <class _CharT, class _Traits> 1300void 1301basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) 1302{ 1303 if (__sb_.open(__s, __mode | ios_base::in)) 1304 this->clear(); 1305 else 1306 this->setstate(ios_base::failbit); 1307} 1308 1309#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1310template <class _CharT, class _Traits> 1311void 1312basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) 1313{ 1314 if (__sb_.open(__s, __mode | ios_base::in)) 1315 this->clear(); 1316 else 1317 this->setstate(ios_base::failbit); 1318} 1319#endif 1320 1321template <class _CharT, class _Traits> 1322void 1323basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) 1324{ 1325 if (__sb_.open(__s, __mode | ios_base::in)) 1326 this->clear(); 1327 else 1328 this->setstate(ios_base::failbit); 1329} 1330 1331template <class _CharT, class _Traits> 1332void basic_ifstream<_CharT, _Traits>::__open(int __fd, 1333 ios_base::openmode __mode) { 1334 if (__sb_.__open(__fd, __mode | ios_base::in)) 1335 this->clear(); 1336 else 1337 this->setstate(ios_base::failbit); 1338} 1339#endif 1340 1341template <class _CharT, class _Traits> 1342inline 1343void 1344basic_ifstream<_CharT, _Traits>::close() 1345{ 1346 if (__sb_.close() == 0) 1347 this->setstate(ios_base::failbit); 1348} 1349 1350// basic_ofstream 1351 1352template <class _CharT, class _Traits> 1353class _LIBCPP_TEMPLATE_VIS basic_ofstream 1354 : public basic_ostream<_CharT, _Traits> 1355{ 1356public: 1357 typedef _CharT char_type; 1358 typedef _Traits traits_type; 1359 typedef typename traits_type::int_type int_type; 1360 typedef typename traits_type::pos_type pos_type; 1361 typedef typename traits_type::off_type off_type; 1362 1363 _LIBCPP_INLINE_VISIBILITY 1364 basic_ofstream(); 1365 _LIBCPP_INLINE_VISIBILITY 1366 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out); 1367#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1368 _LIBCPP_INLINE_VISIBILITY 1369 explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out); 1370#endif 1371 _LIBCPP_INLINE_VISIBILITY 1372 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out); 1373 1374#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY) 1375 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY 1376 explicit basic_ofstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) 1377 : basic_ofstream(__p.c_str(), __mode) {} 1378#endif // _LIBCPP_STD_VER >= 17 1379 1380#ifndef _LIBCPP_CXX03_LANG 1381 _LIBCPP_INLINE_VISIBILITY 1382 basic_ofstream(basic_ofstream&& __rhs); 1383 1384 _LIBCPP_INLINE_VISIBILITY 1385 basic_ofstream& operator=(basic_ofstream&& __rhs); 1386#endif 1387 _LIBCPP_INLINE_VISIBILITY 1388 void swap(basic_ofstream& __rhs); 1389 1390 _LIBCPP_INLINE_VISIBILITY 1391 basic_filebuf<char_type, traits_type>* rdbuf() const; 1392 _LIBCPP_INLINE_VISIBILITY 1393 bool is_open() const; 1394#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 1395 void open(const char* __s, ios_base::openmode __mode = ios_base::out); 1396#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1397 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out); 1398#endif 1399 void open(const string& __s, ios_base::openmode __mode = ios_base::out); 1400 1401#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY) 1402 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY 1403 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) 1404 { return open(__p.c_str(), __mode); } 1405#endif // _LIBCPP_STD_VER >= 17 1406 1407 _LIBCPP_INLINE_VISIBILITY 1408 void __open(int __fd, ios_base::openmode __mode); 1409#endif 1410 _LIBCPP_INLINE_VISIBILITY 1411 void close(); 1412 1413private: 1414 basic_filebuf<char_type, traits_type> __sb_; 1415}; 1416 1417template <class _CharT, class _Traits> 1418inline 1419basic_ofstream<_CharT, _Traits>::basic_ofstream() 1420 : basic_ostream<char_type, traits_type>(&__sb_) 1421{ 1422} 1423 1424#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 1425template <class _CharT, class _Traits> 1426inline 1427basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode) 1428 : basic_ostream<char_type, traits_type>(&__sb_) 1429{ 1430 if (__sb_.open(__s, __mode | ios_base::out) == nullptr) 1431 this->setstate(ios_base::failbit); 1432} 1433 1434#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1435template <class _CharT, class _Traits> 1436inline 1437basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode) 1438 : basic_ostream<char_type, traits_type>(&__sb_) 1439{ 1440 if (__sb_.open(__s, __mode | ios_base::out) == nullptr) 1441 this->setstate(ios_base::failbit); 1442} 1443#endif 1444 1445template <class _CharT, class _Traits> 1446inline 1447basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode) 1448 : basic_ostream<char_type, traits_type>(&__sb_) 1449{ 1450 if (__sb_.open(__s, __mode | ios_base::out) == nullptr) 1451 this->setstate(ios_base::failbit); 1452} 1453#endif 1454 1455#ifndef _LIBCPP_CXX03_LANG 1456 1457template <class _CharT, class _Traits> 1458inline 1459basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs) 1460 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)), 1461 __sb_(_VSTD::move(__rhs.__sb_)) 1462{ 1463 this->set_rdbuf(&__sb_); 1464} 1465 1466template <class _CharT, class _Traits> 1467inline 1468basic_ofstream<_CharT, _Traits>& 1469basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) 1470{ 1471 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs)); 1472 __sb_ = _VSTD::move(__rhs.__sb_); 1473 return *this; 1474} 1475 1476#endif // _LIBCPP_CXX03_LANG 1477 1478template <class _CharT, class _Traits> 1479inline 1480void 1481basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) 1482{ 1483 basic_ostream<char_type, traits_type>::swap(__rhs); 1484 __sb_.swap(__rhs.__sb_); 1485} 1486 1487template <class _CharT, class _Traits> 1488inline _LIBCPP_INLINE_VISIBILITY 1489void 1490swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) 1491{ 1492 __x.swap(__y); 1493} 1494 1495template <class _CharT, class _Traits> 1496inline 1497basic_filebuf<_CharT, _Traits>* 1498basic_ofstream<_CharT, _Traits>::rdbuf() const 1499{ 1500 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_); 1501} 1502 1503template <class _CharT, class _Traits> 1504inline 1505bool 1506basic_ofstream<_CharT, _Traits>::is_open() const 1507{ 1508 return __sb_.is_open(); 1509} 1510 1511#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 1512template <class _CharT, class _Traits> 1513void 1514basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) 1515{ 1516 if (__sb_.open(__s, __mode | ios_base::out)) 1517 this->clear(); 1518 else 1519 this->setstate(ios_base::failbit); 1520} 1521 1522#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1523template <class _CharT, class _Traits> 1524void 1525basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) 1526{ 1527 if (__sb_.open(__s, __mode | ios_base::out)) 1528 this->clear(); 1529 else 1530 this->setstate(ios_base::failbit); 1531} 1532#endif 1533 1534template <class _CharT, class _Traits> 1535void 1536basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) 1537{ 1538 if (__sb_.open(__s, __mode | ios_base::out)) 1539 this->clear(); 1540 else 1541 this->setstate(ios_base::failbit); 1542} 1543 1544template <class _CharT, class _Traits> 1545void basic_ofstream<_CharT, _Traits>::__open(int __fd, 1546 ios_base::openmode __mode) { 1547 if (__sb_.__open(__fd, __mode | ios_base::out)) 1548 this->clear(); 1549 else 1550 this->setstate(ios_base::failbit); 1551} 1552#endif 1553 1554template <class _CharT, class _Traits> 1555inline 1556void 1557basic_ofstream<_CharT, _Traits>::close() 1558{ 1559 if (__sb_.close() == nullptr) 1560 this->setstate(ios_base::failbit); 1561} 1562 1563// basic_fstream 1564 1565template <class _CharT, class _Traits> 1566class _LIBCPP_TEMPLATE_VIS basic_fstream 1567 : public basic_iostream<_CharT, _Traits> 1568{ 1569public: 1570 typedef _CharT char_type; 1571 typedef _Traits traits_type; 1572 typedef typename traits_type::int_type int_type; 1573 typedef typename traits_type::pos_type pos_type; 1574 typedef typename traits_type::off_type off_type; 1575 1576 _LIBCPP_INLINE_VISIBILITY 1577 basic_fstream(); 1578#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 1579 _LIBCPP_INLINE_VISIBILITY 1580 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); 1581#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1582 _LIBCPP_INLINE_VISIBILITY 1583 explicit basic_fstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); 1584#endif 1585 _LIBCPP_INLINE_VISIBILITY 1586 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out); 1587 1588#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY) 1589 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY 1590 explicit basic_fstream(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) 1591 : basic_fstream(__p.c_str(), __mode) {} 1592#endif // _LIBCPP_STD_VER >= 17 1593 1594#endif 1595#ifndef _LIBCPP_CXX03_LANG 1596 _LIBCPP_INLINE_VISIBILITY 1597 basic_fstream(basic_fstream&& __rhs); 1598 1599 _LIBCPP_INLINE_VISIBILITY 1600 basic_fstream& operator=(basic_fstream&& __rhs); 1601#endif 1602 _LIBCPP_INLINE_VISIBILITY 1603 void swap(basic_fstream& __rhs); 1604 1605 _LIBCPP_INLINE_VISIBILITY 1606 basic_filebuf<char_type, traits_type>* rdbuf() const; 1607 _LIBCPP_INLINE_VISIBILITY 1608 bool is_open() const; 1609#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 1610 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); 1611#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1612 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out); 1613#endif 1614 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out); 1615 1616#if _LIBCPP_STD_VER >= 17 && !defined(_LIBCPP_HAS_NO_FILESYSTEM_LIBRARY) 1617 _LIBCPP_AVAILABILITY_FILESYSTEM _LIBCPP_INLINE_VISIBILITY 1618 void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in|ios_base::out) 1619 { return open(__p.c_str(), __mode); } 1620#endif // _LIBCPP_STD_VER >= 17 1621 1622#endif 1623 _LIBCPP_INLINE_VISIBILITY 1624 void close(); 1625 1626private: 1627 basic_filebuf<char_type, traits_type> __sb_; 1628}; 1629 1630template <class _CharT, class _Traits> 1631inline 1632basic_fstream<_CharT, _Traits>::basic_fstream() 1633 : basic_iostream<char_type, traits_type>(&__sb_) 1634{ 1635} 1636 1637#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 1638template <class _CharT, class _Traits> 1639inline 1640basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode) 1641 : basic_iostream<char_type, traits_type>(&__sb_) 1642{ 1643 if (__sb_.open(__s, __mode) == nullptr) 1644 this->setstate(ios_base::failbit); 1645} 1646 1647#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1648template <class _CharT, class _Traits> 1649inline 1650basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode) 1651 : basic_iostream<char_type, traits_type>(&__sb_) 1652{ 1653 if (__sb_.open(__s, __mode) == nullptr) 1654 this->setstate(ios_base::failbit); 1655} 1656#endif 1657 1658template <class _CharT, class _Traits> 1659inline 1660basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode) 1661 : basic_iostream<char_type, traits_type>(&__sb_) 1662{ 1663 if (__sb_.open(__s, __mode) == nullptr) 1664 this->setstate(ios_base::failbit); 1665} 1666#endif 1667 1668#ifndef _LIBCPP_CXX03_LANG 1669 1670template <class _CharT, class _Traits> 1671inline 1672basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs) 1673 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)), 1674 __sb_(_VSTD::move(__rhs.__sb_)) 1675{ 1676 this->set_rdbuf(&__sb_); 1677} 1678 1679template <class _CharT, class _Traits> 1680inline 1681basic_fstream<_CharT, _Traits>& 1682basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs) 1683{ 1684 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs)); 1685 __sb_ = _VSTD::move(__rhs.__sb_); 1686 return *this; 1687} 1688 1689#endif // _LIBCPP_CXX03_LANG 1690 1691template <class _CharT, class _Traits> 1692inline 1693void 1694basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs) 1695{ 1696 basic_iostream<char_type, traits_type>::swap(__rhs); 1697 __sb_.swap(__rhs.__sb_); 1698} 1699 1700template <class _CharT, class _Traits> 1701inline _LIBCPP_INLINE_VISIBILITY 1702void 1703swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) 1704{ 1705 __x.swap(__y); 1706} 1707 1708template <class _CharT, class _Traits> 1709inline 1710basic_filebuf<_CharT, _Traits>* 1711basic_fstream<_CharT, _Traits>::rdbuf() const 1712{ 1713 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_); 1714} 1715 1716template <class _CharT, class _Traits> 1717inline 1718bool 1719basic_fstream<_CharT, _Traits>::is_open() const 1720{ 1721 return __sb_.is_open(); 1722} 1723 1724#ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE 1725template <class _CharT, class _Traits> 1726void 1727basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) 1728{ 1729 if (__sb_.open(__s, __mode)) 1730 this->clear(); 1731 else 1732 this->setstate(ios_base::failbit); 1733} 1734 1735#ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR 1736template <class _CharT, class _Traits> 1737void 1738basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) 1739{ 1740 if (__sb_.open(__s, __mode)) 1741 this->clear(); 1742 else 1743 this->setstate(ios_base::failbit); 1744} 1745#endif 1746 1747template <class _CharT, class _Traits> 1748void 1749basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) 1750{ 1751 if (__sb_.open(__s, __mode)) 1752 this->clear(); 1753 else 1754 this->setstate(ios_base::failbit); 1755} 1756#endif 1757 1758template <class _CharT, class _Traits> 1759inline 1760void 1761basic_fstream<_CharT, _Traits>::close() 1762{ 1763 if (__sb_.close() == nullptr) 1764 this->setstate(ios_base::failbit); 1765} 1766 1767#if defined(_LIBCPP_ABI_ENABLE_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1) 1768_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>) 1769_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>) 1770_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>) 1771#endif 1772 1773_LIBCPP_END_NAMESPACE_STD 1774 1775_LIBCPP_POP_MACROS 1776 1777#endif // _LIBCPP_FSTREAM 1778