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