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