2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_FSTREAM
11 #define _LIBCPP_FSTREAM
16 template <class charT, class traits = char_traits<charT> >
18 : public basic_streambuf<charT, traits>
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;
27 // 27.9.1.2 Constructors/destructor:
29 basic_filebuf(basic_filebuf&& rhs);
30 virtual ~basic_filebuf();
32 // 27.9.1.3 Assign/swap:
33 basic_filebuf& operator=(basic_filebuf&& rhs);
34 void swap(basic_filebuf& rhs);
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();
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);
56 virtual void imbue(const locale& loc);
59 template <class charT, class traits>
61 swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
63 typedef basic_filebuf<char> filebuf;
64 typedef basic_filebuf<wchar_t> wfilebuf;
66 template <class charT, class traits = char_traits<charT> >
68 : public basic_istream<charT,traits>
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++26
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);
82 explicit basic_ifstream(const T& s, ios_base::openmode mode = ios_base::in); // Since C++17
83 basic_ifstream(basic_ifstream&& rhs);
85 basic_ifstream& operator=(basic_ifstream&& rhs);
86 void swap(basic_ifstream& rhs);
88 basic_filebuf<char_type, traits_type>* rdbuf() const;
89 native_handle_type native_handle() const noexcept; // Since C++26
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++17
98 template <class charT, class traits>
100 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
102 typedef basic_ifstream<char> ifstream;
103 typedef basic_ifstream<wchar_t> wifstream;
105 template <class charT, class traits = char_traits<charT> >
107 : public basic_ostream<charT,traits>
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++26
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);
121 explicit basic_ofstream(const T& s, ios_base::openmode mode = ios_base::out); // Since C++17
122 basic_ofstream(basic_ofstream&& rhs);
124 basic_ofstream& operator=(basic_ofstream&& rhs);
125 void swap(basic_ofstream& rhs);
127 basic_filebuf<char_type, traits_type>* rdbuf() const;
128 native_handle_type native_handle() const noexcept; // Since C++26
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++17
139 template <class charT, class traits>
141 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
143 typedef basic_ofstream<char> ofstream;
144 typedef basic_ofstream<wchar_t> wofstream;
146 template <class charT, class traits=char_traits<charT> >
148 : public basic_iostream<charT,traits>
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++26
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);
162 explicit basic_fstream(const T& s, ios_base::openmode mode = ios_base::in | ios_base::out); // Since C++17
163 basic_fstream(basic_fstream&& rhs);
165 basic_fstream& operator=(basic_fstream&& rhs);
166 void swap(basic_fstream& rhs);
168 basic_filebuf<char_type, traits_type>* rdbuf() const;
169 native_handle_type native_handle() const noexcept; // Since C++26
170 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++17
179 template <class charT, class traits>
180 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
182 typedef basic_fstream<char> fstream;
183 typedef basic_fstream<wchar_t> wfstream;
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/__type_traits/enable_if.h>
195 #include <__cxx03/__type_traits/is_same.h>
196 #include <__cxx03/__utility/move.h>
197 #include <__cxx03/__utility/swap.h>
198 #include <__cxx03/__utility/unreachable.h>
199 #include <__cxx03/cstdio>
200 #include <__cxx03/filesystem>
201 #include <__cxx03/istream>
202 #include <__cxx03/ostream>
203 #include <__cxx03/typeinfo>
204 #include <__cxx03/version>
206 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
207 # pragma GCC system_header
211 #include <__cxx03/__undef_macros>
213 #if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
214 # define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
217 #if !defined(_LIBCPP_HAS_NO_FILESYSTEM)
219 _LIBCPP_BEGIN_NAMESPACE_STD
221 # if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_WIN32API)
222 _LIBCPP_EXPORTED_FROM_ABI void* __filebuf_windows_native_handle(FILE* __file) noexcept;
225 template <class _CharT, class _Traits>
226 class _LIBCPP_TEMPLATE_VIS basic_filebuf : public basic_streambuf<_CharT, _Traits> {
228 typedef _CharT char_type;
229 typedef _Traits traits_type;
230 typedef typename traits_type::int_type int_type;
231 typedef typename traits_type::pos_type pos_type;
232 typedef typename traits_type::off_type off_type;
233 typedef typename traits_type::state_type state_type;
234 # if _LIBCPP_STD_VER >= 26
235 # if defined(_LIBCPP_WIN32API)
236 using native_handle_type = void*; // HANDLE
237 # elif __has_include(<unistd.h>)
238 using native_handle_type = int; // POSIX file descriptor
240 # error "Provide a native file handle!"
244 // 27.9.1.2 Constructors/destructor:
246 basic_filebuf(basic_filebuf&& __rhs);
247 ~basic_filebuf() override;
249 // 27.9.1.3 Assign/swap:
250 _LIBCPP_HIDE_FROM_ABI basic_filebuf& operator=(basic_filebuf&& __rhs);
251 void swap(basic_filebuf& __rhs);
254 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
255 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
256 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
257 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
259 _LIBCPP_HIDE_FROM_ABI basic_filebuf* open(const string& __s, ios_base::openmode __mode);
261 # if _LIBCPP_STD_VER >= 17
262 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI basic_filebuf*
263 open(const filesystem::path& __p, ios_base::openmode __mode) {
264 return open(__p.c_str(), __mode);
267 _LIBCPP_HIDE_FROM_ABI basic_filebuf* __open(int __fd, ios_base::openmode __mode);
268 basic_filebuf* close();
269 # if _LIBCPP_STD_VER >= 26
270 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
271 _LIBCPP_ASSERT_UNCATEGORIZED(this->is_open(), "File must be opened");
272 # if defined(_LIBCPP_WIN32API)
273 return std::__filebuf_windows_native_handle(__file_);
274 # elif __has_include(<unistd.h>)
275 return fileno(__file_);
277 # error "Provide a way to determine the file native handle!"
280 # endif // _LIBCPP_STD_VER >= 26
282 _LIBCPP_HIDE_FROM_ABI inline static const char* __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
283 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
284 _LIBCPP_HIDE_FROM_ABI inline static const wchar_t* __make_mdwstring(ios_base::openmode __mode) _NOEXCEPT;
288 // 27.9.1.5 Overridden virtual functions:
289 int_type underflow() override;
290 int_type pbackfail(int_type __c = traits_type::eof()) override;
291 int_type overflow(int_type __c = traits_type::eof()) override;
292 basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override;
294 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
295 pos_type seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
297 void imbue(const locale& __loc) override;
301 const char* __extbufnext_;
302 const char* __extbufend_;
303 char __extbuf_min_[8];
305 char_type* __intbuf_;
308 const codecvt<char_type, char, state_type>* __cv_;
310 state_type __st_last_;
311 ios_base::openmode __om_;
312 // There have been no file operations yet, which allows setting unbuffered
314 static const ios_base::openmode __no_io_operations = ios_base::trunc;
315 // Unbuffered I/O mode has been requested.
316 static const ios_base::openmode __use_unbuffered_io = ios_base::ate;
317 // Used to track the currently used mode and track whether the output should
319 // [filebuf.virtuals]/12
320 // If setbuf(0, 0) is called on a stream before any I/O has occurred on
321 // that stream, the stream becomes unbuffered. Otherwise the results are
322 // implementation-defined.
323 // This allows calling setbuf(0, 0)
324 // - before opening a file,
325 // - after opening a file, before
329 // Note that opening a file with ios_base::ate does a seek operation.
330 // Normally underflow, overflow, and sync change this flag to ios_base::in,
331 // ios_base_out, or 0.
333 // The ios_base::trunc and ios_base::ate flags are not used in __cm_. They
334 // are used to track the state of the unbuffered request. For readability
335 // they have the aliases __no_io_operations and __use_unbuffered_io
338 // The __no_io_operations and __use_unbuffered_io flags are used in the
340 // - __no_io_operations is set upon construction to indicate the unbuffered
342 // - When requesting unbuffered output:
343 // - If the file is open it sets the mode.
344 // - Else places a request by adding the __use_unbuffered_io flag.
345 // - When a file is opened it checks whether both __no_io_operations and
346 // __use_unbuffered_io are set. If so switches to unbuffered mode.
347 // - All file I/O operations change the mode effectively clearing the
348 // __no_io_operations and __use_unbuffered_io flags.
349 ios_base::openmode __cm_;
352 bool __always_noconv_;
357 _LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);
359 // There are multiple (__)open function, they use different C-API open
360 // function. After that call these functions behave the same. This function
361 // does that part and determines the final return value.
362 _LIBCPP_HIDE_FROM_ABI basic_filebuf* __do_open(FILE* __file, ios_base::openmode __mode) {
368 if (__cm_ == (__no_io_operations | __use_unbuffered_io)) {
369 std::setbuf(__file_, nullptr);
373 if (__mode & ios_base::ate) {
375 if (fseek(__file_, 0, SEEK_END)) {
385 // If the file is already open, switch to unbuffered mode. Otherwise, record
386 // the request to use unbuffered mode so that we use that mode when we
387 // eventually open the file.
388 _LIBCPP_HIDE_FROM_ABI void __request_unbuffered_mode(char_type* __s, streamsize __n) {
389 if (__cm_ == __no_io_operations && __s == nullptr && __n == 0) {
391 std::setbuf(__file_, nullptr);
394 __cm_ = __no_io_operations | __use_unbuffered_io;
400 template <class _CharT, class _Traits>
401 basic_filebuf<_CharT, _Traits>::basic_filebuf()
402 : __extbuf_(nullptr),
403 __extbufnext_(nullptr),
404 __extbufend_(nullptr),
413 __cm_(__no_io_operations),
416 __always_noconv_(false) {
417 if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc())) {
418 __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(this->getloc());
419 __always_noconv_ = __cv_->always_noconv();
421 setbuf(nullptr, 4096);
424 template <class _CharT, class _Traits>
425 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) : basic_streambuf<_CharT, _Traits>(__rhs) {
426 if (__rhs.__extbuf_ == __rhs.__extbuf_min_) {
427 __extbuf_ = __extbuf_min_;
428 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
429 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
431 __extbuf_ = __rhs.__extbuf_;
432 __extbufnext_ = __rhs.__extbufnext_;
433 __extbufend_ = __rhs.__extbufend_;
435 __ebs_ = __rhs.__ebs_;
436 __intbuf_ = __rhs.__intbuf_;
437 __ibs_ = __rhs.__ibs_;
438 __file_ = __rhs.__file_;
441 __st_last_ = __rhs.__st_last_;
444 __owns_eb_ = __rhs.__owns_eb_;
445 __owns_ib_ = __rhs.__owns_ib_;
446 __always_noconv_ = __rhs.__always_noconv_;
448 if (__rhs.pbase() == __rhs.__intbuf_)
449 this->setp(__intbuf_, __intbuf_ + (__rhs.epptr() - __rhs.pbase()));
451 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__rhs.epptr() - __rhs.pbase()));
452 this->__pbump(__rhs.pptr() - __rhs.pbase());
453 } else if (__rhs.eback()) {
454 if (__rhs.eback() == __rhs.__intbuf_)
455 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), __intbuf_ + (__rhs.egptr() - __rhs.eback()));
457 this->setg((char_type*)__extbuf_,
458 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
459 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
461 __rhs.__extbuf_ = nullptr;
462 __rhs.__extbufnext_ = nullptr;
463 __rhs.__extbufend_ = nullptr;
467 __rhs.__file_ = nullptr;
468 __rhs.__st_ = state_type();
469 __rhs.__st_last_ = state_type();
472 __rhs.__owns_eb_ = false;
473 __rhs.__owns_ib_ = false;
478 template <class _CharT, class _Traits>
479 inline basic_filebuf<_CharT, _Traits>& basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) {
485 template <class _CharT, class _Traits>
486 basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
487 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
489 # endif // _LIBCPP_HAS_NO_EXCEPTIONS
491 # ifndef _LIBCPP_HAS_NO_EXCEPTIONS
494 # endif // _LIBCPP_HAS_NO_EXCEPTIONS
501 template <class _CharT, class _Traits>
502 void basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) {
503 basic_streambuf<char_type, traits_type>::swap(__rhs);
504 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
505 // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
506 std::swap(__extbuf_, __rhs.__extbuf_);
507 std::swap(__extbufnext_, __rhs.__extbufnext_);
508 std::swap(__extbufend_, __rhs.__extbufend_);
510 ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
511 ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
512 ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
513 ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
514 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
515 // *this uses the small buffer, but __rhs doesn't.
516 __extbuf_ = __rhs.__extbuf_;
517 __rhs.__extbuf_ = __rhs.__extbuf_min_;
518 std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));
519 } else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) {
520 // *this doesn't use the small buffer, but __rhs does.
521 __rhs.__extbuf_ = __extbuf_;
522 __extbuf_ = __extbuf_min_;
523 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
525 // Both *this and __rhs use the small buffer.
526 char __tmp[sizeof(__extbuf_min_)];
527 std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));
528 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
529 std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));
531 __extbufnext_ = __extbuf_ + __rn;
532 __extbufend_ = __extbuf_ + __re;
533 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
534 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
536 std::swap(__ebs_, __rhs.__ebs_);
537 std::swap(__intbuf_, __rhs.__intbuf_);
538 std::swap(__ibs_, __rhs.__ibs_);
539 std::swap(__file_, __rhs.__file_);
540 std::swap(__cv_, __rhs.__cv_);
541 std::swap(__st_, __rhs.__st_);
542 std::swap(__st_last_, __rhs.__st_last_);
543 std::swap(__om_, __rhs.__om_);
544 std::swap(__cm_, __rhs.__cm_);
545 std::swap(__owns_eb_, __rhs.__owns_eb_);
546 std::swap(__owns_ib_, __rhs.__owns_ib_);
547 std::swap(__always_noconv_, __rhs.__always_noconv_);
548 if (this->eback() == (char_type*)__rhs.__extbuf_min_) {
549 ptrdiff_t __n = this->gptr() - this->eback();
550 ptrdiff_t __e = this->egptr() - this->eback();
551 this->setg((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __n, (char_type*)__extbuf_min_ + __e);
552 } else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) {
553 ptrdiff_t __n = this->pptr() - this->pbase();
554 ptrdiff_t __e = this->epptr() - this->pbase();
555 this->setp((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __e);
558 if (__rhs.eback() == (char_type*)__extbuf_min_) {
559 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
560 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
562 (char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __n, (char_type*)__rhs.__extbuf_min_ + __e);
563 } else if (__rhs.pbase() == (char_type*)__extbuf_min_) {
564 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
565 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
566 __rhs.setp((char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __e);
571 template <class _CharT, class _Traits>
572 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) {
576 template <class _CharT, class _Traits>
577 inline bool basic_filebuf<_CharT, _Traits>::is_open() const {
578 return __file_ != nullptr;
581 template <class _CharT, class _Traits>
582 const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(ios_base::openmode __mode) _NOEXCEPT {
583 switch (__mode & ~ios_base::ate) {
585 case ios_base::out | ios_base::trunc:
586 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
587 case ios_base::out | ios_base::app:
589 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
591 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
592 case ios_base::in | ios_base::out:
593 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
594 case ios_base::in | ios_base::out | ios_base::trunc:
595 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
596 case ios_base::in | ios_base::out | ios_base::app:
597 case ios_base::in | ios_base::app:
598 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
599 case ios_base::out | ios_base::binary:
600 case ios_base::out | ios_base::trunc | ios_base::binary:
601 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
602 case ios_base::out | ios_base::app | ios_base::binary:
603 case ios_base::app | ios_base::binary:
604 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
605 case ios_base::in | ios_base::binary:
606 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
607 case ios_base::in | ios_base::out | ios_base::binary:
608 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
609 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
610 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
611 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
612 case ios_base::in | ios_base::app | ios_base::binary:
613 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
614 # if _LIBCPP_STD_VER >= 23
615 case ios_base::out | ios_base::noreplace:
616 case ios_base::out | ios_base::trunc | ios_base::noreplace:
617 return "wx" _LIBCPP_FOPEN_CLOEXEC_MODE;
618 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
619 return "w+x" _LIBCPP_FOPEN_CLOEXEC_MODE;
620 case ios_base::out | ios_base::binary | ios_base::noreplace:
621 case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
622 return "wbx" _LIBCPP_FOPEN_CLOEXEC_MODE;
623 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
624 return "w+bx" _LIBCPP_FOPEN_CLOEXEC_MODE;
625 # endif // _LIBCPP_STD_VER >= 23
629 __libcpp_unreachable();
632 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
633 template <class _CharT, class _Traits>
634 const wchar_t* basic_filebuf<_CharT, _Traits>::__make_mdwstring(ios_base::openmode __mode) _NOEXCEPT {
635 switch (__mode & ~ios_base::ate) {
637 case ios_base::out | ios_base::trunc:
639 case ios_base::out | ios_base::app:
644 case ios_base::in | ios_base::out:
646 case ios_base::in | ios_base::out | ios_base::trunc:
648 case ios_base::in | ios_base::out | ios_base::app:
649 case ios_base::in | ios_base::app:
651 case ios_base::out | ios_base::binary:
652 case ios_base::out | ios_base::trunc | ios_base::binary:
654 case ios_base::out | ios_base::app | ios_base::binary:
655 case ios_base::app | ios_base::binary:
657 case ios_base::in | ios_base::binary:
659 case ios_base::in | ios_base::out | ios_base::binary:
661 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
663 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
664 case ios_base::in | ios_base::app | ios_base::binary:
666 # if _LIBCPP_STD_VER >= 23
667 case ios_base::out | ios_base::noreplace:
668 case ios_base::out | ios_base::trunc | ios_base::noreplace:
670 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
672 case ios_base::out | ios_base::binary | ios_base::noreplace:
673 case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
675 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
677 # endif // _LIBCPP_STD_VER >= 23
681 __libcpp_unreachable();
685 template <class _CharT, class _Traits>
686 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
689 const char* __mdstr = __make_mdstring(__mode);
693 return __do_open(fopen(__s, __mdstr), __mode);
696 template <class _CharT, class _Traits>
697 inline basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
700 const char* __mdstr = __make_mdstring(__mode);
704 return __do_open(fdopen(__fd, __mdstr), __mode);
707 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
708 // This is basically the same as the char* overload except that it uses _wfopen
709 // and long mode strings.
710 template <class _CharT, class _Traits>
711 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
714 const wchar_t* __mdstr = __make_mdwstring(__mode);
718 return __do_open(_wfopen(__s, __mdstr), __mode);
722 template <class _CharT, class _Traits>
723 inline basic_filebuf<_CharT, _Traits>*
724 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
725 return open(__s.c_str(), __mode);
728 template <class _CharT, class _Traits>
729 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::close() {
730 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
733 unique_ptr<FILE, int (*)(FILE*)> __h(__file_, fclose);
736 if (fclose(__h.release()))
744 template <class _CharT, class _Traits>
745 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::underflow() {
746 if (__file_ == nullptr)
747 return traits_type::eof();
748 bool __initial = __read_mode();
750 if (this->gptr() == nullptr)
751 this->setg(&__1buf, &__1buf + 1, &__1buf + 1);
752 const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
753 int_type __c = traits_type::eof();
754 if (this->gptr() == this->egptr()) {
755 std::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
756 if (__always_noconv_) {
757 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
758 __nmemb = ::fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
760 this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);
761 __c = traits_type::to_int_type(*this->gptr());
764 if (__extbufend_ != __extbufnext_) {
765 _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");
766 _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");
767 std::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
769 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
770 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
772 std::min(static_cast<size_t>(__ibs_ - __unget_sz), static_cast<size_t>(__extbufend_ - __extbufnext_));
773 codecvt_base::result __r;
775 size_t __nr = fread((void*)const_cast<char*>(__extbufnext_), 1, __nmemb, __file_);
780 __extbufend_ = __extbufnext_ + __nr;
783 __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->eback() + __ibs_, __inext);
784 if (__r == codecvt_base::noconv) {
785 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));
786 __c = traits_type::to_int_type(*this->gptr());
787 } else if (__inext != this->eback() + __unget_sz) {
788 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
789 __c = traits_type::to_int_type(*this->gptr());
794 __c = traits_type::to_int_type(*this->gptr());
795 if (this->eback() == &__1buf)
796 this->setg(nullptr, nullptr, nullptr);
800 template <class _CharT, class _Traits>
801 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
802 if (__file_ && this->eback() < this->gptr()) {
803 if (traits_type::eq_int_type(__c, traits_type::eof())) {
805 return traits_type::not_eof(__c);
807 if ((__om_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {
809 *this->gptr() = traits_type::to_char_type(__c);
813 return traits_type::eof();
816 template <class _CharT, class _Traits>
817 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
818 if (__file_ == nullptr)
819 return traits_type::eof();
822 char_type* __pb_save = this->pbase();
823 char_type* __epb_save = this->epptr();
824 if (!traits_type::eq_int_type(__c, traits_type::eof())) {
825 if (this->pptr() == nullptr)
826 this->setp(&__1buf, &__1buf + 1);
827 *this->pptr() = traits_type::to_char_type(__c);
830 if (this->pptr() != this->pbase()) {
831 if (__always_noconv_) {
832 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
833 if (std::fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
834 return traits_type::eof();
836 char* __extbe = __extbuf_;
837 codecvt_base::result __r;
842 const char_type* __e;
843 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
844 if (__e == this->pbase())
845 return traits_type::eof();
846 if (__r == codecvt_base::noconv) {
847 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
848 if (std::fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
849 return traits_type::eof();
850 } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {
851 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
852 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
853 return traits_type::eof();
854 if (__r == codecvt_base::partial) {
855 this->setp(const_cast<char_type*>(__e), this->pptr());
856 this->__pbump(this->epptr() - this->pbase());
859 return traits_type::eof();
860 } while (__r == codecvt_base::partial);
862 this->setp(__pb_save, __epb_save);
864 return traits_type::not_eof(__c);
867 template <class _CharT, class _Traits>
868 basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {
869 this->setg(nullptr, nullptr, nullptr);
870 this->setp(nullptr, nullptr);
871 __request_unbuffered_mode(__s, __n);
877 if (__ebs_ > sizeof(__extbuf_min_)) {
878 if (__always_noconv_ && __s) {
879 __extbuf_ = (char*)__s;
882 __extbuf_ = new char[__ebs_];
886 __extbuf_ = __extbuf_min_;
887 __ebs_ = sizeof(__extbuf_min_);
890 if (!__always_noconv_) {
891 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
892 if (__s && __ibs_ > sizeof(__extbuf_min_)) {
896 __intbuf_ = new char_type[__ibs_];
907 template <class _CharT, class _Traits>
908 typename basic_filebuf<_CharT, _Traits>::pos_type
909 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) {
913 int __width = __cv_->encoding();
914 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
915 return pos_type(off_type(-1));
916 // __width > 0 || __off == 0
929 return pos_type(off_type(-1));
931 # if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
932 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
933 return pos_type(off_type(-1));
934 pos_type __r = ftell(__file_);
936 if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
937 return pos_type(off_type(-1));
938 pos_type __r = ftello(__file_);
944 template <class _CharT, class _Traits>
945 typename basic_filebuf<_CharT, _Traits>::pos_type
946 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {
947 if (__file_ == nullptr || sync())
948 return pos_type(off_type(-1));
949 # if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
950 if (fseek(__file_, __sp, SEEK_SET))
951 return pos_type(off_type(-1));
953 if (::fseeko(__file_, __sp, SEEK_SET))
954 return pos_type(off_type(-1));
956 __st_ = __sp.state();
960 template <class _CharT, class _Traits>
961 int basic_filebuf<_CharT, _Traits>::sync() {
962 if (__file_ == nullptr)
967 if (__cm_ & ios_base::out) {
968 if (this->pptr() != this->pbase())
969 if (overflow() == traits_type::eof())
971 codecvt_base::result __r;
974 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
975 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
976 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
978 } while (__r == codecvt_base::partial);
979 if (__r == codecvt_base::error)
983 } else if (__cm_ & ios_base::in) {
985 state_type __state = __st_last_;
986 bool __update_st = false;
987 if (__always_noconv_)
988 __c = this->egptr() - this->gptr();
990 int __width = __cv_->encoding();
991 __c = __extbufend_ - __extbufnext_;
993 __c += __width * (this->egptr() - this->gptr());
995 if (this->gptr() != this->egptr()) {
996 const int __off = __cv_->length(__state, __extbuf_, __extbufnext_, this->gptr() - this->eback());
997 __c += __extbufnext_ - __extbuf_ - __off;
1002 # if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
1003 if (fseek(__file_, -__c, SEEK_CUR))
1006 if (::fseeko(__file_, -__c, SEEK_CUR))
1011 __extbufnext_ = __extbufend_ = __extbuf_;
1012 this->setg(nullptr, nullptr, nullptr);
1018 template <class _CharT, class _Traits>
1019 void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
1021 __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(__loc);
1022 bool __old_anc = __always_noconv_;
1023 __always_noconv_ = __cv_->always_noconv();
1024 if (__old_anc != __always_noconv_) {
1025 this->setg(nullptr, nullptr, nullptr);
1026 this->setp(nullptr, nullptr);
1027 // invariant, char_type is char, else we couldn't get here
1028 if (__always_noconv_) // need to dump __intbuf_
1032 __owns_eb_ = __owns_ib_;
1034 __extbuf_ = (char*)__intbuf_;
1036 __intbuf_ = nullptr;
1038 } else // need to obtain an __intbuf_.
1039 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1040 if (!__owns_eb_ && __extbuf_ != __extbuf_min_) {
1042 __intbuf_ = (char_type*)__extbuf_;
1044 __extbuf_ = new char[__ebs_];
1048 __intbuf_ = new char_type[__ibs_];
1055 template <class _CharT, class _Traits>
1056 bool basic_filebuf<_CharT, _Traits>::__read_mode() {
1057 if (!(__cm_ & ios_base::in)) {
1058 this->setp(nullptr, nullptr);
1059 if (__always_noconv_)
1060 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_ + __ebs_, (char_type*)__extbuf_ + __ebs_);
1062 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1063 __cm_ = ios_base::in;
1069 template <class _CharT, class _Traits>
1070 void basic_filebuf<_CharT, _Traits>::__write_mode() {
1071 if (!(__cm_ & ios_base::out)) {
1072 this->setg(nullptr, nullptr, nullptr);
1073 if (__ebs_ > sizeof(__extbuf_min_)) {
1074 if (__always_noconv_)
1075 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));
1077 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1079 this->setp(nullptr, nullptr);
1080 __cm_ = ios_base::out;
1086 template <class _CharT, class _Traits>
1087 class _LIBCPP_TEMPLATE_VIS basic_ifstream : public basic_istream<_CharT, _Traits> {
1089 typedef _CharT char_type;
1090 typedef _Traits traits_type;
1091 typedef typename traits_type::int_type int_type;
1092 typedef typename traits_type::pos_type pos_type;
1093 typedef typename traits_type::off_type off_type;
1094 # if _LIBCPP_STD_VER >= 26
1095 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1098 _LIBCPP_HIDE_FROM_ABI basic_ifstream();
1099 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1100 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1101 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1103 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1104 # if _LIBCPP_STD_VER >= 17
1105 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1106 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
1107 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const _Tp& __p, ios_base::openmode __mode = ios_base::in)
1108 : basic_ifstream(__p.c_str(), __mode) {}
1109 # endif // _LIBCPP_STD_VER >= 17
1110 _LIBCPP_HIDE_FROM_ABI basic_ifstream(basic_ifstream&& __rhs);
1111 _LIBCPP_HIDE_FROM_ABI basic_ifstream& operator=(basic_ifstream&& __rhs);
1112 _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream& __rhs);
1114 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1115 # if _LIBCPP_STD_VER >= 26
1116 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1118 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1119 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1120 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1121 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1123 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1124 # if _LIBCPP_STD_VER >= 17
1125 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1126 open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) {
1127 return open(__p.c_str(), __mode);
1129 # endif // _LIBCPP_STD_VER >= 17
1131 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1132 _LIBCPP_HIDE_FROM_ABI void close();
1135 basic_filebuf<char_type, traits_type> __sb_;
1138 template <class _CharT, class _Traits>
1139 inline basic_ifstream<_CharT, _Traits>::basic_ifstream() : basic_istream<char_type, traits_type>(&__sb_) {}
1141 template <class _CharT, class _Traits>
1142 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1143 : basic_istream<char_type, traits_type>(&__sb_) {
1144 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1145 this->setstate(ios_base::failbit);
1148 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1149 template <class _CharT, class _Traits>
1150 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1151 : basic_istream<char_type, traits_type>(&__sb_) {
1152 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1153 this->setstate(ios_base::failbit);
1157 template <class _CharT, class _Traits>
1158 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1159 : basic_istream<char_type, traits_type>(&__sb_) {
1160 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1161 this->setstate(ios_base::failbit);
1164 template <class _CharT, class _Traits>
1165 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1166 : basic_istream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1167 this->set_rdbuf(&__sb_);
1170 template <class _CharT, class _Traits>
1171 inline basic_ifstream<_CharT, _Traits>& basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) {
1172 basic_istream<char_type, traits_type>::operator=(std::move(__rhs));
1173 __sb_ = std::move(__rhs.__sb_);
1177 template <class _CharT, class _Traits>
1178 inline void basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) {
1179 basic_istream<char_type, traits_type>::swap(__rhs);
1180 __sb_.swap(__rhs.__sb_);
1183 template <class _CharT, class _Traits>
1184 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) {
1188 template <class _CharT, class _Traits>
1189 inline basic_filebuf<_CharT, _Traits>* basic_ifstream<_CharT, _Traits>::rdbuf() const {
1190 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1193 template <class _CharT, class _Traits>
1194 inline bool basic_ifstream<_CharT, _Traits>::is_open() const {
1195 return __sb_.is_open();
1198 template <class _CharT, class _Traits>
1199 void basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1200 if (__sb_.open(__s, __mode | ios_base::in))
1203 this->setstate(ios_base::failbit);
1206 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1207 template <class _CharT, class _Traits>
1208 void basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1209 if (__sb_.open(__s, __mode | ios_base::in))
1212 this->setstate(ios_base::failbit);
1216 template <class _CharT, class _Traits>
1217 void basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1218 if (__sb_.open(__s, __mode | ios_base::in))
1221 this->setstate(ios_base::failbit);
1224 template <class _CharT, class _Traits>
1225 inline void basic_ifstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1226 if (__sb_.__open(__fd, __mode | ios_base::in))
1229 this->setstate(ios_base::failbit);
1232 template <class _CharT, class _Traits>
1233 inline void basic_ifstream<_CharT, _Traits>::close() {
1234 if (__sb_.close() == 0)
1235 this->setstate(ios_base::failbit);
1240 template <class _CharT, class _Traits>
1241 class _LIBCPP_TEMPLATE_VIS basic_ofstream : public basic_ostream<_CharT, _Traits> {
1243 typedef _CharT char_type;
1244 typedef _Traits traits_type;
1245 typedef typename traits_type::int_type int_type;
1246 typedef typename traits_type::pos_type pos_type;
1247 typedef typename traits_type::off_type off_type;
1248 # if _LIBCPP_STD_VER >= 26
1249 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1252 _LIBCPP_HIDE_FROM_ABI basic_ofstream();
1253 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1254 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1255 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1257 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1259 # if _LIBCPP_STD_VER >= 17
1260 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1261 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
1262 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const _Tp& __p, ios_base::openmode __mode = ios_base::out)
1263 : basic_ofstream(__p.c_str(), __mode) {}
1264 # endif // _LIBCPP_STD_VER >= 17
1266 _LIBCPP_HIDE_FROM_ABI basic_ofstream(basic_ofstream&& __rhs);
1267 _LIBCPP_HIDE_FROM_ABI basic_ofstream& operator=(basic_ofstream&& __rhs);
1268 _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream& __rhs);
1270 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1271 # if _LIBCPP_STD_VER >= 26
1272 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1274 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1275 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1276 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1277 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1279 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1281 # if _LIBCPP_STD_VER >= 17
1282 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1283 open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) {
1284 return open(__p.c_str(), __mode);
1286 # endif // _LIBCPP_STD_VER >= 17
1288 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1289 _LIBCPP_HIDE_FROM_ABI void close();
1292 basic_filebuf<char_type, traits_type> __sb_;
1295 template <class _CharT, class _Traits>
1296 inline basic_ofstream<_CharT, _Traits>::basic_ofstream() : basic_ostream<char_type, traits_type>(&__sb_) {}
1298 template <class _CharT, class _Traits>
1299 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1300 : basic_ostream<char_type, traits_type>(&__sb_) {
1301 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1302 this->setstate(ios_base::failbit);
1305 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1306 template <class _CharT, class _Traits>
1307 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1308 : basic_ostream<char_type, traits_type>(&__sb_) {
1309 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1310 this->setstate(ios_base::failbit);
1314 template <class _CharT, class _Traits>
1315 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1316 : basic_ostream<char_type, traits_type>(&__sb_) {
1317 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1318 this->setstate(ios_base::failbit);
1321 template <class _CharT, class _Traits>
1322 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1323 : basic_ostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1324 this->set_rdbuf(&__sb_);
1327 template <class _CharT, class _Traits>
1328 inline basic_ofstream<_CharT, _Traits>& basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) {
1329 basic_ostream<char_type, traits_type>::operator=(std::move(__rhs));
1330 __sb_ = std::move(__rhs.__sb_);
1334 template <class _CharT, class _Traits>
1335 inline void basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) {
1336 basic_ostream<char_type, traits_type>::swap(__rhs);
1337 __sb_.swap(__rhs.__sb_);
1340 template <class _CharT, class _Traits>
1341 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) {
1345 template <class _CharT, class _Traits>
1346 inline basic_filebuf<_CharT, _Traits>* basic_ofstream<_CharT, _Traits>::rdbuf() const {
1347 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1350 template <class _CharT, class _Traits>
1351 inline bool basic_ofstream<_CharT, _Traits>::is_open() const {
1352 return __sb_.is_open();
1355 template <class _CharT, class _Traits>
1356 void basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1357 if (__sb_.open(__s, __mode | ios_base::out))
1360 this->setstate(ios_base::failbit);
1363 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1364 template <class _CharT, class _Traits>
1365 void basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1366 if (__sb_.open(__s, __mode | ios_base::out))
1369 this->setstate(ios_base::failbit);
1373 template <class _CharT, class _Traits>
1374 void basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1375 if (__sb_.open(__s, __mode | ios_base::out))
1378 this->setstate(ios_base::failbit);
1381 template <class _CharT, class _Traits>
1382 inline void basic_ofstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1383 if (__sb_.__open(__fd, __mode | ios_base::out))
1386 this->setstate(ios_base::failbit);
1389 template <class _CharT, class _Traits>
1390 inline void basic_ofstream<_CharT, _Traits>::close() {
1391 if (__sb_.close() == nullptr)
1392 this->setstate(ios_base::failbit);
1397 template <class _CharT, class _Traits>
1398 class _LIBCPP_TEMPLATE_VIS basic_fstream : public basic_iostream<_CharT, _Traits> {
1400 typedef _CharT char_type;
1401 typedef _Traits traits_type;
1402 typedef typename traits_type::int_type int_type;
1403 typedef typename traits_type::pos_type pos_type;
1404 typedef typename traits_type::off_type off_type;
1405 # if _LIBCPP_STD_VER >= 26
1406 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1409 _LIBCPP_HIDE_FROM_ABI basic_fstream();
1410 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const char* __s,
1411 ios_base::openmode __mode = ios_base::in | ios_base::out);
1412 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1413 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const wchar_t* __s,
1414 ios_base::openmode __mode = ios_base::in | ios_base::out);
1416 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const string& __s,
1417 ios_base::openmode __mode = ios_base::in | ios_base::out);
1419 # if _LIBCPP_STD_VER >= 17
1420 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1421 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(
1422 const _Tp& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1423 : basic_fstream(__p.c_str(), __mode) {}
1424 # endif // _LIBCPP_STD_VER >= 17
1426 _LIBCPP_HIDE_FROM_ABI basic_fstream(basic_fstream&& __rhs);
1428 _LIBCPP_HIDE_FROM_ABI basic_fstream& operator=(basic_fstream&& __rhs);
1430 _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream& __rhs);
1432 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1433 # if _LIBCPP_STD_VER >= 26
1434 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1436 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1437 _LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1438 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1439 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1441 _LIBCPP_HIDE_FROM_ABI void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1443 # if _LIBCPP_STD_VER >= 17
1444 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1445 open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1446 return open(__p.c_str(), __mode);
1448 # endif // _LIBCPP_STD_VER >= 17
1450 _LIBCPP_HIDE_FROM_ABI void close();
1453 basic_filebuf<char_type, traits_type> __sb_;
1456 template <class _CharT, class _Traits>
1457 inline basic_fstream<_CharT, _Traits>::basic_fstream() : basic_iostream<char_type, traits_type>(&__sb_) {}
1459 template <class _CharT, class _Traits>
1460 inline basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1461 : basic_iostream<char_type, traits_type>(&__sb_) {
1462 if (__sb_.open(__s, __mode) == nullptr)
1463 this->setstate(ios_base::failbit);
1466 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1467 template <class _CharT, class _Traits>
1468 inline basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1469 : basic_iostream<char_type, traits_type>(&__sb_) {
1470 if (__sb_.open(__s, __mode) == nullptr)
1471 this->setstate(ios_base::failbit);
1475 template <class _CharT, class _Traits>
1476 inline basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1477 : basic_iostream<char_type, traits_type>(&__sb_) {
1478 if (__sb_.open(__s, __mode) == nullptr)
1479 this->setstate(ios_base::failbit);
1482 template <class _CharT, class _Traits>
1483 inline basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1484 : basic_iostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1485 this->set_rdbuf(&__sb_);
1488 template <class _CharT, class _Traits>
1489 inline basic_fstream<_CharT, _Traits>& basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs) {
1490 basic_iostream<char_type, traits_type>::operator=(std::move(__rhs));
1491 __sb_ = std::move(__rhs.__sb_);
1495 template <class _CharT, class _Traits>
1496 inline void basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs) {
1497 basic_iostream<char_type, traits_type>::swap(__rhs);
1498 __sb_.swap(__rhs.__sb_);
1501 template <class _CharT, class _Traits>
1502 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) {
1506 template <class _CharT, class _Traits>
1507 inline basic_filebuf<_CharT, _Traits>* basic_fstream<_CharT, _Traits>::rdbuf() const {
1508 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1511 template <class _CharT, class _Traits>
1512 inline bool basic_fstream<_CharT, _Traits>::is_open() const {
1513 return __sb_.is_open();
1516 template <class _CharT, class _Traits>
1517 void basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1518 if (__sb_.open(__s, __mode))
1521 this->setstate(ios_base::failbit);
1524 # ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1525 template <class _CharT, class _Traits>
1526 void basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1527 if (__sb_.open(__s, __mode))
1530 this->setstate(ios_base::failbit);
1534 template <class _CharT, class _Traits>
1535 void basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1536 if (__sb_.open(__s, __mode))
1539 this->setstate(ios_base::failbit);
1542 template <class _CharT, class _Traits>
1543 inline void basic_fstream<_CharT, _Traits>::close() {
1544 if (__sb_.close() == nullptr)
1545 this->setstate(ios_base::failbit);
1548 # if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1
1549 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1550 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1551 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
1554 _LIBCPP_END_NAMESPACE_STD
1556 #endif // _LIBCPP_HAS_NO_FILESYSTEM
1560 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1561 # include <__cxx03/atomic>
1562 # include <__cxx03/concepts>
1563 # include <__cxx03/cstdlib>
1564 # include <__cxx03/iosfwd>
1565 # include <__cxx03/limits>
1566 # include <__cxx03/mutex>
1567 # include <__cxx03/new>
1568 # include <__cxx03/stdexcept>
1569 # include <__cxx03/type_traits>
1572 #endif // _LIBCPP_FSTREAM