1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_FSTREAM
11#define _LIBCPP_FSTREAM
12
13/*
14 fstream synopsis
15
16template <class charT, class traits = char_traits<charT> >
17class basic_filebuf
18 : public basic_streambuf<charT, traits>
19{
20public:
21 typedef charT char_type;
22 typedef traits traits_type;
23 typedef typename traits_type::int_type int_type;
24 typedef typename traits_type::pos_type pos_type;
25 typedef typename traits_type::off_type off_type;
26
27 // 27.9.1.2 Constructors/destructor:
28 basic_filebuf();
29 basic_filebuf(basic_filebuf&& rhs);
30 virtual ~basic_filebuf();
31
32 // 27.9.1.3 Assign/swap:
33 basic_filebuf& operator=(basic_filebuf&& rhs);
34 void swap(basic_filebuf& rhs);
35
36 // 27.9.1.4 Members:
37 bool is_open() const;
38 basic_filebuf* open(const char* s, ios_base::openmode mode);
39 basic_filebuf* open(const string& s, ios_base::openmode mode);
40 basic_filebuf* open(const filesystem::path& p, ios_base::openmode mode); // C++17
41 basic_filebuf* close();
42
43protected:
44 // 27.9.1.5 Overridden virtual functions:
45 virtual streamsize showmanyc();
46 virtual int_type underflow();
47 virtual int_type uflow();
48 virtual int_type pbackfail(int_type c = traits_type::eof());
49 virtual int_type overflow (int_type c = traits_type::eof());
50 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* s, streamsize n);
51 virtual pos_type seekoff(off_type off, ios_base::seekdir way,
52 ios_base::openmode which = ios_base::in | ios_base::out);
53 virtual pos_type seekpos(pos_type sp,
54 ios_base::openmode which = ios_base::in | ios_base::out);
55 virtual int sync();
56 virtual void imbue(const locale& loc);
57};
58
59template <class charT, class traits>
60 void
61 swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
62
63typedef basic_filebuf<char> filebuf;
64typedef basic_filebuf<wchar_t> wfilebuf;
65
66template <class charT, class traits = char_traits<charT> >
67class basic_ifstream
68 : public basic_istream<charT,traits>
69{
70public:
71 typedef charT char_type;
72 typedef traits traits_type;
73 typedef typename traits_type::int_type int_type;
74 typedef typename traits_type::pos_type pos_type;
75 typedef typename traits_type::off_type off_type;
76 using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26
77
78 basic_ifstream();
79 explicit basic_ifstream(const char* s, ios_base::openmode mode = ios_base::in);
80 explicit basic_ifstream(const string& s, ios_base::openmode mode = ios_base::in);
81 template<class T>
82 explicit basic_ifstream(const T& s, ios_base::openmode mode = ios_base::in); // Since C++17
83 basic_ifstream(basic_ifstream&& rhs);
84
85 basic_ifstream& operator=(basic_ifstream&& rhs);
86 void swap(basic_ifstream& rhs);
87
88 basic_filebuf<char_type, traits_type>* rdbuf() const;
89 native_handle_type native_handle() const noexcept; // Since C++26
90 bool is_open() const;
91 void open(const char* s, ios_base::openmode mode = ios_base::in);
92 void open(const string& s, ios_base::openmode mode = ios_base::in);
93 void open(const filesystem::path& s, ios_base::openmode mode = ios_base::in); // C++17
94
95 void close();
96};
97
98template <class charT, class traits>
99 void
100 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
101
102typedef basic_ifstream<char> ifstream;
103typedef basic_ifstream<wchar_t> wifstream;
104
105template <class charT, class traits = char_traits<charT> >
106class basic_ofstream
107 : public basic_ostream<charT,traits>
108{
109public:
110 typedef charT char_type;
111 typedef traits traits_type;
112 typedef typename traits_type::int_type int_type;
113 typedef typename traits_type::pos_type pos_type;
114 typedef typename traits_type::off_type off_type;
115 using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26
116
117 basic_ofstream();
118 explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
119 explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
120 template<class T>
121 explicit basic_ofstream(const T& s, ios_base::openmode mode = ios_base::out); // Since C++17
122 basic_ofstream(basic_ofstream&& rhs);
123
124 basic_ofstream& operator=(basic_ofstream&& rhs);
125 void swap(basic_ofstream& rhs);
126
127 basic_filebuf<char_type, traits_type>* rdbuf() const;
128 native_handle_type native_handle() const noexcept; // Since C++26
129
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
135
136 void close();
137};
138
139template <class charT, class traits>
140 void
141 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
142
143typedef basic_ofstream<char> ofstream;
144typedef basic_ofstream<wchar_t> wofstream;
145
146template <class charT, class traits=char_traits<charT> >
147class basic_fstream
148 : public basic_iostream<charT,traits>
149{
150public:
151 typedef charT char_type;
152 typedef traits traits_type;
153 typedef typename traits_type::int_type int_type;
154 typedef typename traits_type::pos_type pos_type;
155 typedef typename traits_type::off_type off_type;
156 using native_handle_type = typename basic_filebuf<charT, traits>::native_handle_type; // Since C++26
157
158 basic_fstream();
159 explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
160 explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
161 template<class T>
162 explicit basic_fstream(const T& s, ios_base::openmode mode = ios_base::in | ios_base::out); // Since C++17
163 basic_fstream(basic_fstream&& rhs);
164
165 basic_fstream& operator=(basic_fstream&& rhs);
166 void swap(basic_fstream& rhs);
167
168 basic_filebuf<char_type, traits_type>* rdbuf() const;
169 native_handle_type native_handle() const noexcept; // Since C++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
175
176 void close();
177};
178
179template <class charT, class traits>
180 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
181
182typedef basic_fstream<char> fstream;
183typedef basic_fstream<wchar_t> wfstream;
184
185} // std
186
187*/
188
189#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
190# include <__cxx03/fstream>
191#else
192# include <__config>
193
194# if _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
195
196# include <__algorithm/max.h>
197# include <__assert>
198# include <__filesystem/path.h>
199# include <__fwd/fstream.h>
200# include <__locale_dir/codecvt.h>
201# include <__locale_dir/locale.h>
202# include <__memory/addressof.h>
203# include <__memory/unique_ptr.h>
204# include <__ostream/basic_ostream.h>
205# include <__type_traits/enable_if.h>
206# include <__type_traits/is_same.h>
207# include <__utility/move.h>
208# include <__utility/swap.h>
209# include <__utility/unreachable.h>
210# include <cstdio>
211# include <istream>
212# include <streambuf>
213# include <typeinfo>
214# include <version>
215
216# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
217# pragma GCC system_header
218# endif
219
220# if defined(_AIX)
221// On AIX, fopen64/fseeko64/ftello64 are not guaranteed to always be declared, so we declare them
222// here. We use `__asm__` to avoid colliding with the declaration in the header in any way.
223extern "C" FILE* __libcpp_fopen64(const char*, const char*) __asm__("fopen64");
224extern "C" long long __libcpp_ftello64(FILE*) __asm__("ftello64");
225extern "C" int __libcpp_fseeko64(FILE*, long long, int) __asm__("fseeko64");
226# endif
227
228_LIBCPP_PUSH_MACROS
229# include <__undef_macros>
230
231_LIBCPP_BEGIN_NAMESPACE_STD
232_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
233
234# if _LIBCPP_STD_VER >= 23 && defined(_WIN32)
235_LIBCPP_EXPORTED_FROM_ABI void* __filebuf_windows_native_handle(FILE* __file) noexcept;
236# endif
237
238template <class _CharT, class _Traits>
239class basic_filebuf : public basic_streambuf<_CharT, _Traits> {
240public:
241 typedef _CharT char_type;
242 typedef _Traits traits_type;
243 typedef typename traits_type::int_type int_type;
244 typedef typename traits_type::pos_type pos_type;
245 typedef typename traits_type::off_type off_type;
246 typedef typename traits_type::state_type state_type;
247# if _LIBCPP_STD_VER >= 26
248# if defined(_WIN32)
249 using native_handle_type = void*; // HANDLE
250# elif __has_include(<unistd.h>)
251 using native_handle_type = int; // POSIX file descriptor
252# else
253# error "Provide a native file handle!"
254# endif
255# endif
256
257 // 27.9.1.2 Constructors/destructor:
258 basic_filebuf();
259 basic_filebuf(basic_filebuf&& __rhs);
260 ~basic_filebuf() override;
261
262 // 27.9.1.3 Assign/swap:
263 _LIBCPP_HIDE_FROM_ABI basic_filebuf& operator=(basic_filebuf&& __rhs);
264 void swap(basic_filebuf& __rhs);
265
266 // 27.9.1.4 Members:
267 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool is_open() const;
268 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
269# if _LIBCPP_HAS_OPEN_WITH_WCHAR
270 basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
271# endif
272 _LIBCPP_HIDE_FROM_ABI basic_filebuf* open(const string& __s, ios_base::openmode __mode);
273
274# if _LIBCPP_STD_VER >= 17
275 _LIBCPP_HIDE_FROM_ABI basic_filebuf* open(const filesystem::path& __p, ios_base::openmode __mode) {
276 return open(__p.c_str(), __mode);
277 }
278# endif
279 _LIBCPP_HIDE_FROM_ABI basic_filebuf* __open(int __fd, ios_base::openmode __mode);
280 basic_filebuf* close();
281# if _LIBCPP_STD_VER >= 26
282 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
283 _LIBCPP_ASSERT_UNCATEGORIZED(this->is_open(), "File must be opened");
284# if defined(_WIN32)
285 return std::__filebuf_windows_native_handle(__file_);
286# elif __has_include(<unistd.h>)
287 return fileno(stream: __file_);
288# else
289# error "Provide a way to determine the file native handle!"
290# endif
291 }
292# endif // _LIBCPP_STD_VER >= 26
293
294 _LIBCPP_HIDE_FROM_ABI inline static const char* __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
295# if _LIBCPP_HAS_OPEN_WITH_WCHAR
296 _LIBCPP_HIDE_FROM_ABI inline static const wchar_t* __make_mdwstring(ios_base::openmode __mode) _NOEXCEPT;
297# endif
298
299protected:
300 // 27.9.1.5 Overridden virtual functions:
301 int_type underflow() override;
302 int_type pbackfail(int_type __c = traits_type::eof()) override;
303 int_type overflow(int_type __c = traits_type::eof()) override;
304 basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override;
305 pos_type
306 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
307 pos_type seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
308 int sync() override;
309 void imbue(const locale& __loc) override;
310
311 _LIBCPP_HIDE_FROM_ABI_VIRTUAL streamsize xsputn(const char_type* __str, streamsize __len) override {
312 if (__always_noconv_ && __len >= (this->epptr() - this->pbase())) {
313 if (traits_type::eq_int_type(overflow(), traits_type::eof()))
314 return 0;
315
316 return std::fwrite(ptr: __str, size: sizeof(char_type), n: __len, s: __file_);
317 }
318 return basic_streambuf<_CharT, _Traits>::xsputn(__str, __len);
319 }
320
321 _LIBCPP_HIDE_FROM_ABI_VIRTUAL streamsize xsgetn(char_type* __str, streamsize __len) override {
322 if (__file_ && __always_noconv_) {
323 char_type __1buf;
324 __read_guard __guard(__1buf, *this);
325 const streamsize __n = std::min(this->egptr() - this->gptr(), __len);
326 if (__n != 0) {
327 traits_type::copy(__str, this->gptr(), __n);
328 this->__gbump_ptrdiff(__n);
329 }
330 const streamsize __remainder = __len - __n;
331 const streamsize __buffer_space = this->egptr() - this->eback();
332
333 if (__remainder >= __buffer_space) {
334 auto __res = std::fread(ptr: __str + __n, size: sizeof(char_type), n: __remainder, stream: __file_) + __n;
335
336 // Copy the buffer-sized tail into the buffer so that `unget`ting works correctly
337 auto __buf_size = std::min<size_t>(__res, this->egptr() - this->eback());
338 traits_type::copy(this->eback(), __str + __res - __buf_size, __buf_size);
339 this->setg(this->eback(), this->eback() + __buf_size, this->eback() + __buf_size);
340
341 return __res;
342 } else if (__remainder > 0)
343 return basic_streambuf<_CharT, _Traits>::xsgetn(__str + __n, __remainder) + __n;
344 return __n;
345 }
346 return basic_streambuf<_CharT, _Traits>::xsgetn(__str, __len);
347 }
348
349private:
350 char* __extbuf_;
351 const char* __extbufnext_;
352 const char* __extbufend_;
353 char __extbuf_min_[8];
354 size_t __ebs_;
355 char_type* __intbuf_;
356 size_t __ibs_;
357 FILE* __file_;
358 const codecvt<char_type, char, state_type>* __cv_;
359 state_type __st_;
360 state_type __st_last_;
361 ios_base::openmode __om_;
362 // There have been no file operations yet, which allows setting unbuffered
363 // I/O mode.
364 static const ios_base::openmode __no_io_operations = ios_base::trunc;
365 // Unbuffered I/O mode has been requested.
366 static const ios_base::openmode __use_unbuffered_io = ios_base::ate;
367 // Used to track the currently used mode and track whether the output should
368 // be unbuffered.
369 // [filebuf.virtuals]/12
370 // If setbuf(0, 0) is called on a stream before any I/O has occurred on
371 // that stream, the stream becomes unbuffered. Otherwise the results are
372 // implementation-defined.
373 // This allows calling setbuf(0, 0)
374 // - before opening a file,
375 // - after opening a file, before
376 // - a read
377 // - a write
378 // - a seek.
379 // Note that opening a file with ios_base::ate does a seek operation.
380 // Normally underflow, overflow, and sync change this flag to ios_base::in,
381 // ios_base_out, or 0.
382 //
383 // The ios_base::trunc and ios_base::ate flags are not used in __cm_. They
384 // are used to track the state of the unbuffered request. For readability
385 // they have the aliases __no_io_operations and __use_unbuffered_io
386 // respectively.
387 //
388 // The __no_io_operations and __use_unbuffered_io flags are used in the
389 // following way:
390 // - __no_io_operations is set upon construction to indicate the unbuffered
391 // state can be set.
392 // - When requesting unbuffered output:
393 // - If the file is open it sets the mode.
394 // - Else places a request by adding the __use_unbuffered_io flag.
395 // - When a file is opened it checks whether both __no_io_operations and
396 // __use_unbuffered_io are set. If so switches to unbuffered mode.
397 // - All file I/O operations change the mode effectively clearing the
398 // __no_io_operations and __use_unbuffered_io flags.
399 ios_base::openmode __cm_;
400 bool __owns_eb_;
401 bool __owns_ib_;
402 bool __always_noconv_;
403
404 bool __read_mode();
405
406 struct [[__nodiscard__]] __read_guard {
407 basic_filebuf& __self_;
408 size_t __unget_size_;
409 char_type& __1buf_;
410
411 _LIBCPP_HIDE_FROM_ABI __read_guard(char_type& __1buf, basic_filebuf& __self) : __self_(__self), __1buf_(__1buf) {
412 if (__self.__cm_ & ios_base::in) {
413 __unget_size_ = std::min<size_t>((__self.egptr() - __self.eback()) / 2, 4);
414 } else {
415 __self.setp(nullptr, nullptr);
416 if (__self.__always_noconv_) {
417 __self.setg(reinterpret_cast<char_type*>(__self.__extbuf_),
418 reinterpret_cast<char_type*>(__self.__extbuf_) + __self.__ebs_,
419 reinterpret_cast<char_type*>(__self.__extbuf_) + __self.__ebs_);
420 } else {
421 __self.setg(__self.__intbuf_, __self.__intbuf_ + __self.__ibs_, __self.__intbuf_ + __self.__ibs_);
422 }
423 __self.__cm_ = ios_base::in;
424 __unget_size_ = 0;
425 }
426
427 if (__self.gptr() == nullptr)
428 __self.setg(std::addressof(__1buf_), std::addressof(__1buf_) + 1, std::addressof(__1buf_) + 1);
429 }
430
431 _LIBCPP_HIDE_FROM_ABI size_t __unget_size() { return __unget_size_; }
432
433 _LIBCPP_HIDE_FROM_ABI ~__read_guard() {
434 if (__self_.eback() == std::addressof(__1buf_))
435 __self_.setg(nullptr, nullptr, nullptr);
436 }
437 };
438
439 void __write_mode();
440
441 _LIBCPP_HIDE_FROM_ABI static int __fseek(FILE* __file, pos_type __offset, int __whence);
442 _LIBCPP_HIDE_FROM_ABI static pos_type __ftell(FILE* __file);
443 _LIBCPP_HIDE_FROM_ABI static FILE* __fopen(const char* __name, const char* __mode);
444
445 _LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);
446
447public:
448 // Update the buffer so that __file is considered to be the underlying file and is opened as __mode. This is used in
449 // the open family of functions as well as various iostreams in sync_with_stdio(false) mode.
450 _LIBCPP_HIDE_FROM_ABI basic_filebuf* __adopt_file(FILE* __file, ios_base::openmode __mode) {
451 __file_ = __file;
452 if (!__file_)
453 return nullptr;
454
455 __om_ = __mode;
456 if (__cm_ == (__no_io_operations | __use_unbuffered_io)) {
457 std::setbuf(stream: __file_, buf: nullptr);
458 __cm_ = 0;
459 }
460
461 if (__mode & ios_base::ate) {
462 __cm_ = 0;
463 if (fseek(stream: __file_, off: 0, SEEK_END)) {
464 fclose(stream: __file_);
465 __file_ = nullptr;
466 return nullptr;
467 }
468 }
469
470 return this;
471 }
472
473private:
474 // If the file is already open, switch to unbuffered mode. Otherwise, record
475 // the request to use unbuffered mode so that we use that mode when we
476 // eventually open the file.
477 _LIBCPP_HIDE_FROM_ABI void __request_unbuffered_mode() {
478 if (__file_) {
479 std::setbuf(stream: __file_, buf: nullptr);
480 __cm_ = 0;
481 } else {
482 __cm_ = __no_io_operations | __use_unbuffered_io;
483 }
484 }
485
486 _LIBCPP_HIDE_FROM_ABI typename traits_type::int_type __overflow_failed() {
487 if (this->pptr() == this->epptr() + 1) {
488 this->pbump(-1); // lose the character we overflowed above -- we don't really have a
489 // choice since we couldn't commit the contents of the put area
490 }
491 return traits_type::eof();
492 }
493};
494
495template <class _CharT, class _Traits>
496basic_filebuf<_CharT, _Traits>::basic_filebuf()
497 : __extbuf_(nullptr),
498 __extbufnext_(nullptr),
499 __extbufend_(nullptr),
500 __ebs_(0),
501 __intbuf_(nullptr),
502 __ibs_(0),
503 __file_(nullptr),
504 __cv_(nullptr),
505 __st_(),
506 __st_last_(),
507 __om_(0),
508 __cm_(__no_io_operations),
509 __owns_eb_(false),
510 __owns_ib_(false),
511 __always_noconv_(false) {
512 if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc())) {
513 __cv_ = std::addressof(std::use_facet<codecvt<char_type, char, state_type> >(this->getloc()));
514 __always_noconv_ = __cv_->always_noconv();
515 }
516 setbuf(s: nullptr, n: 4096);
517}
518
519template <class _CharT, class _Traits>
520basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) : basic_streambuf<_CharT, _Traits>(__rhs) {
521 if (__rhs.__extbuf_ == __rhs.__extbuf_min_) {
522 __extbuf_ = __extbuf_min_;
523 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
524 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
525 } else {
526 __extbuf_ = __rhs.__extbuf_;
527 __extbufnext_ = __rhs.__extbufnext_;
528 __extbufend_ = __rhs.__extbufend_;
529 }
530 __ebs_ = __rhs.__ebs_;
531 __intbuf_ = __rhs.__intbuf_;
532 __ibs_ = __rhs.__ibs_;
533 __file_ = __rhs.__file_;
534 __cv_ = __rhs.__cv_;
535 __st_ = __rhs.__st_;
536 __st_last_ = __rhs.__st_last_;
537 __om_ = __rhs.__om_;
538 __cm_ = __rhs.__cm_;
539 __owns_eb_ = __rhs.__owns_eb_;
540 __owns_ib_ = __rhs.__owns_ib_;
541 __always_noconv_ = __rhs.__always_noconv_;
542 if (__rhs.pbase()) {
543 if (__rhs.pbase() == __rhs.__intbuf_)
544 this->setp(__intbuf_, __intbuf_ + (__rhs.epptr() - __rhs.pbase()));
545 else
546 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__rhs.epptr() - __rhs.pbase()));
547 this->__pbump(__rhs.pptr() - __rhs.pbase());
548 } else if (__rhs.eback()) {
549 if (__rhs.eback() == __rhs.__intbuf_)
550 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), __intbuf_ + (__rhs.egptr() - __rhs.eback()));
551 else
552 this->setg((char_type*)__extbuf_,
553 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
554 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
555 }
556 __rhs.__extbuf_ = nullptr;
557 __rhs.__extbufnext_ = nullptr;
558 __rhs.__extbufend_ = nullptr;
559 __rhs.__ebs_ = 0;
560 __rhs.__intbuf_ = 0;
561 __rhs.__ibs_ = 0;
562 __rhs.__file_ = nullptr;
563 __rhs.__st_ = state_type();
564 __rhs.__st_last_ = state_type();
565 __rhs.__om_ = 0;
566 __rhs.__cm_ = 0;
567 __rhs.__owns_eb_ = false;
568 __rhs.__owns_ib_ = false;
569 __rhs.setg(0, 0, 0);
570 __rhs.setp(0, 0);
571}
572
573template <class _CharT, class _Traits>
574inline basic_filebuf<_CharT, _Traits>& basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) {
575 close();
576 swap(__rhs);
577 return *this;
578}
579
580template <class _CharT, class _Traits>
581basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
582# if _LIBCPP_HAS_EXCEPTIONS
583 try {
584# endif // _LIBCPP_HAS_EXCEPTIONS
585 close();
586# if _LIBCPP_HAS_EXCEPTIONS
587 } catch (...) {
588 }
589# endif // _LIBCPP_HAS_EXCEPTIONS
590 if (__owns_eb_)
591 delete[] __extbuf_;
592 if (__owns_ib_)
593 delete[] __intbuf_;
594}
595
596template <class _CharT, class _Traits>
597void basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) {
598 basic_streambuf<char_type, traits_type>::swap(__rhs);
599 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
600 // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
601 std::swap(x&: __extbuf_, y&: __rhs.__extbuf_);
602 std::swap(x&: __extbufnext_, y&: __rhs.__extbufnext_);
603 std::swap(x&: __extbufend_, y&: __rhs.__extbufend_);
604 } else {
605 ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
606 ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
607 ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
608 ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
609 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
610 // *this uses the small buffer, but __rhs doesn't.
611 __extbuf_ = __rhs.__extbuf_;
612 __rhs.__extbuf_ = __rhs.__extbuf_min_;
613 std::memmove(dest: __rhs.__extbuf_min_, src: __extbuf_min_, n: sizeof(__extbuf_min_));
614 } else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) {
615 // *this doesn't use the small buffer, but __rhs does.
616 __rhs.__extbuf_ = __extbuf_;
617 __extbuf_ = __extbuf_min_;
618 std::memmove(dest: __extbuf_min_, src: __rhs.__extbuf_min_, n: sizeof(__extbuf_min_));
619 } else {
620 // Both *this and __rhs use the small buffer.
621 char __tmp[sizeof(__extbuf_min_)];
622 std::memmove(dest: __tmp, src: __extbuf_min_, n: sizeof(__extbuf_min_));
623 std::memmove(dest: __extbuf_min_, src: __rhs.__extbuf_min_, n: sizeof(__extbuf_min_));
624 std::memmove(dest: __rhs.__extbuf_min_, src: __tmp, n: sizeof(__extbuf_min_));
625 }
626 __extbufnext_ = __extbuf_ + __rn;
627 __extbufend_ = __extbuf_ + __re;
628 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
629 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
630 }
631 std::swap(x&: __ebs_, y&: __rhs.__ebs_);
632 std::swap(__intbuf_, __rhs.__intbuf_);
633 std::swap(x&: __ibs_, y&: __rhs.__ibs_);
634 std::swap(x&: __file_, y&: __rhs.__file_);
635 std::swap(__cv_, __rhs.__cv_);
636 std::swap(__st_, __rhs.__st_);
637 std::swap(__st_last_, __rhs.__st_last_);
638 std::swap(x&: __om_, y&: __rhs.__om_);
639 std::swap(x&: __cm_, y&: __rhs.__cm_);
640 std::swap(x&: __owns_eb_, y&: __rhs.__owns_eb_);
641 std::swap(x&: __owns_ib_, y&: __rhs.__owns_ib_);
642 std::swap(x&: __always_noconv_, y&: __rhs.__always_noconv_);
643 if (this->eback() == (char_type*)__rhs.__extbuf_min_) {
644 ptrdiff_t __n = this->gptr() - this->eback();
645 ptrdiff_t __e = this->egptr() - this->eback();
646 this->setg((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __n, (char_type*)__extbuf_min_ + __e);
647 } else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) {
648 ptrdiff_t __n = this->pptr() - this->pbase();
649 ptrdiff_t __e = this->epptr() - this->pbase();
650 this->setp((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __e);
651 this->__pbump(__n);
652 }
653 if (__rhs.eback() == (char_type*)__extbuf_min_) {
654 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
655 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
656 __rhs.setg(
657 (char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __n, (char_type*)__rhs.__extbuf_min_ + __e);
658 } else if (__rhs.pbase() == (char_type*)__extbuf_min_) {
659 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
660 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
661 __rhs.setp((char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __e);
662 __rhs.__pbump(__n);
663 }
664}
665
666template <class _CharT, class _Traits>
667inline _LIBCPP_HIDE_FROM_ABI void swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) {
668 __x.swap(__y);
669}
670
671template <class _CharT, class _Traits>
672inline bool basic_filebuf<_CharT, _Traits>::is_open() const {
673 return __file_ != nullptr;
674}
675
676// Configures the fopen close-on-exec mode character, if any. This string will
677// be appended to any mode string used by fstream for fopen/fdopen.
678//
679// Not all platforms support this, but it helps avoid fd-leaks on platforms that
680// do.
681# if defined(__BIONIC__)
682# define _LIBCPP_FOPEN_CLOEXEC_MODE "e"
683# else
684# define _LIBCPP_FOPEN_CLOEXEC_MODE
685# endif
686
687template <class _CharT, class _Traits>
688const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(ios_base::openmode __mode) _NOEXCEPT {
689 switch (__mode & ~ios_base::ate) {
690 case ios_base::out:
691 case ios_base::out | ios_base::trunc:
692 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
693 case ios_base::out | ios_base::app:
694 case ios_base::app:
695 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
696 case ios_base::in:
697 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
698 case ios_base::in | ios_base::out:
699 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
700 case ios_base::in | ios_base::out | ios_base::trunc:
701 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
702 case ios_base::in | ios_base::out | ios_base::app:
703 case ios_base::in | ios_base::app:
704 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
705 case ios_base::out | ios_base::binary:
706 case ios_base::out | ios_base::trunc | ios_base::binary:
707 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
708 case ios_base::out | ios_base::app | ios_base::binary:
709 case ios_base::app | ios_base::binary:
710 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
711 case ios_base::in | ios_base::binary:
712 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
713 case ios_base::in | ios_base::out | ios_base::binary:
714 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
715 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
716 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
717 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
718 case ios_base::in | ios_base::app | ios_base::binary:
719 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
720# if _LIBCPP_STD_VER >= 23
721 case ios_base::out | ios_base::noreplace:
722 case ios_base::out | ios_base::trunc | ios_base::noreplace:
723 return "wx" _LIBCPP_FOPEN_CLOEXEC_MODE;
724 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
725 return "w+x" _LIBCPP_FOPEN_CLOEXEC_MODE;
726 case ios_base::out | ios_base::binary | ios_base::noreplace:
727 case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
728 return "wbx" _LIBCPP_FOPEN_CLOEXEC_MODE;
729 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
730 return "w+bx" _LIBCPP_FOPEN_CLOEXEC_MODE;
731# endif // _LIBCPP_STD_VER >= 23
732 default:
733 return nullptr;
734 }
735 __libcpp_unreachable();
736}
737
738# if _LIBCPP_HAS_OPEN_WITH_WCHAR
739template <class _CharT, class _Traits>
740const wchar_t* basic_filebuf<_CharT, _Traits>::__make_mdwstring(ios_base::openmode __mode) _NOEXCEPT {
741 switch (__mode & ~ios_base::ate) {
742 case ios_base::out:
743 case ios_base::out | ios_base::trunc:
744 return L"w";
745 case ios_base::out | ios_base::app:
746 case ios_base::app:
747 return L"a";
748 case ios_base::in:
749 return L"r";
750 case ios_base::in | ios_base::out:
751 return L"r+";
752 case ios_base::in | ios_base::out | ios_base::trunc:
753 return L"w+";
754 case ios_base::in | ios_base::out | ios_base::app:
755 case ios_base::in | ios_base::app:
756 return L"a+";
757 case ios_base::out | ios_base::binary:
758 case ios_base::out | ios_base::trunc | ios_base::binary:
759 return L"wb";
760 case ios_base::out | ios_base::app | ios_base::binary:
761 case ios_base::app | ios_base::binary:
762 return L"ab";
763 case ios_base::in | ios_base::binary:
764 return L"rb";
765 case ios_base::in | ios_base::out | ios_base::binary:
766 return L"r+b";
767 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
768 return L"w+b";
769 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
770 case ios_base::in | ios_base::app | ios_base::binary:
771 return L"a+b";
772# if _LIBCPP_STD_VER >= 23
773 case ios_base::out | ios_base::noreplace:
774 case ios_base::out | ios_base::trunc | ios_base::noreplace:
775 return L"wx";
776 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
777 return L"w+x";
778 case ios_base::out | ios_base::binary | ios_base::noreplace:
779 case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
780 return L"wbx";
781 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
782 return L"w+bx";
783# endif // _LIBCPP_STD_VER >= 23
784 default:
785 return nullptr;
786 }
787 __libcpp_unreachable();
788}
789# endif
790
791template <class _CharT, class _Traits>
792FILE* basic_filebuf<_CharT, _Traits>::__fopen(const char* __name, const char* __mode) {
793# if defined(_AIX)
794 return ::__libcpp_fopen64(__name, __mode);
795# elif defined(__GLIBC__)
796 return ::fopen64(filename: __name, modes: __mode);
797# else
798 return std::fopen(__name, __mode);
799# endif
800}
801
802template <class _CharT, class _Traits>
803basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
804 if (__file_)
805 return nullptr;
806 const char* __mdstr = __make_mdstring(__mode);
807 if (!__mdstr)
808 return nullptr;
809
810 return __adopt_file(file: __fopen(name: __s, mode: __mdstr), __mode);
811}
812
813template <class _CharT, class _Traits>
814inline basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
815 if (__file_)
816 return nullptr;
817 const char* __mdstr = __make_mdstring(__mode);
818 if (!__mdstr)
819 return nullptr;
820
821 return __adopt_file(file: fdopen(__fd, modes: __mdstr), __mode);
822}
823
824# if _LIBCPP_HAS_OPEN_WITH_WCHAR
825// This is basically the same as the char* overload except that it uses _wfopen
826// and long mode strings.
827template <class _CharT, class _Traits>
828basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
829 if (__file_)
830 return nullptr;
831 const wchar_t* __mdstr = __make_mdwstring(__mode);
832 if (!__mdstr)
833 return nullptr;
834
835 return __adopt_file(_wfopen(__s, __mdstr), __mode);
836}
837# endif
838
839template <class _CharT, class _Traits>
840inline basic_filebuf<_CharT, _Traits>*
841basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
842 return open(__s.c_str(), __mode);
843}
844
845template <class _CharT, class _Traits>
846basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::close() {
847 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
848 if (__file_) {
849 __rt = this;
850 unique_ptr<FILE, int (*)(FILE*)> __h(__file_, fclose);
851 if (sync())
852 __rt = nullptr;
853 if (fclose(stream: __h.release()))
854 __rt = nullptr;
855 __file_ = nullptr;
856 // Reset the get and the put areas without getting rid of the underlying buffers,
857 // which might have been configured by the user. Make sure to keep the buffers
858 // since the user may re-open the stream.
859 this->setg(nullptr, nullptr, nullptr);
860 this->setp(nullptr, nullptr);
861 __cm_ = __no_io_operations;
862 }
863 return __rt;
864}
865
866template <class _CharT, class _Traits>
867typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::underflow() {
868 if (__file_ == nullptr)
869 return traits_type::eof();
870
871 char_type __1buf;
872 __read_guard __guard(__1buf, *this);
873
874 const size_t __unget_sz = __guard.__unget_size();
875 int_type __c = traits_type::eof();
876 if (this->gptr() == this->egptr()) {
877 std::memmove(dest: this->eback(), src: this->egptr() - __unget_sz, n: __unget_sz * sizeof(char_type));
878 if (__always_noconv_) {
879 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
880 __nmemb = std::fread(ptr: this->eback() + __unget_sz, size: 1, n: __nmemb, stream: __file_);
881 if (__nmemb != 0) {
882 this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);
883 __c = traits_type::to_int_type(*this->gptr());
884 }
885 } else {
886 if (__extbufend_ != __extbufnext_) {
887 _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");
888 _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");
889 std::memmove(dest: __extbuf_, src: __extbufnext_, n: __extbufend_ - __extbufnext_);
890 }
891 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
892 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
893 size_t __nmemb =
894 std::min(a: static_cast<size_t>(__ibs_ - __unget_sz), b: static_cast<size_t>(__extbufend_ - __extbufnext_));
895 codecvt_base::result __r;
896 __st_last_ = __st_;
897 size_t __nr = std::fread(ptr: (void*)const_cast<char*>(__extbufnext_), size: 1, n: __nmemb, stream: __file_);
898 if (__nr != 0) {
899 if (!__cv_)
900 std::__throw_bad_cast();
901
902 __extbufend_ = __extbufnext_ + __nr;
903 char_type* __inext;
904 __r = __cv_->in(
905 __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->eback() + __ibs_, __inext);
906 if (__r == codecvt_base::noconv) {
907 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));
908 __c = traits_type::to_int_type(*this->gptr());
909 } else if (__inext != this->eback() + __unget_sz) {
910 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
911 __c = traits_type::to_int_type(*this->gptr());
912 }
913 }
914 }
915 } else
916 __c = traits_type::to_int_type(*this->gptr());
917 return __c;
918}
919
920template <class _CharT, class _Traits>
921typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
922 if (__file_ && this->eback() < this->gptr()) {
923 if (traits_type::eq_int_type(__c, traits_type::eof())) {
924 this->gbump(-1);
925 return traits_type::not_eof(__c);
926 }
927 if ((__om_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {
928 this->gbump(-1);
929 *this->gptr() = traits_type::to_char_type(__c);
930 return __c;
931 }
932 }
933 return traits_type::eof();
934}
935
936template <class _CharT, class _Traits>
937typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
938 if (__file_ == nullptr)
939 return traits_type::eof();
940 __write_mode();
941 char_type __1buf;
942 char_type* __pb_save = this->pbase();
943 char_type* __epb_save = this->epptr();
944 if (!traits_type::eq_int_type(__c, traits_type::eof())) {
945 if (this->pptr() == nullptr)
946 this->setp(std::addressof(__1buf), std::addressof(__1buf) + 1);
947 *this->pptr() = traits_type::to_char_type(__c);
948 this->pbump(1);
949 }
950
951 // There is nothing to write, early return
952 if (this->pptr() == this->pbase()) {
953 return traits_type::not_eof(__c);
954 }
955
956 if (__always_noconv_) {
957 size_t __n = static_cast<size_t>(this->pptr() - this->pbase());
958 if (std::fwrite(ptr: this->pbase(), size: sizeof(char_type), __n, s: __file_) != __n) {
959 return __overflow_failed();
960 }
961 } else {
962 if (!__cv_)
963 std::__throw_bad_cast();
964
965 // See [filebuf.virtuals]
966 char_type* __b = this->pbase();
967 char_type* __p = this->pptr();
968 const char_type* __end;
969 char* __extbuf_end = __extbuf_;
970 do {
971 codecvt_base::result __r = __cv_->out(__st_, __b, __p, __end, __extbuf_, __extbuf_ + __ebs_, __extbuf_end);
972 if (__end == __b) {
973 return __overflow_failed();
974 }
975
976 // No conversion needed: output characters directly to the file, done.
977 if (__r == codecvt_base::noconv) {
978 size_t __n = static_cast<size_t>(__p - __b);
979 if (std::fwrite(ptr: __b, size: 1, __n, s: __file_) != __n) {
980 return __overflow_failed();
981 }
982 break;
983
984 // Conversion successful: output the converted characters to the file, done.
985 } else if (__r == codecvt_base::ok) {
986 size_t __n = static_cast<size_t>(__extbuf_end - __extbuf_);
987 if (std::fwrite(ptr: __extbuf_, size: 1, __n, s: __file_) != __n) {
988 return __overflow_failed();
989 }
990 break;
991
992 // Conversion partially successful: output converted characters to the file and repeat with the
993 // remaining characters.
994 } else if (__r == codecvt_base::partial) {
995 size_t __n = static_cast<size_t>(__extbuf_end - __extbuf_);
996 if (std::fwrite(ptr: __extbuf_, size: 1, __n, s: __file_) != __n) {
997 return __overflow_failed();
998 }
999 __b = const_cast<char_type*>(__end);
1000 continue;
1001
1002 } else {
1003 return __overflow_failed();
1004 }
1005 } while (true);
1006 }
1007 this->setp(__pb_save, __epb_save);
1008 return traits_type::not_eof(__c);
1009}
1010
1011template <class _CharT, class _Traits>
1012basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {
1013 this->setg(nullptr, nullptr, nullptr);
1014 this->setp(nullptr, nullptr);
1015 // Calling setbuf(nullptr, 0) before any i/o operation switches the stream to unbuffered mode
1016 if (__cm_ == __no_io_operations && __s == nullptr && __n == 0)
1017 __request_unbuffered_mode();
1018 if (__owns_eb_)
1019 delete[] __extbuf_;
1020 if (__owns_ib_)
1021 delete[] __intbuf_;
1022 __ebs_ = __n;
1023 if (__ebs_ > sizeof(__extbuf_min_)) {
1024 if (__always_noconv_ && __s) {
1025 __extbuf_ = (char*)__s;
1026 __owns_eb_ = false;
1027 } else {
1028 __extbuf_ = new char[__ebs_];
1029 __owns_eb_ = true;
1030 }
1031 } else {
1032 __extbuf_ = __extbuf_min_;
1033 __ebs_ = sizeof(__extbuf_min_);
1034 __owns_eb_ = false;
1035 }
1036 if (!__always_noconv_) {
1037 __ibs_ = max<streamsize>(a: __n, b: sizeof(__extbuf_min_));
1038 if (__s && __ibs_ > sizeof(__extbuf_min_)) {
1039 __intbuf_ = __s;
1040 __owns_ib_ = false;
1041 } else {
1042 __intbuf_ = new char_type[__ibs_];
1043 __owns_ib_ = true;
1044 }
1045 } else {
1046 __ibs_ = 0;
1047 __intbuf_ = nullptr;
1048 __owns_ib_ = false;
1049 }
1050 return this;
1051}
1052
1053template <class _CharT, class _Traits>
1054typename basic_filebuf<_CharT, _Traits>::pos_type
1055basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) {
1056 if (!__cv_)
1057 std::__throw_bad_cast();
1058
1059 int __width = __cv_->encoding();
1060 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
1061 return pos_type(off_type(-1));
1062 // __width > 0 || __off == 0
1063 int __whence;
1064 switch (__way) {
1065 case ios_base::beg:
1066 __whence = SEEK_SET;
1067 break;
1068 case ios_base::cur:
1069 __whence = SEEK_CUR;
1070 break;
1071 case ios_base::end:
1072 __whence = SEEK_END;
1073 break;
1074 default:
1075 return pos_type(off_type(-1));
1076 }
1077 if (__fseek(file: __file_, offset: __width > 0 ? __width * __off : 0, __whence))
1078 return pos_type(off_type(-1));
1079 pos_type __r = __ftell(file: __file_);
1080 __r.state(__st_);
1081 return __r;
1082}
1083
1084template <class _CharT, class _Traits>
1085int basic_filebuf<_CharT, _Traits>::__fseek(FILE* __file, pos_type __offset, int __whence) {
1086# ifdef _WIN32
1087 return _fseeki64(__file, __offset, __whence);
1088# elif _LIBCPP_LIBC_NEWLIB
1089 return fseek(__file, __offset, __whence);
1090# elif defined(_AIX)
1091 return ::__libcpp_fseeko64(__file, __offset, __whence);
1092# elif defined(__GLIBC__)
1093 return ::fseeko64(stream: __file, off: __offset, __whence);
1094# else
1095 return ::fseeko(__file, __offset, __whence);
1096# endif
1097}
1098
1099template <class _CharT, class _Traits>
1100typename basic_filebuf<_CharT, _Traits>::pos_type basic_filebuf<_CharT, _Traits>::__ftell(FILE* __file) {
1101# ifdef _WIN32
1102 return _ftelli64(__file);
1103# elif _LIBCPP_LIBC_NEWLIB
1104 return ftell(__file);
1105# elif defined(_AIX)
1106 return ::__libcpp_ftello64(__file);
1107# elif defined(__GLIBC__)
1108 return ::ftello64(stream: __file);
1109# else
1110 return ftello(__file);
1111# endif
1112}
1113
1114template <class _CharT, class _Traits>
1115typename basic_filebuf<_CharT, _Traits>::pos_type
1116basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {
1117 if (__file_ == nullptr || sync())
1118 return pos_type(off_type(-1));
1119 if (__fseek(file: __file_, offset: __sp, SEEK_SET))
1120 return pos_type(off_type(-1));
1121 __st_ = __sp.state();
1122 return __sp;
1123}
1124
1125template <class _CharT, class _Traits>
1126int basic_filebuf<_CharT, _Traits>::sync() {
1127 if (__file_ == nullptr)
1128 return 0;
1129 if (!__cv_)
1130 std::__throw_bad_cast();
1131
1132 if (__cm_ & ios_base::out) {
1133 if (this->pptr() != this->pbase())
1134 if (overflow() == traits_type::eof())
1135 return -1;
1136 codecvt_base::result __r;
1137 do {
1138 char* __extbe;
1139 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
1140 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
1141 if (std::fwrite(ptr: __extbuf_, size: 1, n: __nmemb, s: __file_) != __nmemb)
1142 return -1;
1143 } while (__r == codecvt_base::partial);
1144 if (__r == codecvt_base::error)
1145 return -1;
1146 if (std::fflush(stream: __file_))
1147 return -1;
1148 } else if (__cm_ & ios_base::in) {
1149 off_type __c;
1150 state_type __state = __st_last_;
1151 bool __update_st = false;
1152 if (__always_noconv_)
1153 __c = this->egptr() - this->gptr();
1154 else {
1155 int __width = __cv_->encoding();
1156 __c = __extbufend_ - __extbufnext_;
1157 if (__width > 0)
1158 __c += __width * (this->egptr() - this->gptr());
1159 else {
1160 if (this->gptr() != this->egptr()) {
1161 const int __off = __cv_->length(__state, __extbuf_, __extbufnext_, this->gptr() - this->eback());
1162 __c += __extbufnext_ - __extbuf_ - __off;
1163 __update_st = true;
1164 }
1165 }
1166 }
1167 if (__fseek(file: __file_, offset: -__c, SEEK_CUR))
1168 return -1;
1169 if (__update_st)
1170 __st_ = __state;
1171 __extbufnext_ = __extbufend_ = __extbuf_;
1172 this->setg(nullptr, nullptr, nullptr);
1173 __cm_ = 0;
1174 }
1175 return 0;
1176}
1177
1178template <class _CharT, class _Traits>
1179void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
1180 sync();
1181 __cv_ = std::addressof(std::use_facet<codecvt<char_type, char, state_type> >(__loc));
1182 bool __old_anc = __always_noconv_;
1183 __always_noconv_ = __cv_->always_noconv();
1184 if (__old_anc != __always_noconv_) {
1185 this->setg(nullptr, nullptr, nullptr);
1186 this->setp(nullptr, nullptr);
1187 // invariant, char_type is char, else we couldn't get here
1188 if (__always_noconv_) // need to dump __intbuf_
1189 {
1190 if (__owns_eb_)
1191 delete[] __extbuf_;
1192 __owns_eb_ = __owns_ib_;
1193 __ebs_ = __ibs_;
1194 __extbuf_ = (char*)__intbuf_;
1195 __ibs_ = 0;
1196 __intbuf_ = nullptr;
1197 __owns_ib_ = false;
1198 } else // need to obtain an __intbuf_.
1199 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1200 if (!__owns_eb_ && __extbuf_ != __extbuf_min_) {
1201 __ibs_ = __ebs_;
1202 __intbuf_ = (char_type*)__extbuf_;
1203 __owns_ib_ = false;
1204 __extbuf_ = new char[__ebs_];
1205 __owns_eb_ = true;
1206 } else {
1207 __ibs_ = __ebs_;
1208 __intbuf_ = new char_type[__ibs_];
1209 __owns_ib_ = true;
1210 }
1211 }
1212 }
1213}
1214
1215template <class _CharT, class _Traits>
1216void basic_filebuf<_CharT, _Traits>::__write_mode() {
1217 if (!(__cm_ & ios_base::out)) {
1218 this->setg(nullptr, nullptr, nullptr);
1219 if (__ebs_ > sizeof(__extbuf_min_)) {
1220 if (__always_noconv_)
1221 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));
1222 else
1223 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1224 } else
1225 this->setp(nullptr, nullptr);
1226 __cm_ = ios_base::out;
1227 }
1228}
1229
1230// basic_ifstream
1231
1232template <class _CharT, class _Traits>
1233class basic_ifstream : public basic_istream<_CharT, _Traits> {
1234public:
1235 typedef _CharT char_type;
1236 typedef _Traits traits_type;
1237 typedef typename traits_type::int_type int_type;
1238 typedef typename traits_type::pos_type pos_type;
1239 typedef typename traits_type::off_type off_type;
1240# if _LIBCPP_STD_VER >= 26
1241 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1242# endif
1243
1244 _LIBCPP_HIDE_FROM_ABI basic_ifstream();
1245 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1246# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1247 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1248# endif
1249 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1250# if _LIBCPP_STD_VER >= 17
1251 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1252 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const _Tp& __p, ios_base::openmode __mode = ios_base::in)
1253 : basic_ifstream(__p.c_str(), __mode) {}
1254# endif // _LIBCPP_STD_VER >= 17
1255 _LIBCPP_HIDE_FROM_ABI basic_ifstream(basic_ifstream&& __rhs);
1256 _LIBCPP_HIDE_FROM_ABI basic_ifstream& operator=(basic_ifstream&& __rhs);
1257 _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream& __rhs);
1258
1259 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1260# if _LIBCPP_STD_VER >= 26
1261 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
1262 return rdbuf()->native_handle();
1263 }
1264# endif
1265 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1266 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1267# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1268 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1269# endif
1270 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1271# if _LIBCPP_STD_VER >= 17
1272 _LIBCPP_HIDE_FROM_ABI void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) {
1273 return open(__p.c_str(), __mode);
1274 }
1275# endif // _LIBCPP_STD_VER >= 17
1276
1277 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1278 _LIBCPP_HIDE_FROM_ABI void close();
1279
1280private:
1281 basic_filebuf<char_type, traits_type> __sb_;
1282};
1283
1284template <class _CharT, class _Traits>
1285inline basic_ifstream<_CharT, _Traits>::basic_ifstream()
1286 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {}
1287
1288template <class _CharT, class _Traits>
1289inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1290 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1291 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1292 this->setstate(ios_base::failbit);
1293}
1294
1295# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1296template <class _CharT, class _Traits>
1297inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1298 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1299 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1300 this->setstate(ios_base::failbit);
1301}
1302# endif
1303
1304// extension
1305template <class _CharT, class _Traits>
1306inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1307 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1308 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1309 this->setstate(ios_base::failbit);
1310}
1311
1312template <class _CharT, class _Traits>
1313inline basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1314 : basic_istream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1315 this->set_rdbuf(std::addressof(__sb_));
1316}
1317
1318template <class _CharT, class _Traits>
1319inline basic_ifstream<_CharT, _Traits>& basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) {
1320 basic_istream<char_type, traits_type>::operator=(std::move(__rhs));
1321 __sb_ = std::move(__rhs.__sb_);
1322 return *this;
1323}
1324
1325template <class _CharT, class _Traits>
1326inline void basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) {
1327 basic_istream<char_type, traits_type>::swap(__rhs);
1328 __sb_.swap(__rhs.__sb_);
1329}
1330
1331template <class _CharT, class _Traits>
1332inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) {
1333 __x.swap(__y);
1334}
1335
1336template <class _CharT, class _Traits>
1337inline basic_filebuf<_CharT, _Traits>* basic_ifstream<_CharT, _Traits>::rdbuf() const {
1338 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1339}
1340
1341template <class _CharT, class _Traits>
1342inline bool basic_ifstream<_CharT, _Traits>::is_open() const {
1343 return __sb_.is_open();
1344}
1345
1346template <class _CharT, class _Traits>
1347void basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1348 if (__sb_.open(__s, __mode | ios_base::in))
1349 this->clear();
1350 else
1351 this->setstate(ios_base::failbit);
1352}
1353
1354# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1355template <class _CharT, class _Traits>
1356void basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1357 if (__sb_.open(__s, __mode | ios_base::in))
1358 this->clear();
1359 else
1360 this->setstate(ios_base::failbit);
1361}
1362# endif
1363
1364template <class _CharT, class _Traits>
1365void basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1366 if (__sb_.open(__s, __mode | ios_base::in))
1367 this->clear();
1368 else
1369 this->setstate(ios_base::failbit);
1370}
1371
1372template <class _CharT, class _Traits>
1373inline void basic_ifstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1374 if (__sb_.__open(__fd, __mode | ios_base::in))
1375 this->clear();
1376 else
1377 this->setstate(ios_base::failbit);
1378}
1379
1380template <class _CharT, class _Traits>
1381inline void basic_ifstream<_CharT, _Traits>::close() {
1382 if (__sb_.close() == 0)
1383 this->setstate(ios_base::failbit);
1384}
1385
1386// basic_ofstream
1387
1388template <class _CharT, class _Traits>
1389class basic_ofstream : public basic_ostream<_CharT, _Traits> {
1390public:
1391 typedef _CharT char_type;
1392 typedef _Traits traits_type;
1393 typedef typename traits_type::int_type int_type;
1394 typedef typename traits_type::pos_type pos_type;
1395 typedef typename traits_type::off_type off_type;
1396# if _LIBCPP_STD_VER >= 26
1397 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1398# endif
1399
1400 _LIBCPP_HIDE_FROM_ABI basic_ofstream();
1401 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1402# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1403 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1404# endif
1405 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1406
1407# if _LIBCPP_STD_VER >= 17
1408 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1409 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const _Tp& __p, ios_base::openmode __mode = ios_base::out)
1410 : basic_ofstream(__p.c_str(), __mode) {}
1411# endif // _LIBCPP_STD_VER >= 17
1412
1413 _LIBCPP_HIDE_FROM_ABI basic_ofstream(basic_ofstream&& __rhs);
1414 _LIBCPP_HIDE_FROM_ABI basic_ofstream& operator=(basic_ofstream&& __rhs);
1415 _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream& __rhs);
1416
1417 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1418# if _LIBCPP_STD_VER >= 26
1419 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
1420 return rdbuf()->native_handle();
1421 }
1422# endif
1423 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1424 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1425# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1426 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1427# endif
1428 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1429
1430# if _LIBCPP_STD_VER >= 17
1431 _LIBCPP_HIDE_FROM_ABI void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) {
1432 return open(__p.c_str(), __mode);
1433 }
1434# endif // _LIBCPP_STD_VER >= 17
1435
1436 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1437 _LIBCPP_HIDE_FROM_ABI void close();
1438
1439private:
1440 basic_filebuf<char_type, traits_type> __sb_;
1441};
1442
1443template <class _CharT, class _Traits>
1444inline basic_ofstream<_CharT, _Traits>::basic_ofstream()
1445 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {}
1446
1447template <class _CharT, class _Traits>
1448inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1449 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1450 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1451 this->setstate(ios_base::failbit);
1452}
1453
1454# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1455template <class _CharT, class _Traits>
1456inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1457 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1458 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1459 this->setstate(ios_base::failbit);
1460}
1461# endif
1462
1463// extension
1464template <class _CharT, class _Traits>
1465inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1466 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1467 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1468 this->setstate(ios_base::failbit);
1469}
1470
1471template <class _CharT, class _Traits>
1472inline basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1473 : basic_ostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1474 this->set_rdbuf(std::addressof(__sb_));
1475}
1476
1477template <class _CharT, class _Traits>
1478inline basic_ofstream<_CharT, _Traits>& basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) {
1479 basic_ostream<char_type, traits_type>::operator=(std::move(__rhs));
1480 __sb_ = std::move(__rhs.__sb_);
1481 return *this;
1482}
1483
1484template <class _CharT, class _Traits>
1485inline void basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) {
1486 basic_ostream<char_type, traits_type>::swap(__rhs);
1487 __sb_.swap(__rhs.__sb_);
1488}
1489
1490template <class _CharT, class _Traits>
1491inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) {
1492 __x.swap(__y);
1493}
1494
1495template <class _CharT, class _Traits>
1496inline basic_filebuf<_CharT, _Traits>* basic_ofstream<_CharT, _Traits>::rdbuf() const {
1497 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1498}
1499
1500template <class _CharT, class _Traits>
1501inline bool basic_ofstream<_CharT, _Traits>::is_open() const {
1502 return __sb_.is_open();
1503}
1504
1505template <class _CharT, class _Traits>
1506void basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1507 if (__sb_.open(__s, __mode | ios_base::out))
1508 this->clear();
1509 else
1510 this->setstate(ios_base::failbit);
1511}
1512
1513# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1514template <class _CharT, class _Traits>
1515void basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1516 if (__sb_.open(__s, __mode | ios_base::out))
1517 this->clear();
1518 else
1519 this->setstate(ios_base::failbit);
1520}
1521# endif
1522
1523template <class _CharT, class _Traits>
1524void basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1525 if (__sb_.open(__s, __mode | ios_base::out))
1526 this->clear();
1527 else
1528 this->setstate(ios_base::failbit);
1529}
1530
1531template <class _CharT, class _Traits>
1532inline void basic_ofstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1533 if (__sb_.__open(__fd, __mode | ios_base::out))
1534 this->clear();
1535 else
1536 this->setstate(ios_base::failbit);
1537}
1538
1539template <class _CharT, class _Traits>
1540inline void basic_ofstream<_CharT, _Traits>::close() {
1541 if (__sb_.close() == nullptr)
1542 this->setstate(ios_base::failbit);
1543}
1544
1545// basic_fstream
1546
1547template <class _CharT, class _Traits>
1548class basic_fstream : public basic_iostream<_CharT, _Traits> {
1549public:
1550 typedef _CharT char_type;
1551 typedef _Traits traits_type;
1552 typedef typename traits_type::int_type int_type;
1553 typedef typename traits_type::pos_type pos_type;
1554 typedef typename traits_type::off_type off_type;
1555# if _LIBCPP_STD_VER >= 26
1556 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1557# endif
1558
1559 _LIBCPP_HIDE_FROM_ABI basic_fstream() : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {}
1560 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const char* __s,
1561 ios_base::openmode __mode = ios_base::in | ios_base::out)
1562 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1563 if (__sb_.open(__s, __mode) == nullptr)
1564 this->setstate(ios_base::failbit);
1565 }
1566# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1567 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const wchar_t* __s,
1568 ios_base::openmode __mode = ios_base::in | ios_base::out)
1569 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1570 if (__sb_.open(__s, __mode) == nullptr)
1571 this->setstate(ios_base::failbit);
1572 }
1573# endif
1574
1575 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const string& __s,
1576 ios_base::openmode __mode = ios_base::in | ios_base::out)
1577 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1578 if (__sb_.open(__s, __mode) == nullptr)
1579 this->setstate(ios_base::failbit);
1580 }
1581
1582# if _LIBCPP_STD_VER >= 17
1583 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1584 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const _Tp& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1585 : basic_fstream(__p.c_str(), __mode) {}
1586# endif // _LIBCPP_STD_VER >= 17
1587
1588 _LIBCPP_HIDE_FROM_ABI basic_fstream(basic_fstream&& __rhs)
1589 : basic_iostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1590 this->set_rdbuf(std::addressof(__sb_));
1591 }
1592
1593 _LIBCPP_HIDE_FROM_ABI basic_fstream& operator=(basic_fstream&& __rhs) {
1594 basic_iostream<char_type, traits_type>::operator=(std::move(__rhs));
1595 __sb_ = std::move(__rhs.__sb_);
1596 return *this;
1597 }
1598
1599 _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream& __rhs) {
1600 basic_iostream<char_type, traits_type>::swap(__rhs);
1601 __sb_.swap(__rhs.__sb_);
1602 }
1603
1604 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const {
1605 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1606 }
1607
1608# if _LIBCPP_STD_VER >= 26
1609 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
1610 return rdbuf()->native_handle();
1611 }
1612# endif
1613
1614 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool is_open() const { return __sb_.is_open(); }
1615
1616 _LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1617 if (__sb_.open(__s, __mode))
1618 this->clear();
1619 else
1620 this->setstate(ios_base::failbit);
1621 }
1622
1623# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1624 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1625 if (__sb_.open(__s, __mode))
1626 this->clear();
1627 else
1628 this->setstate(ios_base::failbit);
1629 }
1630# endif
1631 _LIBCPP_HIDE_FROM_ABI void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1632 if (__sb_.open(__s, __mode))
1633 this->clear();
1634 else
1635 this->setstate(ios_base::failbit);
1636 }
1637
1638# if _LIBCPP_STD_VER >= 17
1639 _LIBCPP_HIDE_FROM_ABI void
1640 open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1641 return open(__p.c_str(), __mode);
1642 }
1643# endif // _LIBCPP_STD_VER >= 17
1644
1645 _LIBCPP_HIDE_FROM_ABI void close() {
1646 if (__sb_.close() == nullptr)
1647 this->setstate(ios_base::failbit);
1648 }
1649
1650private:
1651 basic_filebuf<char_type, traits_type> __sb_;
1652};
1653
1654template <class _CharT, class _Traits>
1655inline _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) {
1656 __x.swap(__y);
1657}
1658
1659# if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1
1660extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1661extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1662extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
1663# endif
1664
1665_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
1666_LIBCPP_END_NAMESPACE_STD
1667
1668_LIBCPP_POP_MACROS
1669
1670# endif // _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
1671
1672#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1673
1674#endif // _LIBCPP_FSTREAM
1675