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