Coverage Report

Created: 2025-08-28 06:28

/src/llvm-project-18.1.8.build/include/c++/v1/fstream
Line
Count
Source (jump to first uncovered line)
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
16
template <class charT, class traits = char_traits<charT> >
17
class basic_filebuf
18
    : public basic_streambuf<charT, traits>
19
{
20
public:
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
43
protected:
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
59
template <class charT, class traits>
60
  void
61
  swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
62
63
typedef basic_filebuf<char>    filebuf;
64
typedef basic_filebuf<wchar_t> wfilebuf;
65
66
template <class charT, class traits = char_traits<charT> >
67
class basic_ifstream
68
    : public basic_istream<charT,traits>
69
{
70
public:
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
    explicit basic_ifstream(const filesystem::path& p,
82
                            ios_base::openmode mode = ios_base::in); // 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
98
template <class charT, class traits>
99
  void
100
  swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
101
102
typedef basic_ifstream<char>    ifstream;
103
typedef basic_ifstream<wchar_t> wifstream;
104
105
template <class charT, class traits = char_traits<charT> >
106
class basic_ofstream
107
    : public basic_ostream<charT,traits>
108
{
109
public:
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
    explicit basic_ofstream(const filesystem::path& p,
121
                            ios_base::openmode mode = ios_base::out); // 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
139
template <class charT, class traits>
140
  void
141
  swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
142
143
typedef basic_ofstream<char>    ofstream;
144
typedef basic_ofstream<wchar_t> wofstream;
145
146
template <class charT, class traits=char_traits<charT> >
147
class basic_fstream
148
    : public basic_iostream<charT,traits>
149
{
150
public:
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
    explicit basic_fstream(const filesystem::path& p,
162
                           ios_base::openmode mode = ios_base::in|ios_base::out); 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
179
template <class charT, class traits>
180
  void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
181
182
typedef basic_fstream<char>    fstream;
183
typedef basic_fstream<wchar_t> wfstream;
184
185
}  // std
186
187
*/
188
189
#include <__algorithm/max.h>
190
#include <__assert> // all public C++ headers provide the assertion handler
191
#include <__availability>
192
#include <__config>
193
#include <__fwd/fstream.h>
194
#include <__locale>
195
#include <__utility/move.h>
196
#include <__utility/swap.h>
197
#include <__utility/unreachable.h>
198
#include <cstdio>
199
#include <filesystem>
200
#include <istream>
201
#include <ostream>
202
#include <typeinfo>
203
#include <version>
204
205
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
206
#  pragma GCC system_header
207
#endif
208
209
_LIBCPP_PUSH_MACROS
210
#include <__undef_macros>
211
212
#if defined(_LIBCPP_MSVCRT) || defined(_NEWLIB_VERSION)
213
#  define _LIBCPP_HAS_NO_OFF_T_FUNCTIONS
214
#endif
215
216
#if !defined(_LIBCPP_HAS_NO_FILESYSTEM)
217
218
_LIBCPP_BEGIN_NAMESPACE_STD
219
220
#  if _LIBCPP_STD_VER >= 26 && defined(_LIBCPP_WIN32API)
221
_LIBCPP_EXPORTED_FROM_ABI void* __filebuf_windows_native_handle(FILE* __file) noexcept;
222
#  endif
223
224
template <class _CharT, class _Traits>
225
class _LIBCPP_TEMPLATE_VIS basic_filebuf : public basic_streambuf<_CharT, _Traits> {
226
public:
227
  typedef _CharT char_type;
228
  typedef _Traits traits_type;
229
  typedef typename traits_type::int_type int_type;
230
  typedef typename traits_type::pos_type pos_type;
231
  typedef typename traits_type::off_type off_type;
232
  typedef typename traits_type::state_type state_type;
233
#  if _LIBCPP_STD_VER >= 26
234
#    if defined(_LIBCPP_WIN32API)
235
  using native_handle_type = void*; // HANDLE
236
#    elif __has_include(<unistd.h>)
237
  using native_handle_type = int; // POSIX file descriptor
238
#    else
239
#      error "Provide a native file handle!"
240
#    endif
241
#  endif
242
243
  // 27.9.1.2 Constructors/destructor:
244
  basic_filebuf();
245
  basic_filebuf(basic_filebuf&& __rhs);
246
  ~basic_filebuf() override;
247
248
  // 27.9.1.3 Assign/swap:
249
  _LIBCPP_HIDE_FROM_ABI basic_filebuf& operator=(basic_filebuf&& __rhs);
250
  void swap(basic_filebuf& __rhs);
251
252
  // 27.9.1.4 Members:
253
  _LIBCPP_HIDE_FROM_ABI bool is_open() const;
254
  basic_filebuf* open(const char* __s, ios_base::openmode __mode);
255
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
256
  basic_filebuf* open(const wchar_t* __s, ios_base::openmode __mode);
257
#  endif
258
  _LIBCPP_HIDE_FROM_ABI basic_filebuf* open(const string& __s, ios_base::openmode __mode);
259
260
#  if _LIBCPP_STD_VER >= 17
261
  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI basic_filebuf*
262
  open(const filesystem::path& __p, ios_base::openmode __mode) {
263
    return open(__p.c_str(), __mode);
264
  }
265
#  endif
266
  _LIBCPP_HIDE_FROM_ABI basic_filebuf* __open(int __fd, ios_base::openmode __mode);
267
  basic_filebuf* close();
268
#  if _LIBCPP_STD_VER >= 26
269
  _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept {
270
    _LIBCPP_ASSERT_UNCATEGORIZED(this->is_open(), "File must be opened");
271
#    if defined(_LIBCPP_WIN32API)
272
    return std::__filebuf_windows_native_handle(__file_);
273
#    elif __has_include(<unistd.h>)
274
    return fileno(__file_);
275
#    else
276
#      error "Provide a way to determine the file native handle!"
277
#    endif
278
  }
279
#  endif //  _LIBCPP_STD_VER >= 26
280
281
  _LIBCPP_HIDE_FROM_ABI inline static const char* __make_mdstring(ios_base::openmode __mode) _NOEXCEPT;
282
283
protected:
284
  // 27.9.1.5 Overridden virtual functions:
285
  int_type underflow() override;
286
  int_type pbackfail(int_type __c = traits_type::eof()) override;
287
  int_type overflow(int_type __c = traits_type::eof()) override;
288
  basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n) override;
289
  pos_type
290
  seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
291
  pos_type seekpos(pos_type __sp, ios_base::openmode __wch = ios_base::in | ios_base::out) override;
292
  int sync() override;
293
  void imbue(const locale& __loc) override;
294
295
private:
296
  char* __extbuf_;
297
  const char* __extbufnext_;
298
  const char* __extbufend_;
299
  char __extbuf_min_[8];
300
  size_t __ebs_;
301
  char_type* __intbuf_;
302
  size_t __ibs_;
303
  FILE* __file_;
304
  const codecvt<char_type, char, state_type>* __cv_;
305
  state_type __st_;
306
  state_type __st_last_;
307
  ios_base::openmode __om_;
308
  ios_base::openmode __cm_;
309
  bool __owns_eb_;
310
  bool __owns_ib_;
311
  bool __always_noconv_;
312
313
  bool __read_mode();
314
  void __write_mode();
315
316
  _LIBCPP_EXPORTED_FROM_ABI friend FILE* __get_ostream_file(ostream&);
317
};
318
319
template <class _CharT, class _Traits>
320
basic_filebuf<_CharT, _Traits>::basic_filebuf()
321
0
    : __extbuf_(nullptr),
322
0
      __extbufnext_(nullptr),
323
0
      __extbufend_(nullptr),
324
0
      __ebs_(0),
325
0
      __intbuf_(nullptr),
326
0
      __ibs_(0),
327
0
      __file_(nullptr),
328
0
      __cv_(nullptr),
329
0
      __st_(),
330
0
      __st_last_(),
331
0
      __om_(0),
332
0
      __cm_(0),
333
0
      __owns_eb_(false),
334
0
      __owns_ib_(false),
335
0
      __always_noconv_(false) {
336
0
  if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc())) {
337
0
    __cv_            = &std::use_facet<codecvt<char_type, char, state_type> >(this->getloc());
338
0
    __always_noconv_ = __cv_->always_noconv();
339
0
  }
340
0
  setbuf(nullptr, 4096);
341
0
}
342
343
template <class _CharT, class _Traits>
344
0
basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs) : basic_streambuf<_CharT, _Traits>(__rhs) {
345
0
  if (__rhs.__extbuf_ == __rhs.__extbuf_min_) {
346
0
    __extbuf_     = __extbuf_min_;
347
0
    __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
348
0
    __extbufend_  = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
349
0
  } else {
350
0
    __extbuf_     = __rhs.__extbuf_;
351
0
    __extbufnext_ = __rhs.__extbufnext_;
352
0
    __extbufend_  = __rhs.__extbufend_;
353
0
  }
354
0
  __ebs_           = __rhs.__ebs_;
355
0
  __intbuf_        = __rhs.__intbuf_;
356
0
  __ibs_           = __rhs.__ibs_;
357
0
  __file_          = __rhs.__file_;
358
0
  __cv_            = __rhs.__cv_;
359
0
  __st_            = __rhs.__st_;
360
0
  __st_last_       = __rhs.__st_last_;
361
0
  __om_            = __rhs.__om_;
362
0
  __cm_            = __rhs.__cm_;
363
0
  __owns_eb_       = __rhs.__owns_eb_;
364
0
  __owns_ib_       = __rhs.__owns_ib_;
365
0
  __always_noconv_ = __rhs.__always_noconv_;
366
0
  if (__rhs.pbase()) {
367
0
    if (__rhs.pbase() == __rhs.__intbuf_)
368
0
      this->setp(__intbuf_, __intbuf_ + (__rhs.epptr() - __rhs.pbase()));
369
0
    else
370
0
      this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__rhs.epptr() - __rhs.pbase()));
371
0
    this->__pbump(__rhs.pptr() - __rhs.pbase());
372
0
  } else if (__rhs.eback()) {
373
0
    if (__rhs.eback() == __rhs.__intbuf_)
374
0
      this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()), __intbuf_ + (__rhs.egptr() - __rhs.eback()));
375
0
    else
376
0
      this->setg((char_type*)__extbuf_,
377
0
                 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
378
0
                 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
379
0
  }
380
0
  __rhs.__extbuf_     = nullptr;
381
0
  __rhs.__extbufnext_ = nullptr;
382
0
  __rhs.__extbufend_  = nullptr;
383
0
  __rhs.__ebs_        = 0;
384
0
  __rhs.__intbuf_     = 0;
385
0
  __rhs.__ibs_        = 0;
386
0
  __rhs.__file_       = nullptr;
387
0
  __rhs.__st_         = state_type();
388
0
  __rhs.__st_last_    = state_type();
389
0
  __rhs.__om_         = 0;
390
0
  __rhs.__cm_         = 0;
391
0
  __rhs.__owns_eb_    = false;
392
0
  __rhs.__owns_ib_    = false;
393
0
  __rhs.setg(0, 0, 0);
394
0
  __rhs.setp(0, 0);
395
0
}
396
397
template <class _CharT, class _Traits>
398
inline basic_filebuf<_CharT, _Traits>& basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs) {
399
  close();
400
  swap(__rhs);
401
  return *this;
402
}
403
404
template <class _CharT, class _Traits>
405
0
basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
406
0
#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
407
0
  try {
408
0
#  endif // _LIBCPP_HAS_NO_EXCEPTIONS
409
0
    close();
410
0
#  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
411
0
  } catch (...) {
412
0
  }
413
0
#  endif // _LIBCPP_HAS_NO_EXCEPTIONS
414
0
  if (__owns_eb_)
415
0
    delete[] __extbuf_;
416
0
  if (__owns_ib_)
417
0
    delete[] __intbuf_;
418
0
}
419
420
template <class _CharT, class _Traits>
421
0
void basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs) {
422
0
  basic_streambuf<char_type, traits_type>::swap(__rhs);
423
0
  if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
424
    // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
425
0
    std::swap(__extbuf_, __rhs.__extbuf_);
426
0
    std::swap(__extbufnext_, __rhs.__extbufnext_);
427
0
    std::swap(__extbufend_, __rhs.__extbufend_);
428
0
  } else {
429
0
    ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
430
0
    ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
431
0
    ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
432
0
    ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
433
0
    if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) {
434
      // *this uses the small buffer, but __rhs doesn't.
435
0
      __extbuf_       = __rhs.__extbuf_;
436
0
      __rhs.__extbuf_ = __rhs.__extbuf_min_;
437
0
      std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));
438
0
    } else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) {
439
      // *this doesn't use the small buffer, but __rhs does.
440
0
      __rhs.__extbuf_ = __extbuf_;
441
0
      __extbuf_       = __extbuf_min_;
442
0
      std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
443
0
    } else {
444
      // Both *this and __rhs use the small buffer.
445
0
      char __tmp[sizeof(__extbuf_min_)];
446
0
      std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));
447
0
      std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
448
0
      std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));
449
0
    }
450
0
    __extbufnext_       = __extbuf_ + __rn;
451
0
    __extbufend_        = __extbuf_ + __re;
452
0
    __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
453
0
    __rhs.__extbufend_  = __rhs.__extbuf_ + __le;
454
0
  }
455
0
  std::swap(__ebs_, __rhs.__ebs_);
456
0
  std::swap(__intbuf_, __rhs.__intbuf_);
457
0
  std::swap(__ibs_, __rhs.__ibs_);
458
0
  std::swap(__file_, __rhs.__file_);
459
0
  std::swap(__cv_, __rhs.__cv_);
460
0
  std::swap(__st_, __rhs.__st_);
461
0
  std::swap(__st_last_, __rhs.__st_last_);
462
0
  std::swap(__om_, __rhs.__om_);
463
0
  std::swap(__cm_, __rhs.__cm_);
464
0
  std::swap(__owns_eb_, __rhs.__owns_eb_);
465
0
  std::swap(__owns_ib_, __rhs.__owns_ib_);
466
0
  std::swap(__always_noconv_, __rhs.__always_noconv_);
467
0
  if (this->eback() == (char_type*)__rhs.__extbuf_min_) {
468
0
    ptrdiff_t __n = this->gptr() - this->eback();
469
0
    ptrdiff_t __e = this->egptr() - this->eback();
470
0
    this->setg((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __n, (char_type*)__extbuf_min_ + __e);
471
0
  } else if (this->pbase() == (char_type*)__rhs.__extbuf_min_) {
472
0
    ptrdiff_t __n = this->pptr() - this->pbase();
473
0
    ptrdiff_t __e = this->epptr() - this->pbase();
474
0
    this->setp((char_type*)__extbuf_min_, (char_type*)__extbuf_min_ + __e);
475
0
    this->__pbump(__n);
476
0
  }
477
0
  if (__rhs.eback() == (char_type*)__extbuf_min_) {
478
0
    ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
479
0
    ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
480
0
    __rhs.setg(
481
0
        (char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __n, (char_type*)__rhs.__extbuf_min_ + __e);
482
0
  } else if (__rhs.pbase() == (char_type*)__extbuf_min_) {
483
0
    ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
484
0
    ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
485
0
    __rhs.setp((char_type*)__rhs.__extbuf_min_, (char_type*)__rhs.__extbuf_min_ + __e);
486
0
    __rhs.__pbump(__n);
487
0
  }
488
0
}
489
490
template <class _CharT, class _Traits>
491
inline _LIBCPP_HIDE_FROM_ABI void swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y) {
492
  __x.swap(__y);
493
}
494
495
template <class _CharT, class _Traits>
496
inline bool basic_filebuf<_CharT, _Traits>::is_open() const {
497
  return __file_ != nullptr;
498
}
499
500
template <class _CharT, class _Traits>
501
0
const char* basic_filebuf<_CharT, _Traits>::__make_mdstring(ios_base::openmode __mode) _NOEXCEPT {
502
0
  switch (__mode & ~ios_base::ate) {
503
0
  case ios_base::out:
504
0
  case ios_base::out | ios_base::trunc:
505
0
    return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
506
0
  case ios_base::out | ios_base::app:
507
0
  case ios_base::app:
508
0
    return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
509
0
  case ios_base::in:
510
0
    return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
511
0
  case ios_base::in | ios_base::out:
512
0
    return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
513
0
  case ios_base::in | ios_base::out | ios_base::trunc:
514
0
    return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
515
0
  case ios_base::in | ios_base::out | ios_base::app:
516
0
  case ios_base::in | ios_base::app:
517
0
    return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
518
0
  case ios_base::out | ios_base::binary:
519
0
  case ios_base::out | ios_base::trunc | ios_base::binary:
520
0
    return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
521
0
  case ios_base::out | ios_base::app | ios_base::binary:
522
0
  case ios_base::app | ios_base::binary:
523
0
    return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
524
0
  case ios_base::in | ios_base::binary:
525
0
    return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
526
0
  case ios_base::in | ios_base::out | ios_base::binary:
527
0
    return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
528
0
  case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
529
0
    return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
530
0
  case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
531
0
  case ios_base::in | ios_base::app | ios_base::binary:
532
0
    return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
533
0
#  if _LIBCPP_STD_VER >= 23
534
0
  case ios_base::out | ios_base::noreplace:
535
0
  case ios_base::out | ios_base::trunc | ios_base::noreplace:
536
0
    return "wx" _LIBCPP_FOPEN_CLOEXEC_MODE;
537
0
  case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
538
0
    return "w+x" _LIBCPP_FOPEN_CLOEXEC_MODE;
539
0
  case ios_base::out | ios_base::binary | ios_base::noreplace:
540
0
  case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
541
0
    return "wbx" _LIBCPP_FOPEN_CLOEXEC_MODE;
542
0
  case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
543
0
    return "w+bx" _LIBCPP_FOPEN_CLOEXEC_MODE;
544
0
#  endif // _LIBCPP_STD_VER >= 23
545
0
  default:
546
0
    return nullptr;
547
0
  }
548
0
  __libcpp_unreachable();
549
0
}
550
551
template <class _CharT, class _Traits>
552
0
basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
553
0
  basic_filebuf<_CharT, _Traits>* __rt = nullptr;
554
0
  if (__file_ == nullptr) {
555
0
    if (const char* __mdstr = __make_mdstring(__mode)) {
556
0
      __rt    = this;
557
0
      __file_ = fopen(__s, __mdstr);
558
0
      if (__file_) {
559
0
        __om_ = __mode;
560
0
        if (__mode & ios_base::ate) {
561
0
          if (fseek(__file_, 0, SEEK_END)) {
562
0
            fclose(__file_);
563
0
            __file_ = nullptr;
564
0
            __rt    = nullptr;
565
0
          }
566
0
        }
567
0
      } else
568
0
        __rt = nullptr;
569
0
    }
570
0
  }
571
0
  return __rt;
572
0
}
573
574
template <class _CharT, class _Traits>
575
inline basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
576
  basic_filebuf<_CharT, _Traits>* __rt = nullptr;
577
  if (__file_ == nullptr) {
578
    if (const char* __mdstr = __make_mdstring(__mode)) {
579
      __rt    = this;
580
      __file_ = fdopen(__fd, __mdstr);
581
      if (__file_) {
582
        __om_ = __mode;
583
        if (__mode & ios_base::ate) {
584
          if (fseek(__file_, 0, SEEK_END)) {
585
            fclose(__file_);
586
            __file_ = nullptr;
587
            __rt    = nullptr;
588
          }
589
        }
590
      } else
591
        __rt = nullptr;
592
    }
593
  }
594
  return __rt;
595
}
596
597
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
598
// This is basically the same as the char* overload except that it uses _wfopen
599
// and long mode strings.
600
template <class _CharT, class _Traits>
601
basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
602
  basic_filebuf<_CharT, _Traits>* __rt = nullptr;
603
  if (__file_ == nullptr) {
604
    __rt = this;
605
    const wchar_t* __mdstr;
606
    switch (__mode & ~ios_base::ate) {
607
    case ios_base::out:
608
    case ios_base::out | ios_base::trunc:
609
      __mdstr = L"w";
610
      break;
611
    case ios_base::out | ios_base::app:
612
    case ios_base::app:
613
      __mdstr = L"a";
614
      break;
615
    case ios_base::in:
616
      __mdstr = L"r";
617
      break;
618
    case ios_base::in | ios_base::out:
619
      __mdstr = L"r+";
620
      break;
621
    case ios_base::in | ios_base::out | ios_base::trunc:
622
      __mdstr = L"w+";
623
      break;
624
    case ios_base::in | ios_base::out | ios_base::app:
625
    case ios_base::in | ios_base::app:
626
      __mdstr = L"a+";
627
      break;
628
    case ios_base::out | ios_base::binary:
629
    case ios_base::out | ios_base::trunc | ios_base::binary:
630
      __mdstr = L"wb";
631
      break;
632
    case ios_base::out | ios_base::app | ios_base::binary:
633
    case ios_base::app | ios_base::binary:
634
      __mdstr = L"ab";
635
      break;
636
    case ios_base::in | ios_base::binary:
637
      __mdstr = L"rb";
638
      break;
639
    case ios_base::in | ios_base::out | ios_base::binary:
640
      __mdstr = L"r+b";
641
      break;
642
    case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
643
      __mdstr = L"w+b";
644
      break;
645
    case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
646
    case ios_base::in | ios_base::app | ios_base::binary:
647
      __mdstr = L"a+b";
648
      break;
649
#    if _LIBCPP_STD_VER >= 23
650
    case ios_base::out | ios_base::noreplace:
651
    case ios_base::out | ios_base::trunc | ios_base::noreplace:
652
      __mdstr = L"wx";
653
      break;
654
    case ios_base::in | ios_base::out | ios_base::trunc | ios_base::noreplace:
655
      __mdstr = L"w+x";
656
      break;
657
    case ios_base::out | ios_base::binary | ios_base::noreplace:
658
    case ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
659
      __mdstr = L"wbx";
660
      break;
661
    case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary | ios_base::noreplace:
662
      __mdstr = L"w+bx";
663
      break;
664
#    endif // _LIBCPP_STD_VER >= 23
665
    default:
666
      __rt = nullptr;
667
      break;
668
    }
669
    if (__rt) {
670
      __file_ = _wfopen(__s, __mdstr);
671
      if (__file_) {
672
        __om_ = __mode;
673
        if (__mode & ios_base::ate) {
674
          if (fseek(__file_, 0, SEEK_END)) {
675
            fclose(__file_);
676
            __file_ = nullptr;
677
            __rt    = nullptr;
678
          }
679
        }
680
      } else
681
        __rt = nullptr;
682
    }
683
  }
684
  return __rt;
685
}
686
#  endif
687
688
template <class _CharT, class _Traits>
689
inline basic_filebuf<_CharT, _Traits>*
690
0
basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
691
0
  return open(__s.c_str(), __mode);
692
0
}
693
694
template <class _CharT, class _Traits>
695
0
basic_filebuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::close() {
696
0
  basic_filebuf<_CharT, _Traits>* __rt = nullptr;
697
0
  if (__file_) {
698
0
    __rt = this;
699
0
    unique_ptr<FILE, int (*)(FILE*)> __h(__file_, fclose);
700
0
    if (sync())
701
0
      __rt = nullptr;
702
0
    if (fclose(__h.release()))
703
0
      __rt = nullptr;
704
0
    __file_ = nullptr;
705
0
    setbuf(0, 0);
706
0
  }
707
0
  return __rt;
708
0
}
709
710
template <class _CharT, class _Traits>
711
0
typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::underflow() {
712
0
  if (__file_ == nullptr)
713
0
    return traits_type::eof();
714
0
  bool __initial = __read_mode();
715
0
  char_type __1buf;
716
0
  if (this->gptr() == nullptr)
717
0
    this->setg(&__1buf, &__1buf + 1, &__1buf + 1);
718
0
  const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
719
0
  int_type __c            = traits_type::eof();
720
0
  if (this->gptr() == this->egptr()) {
721
0
    std::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
722
0
    if (__always_noconv_) {
723
0
      size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
724
0
      __nmemb        = ::fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
725
0
      if (__nmemb != 0) {
726
0
        this->setg(this->eback(), this->eback() + __unget_sz, this->eback() + __unget_sz + __nmemb);
727
0
        __c = traits_type::to_int_type(*this->gptr());
728
0
      }
729
0
    } else {
730
0
      if (__extbufend_ != __extbufnext_) {
731
0
        _LIBCPP_ASSERT_NON_NULL(__extbufnext_ != nullptr, "underflow moving from nullptr");
732
0
        _LIBCPP_ASSERT_NON_NULL(__extbuf_ != nullptr, "underflow moving into nullptr");
733
0
        std::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
734
0
      }
735
0
      __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
736
0
      __extbufend_  = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
737
0
      size_t __nmemb =
738
0
          std::min(static_cast<size_t>(__ibs_ - __unget_sz), static_cast<size_t>(__extbufend_ - __extbufnext_));
739
0
      codecvt_base::result __r;
740
0
      __st_last_  = __st_;
741
0
      size_t __nr = fread((void*)const_cast<char*>(__extbufnext_), 1, __nmemb, __file_);
742
0
      if (__nr != 0) {
743
0
        if (!__cv_)
744
0
          __throw_bad_cast();
745
746
0
        __extbufend_ = __extbufnext_ + __nr;
747
0
        char_type* __inext;
748
0
        __r = __cv_->in(
749
0
            __st_, __extbuf_, __extbufend_, __extbufnext_, this->eback() + __unget_sz, this->eback() + __ibs_, __inext);
750
0
        if (__r == codecvt_base::noconv) {
751
0
          this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)const_cast<char*>(__extbufend_));
752
0
          __c = traits_type::to_int_type(*this->gptr());
753
0
        } else if (__inext != this->eback() + __unget_sz) {
754
0
          this->setg(this->eback(), this->eback() + __unget_sz, __inext);
755
0
          __c = traits_type::to_int_type(*this->gptr());
756
0
        }
757
0
      }
758
0
    }
759
0
  } else
760
0
    __c = traits_type::to_int_type(*this->gptr());
761
0
  if (this->eback() == &__1buf)
762
0
    this->setg(nullptr, nullptr, nullptr);
763
0
  return __c;
764
0
}
765
766
template <class _CharT, class _Traits>
767
0
typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
768
0
  if (__file_ && this->eback() < this->gptr()) {
769
0
    if (traits_type::eq_int_type(__c, traits_type::eof())) {
770
0
      this->gbump(-1);
771
0
      return traits_type::not_eof(__c);
772
0
    }
773
0
    if ((__om_ & ios_base::out) || traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1])) {
774
0
      this->gbump(-1);
775
0
      *this->gptr() = traits_type::to_char_type(__c);
776
0
      return __c;
777
0
    }
778
0
  }
779
0
  return traits_type::eof();
780
0
}
781
782
template <class _CharT, class _Traits>
783
0
typename basic_filebuf<_CharT, _Traits>::int_type basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
784
0
  if (__file_ == nullptr)
785
0
    return traits_type::eof();
786
0
  __write_mode();
787
0
  char_type __1buf;
788
0
  char_type* __pb_save  = this->pbase();
789
0
  char_type* __epb_save = this->epptr();
790
0
  if (!traits_type::eq_int_type(__c, traits_type::eof())) {
791
0
    if (this->pptr() == nullptr)
792
0
      this->setp(&__1buf, &__1buf + 1);
793
0
    *this->pptr() = traits_type::to_char_type(__c);
794
0
    this->pbump(1);
795
0
  }
796
0
  if (this->pptr() != this->pbase()) {
797
0
    if (__always_noconv_) {
798
0
      size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
799
0
      if (std::fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
800
0
        return traits_type::eof();
801
0
    } else {
802
0
      char* __extbe = __extbuf_;
803
0
      codecvt_base::result __r;
804
0
      do {
805
0
        if (!__cv_)
806
0
          __throw_bad_cast();
807
808
0
        const char_type* __e;
809
0
        __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e, __extbuf_, __extbuf_ + __ebs_, __extbe);
810
0
        if (__e == this->pbase())
811
0
          return traits_type::eof();
812
0
        if (__r == codecvt_base::noconv) {
813
0
          size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
814
0
          if (std::fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
815
0
            return traits_type::eof();
816
0
        } else if (__r == codecvt_base::ok || __r == codecvt_base::partial) {
817
0
          size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
818
0
          if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
819
0
            return traits_type::eof();
820
0
          if (__r == codecvt_base::partial) {
821
0
            this->setp(const_cast<char_type*>(__e), this->pptr());
822
0
            this->__pbump(this->epptr() - this->pbase());
823
0
          }
824
0
        } else
825
0
          return traits_type::eof();
826
0
      } while (__r == codecvt_base::partial);
827
0
    }
828
0
    this->setp(__pb_save, __epb_save);
829
0
  }
830
0
  return traits_type::not_eof(__c);
831
0
}
832
833
template <class _CharT, class _Traits>
834
0
basic_streambuf<_CharT, _Traits>* basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n) {
835
0
  this->setg(nullptr, nullptr, nullptr);
836
0
  this->setp(nullptr, nullptr);
837
0
  if (__owns_eb_)
838
0
    delete[] __extbuf_;
839
0
  if (__owns_ib_)
840
0
    delete[] __intbuf_;
841
0
  __ebs_ = __n;
842
0
  if (__ebs_ > sizeof(__extbuf_min_)) {
843
0
    if (__always_noconv_ && __s) {
844
0
      __extbuf_  = (char*)__s;
845
0
      __owns_eb_ = false;
846
0
    } else {
847
0
      __extbuf_  = new char[__ebs_];
848
0
      __owns_eb_ = true;
849
0
    }
850
0
  } else {
851
0
    __extbuf_  = __extbuf_min_;
852
0
    __ebs_     = sizeof(__extbuf_min_);
853
0
    __owns_eb_ = false;
854
0
  }
855
0
  if (!__always_noconv_) {
856
0
    __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
857
0
    if (__s && __ibs_ > sizeof(__extbuf_min_)) {
858
0
      __intbuf_  = __s;
859
0
      __owns_ib_ = false;
860
0
    } else {
861
0
      __intbuf_  = new char_type[__ibs_];
862
0
      __owns_ib_ = true;
863
0
    }
864
0
  } else {
865
0
    __ibs_     = 0;
866
0
    __intbuf_  = nullptr;
867
0
    __owns_ib_ = false;
868
0
  }
869
0
  return this;
870
0
}
871
872
template <class _CharT, class _Traits>
873
typename basic_filebuf<_CharT, _Traits>::pos_type
874
0
basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode) {
875
0
  if (!__cv_)
876
0
    __throw_bad_cast();
877
878
0
  int __width = __cv_->encoding();
879
0
  if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
880
0
    return pos_type(off_type(-1));
881
  // __width > 0 || __off == 0
882
0
  int __whence;
883
0
  switch (__way) {
884
0
  case ios_base::beg:
885
0
    __whence = SEEK_SET;
886
0
    break;
887
0
  case ios_base::cur:
888
0
    __whence = SEEK_CUR;
889
0
    break;
890
0
  case ios_base::end:
891
0
    __whence = SEEK_END;
892
0
    break;
893
0
  default:
894
0
    return pos_type(off_type(-1));
895
0
  }
896
#  if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
897
  if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
898
    return pos_type(off_type(-1));
899
  pos_type __r = ftell(__file_);
900
#  else
901
0
  if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
902
0
    return pos_type(off_type(-1));
903
0
  pos_type __r = ftello(__file_);
904
0
#  endif
905
0
  __r.state(__st_);
906
0
  return __r;
907
0
}
908
909
template <class _CharT, class _Traits>
910
typename basic_filebuf<_CharT, _Traits>::pos_type
911
0
basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode) {
912
0
  if (__file_ == nullptr || sync())
913
0
    return pos_type(off_type(-1));
914
#  if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
915
  if (fseek(__file_, __sp, SEEK_SET))
916
    return pos_type(off_type(-1));
917
#  else
918
0
  if (::fseeko(__file_, __sp, SEEK_SET))
919
0
    return pos_type(off_type(-1));
920
0
#  endif
921
0
  __st_ = __sp.state();
922
0
  return __sp;
923
0
}
924
925
template <class _CharT, class _Traits>
926
0
int basic_filebuf<_CharT, _Traits>::sync() {
927
0
  if (__file_ == nullptr)
928
0
    return 0;
929
0
  if (!__cv_)
930
0
    __throw_bad_cast();
931
932
0
  if (__cm_ & ios_base::out) {
933
0
    if (this->pptr() != this->pbase())
934
0
      if (overflow() == traits_type::eof())
935
0
        return -1;
936
0
    codecvt_base::result __r;
937
0
    do {
938
0
      char* __extbe;
939
0
      __r            = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
940
0
      size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
941
0
      if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
942
0
        return -1;
943
0
    } while (__r == codecvt_base::partial);
944
0
    if (__r == codecvt_base::error)
945
0
      return -1;
946
0
    if (fflush(__file_))
947
0
      return -1;
948
0
  } else if (__cm_ & ios_base::in) {
949
0
    off_type __c;
950
0
    state_type __state = __st_last_;
951
0
    bool __update_st   = false;
952
0
    if (__always_noconv_)
953
0
      __c = this->egptr() - this->gptr();
954
0
    else {
955
0
      int __width = __cv_->encoding();
956
0
      __c         = __extbufend_ - __extbufnext_;
957
0
      if (__width > 0)
958
0
        __c += __width * (this->egptr() - this->gptr());
959
0
      else {
960
0
        if (this->gptr() != this->egptr()) {
961
0
          const int __off = __cv_->length(__state, __extbuf_, __extbufnext_, this->gptr() - this->eback());
962
0
          __c += __extbufnext_ - __extbuf_ - __off;
963
0
          __update_st = true;
964
0
        }
965
0
      }
966
0
    }
967
#  if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
968
    if (fseek(__file_, -__c, SEEK_CUR))
969
      return -1;
970
#  else
971
0
    if (::fseeko(__file_, -__c, SEEK_CUR))
972
0
      return -1;
973
0
#  endif
974
0
    if (__update_st)
975
0
      __st_ = __state;
976
0
    __extbufnext_ = __extbufend_ = __extbuf_;
977
0
    this->setg(nullptr, nullptr, nullptr);
978
0
    __cm_ = 0;
979
0
  }
980
0
  return 0;
981
0
}
982
983
template <class _CharT, class _Traits>
984
0
void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
985
0
  sync();
986
0
  __cv_            = &std::use_facet<codecvt<char_type, char, state_type> >(__loc);
987
0
  bool __old_anc   = __always_noconv_;
988
0
  __always_noconv_ = __cv_->always_noconv();
989
0
  if (__old_anc != __always_noconv_) {
990
0
    this->setg(nullptr, nullptr, nullptr);
991
0
    this->setp(nullptr, nullptr);
992
    // invariant, char_type is char, else we couldn't get here
993
0
    if (__always_noconv_) // need to dump __intbuf_
994
0
    {
995
0
      if (__owns_eb_)
996
0
        delete[] __extbuf_;
997
0
      __owns_eb_ = __owns_ib_;
998
0
      __ebs_     = __ibs_;
999
0
      __extbuf_  = (char*)__intbuf_;
1000
0
      __ibs_     = 0;
1001
0
      __intbuf_  = nullptr;
1002
0
      __owns_ib_ = false;
1003
0
    } else // need to obtain an __intbuf_.
1004
0
    {      // If __extbuf_ is user-supplied, use it, else new __intbuf_
1005
0
      if (!__owns_eb_ && __extbuf_ != __extbuf_min_) {
1006
0
        __ibs_     = __ebs_;
1007
0
        __intbuf_  = (char_type*)__extbuf_;
1008
0
        __owns_ib_ = false;
1009
0
        __extbuf_  = new char[__ebs_];
1010
0
        __owns_eb_ = true;
1011
0
      } else {
1012
0
        __ibs_     = __ebs_;
1013
0
        __intbuf_  = new char_type[__ibs_];
1014
0
        __owns_ib_ = true;
1015
0
      }
1016
0
    }
1017
0
  }
1018
0
}
1019
1020
template <class _CharT, class _Traits>
1021
0
bool basic_filebuf<_CharT, _Traits>::__read_mode() {
1022
0
  if (!(__cm_ & ios_base::in)) {
1023
0
    this->setp(nullptr, nullptr);
1024
0
    if (__always_noconv_)
1025
0
      this->setg((char_type*)__extbuf_, (char_type*)__extbuf_ + __ebs_, (char_type*)__extbuf_ + __ebs_);
1026
0
    else
1027
0
      this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1028
0
    __cm_ = ios_base::in;
1029
0
    return true;
1030
0
  }
1031
0
  return false;
1032
0
}
1033
1034
template <class _CharT, class _Traits>
1035
0
void basic_filebuf<_CharT, _Traits>::__write_mode() {
1036
0
  if (!(__cm_ & ios_base::out)) {
1037
0
    this->setg(nullptr, nullptr, nullptr);
1038
0
    if (__ebs_ > sizeof(__extbuf_min_)) {
1039
0
      if (__always_noconv_)
1040
0
        this->setp((char_type*)__extbuf_, (char_type*)__extbuf_ + (__ebs_ - 1));
1041
0
      else
1042
0
        this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1043
0
    } else
1044
0
      this->setp(nullptr, nullptr);
1045
0
    __cm_ = ios_base::out;
1046
0
  }
1047
0
}
1048
1049
// basic_ifstream
1050
1051
template <class _CharT, class _Traits>
1052
class _LIBCPP_TEMPLATE_VIS basic_ifstream : public basic_istream<_CharT, _Traits> {
1053
public:
1054
  typedef _CharT char_type;
1055
  typedef _Traits traits_type;
1056
  typedef typename traits_type::int_type int_type;
1057
  typedef typename traits_type::pos_type pos_type;
1058
  typedef typename traits_type::off_type off_type;
1059
#  if _LIBCPP_STD_VER >= 26
1060
  using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1061
#  endif
1062
1063
  _LIBCPP_HIDE_FROM_ABI basic_ifstream();
1064
  _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1065
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1066
  _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1067
#  endif
1068
  _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1069
#  if _LIBCPP_STD_VER >= 17
1070
  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_ifstream(
1071
      const filesystem::path& __p, ios_base::openmode __mode = ios_base::in)
1072
      : basic_ifstream(__p.c_str(), __mode) {}
1073
#  endif // _LIBCPP_STD_VER >= 17
1074
  _LIBCPP_HIDE_FROM_ABI basic_ifstream(basic_ifstream&& __rhs);
1075
  _LIBCPP_HIDE_FROM_ABI basic_ifstream& operator=(basic_ifstream&& __rhs);
1076
  _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream& __rhs);
1077
1078
  _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1079
#  if _LIBCPP_STD_VER >= 26
1080
  _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1081
#  endif
1082
  _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1083
  void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1084
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1085
  void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in);
1086
#  endif
1087
  void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1088
#  if _LIBCPP_STD_VER >= 17
1089
  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1090
  open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in) {
1091
    return open(__p.c_str(), __mode);
1092
  }
1093
#  endif // _LIBCPP_STD_VER >= 17
1094
1095
  _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1096
  _LIBCPP_HIDE_FROM_ABI void close();
1097
1098
private:
1099
  basic_filebuf<char_type, traits_type> __sb_;
1100
};
1101
1102
template <class _CharT, class _Traits>
1103
inline basic_ifstream<_CharT, _Traits>::basic_ifstream() : basic_istream<char_type, traits_type>(&__sb_) {}
1104
1105
template <class _CharT, class _Traits>
1106
inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1107
    : basic_istream<char_type, traits_type>(&__sb_) {
1108
  if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1109
    this->setstate(ios_base::failbit);
1110
}
1111
1112
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1113
template <class _CharT, class _Traits>
1114
inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const wchar_t* __s, ios_base::openmode __mode)
1115
    : basic_istream<char_type, traits_type>(&__sb_) {
1116
  if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1117
    this->setstate(ios_base::failbit);
1118
}
1119
#  endif
1120
1121
template <class _CharT, class _Traits>
1122
inline basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1123
    : basic_istream<char_type, traits_type>(&__sb_) {
1124
  if (__sb_.open(__s, __mode | ios_base::in) == nullptr)
1125
    this->setstate(ios_base::failbit);
1126
}
1127
1128
template <class _CharT, class _Traits>
1129
inline basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1130
    : basic_istream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1131
  this->set_rdbuf(&__sb_);
1132
}
1133
1134
template <class _CharT, class _Traits>
1135
inline basic_ifstream<_CharT, _Traits>& basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs) {
1136
  basic_istream<char_type, traits_type>::operator=(std::move(__rhs));
1137
  __sb_ = std::move(__rhs.__sb_);
1138
  return *this;
1139
}
1140
1141
template <class _CharT, class _Traits>
1142
inline void basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs) {
1143
  basic_istream<char_type, traits_type>::swap(__rhs);
1144
  __sb_.swap(__rhs.__sb_);
1145
}
1146
1147
template <class _CharT, class _Traits>
1148
inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y) {
1149
  __x.swap(__y);
1150
}
1151
1152
template <class _CharT, class _Traits>
1153
inline basic_filebuf<_CharT, _Traits>* basic_ifstream<_CharT, _Traits>::rdbuf() const {
1154
  return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1155
}
1156
1157
template <class _CharT, class _Traits>
1158
inline bool basic_ifstream<_CharT, _Traits>::is_open() const {
1159
  return __sb_.is_open();
1160
}
1161
1162
template <class _CharT, class _Traits>
1163
0
void basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1164
0
  if (__sb_.open(__s, __mode | ios_base::in))
1165
0
    this->clear();
1166
0
  else
1167
0
    this->setstate(ios_base::failbit);
1168
0
}
1169
1170
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1171
template <class _CharT, class _Traits>
1172
void basic_ifstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1173
  if (__sb_.open(__s, __mode | ios_base::in))
1174
    this->clear();
1175
  else
1176
    this->setstate(ios_base::failbit);
1177
}
1178
#  endif
1179
1180
template <class _CharT, class _Traits>
1181
0
void basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1182
0
  if (__sb_.open(__s, __mode | ios_base::in))
1183
0
    this->clear();
1184
0
  else
1185
0
    this->setstate(ios_base::failbit);
1186
0
}
1187
1188
template <class _CharT, class _Traits>
1189
inline void basic_ifstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1190
  if (__sb_.__open(__fd, __mode | ios_base::in))
1191
    this->clear();
1192
  else
1193
    this->setstate(ios_base::failbit);
1194
}
1195
1196
template <class _CharT, class _Traits>
1197
inline void basic_ifstream<_CharT, _Traits>::close() {
1198
  if (__sb_.close() == 0)
1199
    this->setstate(ios_base::failbit);
1200
}
1201
1202
// basic_ofstream
1203
1204
template <class _CharT, class _Traits>
1205
class _LIBCPP_TEMPLATE_VIS basic_ofstream : public basic_ostream<_CharT, _Traits> {
1206
public:
1207
  typedef _CharT char_type;
1208
  typedef _Traits traits_type;
1209
  typedef typename traits_type::int_type int_type;
1210
  typedef typename traits_type::pos_type pos_type;
1211
  typedef typename traits_type::off_type off_type;
1212
#  if _LIBCPP_STD_VER >= 26
1213
  using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1214
#  endif
1215
1216
  _LIBCPP_HIDE_FROM_ABI basic_ofstream();
1217
  _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1218
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1219
  _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1220
#  endif
1221
  _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1222
1223
#  if _LIBCPP_STD_VER >= 17
1224
  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_ofstream(
1225
      const filesystem::path& __p, ios_base::openmode __mode = ios_base::out)
1226
      : basic_ofstream(__p.c_str(), __mode) {}
1227
#  endif // _LIBCPP_STD_VER >= 17
1228
1229
  _LIBCPP_HIDE_FROM_ABI basic_ofstream(basic_ofstream&& __rhs);
1230
  _LIBCPP_HIDE_FROM_ABI basic_ofstream& operator=(basic_ofstream&& __rhs);
1231
  _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream& __rhs);
1232
1233
  _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1234
#  if _LIBCPP_STD_VER >= 26
1235
  _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1236
#  endif
1237
  _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1238
  void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1239
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1240
  void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::out);
1241
#  endif
1242
  void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1243
1244
#  if _LIBCPP_STD_VER >= 17
1245
  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1246
  open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::out) {
1247
    return open(__p.c_str(), __mode);
1248
  }
1249
#  endif // _LIBCPP_STD_VER >= 17
1250
1251
  _LIBCPP_HIDE_FROM_ABI void __open(int __fd, ios_base::openmode __mode);
1252
  _LIBCPP_HIDE_FROM_ABI void close();
1253
1254
private:
1255
  basic_filebuf<char_type, traits_type> __sb_;
1256
};
1257
1258
template <class _CharT, class _Traits>
1259
inline basic_ofstream<_CharT, _Traits>::basic_ofstream() : basic_ostream<char_type, traits_type>(&__sb_) {}
1260
1261
template <class _CharT, class _Traits>
1262
inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1263
    : basic_ostream<char_type, traits_type>(&__sb_) {
1264
  if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1265
    this->setstate(ios_base::failbit);
1266
}
1267
1268
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1269
template <class _CharT, class _Traits>
1270
inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const wchar_t* __s, ios_base::openmode __mode)
1271
    : basic_ostream<char_type, traits_type>(&__sb_) {
1272
  if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1273
    this->setstate(ios_base::failbit);
1274
}
1275
#  endif
1276
1277
template <class _CharT, class _Traits>
1278
inline basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1279
    : basic_ostream<char_type, traits_type>(&__sb_) {
1280
  if (__sb_.open(__s, __mode | ios_base::out) == nullptr)
1281
    this->setstate(ios_base::failbit);
1282
}
1283
1284
template <class _CharT, class _Traits>
1285
inline basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1286
    : basic_ostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1287
  this->set_rdbuf(&__sb_);
1288
}
1289
1290
template <class _CharT, class _Traits>
1291
inline basic_ofstream<_CharT, _Traits>& basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs) {
1292
  basic_ostream<char_type, traits_type>::operator=(std::move(__rhs));
1293
  __sb_ = std::move(__rhs.__sb_);
1294
  return *this;
1295
}
1296
1297
template <class _CharT, class _Traits>
1298
inline void basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs) {
1299
  basic_ostream<char_type, traits_type>::swap(__rhs);
1300
  __sb_.swap(__rhs.__sb_);
1301
}
1302
1303
template <class _CharT, class _Traits>
1304
inline _LIBCPP_HIDE_FROM_ABI void swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y) {
1305
  __x.swap(__y);
1306
}
1307
1308
template <class _CharT, class _Traits>
1309
inline basic_filebuf<_CharT, _Traits>* basic_ofstream<_CharT, _Traits>::rdbuf() const {
1310
  return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1311
}
1312
1313
template <class _CharT, class _Traits>
1314
inline bool basic_ofstream<_CharT, _Traits>::is_open() const {
1315
  return __sb_.is_open();
1316
}
1317
1318
template <class _CharT, class _Traits>
1319
0
void basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1320
0
  if (__sb_.open(__s, __mode | ios_base::out))
1321
0
    this->clear();
1322
0
  else
1323
0
    this->setstate(ios_base::failbit);
1324
0
}
1325
1326
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1327
template <class _CharT, class _Traits>
1328
void basic_ofstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1329
  if (__sb_.open(__s, __mode | ios_base::out))
1330
    this->clear();
1331
  else
1332
    this->setstate(ios_base::failbit);
1333
}
1334
#  endif
1335
1336
template <class _CharT, class _Traits>
1337
0
void basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1338
0
  if (__sb_.open(__s, __mode | ios_base::out))
1339
0
    this->clear();
1340
0
  else
1341
0
    this->setstate(ios_base::failbit);
1342
0
}
1343
1344
template <class _CharT, class _Traits>
1345
inline void basic_ofstream<_CharT, _Traits>::__open(int __fd, ios_base::openmode __mode) {
1346
  if (__sb_.__open(__fd, __mode | ios_base::out))
1347
    this->clear();
1348
  else
1349
    this->setstate(ios_base::failbit);
1350
}
1351
1352
template <class _CharT, class _Traits>
1353
inline void basic_ofstream<_CharT, _Traits>::close() {
1354
  if (__sb_.close() == nullptr)
1355
    this->setstate(ios_base::failbit);
1356
}
1357
1358
// basic_fstream
1359
1360
template <class _CharT, class _Traits>
1361
class _LIBCPP_TEMPLATE_VIS basic_fstream : public basic_iostream<_CharT, _Traits> {
1362
public:
1363
  typedef _CharT char_type;
1364
  typedef _Traits traits_type;
1365
  typedef typename traits_type::int_type int_type;
1366
  typedef typename traits_type::pos_type pos_type;
1367
  typedef typename traits_type::off_type off_type;
1368
#  if _LIBCPP_STD_VER >= 26
1369
  using native_handle_type = typename basic_filebuf<_CharT, _Traits>::native_handle_type;
1370
#  endif
1371
1372
  _LIBCPP_HIDE_FROM_ABI basic_fstream();
1373
  _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const char* __s,
1374
                                               ios_base::openmode __mode = ios_base::in | ios_base::out);
1375
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1376
  _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const wchar_t* __s,
1377
                                               ios_base::openmode __mode = ios_base::in | ios_base::out);
1378
#  endif
1379
  _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(const string& __s,
1380
                                               ios_base::openmode __mode = ios_base::in | ios_base::out);
1381
1382
#  if _LIBCPP_STD_VER >= 17
1383
  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI explicit basic_fstream(
1384
      const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out)
1385
      : basic_fstream(__p.c_str(), __mode) {}
1386
#  endif // _LIBCPP_STD_VER >= 17
1387
1388
  _LIBCPP_HIDE_FROM_ABI basic_fstream(basic_fstream&& __rhs);
1389
1390
  _LIBCPP_HIDE_FROM_ABI basic_fstream& operator=(basic_fstream&& __rhs);
1391
1392
  _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream& __rhs);
1393
1394
  _LIBCPP_HIDE_FROM_ABI basic_filebuf<char_type, traits_type>* rdbuf() const;
1395
#  if _LIBCPP_STD_VER >= 26
1396
  _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() const noexcept { return rdbuf()->native_handle(); }
1397
#  endif
1398
  _LIBCPP_HIDE_FROM_ABI bool is_open() const;
1399
  _LIBCPP_HIDE_FROM_ABI void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1400
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1401
  void open(const wchar_t* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1402
#  endif
1403
  _LIBCPP_HIDE_FROM_ABI void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1404
1405
#  if _LIBCPP_STD_VER >= 17
1406
  _LIBCPP_AVAILABILITY_FILESYSTEM_LIBRARY _LIBCPP_HIDE_FROM_ABI void
1407
  open(const filesystem::path& __p, ios_base::openmode __mode = ios_base::in | ios_base::out) {
1408
    return open(__p.c_str(), __mode);
1409
  }
1410
#  endif // _LIBCPP_STD_VER >= 17
1411
1412
  _LIBCPP_HIDE_FROM_ABI void close();
1413
1414
private:
1415
  basic_filebuf<char_type, traits_type> __sb_;
1416
};
1417
1418
template <class _CharT, class _Traits>
1419
inline basic_fstream<_CharT, _Traits>::basic_fstream() : basic_iostream<char_type, traits_type>(&__sb_) {}
1420
1421
template <class _CharT, class _Traits>
1422
inline basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1423
    : basic_iostream<char_type, traits_type>(&__sb_) {
1424
  if (__sb_.open(__s, __mode) == nullptr)
1425
    this->setstate(ios_base::failbit);
1426
}
1427
1428
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1429
template <class _CharT, class _Traits>
1430
inline basic_fstream<_CharT, _Traits>::basic_fstream(const wchar_t* __s, ios_base::openmode __mode)
1431
    : basic_iostream<char_type, traits_type>(&__sb_) {
1432
  if (__sb_.open(__s, __mode) == nullptr)
1433
    this->setstate(ios_base::failbit);
1434
}
1435
#  endif
1436
1437
template <class _CharT, class _Traits>
1438
inline basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1439
    : basic_iostream<char_type, traits_type>(&__sb_) {
1440
  if (__sb_.open(__s, __mode) == nullptr)
1441
    this->setstate(ios_base::failbit);
1442
}
1443
1444
template <class _CharT, class _Traits>
1445
inline basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1446
    : basic_iostream<char_type, traits_type>(std::move(__rhs)), __sb_(std::move(__rhs.__sb_)) {
1447
  this->set_rdbuf(&__sb_);
1448
}
1449
1450
template <class _CharT, class _Traits>
1451
inline basic_fstream<_CharT, _Traits>& basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs) {
1452
  basic_iostream<char_type, traits_type>::operator=(std::move(__rhs));
1453
  __sb_ = std::move(__rhs.__sb_);
1454
  return *this;
1455
}
1456
1457
template <class _CharT, class _Traits>
1458
inline void basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs) {
1459
  basic_iostream<char_type, traits_type>::swap(__rhs);
1460
  __sb_.swap(__rhs.__sb_);
1461
}
1462
1463
template <class _CharT, class _Traits>
1464
inline _LIBCPP_HIDE_FROM_ABI void swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y) {
1465
  __x.swap(__y);
1466
}
1467
1468
template <class _CharT, class _Traits>
1469
inline basic_filebuf<_CharT, _Traits>* basic_fstream<_CharT, _Traits>::rdbuf() const {
1470
  return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1471
}
1472
1473
template <class _CharT, class _Traits>
1474
inline bool basic_fstream<_CharT, _Traits>::is_open() const {
1475
  return __sb_.is_open();
1476
}
1477
1478
template <class _CharT, class _Traits>
1479
void basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode) {
1480
  if (__sb_.open(__s, __mode))
1481
    this->clear();
1482
  else
1483
    this->setstate(ios_base::failbit);
1484
}
1485
1486
#  ifdef _LIBCPP_HAS_OPEN_WITH_WCHAR
1487
template <class _CharT, class _Traits>
1488
void basic_fstream<_CharT, _Traits>::open(const wchar_t* __s, ios_base::openmode __mode) {
1489
  if (__sb_.open(__s, __mode))
1490
    this->clear();
1491
  else
1492
    this->setstate(ios_base::failbit);
1493
}
1494
#  endif
1495
1496
template <class _CharT, class _Traits>
1497
void basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode) {
1498
  if (__sb_.open(__s, __mode))
1499
    this->clear();
1500
  else
1501
    this->setstate(ios_base::failbit);
1502
}
1503
1504
template <class _CharT, class _Traits>
1505
inline void basic_fstream<_CharT, _Traits>::close() {
1506
  if (__sb_.close() == nullptr)
1507
    this->setstate(ios_base::failbit);
1508
}
1509
1510
#  if _LIBCPP_AVAILABILITY_HAS_ADDITIONAL_IOSTREAM_EXPLICIT_INSTANTIATIONS_1
1511
extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ifstream<char>;
1512
extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ofstream<char>;
1513
extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_filebuf<char>;
1514
#  endif
1515
1516
_LIBCPP_END_NAMESPACE_STD
1517
1518
#endif // _LIBCPP_HAS_NO_FILESYSTEM
1519
1520
_LIBCPP_POP_MACROS
1521
1522
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1523
#  include <atomic>
1524
#  include <concepts>
1525
#  include <cstdlib>
1526
#  include <iosfwd>
1527
#  include <limits>
1528
#  include <mutex>
1529
#  include <new>
1530
#  include <stdexcept>
1531
#  include <type_traits>
1532
#endif
1533
1534
#endif // _LIBCPP_FSTREAM