Coverage Report

Created: 2024-09-08 06:18

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