Debian Codegraph @

llvm-project/libcxx/include/fstream

raw
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 {
316 if (std::has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
317 {
318 __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(this->getloc());
319 __always_noconv_ = __cv_->always_noconv();
320 }
321 setbuf(nullptr, 4096);
322 }
323
324 template <class _CharT, class _Traits>
325 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
326 : basic_streambuf<_CharT, _Traits>(__rhs)
327 {
328 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
329 {
330 __extbuf_ = __extbuf_min_;
331 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
332 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
333 }
334 else
335 {
336 __extbuf_ = __rhs.__extbuf_;
337 __extbufnext_ = __rhs.__extbufnext_;
338 __extbufend_ = __rhs.__extbufend_;
339 }
340 __ebs_ = __rhs.__ebs_;
341 __intbuf_ = __rhs.__intbuf_;
342 __ibs_ = __rhs.__ibs_;
343 __file_ = __rhs.__file_;
344 __cv_ = __rhs.__cv_;
345 __st_ = __rhs.__st_;
346 __st_last_ = __rhs.__st_last_;
347 __om_ = __rhs.__om_;
348 __cm_ = __rhs.__cm_;
349 __owns_eb_ = __rhs.__owns_eb_;
350 __owns_ib_ = __rhs.__owns_ib_;
351 __always_noconv_ = __rhs.__always_noconv_;
352 if (__rhs.pbase())
353 {
354 if (__rhs.pbase() == __rhs.__intbuf_)
355 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
356 else
357 this->setp((char_type*)__extbuf_,
358 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
359 this->__pbump(__rhs. pptr() - __rhs.pbase());
360 }
361 else if (__rhs.eback())
362 {
363 if (__rhs.eback() == __rhs.__intbuf_)
364 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
365 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
366 else
367 this->setg((char_type*)__extbuf_,
368 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
369 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
370 }
371 __rhs.__extbuf_ = nullptr;
372 __rhs.__extbufnext_ = nullptr;
373 __rhs.__extbufend_ = nullptr;
374 __rhs.__ebs_ = 0;
375 __rhs.__intbuf_ = 0;
376 __rhs.__ibs_ = 0;
377 __rhs.__file_ = nullptr;
378 __rhs.__st_ = state_type();
379 __rhs.__st_last_ = state_type();
380 __rhs.__om_ = 0;
381 __rhs.__cm_ = 0;
382 __rhs.__owns_eb_ = false;
383 __rhs.__owns_ib_ = false;
384 __rhs.setg(0, 0, 0);
385 __rhs.setp(0, 0);
386 }
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 {
401 #ifndef _LIBCPP_NO_EXCEPTIONS
402 try
403 {
404 #endif // _LIBCPP_NO_EXCEPTIONS
405 close();
406 #ifndef _LIBCPP_NO_EXCEPTIONS
407 }
408 catch (...)
409 {
410 }
411 #endif // _LIBCPP_NO_EXCEPTIONS
412 if (__owns_eb_)
413 delete [] __extbuf_;
414 if (__owns_ib_)
415 delete [] __intbuf_;
416 }
417
418 template <class _CharT, class _Traits>
419 void
420 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
421 {
422 basic_streambuf<char_type, traits_type>::swap(__rhs);
423 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
424 {
425 // Neither *this nor __rhs uses the small buffer, so we can simply swap the pointers.
426 std::swap(__extbuf_, __rhs.__extbuf_);
427 std::swap(__extbufnext_, __rhs.__extbufnext_);
428 std::swap(__extbufend_, __rhs.__extbufend_);
429 }
430 else
431 {
432 ptrdiff_t __ln = __extbufnext_ ? __extbufnext_ - __extbuf_ : 0;
433 ptrdiff_t __le = __extbufend_ ? __extbufend_ - __extbuf_ : 0;
434 ptrdiff_t __rn = __rhs.__extbufnext_ ? __rhs.__extbufnext_ - __rhs.__extbuf_ : 0;
435 ptrdiff_t __re = __rhs.__extbufend_ ? __rhs.__extbufend_ - __rhs.__extbuf_ : 0;
436 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
437 {
438 // *this uses the small buffer, but __rhs doesn't.
439 __extbuf_ = __rhs.__extbuf_;
440 __rhs.__extbuf_ = __rhs.__extbuf_min_;
441 std::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_));
442 }
443 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
444 {
445 // *this doesn't use the small buffer, but __rhs does.
446 __rhs.__extbuf_ = __extbuf_;
447 __extbuf_ = __extbuf_min_;
448 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
449 }
450 else
451 {
452 // Both *this and __rhs use the small buffer.
453 char __tmp[sizeof(__extbuf_min_)];
454 std::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_));
455 std::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_));
456 std::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_));
457 }
458 __extbufnext_ = __extbuf_ + __rn;
459 __extbufend_ = __extbuf_ + __re;
460 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
461 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
462 }
463 _VSTD::swap(__ebs_, __rhs.__ebs_);
464 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
465 _VSTD::swap(__ibs_, __rhs.__ibs_);
466 _VSTD::swap(__file_, __rhs.__file_);
467 _VSTD::swap(__cv_, __rhs.__cv_);
468 _VSTD::swap(__st_, __rhs.__st_);
469 _VSTD::swap(__st_last_, __rhs.__st_last_);
470 _VSTD::swap(__om_, __rhs.__om_);
471 _VSTD::swap(__cm_, __rhs.__cm_);
472 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
473 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
474 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
475 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
476 {
477 ptrdiff_t __n = this->gptr() - this->eback();
478 ptrdiff_t __e = this->egptr() - this->eback();
479 this->setg((char_type*)__extbuf_min_,
480 (char_type*)__extbuf_min_ + __n,
481 (char_type*)__extbuf_min_ + __e);
482 }
483 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
484 {
485 ptrdiff_t __n = this->pptr() - this->pbase();
486 ptrdiff_t __e = this->epptr() - this->pbase();
487 this->setp((char_type*)__extbuf_min_,
488 (char_type*)__extbuf_min_ + __e);
489 this->__pbump(__n);
490 }
491 if (__rhs.eback() == (char_type*)__extbuf_min_)
492 {
493 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
494 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
495 __rhs.setg((char_type*)__rhs.__extbuf_min_,
496 (char_type*)__rhs.__extbuf_min_ + __n,
497 (char_type*)__rhs.__extbuf_min_ + __e);
498 }
499 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
500 {
501 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
502 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
503 __rhs.setp((char_type*)__rhs.__extbuf_min_,
504 (char_type*)__rhs.__extbuf_min_ + __e);
505 __rhs.__pbump(__n);
506 }
507 }
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 ios_base::openmode __mode) _NOEXCEPT {
528 switch (__mode & ~ios_base::ate) {
529 case ios_base::out:
530 case ios_base::out | ios_base::trunc:
531 return "w" _LIBCPP_FOPEN_CLOEXEC_MODE;
532 case ios_base::out | ios_base::app:
533 case ios_base::app:
534 return "a" _LIBCPP_FOPEN_CLOEXEC_MODE;
535 case ios_base::in:
536 return "r" _LIBCPP_FOPEN_CLOEXEC_MODE;
537 case ios_base::in | ios_base::out:
538 return "r+" _LIBCPP_FOPEN_CLOEXEC_MODE;
539 case ios_base::in | ios_base::out | ios_base::trunc:
540 return "w+" _LIBCPP_FOPEN_CLOEXEC_MODE;
541 case ios_base::in | ios_base::out | ios_base::app:
542 case ios_base::in | ios_base::app:
543 return "a+" _LIBCPP_FOPEN_CLOEXEC_MODE;
544 case ios_base::out | ios_base::binary:
545 case ios_base::out | ios_base::trunc | ios_base::binary:
546 return "wb" _LIBCPP_FOPEN_CLOEXEC_MODE;
547 case ios_base::out | ios_base::app | ios_base::binary:
548 case ios_base::app | ios_base::binary:
549 return "ab" _LIBCPP_FOPEN_CLOEXEC_MODE;
550 case ios_base::in | ios_base::binary:
551 return "rb" _LIBCPP_FOPEN_CLOEXEC_MODE;
552 case ios_base::in | ios_base::out | ios_base::binary:
553 return "r+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
554 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
555 return "w+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
556 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
557 case ios_base::in | ios_base::app | ios_base::binary:
558 return "a+b" _LIBCPP_FOPEN_CLOEXEC_MODE;
559 default:
560 return nullptr;
561 }
562 __libcpp_unreachable();
563 }
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 {
569 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
570 if (__file_ == nullptr)
571 {
572 if (const char* __mdstr = __make_mdstring(__mode)) {
573 __rt = this;
574 __file_ = fopen(__s, __mdstr);
575 if (__file_) {
576 __om_ = __mode;
577 if (__mode & ios_base::ate) {
578 if (fseek(__file_, 0, SEEK_END)) {
579 fclose(__file_);
580 __file_ = nullptr;
581 __rt = nullptr;
582 }
583 }
584 } else
585 __rt = nullptr;
586 }
587 }
588 return __rt;
589 }
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 {
705 return open(__s.c_str(), __mode);
706 }
707
708 template <class _CharT, class _Traits>
709 basic_filebuf<_CharT, _Traits>*
710 basic_filebuf<_CharT, _Traits>::close()
711 {
712 basic_filebuf<_CharT, _Traits>* __rt = nullptr;
713 if (__file_)
714 {
715 __rt = this;
716 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
717 if (sync())
718 __rt = nullptr;
719 if (fclose(__h.release()))
720 __rt = nullptr;
721 __file_ = nullptr;
722 setbuf(0, 0);
723 }
724 return __rt;
725 }
726
727 template <class _CharT, class _Traits>
728 typename basic_filebuf<_CharT, _Traits>::int_type
729 basic_filebuf<_CharT, _Traits>::underflow()
730 {
731 if (__file_ == nullptr)
732 return traits_type::eof();
733 bool __initial = __read_mode();
734 char_type __1buf;
735 if (this->gptr() == nullptr)
736 this->setg(&__1buf, &__1buf+1, &__1buf+1);
737 const size_t __unget_sz = __initial ? 0 : std::min<size_t>((this->egptr() - this->eback()) / 2, 4);
738 int_type __c = traits_type::eof();
739 if (this->gptr() == this->egptr())
740 {
741 _VSTD::memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
742 if (__always_noconv_)
743 {
744 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
745 __nmemb = ::fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
746 if (__nmemb != 0)
747 {
748 this->setg(this->eback(),
749 this->eback() + __unget_sz,
750 this->eback() + __unget_sz + __nmemb);
751 __c = traits_type::to_int_type(*this->gptr());
752 }
753 }
754 else
755 {
756 if (__extbufend_ != __extbufnext_) {
757 _LIBCPP_ASSERT(__extbufnext_ != nullptr, "underflow moving from nullptr");
758 _LIBCPP_ASSERT(__extbuf_ != nullptr, "underflow moving into nullptr");
759 _VSTD::memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
760 }
761 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
762 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
763 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
764 static_cast<size_t>(__extbufend_ - __extbufnext_));
765 codecvt_base::result __r;
766 __st_last_ = __st_;
767 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
768 if (__nr != 0)
769 {
770 if (!__cv_)
771 __throw_bad_cast();
772
773 __extbufend_ = __extbufnext_ + __nr;
774 char_type* __inext;
775 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
776 this->eback() + __unget_sz,
777 this->eback() + __ibs_, __inext);
778 if (__r == codecvt_base::noconv)
779 {
780 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
781 (char_type*)const_cast<char *>(__extbufend_));
782 __c = traits_type::to_int_type(*this->gptr());
783 }
784 else if (__inext != this->eback() + __unget_sz)
785 {
786 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
787 __c = traits_type::to_int_type(*this->gptr());
788 }
789 }
790 }
791 }
792 else
793 __c = traits_type::to_int_type(*this->gptr());
794 if (this->eback() == &__1buf)
795 this->setg(nullptr, nullptr, nullptr);
796 return __c;
797 }
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 {
803 if (__file_ && this->eback() < this->gptr())
804 {
805 if (traits_type::eq_int_type(__c, traits_type::eof()))
806 {
807 this->gbump(-1);
808 return traits_type::not_eof(__c);
809 }
810 if ((__om_ & ios_base::out) ||
811 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
812 {
813 this->gbump(-1);
814 *this->gptr() = traits_type::to_char_type(__c);
815 return __c;
816 }
817 }
818 return traits_type::eof();
819 }
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 {
825 if (__file_ == nullptr)
826 return traits_type::eof();
827 __write_mode();
828 char_type __1buf;
829 char_type* __pb_save = this->pbase();
830 char_type* __epb_save = this->epptr();
831 if (!traits_type::eq_int_type(__c, traits_type::eof()))
832 {
833 if (this->pptr() == nullptr)
834 this->setp(&__1buf, &__1buf+1);
835 *this->pptr() = traits_type::to_char_type(__c);
836 this->pbump(1);
837 }
838 if (this->pptr() != this->pbase())
839 {
840 if (__always_noconv_)
841 {
842 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
843 if (std::fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
844 return traits_type::eof();
845 }
846 else
847 {
848 char* __extbe = __extbuf_;
849 codecvt_base::result __r;
850 do
851 {
852 if (!__cv_)
853 __throw_bad_cast();
854
855 const char_type* __e;
856 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
857 __extbuf_, __extbuf_ + __ebs_, __extbe);
858 if (__e == this->pbase())
859 return traits_type::eof();
860 if (__r == codecvt_base::noconv)
861 {
862 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
863 if (std::fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
864 return traits_type::eof();
865 }
866 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
867 {
868 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
869 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
870 return traits_type::eof();
871 if (__r == codecvt_base::partial)
872 {
873 this->setp(const_cast<char_type*>(__e), this->pptr());
874 this->__pbump(this->epptr() - this->pbase());
875 }
876 }
877 else
878 return traits_type::eof();
879 } while (__r == codecvt_base::partial);
880 }
881 this->setp(__pb_save, __epb_save);
882 }
883 return traits_type::not_eof(__c);
884 }
885
886 template <class _CharT, class _Traits>
887 basic_streambuf<_CharT, _Traits>*
888 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
889 {
890 this->setg(nullptr, nullptr, nullptr);
891 this->setp(nullptr, nullptr);
892 if (__owns_eb_)
893 delete [] __extbuf_;
894 if (__owns_ib_)
895 delete [] __intbuf_;
896 __ebs_ = __n;
897 if (__ebs_ > sizeof(__extbuf_min_))
898 {
899 if (__always_noconv_ && __s)
900 {
901 __extbuf_ = (char*)__s;
902 __owns_eb_ = false;
903 }
904 else
905 {
906 __extbuf_ = new char[__ebs_];
907 __owns_eb_ = true;
908 }
909 }
910 else
911 {
912 __extbuf_ = __extbuf_min_;
913 __ebs_ = sizeof(__extbuf_min_);
914 __owns_eb_ = false;
915 }
916 if (!__always_noconv_)
917 {
918 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
919 if (__s && __ibs_ >= sizeof(__extbuf_min_))
920 {
921 __intbuf_ = __s;
922 __owns_ib_ = false;
923 }
924 else
925 {
926 __intbuf_ = new char_type[__ibs_];
927 __owns_ib_ = true;
928 }
929 }
930 else
931 {
932 __ibs_ = 0;
933 __intbuf_ = nullptr;
934 __owns_ib_ = false;
935 }
936 return this;
937 }
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 {
944 if (!__cv_)
945 __throw_bad_cast();
946
947 int __width = __cv_->encoding();
948 if (__file_ == nullptr || (__width <= 0 && __off != 0) || sync())
949 return pos_type(off_type(-1));
950 // __width > 0 || __off == 0
951 int __whence;
952 switch (__way)
953 {
954 case ios_base::beg:
955 __whence = SEEK_SET;
956 break;
957 case ios_base::cur:
958 __whence = SEEK_CUR;
959 break;
960 case ios_base::end:
961 __whence = SEEK_END;
962 break;
963 default:
964 return pos_type(off_type(-1));
965 }
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 if (::fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
972 return pos_type(off_type(-1));
973 pos_type __r = ftello(__file_);
974 #endif
975 __r.state(__st_);
976 return __r;
977 }
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 {
983 if (__file_ == nullptr || sync())
984 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 if (::fseeko(__file_, __sp, SEEK_SET))
990 return pos_type(off_type(-1));
991 #endif
992 __st_ = __sp.state();
993 return __sp;
994 }
995
996 template <class _CharT, class _Traits>
997 int
998 basic_filebuf<_CharT, _Traits>::sync()
999 {
1000 if (__file_ == nullptr)
1001 return 0;
1002 if (!__cv_)
1003 __throw_bad_cast();
1004
1005 if (__cm_ & ios_base::out)
1006 {
1007 if (this->pptr() != this->pbase())
1008 if (overflow() == traits_type::eof())
1009 return -1;
1010 codecvt_base::result __r;
1011 do
1012 {
1013 char* __extbe;
1014 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
1015 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
1016 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
1017 return -1;
1018 } while (__r == codecvt_base::partial);
1019 if (__r == codecvt_base::error)
1020 return -1;
1021 if (fflush(__file_))
1022 return -1;
1023 }
1024 else if (__cm_ & ios_base::in)
1025 {
1026 off_type __c;
1027 state_type __state = __st_last_;
1028 bool __update_st = false;
1029 if (__always_noconv_)
1030 __c = this->egptr() - this->gptr();
1031 else
1032 {
1033 int __width = __cv_->encoding();
1034 __c = __extbufend_ - __extbufnext_;
1035 if (__width > 0)
1036 __c += __width * (this->egptr() - this->gptr());
1037 else
1038 {
1039 if (this->gptr() != this->egptr())
1040 {
1041 const int __off = __cv_->length(__state, __extbuf_,
1042 __extbufnext_,
1043 this->gptr() - this->eback());
1044 __c += __extbufnext_ - __extbuf_ - __off;
1045 __update_st = true;
1046 }
1047 }
1048 }
1049 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
1050 if (fseek(__file_, -__c, SEEK_CUR))
1051 return -1;
1052 #else
1053 if (::fseeko(__file_, -__c, SEEK_CUR))
1054 return -1;
1055 #endif
1056 if (__update_st)
1057 __st_ = __state;
1058 __extbufnext_ = __extbufend_ = __extbuf_;
1059 this->setg(nullptr, nullptr, nullptr);
1060 __cm_ = 0;
1061 }
1062 return 0;
1063 }
1064
1065 template <class _CharT, class _Traits>
1066 void
1067 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
1068 {
1069 sync();
1070 __cv_ = &std::use_facet<codecvt<char_type, char, state_type> >(__loc);
1071 bool __old_anc = __always_noconv_;
1072 __always_noconv_ = __cv_->always_noconv();
1073 if (__old_anc != __always_noconv_)
1074 {
1075 this->setg(nullptr, nullptr, nullptr);
1076 this->setp(nullptr, nullptr);
1077 // invariant, char_type is char, else we couldn't get here
1078 if (__always_noconv_) // need to dump __intbuf_
1079 {
1080 if (__owns_eb_)
1081 delete [] __extbuf_;
1082 __owns_eb_ = __owns_ib_;
1083 __ebs_ = __ibs_;
1084 __extbuf_ = (char*)__intbuf_;
1085 __ibs_ = 0;
1086 __intbuf_ = nullptr;
1087 __owns_ib_ = false;
1088 }
1089 else // need to obtain an __intbuf_.
1090 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
1091 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
1092 {
1093 __ibs_ = __ebs_;
1094 __intbuf_ = (char_type*)__extbuf_;
1095 __owns_ib_ = false;
1096 __extbuf_ = new char[__ebs_];
1097 __owns_eb_ = true;
1098 }
1099 else
1100 {
1101 __ibs_ = __ebs_;
1102 __intbuf_ = new char_type[__ibs_];
1103 __owns_ib_ = true;
1104 }
1105 }
1106 }
1107 }
1108
1109 template <class _CharT, class _Traits>
1110 bool
1111 basic_filebuf<_CharT, _Traits>::__read_mode()
1112 {
1113 if (!(__cm_ & ios_base::in))
1114 {
1115 this->setp(nullptr, nullptr);
1116 if (__always_noconv_)
1117 this->setg((char_type*)__extbuf_,
1118 (char_type*)__extbuf_ + __ebs_,
1119 (char_type*)__extbuf_ + __ebs_);
1120 else
1121 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
1122 __cm_ = ios_base::in;
1123 return true;
1124 }
1125 return false;
1126 }
1127
1128 template <class _CharT, class _Traits>
1129 void
1130 basic_filebuf<_CharT, _Traits>::__write_mode()
1131 {
1132 if (!(__cm_ & ios_base::out))
1133 {
1134 this->setg(nullptr, nullptr, nullptr);
1135 if (__ebs_ > sizeof(__extbuf_min_))
1136 {
1137 if (__always_noconv_)
1138 this->setp((char_type*)__extbuf_,
1139 (char_type*)__extbuf_ + (__ebs_ - 1));
1140 else
1141 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
1142 }
1143 else
1144 this->setp(nullptr, nullptr);
1145 __cm_ = ios_base::out;
1146 }
1147 }
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 {
1302 if (__sb_.open(__s, __mode | ios_base::in))
1303 this->clear();
1304 else
1305 this->setstate(ios_base::failbit);
1306 }
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 {
1324 if (__sb_.open(__s, __mode | ios_base::in))
1325 this->clear();
1326 else
1327 this->setstate(ios_base::failbit);
1328 }
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 {
1503 if (__sb_.open(__s, __mode | ios_base::out))
1504 this->clear();
1505 else
1506 this->setstate(ios_base::failbit);
1507 }
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 {
1525 if (__sb_.open(__s, __mode | ios_base::out))
1526 this->clear();
1527 else
1528 this->setstate(ios_base::failbit);
1529 }
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