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 off_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 _LIBCPP_HIDE_FROM_ABI pos_type __seek_from_begin(off_type __off, off_type __byte_off) {
495 if (__off < 0)
496 return pos_type(off_type(-1));
497
498 pos_type __success_pos = __off;
499 __success_pos.state(__st_);
500
501 // If we're not always noconv or have a write buffer, sync and seek.
502 // TODO: We don't have to sync() even if we're not always_noconv.
503 if (!__always_noconv_ || (__cm_ & ios::out)) {
504 if (sync() || __fseek(file: __file_, offset: __byte_off, SEEK_SET))
505 return pos_type(off_type(-1));
506 return __success_pos;
507 }
508
509 off_type __file_pos = __ftell(file: __file_);
510 auto __buffer_base_pos = __file_pos - (this->egptr() - this->eback());
511
512 // If the new position is within the buffer we can just move the current read pointer
513 if (__off >= __buffer_base_pos && __off <= __file_pos) {
514 this->setg(this->eback(), this->eback() + (__off - __buffer_base_pos), this->egptr());
515 return __success_pos;
516 } else {
517 // Otherwise drop the buffer and seek to the requested position in the underlying C stream. This is the expensive
518 // path, since it performs a syscall.
519 this->setg(nullptr, nullptr, nullptr);
520 __cm_ = 0;
521 if (__fseek(file: __file_, offset: __byte_off, SEEK_SET))
522 return pos_type(off_type(-1));
523 return __success_pos;
524 }
525 }
526
527 _LIBCPP_HIDE_FROM_ABI pos_type __seek_from_current(off_type __off, off_type __byte_off) {
528 // If we're not always noconv or have a write buffer, sync and seek.
529 // TODO: We don't have to sync() even if we're not always_noconv.
530 // TODO: We don't have to sync() if `(__cm_ & ios::out) && __off == 0`.
531 if (!__always_noconv_ || (__cm_ & ios::out)) {
532 if (sync() || __fseek(file: __file_, offset: __byte_off, SEEK_CUR))
533 return pos_type(off_type(-1));
534 pos_type __r = __ftell(file: __file_);
535 __r.state(__st_);
536 return __r;
537 }
538
539 // If the new position is within the buffer we can just move the current read pointer
540 if (__off < 0 ? this->gptr() - this->eback() <= __off : this->egptr() - this->gptr() >= __off) {
541 this->__gbump_ptrdiff(__off);
542
543 pos_type __ret = __ftell(file: __file_) - (this->egptr() - this->gptr());
544 __ret.state(__st_);
545 return __ret;
546 } else {
547 // Otherwise seek to the requested position in the underlying C stream and drop the buffer. This is the expensive
548 // path, since it performs a syscall.
549 if (off_type __offset; __builtin_sub_overflow(__byte_off, this->egptr() - this->gptr(), &__offset) ||
550 __fseek(file: __file_, __offset, SEEK_CUR))
551 return pos_type(off_type(-1));
552 this->setg(nullptr, nullptr, nullptr);
553 __cm_ = 0;
554 pos_type __ret = __ftell(file: __file_);
555 __ret.state(__st_);
556 return __ret;
557 }
558 }
559
560 _LIBCPP_HIDE_FROM_ABI pos_type __seek_from_end(off_type, off_type __byte_off) {
561 // If we're not always noconv or have a write buffer, sync and seek.
562 if (!__always_noconv_ || (__cm_ & ios::out)) {
563 if (sync() || __fseek(file: __file_, offset: __byte_off, SEEK_END))
564 return pos_type(off_type(-1));
565 pos_type __r = __ftell(file: __file_);
566 __r.state(__st_);
567 return __r;
568 }
569
570 this->setg(nullptr, nullptr, nullptr);
571 __cm_ = 0;
572 if (__fseek(file: __file_, offset: __byte_off, SEEK_END))
573 return pos_type(off_type(-1));
574 pos_type __ret = __ftell(file: __file_);
575 __ret.state(__st_);
576 return __ret;
577 }
578};
579
580template <class _CharT, class _Traits>
581basic_filebuf<_CharT, _Traits>::basic_filebuf()
582 : __extbuf_(nullptr),
583 __extbufnext_(nullptr),
584 __extbufend_(nullptr),
585 __ebs_(0),
586 __intbuf_(nullptr),
587 __ibs_(0),
588 __file_(nullptr),
589 __cv_(nullptr),
590 __st_(),
591 __st_last_(),
592 __om_(0),
593 __cm_(__no_io_operations),
594 __owns_eb_(false),
595 __owns_ib_(false),
596 __always_noconv_(false) {
597 if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc())) {
598 __cv_ = std::addressof(std::use_facet<codecvt<char_type, char, state_type> >(this->getloc()));
599 __always_noconv_ = __cv_->always_noconv();
600 }
601 setbuf(s: nullptr, n: 4096);
602}
603
604template <class _CharT, class _Traits>
605basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) : basic_streambuf<_CharT, _Traits>(__rhs) {
606 if (__rhs.__extbuf_ == __rhs.__extbuf_min_) {
607 __extbuf_ = __extbuf_min_;
608 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
609 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
610 } else {
611 __extbuf_ = __rhs.__extbuf_;
612 __extbufnext_ = __rhs.__extbufnext_;
613 __extbufend_ = __rhs.__extbufend_;
614 }
615 __ebs_ = __rhs.__ebs_;
616 __intbuf_ = __rhs.__intbuf_;
617 __ibs_ = __rhs.__ibs_;
618 __file_ = __rhs.__file_;
619 __cv_ = __rhs.__cv_;
620 __st_ = __rhs.__st_;
621 __st_last_ = __rhs.__st_last_;
622 __om_ = __rhs.__om_;
623 __cm_ = __rhs.__cm_;
624 __owns_eb_ = __rhs.__owns_eb_;
625 __owns_ib_ = __rhs.__owns_ib_;
626 __always_noconv_ = __rhs.__always_noconv_;
627 if (__rhs.pbase()) {
628 if (__rhs.pbase() == __rhs.__intbuf_)
629 this->setp(__intbuf_, __intbuf_ + (__rhs.epptr() - __rhs.pbase()));
630 else
631 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__rhs.epptr() - __rhs.pbase()));
632 this->__pbump(__rhs.pptr() - __rhs.pbase());
633 } else if (__rhs.eback()) {
634 if (__rhs.eback() == __rhs.__intbuf_)
635 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), __intbuf_ + (__rhs.egptr() - __rhs.eback()));
636 else
637 this->setg((char_type*)__extbuf_,
638 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
639 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
640 }
641 __rhs.__extbuf_ = nullptr;
642 __rhs.__extbufnext_ = nullptr;
643 __rhs.__extbufend_ = nullptr;
644 __rhs.__ebs_ = 0;
645 __rhs.__intbuf_ = 0;
646 __rhs.__ibs_ = 0;
647 __rhs.__file_ = nullptr;
648 __rhs.__st_ = state_type();
649 __rhs.__st_last_ = state_type();
650 __rhs.__om_ = 0;
651 __rhs.__cm_ = 0;
652 __rhs.__owns_eb_ = false;
653 __rhs.__owns_ib_ = false;
654 __rhs.setg(0, 0, 0);
655 __rhs.setp(0, 0);
656}
657
658template <class _CharT, class _Traits>
659inline basic_filebuf<_CharT, _Traits>& basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) {
660 close();
661 swap(__rhs);
662 return *this;
663}
664
665template <class _CharT, class _Traits>
666basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
667# if _LIBCPP_HAS_EXCEPTIONS
668 try {
669# endif // _LIBCPP_HAS_EXCEPTIONS
670 close();
671# if _LIBCPP_HAS_EXCEPTIONS
672 } catch (...) {
673 }
674# endif // _LIBCPP_HAS_EXCEPTIONS
675 if (__owns_eb_)
676 delete[] __extbuf_;
677 if (__owns_ib_)
678 delete[] __intbuf_;
679}
680
681template <class _CharT, class _Traits>
682void basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) {
683 basic_streambuf<char_type, traits_type>::swap(__rhs);
684 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
685 // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
686 std::swap(x&: __extbuf_, y&: __rhs.__extbuf_);
687 std::swap(x&: __extbufnext_, y&: __rhs.__extbufnext_);
688 std::swap(x&: __extbufend_, y&: __rhs.__extbufend_);
689 } else {
690 ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
691 ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
692 ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
693 ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
694 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
695 // *this uses the small buffer, but __rhs doesn't.
696 __extbuf_ = __rhs.__extbuf_;
697 __rhs.__extbuf_ = __rhs.__extbuf_min_;
698 std::memmove(dest: __rhs.__extbuf_min_, src: __extbuf_min_, n: sizeof(__extbuf_min_));
699 } else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) {
700 // *this doesn't use the small buffer, but __rhs does.
701 __rhs.__extbuf_ = __extbuf_;
702 __extbuf_ = __extbuf_min_;
703 std::memmove(dest: __extbuf_min_, src: __rhs.__extbuf_min_, n: sizeof(__extbuf_min_));
704 } else {
705 // Both *this and __rhs use the small buffer.
706 char __tmp[sizeof(__extbuf_min_)];
707 std::memmove(dest: __tmp, src: __extbuf_min_, n: sizeof(__extbuf_min_));
708 std::memmove(dest: __extbuf_min_, src: __rhs.__extbuf_min_, n: sizeof(__extbuf_min_));
709 std::memmove(dest: __rhs.__extbuf_min_, src: __tmp, n: sizeof(__extbuf_min_));
710 }
711 __extbufnext_ = __extbuf_ + __rn;
712 __extbufend_ = __extbuf_ + __re;
713 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
714 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
715 }
716 std::swap(x&: __ebs_, y&: __rhs.__ebs_);
717 std::swap(__intbuf_, __rhs.__intbuf_);
718 std::swap(x&: __ibs_, y&: __rhs.__ibs_);
719 std::swap(x&: __file_, y&: __rhs.__file_);
720 std::swap(__cv_, __rhs.__cv_);
721 std::swap(__st_, __rhs.__st_);
722 std::swap(__st_last_, __rhs.__st_last_);
723 std::swap(x&: __om_, y&: __rhs.__om_);
724 std::swap(x&: __cm_, y&: __rhs.__cm_);
725 std::swap(x&: __owns_eb_, y&: __rhs.__owns_eb_);
726 std::swap(x&: __owns_ib_, y&: __rhs.__owns_ib_);
727 std::swap(x&: __always_noconv_, y&: __rhs.__always_noconv_);
728 if (this->eback() == (char_type*)__rhs.__extbuf_min_) {
729 ptrdiff_t __n = this->gptr() - this->eback();
730 ptrdiff_t __e = this->egptr() - this->eback();
731 this->setg((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __n, (char_type*)__extbuf_min_ + __e);
732 } else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) {
733 ptrdiff_t __n = this->pptr() - this->pbase();
734 ptrdiff_t __e = this->epptr() - this->pbase();
735 this->setp((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __e);
736 this->__pbump(__n);
737 }
738 if (__rhs.eback() == (char_type*)__extbuf_min_) {
739 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
740 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
741 __rhs.setg(
742 (char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __n, (char_type*)__rhs.__extbuf_min_ + __e);
743 } else if (__rhs.pbase() == (char_type*)__extbuf_min_) {
744 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
745 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
746 __rhs.setp((char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __e);
747 __rhs.__pbump(__n);
748 }
749}
750
751template <class _CharT, class _Traits>
752inline _LIBCPP_HIDE_FROM_ABI void swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) {
753 __x.swap(__y);
754}
755
756template <class _CharT, class _Traits>
757inline bool basic_filebuf<_CharT, _Traits>::is_open() const {
758 return __file_ != nullptr;
759}
760
761// Configures the fopen close-on-exec mode character, if any. This string will
762// be appended to any mode string used by fstream for fopen/fdopen.
763//
764// Not all platforms support this, but it helps avoid fd-leaks on platforms that
765// do.
766# if defined(__BIONIC__)
767# define _LIBCPP_FOPEN_CLOEXEC_MODE "e"
768# else
769# define _LIBCPP_FOPEN_CLOEXEC_MODE
770# endif
771
772template <class _CharT, class _Traits>
773const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(ios_base::openmode __mode) _NOEXCEPT {
774 switch (__mode & ~ios_base::ate) {
775 case ios_base::out:
776 case ios_base::out | ios_base::trunc:
777 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
778 case ios_base::out | ios_base::app:
779 case ios_base::app:
780 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
781 case ios_base::in:
782 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
783 case ios_base::in | ios_base::out:
784 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
785 case ios_base::in | ios_base::out | ios_base::trunc:
786 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
787 case ios_base::in | ios_base::out | ios_base::app:
788 case ios_base::in | ios_base::app:
789 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
790 case ios_base::out | ios_base::binary:
791 case ios_base::out | ios_base::trunc | ios_base::binary:
792 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
793 case ios_base::out | ios_base::app | ios_base::binary:
794 case ios_base::app | ios_base::binary:
795 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
796 case ios_base::in | ios_base::binary:
797 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
798 case ios_base::in | ios_base::out | ios_base::binary:
799 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
800 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
801 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
802 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
803 case ios_base::in | ios_base::app | ios_base::binary:
804 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
805# if _LIBCPP_STD_VER >= 23
806 case ios_base::out | ios_base::noreplace:
807 case ios_base::out | ios_base::trunc | ios_base::noreplace:
808 return "wx" _LIBCPP_FOPEN_CLOEXEC_MODE;
809 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
810 return "w+x" _LIBCPP_FOPEN_CLOEXEC_MODE;
811 case ios_base::out | ios_base::binary | ios_base::noreplace:
812 case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
813 return "wbx" _LIBCPP_FOPEN_CLOEXEC_MODE;
814 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
815 return "w+bx" _LIBCPP_FOPEN_CLOEXEC_MODE;
816# endif // _LIBCPP_STD_VER >= 23
817 default:
818 return nullptr;
819 }
820 __libcpp_unreachable();
821}
822
823# if _LIBCPP_HAS_OPEN_WITH_WCHAR
824template <class _CharT, class _Traits>
825const wchar_t* basic_filebuf<_CharT, _Traits>::__make_mdwstring(ios_base::openmode __mode) _NOEXCEPT {
826 switch (__mode & ~ios_base::ate) {
827 case ios_base::out:
828 case ios_base::out | ios_base::trunc:
829 return L"w";
830 case ios_base::out | ios_base::app:
831 case ios_base::app:
832 return L"a";
833 case ios_base::in:
834 return L"r";
835 case ios_base::in | ios_base::out:
836 return L"r+";
837 case ios_base::in | ios_base::out | ios_base::trunc:
838 return L"w+";
839 case ios_base::in | ios_base::out | ios_base::app:
840 case ios_base::in | ios_base::app:
841 return L"a+";
842 case ios_base::out | ios_base::binary:
843 case ios_base::out | ios_base::trunc | ios_base::binary:
844 return L"wb";
845 case ios_base::out | ios_base::app | ios_base::binary:
846 case ios_base::app | ios_base::binary:
847 return L"ab";
848 case ios_base::in | ios_base::binary:
849 return L"rb";
850 case ios_base::in | ios_base::out | ios_base::binary:
851 return L"r+b";
852 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
853 return L"w+b";
854 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
855 case ios_base::in | ios_base::app | ios_base::binary:
856 return L"a+b";
857# if _LIBCPP_STD_VER >= 23
858 case ios_base::out | ios_base::noreplace:
859 case ios_base::out | ios_base::trunc | ios_base::noreplace:
860 return L"wx";
861 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
862 return L"w+x";
863 case ios_base::out | ios_base::binary | ios_base::noreplace:
864 case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
865 return L"wbx";
866 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
867 return L"w+bx";
868# endif // _LIBCPP_STD_VER >= 23
869 default:
870 return nullptr;
871 }
872 __libcpp_unreachable();
873}
874# endif
875
876template <class _CharT, class _Traits>
877FILE* basic_filebuf<_CharT, _Traits>::__fopen(const char* __name, const char* __mode) {
878# if defined(_AIX)
879 return ::__libcpp_fopen64(__name, __mode);
880# elif defined(__GLIBC__)
881 return ::fopen64(filename: __name, modes: __mode);
882# else
883 return std::fopen(__name, __mode);
884# endif
885}
886
887template <class _CharT, class _Traits>
888basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
889 if (__file_)
890 return nullptr;
891 const char* __mdstr = __make_mdstring(__mode);
892 if (!__mdstr)
893 return nullptr;
894
895 return __adopt_file(file: __fopen(name: __s, mode: __mdstr), __mode);
896}
897
898template <class _CharT, class _Traits>
899inline basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
900 if (__file_)
901 return nullptr;
902 const char* __mdstr = __make_mdstring(__mode);
903 if (!__mdstr)
904 return nullptr;
905
906 return __adopt_file(file: fdopen(__fd, modes: __mdstr), __mode);
907}
908
909# if _LIBCPP_HAS_OPEN_WITH_WCHAR
910// This is basically the same as the char* overload except that it uses _wfopen
911// and long mode strings.
912template <class _CharT, class _Traits>
913basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
914 if (__file_)
915 return nullptr;
916 const wchar_t* __mdstr = __make_mdwstring(__mode);
917 if (!__mdstr)
918 return nullptr;
919
920 return __adopt_file(_wfopen(__s, __mdstr), __mode);
921}
922# endif
923
924template <class _CharT, class _Traits>
925inline basic_filebuf<_CharT, _Traits>*
926basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
927 return open(__s.c_str(), __mode);
928}
929
930template <class _CharT, class _Traits>
931basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::close() {
932 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
933 if (__file_) {
934 __rt = this;
935 unique_ptr<FILE, int (*)(FILE*)> __h(__file_, fclose);
936 if (sync())
937 __rt = nullptr;
938 if (fclose(stream: __h.release()))
939 __rt = nullptr;
940 __file_ = nullptr;
941 // Reset the get and the put areas without getting rid of the underlying buffers,
942 // which might have been configured by the user. Make sure to keep the buffers
943 // since the user may re-open the stream.
944 this->setg(nullptr, nullptr, nullptr);
945 this->setp(nullptr, nullptr);
946 __cm_ = __no_io_operations;
947 }
948 return __rt;
949}
950
951template <class _CharT, class _Traits>
952typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::underflow() {
953 if (__file_ == nullptr)
954 return traits_type::eof();
955
956 char_type __1buf;
957 __read_guard __guard(__1buf, *this);
958
959 const size_t __unget_sz = __guard.__unget_size();
960 int_type __c = traits_type::eof();
961 if (this->gptr() == this->egptr()) {
962 std::memmove(dest: this->eback(), src: this->egptr() - __unget_sz, n: __unget_sz * sizeof(char_type));
963 if (__always_noconv_) {
964 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
965 __nmemb = std::fread(ptr: this->eback() + __unget_sz, size: 1, n: __nmemb, stream: __file_);
966 if (__nmemb != 0) {
967 this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);
968 __c = traits_type::to_int_type(*this->gptr());
969 }
970 } else {
971 if (__extbufend_ != __extbufnext_) {
972 _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");
973 _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");
974 std::memmove(dest: __extbuf_, src: __extbufnext_, n: __extbufend_ - __extbufnext_);
975 }
976 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
977 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
978 size_t __nmemb =
979 std::min(a: static_cast<size_t>(__ibs_ - __unget_sz), b: static_cast<size_t>(__extbufend_ - __extbufnext_));
980 codecvt_base::result __r;
981 __st_last_ = __st_;
982 size_t __nr = std::fread(ptr: (void*)const_cast<char*>(__extbufnext_), size: 1, n: __nmemb, stream: __file_);
983 if (__nr != 0) {
984 if (!__cv_)
985 std::__throw_bad_cast();
986
987 __extbufend_ = __extbufnext_ + __nr;
988 char_type* __inext;
989 __r = __cv_->in(
990 __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->eback() + __ibs_, __inext);
991 if (__r == codecvt_base::noconv) {
992 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));
993 __c = traits_type::to_int_type(*this->gptr());
994 } else if (__inext != this->eback() + __unget_sz) {
995 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
996 __c = traits_type::to_int_type(*this->gptr());
997 }
998 }
999 }
1000 } else
1001 __c = traits_type::to_int_type(*this->gptr());
1002 return __c;
1003}
1004
1005template <class _CharT, class _Traits>
1006typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
1007 if (__file_ && this->eback() < this->gptr()) {
1008 if (traits_type::eq_int_type(__c, traits_type::eof())) {
1009 this->gbump(-1);
1010 return traits_type::not_eof(__c);
1011 }
1012 if ((__om_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {
1013 this->gbump(-1);
1014 *this->gptr() = traits_type::to_char_type(__c);
1015 return __c;
1016 }
1017 }
1018 return traits_type::eof();
1019}
1020
1021template <class _CharT, class _Traits>
1022typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
1023 if (__file_ == nullptr)
1024 return traits_type::eof();
1025 __write_mode();
1026 char_type __1buf;
1027 char_type* __pb_save = this->pbase();
1028 char_type* __epb_save = this->epptr();
1029 if (!traits_type::eq_int_type(__c, traits_type::eof())) {
1030 if (this->pptr() == nullptr)
1031 this->setp(std::addressof(__1buf), std::addressof(__1buf) + 1);
1032 *this->pptr() = traits_type::to_char_type(__c);
1033 this->pbump(1);
1034 }
1035
1036 // There is nothing to write, early return
1037 if (this->pptr() == this->pbase()) {
1038 return traits_type::not_eof(__c);
1039 }
1040
1041 if (__always_noconv_) {
1042 size_t __n = static_cast<size_t>(this->pptr() - this->pbase());
1043 if (std::fwrite(ptr: this->pbase(), size: sizeof(char_type), __n, s: __file_) != __n) {
1044 return __overflow_failed();
1045 }
1046 } else {
1047 if (!__cv_)
1048 std::__throw_bad_cast();
1049
1050 // See [filebuf.virtuals]
1051 char_type* __b = this->pbase();
1052 char_type* __p = this->pptr();
1053 const char_type* __end;
1054 char* __extbuf_end = __extbuf_;
1055 do {
1056 codecvt_base::result __r = __cv_->out(__st_, __b, __p, __end, __extbuf_, __extbuf_ + __ebs_, __extbuf_end);
1057 if (__end == __b) {
1058 return __overflow_failed();
1059 }
1060
1061 // No conversion needed: output characters directly to the file, done.
1062 if (__r == codecvt_base::noconv) {
1063 size_t __n = static_cast<size_t>(__p - __b);
1064 if (std::fwrite(ptr: __b, size: 1, __n, s: __file_) != __n) {
1065 return __overflow_failed();
1066 }
1067 break;
1068
1069 // Conversion successful: output the converted characters to the file, done.
1070 } else if (__r == codecvt_base::ok) {
1071 size_t __n = static_cast<size_t>(__extbuf_end - __extbuf_);
1072 if (std::fwrite(ptr: __extbuf_, size: 1, __n, s: __file_) != __n) {
1073 return __overflow_failed();
1074 }
1075 break;
1076
1077 // Conversion partially successful: output converted characters to the file and repeat with the
1078 // remaining characters.
1079 } else if (__r == codecvt_base::partial) {
1080 size_t __n = static_cast<size_t>(__extbuf_end - __extbuf_);
1081 if (std::fwrite(ptr: __extbuf_, size: 1, __n, s: __file_) != __n) {
1082 return __overflow_failed();
1083 }
1084 __b = const_cast<char_type*>(__end);
1085 continue;
1086
1087 } else {
1088 return __overflow_failed();
1089 }
1090 } while (true);
1091 }
1092 this->setp(__pb_save, __epb_save);
1093 return traits_type::not_eof(__c);
1094}
1095
1096template <class _CharT, class _Traits>
1097basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {
1098 this->setg(nullptr, nullptr, nullptr);
1099 this->setp(nullptr, nullptr);
1100 // Calling setbuf(nullptr, 0) before any i/o operation switches the stream to unbuffered mode
1101 if (__cm_ == __no_io_operations && __s == nullptr && __n == 0)
1102 __request_unbuffered_mode();
1103 if (__owns_eb_)
1104 delete[] __extbuf_;
1105 if (__owns_ib_)
1106 delete[] __intbuf_;
1107 __ebs_ = __n;
1108 if (__ebs_ > sizeof(__extbuf_min_)) {
1109 if (__always_noconv_ && __s) {
1110 __extbuf_ = (char*)__s;
1111 __owns_eb_ = false;
1112 } else {
1113 __extbuf_ = new char[__ebs_];
1114 __owns_eb_ = true;
1115 }
1116 } else {
1117 __extbuf_ = __extbuf_min_;
1118 __ebs_ = sizeof(__extbuf_min_);
1119 __owns_eb_ = false;
1120 }
1121 if (!__always_noconv_) {
1122 __ibs_ = max<streamsize>(a: __n, b: sizeof(__extbuf_min_));
1123 if (__s && __ibs_ > sizeof(__extbuf_min_)) {
1124 __intbuf_ = __s;
1125 __owns_ib_ = false;
1126 } else {
1127 __intbuf_ = new char_type[__ibs_];
1128 __owns_ib_ = true;
1129 }
1130 } else {
1131 __ibs_ = 0;
1132 __intbuf_ = nullptr;
1133 __owns_ib_ = false;
1134 }
1135 return this;
1136}
1137
1138template <class _CharT, class _Traits>
1139typename basic_filebuf<_CharT, _Traits>::pos_type
1140basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) {
1141 if (!__cv_)
1142 std::__throw_bad_cast();
1143
1144 int __width = __cv_->encoding();
1145 if (__file_ == nullptr || (__width <= 0 && __off != 0))
1146 return pos_type(off_type(-1));
1147
1148 auto __byte_off = __width <= 0 ? 0 : __width * __off;
1149
1150 switch (__way) {
1151 case ios::beg:
1152 return __seek_from_begin(__off, __byte_off);
1153 case ios::cur:
1154 return __seek_from_current(__off, __byte_off);
1155 case ios::end:
1156 return __seek_from_end(__off, __byte_off);
1157 }
1158 std::__libcpp_unreachable();
1159}
1160
1161template <class _CharT, class _Traits>
1162int basic_filebuf<_CharT, _Traits>::__fseek(FILE* __file, pos_type __offset, int __whence) {
1163# ifdef _WIN32
1164 return _fseeki64(__file, __offset, __whence);
1165# elif _LIBCPP_LIBC_NEWLIB
1166 return fseek(__file, __offset, __whence);
1167# elif defined(_AIX)
1168 return ::__libcpp_fseeko64(__file, __offset, __whence);
1169# elif defined(__GLIBC__)
1170 return ::fseeko64(stream: __file, off: __offset, __whence);
1171# else
1172 return ::fseeko(__file, __offset, __whence);
1173# endif
1174}
1175
1176template <class _CharT, class _Traits>
1177typename basic_filebuf<_CharT, _Traits>::off_type basic_filebuf<_CharT, _Traits>::__ftell(FILE* __file) {
1178# ifdef _WIN32
1179 return _ftelli64(__file);
1180# elif _LIBCPP_LIBC_NEWLIB
1181 return ftell(__file);
1182# elif defined(_AIX)
1183 return ::__libcpp_ftello64(__file);
1184# elif defined(__GLIBC__)
1185 return ::ftello64(stream: __file);
1186# else
1187 return ftello(__file);
1188# endif
1189}
1190
1191template <class _CharT, class _Traits>
1192typename basic_filebuf<_CharT, _Traits>::pos_type
1193basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {
1194 if (__file_ == nullptr || sync())
1195 return pos_type(off_type(-1));
1196 if (__fseek(file: __file_, offset: __sp, SEEK_SET))
1197 return pos_type(off_type(-1));
1198 __st_ = __sp.state();
1199 return __sp;
1200}
1201
1202template <class _CharT, class _Traits>
1203int basic_filebuf<_CharT, _Traits>::sync() {
1204 if (__file_ == nullptr)
1205 return 0;
1206 if (!__cv_)
1207 std::__throw_bad_cast();
1208
1209 if (__cm_ & ios_base::out) {
1210 if (this->pptr() != this->pbase())
1211 if (overflow() == traits_type::eof())
1212 return -1;
1213 codecvt_base::result __r;
1214 do {
1215 char* __extbe;
1216 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
1217 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
1218 if (std::fwrite(ptr: __extbuf_, size: 1, n: __nmemb, s: __file_) != __nmemb)
1219 return -1;
1220 } while (__r == codecvt_base::partial);
1221 if (__r == codecvt_base::error)
1222 return -1;
1223 if (std::fflush(stream: __file_))
1224 return -1;
1225 } else if (__cm_ & ios_base::in) {
1226 off_type __c;
1227 state_type __state = __st_last_;
1228 bool __update_st = false;
1229 if (__always_noconv_)
1230 __c = this->egptr() - this->gptr();
1231 else {
1232 int __width = __cv_->encoding();
1233 __c = __extbufend_ - __extbufnext_;
1234 if (__width > 0)
1235 __c += __width * (this->egptr() - this->gptr());
1236 else {
1237 if (this->gptr() != this->egptr()) {
1238 const int __off = __cv_->length(__state, __extbuf_, __extbufnext_, this->gptr() - this->eback());
1239 __c += __extbufnext_ - __extbuf_ - __off;
1240 __update_st = true;
1241 }
1242 }
1243 }
1244 if (__fseek(file: __file_, offset: -__c, SEEK_CUR))
1245 return -1;
1246 if (__update_st)
1247 __st_ = __state;
1248 __extbufnext_ = __extbufend_ = __extbuf_;
1249 this->setg(nullptr, nullptr, nullptr);
1250 __cm_ = 0;
1251 }
1252 return 0;
1253}
1254
1255template <class _CharT, class _Traits>
1256void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
1257 sync();
1258 __cv_ = std::addressof(std::use_facet<codecvt<char_type, char, state_type> >(__loc));
1259 bool __old_anc = __always_noconv_;
1260 __always_noconv_ = __cv_->always_noconv();
1261 if (__old_anc != __always_noconv_) {
1262 this->setg(nullptr, nullptr, nullptr);
1263 this->setp(nullptr, nullptr);
1264 // invariant, char_type is char, else we couldn't get here
1265 if (__always_noconv_) // need to dump __intbuf_
1266 {
1267 if (__owns_eb_)
1268 delete[] __extbuf_;
1269 __owns_eb_ = __owns_ib_;
1270 __ebs_ = __ibs_;
1271 __extbuf_ = (char*)__intbuf_;
1272 __ibs_ = 0;
1273 __intbuf_ = nullptr;
1274 __owns_ib_ = false;
1275 } else // need to obtain an __intbuf_.
1276 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1277 if (!__owns_eb_ && __extbuf_ != __extbuf_min_) {
1278 __ibs_ = __ebs_;
1279 __intbuf_ = (char_type*)__extbuf_;
1280 __owns_ib_ = false;
1281 __extbuf_ = new char[__ebs_];
1282 __owns_eb_ = true;
1283 } else {
1284 __ibs_ = __ebs_;
1285 __intbuf_ = new char_type[__ibs_];
1286 __owns_ib_ = true;
1287 }
1288 }
1289 }
1290}
1291
1292template <class _CharT, class _Traits>
1293void basic_filebuf<_CharT, _Traits>::__write_mode() {
1294 if (!(__cm_ & ios_base::out)) {
1295 this->setg(nullptr, nullptr, nullptr);
1296 if (__ebs_ > sizeof(__extbuf_min_)) {
1297 if (__always_noconv_)
1298 this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));
1299 else
1300 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1301 } else
1302 this->setp(nullptr, nullptr);
1303 __cm_ = ios_base::out;
1304 }
1305}
1306
1307// basic_ifstream
1308
1309template <class _CharT, class _Traits>
1310class basic_ifstream : public basic_istream<_CharT, _Traits> {
1311public:
1312 typedef _CharT char_type;
1313 typedef _Traits traits_type;
1314 typedef typename traits_type::int_type int_type;
1315 typedef typename traits_type::pos_type pos_type;
1316 typedef typename traits_type::off_type off_type;
1317# if _LIBCPP_STD_VER >= 26
1318 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1319# endif
1320
1321 _LIBCPP_HIDE_FROM_ABI basic_ifstream();
1322 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1323# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1324 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1325# endif
1326 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1327# if _LIBCPP_STD_VER >= 17
1328 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1329 _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const _Tp& __p, ios_base::openmode __mode = ios_base::in)
1330 : basic_ifstream(__p.c_str(), __mode) {}
1331# endif // _LIBCPP_STD_VER >= 17
1332 _LIBCPP_HIDE_FROM_ABI basic_ifstream(basic_ifstream&& __rhs);
1333 _LIBCPP_HIDE_FROM_ABI basic_ifstream& operator=(basic_ifstream&& __rhs);
1334 _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream& __rhs);
1335
1336 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1337# if _LIBCPP_STD_VER >= 26
1338 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
1339 return rdbuf()->native_handle();
1340 }
1341# endif
1342 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1343 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1344# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1345 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1346# endif
1347 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1348# if _LIBCPP_STD_VER >= 17
1349 _LIBCPP_HIDE_FROM_ABI void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) {
1350 return open(__p.c_str(), __mode);
1351 }
1352# endif // _LIBCPP_STD_VER >= 17
1353
1354 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1355 _LIBCPP_HIDE_FROM_ABI void close();
1356
1357private:
1358 basic_filebuf<char_type, traits_type> __sb_;
1359};
1360
1361template <class _CharT, class _Traits>
1362inline basic_ifstream<_CharT, _Traits>::basic_ifstream()
1363 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {}
1364
1365template <class _CharT, class _Traits>
1366inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1367 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1368 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1369 this->setstate(ios_base::failbit);
1370}
1371
1372# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1373template <class _CharT, class _Traits>
1374inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1375 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1376 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1377 this->setstate(ios_base::failbit);
1378}
1379# endif
1380
1381// extension
1382template <class _CharT, class _Traits>
1383inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1384 : basic_istream<char_type, traits_type>(std::addressof(__sb_)) {
1385 if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1386 this->setstate(ios_base::failbit);
1387}
1388
1389template <class _CharT, class _Traits>
1390inline basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1391 : basic_istream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1392 this->set_rdbuf(std::addressof(__sb_));
1393}
1394
1395template <class _CharT, class _Traits>
1396inline basic_ifstream<_CharT, _Traits>& basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) {
1397 basic_istream<char_type, traits_type>::operator=(std::move(__rhs));
1398 __sb_ = std::move(__rhs.__sb_);
1399 return *this;
1400}
1401
1402template <class _CharT, class _Traits>
1403inline void basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) {
1404 basic_istream<char_type, traits_type>::swap(__rhs);
1405 __sb_.swap(__rhs.__sb_);
1406}
1407
1408template <class _CharT, class _Traits>
1409inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) {
1410 __x.swap(__y);
1411}
1412
1413template <class _CharT, class _Traits>
1414inline basic_filebuf<_CharT, _Traits>* basic_ifstream<_CharT, _Traits>::rdbuf() const {
1415 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1416}
1417
1418template <class _CharT, class _Traits>
1419inline bool basic_ifstream<_CharT, _Traits>::is_open() const {
1420 return __sb_.is_open();
1421}
1422
1423template <class _CharT, class _Traits>
1424void basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1425 if (__sb_.open(__s, __mode | ios_base::in))
1426 this->clear();
1427 else
1428 this->setstate(ios_base::failbit);
1429}
1430
1431# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1432template <class _CharT, class _Traits>
1433void basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1434 if (__sb_.open(__s, __mode | ios_base::in))
1435 this->clear();
1436 else
1437 this->setstate(ios_base::failbit);
1438}
1439# endif
1440
1441template <class _CharT, class _Traits>
1442void basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1443 if (__sb_.open(__s, __mode | ios_base::in))
1444 this->clear();
1445 else
1446 this->setstate(ios_base::failbit);
1447}
1448
1449template <class _CharT, class _Traits>
1450inline void basic_ifstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1451 if (__sb_.__open(__fd, __mode | ios_base::in))
1452 this->clear();
1453 else
1454 this->setstate(ios_base::failbit);
1455}
1456
1457template <class _CharT, class _Traits>
1458inline void basic_ifstream<_CharT, _Traits>::close() {
1459 if (__sb_.close() == 0)
1460 this->setstate(ios_base::failbit);
1461}
1462
1463// basic_ofstream
1464
1465template <class _CharT, class _Traits>
1466class basic_ofstream : public basic_ostream<_CharT, _Traits> {
1467public:
1468 typedef _CharT char_type;
1469 typedef _Traits traits_type;
1470 typedef typename traits_type::int_type int_type;
1471 typedef typename traits_type::pos_type pos_type;
1472 typedef typename traits_type::off_type off_type;
1473# if _LIBCPP_STD_VER >= 26
1474 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1475# endif
1476
1477 _LIBCPP_HIDE_FROM_ABI basic_ofstream();
1478 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1479# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1480 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1481# endif
1482 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1483
1484# if _LIBCPP_STD_VER >= 17
1485 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1486 _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const _Tp& __p, ios_base::openmode __mode = ios_base::out)
1487 : basic_ofstream(__p.c_str(), __mode) {}
1488# endif // _LIBCPP_STD_VER >= 17
1489
1490 _LIBCPP_HIDE_FROM_ABI basic_ofstream(basic_ofstream&& __rhs);
1491 _LIBCPP_HIDE_FROM_ABI basic_ofstream& operator=(basic_ofstream&& __rhs);
1492 _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream& __rhs);
1493
1494 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1495# if _LIBCPP_STD_VER >= 26
1496 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
1497 return rdbuf()->native_handle();
1498 }
1499# endif
1500 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1501 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1502# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1503 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1504# endif
1505 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1506
1507# if _LIBCPP_STD_VER >= 17
1508 _LIBCPP_HIDE_FROM_ABI void open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) {
1509 return open(__p.c_str(), __mode);
1510 }
1511# endif // _LIBCPP_STD_VER >= 17
1512
1513 _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1514 _LIBCPP_HIDE_FROM_ABI void close();
1515
1516private:
1517 basic_filebuf<char_type, traits_type> __sb_;
1518};
1519
1520template <class _CharT, class _Traits>
1521inline basic_ofstream<_CharT, _Traits>::basic_ofstream()
1522 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {}
1523
1524template <class _CharT, class _Traits>
1525inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1526 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1527 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1528 this->setstate(ios_base::failbit);
1529}
1530
1531# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1532template <class _CharT, class _Traits>
1533inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1534 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1535 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1536 this->setstate(ios_base::failbit);
1537}
1538# endif
1539
1540// extension
1541template <class _CharT, class _Traits>
1542inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1543 : basic_ostream<char_type, traits_type>(std::addressof(__sb_)) {
1544 if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1545 this->setstate(ios_base::failbit);
1546}
1547
1548template <class _CharT, class _Traits>
1549inline basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1550 : basic_ostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1551 this->set_rdbuf(std::addressof(__sb_));
1552}
1553
1554template <class _CharT, class _Traits>
1555inline basic_ofstream<_CharT, _Traits>& basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) {
1556 basic_ostream<char_type, traits_type>::operator=(std::move(__rhs));
1557 __sb_ = std::move(__rhs.__sb_);
1558 return *this;
1559}
1560
1561template <class _CharT, class _Traits>
1562inline void basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) {
1563 basic_ostream<char_type, traits_type>::swap(__rhs);
1564 __sb_.swap(__rhs.__sb_);
1565}
1566
1567template <class _CharT, class _Traits>
1568inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) {
1569 __x.swap(__y);
1570}
1571
1572template <class _CharT, class _Traits>
1573inline basic_filebuf<_CharT, _Traits>* basic_ofstream<_CharT, _Traits>::rdbuf() const {
1574 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1575}
1576
1577template <class _CharT, class _Traits>
1578inline bool basic_ofstream<_CharT, _Traits>::is_open() const {
1579 return __sb_.is_open();
1580}
1581
1582template <class _CharT, class _Traits>
1583void basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1584 if (__sb_.open(__s, __mode | ios_base::out))
1585 this->clear();
1586 else
1587 this->setstate(ios_base::failbit);
1588}
1589
1590# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1591template <class _CharT, class _Traits>
1592void basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1593 if (__sb_.open(__s, __mode | ios_base::out))
1594 this->clear();
1595 else
1596 this->setstate(ios_base::failbit);
1597}
1598# endif
1599
1600template <class _CharT, class _Traits>
1601void basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1602 if (__sb_.open(__s, __mode | ios_base::out))
1603 this->clear();
1604 else
1605 this->setstate(ios_base::failbit);
1606}
1607
1608template <class _CharT, class _Traits>
1609inline void basic_ofstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1610 if (__sb_.__open(__fd, __mode | ios_base::out))
1611 this->clear();
1612 else
1613 this->setstate(ios_base::failbit);
1614}
1615
1616template <class _CharT, class _Traits>
1617inline void basic_ofstream<_CharT, _Traits>::close() {
1618 if (__sb_.close() == nullptr)
1619 this->setstate(ios_base::failbit);
1620}
1621
1622// basic_fstream
1623
1624template <class _CharT, class _Traits>
1625class basic_fstream : public basic_iostream<_CharT, _Traits> {
1626public:
1627 typedef _CharT char_type;
1628 typedef _Traits traits_type;
1629 typedef typename traits_type::int_type int_type;
1630 typedef typename traits_type::pos_type pos_type;
1631 typedef typename traits_type::off_type off_type;
1632# if _LIBCPP_STD_VER >= 26
1633 using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1634# endif
1635
1636 _LIBCPP_HIDE_FROM_ABI basic_fstream() : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {}
1637 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const char* __s,
1638 ios_base::openmode __mode = ios_base::in | ios_base::out)
1639 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1640 if (__sb_.open(__s, __mode) == nullptr)
1641 this->setstate(ios_base::failbit);
1642 }
1643# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1644 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const wchar_t* __s,
1645 ios_base::openmode __mode = ios_base::in | ios_base::out)
1646 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1647 if (__sb_.open(__s, __mode) == nullptr)
1648 this->setstate(ios_base::failbit);
1649 }
1650# endif
1651
1652 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const string& __s,
1653 ios_base::openmode __mode = ios_base::in | ios_base::out)
1654 : basic_iostream<char_type, traits_type>(std::addressof(__sb_)) {
1655 if (__sb_.open(__s, __mode) == nullptr)
1656 this->setstate(ios_base::failbit);
1657 }
1658
1659# if _LIBCPP_STD_VER >= 17
1660 template <class _Tp, class = enable_if_t<is_same_v<_Tp, filesystem::path>>>
1661 _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const _Tp& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1662 : basic_fstream(__p.c_str(), __mode) {}
1663# endif // _LIBCPP_STD_VER >= 17
1664
1665 _LIBCPP_HIDE_FROM_ABI basic_fstream(basic_fstream&& __rhs)
1666 : basic_iostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1667 this->set_rdbuf(std::addressof(__sb_));
1668 }
1669
1670 _LIBCPP_HIDE_FROM_ABI basic_fstream& operator=(basic_fstream&& __rhs) {
1671 basic_iostream<char_type, traits_type>::operator=(std::move(__rhs));
1672 __sb_ = std::move(__rhs.__sb_);
1673 return *this;
1674 }
1675
1676 _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream& __rhs) {
1677 basic_iostream<char_type, traits_type>::swap(__rhs);
1678 __sb_.swap(__rhs.__sb_);
1679 }
1680
1681 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const {
1682 return const_cast<basic_filebuf<char_type, traits_type>*>(std::addressof(__sb_));
1683 }
1684
1685# if _LIBCPP_STD_VER >= 26
1686 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
1687 return rdbuf()->native_handle();
1688 }
1689# endif
1690
1691 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI bool is_open() const { return __sb_.is_open(); }
1692
1693 _LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1694 if (__sb_.open(__s, __mode))
1695 this->clear();
1696 else
1697 this->setstate(ios_base::failbit);
1698 }
1699
1700# if _LIBCPP_HAS_OPEN_WITH_WCHAR
1701 void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1702 if (__sb_.open(__s, __mode))
1703 this->clear();
1704 else
1705 this->setstate(ios_base::failbit);
1706 }
1707# endif
1708 _LIBCPP_HIDE_FROM_ABI void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1709 if (__sb_.open(__s, __mode))
1710 this->clear();
1711 else
1712 this->setstate(ios_base::failbit);
1713 }
1714
1715# if _LIBCPP_STD_VER >= 17
1716 _LIBCPP_HIDE_FROM_ABI void
1717 open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1718 return open(__p.c_str(), __mode);
1719 }
1720# endif // _LIBCPP_STD_VER >= 17
1721
1722 _LIBCPP_HIDE_FROM_ABI void close() {
1723 if (__sb_.close() == nullptr)
1724 this->setstate(ios_base::failbit);
1725 }
1726
1727private:
1728 basic_filebuf<char_type, traits_type> __sb_;
1729};
1730
1731template <class _CharT, class _Traits>
1732inline _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) {
1733 __x.swap(__y);
1734}
1735
1736# if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1
1737extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1738extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1739extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
1740# endif
1741
1742_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
1743_LIBCPP_END_NAMESPACE_STD
1744
1745_LIBCPP_POP_MACROS
1746
1747# endif // _LIBCPP_HAS_FILESYSTEM && _LIBCPP_HAS_LOCALIZATION
1748
1749#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
1750
1751#endif // _LIBCPP_FSTREAM
1752