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 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
190 # include <__cxx03/fstream>
192 # include <__algorithm/max.h>
195 # include <__filesystem/path.h>
196 # include <__fwd/fstream.h>
198 # include <__memory/addressof.h>
199 # include <__memory/unique_ptr.h>
200 # include <__ostream/basic_ostream.h>
201 # include <__type_traits/enable_if.h>
202 # include <__type_traits/is_same.h>
203 # include <__utility/move.h>
204 # include <__utility/swap.h>
205 # include <__utility/unreachable.h>
208 # include <streambuf>
212 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
213 # pragma GCC system_header
217 # include <__undef_macros>
219 # if !defined(_LIBCPP_MSVCRT) && !defined(_NEWLIB_VERSION)
220 # define _LIBCPP_HAS_OFF_T_FUNCTIONS 1
222 # define _LIBCPP_HAS_OFF_T_FUNCTIONS 0
225 # if _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
227 _LIBCPP_BEGIN_NAMESPACE_STD
229 # if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_WIN32API)
230 _LIBCPP_EXPORTED_FROM_ABI void* __filebuf_windows_native_handle(FILE* __file) noexcept;
233 template <class _CharT, class _Traits>
234 class _LIBCPP_TEMPLATE_VIS basic_filebuf : public basic_streambuf<_CharT, _Traits> {
236 typedef _CharT char_type;
237 typedef _Traits traits_type;
238 typedef typename traits_type::int_type int_type;
239 typedef typename traits_type::pos_type pos_type;
240 typedef typename traits_type::off_type off_type;
241 typedef typename traits_type::state_type state_type;
242 # if _LIBCPP_STD_VER >= 26
243 # if defined(_LIBCPP_WIN32API)
244 using native_handle_type = void*; // HANDLE
245 # elif __has_include(<unistd.h>)
246 using native_handle_type = int; // POSIX file descriptor
248 # error "Provide a native file handle!"
252 // 27.9.1.2 Constructors/destructor:
254 basic_filebuf(basic_filebuf&& __rhs);
255 ~basic_filebuf() override;
257 // 27.9.1.3 Assign/swap:
258 _LIBCPP_HIDE_FROM_ABI basic_filebuf& operator=(basic_filebuf&& __rhs);
259 void swap(basic_filebuf& __rhs);
262 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
263 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
264 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
265 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
267 _LIBCPP_HIDE_FROM_ABI basic_filebuf* open(const string& __s, ios_base::openmode __mode);
269 # if _LIBCPP_STD_VER >= 17
270 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI basic_filebuf*
271 open(const filesystem::path& __p, ios_base::openmode __mode) {
272 return open(__p.c_str(), __mode);
275 _LIBCPP_HIDE_FROM_ABI basic_filebuf* __open(int __fd, ios_base::openmode __mode);
276 basic_filebuf* close();
277 # if _LIBCPP_STD_VER >= 26
278 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
279 _LIBCPP_ASSERT_UNCATEGORIZED(this->is_open(), "File must be opened");
280 # if defined(_LIBCPP_WIN32API)
281 return std::__filebuf_windows_native_handle(__file_);
282 # elif __has_include(<unistd.h>)
283 return fileno(__file_);
285 # error "Provide a way to determine the file native handle!"
288 # endif // _LIBCPP_STD_VER >= 26
290 _LIBCPP_HIDE_FROM_ABI inline static const char* __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
291 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
292 _LIBCPP_HIDE_FROM_ABI inline static const wchar_t* __make_mdwstring(ios_base::openmode __mode) _NOEXCEPT;
296 // 27.9.1.5 Overridden virtual functions:
297 int_type underflow() override;
298 int_type pbackfail(int_type __c = traits_type::eof()) override;
299 int_type overflow(int_type __c = traits_type::eof()) override;
300 basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override;
302 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
303 pos_type seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
305 void imbue(const locale& __loc) override;
309 const char* __extbufnext_;
310 const char* __extbufend_;
311 char __extbuf_min_[8];
313 char_type* __intbuf_;
316 const codecvt<char_type, char, state_type>* __cv_;
318 state_type __st_last_;
319 ios_base::openmode __om_;
320 // There have been no file operations yet, which allows setting unbuffered
322 static const ios_base::openmode __no_io_operations = ios_base::trunc;
323 // Unbuffered I/O mode has been requested.
324 static const ios_base::openmode __use_unbuffered_io = ios_base::ate;
325 // Used to track the currently used mode and track whether the output should
327 // [filebuf.virtuals]/12
328 // If setbuf(0, 0) is called on a stream before any I/O has occurred on
329 // that stream, the stream becomes unbuffered. Otherwise the results are
330 // implementation-defined.
331 // This allows calling setbuf(0, 0)
332 // - before opening a file,
333 // - after opening a file, before
337 // Note that opening a file with ios_base::ate does a seek operation.
338 // Normally underflow, overflow, and sync change this flag to ios_base::in,
339 // ios_base_out, or 0.
341 // The ios_base::trunc and ios_base::ate flags are not used in __cm_. They
342 // are used to track the state of the unbuffered request. For readability
343 // they have the aliases __no_io_operations and __use_unbuffered_io
346 // The __no_io_operations and __use_unbuffered_io flags are used in the
348 // - __no_io_operations is set upon construction to indicate the unbuffered
350 // - When requesting unbuffered output:
351 // - If the file is open it sets the mode.
352 // - Else places a request by adding the __use_unbuffered_io flag.
353 // - When a file is opened it checks whether both __no_io_operations and
354 // __use_unbuffered_io are set. If so switches to unbuffered mode.
355 // - All file I/O operations change the mode effectively clearing the
356 // __no_io_operations and __use_unbuffered_io flags.
357 ios_base::openmode __cm_;
360 bool __always_noconv_;
365 _LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);
367 // There are multiple (__)open function, they use different C-API open
368 // function. After that call these functions behave the same. This function
369 // does that part and determines the final return value.
370 _LIBCPP_HIDE_FROM_ABI basic_filebuf* __do_open(FILE* __file, ios_base::openmode __mode) {
376 if (__cm_ == (__no_io_operations | __use_unbuffered_io)) {
377 std::setbuf(__file_, nullptr);
381 if (__mode & ios_base::ate) {
383 if (fseek(__file_, 0, SEEK_END)) {
393 // If the file is already open, switch to unbuffered mode. Otherwise, record
394 // the request to use unbuffered mode so that we use that mode when we
395 // eventually open the file.
396 _LIBCPP_HIDE_FROM_ABI void __request_unbuffered_mode(char_type* __s, streamsize __n) {
397 if (__cm_ == __no_io_operations && __s == nullptr && __n == 0) {
399 std::setbuf(__file_, nullptr);
402 __cm_ = __no_io_operations | __use_unbuffered_io;
408 template <class _CharT, class _Traits>
409 basic_filebuf<_CharT, _Traits>::basic_filebuf()
410 : __extbuf_(nullptr),
411 __extbufnext_(nullptr),
412 __extbufend_(nullptr),
421 __cm_(__no_io_operations),
424 __always_noconv_(false) {
425 if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc())) {
426 __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(this->getloc());
427 __always_noconv_ = __cv_->always_noconv();
429 setbuf(nullptr, 4096);
432 template <class _CharT, class _Traits>
433 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) : basic_streambuf<_CharT, _Traits>(__rhs) {
434 if (__rhs.__extbuf_ == __rhs.__extbuf_min_) {
435 __extbuf_ = __extbuf_min_;
436 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
437 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
439 __extbuf_ = __rhs.__extbuf_;
440 __extbufnext_ = __rhs.__extbufnext_;
441 __extbufend_ = __rhs.__extbufend_;
443 __ebs_ = __rhs.__ebs_;
444 __intbuf_ = __rhs.__intbuf_;
445 __ibs_ = __rhs.__ibs_;
446 __file_ = __rhs.__file_;
449 __st_last_ = __rhs.__st_last_;
452 __owns_eb_ = __rhs.__owns_eb_;
453 __owns_ib_ = __rhs.__owns_ib_;
454 __always_noconv_ = __rhs.__always_noconv_;
456 if (__rhs.pbase() == __rhs.__intbuf_)
457 this->setp(__intbuf_, __intbuf_ + (__rhs.epptr() - __rhs.pbase()));
459 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__rhs.epptr() - __rhs.pbase()));
460 this->__pbump(__rhs.pptr() - __rhs.pbase());
461 } else if (__rhs.eback()) {
462 if (__rhs.eback() == __rhs.__intbuf_)
463 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), __intbuf_ + (__rhs.egptr() - __rhs.eback()));
465 this->setg((char_type*)__extbuf_,
466 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
467 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
469 __rhs.__extbuf_ = nullptr;
470 __rhs.__extbufnext_ = nullptr;
471 __rhs.__extbufend_ = nullptr;
475 __rhs.__file_ = nullptr;
476 __rhs.__st_ = state_type();
477 __rhs.__st_last_ = state_type();
480 __rhs.__owns_eb_ = false;
481 __rhs.__owns_ib_ = false;
486 template <class _CharT, class _Traits>
487 inline basic_filebuf<_CharT, _Traits>& basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) {
493 template <class _CharT, class _Traits>
494 basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
495 # if _LIBCPP_HAS_EXCEPTIONS
497 # endif // _LIBCPP_HAS_EXCEPTIONS
499 # if _LIBCPP_HAS_EXCEPTIONS
502 # endif // _LIBCPP_HAS_EXCEPTIONS
509 template <class _CharT, class _Traits>
510 void basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) {
511 basic_streambuf<char_type, traits_type>::swap(__rhs);
512 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
513 // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
514 std::swap(__extbuf_, __rhs.__extbuf_);
515 std::swap(__extbufnext_, __rhs.__extbufnext_);
516 std::swap(__extbufend_, __rhs.__extbufend_);
518 ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
519 ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
520 ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
521 ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
522 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
523 // *this uses the small buffer, but __rhs doesn't.
524 __extbuf_ = __rhs.__extbuf_;
525 __rhs.__extbuf_ = __rhs.__extbuf_min_;
526 std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));
527 } else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) {
528 // *this doesn't use the small buffer, but __rhs does.
529 __rhs.__extbuf_ = __extbuf_;
530 __extbuf_ = __extbuf_min_;
531 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
533 // Both *this and __rhs use the small buffer.
534 char __tmp[sizeof(__extbuf_min_)];
535 std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));
536 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
537 std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));
539 __extbufnext_ = __extbuf_ + __rn;
540 __extbufend_ = __extbuf_ + __re;
541 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
542 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
544 std::swap(__ebs_, __rhs.__ebs_);
545 std::swap(__intbuf_, __rhs.__intbuf_);
546 std::swap(__ibs_, __rhs.__ibs_);
547 std::swap(__file_, __rhs.__file_);
548 std::swap(__cv_, __rhs.__cv_);
549 std::swap(__st_, __rhs.__st_);
550 std::swap(__st_last_, __rhs.__st_last_);
551 std::swap(__om_, __rhs.__om_);
552 std::swap(__cm_, __rhs.__cm_);
553 std::swap(__owns_eb_, __rhs.__owns_eb_);
554 std::swap(__owns_ib_, __rhs.__owns_ib_);
555 std::swap(__always_noconv_, __rhs.__always_noconv_);
556 if (this->eback() == (char_type*)__rhs.__extbuf_min_) {
557 ptrdiff_t __n = this->gptr() - this->eback();
558 ptrdiff_t __e = this->egptr() - this->eback();
559 this->setg((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __n, (char_type*)__extbuf_min_ + __e);
560 } else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) {
561 ptrdiff_t __n = this->pptr() - this->pbase();
562 ptrdiff_t __e = this->epptr() - this->pbase();
563 this->setp((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __e);
566 if (__rhs.eback() == (char_type*)__extbuf_min_) {
567 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
568 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
570 (char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __n, (char_type*)__rhs.__extbuf_min_ + __e);
571 } else if (__rhs.pbase() == (char_type*)__extbuf_min_) {
572 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
573 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
574 __rhs.setp((char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __e);
579 template <class _CharT, class _Traits>
580 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) {
584 template <class _CharT, class _Traits>
585 inline bool basic_filebuf<_CharT, _Traits>::is_open() const {
586 return __file_ != nullptr;
589 template <class _CharT, class _Traits>
590 const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(ios_base::openmode __mode) _NOEXCEPT {
591 switch (__mode & ~ios_base::ate) {
593 case ios_base::out | ios_base::trunc:
594 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
595 case ios_base::out | ios_base::app:
597 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
599 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
600 case ios_base::in | ios_base::out:
601 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
602 case ios_base::in | ios_base::out | ios_base::trunc:
603 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
604 case ios_base::in | ios_base::out | ios_base::app:
605 case ios_base::in | ios_base::app:
606 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
607 case ios_base::out | ios_base::binary:
608 case ios_base::out | ios_base::trunc | ios_base::binary:
609 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
610 case ios_base::out | ios_base::app | ios_base::binary:
611 case ios_base::app | ios_base::binary:
612 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
613 case ios_base::in | ios_base::binary:
614 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
615 case ios_base::in | ios_base::out | ios_base::binary:
616 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
617 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
618 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
619 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
620 case ios_base::in | ios_base::app | ios_base::binary:
621 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
622 # if _LIBCPP_STD_VER >= 23
623 case ios_base::out | ios_base::noreplace:
624 case ios_base::out | ios_base::trunc | ios_base::noreplace:
625 return "wx" _LIBCPP_FOPEN_CLOEXEC_MODE;
626 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
627 return "w+x" _LIBCPP_FOPEN_CLOEXEC_MODE;
628 case ios_base::out | ios_base::binary | ios_base::noreplace:
629 case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
630 return "wbx" _LIBCPP_FOPEN_CLOEXEC_MODE;
631 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
632 return "w+bx" _LIBCPP_FOPEN_CLOEXEC_MODE;
633 # endif // _LIBCPP_STD_VER >= 23
637 __libcpp_unreachable();
640 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
641 template <class _CharT, class _Traits>
642 const wchar_t* basic_filebuf<_CharT, _Traits>::__make_mdwstring(ios_base::openmode __mode) _NOEXCEPT {
643 switch (__mode & ~ios_base::ate) {
645 case ios_base::out | ios_base::trunc:
647 case ios_base::out | ios_base::app:
652 case ios_base::in | ios_base::out:
654 case ios_base::in | ios_base::out | ios_base::trunc:
656 case ios_base::in | ios_base::out | ios_base::app:
657 case ios_base::in | ios_base::app:
659 case ios_base::out | ios_base::binary:
660 case ios_base::out | ios_base::trunc | ios_base::binary:
662 case ios_base::out | ios_base::app | ios_base::binary:
663 case ios_base::app | ios_base::binary:
665 case ios_base::in | ios_base::binary:
667 case ios_base::in | ios_base::out | ios_base::binary:
669 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
671 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
672 case ios_base::in | ios_base::app | ios_base::binary:
674 # if _LIBCPP_STD_VER >= 23
675 case ios_base::out | ios_base::noreplace:
676 case ios_base::out | ios_base::trunc | ios_base::noreplace:
678 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
680 case ios_base::out | ios_base::binary | ios_base::noreplace:
681 case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
683 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
685 # endif // _LIBCPP_STD_VER >= 23
689 __libcpp_unreachable();
693 template <class _CharT, class _Traits>
694 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
697 const char* __mdstr = __make_mdstring(__mode);
701 return __do_open(fopen(__s, __mdstr), __mode);
704 template <class _CharT, class _Traits>
705 inline basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
708 const char* __mdstr = __make_mdstring(__mode);
712 return __do_open(fdopen(__fd, __mdstr), __mode);
715 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
716 // This is basically the same as the char* overload except that it uses _wfopen
717 // and long mode strings.
718 template <class _CharT, class _Traits>
719 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
722 const wchar_t* __mdstr = __make_mdwstring(__mode);
726 return __do_open(_wfopen(__s, __mdstr), __mode);
730 template <class _CharT, class _Traits>
731 inline basic_filebuf<_CharT, _Traits>*
732 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
733 return open(__s.c_str(), __mode);
736 template <class _CharT, class _Traits>
737 basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::close() {
738 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
741 unique_ptr<FILE, int (*)(FILE*)> __h(__file_, fclose);
744 if (fclose(__h.release()))
752 template <class _CharT, class _Traits>
753 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::underflow() {
754 if (__file_ == nullptr)
755 return traits_type::eof();
756 bool __initial = __read_mode();
758 if (this->gptr() == nullptr)
759 this->setg(&__1buf, &__1buf + 1, &__1buf + 1);
760 const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
761 int_type __c = traits_type::eof();
762 if (this->gptr() == this->egptr()) {
763 std::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
764 if (__always_noconv_) {
765 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
766 __nmemb = ::fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
768 this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);
769 __c = traits_type::to_int_type(*this->gptr());
772 if (__extbufend_ != __extbufnext_) {
773 _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");
774 _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");
775 std::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
777 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
778 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
780 std::min(static_cast<size_t>(__ibs_ - __unget_sz), static_cast<size_t>(__extbufend_ - __extbufnext_));
781 codecvt_base::result __r;
783 size_t __nr = fread((void*)const_cast<char*>(__extbufnext_), 1, __nmemb, __file_);
788 __extbufend_ = __extbufnext_ + __nr;
791 __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->eback() + __ibs_, __inext);
792 if (__r == codecvt_base::noconv) {
793 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));
794 __c = traits_type::to_int_type(*this->gptr());
795 } else if (__inext != this->eback() + __unget_sz) {
796 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
797 __c = traits_type::to_int_type(*this->gptr());
802 __c = traits_type::to_int_type(*this->gptr());
803 if (this->eback() == &__1buf)
804 this->setg(nullptr, nullptr, nullptr);
808 template <class _CharT, class _Traits>
809 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
810 if (__file_ && this->eback() < this->gptr()) {
811 if (traits_type::eq_int_type(__c, traits_type::eof())) {
813 return traits_type::not_eof(__c);
815 if ((__om_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {
817 *this->gptr() = traits_type::to_char_type(__c);
821 return traits_type::eof();
824 template <class _CharT, class _Traits>
825 typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
826 if (__file_ == nullptr)
827 return traits_type::eof();
830 char_type* __pb_save = this->pbase();
831 char_type* __epb_save = this->epptr();
832 if (!traits_type::eq_int_type(__c, traits_type::eof())) {
833 if (this->pptr() == nullptr)
834 this->setp(&__1buf, &__1buf + 1);
835 *this->pptr() = traits_type::to_char_type(__c);
838 if (this->pptr() != this->pbase()) {
839 if (__always_noconv_) {
840 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
841 if (std::fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
842 return traits_type::eof();
844 char* __extbe = __extbuf_;
845 codecvt_base::result __r;
850 const char_type* __e;
851 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
852 if (__e == this->pbase())
853 return traits_type::eof();
854 if (__r == codecvt_base::noconv) {
855 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
856 if (std::fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
857 return traits_type::eof();
858 } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {
859 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
860 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
861 return traits_type::eof();
862 if (__r == codecvt_base::partial) {
863 this->setp(const_cast<char_type*>(__e), this->pptr());
864 this->__pbump(this->epptr() - this->pbase());
867 return traits_type::eof();
868 } while (__r == codecvt_base::partial);
870 this->setp(__pb_save, __epb_save);
872 return traits_type::not_eof(__c);
875 template <class _CharT, class _Traits>
876 basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {
877 this->setg(nullptr, nullptr, nullptr);
878 this->setp(nullptr, nullptr);
879 __request_unbuffered_mode(__s, __n);
885 if (__ebs_ > sizeof(__extbuf_min_)) {
886 if (__always_noconv_ && __s) {
887 __extbuf_ = (char*)__s;
890 __extbuf_ = new char[__ebs_];
894 __extbuf_ = __extbuf_min_;
895 __ebs_ = sizeof(__extbuf_min_);
898 if (!__always_noconv_) {
899 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
900 if (__s && __ibs_ > sizeof(__extbuf_min_)) {
904 __intbuf_ = new char_type[__ibs_];
915 template <class _CharT, class _Traits>
916 typename basic_filebuf<_CharT, _Traits>::pos_type
917 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) {
921 int __width = __cv_->encoding();
922 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
923 return pos_type(off_type(-1));
924 // __width > 0 || __off == 0
937 return pos_type(off_type(-1));
939 # if !_LIBCPP_HAS_OFF_T_FUNCTIONS
940 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
941 return pos_type(off_type(-1));
942 pos_type __r = ftell(__file_);
944 if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
945 return pos_type(off_type(-1));
946 pos_type __r = ftello(__file_);
952 template <class _CharT, class _Traits>
953 typename basic_filebuf<_CharT, _Traits>::pos_type
954 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {
955 if (__file_ == nullptr || sync())
956 return pos_type(off_type(-1));
957 # if !_LIBCPP_HAS_OFF_T_FUNCTIONS
958 if (fseek(__file_, __sp, SEEK_SET))
959 return pos_type(off_type(-1));
961 if (::fseeko(__file_, __sp, SEEK_SET))
962 return pos_type(off_type(-1));
964 __st_ = __sp.state();
968 template <class _CharT, class _Traits>
969 int basic_filebuf<_CharT, _Traits>::sync() {
970 if (__file_ == nullptr)
975 if (__cm_ & ios_base::out) {
976 if (this->pptr() != this->pbase())
977 if (overflow() == traits_type::eof())
979 codecvt_base::result __r;
982 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
983 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
984 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
986 } while (__r == codecvt_base::partial);
987 if (__r == codecvt_base::error)
991 } else if (__cm_ & ios_base::in) {
993 state_type __state = __st_last_;
994 bool __update_st = false;
995 if (__always_noconv_)
996 __c = this->egptr() - this->gptr();
998 int __width = __cv_->encoding();
999 __c = __extbufend_ - __extbufnext_;
1001 __c += __width * (this->egptr() - this->gptr());
1003 if (this->gptr() != this->egptr()) {
1004 const int __off = __cv_->length(__state, __extbuf_, __extbufnext_, this->gptr() - this->eback());
1005 __c += __extbufnext_ - __extbuf_ - __off;
1010 # if !_LIBCPP_HAS_OFF_T_FUNCTIONS
1011 if (fseek(__file_, -__c, SEEK_CUR))
1014 if (::fseeko(__file_, -__c, SEEK_CUR))
1019 __extbufnext_ = __extbufend_ = __extbuf_;
1020 this->setg(nullptr, nullptr, nullptr);
1026 template <class _CharT, class _Traits>
1027 void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
1029 __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(__loc);
1030 bool __old_anc = __always_noconv_;
1031 __always_noconv_ = __cv_->always_noconv();
1032 if (__old_anc != __always_noconv_) {
1033 this->setg(nullptr, nullptr, nullptr);
1034 this->setp(nullptr, nullptr);
1035 // invariant, char_type is char, else we couldn't get here
1036 if (__always_noconv_) // need to dump __intbuf_
1040 __owns_eb_ = __owns_ib_;
1042 __extbuf_ = (char*)__intbuf_;
1044 __intbuf_ = nullptr;
1046 } else // need to obtain an __intbuf_.
1047 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1048 if (!__owns_eb_ && __extbuf_ != __extbuf_min_) {
1050 __intbuf_ = (char_type*)__extbuf_;
1052 __extbuf_ = new char[__ebs_];
1056 __intbuf_ = new char_type[__ibs_];
1063 template <class _CharT, class _Traits>
1064 bool basic_filebuf<_CharT, _Traits>::__read_mode() {
1065 if (!(__cm_ & ios_base::in)) {
1066 this->setp(nullptr, nullptr);
1067 if (__always_noconv_)
1068 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_ + __ebs_, (char_type*)__extbuf_ + __ebs_);
1070 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1071 __cm_ = ios_base::in;
1077 template <class _CharT, class _Traits>
1078 void basic_filebuf<_CharT, _Traits>::__write_mode() {
1079 if (!(__cm_ & ios_base::out)) {
1080 this->setg(nullptr, nullptr, nullptr);
1081 if (__ebs_ > sizeof(__extbuf_min_)) {
1082 if (__always_noconv_)
1083 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));
1085 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1087 this->setp(nullptr, nullptr);
1088 __cm_ = ios_base::out;
1094 template <class _CharT, class _Traits>
1095 class _LIBCPP_TEMPLATE_VIS basic_ifstream : public basic_istream<_CharT, _Traits> {
1097 typedef _CharT char_type;
1098 typedef _Traits traits_type;
1099 typedef typename traits_type::int_type int_type;
1100 typedef typename traits_type::pos_type pos_type;
1101 typedef typename traits_type::off_type off_type;
1102 # if _LIBCPP_STD_VER >= 26
1103 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1106 _LIBCPP_HIDE_FROM_ABI basic_ifstream();
1107 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1108 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1109 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1111 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1112 # if _LIBCPP_STD_VER >= 17
1113 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1114 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
1115 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const _Tp& __p, ios_base::openmode __mode = ios_base::in)
1116 : basic_ifstream(__p.c_str(), __mode) {}
1117 # endif // _LIBCPP_STD_VER >= 17
1118 _LIBCPP_HIDE_FROM_ABI basic_ifstream(basic_ifstream&& __rhs);
1119 _LIBCPP_HIDE_FROM_ABI basic_ifstream& operator=(basic_ifstream&& __rhs);
1120 _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream& __rhs);
1122 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1123 # if _LIBCPP_STD_VER >= 26
1124 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1126 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1127 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1128 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1129 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1131 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1132 # if _LIBCPP_STD_VER >= 17
1133 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1134 open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) {
1135 return open(__p.c_str(), __mode);
1137 # endif // _LIBCPP_STD_VER >= 17
1139 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1140 _LIBCPP_HIDE_FROM_ABI void close();
1143 basic_filebuf<char_type, traits_type> __sb_;
1146 template <class _CharT, class _Traits>
1147 inline basic_ifstream<_CharT, _Traits>::basic_ifstream()
1148 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {}
1150 template <class _CharT, class _Traits>
1151 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1152 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1153 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1154 this->setstate(ios_base::failbit);
1157 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1158 template <class _CharT, class _Traits>
1159 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1160 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1161 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1162 this->setstate(ios_base::failbit);
1167 template <class _CharT, class _Traits>
1168 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1169 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1170 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1171 this->setstate(ios_base::failbit);
1174 template <class _CharT, class _Traits>
1175 inline basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1176 : basic_istream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1177 this->set_rdbuf(std::addressof(__sb_));
1180 template <class _CharT, class _Traits>
1181 inline basic_ifstream<_CharT, _Traits>& basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) {
1182 basic_istream<char_type, traits_type>::operator=(std::move(__rhs));
1183 __sb_ = std::move(__rhs.__sb_);
1187 template <class _CharT, class _Traits>
1188 inline void basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) {
1189 basic_istream<char_type, traits_type>::swap(__rhs);
1190 __sb_.swap(__rhs.__sb_);
1193 template <class _CharT, class _Traits>
1194 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) {
1198 template <class _CharT, class _Traits>
1199 inline basic_filebuf<_CharT, _Traits>* basic_ifstream<_CharT, _Traits>::rdbuf() const {
1200 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1203 template <class _CharT, class _Traits>
1204 inline bool basic_ifstream<_CharT, _Traits>::is_open() const {
1205 return __sb_.is_open();
1208 template <class _CharT, class _Traits>
1209 void basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1210 if (__sb_.open(__s, __mode | ios_base::in))
1213 this->setstate(ios_base::failbit);
1216 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1217 template <class _CharT, class _Traits>
1218 void basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1219 if (__sb_.open(__s, __mode | ios_base::in))
1222 this->setstate(ios_base::failbit);
1226 template <class _CharT, class _Traits>
1227 void basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1228 if (__sb_.open(__s, __mode | ios_base::in))
1231 this->setstate(ios_base::failbit);
1234 template <class _CharT, class _Traits>
1235 inline void basic_ifstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1236 if (__sb_.__open(__fd, __mode | ios_base::in))
1239 this->setstate(ios_base::failbit);
1242 template <class _CharT, class _Traits>
1243 inline void basic_ifstream<_CharT, _Traits>::close() {
1244 if (__sb_.close() == 0)
1245 this->setstate(ios_base::failbit);
1250 template <class _CharT, class _Traits>
1251 class _LIBCPP_TEMPLATE_VIS basic_ofstream : public basic_ostream<_CharT, _Traits> {
1253 typedef _CharT char_type;
1254 typedef _Traits traits_type;
1255 typedef typename traits_type::int_type int_type;
1256 typedef typename traits_type::pos_type pos_type;
1257 typedef typename traits_type::off_type off_type;
1258 # if _LIBCPP_STD_VER >= 26
1259 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1262 _LIBCPP_HIDE_FROM_ABI basic_ofstream();
1263 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1264 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1265 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1267 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1269 # if _LIBCPP_STD_VER >= 17
1270 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1271 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY
1272 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const _Tp& __p, ios_base::openmode __mode = ios_base::out)
1273 : basic_ofstream(__p.c_str(), __mode) {}
1274 # endif // _LIBCPP_STD_VER >= 17
1276 _LIBCPP_HIDE_FROM_ABI basic_ofstream(basic_ofstream&& __rhs);
1277 _LIBCPP_HIDE_FROM_ABI basic_ofstream& operator=(basic_ofstream&& __rhs);
1278 _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream& __rhs);
1280 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1281 # if _LIBCPP_STD_VER >= 26
1282 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1284 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1285 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1286 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1287 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1289 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1291 # if _LIBCPP_STD_VER >= 17
1292 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1293 open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) {
1294 return open(__p.c_str(), __mode);
1296 # endif // _LIBCPP_STD_VER >= 17
1298 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1299 _LIBCPP_HIDE_FROM_ABI void close();
1302 basic_filebuf<char_type, traits_type> __sb_;
1305 template <class _CharT, class _Traits>
1306 inline basic_ofstream<_CharT, _Traits>::basic_ofstream()
1307 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {}
1309 template <class _CharT, class _Traits>
1310 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1311 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1312 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1313 this->setstate(ios_base::failbit);
1316 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1317 template <class _CharT, class _Traits>
1318 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1319 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1320 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1321 this->setstate(ios_base::failbit);
1326 template <class _CharT, class _Traits>
1327 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1328 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1329 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1330 this->setstate(ios_base::failbit);
1333 template <class _CharT, class _Traits>
1334 inline basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1335 : basic_ostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1336 this->set_rdbuf(std::addressof(__sb_));
1339 template <class _CharT, class _Traits>
1340 inline basic_ofstream<_CharT, _Traits>& basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) {
1341 basic_ostream<char_type, traits_type>::operator=(std::move(__rhs));
1342 __sb_ = std::move(__rhs.__sb_);
1346 template <class _CharT, class _Traits>
1347 inline void basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) {
1348 basic_ostream<char_type, traits_type>::swap(__rhs);
1349 __sb_.swap(__rhs.__sb_);
1352 template <class _CharT, class _Traits>
1353 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) {
1357 template <class _CharT, class _Traits>
1358 inline basic_filebuf<_CharT, _Traits>* basic_ofstream<_CharT, _Traits>::rdbuf() const {
1359 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1362 template <class _CharT, class _Traits>
1363 inline bool basic_ofstream<_CharT, _Traits>::is_open() const {
1364 return __sb_.is_open();
1367 template <class _CharT, class _Traits>
1368 void basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1369 if (__sb_.open(__s, __mode | ios_base::out))
1372 this->setstate(ios_base::failbit);
1375 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1376 template <class _CharT, class _Traits>
1377 void basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1378 if (__sb_.open(__s, __mode | ios_base::out))
1381 this->setstate(ios_base::failbit);
1385 template <class _CharT, class _Traits>
1386 void basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1387 if (__sb_.open(__s, __mode | ios_base::out))
1390 this->setstate(ios_base::failbit);
1393 template <class _CharT, class _Traits>
1394 inline void basic_ofstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1395 if (__sb_.__open(__fd, __mode | ios_base::out))
1398 this->setstate(ios_base::failbit);
1401 template <class _CharT, class _Traits>
1402 inline void basic_ofstream<_CharT, _Traits>::close() {
1403 if (__sb_.close() == nullptr)
1404 this->setstate(ios_base::failbit);
1409 template <class _CharT, class _Traits>
1410 class _LIBCPP_TEMPLATE_VIS basic_fstream : public basic_iostream<_CharT, _Traits> {
1412 typedef _CharT char_type;
1413 typedef _Traits traits_type;
1414 typedef typename traits_type::int_type int_type;
1415 typedef typename traits_type::pos_type pos_type;
1416 typedef typename traits_type::off_type off_type;
1417 # if _LIBCPP_STD_VER >= 26
1418 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1421 _LIBCPP_HIDE_FROM_ABI basic_fstream();
1422 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const char* __s,
1423 ios_base::openmode __mode = ios_base::in | ios_base::out);
1424 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1425 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const wchar_t* __s,
1426 ios_base::openmode __mode = ios_base::in | ios_base::out);
1428 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const string& __s,
1429 ios_base::openmode __mode = ios_base::in | ios_base::out);
1431 # if _LIBCPP_STD_VER >= 17
1432 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1433 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(
1434 const _Tp& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1435 : basic_fstream(__p.c_str(), __mode) {}
1436 # endif // _LIBCPP_STD_VER >= 17
1438 _LIBCPP_HIDE_FROM_ABI basic_fstream(basic_fstream&& __rhs);
1440 _LIBCPP_HIDE_FROM_ABI basic_fstream& operator=(basic_fstream&& __rhs);
1442 _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream& __rhs);
1444 _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1445 # if _LIBCPP_STD_VER >= 26
1446 _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1448 _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1449 _LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1450 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1451 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1453 _LIBCPP_HIDE_FROM_ABI void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1455 # if _LIBCPP_STD_VER >= 17
1456 _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1457 open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1458 return open(__p.c_str(), __mode);
1460 # endif // _LIBCPP_STD_VER >= 17
1462 _LIBCPP_HIDE_FROM_ABI void close();
1465 basic_filebuf<char_type, traits_type> __sb_;
1468 template <class _CharT, class _Traits>
1469 inline basic_fstream<_CharT, _Traits>::basic_fstream()
1470 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {}
1472 template <class _CharT, class _Traits>
1473 inline basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1474 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1475 if (__sb_.open(__s, __mode) == nullptr)
1476 this->setstate(ios_base::failbit);
1479 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1480 template <class _CharT, class _Traits>
1481 inline basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1482 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1483 if (__sb_.open(__s, __mode) == nullptr)
1484 this->setstate(ios_base::failbit);
1488 template <class _CharT, class _Traits>
1489 inline basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1490 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1491 if (__sb_.open(__s, __mode) == nullptr)
1492 this->setstate(ios_base::failbit);
1496 template <class _CharT, class _Traits>
1497 inline basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1498 : basic_iostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1499 this->set_rdbuf(std::addressof(__sb_));
1502 template <class _CharT, class _Traits>
1503 inline basic_fstream<_CharT, _Traits>& basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs) {
1504 basic_iostream<char_type, traits_type>::operator=(std::move(__rhs));
1505 __sb_ = std::move(__rhs.__sb_);
1509 template <class _CharT, class _Traits>
1510 inline void basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs) {
1511 basic_iostream<char_type, traits_type>::swap(__rhs);
1512 __sb_.swap(__rhs.__sb_);
1515 template <class _CharT, class _Traits>
1516 inline _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) {
1520 template <class _CharT, class _Traits>
1521 inline basic_filebuf<_CharT, _Traits>* basic_fstream<_CharT, _Traits>::rdbuf() const {
1522 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1525 template <class _CharT, class _Traits>
1526 inline bool basic_fstream<_CharT, _Traits>::is_open() const {
1527 return __sb_.is_open();
1530 template <class _CharT, class _Traits>
1531 void basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1532 if (__sb_.open(__s, __mode))
1535 this->setstate(ios_base::failbit);
1538 # if _LIBCPP_HAS_OPEN_WITH_WCHAR
1539 template <class _CharT, class _Traits>
1540 void basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1541 if (__sb_.open(__s, __mode))
1544 this->setstate(ios_base::failbit);
1548 template <class _CharT, class _Traits>
1549 void basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1550 if (__sb_.open(__s, __mode))
1553 this->setstate(ios_base::failbit);
1556 template <class _CharT, class _Traits>
1557 inline void basic_fstream<_CharT, _Traits>::close() {
1558 if (__sb_.close() == nullptr)
1559 this->setstate(ios_base::failbit);
1562 # if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1
1563 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1564 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1565 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
1568 _LIBCPP_END_NAMESPACE_STD
1570 # endif // _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
1574 # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1576 # include <concepts>
1582 # include <stdexcept>
1583 # include <type_traits>
1586 # if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 23
1587 # include <filesystem>
1589 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1591 #endif // _LIBCPP_FSTREAM