2 //===------------------------- fstream ------------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_FSTREAM
12 #define _LIBCPP_FSTREAM
17 template <class charT, class traits = char_traits<charT> >
19 : public basic_streambuf<charT, traits>
22 typedef charT char_type;
23 typedef traits traits_type;
24 typedef typename traits_type::int_type int_type;
25 typedef typename traits_type::pos_type pos_type;
26 typedef typename traits_type::off_type off_type;
28 // 27.9.1.2 Constructors/destructor:
30 basic_filebuf(basic_filebuf&& rhs);
31 virtual ~basic_filebuf();
33 // 27.9.1.3 Assign/swap:
34 basic_filebuf& operator=(basic_filebuf&& rhs);
35 void swap(basic_filebuf& rhs);
39 basic_filebuf* open(const char* s, ios_base::openmode mode);
40 basic_filebuf* open(const string& s, ios_base::openmode mode);
41 basic_filebuf* close();
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);
56 virtual void imbue(const locale& loc);
59 template <class charT, class traits>
61 swap(basic_filebuf<charT, traits>& x, basic_filebuf<charT, traits>& y);
63 typedef basic_filebuf<char> filebuf;
64 typedef basic_filebuf<wchar_t> wfilebuf;
66 template <class charT, class traits = char_traits<charT> >
68 : public basic_istream<charT,traits>
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;
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 basic_ifstream(basic_ifstream&& rhs);
82 basic_ifstream& operator=(basic_ifstream&& rhs);
83 void swap(basic_ifstream& rhs);
85 basic_filebuf<char_type, traits_type>* rdbuf() const;
87 void open(const char* s, ios_base::openmode mode = ios_base::in);
88 void open(const string& s, ios_base::openmode mode = ios_base::in);
92 template <class charT, class traits>
94 swap(basic_ifstream<charT, traits>& x, basic_ifstream<charT, traits>& y);
96 typedef basic_ifstream<char> ifstream;
97 typedef basic_ifstream<wchar_t> wifstream;
99 template <class charT, class traits = char_traits<charT> >
101 : public basic_ostream<charT,traits>
104 typedef charT char_type;
105 typedef traits traits_type;
106 typedef typename traits_type::int_type int_type;
107 typedef typename traits_type::pos_type pos_type;
108 typedef typename traits_type::off_type off_type;
111 explicit basic_ofstream(const char* s, ios_base::openmode mode = ios_base::out);
112 explicit basic_ofstream(const string& s, ios_base::openmode mode = ios_base::out);
113 basic_ofstream(basic_ofstream&& rhs);
115 basic_ofstream& operator=(basic_ofstream&& rhs);
116 void swap(basic_ofstream& rhs);
118 basic_filebuf<char_type, traits_type>* rdbuf() const;
119 bool is_open() const;
120 void open(const char* s, ios_base::openmode mode = ios_base::out);
121 void open(const string& s, ios_base::openmode mode = ios_base::out);
125 template <class charT, class traits>
127 swap(basic_ofstream<charT, traits>& x, basic_ofstream<charT, traits>& y);
129 typedef basic_ofstream<char> ofstream;
130 typedef basic_ofstream<wchar_t> wofstream;
132 template <class charT, class traits=char_traits<charT> >
134 : public basic_iostream<charT,traits>
137 typedef charT char_type;
138 typedef traits traits_type;
139 typedef typename traits_type::int_type int_type;
140 typedef typename traits_type::pos_type pos_type;
141 typedef typename traits_type::off_type off_type;
144 explicit basic_fstream(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
145 explicit basic_fstream(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
146 basic_fstream(basic_fstream&& rhs);
148 basic_fstream& operator=(basic_fstream&& rhs);
149 void swap(basic_fstream& rhs);
151 basic_filebuf<char_type, traits_type>* rdbuf() const;
152 bool is_open() const;
153 void open(const char* s, ios_base::openmode mode = ios_base::in|ios_base::out);
154 void open(const string& s, ios_base::openmode mode = ios_base::in|ios_base::out);
158 template <class charT, class traits>
159 void swap(basic_fstream<charT, traits>& x, basic_fstream<charT, traits>& y);
161 typedef basic_fstream<char> fstream;
162 typedef basic_fstream<wchar_t> wfstream;
174 #include <__undef_min_max>
176 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
177 #pragma GCC system_header
180 _LIBCPP_BEGIN_NAMESPACE_STD
182 template <class _CharT, class _Traits>
183 class _LIBCPP_VISIBLE basic_filebuf
184 : public basic_streambuf<_CharT, _Traits>
187 typedef _CharT char_type;
188 typedef _Traits traits_type;
189 typedef typename traits_type::int_type int_type;
190 typedef typename traits_type::pos_type pos_type;
191 typedef typename traits_type::off_type off_type;
192 typedef typename traits_type::state_type state_type;
194 // 27.9.1.2 Constructors/destructor:
196 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
197 basic_filebuf(basic_filebuf&& __rhs);
199 virtual ~basic_filebuf();
201 // 27.9.1.3 Assign/swap:
202 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
203 basic_filebuf& operator=(basic_filebuf&& __rhs);
205 void swap(basic_filebuf& __rhs);
208 bool is_open() const;
209 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
210 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
211 basic_filebuf* close();
214 // 27.9.1.5 Overridden virtual functions:
215 virtual int_type underflow();
216 virtual int_type pbackfail(int_type __c = traits_type::eof());
217 virtual int_type overflow (int_type __c = traits_type::eof());
218 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
219 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
220 ios_base::openmode __wch = ios_base::in | ios_base::out);
221 virtual pos_type seekpos(pos_type __sp,
222 ios_base::openmode __wch = ios_base::in | ios_base::out);
224 virtual void imbue(const locale& __loc);
228 const char* __extbufnext_;
229 const char* __extbufend_;
230 char __extbuf_min_[8];
232 char_type* __intbuf_;
235 const codecvt<char_type, char, state_type>* __cv_;
237 state_type __st_last_;
238 ios_base::openmode __om_;
239 ios_base::openmode __cm_;
242 bool __always_noconv_;
248 template <class _CharT, class _Traits>
249 basic_filebuf<_CharT, _Traits>::basic_filebuf()
264 __always_noconv_(false)
266 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
268 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
269 __always_noconv_ = __cv_->always_noconv();
274 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
276 template <class _CharT, class _Traits>
277 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
278 : basic_streambuf<_CharT, _Traits>(__rhs)
280 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
282 __extbuf_ = __extbuf_min_;
283 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
284 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
288 __extbuf_ = __rhs.__extbuf_;
289 __extbufnext_ = __rhs.__extbufnext_;
290 __extbufend_ = __rhs.__extbufend_;
292 __ebs_ = __rhs.__ebs_;
293 __intbuf_ = __rhs.__intbuf_;
294 __ibs_ = __rhs.__ibs_;
295 __file_ = __rhs.__file_;
298 __st_last_ = __rhs.__st_last_;
301 __owns_eb_ = __rhs.__owns_eb_;
302 __owns_ib_ = __rhs.__owns_ib_;
303 __always_noconv_ = __rhs.__always_noconv_;
306 if (__rhs.pbase() == __rhs.__intbuf_)
307 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
309 this->setp((char_type*)__extbuf_,
310 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
311 this->pbump(__rhs. pptr() - __rhs.pbase());
313 else if (__rhs.eback())
315 if (__rhs.eback() == __rhs.__intbuf_)
316 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
317 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
319 this->setg((char_type*)__extbuf_,
320 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
321 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
324 __rhs.__extbufnext_ = 0;
325 __rhs.__extbufend_ = 0;
330 __rhs.__st_ = state_type();
331 __rhs.__st_last_ = state_type();
334 __rhs.__owns_eb_ = false;
335 __rhs.__owns_ib_ = false;
340 template <class _CharT, class _Traits>
341 inline _LIBCPP_INLINE_VISIBILITY
342 basic_filebuf<_CharT, _Traits>&
343 basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
350 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
352 template <class _CharT, class _Traits>
353 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
355 #ifndef _LIBCPP_NO_EXCEPTIONS
358 #endif // _LIBCPP_NO_EXCEPTIONS
360 #ifndef _LIBCPP_NO_EXCEPTIONS
365 #endif // _LIBCPP_NO_EXCEPTIONS
372 template <class _CharT, class _Traits>
374 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
376 basic_streambuf<char_type, traits_type>::swap(__rhs);
377 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
379 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
380 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
381 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
385 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
386 ptrdiff_t __le = __extbufend_ - __extbuf_;
387 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
388 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
389 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
391 __extbuf_ = __rhs.__extbuf_;
392 __rhs.__extbuf_ = __rhs.__extbuf_min_;
394 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
396 __rhs.__extbuf_ = __extbuf_;
397 __extbuf_ = __extbuf_min_;
399 __extbufnext_ = __extbuf_ + __rn;
400 __extbufend_ = __extbuf_ + __re;
401 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
402 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
404 _VSTD::swap(__ebs_, __rhs.__ebs_);
405 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
406 _VSTD::swap(__ibs_, __rhs.__ibs_);
407 _VSTD::swap(__file_, __rhs.__file_);
408 _VSTD::swap(__cv_, __rhs.__cv_);
409 _VSTD::swap(__st_, __rhs.__st_);
410 _VSTD::swap(__st_last_, __rhs.__st_last_);
411 _VSTD::swap(__om_, __rhs.__om_);
412 _VSTD::swap(__cm_, __rhs.__cm_);
413 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
414 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
415 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
416 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
418 ptrdiff_t __n = this->gptr() - this->eback();
419 ptrdiff_t __e = this->egptr() - this->eback();
420 this->setg((char_type*)__extbuf_min_,
421 (char_type*)__extbuf_min_ + __n,
422 (char_type*)__extbuf_min_ + __e);
424 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
426 ptrdiff_t __n = this->pptr() - this->pbase();
427 ptrdiff_t __e = this->epptr() - this->pbase();
428 this->setp((char_type*)__extbuf_min_,
429 (char_type*)__extbuf_min_ + __e);
432 if (__rhs.eback() == (char_type*)__extbuf_min_)
434 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
435 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
436 __rhs.setg((char_type*)__rhs.__extbuf_min_,
437 (char_type*)__rhs.__extbuf_min_ + __n,
438 (char_type*)__rhs.__extbuf_min_ + __e);
440 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
442 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
443 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
444 __rhs.setp((char_type*)__rhs.__extbuf_min_,
445 (char_type*)__rhs.__extbuf_min_ + __e);
450 template <class _CharT, class _Traits>
451 inline _LIBCPP_INLINE_VISIBILITY
453 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
458 template <class _CharT, class _Traits>
459 inline _LIBCPP_INLINE_VISIBILITY
461 basic_filebuf<_CharT, _Traits>::is_open() const
466 template <class _CharT, class _Traits>
467 basic_filebuf<_CharT, _Traits>*
468 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
470 basic_filebuf<_CharT, _Traits>* __rt = 0;
475 switch (__mode & ~ios_base::ate)
478 case ios_base::out | ios_base::trunc:
481 case ios_base::out | ios_base::app:
488 case ios_base::in | ios_base::out:
491 case ios_base::in | ios_base::out | ios_base::trunc:
494 case ios_base::in | ios_base::out | ios_base::app:
495 case ios_base::in | ios_base::app:
498 case ios_base::out | ios_base::binary:
499 case ios_base::out | ios_base::trunc | ios_base::binary:
502 case ios_base::out | ios_base::app | ios_base::binary:
503 case ios_base::app | ios_base::binary:
506 case ios_base::in | ios_base::binary:
509 case ios_base::in | ios_base::out | ios_base::binary:
512 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
515 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
516 case ios_base::in | ios_base::app | ios_base::binary:
525 __file_ = fopen(__s, __mdstr);
529 if (__mode & ios_base::ate)
531 if (fseek(__file_, 0, SEEK_END))
546 template <class _CharT, class _Traits>
547 inline _LIBCPP_INLINE_VISIBILITY
548 basic_filebuf<_CharT, _Traits>*
549 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
551 return open(__s.c_str(), __mode);
554 template <class _CharT, class _Traits>
555 basic_filebuf<_CharT, _Traits>*
556 basic_filebuf<_CharT, _Traits>::close()
558 basic_filebuf<_CharT, _Traits>* __rt = 0;
562 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
565 if (fclose(__h.release()) == 0)
573 template <class _CharT, class _Traits>
574 typename basic_filebuf<_CharT, _Traits>::int_type
575 basic_filebuf<_CharT, _Traits>::underflow()
578 return traits_type::eof();
579 bool __initial = __read_mode();
581 if (this->gptr() == 0)
582 this->setg(&__1buf, &__1buf+1, &__1buf+1);
583 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
584 int_type __c = traits_type::eof();
585 if (this->gptr() == this->egptr())
587 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
588 if (__always_noconv_)
590 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
591 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
594 this->setg(this->eback(),
595 this->eback() + __unget_sz,
596 this->eback() + __unget_sz + __nmemb);
597 __c = traits_type::to_int_type(*this->gptr());
602 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
603 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
604 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
605 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
606 static_cast<size_t>(__extbufend_ - __extbufnext_));
607 codecvt_base::result __r;
609 size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
612 #ifndef _LIBCPP_NO_EXCEPTIONS
616 __extbufend_ = __extbufnext_ + __nr;
618 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
619 this->eback() + __unget_sz,
620 this->eback() + __ibs_, __inext);
621 if (__r == codecvt_base::noconv)
623 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
624 __c = traits_type::to_int_type(*this->gptr());
626 else if (__inext != this->eback() + __unget_sz)
628 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
629 __c = traits_type::to_int_type(*this->gptr());
635 __c = traits_type::to_int_type(*this->gptr());
636 if (this->eback() == &__1buf)
641 template <class _CharT, class _Traits>
642 typename basic_filebuf<_CharT, _Traits>::int_type
643 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
645 if (__file_ && this->eback() < this->gptr())
647 if (traits_type::eq_int_type(__c, traits_type::eof()))
650 return traits_type::not_eof(__c);
652 if ((__om_ & ios_base::out) ||
653 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
656 *this->gptr() = traits_type::to_char_type(__c);
660 return traits_type::eof();
663 template <class _CharT, class _Traits>
664 typename basic_filebuf<_CharT, _Traits>::int_type
665 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
668 return traits_type::eof();
671 char_type* __pb_save = this->pbase();
672 char_type* __epb_save = this->epptr();
673 if (!traits_type::eq_int_type(__c, traits_type::eof()))
675 if (this->pptr() == 0)
676 this->setp(&__1buf, &__1buf+1);
677 *this->pptr() = traits_type::to_char_type(__c);
680 if (this->pptr() != this->pbase())
682 if (__always_noconv_)
684 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
685 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
686 return traits_type::eof();
690 char* __extbe = __extbuf_;
691 codecvt_base::result __r;
694 #ifndef _LIBCPP_NO_EXCEPTIONS
698 const char_type* __e;
699 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
700 __extbuf_, __extbuf_ + __ebs_, __extbe);
701 if (__e == this->pbase())
702 return traits_type::eof();
703 if (__r == codecvt_base::noconv)
705 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
706 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
707 return traits_type::eof();
709 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
711 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
712 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
713 return traits_type::eof();
714 if (__r == codecvt_base::partial)
716 this->setp((char_type*)__e, this->pptr());
717 this->pbump(this->epptr() - this->pbase());
721 return traits_type::eof();
722 } while (__r == codecvt_base::partial);
724 this->setp(__pb_save, __epb_save);
726 return traits_type::not_eof(__c);
729 template <class _CharT, class _Traits>
730 basic_streambuf<_CharT, _Traits>*
731 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
740 if (__ebs_ > sizeof(__extbuf_min_))
742 if (__always_noconv_ && __s)
744 __extbuf_ = (char*)__s;
749 __extbuf_ = new char[__ebs_];
755 __extbuf_ = __extbuf_min_;
756 __ebs_ = sizeof(__extbuf_min_);
759 if (!__always_noconv_)
761 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
762 if (__s && __ibs_ >= sizeof(__extbuf_min_))
769 __intbuf_ = new char_type[__ibs_];
782 template <class _CharT, class _Traits>
783 typename basic_filebuf<_CharT, _Traits>::pos_type
784 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
787 #ifndef _LIBCPP_NO_EXCEPTIONS
791 int __width = __cv_->encoding();
792 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
793 return pos_type(off_type(-1));
794 // __width > 0 || __off == 0
808 return pos_type(off_type(-1));
810 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
811 return pos_type(off_type(-1));
812 pos_type __r = ftello(__file_);
817 template <class _CharT, class _Traits>
818 typename basic_filebuf<_CharT, _Traits>::pos_type
819 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
821 if (__file_ == 0 || sync())
822 return pos_type(off_type(-1));
823 if (fseeko(__file_, __sp, SEEK_SET))
824 return pos_type(off_type(-1));
825 __st_ = __sp.state();
829 template <class _CharT, class _Traits>
831 basic_filebuf<_CharT, _Traits>::sync()
835 #ifndef _LIBCPP_NO_EXCEPTIONS
839 if (__cm_ & ios_base::out)
841 if (this->pptr() != this->pbase())
842 if (overflow() == traits_type::eof())
844 codecvt_base::result __r;
848 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
849 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
850 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
852 } while (__r == codecvt_base::partial);
853 if (__r == codecvt_base::error)
858 else if (__cm_ & ios_base::in)
861 state_type __state = __st_last_;
862 bool __update_st = false;
863 if (__always_noconv_)
864 __c = this->egptr() - this->gptr();
867 int __width = __cv_->encoding();
868 __c = __extbufend_ - __extbufnext_;
870 __c += __width * (this->egptr() - this->gptr());
873 if (this->gptr() != this->egptr())
875 const int __off = __cv_->length(__state, __extbuf_,
877 this->gptr() - this->eback());
878 __c += __extbufnext_ - __extbuf_ - __off;
883 if (fseeko(__file_, -__c, SEEK_CUR))
887 __extbufnext_ = __extbufend_ = __extbuf_;
894 template <class _CharT, class _Traits>
896 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
899 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
900 bool __old_anc = __always_noconv_;
901 __always_noconv_ = __cv_->always_noconv();
902 if (__old_anc != __always_noconv_)
906 // invariant, char_type is char, else we couldn't get here
907 if (__always_noconv_) // need to dump __intbuf_
911 __owns_eb_ = __owns_ib_;
913 __extbuf_ = (char*)__intbuf_;
918 else // need to obtain an __intbuf_.
919 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
920 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
923 __intbuf_ = (char_type*)__extbuf_;
925 __extbuf_ = new char[__ebs_];
931 __intbuf_ = new char_type[__ibs_];
938 template <class _CharT, class _Traits>
940 basic_filebuf<_CharT, _Traits>::__read_mode()
942 if (!(__cm_ & ios_base::in))
945 if (__always_noconv_)
946 this->setg((char_type*)__extbuf_,
947 (char_type*)__extbuf_ + __ebs_,
948 (char_type*)__extbuf_ + __ebs_);
950 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
951 __cm_ = ios_base::in;
957 template <class _CharT, class _Traits>
959 basic_filebuf<_CharT, _Traits>::__write_mode()
961 if (!(__cm_ & ios_base::out))
964 if (__ebs_ > sizeof(__extbuf_min_))
966 if (__always_noconv_)
967 this->setp((char_type*)__extbuf_,
968 (char_type*)__extbuf_ + (__ebs_ - 1));
970 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
974 __cm_ = ios_base::out;
980 template <class _CharT, class _Traits>
981 class _LIBCPP_VISIBLE basic_ifstream
982 : public basic_istream<_CharT, _Traits>
985 typedef _CharT char_type;
986 typedef _Traits traits_type;
987 typedef typename traits_type::int_type int_type;
988 typedef typename traits_type::pos_type pos_type;
989 typedef typename traits_type::off_type off_type;
992 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
993 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
994 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
995 basic_ifstream(basic_ifstream&& __rhs);
998 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
999 basic_ifstream& operator=(basic_ifstream&& __rhs);
1001 void swap(basic_ifstream& __rhs);
1003 basic_filebuf<char_type, traits_type>* rdbuf() const;
1004 bool is_open() const;
1005 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1006 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1010 basic_filebuf<char_type, traits_type> __sb_;
1013 template <class _CharT, class _Traits>
1014 inline _LIBCPP_INLINE_VISIBILITY
1015 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1016 : basic_istream<char_type, traits_type>(&__sb_)
1020 template <class _CharT, class _Traits>
1021 inline _LIBCPP_INLINE_VISIBILITY
1022 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1023 : basic_istream<char_type, traits_type>(&__sb_)
1025 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1026 this->setstate(ios_base::failbit);
1029 template <class _CharT, class _Traits>
1030 inline _LIBCPP_INLINE_VISIBILITY
1031 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1032 : basic_istream<char_type, traits_type>(&__sb_)
1034 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1035 this->setstate(ios_base::failbit);
1038 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1040 template <class _CharT, class _Traits>
1041 inline _LIBCPP_INLINE_VISIBILITY
1042 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1043 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1044 __sb_(_VSTD::move(__rhs.__sb_))
1046 this->set_rdbuf(&__sb_);
1049 template <class _CharT, class _Traits>
1050 inline _LIBCPP_INLINE_VISIBILITY
1051 basic_ifstream<_CharT, _Traits>&
1052 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1054 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1055 __sb_ = _VSTD::move(__rhs.__sb_);
1059 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1061 template <class _CharT, class _Traits>
1062 inline _LIBCPP_INLINE_VISIBILITY
1064 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1066 basic_istream<char_type, traits_type>::swap(__rhs);
1067 __sb_.swap(__rhs.__sb_);
1070 template <class _CharT, class _Traits>
1071 inline _LIBCPP_INLINE_VISIBILITY
1073 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1078 template <class _CharT, class _Traits>
1079 inline _LIBCPP_INLINE_VISIBILITY
1080 basic_filebuf<_CharT, _Traits>*
1081 basic_ifstream<_CharT, _Traits>::rdbuf() const
1083 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1086 template <class _CharT, class _Traits>
1087 inline _LIBCPP_INLINE_VISIBILITY
1089 basic_ifstream<_CharT, _Traits>::is_open() const
1091 return __sb_.is_open();
1094 template <class _CharT, class _Traits>
1096 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1098 if (__sb_.open(__s, __mode | ios_base::in))
1101 this->setstate(ios_base::failbit);
1104 template <class _CharT, class _Traits>
1106 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1108 if (__sb_.open(__s, __mode | ios_base::in))
1111 this->setstate(ios_base::failbit);
1114 template <class _CharT, class _Traits>
1115 inline _LIBCPP_INLINE_VISIBILITY
1117 basic_ifstream<_CharT, _Traits>::close()
1119 if (__sb_.close() == 0)
1120 this->setstate(ios_base::failbit);
1125 template <class _CharT, class _Traits>
1126 class _LIBCPP_VISIBLE basic_ofstream
1127 : public basic_ostream<_CharT, _Traits>
1130 typedef _CharT char_type;
1131 typedef _Traits traits_type;
1132 typedef typename traits_type::int_type int_type;
1133 typedef typename traits_type::pos_type pos_type;
1134 typedef typename traits_type::off_type off_type;
1137 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1138 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1139 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1140 basic_ofstream(basic_ofstream&& __rhs);
1143 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1144 basic_ofstream& operator=(basic_ofstream&& __rhs);
1146 void swap(basic_ofstream& __rhs);
1148 basic_filebuf<char_type, traits_type>* rdbuf() const;
1149 bool is_open() const;
1150 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1151 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1155 basic_filebuf<char_type, traits_type> __sb_;
1158 template <class _CharT, class _Traits>
1159 inline _LIBCPP_INLINE_VISIBILITY
1160 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1161 : basic_ostream<char_type, traits_type>(&__sb_)
1165 template <class _CharT, class _Traits>
1166 inline _LIBCPP_INLINE_VISIBILITY
1167 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1168 : basic_ostream<char_type, traits_type>(&__sb_)
1170 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1171 this->setstate(ios_base::failbit);
1174 template <class _CharT, class _Traits>
1175 inline _LIBCPP_INLINE_VISIBILITY
1176 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1177 : basic_ostream<char_type, traits_type>(&__sb_)
1179 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1180 this->setstate(ios_base::failbit);
1183 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1185 template <class _CharT, class _Traits>
1186 inline _LIBCPP_INLINE_VISIBILITY
1187 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1188 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1189 __sb_(_VSTD::move(__rhs.__sb_))
1191 this->set_rdbuf(&__sb_);
1194 template <class _CharT, class _Traits>
1195 inline _LIBCPP_INLINE_VISIBILITY
1196 basic_ofstream<_CharT, _Traits>&
1197 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1199 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1200 __sb_ = _VSTD::move(__rhs.__sb_);
1204 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1206 template <class _CharT, class _Traits>
1207 inline _LIBCPP_INLINE_VISIBILITY
1209 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1211 basic_ostream<char_type, traits_type>::swap(__rhs);
1212 __sb_.swap(__rhs.__sb_);
1215 template <class _CharT, class _Traits>
1216 inline _LIBCPP_INLINE_VISIBILITY
1218 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1223 template <class _CharT, class _Traits>
1224 inline _LIBCPP_INLINE_VISIBILITY
1225 basic_filebuf<_CharT, _Traits>*
1226 basic_ofstream<_CharT, _Traits>::rdbuf() const
1228 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1231 template <class _CharT, class _Traits>
1232 inline _LIBCPP_INLINE_VISIBILITY
1234 basic_ofstream<_CharT, _Traits>::is_open() const
1236 return __sb_.is_open();
1239 template <class _CharT, class _Traits>
1241 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1243 if (__sb_.open(__s, __mode | ios_base::out))
1246 this->setstate(ios_base::failbit);
1249 template <class _CharT, class _Traits>
1251 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1253 if (__sb_.open(__s, __mode | ios_base::out))
1256 this->setstate(ios_base::failbit);
1259 template <class _CharT, class _Traits>
1260 inline _LIBCPP_INLINE_VISIBILITY
1262 basic_ofstream<_CharT, _Traits>::close()
1264 if (__sb_.close() == 0)
1265 this->setstate(ios_base::failbit);
1270 template <class _CharT, class _Traits>
1271 class _LIBCPP_VISIBLE basic_fstream
1272 : public basic_iostream<_CharT, _Traits>
1275 typedef _CharT char_type;
1276 typedef _Traits traits_type;
1277 typedef typename traits_type::int_type int_type;
1278 typedef typename traits_type::pos_type pos_type;
1279 typedef typename traits_type::off_type off_type;
1282 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1283 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1284 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1285 basic_fstream(basic_fstream&& __rhs);
1288 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1289 basic_fstream& operator=(basic_fstream&& __rhs);
1291 void swap(basic_fstream& __rhs);
1293 basic_filebuf<char_type, traits_type>* rdbuf() const;
1294 bool is_open() const;
1295 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1296 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1300 basic_filebuf<char_type, traits_type> __sb_;
1303 template <class _CharT, class _Traits>
1304 inline _LIBCPP_INLINE_VISIBILITY
1305 basic_fstream<_CharT, _Traits>::basic_fstream()
1306 : basic_iostream<char_type, traits_type>(&__sb_)
1310 template <class _CharT, class _Traits>
1311 inline _LIBCPP_INLINE_VISIBILITY
1312 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1313 : basic_iostream<char_type, traits_type>(&__sb_)
1315 if (__sb_.open(__s, __mode) == 0)
1316 this->setstate(ios_base::failbit);
1319 template <class _CharT, class _Traits>
1320 inline _LIBCPP_INLINE_VISIBILITY
1321 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1322 : basic_iostream<char_type, traits_type>(&__sb_)
1324 if (__sb_.open(__s, __mode) == 0)
1325 this->setstate(ios_base::failbit);
1328 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1330 template <class _CharT, class _Traits>
1331 inline _LIBCPP_INLINE_VISIBILITY
1332 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1333 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1334 __sb_(_VSTD::move(__rhs.__sb_))
1336 this->set_rdbuf(&__sb_);
1339 template <class _CharT, class _Traits>
1340 inline _LIBCPP_INLINE_VISIBILITY
1341 basic_fstream<_CharT, _Traits>&
1342 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1344 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1345 __sb_ = _VSTD::move(__rhs.__sb_);
1349 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1351 template <class _CharT, class _Traits>
1352 inline _LIBCPP_INLINE_VISIBILITY
1354 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1356 basic_iostream<char_type, traits_type>::swap(__rhs);
1357 __sb_.swap(__rhs.__sb_);
1360 template <class _CharT, class _Traits>
1361 inline _LIBCPP_INLINE_VISIBILITY
1363 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1368 template <class _CharT, class _Traits>
1369 inline _LIBCPP_INLINE_VISIBILITY
1370 basic_filebuf<_CharT, _Traits>*
1371 basic_fstream<_CharT, _Traits>::rdbuf() const
1373 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1376 template <class _CharT, class _Traits>
1377 inline _LIBCPP_INLINE_VISIBILITY
1379 basic_fstream<_CharT, _Traits>::is_open() const
1381 return __sb_.is_open();
1384 template <class _CharT, class _Traits>
1386 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1388 if (__sb_.open(__s, __mode))
1391 this->setstate(ios_base::failbit);
1394 template <class _CharT, class _Traits>
1396 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1398 if (__sb_.open(__s, __mode))
1401 this->setstate(ios_base::failbit);
1404 template <class _CharT, class _Traits>
1405 inline _LIBCPP_INLINE_VISIBILITY
1407 basic_fstream<_CharT, _Traits>::close()
1409 if (__sb_.close() == 0)
1410 this->setstate(ios_base::failbit);
1413 _LIBCPP_END_NAMESPACE_STD
1415 #endif // _LIBCPP_FSTREAM