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 <__algorithm/max.h>
192 #include <__filesystem/path.h>
193 #include <__fwd/fstream.h>
195 #include <__memory/addressof.h>
196 #include <__memory/unique_ptr.h>
197 #include <__ostream/basic_ostream.h>
198 #include <__type_traits/enable_if.h>
199 #include <__type_traits/is_same.h>
200 #include <__utility/move.h>
201 #include <__utility/swap.h>
202 #include <__utility/unreachable.h>
209 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
210 # pragma GCC system_header
214 #include <__undef_macros>
216 #if !defined(_LIBCPP_MSVCRT) && !defined(_NEWLIB_VERSION)
217 # define _LIBCPP_HAS_OFF_T_FUNCTIONS 1
219 # define _LIBCPP_HAS_OFF_T_FUNCTIONS 0
222 #if _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
224 _LIBCPP_BEGIN_NAMESPACE_STD
226 # if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_WIN32API)
227 _LIBCPP_EXPORTED_FROM_ABI void* __filebuf_windows_native_handle(FILE* __file) noexcept;
230 template <class _CharT, class _Traits>
231 class _LIBCPP_TEMPLATE_VIS basic_filebuf : public basic_streambuf<_CharT, _Traits> {
233 typedef _CharT char_type;
234 typedef _Traits traits_type;
235 typedef typename traits_type::int_type int_type;
236 typedef typename traits_type::pos_type pos_type;
237 typedef typename traits_type::off_type off_type;
238 typedef typename traits_type::state_type state_type;
239 # if _LIBCPP_STD_VER >= 26
240 # if defined(_LIBCPP_WIN32API)
241 using native_handle_type = void*; // HANDLE
242 # elif __has_include(<unistd.h>)
243 using native_handle_type = int; // POSIX file descriptor
245 # error "Provide a native file handle!"
249 // 27.9.1.2 Constructors/destructor:
251 basic_filebuf(basic_filebuf&& __rhs);
252 ~basic_filebuf() override;
254 // 27.9.1.3 Assign/swap:
255 _LIBCPP_HIDE_FROM_ABI basic_filebuf& operator=(basic_filebuf&& __rhs);
256 void swap(basic_filebuf& __rhs);
259 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
260 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
261 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
262 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
264 _LIBCPP_HIDE_FROM_ABI basic_filebuf* open(const string& __s, ios_base::openmode __mode);
266 # if _LIBCPP_STD_VER >= 17
267 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI basic_filebuf*
268 open(const filesystem::path& __p, ios_base::openmode __mode) {
269 return open(__p.c_str(), __mode);
272 _LIBCPP_HIDE_FROM_ABI basic_filebuf* __open(int __fd, ios_base::openmode __mode);
273 basic_filebuf* close();
274 # if _LIBCPP_STD_VER >= 26
275 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
276 _LIBCPP_ASSERT_UNCATEGORIZED(this->is_open(), "File must be opened");
277 # if defined(_LIBCPP_WIN32API)
278 return std::__filebuf_windows_native_handle(__file_);
279 # elif __has_include(<unistd.h>)
280 return fileno(__file_);
282 # error "Provide a way to determine the file native handle!"
285 # endif // _LIBCPP_STD_VER >= 26
287 _LIBCPP_HIDE_FROM_ABI inline static const char* __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
288 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
289 _LIBCPP_HIDE_FROM_ABI inline static const wchar_t* __make_mdwstring(ios_base::openmode __mode) _NOEXCEPT;
293 // 27.9.1.5 Overridden virtual functions:
294 int_type underflow() override;
295 int_type pbackfail(int_type __c = traits_type::eof()) override;
296 int_type overflow(int_type __c = traits_type::eof()) override;
297 basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override;
299 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
300 pos_type seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
302 void imbue(const locale& __loc) override;
306 const char* __extbufnext_;
307 const char* __extbufend_;
308 char __extbuf_min_[8];
310 char_type* __intbuf_;
313 const codecvt<char_type, char, state_type>* __cv_;
315 state_type __st_last_;
316 ios_base::openmode __om_;
317 // There have been no file operations yet, which allows setting unbuffered
319 static const ios_base::openmode __no_io_operations = ios_base::trunc;
320 // Unbuffered I/O mode has been requested.
321 static const ios_base::openmode __use_unbuffered_io = ios_base::ate;
322 // Used to track the currently used mode and track whether the output should
324 // [filebuf.virtuals]/12
325 // If setbuf(0, 0) is called on a stream before any I/O has occurred on
326 // that stream, the stream becomes unbuffered. Otherwise the results are
327 // implementation-defined.
328 // This allows calling setbuf(0, 0)
329 // - before opening a file,
330 // - after opening a file, before
334 // Note that opening a file with ios_base::ate does a seek operation.
335 // Normally underflow, overflow, and sync change this flag to ios_base::in,
336 // ios_base_out, or 0.
338 // The ios_base::trunc and ios_base::ate flags are not used in __cm_. They
339 // are used to track the state of the unbuffered request. For readability
340 // they have the aliases __no_io_operations and __use_unbuffered_io
343 // The __no_io_operations and __use_unbuffered_io flags are used in the
345 // - __no_io_operations is set upon construction to indicate the unbuffered
347 // - When requesting unbuffered output:
348 // - If the file is open it sets the mode.
349 // - Else places a request by adding the __use_unbuffered_io flag.
350 // - When a file is opened it checks whether both __no_io_operations and
351 // __use_unbuffered_io are set. If so switches to unbuffered mode.
352 // - All file I/O operations change the mode effectively clearing the
353 // __no_io_operations and __use_unbuffered_io flags.
354 ios_base::openmode __cm_;
357 bool __always_noconv_;
362 _LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);
364 // There are multiple (__)open function, they use different C-API open
365 // function. After that call these functions behave the same. This function
366 // does that part and determines the final return value.
367 _LIBCPP_HIDE_FROM_ABI basic_filebuf* __do_open(FILE* __file, ios_base::openmode __mode) {
373 if (__cm_ == (__no_io_operations | __use_unbuffered_io)) {
374 std::setbuf(__file_, nullptr);
378 if (__mode & ios_base::ate) {
380 if (fseek(__file_, 0, SEEK_END)) {
390 // If the file is already open, switch to unbuffered mode. Otherwise, record
391 // the request to use unbuffered mode so that we use that mode when we
392 // eventually open the file.
393 _LIBCPP_HIDE_FROM_ABI void __request_unbuffered_mode(char_type* __s, streamsize __n) {
394 if (__cm_ == __no_io_operations && __s == nullptr && __n == 0) {
396 std::setbuf(__file_, nullptr);
399 __cm_ = __no_io_operations | __use_unbuffered_io;
405 template <class _CharT, class _Traits>
406 basic_filebuf<_CharT, _Traits>::basic_filebuf()
407 : __extbuf_(nullptr),
408 __extbufnext_(nullptr),
409 __extbufend_(nullptr),
418 __cm_(__no_io_operations),
421 __always_noconv_(false) {
422 if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc())) {
423 __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(this->getloc());
424 __always_noconv_ = __cv_->always_noconv();
426 setbuf(nullptr, 4096);
429 template <class _CharT, class _Traits>
430 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) : basic_streambuf<_CharT, _Traits>(__rhs) {
431 if (__rhs.__extbuf_ == __rhs.__extbuf_min_) {
432 __extbuf_ = __extbuf_min_;
433 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
434 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
436 __extbuf_ = __rhs.__extbuf_;
437 __extbufnext_ = __rhs.__extbufnext_;
438 __extbufend_ = __rhs.__extbufend_;
440 __ebs_ = __rhs.__ebs_;
441 __intbuf_ = __rhs.__intbuf_;
442 __ibs_ = __rhs.__ibs_;
443 __file_ = __rhs.__file_;
446 __st_last_ = __rhs.__st_last_;
449 __owns_eb_ = __rhs.__owns_eb_;
450 __owns_ib_ = __rhs.__owns_ib_;
451 __always_noconv_ = __rhs.__always_noconv_;
453 if (__rhs.pbase() == __rhs.__intbuf_)
454 this->setp(__intbuf_, __intbuf_ + (__rhs.epptr() - __rhs.pbase()));
456 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__rhs.epptr() - __rhs.pbase()));
457 this->__pbump(__rhs.pptr() - __rhs.pbase());
458 } else if (__rhs.eback()) {
459 if (__rhs.eback() == __rhs.__intbuf_)
460 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), __intbuf_ + (__rhs.egptr() - __rhs.eback()));
462 this->setg((char_type*)__extbuf_,
463 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
464 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
466 __rhs.__extbuf_ = nullptr;
467 __rhs.__extbufnext_ = nullptr;
468 __rhs.__extbufend_ = nullptr;
472 __rhs.__file_ = nullptr;
473 __rhs.__st_ = state_type();
474 __rhs.__st_last_ = state_type();
477 __rhs.__owns_eb_ = false;
478 __rhs.__owns_ib_ = false;
483 template <class _CharT, class _Traits>
484 inline basic_filebuf<_CharT, _Traits>& basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) {
490 template <class _CharT, class _Traits>
491 basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
492 # if _LIBCPP_HAS_EXCEPTIONS
494 # endif // _LIBCPP_HAS_EXCEPTIONS
496 # if _LIBCPP_HAS_EXCEPTIONS
499 # endif // _LIBCPP_HAS_EXCEPTIONS
506 template <class _CharT, class _Traits>
507 void basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) {
508 basic_streambuf<char_type, traits_type>::swap(__rhs);
509 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
510 // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
511 std::swap(__extbuf_, __rhs.__extbuf_);
512 std::swap(__extbufnext_, __rhs.__extbufnext_);
513 std::swap(__extbufend_, __rhs.__extbufend_);
515 ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
516 ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
517 ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
518 ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
519 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
520 // *this uses the small buffer, but __rhs doesn't.
521 __extbuf_ = __rhs.__extbuf_;
522 __rhs.__extbuf_ = __rhs.__extbuf_min_;
523 std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));
524 } else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) {
525 // *this doesn't use the small buffer, but __rhs does.
526 __rhs.__extbuf_ = __extbuf_;
527 __extbuf_ = __extbuf_min_;
528 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
530 // Both *this and __rhs use the small buffer.
531 char __tmp[sizeof(__extbuf_min_)];
532 std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));
533 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
534 std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));
536 __extbufnext_ = __extbuf_ + __rn;
537 __extbufend_ = __extbuf_ + __re;
538 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
539 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
541 std::swap(__ebs_, __rhs.__ebs_);
542 std::swap(__intbuf_, __rhs.__intbuf_);
543 std::swap(__ibs_, __rhs.__ibs_);
544 std::swap(__file_, __rhs.__file_);
545 std::swap(__cv_, __rhs.__cv_);
546 std::swap(__st_, __rhs.__st_);
547 std::swap(__st_last_, __rhs.__st_last_);
548 std::swap(__om_, __rhs.__om_);
549 std::swap(__cm_, __rhs.__cm_);
550 std::swap(__owns_eb_, __rhs.__owns_eb_);
551 std::swap(__owns_ib_, __rhs.__owns_ib_);
552 std::swap(__always_noconv_, __rhs.__always_noconv_);
553 if (this->eback() == (char_type*)__rhs.__extbuf_min_) {
554 ptrdiff_t __n = this->gptr() - this->eback();
555 ptrdiff_t __e = this->egptr() - this->eback();
556 this->setg((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __n, (char_type*)__extbuf_min_ + __e);
557 } else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) {
558 ptrdiff_t __n = this->pptr() - this->pbase();
559 ptrdiff_t __e = this->epptr() - this->pbase();
560 this->setp((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __e);
563 if (__rhs.eback() == (char_type*)__extbuf_min_) {
564 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
565 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
567 (char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __n, (char_type*)__rhs.__extbuf_min_ + __e);
568 } else if (__rhs.pbase() == (char_type*)__extbuf_min_) {
569 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
570 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
571 __rhs.setp((char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __e);
576 template <class _CharT, class _Traits>
577 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) {
581 template <class _CharT, class _Traits>
582 inline bool basic_filebuf<_CharT, _Traits>::is_open() const {
583 return __file_ != nullptr;
586 template <class _CharT, class _Traits>
587 const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(ios_base::openmode __mode) _NOEXCEPT {
588 switch (__mode & ~ios_base::ate) {
590 case ios_base::out | ios_base::trunc:
591 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
592 case ios_base::out | ios_base::app:
594 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
596 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
597 case ios_base::in | ios_base::out:
598 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
599 case ios_base::in | ios_base::out | ios_base::trunc:
600 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
601 case ios_base::in | ios_base::out | ios_base::app:
602 case ios_base::in | ios_base::app:
603 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
604 case ios_base::out | ios_base::binary:
605 case ios_base::out | ios_base::trunc | ios_base::binary:
606 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
607 case ios_base::out | ios_base::app | ios_base::binary:
608 case ios_base::app | ios_base::binary:
609 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
610 case ios_base::in | ios_base::binary:
611 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
612 case ios_base::in | ios_base::out | ios_base::binary:
613 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
614 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
615 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
616 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
617 case ios_base::in | ios_base::app | ios_base::binary:
618 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
619 # if _LIBCPP_STD_VER >= 23
620 case ios_base::out | ios_base::noreplace:
621 case ios_base::out | ios_base::trunc | ios_base::noreplace:
622 return "wx" _LIBCPP_FOPEN_CLOEXEC_MODE;
623 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
624 return "w+x" _LIBCPP_FOPEN_CLOEXEC_MODE;
625 case ios_base::out | ios_base::binary | ios_base::noreplace:
626 case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
627 return "wbx" _LIBCPP_FOPEN_CLOEXEC_MODE;
628 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
629 return "w+bx" _LIBCPP_FOPEN_CLOEXEC_MODE;
630 # endif // _LIBCPP_STD_VER >= 23
634 __libcpp_unreachable();
637 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
638 template <class _CharT, class _Traits>
639 const wchar_t* basic_filebuf<_CharT, _Traits>::__make_mdwstring(ios_base::openmode __mode) _NOEXCEPT {
640 switch (__mode & ~ios_base::ate) {
642 case ios_base::out | ios_base::trunc:
644 case ios_base::out | ios_base::app:
649 case ios_base::in | ios_base::out:
651 case ios_base::in | ios_base::out | ios_base::trunc:
653 case ios_base::in | ios_base::out | ios_base::app:
654 case ios_base::in | ios_base::app:
656 case ios_base::out | ios_base::binary:
657 case ios_base::out | ios_base::trunc | ios_base::binary:
659 case ios_base::out | ios_base::app | ios_base::binary:
660 case ios_base::app | ios_base::binary:
662 case ios_base::in | ios_base::binary:
664 case ios_base::in | ios_base::out | ios_base::binary:
666 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
668 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
669 case ios_base::in | ios_base::app | ios_base::binary:
671 # if _LIBCPP_STD_VER >= 23
672 case ios_base::out | ios_base::noreplace:
673 case ios_base::out | ios_base::trunc | ios_base::noreplace:
675 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
677 case ios_base::out | ios_base::binary | ios_base::noreplace:
678 case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
680 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
682 # endif // _LIBCPP_STD_VER >= 23
686 __libcpp_unreachable();
690 template <class _CharT, class _Traits>
691 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
694 const char* __mdstr = __make_mdstring(__mode);
698 return __do_open(fopen(__s, __mdstr), __mode);
701 template <class _CharT, class _Traits>
702 inline basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
705 const char* __mdstr = __make_mdstring(__mode);
709 return __do_open(fdopen(__fd, __mdstr), __mode);
712 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
713 // This is basically the same as the char* overload except that it uses _wfopen
714 // and long mode strings.
715 template <class _CharT, class _Traits>
716 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
719 const wchar_t* __mdstr = __make_mdwstring(__mode);
723 return __do_open(_wfopen(__s, __mdstr), __mode);
727 template <class _CharT, class _Traits>
728 inline basic_filebuf<_CharT, _Traits>*
729 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
730 return open(__s.c_str(), __mode);
733 template <class _CharT, class _Traits>
734 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::close() {
735 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
738 unique_ptr<FILE, int (*)(FILE*)> __h(__file_, fclose);
741 if (fclose(__h.release()))
749 template <class _CharT, class _Traits>
750 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::underflow() {
751 if (__file_ == nullptr)
752 return traits_type::eof();
753 bool __initial = __read_mode();
755 if (this->gptr() == nullptr)
756 this->setg(&__1buf, &__1buf + 1, &__1buf + 1);
757 const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
758 int_type __c = traits_type::eof();
759 if (this->gptr() == this->egptr()) {
760 std::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
761 if (__always_noconv_) {
762 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
763 __nmemb = ::fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
765 this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);
766 __c = traits_type::to_int_type(*this->gptr());
769 if (__extbufend_ != __extbufnext_) {
770 _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");
771 _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");
772 std::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
774 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
775 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
777 std::min(static_cast<size_t>(__ibs_ - __unget_sz), static_cast<size_t>(__extbufend_ - __extbufnext_));
778 codecvt_base::result __r;
780 size_t __nr = fread((void*)const_cast<char*>(__extbufnext_), 1, __nmemb, __file_);
785 __extbufend_ = __extbufnext_ + __nr;
788 __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->eback() + __ibs_, __inext);
789 if (__r == codecvt_base::noconv) {
790 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));
791 __c = traits_type::to_int_type(*this->gptr());
792 } else if (__inext != this->eback() + __unget_sz) {
793 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
794 __c = traits_type::to_int_type(*this->gptr());
799 __c = traits_type::to_int_type(*this->gptr());
800 if (this->eback() == &__1buf)
801 this->setg(nullptr, nullptr, nullptr);
805 template <class _CharT, class _Traits>
806 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
807 if (__file_ && this->eback() < this->gptr()) {
808 if (traits_type::eq_int_type(__c, traits_type::eof())) {
810 return traits_type::not_eof(__c);
812 if ((__om_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {
814 *this->gptr() = traits_type::to_char_type(__c);
818 return traits_type::eof();
821 template <class _CharT, class _Traits>
822 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
823 if (__file_ == nullptr)
824 return traits_type::eof();
827 char_type* __pb_save = this->pbase();
828 char_type* __epb_save = this->epptr();
829 if (!traits_type::eq_int_type(__c, traits_type::eof())) {
830 if (this->pptr() == nullptr)
831 this->setp(&__1buf, &__1buf + 1);
832 *this->pptr() = traits_type::to_char_type(__c);
835 if (this->pptr() != this->pbase()) {
836 if (__always_noconv_) {
837 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
838 if (std::fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
839 return traits_type::eof();
841 char* __extbe = __extbuf_;
842 codecvt_base::result __r;
847 const char_type* __e;
848 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
849 if (__e == this->pbase())
850 return traits_type::eof();
851 if (__r == codecvt_base::noconv) {
852 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
853 if (std::fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
854 return traits_type::eof();
855 } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {
856 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
857 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
858 return traits_type::eof();
859 if (__r == codecvt_base::partial) {
860 this->setp(const_cast<char_type*>(__e), this->pptr());
861 this->__pbump(this->epptr() - this->pbase());
864 return traits_type::eof();
865 } while (__r == codecvt_base::partial);
867 this->setp(__pb_save, __epb_save);
869 return traits_type::not_eof(__c);
872 template <class _CharT, class _Traits>
873 basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {
874 this->setg(nullptr, nullptr, nullptr);
875 this->setp(nullptr, nullptr);
876 __request_unbuffered_mode(__s, __n);
882 if (__ebs_ > sizeof(__extbuf_min_)) {
883 if (__always_noconv_ && __s) {
884 __extbuf_ = (char*)__s;
887 __extbuf_ = new char[__ebs_];
891 __extbuf_ = __extbuf_min_;
892 __ebs_ = sizeof(__extbuf_min_);
895 if (!__always_noconv_) {
896 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
897 if (__s && __ibs_ > sizeof(__extbuf_min_)) {
901 __intbuf_ = new char_type[__ibs_];
912 template <class _CharT, class _Traits>
913 typename basic_filebuf<_CharT, _Traits>::pos_type
914 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) {
918 int __width = __cv_->encoding();
919 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
920 return pos_type(off_type(-1));
921 // __width > 0 || __off == 0
934 return pos_type(off_type(-1));
936 # if !_LIBCPP_HAS_OFF_T_FUNCTIONS
937 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
938 return pos_type(off_type(-1));
939 pos_type __r = ftell(__file_);
941 if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
942 return pos_type(off_type(-1));
943 pos_type __r = ftello(__file_);
949 template <class _CharT, class _Traits>
950 typename basic_filebuf<_CharT, _Traits>::pos_type
951 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {
952 if (__file_ == nullptr || sync())
953 return pos_type(off_type(-1));
954 # if !_LIBCPP_HAS_OFF_T_FUNCTIONS
955 if (fseek(__file_, __sp, SEEK_SET))
956 return pos_type(off_type(-1));
958 if (::fseeko(__file_, __sp, SEEK_SET))
959 return pos_type(off_type(-1));
961 __st_ = __sp.state();
965 template <class _CharT, class _Traits>
966 int basic_filebuf<_CharT, _Traits>::sync() {
967 if (__file_ == nullptr)
972 if (__cm_ & ios_base::out) {
973 if (this->pptr() != this->pbase())
974 if (overflow() == traits_type::eof())
976 codecvt_base::result __r;
979 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
980 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
981 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
983 } while (__r == codecvt_base::partial);
984 if (__r == codecvt_base::error)
988 } else if (__cm_ & ios_base::in) {
990 state_type __state = __st_last_;
991 bool __update_st = false;
992 if (__always_noconv_)
993 __c = this->egptr() - this->gptr();
995 int __width = __cv_->encoding();
996 __c = __extbufend_ - __extbufnext_;
998 __c += __width * (this->egptr() - this->gptr());
1000 if (this->gptr() != this->egptr()) {
1001 const int __off = __cv_->length(__state, __extbuf_, __extbufnext_, this->gptr() - this->eback());
1002 __c += __extbufnext_ - __extbuf_ - __off;
1007 # if !_LIBCPP_HAS_OFF_T_FUNCTIONS
1008 if (fseek(__file_, -__c, SEEK_CUR))
1011 if (::fseeko(__file_, -__c, SEEK_CUR))
1016 __extbufnext_ = __extbufend_ = __extbuf_;
1017 this->setg(nullptr, nullptr, nullptr);
1023 template <class _CharT, class _Traits>
1024 void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
1026 __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(__loc);
1027 bool __old_anc = __always_noconv_;
1028 __always_noconv_ = __cv_->always_noconv();
1029 if (__old_anc != __always_noconv_) {
1030 this->setg(nullptr, nullptr, nullptr);
1031 this->setp(nullptr, nullptr);
1032 // invariant, char_type is char, else we couldn't get here
1033 if (__always_noconv_) // need to dump __intbuf_
1037 __owns_eb_ = __owns_ib_;
1039 __extbuf_ = (char*)__intbuf_;
1041 __intbuf_ = nullptr;
1043 } else // need to obtain an __intbuf_.
1044 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1045 if (!__owns_eb_ && __extbuf_ != __extbuf_min_) {
1047 __intbuf_ = (char_type*)__extbuf_;
1049 __extbuf_ = new char[__ebs_];
1053 __intbuf_ = new char_type[__ibs_];
1060 template <class _CharT, class _Traits>
1061 bool basic_filebuf<_CharT, _Traits>::__read_mode() {
1062 if (!(__cm_ & ios_base::in)) {
1063 this->setp(nullptr, nullptr);
1064 if (__always_noconv_)
1065 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_ + __ebs_, (char_type*)__extbuf_ + __ebs_);
1067 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1068 __cm_ = ios_base::in;
1074 template <class _CharT, class _Traits>
1075 void basic_filebuf<_CharT, _Traits>::__write_mode() {
1076 if (!(__cm_ & ios_base::out)) {
1077 this->setg(nullptr, nullptr, nullptr);
1078 if (__ebs_ > sizeof(__extbuf_min_)) {
1079 if (__always_noconv_)
1080 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));
1082 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1084 this->setp(nullptr, nullptr);
1085 __cm_ = ios_base::out;
1091 template <class _CharT, class _Traits>
1092 class _LIBCPP_TEMPLATE_VIS basic_ifstream : public basic_istream<_CharT, _Traits> {
1094 typedef _CharT char_type;
1095 typedef _Traits traits_type;
1096 typedef typename traits_type::int_type int_type;
1097 typedef typename traits_type::pos_type pos_type;
1098 typedef typename traits_type::off_type off_type;
1099 # if _LIBCPP_STD_VER >= 26
1100 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1103 _LIBCPP_HIDE_FROM_ABI basic_ifstream();
1104 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1105 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1106 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1108 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1109 # if _LIBCPP_STD_VER >= 17
1110 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1111 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
1112 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const _Tp& __p, ios_base::openmode __mode = ios_base::in)
1113 : basic_ifstream(__p.c_str(), __mode) {}
1114 # endif // _LIBCPP_STD_VER >= 17
1115 _LIBCPP_HIDE_FROM_ABI basic_ifstream(basic_ifstream&& __rhs);
1116 _LIBCPP_HIDE_FROM_ABI basic_ifstream& operator=(basic_ifstream&& __rhs);
1117 _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream& __rhs);
1119 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1120 # if _LIBCPP_STD_VER >= 26
1121 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1123 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1124 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1125 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1126 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1128 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1129 # if _LIBCPP_STD_VER >= 17
1130 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1131 open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) {
1132 return open(__p.c_str(), __mode);
1134 # endif // _LIBCPP_STD_VER >= 17
1136 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1137 _LIBCPP_HIDE_FROM_ABI void close();
1140 basic_filebuf<char_type, traits_type> __sb_;
1143 template <class _CharT, class _Traits>
1144 inline basic_ifstream<_CharT, _Traits>::basic_ifstream()
1145 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {}
1147 template <class _CharT, class _Traits>
1148 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1149 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1150 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1151 this->setstate(ios_base::failbit);
1154 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1155 template <class _CharT, class _Traits>
1156 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1157 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1158 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1159 this->setstate(ios_base::failbit);
1164 template <class _CharT, class _Traits>
1165 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1166 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1167 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1168 this->setstate(ios_base::failbit);
1171 template <class _CharT, class _Traits>
1172 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1173 : basic_istream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1174 this->set_rdbuf(std::addressof(__sb_));
1177 template <class _CharT, class _Traits>
1178 inline basic_ifstream<_CharT, _Traits>& basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) {
1179 basic_istream<char_type, traits_type>::operator=(std::move(__rhs));
1180 __sb_ = std::move(__rhs.__sb_);
1184 template <class _CharT, class _Traits>
1185 inline void basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) {
1186 basic_istream<char_type, traits_type>::swap(__rhs);
1187 __sb_.swap(__rhs.__sb_);
1190 template <class _CharT, class _Traits>
1191 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) {
1195 template <class _CharT, class _Traits>
1196 inline basic_filebuf<_CharT, _Traits>* basic_ifstream<_CharT, _Traits>::rdbuf() const {
1197 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1200 template <class _CharT, class _Traits>
1201 inline bool basic_ifstream<_CharT, _Traits>::is_open() const {
1202 return __sb_.is_open();
1205 template <class _CharT, class _Traits>
1206 void basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1207 if (__sb_.open(__s, __mode | ios_base::in))
1210 this->setstate(ios_base::failbit);
1213 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1214 template <class _CharT, class _Traits>
1215 void basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1216 if (__sb_.open(__s, __mode | ios_base::in))
1219 this->setstate(ios_base::failbit);
1223 template <class _CharT, class _Traits>
1224 void basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1225 if (__sb_.open(__s, __mode | ios_base::in))
1228 this->setstate(ios_base::failbit);
1231 template <class _CharT, class _Traits>
1232 inline void basic_ifstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1233 if (__sb_.__open(__fd, __mode | ios_base::in))
1236 this->setstate(ios_base::failbit);
1239 template <class _CharT, class _Traits>
1240 inline void basic_ifstream<_CharT, _Traits>::close() {
1241 if (__sb_.close() == 0)
1242 this->setstate(ios_base::failbit);
1247 template <class _CharT, class _Traits>
1248 class _LIBCPP_TEMPLATE_VIS basic_ofstream : public basic_ostream<_CharT, _Traits> {
1250 typedef _CharT char_type;
1251 typedef _Traits traits_type;
1252 typedef typename traits_type::int_type int_type;
1253 typedef typename traits_type::pos_type pos_type;
1254 typedef typename traits_type::off_type off_type;
1255 # if _LIBCPP_STD_VER >= 26
1256 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1259 _LIBCPP_HIDE_FROM_ABI basic_ofstream();
1260 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1261 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1262 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1264 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1266 # if _LIBCPP_STD_VER >= 17
1267 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1268 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
1269 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const _Tp& __p, ios_base::openmode __mode = ios_base::out)
1270 : basic_ofstream(__p.c_str(), __mode) {}
1271 # endif // _LIBCPP_STD_VER >= 17
1273 _LIBCPP_HIDE_FROM_ABI basic_ofstream(basic_ofstream&& __rhs);
1274 _LIBCPP_HIDE_FROM_ABI basic_ofstream& operator=(basic_ofstream&& __rhs);
1275 _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream& __rhs);
1277 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1278 # if _LIBCPP_STD_VER >= 26
1279 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1281 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1282 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1283 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1284 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1286 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1288 # if _LIBCPP_STD_VER >= 17
1289 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1290 open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) {
1291 return open(__p.c_str(), __mode);
1293 # endif // _LIBCPP_STD_VER >= 17
1295 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1296 _LIBCPP_HIDE_FROM_ABI void close();
1299 basic_filebuf<char_type, traits_type> __sb_;
1302 template <class _CharT, class _Traits>
1303 inline basic_ofstream<_CharT, _Traits>::basic_ofstream()
1304 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {}
1306 template <class _CharT, class _Traits>
1307 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1308 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1309 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1310 this->setstate(ios_base::failbit);
1313 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1314 template <class _CharT, class _Traits>
1315 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1316 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1317 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1318 this->setstate(ios_base::failbit);
1323 template <class _CharT, class _Traits>
1324 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1325 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1326 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1327 this->setstate(ios_base::failbit);
1330 template <class _CharT, class _Traits>
1331 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1332 : basic_ostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1333 this->set_rdbuf(std::addressof(__sb_));
1336 template <class _CharT, class _Traits>
1337 inline basic_ofstream<_CharT, _Traits>& basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) {
1338 basic_ostream<char_type, traits_type>::operator=(std::move(__rhs));
1339 __sb_ = std::move(__rhs.__sb_);
1343 template <class _CharT, class _Traits>
1344 inline void basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) {
1345 basic_ostream<char_type, traits_type>::swap(__rhs);
1346 __sb_.swap(__rhs.__sb_);
1349 template <class _CharT, class _Traits>
1350 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) {
1354 template <class _CharT, class _Traits>
1355 inline basic_filebuf<_CharT, _Traits>* basic_ofstream<_CharT, _Traits>::rdbuf() const {
1356 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1359 template <class _CharT, class _Traits>
1360 inline bool basic_ofstream<_CharT, _Traits>::is_open() const {
1361 return __sb_.is_open();
1364 template <class _CharT, class _Traits>
1365 void basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1366 if (__sb_.open(__s, __mode | ios_base::out))
1369 this->setstate(ios_base::failbit);
1372 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1373 template <class _CharT, class _Traits>
1374 void basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1375 if (__sb_.open(__s, __mode | ios_base::out))
1378 this->setstate(ios_base::failbit);
1382 template <class _CharT, class _Traits>
1383 void basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1384 if (__sb_.open(__s, __mode | ios_base::out))
1387 this->setstate(ios_base::failbit);
1390 template <class _CharT, class _Traits>
1391 inline void basic_ofstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1392 if (__sb_.__open(__fd, __mode | ios_base::out))
1395 this->setstate(ios_base::failbit);
1398 template <class _CharT, class _Traits>
1399 inline void basic_ofstream<_CharT, _Traits>::close() {
1400 if (__sb_.close() == nullptr)
1401 this->setstate(ios_base::failbit);
1406 template <class _CharT, class _Traits>
1407 class _LIBCPP_TEMPLATE_VIS basic_fstream : public basic_iostream<_CharT, _Traits> {
1409 typedef _CharT char_type;
1410 typedef _Traits traits_type;
1411 typedef typename traits_type::int_type int_type;
1412 typedef typename traits_type::pos_type pos_type;
1413 typedef typename traits_type::off_type off_type;
1414 # if _LIBCPP_STD_VER >= 26
1415 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1418 _LIBCPP_HIDE_FROM_ABI basic_fstream();
1419 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const char* __s,
1420 ios_base::openmode __mode = ios_base::in | ios_base::out);
1421 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1422 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const wchar_t* __s,
1423 ios_base::openmode __mode = ios_base::in | ios_base::out);
1425 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const string& __s,
1426 ios_base::openmode __mode = ios_base::in | ios_base::out);
1428 # if _LIBCPP_STD_VER >= 17
1429 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1430 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(
1431 const _Tp& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1432 : basic_fstream(__p.c_str(), __mode) {}
1433 # endif // _LIBCPP_STD_VER >= 17
1435 _LIBCPP_HIDE_FROM_ABI basic_fstream(basic_fstream&& __rhs);
1437 _LIBCPP_HIDE_FROM_ABI basic_fstream& operator=(basic_fstream&& __rhs);
1439 _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream& __rhs);
1441 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1442 # if _LIBCPP_STD_VER >= 26
1443 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1445 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1446 _LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1447 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1448 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1450 _LIBCPP_HIDE_FROM_ABI void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1452 # if _LIBCPP_STD_VER >= 17
1453 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1454 open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1455 return open(__p.c_str(), __mode);
1457 # endif // _LIBCPP_STD_VER >= 17
1459 _LIBCPP_HIDE_FROM_ABI void close();
1462 basic_filebuf<char_type, traits_type> __sb_;
1465 template <class _CharT, class _Traits>
1466 inline basic_fstream<_CharT, _Traits>::basic_fstream()
1467 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {}
1469 template <class _CharT, class _Traits>
1470 inline basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1471 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1472 if (__sb_.open(__s, __mode) == nullptr)
1473 this->setstate(ios_base::failbit);
1476 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1477 template <class _CharT, class _Traits>
1478 inline basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1479 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1480 if (__sb_.open(__s, __mode) == nullptr)
1481 this->setstate(ios_base::failbit);
1485 template <class _CharT, class _Traits>
1486 inline basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1487 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1488 if (__sb_.open(__s, __mode) == nullptr)
1489 this->setstate(ios_base::failbit);
1493 template <class _CharT, class _Traits>
1494 inline basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1495 : basic_iostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1496 this->set_rdbuf(std::addressof(__sb_));
1499 template <class _CharT, class _Traits>
1500 inline basic_fstream<_CharT, _Traits>& basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs) {
1501 basic_iostream<char_type, traits_type>::operator=(std::move(__rhs));
1502 __sb_ = std::move(__rhs.__sb_);
1506 template <class _CharT, class _Traits>
1507 inline void basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs) {
1508 basic_iostream<char_type, traits_type>::swap(__rhs);
1509 __sb_.swap(__rhs.__sb_);
1512 template <class _CharT, class _Traits>
1513 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) {
1517 template <class _CharT, class _Traits>
1518 inline basic_filebuf<_CharT, _Traits>* basic_fstream<_CharT, _Traits>::rdbuf() const {
1519 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1522 template <class _CharT, class _Traits>
1523 inline bool basic_fstream<_CharT, _Traits>::is_open() const {
1524 return __sb_.is_open();
1527 template <class _CharT, class _Traits>
1528 void basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1529 if (__sb_.open(__s, __mode))
1532 this->setstate(ios_base::failbit);
1535 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1536 template <class _CharT, class _Traits>
1537 void basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1538 if (__sb_.open(__s, __mode))
1541 this->setstate(ios_base::failbit);
1545 template <class _CharT, class _Traits>
1546 void basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1547 if (__sb_.open(__s, __mode))
1550 this->setstate(ios_base::failbit);
1553 template <class _CharT, class _Traits>
1554 inline void basic_fstream<_CharT, _Traits>::close() {
1555 if (__sb_.close() == nullptr)
1556 this->setstate(ios_base::failbit);
1559 # if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1
1560 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1561 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1562 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
1565 _LIBCPP_END_NAMESPACE_STD
1567 #endif // _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
1571 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1573 # include <concepts>
1579 # include <stdexcept>
1580 # include <type_traits>
1583 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 23
1584 # include <filesystem>
1587 #endif // _LIBCPP_FSTREAM