1465 lines · plain
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03_FSTREAM11#define _LIBCPP___CXX03_FSTREAM12 13/*14 fstream synopsis15 16template <class charT, class traits = char_traits<charT> >17class basic_filebuf18 : 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++1741 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 void61 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_ifstream68 : 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 using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++2677 78 basic_ifstream();79 explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);80 explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);81 template<class T>82 explicit basic_ifstream(const T& s, ios_base::openmode mode = ios_base::in); // Since C++1783 basic_ifstream(basic_ifstream&& rhs);84 85 basic_ifstream& operator=(basic_ifstream&& rhs);86 void swap(basic_ifstream& rhs);87 88 basic_filebuf<char_type, traits_type>* rdbuf() const;89 native_handle_type native_handle() const noexcept; // Since C++2690 bool is_open() const;91 void open(const char* s, ios_base::openmode mode = ios_base::in);92 void open(const string& s, ios_base::openmode mode = ios_base::in);93 void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++1794 95 void close();96};97 98template <class charT, class traits>99 void100 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);101 102typedef basic_ifstream<char> ifstream;103typedef basic_ifstream<wchar_t> wifstream;104 105template <class charT, class traits = char_traits<charT> >106class basic_ofstream107 : public basic_ostream<charT,traits>108{109public:110 typedef charT char_type;111 typedef traits traits_type;112 typedef typename traits_type::int_type int_type;113 typedef typename traits_type::pos_type pos_type;114 typedef typename traits_type::off_type off_type;115 using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26116 117 basic_ofstream();118 explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);119 explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);120 template<class T>121 explicit basic_ofstream(const T& s, ios_base::openmode mode = ios_base::out); // Since C++17122 basic_ofstream(basic_ofstream&& rhs);123 124 basic_ofstream& operator=(basic_ofstream&& rhs);125 void swap(basic_ofstream& rhs);126 127 basic_filebuf<char_type, traits_type>* rdbuf() const;128 native_handle_type native_handle() const noexcept; // Since C++26129 130 bool is_open() const;131 void open(const char* s, ios_base::openmode mode = ios_base::out);132 void open(const string& s, ios_base::openmode mode = ios_base::out);133 void open(const filesystem::path& p,134 ios_base::openmode mode = ios_base::out); // C++17135 136 void close();137};138 139template <class charT, class traits>140 void141 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);142 143typedef basic_ofstream<char> ofstream;144typedef basic_ofstream<wchar_t> wofstream;145 146template <class charT, class traits=char_traits<charT> >147class basic_fstream148 : public basic_iostream<charT,traits>149{150public:151 typedef charT char_type;152 typedef traits traits_type;153 typedef typename traits_type::int_type int_type;154 typedef typename traits_type::pos_type pos_type;155 typedef typename traits_type::off_type off_type;156 using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26157 158 basic_fstream();159 explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);160 explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);161 template<class T>162 explicit basic_fstream(const T& s, ios_base::openmode mode = ios_base::in | ios_base::out); // Since C++17163 basic_fstream(basic_fstream&& rhs);164 165 basic_fstream& operator=(basic_fstream&& rhs);166 void swap(basic_fstream& rhs);167 168 basic_filebuf<char_type, traits_type>* rdbuf() const;169 native_handle_type native_handle() const noexcept; // Since C++26170 bool is_open() const;171 void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);172 void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);173 void open(const filesystem::path& s,174 ios_base::openmode mode = ios_base::in|ios_base::out); // C++17175 176 void close();177};178 179template <class charT, class traits>180 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);181 182typedef basic_fstream<char> fstream;183typedef basic_fstream<wchar_t> wfstream;184 185} // std186 187*/188 189#include <__cxx03/__algorithm/max.h>190#include <__cxx03/__assert>191#include <__cxx03/__config>192#include <__cxx03/__fwd/fstream.h>193#include <__cxx03/__locale>194#include <__cxx03/__memory/addressof.h>195#include <__cxx03/__type_traits/enable_if.h>196#include <__cxx03/__type_traits/is_same.h>197#include <__cxx03/__utility/move.h>198#include <__cxx03/__utility/swap.h>199#include <__cxx03/__utility/unreachable.h>200#include <__cxx03/cstdio>201#include <__cxx03/istream>202#include <__cxx03/ostream>203#include <__cxx03/typeinfo>204#include <__cxx03/version>205 206#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)207# pragma GCC system_header208#endif209 210_LIBCPP_PUSH_MACROS211#include <__cxx03/__undef_macros>212 213#if defined(_LIBCPP_MSVCRT) || _LIBCPP_LIBC_NEWLIB214# define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS215#endif216 217#if !defined(_LIBCPP_HAS_NO_FILESYSTEM)218 219_LIBCPP_BEGIN_NAMESPACE_STD220 221template <class _CharT, class _Traits>222class _LIBCPP_TEMPLATE_VIS basic_filebuf : public basic_streambuf<_CharT, _Traits> {223public:224 typedef _CharT char_type;225 typedef _Traits traits_type;226 typedef typename traits_type::int_type int_type;227 typedef typename traits_type::pos_type pos_type;228 typedef typename traits_type::off_type off_type;229 typedef typename traits_type::state_type state_type;230 231 // 27.9.1.2 Constructors/destructor:232 basic_filebuf();233 basic_filebuf(basic_filebuf&& __rhs);234 ~basic_filebuf() override;235 236 // 27.9.1.3 Assign/swap:237 _LIBCPP_HIDE_FROM_ABI basic_filebuf& operator=(basic_filebuf&& __rhs);238 void swap(basic_filebuf& __rhs);239 240 // 27.9.1.4 Members:241 _LIBCPP_HIDE_FROM_ABI bool is_open() const;242 basic_filebuf* open(const char* __s, ios_base::openmode __mode);243# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR244 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);245# endif246 _LIBCPP_HIDE_FROM_ABI basic_filebuf* open(const string& __s, ios_base::openmode __mode);247 248 _LIBCPP_HIDE_FROM_ABI basic_filebuf* __open(int __fd, ios_base::openmode __mode);249 basic_filebuf* close();250 251 _LIBCPP_HIDE_FROM_ABI inline static const char* __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;252# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR253 _LIBCPP_HIDE_FROM_ABI inline static const wchar_t* __make_mdwstring(ios_base::openmode __mode) _NOEXCEPT;254# endif255 256protected:257 // 27.9.1.5 Overridden virtual functions:258 int_type underflow() override;259 int_type pbackfail(int_type __c = traits_type::eof()) override;260 int_type overflow(int_type __c = traits_type::eof()) override;261 basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override;262 pos_type263 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out) override;264 pos_type seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out) override;265 int sync() override;266 void imbue(const locale& __loc) override;267 268private:269 char* __extbuf_;270 const char* __extbufnext_;271 const char* __extbufend_;272 char __extbuf_min_[8];273 size_t __ebs_;274 char_type* __intbuf_;275 size_t __ibs_;276 FILE* __file_;277 const codecvt<char_type, char, state_type>* __cv_;278 state_type __st_;279 state_type __st_last_;280 ios_base::openmode __om_;281 // There have been no file operations yet, which allows setting unbuffered282 // I/O mode.283 static const ios_base::openmode __no_io_operations = ios_base::trunc;284 // Unbuffered I/O mode has been requested.285 static const ios_base::openmode __use_unbuffered_io = ios_base::ate;286 // Used to track the currently used mode and track whether the output should287 // be unbuffered.288 // [filebuf.virtuals]/12289 // If setbuf(0, 0) is called on a stream before any I/O has occurred on290 // that stream, the stream becomes unbuffered. Otherwise the results are291 // implementation-defined.292 // This allows calling setbuf(0, 0)293 // - before opening a file,294 // - after opening a file, before295 // - a read296 // - a write297 // - a seek.298 // Note that opening a file with ios_base::ate does a seek operation.299 // Normally underflow, overflow, and sync change this flag to ios_base::in,300 // ios_base_out, or 0.301 //302 // The ios_base::trunc and ios_base::ate flags are not used in __cm_. They303 // are used to track the state of the unbuffered request. For readability304 // they have the aliases __no_io_operations and __use_unbuffered_io305 // respectively.306 //307 // The __no_io_operations and __use_unbuffered_io flags are used in the308 // following way:309 // - __no_io_operations is set upon construction to indicate the unbuffered310 // state can be set.311 // - When requesting unbuffered output:312 // - If the file is open it sets the mode.313 // - Else places a request by adding the __use_unbuffered_io flag.314 // - When a file is opened it checks whether both __no_io_operations and315 // __use_unbuffered_io are set. If so switches to unbuffered mode.316 // - All file I/O operations change the mode effectively clearing the317 // __no_io_operations and __use_unbuffered_io flags.318 ios_base::openmode __cm_;319 bool __owns_eb_;320 bool __owns_ib_;321 bool __always_noconv_;322 323 bool __read_mode();324 void __write_mode();325 326 _LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);327 328 // There are multiple (__)open function, they use different C-API open329 // function. After that call these functions behave the same. This function330 // does that part and determines the final return value.331 _LIBCPP_HIDE_FROM_ABI basic_filebuf* __do_open(FILE* __file, ios_base::openmode __mode) {332 __file_ = __file;333 if (!__file_)334 return nullptr;335 336 __om_ = __mode;337 if (__cm_ == (__no_io_operations | __use_unbuffered_io)) {338 std::setbuf(__file_, nullptr);339 __cm_ = 0;340 }341 342 if (__mode & ios_base::ate) {343 __cm_ = 0;344 if (fseek(__file_, 0, SEEK_END)) {345 fclose(__file_);346 __file_ = nullptr;347 return nullptr;348 }349 }350 351 return this;352 }353 354 // If the file is already open, switch to unbuffered mode. Otherwise, record355 // the request to use unbuffered mode so that we use that mode when we356 // eventually open the file.357 _LIBCPP_HIDE_FROM_ABI void __request_unbuffered_mode(char_type* __s, streamsize __n) {358 if (__cm_ == __no_io_operations && __s == nullptr && __n == 0) {359 if (__file_) {360 std::setbuf(__file_, nullptr);361 __cm_ = 0;362 } else {363 __cm_ = __no_io_operations | __use_unbuffered_io;364 }365 }366 }367};368 369template <class _CharT, class _Traits>370basic_filebuf<_CharT, _Traits>::basic_filebuf()371 : __extbuf_(nullptr),372 __extbufnext_(nullptr),373 __extbufend_(nullptr),374 __ebs_(0),375 __intbuf_(nullptr),376 __ibs_(0),377 __file_(nullptr),378 __cv_(nullptr),379 __st_(),380 __st_last_(),381 __om_(0),382 __cm_(__no_io_operations),383 __owns_eb_(false),384 __owns_ib_(false),385 __always_noconv_(false) {386 if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc())) {387 __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(this->getloc());388 __always_noconv_ = __cv_->always_noconv();389 }390 setbuf(nullptr, 4096);391}392 393template <class _CharT, class _Traits>394basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) : basic_streambuf<_CharT, _Traits>(__rhs) {395 if (__rhs.__extbuf_ == __rhs.__extbuf_min_) {396 __extbuf_ = __extbuf_min_;397 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);398 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);399 } else {400 __extbuf_ = __rhs.__extbuf_;401 __extbufnext_ = __rhs.__extbufnext_;402 __extbufend_ = __rhs.__extbufend_;403 }404 __ebs_ = __rhs.__ebs_;405 __intbuf_ = __rhs.__intbuf_;406 __ibs_ = __rhs.__ibs_;407 __file_ = __rhs.__file_;408 __cv_ = __rhs.__cv_;409 __st_ = __rhs.__st_;410 __st_last_ = __rhs.__st_last_;411 __om_ = __rhs.__om_;412 __cm_ = __rhs.__cm_;413 __owns_eb_ = __rhs.__owns_eb_;414 __owns_ib_ = __rhs.__owns_ib_;415 __always_noconv_ = __rhs.__always_noconv_;416 if (__rhs.pbase()) {417 if (__rhs.pbase() == __rhs.__intbuf_)418 this->setp(__intbuf_, __intbuf_ + (__rhs.epptr() - __rhs.pbase()));419 else420 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__rhs.epptr() - __rhs.pbase()));421 this->__pbump(__rhs.pptr() - __rhs.pbase());422 } else if (__rhs.eback()) {423 if (__rhs.eback() == __rhs.__intbuf_)424 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), __intbuf_ + (__rhs.egptr() - __rhs.eback()));425 else426 this->setg((char_type*)__extbuf_,427 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),428 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));429 }430 __rhs.__extbuf_ = nullptr;431 __rhs.__extbufnext_ = nullptr;432 __rhs.__extbufend_ = nullptr;433 __rhs.__ebs_ = 0;434 __rhs.__intbuf_ = 0;435 __rhs.__ibs_ = 0;436 __rhs.__file_ = nullptr;437 __rhs.__st_ = state_type();438 __rhs.__st_last_ = state_type();439 __rhs.__om_ = 0;440 __rhs.__cm_ = 0;441 __rhs.__owns_eb_ = false;442 __rhs.__owns_ib_ = false;443 __rhs.setg(0, 0, 0);444 __rhs.setp(0, 0);445}446 447template <class _CharT, class _Traits>448inline basic_filebuf<_CharT, _Traits>& basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) {449 close();450 swap(__rhs);451 return *this;452}453 454template <class _CharT, class _Traits>455basic_filebuf<_CharT, _Traits>::~basic_filebuf() {456# ifndef _LIBCPP_HAS_NO_EXCEPTIONS457 try {458# endif // _LIBCPP_HAS_NO_EXCEPTIONS459 close();460# ifndef _LIBCPP_HAS_NO_EXCEPTIONS461 } catch (...) {462 }463# endif // _LIBCPP_HAS_NO_EXCEPTIONS464 if (__owns_eb_)465 delete[] __extbuf_;466 if (__owns_ib_)467 delete[] __intbuf_;468}469 470template <class _CharT, class _Traits>471void basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) {472 basic_streambuf<char_type, traits_type>::swap(__rhs);473 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {474 // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.475 std::swap(__extbuf_, __rhs.__extbuf_);476 std::swap(__extbufnext_, __rhs.__extbufnext_);477 std::swap(__extbufend_, __rhs.__extbufend_);478 } else {479 ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;480 ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;481 ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;482 ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;483 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {484 // *this uses the small buffer, but __rhs doesn't.485 __extbuf_ = __rhs.__extbuf_;486 __rhs.__extbuf_ = __rhs.__extbuf_min_;487 std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));488 } else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) {489 // *this doesn't use the small buffer, but __rhs does.490 __rhs.__extbuf_ = __extbuf_;491 __extbuf_ = __extbuf_min_;492 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));493 } else {494 // Both *this and __rhs use the small buffer.495 char __tmp[sizeof(__extbuf_min_)];496 std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));497 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));498 std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));499 }500 __extbufnext_ = __extbuf_ + __rn;501 __extbufend_ = __extbuf_ + __re;502 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;503 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;504 }505 std::swap(__ebs_, __rhs.__ebs_);506 std::swap(__intbuf_, __rhs.__intbuf_);507 std::swap(__ibs_, __rhs.__ibs_);508 std::swap(__file_, __rhs.__file_);509 std::swap(__cv_, __rhs.__cv_);510 std::swap(__st_, __rhs.__st_);511 std::swap(__st_last_, __rhs.__st_last_);512 std::swap(__om_, __rhs.__om_);513 std::swap(__cm_, __rhs.__cm_);514 std::swap(__owns_eb_, __rhs.__owns_eb_);515 std::swap(__owns_ib_, __rhs.__owns_ib_);516 std::swap(__always_noconv_, __rhs.__always_noconv_);517 if (this->eback() == (char_type*)__rhs.__extbuf_min_) {518 ptrdiff_t __n = this->gptr() - this->eback();519 ptrdiff_t __e = this->egptr() - this->eback();520 this->setg((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __n, (char_type*)__extbuf_min_ + __e);521 } else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) {522 ptrdiff_t __n = this->pptr() - this->pbase();523 ptrdiff_t __e = this->epptr() - this->pbase();524 this->setp((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __e);525 this->__pbump(__n);526 }527 if (__rhs.eback() == (char_type*)__extbuf_min_) {528 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();529 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();530 __rhs.setg(531 (char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __n, (char_type*)__rhs.__extbuf_min_ + __e);532 } else if (__rhs.pbase() == (char_type*)__extbuf_min_) {533 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();534 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();535 __rhs.setp((char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __e);536 __rhs.__pbump(__n);537 }538}539 540template <class _CharT, class _Traits>541inline _LIBCPP_HIDE_FROM_ABI void swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) {542 __x.swap(__y);543}544 545template <class _CharT, class _Traits>546inline bool basic_filebuf<_CharT, _Traits>::is_open() const {547 return __file_ != nullptr;548}549 550template <class _CharT, class _Traits>551const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(ios_base::openmode __mode) _NOEXCEPT {552 switch (__mode & ~ios_base::ate) {553 case ios_base::out:554 case ios_base::out | ios_base::trunc:555 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;556 case ios_base::out | ios_base::app:557 case ios_base::app:558 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;559 case ios_base::in:560 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;561 case ios_base::in | ios_base::out:562 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;563 case ios_base::in | ios_base::out | ios_base::trunc:564 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;565 case ios_base::in | ios_base::out | ios_base::app:566 case ios_base::in | ios_base::app:567 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;568 case ios_base::out | ios_base::binary:569 case ios_base::out | ios_base::trunc | ios_base::binary:570 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;571 case ios_base::out | ios_base::app | ios_base::binary:572 case ios_base::app | ios_base::binary:573 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;574 case ios_base::in | ios_base::binary:575 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;576 case ios_base::in | ios_base::out | ios_base::binary:577 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;578 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:579 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;580 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:581 case ios_base::in | ios_base::app | ios_base::binary:582 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;583 default:584 return nullptr;585 }586 __libcpp_unreachable();587}588 589# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR590template <class _CharT, class _Traits>591const wchar_t* basic_filebuf<_CharT, _Traits>::__make_mdwstring(ios_base::openmode __mode) _NOEXCEPT {592 switch (__mode & ~ios_base::ate) {593 case ios_base::out:594 case ios_base::out | ios_base::trunc:595 return L"w";596 case ios_base::out | ios_base::app:597 case ios_base::app:598 return L"a";599 case ios_base::in:600 return L"r";601 case ios_base::in | ios_base::out:602 return L"r+";603 case ios_base::in | ios_base::out | ios_base::trunc:604 return L"w+";605 case ios_base::in | ios_base::out | ios_base::app:606 case ios_base::in | ios_base::app:607 return L"a+";608 case ios_base::out | ios_base::binary:609 case ios_base::out | ios_base::trunc | ios_base::binary:610 return L"wb";611 case ios_base::out | ios_base::app | ios_base::binary:612 case ios_base::app | ios_base::binary:613 return L"ab";614 case ios_base::in | ios_base::binary:615 return L"rb";616 case ios_base::in | ios_base::out | ios_base::binary:617 return L"r+b";618 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:619 return L"w+b";620 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:621 case ios_base::in | ios_base::app | ios_base::binary:622 return L"a+b";623 default:624 return nullptr;625 }626 __libcpp_unreachable();627}628# endif629 630template <class _CharT, class _Traits>631basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {632 if (__file_)633 return nullptr;634 const char* __mdstr = __make_mdstring(__mode);635 if (!__mdstr)636 return nullptr;637 638 return __do_open(fopen(__s, __mdstr), __mode);639}640 641template <class _CharT, class _Traits>642inline basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {643 if (__file_)644 return nullptr;645 const char* __mdstr = __make_mdstring(__mode);646 if (!__mdstr)647 return nullptr;648 649 return __do_open(fdopen(__fd, __mdstr), __mode);650}651 652# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR653// This is basically the same as the char* overload except that it uses _wfopen654// and long mode strings.655template <class _CharT, class _Traits>656basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {657 if (__file_)658 return nullptr;659 const wchar_t* __mdstr = __make_mdwstring(__mode);660 if (!__mdstr)661 return nullptr;662 663 return __do_open(_wfopen(__s, __mdstr), __mode);664}665# endif666 667template <class _CharT, class _Traits>668inline basic_filebuf<_CharT, _Traits>*669basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {670 return open(__s.c_str(), __mode);671}672 673template <class _CharT, class _Traits>674basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::close() {675 basic_filebuf<_CharT, _Traits>* __rt = nullptr;676 if (__file_) {677 __rt = this;678 unique_ptr<FILE, int (*)(FILE*)> __h(__file_, fclose);679 if (sync())680 __rt = nullptr;681 if (fclose(__h.release()))682 __rt = nullptr;683 __file_ = nullptr;684 setbuf(0, 0);685 }686 return __rt;687}688 689template <class _CharT, class _Traits>690typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::underflow() {691 if (__file_ == nullptr)692 return traits_type::eof();693 bool __initial = __read_mode();694 char_type __1buf;695 if (this->gptr() == nullptr)696 this->setg(&__1buf, &__1buf + 1, &__1buf + 1);697 const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);698 int_type __c = traits_type::eof();699 if (this->gptr() == this->egptr()) {700 std::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));701 if (__always_noconv_) {702 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);703 __nmemb = ::fread(this->eback() + __unget_sz, 1, __nmemb, __file_);704 if (__nmemb != 0) {705 this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);706 __c = traits_type::to_int_type(*this->gptr());707 }708 } else {709 if (__extbufend_ != __extbufnext_) {710 _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");711 _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");712 std::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);713 }714 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);715 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);716 size_t __nmemb =717 std::min(static_cast<size_t>(__ibs_ - __unget_sz), static_cast<size_t>(__extbufend_ - __extbufnext_));718 codecvt_base::result __r;719 __st_last_ = __st_;720 size_t __nr = fread((void*)const_cast<char*>(__extbufnext_), 1, __nmemb, __file_);721 if (__nr != 0) {722 if (!__cv_)723 __throw_bad_cast();724 725 __extbufend_ = __extbufnext_ + __nr;726 char_type* __inext;727 __r = __cv_->in(728 __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->eback() + __ibs_, __inext);729 if (__r == codecvt_base::noconv) {730 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));731 __c = traits_type::to_int_type(*this->gptr());732 } else if (__inext != this->eback() + __unget_sz) {733 this->setg(this->eback(), this->eback() + __unget_sz, __inext);734 __c = traits_type::to_int_type(*this->gptr());735 }736 }737 }738 } else739 __c = traits_type::to_int_type(*this->gptr());740 if (this->eback() == &__1buf)741 this->setg(nullptr, nullptr, nullptr);742 return __c;743}744 745template <class _CharT, class _Traits>746typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {747 if (__file_ && this->eback() < this->gptr()) {748 if (traits_type::eq_int_type(__c, traits_type::eof())) {749 this->gbump(-1);750 return traits_type::not_eof(__c);751 }752 if ((__om_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {753 this->gbump(-1);754 *this->gptr() = traits_type::to_char_type(__c);755 return __c;756 }757 }758 return traits_type::eof();759}760 761template <class _CharT, class _Traits>762typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {763 if (__file_ == nullptr)764 return traits_type::eof();765 __write_mode();766 char_type __1buf;767 char_type* __pb_save = this->pbase();768 char_type* __epb_save = this->epptr();769 if (!traits_type::eq_int_type(__c, traits_type::eof())) {770 if (this->pptr() == nullptr)771 this->setp(&__1buf, &__1buf + 1);772 *this->pptr() = traits_type::to_char_type(__c);773 this->pbump(1);774 }775 if (this->pptr() != this->pbase()) {776 if (__always_noconv_) {777 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());778 if (std::fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)779 return traits_type::eof();780 } else {781 char* __extbe = __extbuf_;782 codecvt_base::result __r;783 do {784 if (!__cv_)785 __throw_bad_cast();786 787 const char_type* __e;788 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);789 if (__e == this->pbase())790 return traits_type::eof();791 if (__r == codecvt_base::noconv) {792 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());793 if (std::fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)794 return traits_type::eof();795 } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {796 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);797 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)798 return traits_type::eof();799 if (__r == codecvt_base::partial) {800 this->setp(const_cast<char_type*>(__e), this->pptr());801 this->__pbump(this->epptr() - this->pbase());802 }803 } else804 return traits_type::eof();805 } while (__r == codecvt_base::partial);806 }807 this->setp(__pb_save, __epb_save);808 }809 return traits_type::not_eof(__c);810}811 812template <class _CharT, class _Traits>813basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {814 this->setg(nullptr, nullptr, nullptr);815 this->setp(nullptr, nullptr);816 __request_unbuffered_mode(__s, __n);817 if (__owns_eb_)818 delete[] __extbuf_;819 if (__owns_ib_)820 delete[] __intbuf_;821 __ebs_ = __n;822 if (__ebs_ > sizeof(__extbuf_min_)) {823 if (__always_noconv_ && __s) {824 __extbuf_ = (char*)__s;825 __owns_eb_ = false;826 } else {827 __extbuf_ = new char[__ebs_];828 __owns_eb_ = true;829 }830 } else {831 __extbuf_ = __extbuf_min_;832 __ebs_ = sizeof(__extbuf_min_);833 __owns_eb_ = false;834 }835 if (!__always_noconv_) {836 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));837 if (__s && __ibs_ > sizeof(__extbuf_min_)) {838 __intbuf_ = __s;839 __owns_ib_ = false;840 } else {841 __intbuf_ = new char_type[__ibs_];842 __owns_ib_ = true;843 }844 } else {845 __ibs_ = 0;846 __intbuf_ = nullptr;847 __owns_ib_ = false;848 }849 return this;850}851 852template <class _CharT, class _Traits>853typename basic_filebuf<_CharT, _Traits>::pos_type854basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) {855 if (!__cv_)856 __throw_bad_cast();857 858 int __width = __cv_->encoding();859 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())860 return pos_type(off_type(-1));861 // __width > 0 || __off == 0862 int __whence;863 switch (__way) {864 case ios_base::beg:865 __whence = SEEK_SET;866 break;867 case ios_base::cur:868 __whence = SEEK_CUR;869 break;870 case ios_base::end:871 __whence = SEEK_END;872 break;873 default:874 return pos_type(off_type(-1));875 }876# if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)877 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))878 return pos_type(off_type(-1));879 pos_type __r = ftell(__file_);880# else881 if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))882 return pos_type(off_type(-1));883 pos_type __r = ftello(__file_);884# endif885 __r.state(__st_);886 return __r;887}888 889template <class _CharT, class _Traits>890typename basic_filebuf<_CharT, _Traits>::pos_type891basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {892 if (__file_ == nullptr || sync())893 return pos_type(off_type(-1));894# if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)895 if (fseek(__file_, __sp, SEEK_SET))896 return pos_type(off_type(-1));897# else898 if (::fseeko(__file_, __sp, SEEK_SET))899 return pos_type(off_type(-1));900# endif901 __st_ = __sp.state();902 return __sp;903}904 905template <class _CharT, class _Traits>906int basic_filebuf<_CharT, _Traits>::sync() {907 if (__file_ == nullptr)908 return 0;909 if (!__cv_)910 __throw_bad_cast();911 912 if (__cm_ & ios_base::out) {913 if (this->pptr() != this->pbase())914 if (overflow() == traits_type::eof())915 return -1;916 codecvt_base::result __r;917 do {918 char* __extbe;919 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);920 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);921 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)922 return -1;923 } while (__r == codecvt_base::partial);924 if (__r == codecvt_base::error)925 return -1;926 if (fflush(__file_))927 return -1;928 } else if (__cm_ & ios_base::in) {929 off_type __c;930 state_type __state = __st_last_;931 bool __update_st = false;932 if (__always_noconv_)933 __c = this->egptr() - this->gptr();934 else {935 int __width = __cv_->encoding();936 __c = __extbufend_ - __extbufnext_;937 if (__width > 0)938 __c += __width * (this->egptr() - this->gptr());939 else {940 if (this->gptr() != this->egptr()) {941 const int __off = __cv_->length(__state, __extbuf_, __extbufnext_, this->gptr() - this->eback());942 __c += __extbufnext_ - __extbuf_ - __off;943 __update_st = true;944 }945 }946 }947# if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)948 if (fseek(__file_, -__c, SEEK_CUR))949 return -1;950# else951 if (::fseeko(__file_, -__c, SEEK_CUR))952 return -1;953# endif954 if (__update_st)955 __st_ = __state;956 __extbufnext_ = __extbufend_ = __extbuf_;957 this->setg(nullptr, nullptr, nullptr);958 __cm_ = 0;959 }960 return 0;961}962 963template <class _CharT, class _Traits>964void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {965 sync();966 __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(__loc);967 bool __old_anc = __always_noconv_;968 __always_noconv_ = __cv_->always_noconv();969 if (__old_anc != __always_noconv_) {970 this->setg(nullptr, nullptr, nullptr);971 this->setp(nullptr, nullptr);972 // invariant, char_type is char, else we couldn't get here973 if (__always_noconv_) // need to dump __intbuf_974 {975 if (__owns_eb_)976 delete[] __extbuf_;977 __owns_eb_ = __owns_ib_;978 __ebs_ = __ibs_;979 __extbuf_ = (char*)__intbuf_;980 __ibs_ = 0;981 __intbuf_ = nullptr;982 __owns_ib_ = false;983 } else // need to obtain an __intbuf_.984 { // If __extbuf_ is user-supplied, use it, else new __intbuf_985 if (!__owns_eb_ && __extbuf_ != __extbuf_min_) {986 __ibs_ = __ebs_;987 __intbuf_ = (char_type*)__extbuf_;988 __owns_ib_ = false;989 __extbuf_ = new char[__ebs_];990 __owns_eb_ = true;991 } else {992 __ibs_ = __ebs_;993 __intbuf_ = new char_type[__ibs_];994 __owns_ib_ = true;995 }996 }997 }998}999 1000template <class _CharT, class _Traits>1001bool basic_filebuf<_CharT, _Traits>::__read_mode() {1002 if (!(__cm_ & ios_base::in)) {1003 this->setp(nullptr, nullptr);1004 if (__always_noconv_)1005 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_ + __ebs_, (char_type*)__extbuf_ + __ebs_);1006 else1007 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);1008 __cm_ = ios_base::in;1009 return true;1010 }1011 return false;1012}1013 1014template <class _CharT, class _Traits>1015void basic_filebuf<_CharT, _Traits>::__write_mode() {1016 if (!(__cm_ & ios_base::out)) {1017 this->setg(nullptr, nullptr, nullptr);1018 if (__ebs_ > sizeof(__extbuf_min_)) {1019 if (__always_noconv_)1020 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));1021 else1022 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));1023 } else1024 this->setp(nullptr, nullptr);1025 __cm_ = ios_base::out;1026 }1027}1028 1029// basic_ifstream1030 1031template <class _CharT, class _Traits>1032class _LIBCPP_TEMPLATE_VIS basic_ifstream : public basic_istream<_CharT, _Traits> {1033public:1034 typedef _CharT char_type;1035 typedef _Traits traits_type;1036 typedef typename traits_type::int_type int_type;1037 typedef typename traits_type::pos_type pos_type;1038 typedef typename traits_type::off_type off_type;1039 1040 _LIBCPP_HIDE_FROM_ABI basic_ifstream();1041 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);1042# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1043 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);1044# endif1045 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);1046 _LIBCPP_HIDE_FROM_ABI basic_ifstream(basic_ifstream&& __rhs);1047 _LIBCPP_HIDE_FROM_ABI basic_ifstream& operator=(basic_ifstream&& __rhs);1048 _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream& __rhs);1049 1050 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;1051 _LIBCPP_HIDE_FROM_ABI bool is_open() const;1052 void open(const char* __s, ios_base::openmode __mode = ios_base::in);1053# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1054 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);1055# endif1056 void open(const string& __s, ios_base::openmode __mode = ios_base::in);1057 1058 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);1059 _LIBCPP_HIDE_FROM_ABI void close();1060 1061private:1062 basic_filebuf<char_type, traits_type> __sb_;1063};1064 1065template <class _CharT, class _Traits>1066inline basic_ifstream<_CharT, _Traits>::basic_ifstream()1067 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {}1068 1069template <class _CharT, class _Traits>1070inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)1071 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {1072 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)1073 this->setstate(ios_base::failbit);1074}1075 1076# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1077template <class _CharT, class _Traits>1078inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)1079 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {1080 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)1081 this->setstate(ios_base::failbit);1082}1083# endif1084 1085// extension1086template <class _CharT, class _Traits>1087inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)1088 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {1089 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)1090 this->setstate(ios_base::failbit);1091}1092 1093template <class _CharT, class _Traits>1094inline basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)1095 : basic_istream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {1096 this->set_rdbuf(std::addressof(__sb_));1097}1098 1099template <class _CharT, class _Traits>1100inline basic_ifstream<_CharT, _Traits>& basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) {1101 basic_istream<char_type, traits_type>::operator=(std::move(__rhs));1102 __sb_ = std::move(__rhs.__sb_);1103 return *this;1104}1105 1106template <class _CharT, class _Traits>1107inline void basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) {1108 basic_istream<char_type, traits_type>::swap(__rhs);1109 __sb_.swap(__rhs.__sb_);1110}1111 1112template <class _CharT, class _Traits>1113inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) {1114 __x.swap(__y);1115}1116 1117template <class _CharT, class _Traits>1118inline basic_filebuf<_CharT, _Traits>* basic_ifstream<_CharT, _Traits>::rdbuf() const {1119 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));1120}1121 1122template <class _CharT, class _Traits>1123inline bool basic_ifstream<_CharT, _Traits>::is_open() const {1124 return __sb_.is_open();1125}1126 1127template <class _CharT, class _Traits>1128void basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {1129 if (__sb_.open(__s, __mode | ios_base::in))1130 this->clear();1131 else1132 this->setstate(ios_base::failbit);1133}1134 1135# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1136template <class _CharT, class _Traits>1137void basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {1138 if (__sb_.open(__s, __mode | ios_base::in))1139 this->clear();1140 else1141 this->setstate(ios_base::failbit);1142}1143# endif1144 1145template <class _CharT, class _Traits>1146void basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {1147 if (__sb_.open(__s, __mode | ios_base::in))1148 this->clear();1149 else1150 this->setstate(ios_base::failbit);1151}1152 1153template <class _CharT, class _Traits>1154inline void basic_ifstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {1155 if (__sb_.__open(__fd, __mode | ios_base::in))1156 this->clear();1157 else1158 this->setstate(ios_base::failbit);1159}1160 1161template <class _CharT, class _Traits>1162inline void basic_ifstream<_CharT, _Traits>::close() {1163 if (__sb_.close() == 0)1164 this->setstate(ios_base::failbit);1165}1166 1167// basic_ofstream1168 1169template <class _CharT, class _Traits>1170class _LIBCPP_TEMPLATE_VIS basic_ofstream : public basic_ostream<_CharT, _Traits> {1171public:1172 typedef _CharT char_type;1173 typedef _Traits traits_type;1174 typedef typename traits_type::int_type int_type;1175 typedef typename traits_type::pos_type pos_type;1176 typedef typename traits_type::off_type off_type;1177 1178 _LIBCPP_HIDE_FROM_ABI basic_ofstream();1179 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);1180# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1181 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);1182# endif1183 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);1184 1185 _LIBCPP_HIDE_FROM_ABI basic_ofstream(basic_ofstream&& __rhs);1186 _LIBCPP_HIDE_FROM_ABI basic_ofstream& operator=(basic_ofstream&& __rhs);1187 _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream& __rhs);1188 1189 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;1190 _LIBCPP_HIDE_FROM_ABI bool is_open() const;1191 void open(const char* __s, ios_base::openmode __mode = ios_base::out);1192# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1193 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);1194# endif1195 void open(const string& __s, ios_base::openmode __mode = ios_base::out);1196 1197 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);1198 _LIBCPP_HIDE_FROM_ABI void close();1199 1200private:1201 basic_filebuf<char_type, traits_type> __sb_;1202};1203 1204template <class _CharT, class _Traits>1205inline basic_ofstream<_CharT, _Traits>::basic_ofstream()1206 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {}1207 1208template <class _CharT, class _Traits>1209inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)1210 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {1211 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)1212 this->setstate(ios_base::failbit);1213}1214 1215# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1216template <class _CharT, class _Traits>1217inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)1218 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {1219 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)1220 this->setstate(ios_base::failbit);1221}1222# endif1223 1224// extension1225template <class _CharT, class _Traits>1226inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)1227 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {1228 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)1229 this->setstate(ios_base::failbit);1230}1231 1232template <class _CharT, class _Traits>1233inline basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)1234 : basic_ostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {1235 this->set_rdbuf(std::addressof(__sb_));1236}1237 1238template <class _CharT, class _Traits>1239inline basic_ofstream<_CharT, _Traits>& basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) {1240 basic_ostream<char_type, traits_type>::operator=(std::move(__rhs));1241 __sb_ = std::move(__rhs.__sb_);1242 return *this;1243}1244 1245template <class _CharT, class _Traits>1246inline void basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) {1247 basic_ostream<char_type, traits_type>::swap(__rhs);1248 __sb_.swap(__rhs.__sb_);1249}1250 1251template <class _CharT, class _Traits>1252inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) {1253 __x.swap(__y);1254}1255 1256template <class _CharT, class _Traits>1257inline basic_filebuf<_CharT, _Traits>* basic_ofstream<_CharT, _Traits>::rdbuf() const {1258 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));1259}1260 1261template <class _CharT, class _Traits>1262inline bool basic_ofstream<_CharT, _Traits>::is_open() const {1263 return __sb_.is_open();1264}1265 1266template <class _CharT, class _Traits>1267void basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {1268 if (__sb_.open(__s, __mode | ios_base::out))1269 this->clear();1270 else1271 this->setstate(ios_base::failbit);1272}1273 1274# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1275template <class _CharT, class _Traits>1276void basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {1277 if (__sb_.open(__s, __mode | ios_base::out))1278 this->clear();1279 else1280 this->setstate(ios_base::failbit);1281}1282# endif1283 1284template <class _CharT, class _Traits>1285void basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {1286 if (__sb_.open(__s, __mode | ios_base::out))1287 this->clear();1288 else1289 this->setstate(ios_base::failbit);1290}1291 1292template <class _CharT, class _Traits>1293inline void basic_ofstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {1294 if (__sb_.__open(__fd, __mode | ios_base::out))1295 this->clear();1296 else1297 this->setstate(ios_base::failbit);1298}1299 1300template <class _CharT, class _Traits>1301inline void basic_ofstream<_CharT, _Traits>::close() {1302 if (__sb_.close() == nullptr)1303 this->setstate(ios_base::failbit);1304}1305 1306// basic_fstream1307 1308template <class _CharT, class _Traits>1309class _LIBCPP_TEMPLATE_VIS basic_fstream : public basic_iostream<_CharT, _Traits> {1310public:1311 typedef _CharT char_type;1312 typedef _Traits traits_type;1313 typedef typename traits_type::int_type int_type;1314 typedef typename traits_type::pos_type pos_type;1315 typedef typename traits_type::off_type off_type;1316 1317 _LIBCPP_HIDE_FROM_ABI basic_fstream();1318 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const char* __s,1319 ios_base::openmode __mode = ios_base::in | ios_base::out);1320# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1321 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const wchar_t* __s,1322 ios_base::openmode __mode = ios_base::in | ios_base::out);1323# endif1324 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const string& __s,1325 ios_base::openmode __mode = ios_base::in | ios_base::out);1326 1327 _LIBCPP_HIDE_FROM_ABI basic_fstream(basic_fstream&& __rhs);1328 1329 _LIBCPP_HIDE_FROM_ABI basic_fstream& operator=(basic_fstream&& __rhs);1330 1331 _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream& __rhs);1332 1333 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;1334 _LIBCPP_HIDE_FROM_ABI bool is_open() const;1335 _LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);1336# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1337 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);1338# endif1339 _LIBCPP_HIDE_FROM_ABI void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);1340 1341 _LIBCPP_HIDE_FROM_ABI void close();1342 1343private:1344 basic_filebuf<char_type, traits_type> __sb_;1345};1346 1347template <class _CharT, class _Traits>1348inline basic_fstream<_CharT, _Traits>::basic_fstream()1349 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {}1350 1351template <class _CharT, class _Traits>1352inline basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)1353 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {1354 if (__sb_.open(__s, __mode) == nullptr)1355 this->setstate(ios_base::failbit);1356}1357 1358# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1359template <class _CharT, class _Traits>1360inline basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)1361 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {1362 if (__sb_.open(__s, __mode) == nullptr)1363 this->setstate(ios_base::failbit);1364}1365# endif1366 1367template <class _CharT, class _Traits>1368inline basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)1369 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {1370 if (__sb_.open(__s, __mode) == nullptr)1371 this->setstate(ios_base::failbit);1372}1373 1374// extension1375template <class _CharT, class _Traits>1376inline basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)1377 : basic_iostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {1378 this->set_rdbuf(std::addressof(__sb_));1379}1380 1381template <class _CharT, class _Traits>1382inline basic_fstream<_CharT, _Traits>& basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs) {1383 basic_iostream<char_type, traits_type>::operator=(std::move(__rhs));1384 __sb_ = std::move(__rhs.__sb_);1385 return *this;1386}1387 1388template <class _CharT, class _Traits>1389inline void basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs) {1390 basic_iostream<char_type, traits_type>::swap(__rhs);1391 __sb_.swap(__rhs.__sb_);1392}1393 1394template <class _CharT, class _Traits>1395inline _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) {1396 __x.swap(__y);1397}1398 1399template <class _CharT, class _Traits>1400inline basic_filebuf<_CharT, _Traits>* basic_fstream<_CharT, _Traits>::rdbuf() const {1401 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));1402}1403 1404template <class _CharT, class _Traits>1405inline bool basic_fstream<_CharT, _Traits>::is_open() const {1406 return __sb_.is_open();1407}1408 1409template <class _CharT, class _Traits>1410void basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {1411 if (__sb_.open(__s, __mode))1412 this->clear();1413 else1414 this->setstate(ios_base::failbit);1415}1416 1417# ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR1418template <class _CharT, class _Traits>1419void basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {1420 if (__sb_.open(__s, __mode))1421 this->clear();1422 else1423 this->setstate(ios_base::failbit);1424}1425# endif1426 1427template <class _CharT, class _Traits>1428void basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {1429 if (__sb_.open(__s, __mode))1430 this->clear();1431 else1432 this->setstate(ios_base::failbit);1433}1434 1435template <class _CharT, class _Traits>1436inline void basic_fstream<_CharT, _Traits>::close() {1437 if (__sb_.close() == nullptr)1438 this->setstate(ios_base::failbit);1439}1440 1441# if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_11442extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;1443extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;1444extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;1445# endif1446 1447_LIBCPP_END_NAMESPACE_STD1448 1449#endif // _LIBCPP_HAS_NO_FILESYSTEM1450 1451_LIBCPP_POP_MACROS1452 1453#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)1454# include <__cxx03/atomic>1455# include <__cxx03/cstdlib>1456# include <__cxx03/iosfwd>1457# include <__cxx03/limits>1458# include <__cxx03/mutex>1459# include <__cxx03/new>1460# include <__cxx03/stdexcept>1461# include <__cxx03/type_traits>1462#endif1463 1464#endif // _LIBCPP___CXX03_FSTREAM1465