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 ios_base::openmode __om_;
238 ios_base::openmode __cm_;
241 bool __always_noconv_;
247 template <class _CharT, class _Traits>
248 basic_filebuf<_CharT, _Traits>::basic_filebuf()
262 __always_noconv_(false)
265 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
267 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
268 __always_noconv_ = __cv_->always_noconv();
272 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
274 template <class _CharT, class _Traits>
275 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
276 : basic_streambuf<_CharT, _Traits>(__rhs)
278 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
280 __extbuf_ = __extbuf_min_;
281 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
282 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
286 __extbuf_ = __rhs.__extbuf_;
287 __extbufnext_ = __rhs.__extbufnext_;
288 __extbufend_ = __rhs.__extbufend_;
290 __ebs_ = __rhs.__ebs_;
291 __intbuf_ = __rhs.__intbuf_;
292 __ibs_ = __rhs.__ibs_;
293 __file_ = __rhs.__file_;
298 __owns_eb_ = __rhs.__owns_eb_;
299 __owns_ib_ = __rhs.__owns_ib_;
300 __always_noconv_ = __rhs.__always_noconv_;
303 if (__rhs.pbase() == __rhs.__intbuf_)
304 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
306 this->setp((char_type*)__extbuf_,
307 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
308 this->pbump(__rhs. pptr() - __rhs.pbase());
310 else if (__rhs.eback())
312 if (__rhs.eback() == __rhs.__intbuf_)
313 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
314 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
316 this->setg((char_type*)__extbuf_,
317 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
318 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
321 __rhs.__extbufnext_ = 0;
322 __rhs.__extbufend_ = 0;
327 __rhs.__st_ = state_type();
330 __rhs.__owns_eb_ = false;
331 __rhs.__owns_ib_ = false;
336 template <class _CharT, class _Traits>
337 inline _LIBCPP_INLINE_VISIBILITY
338 basic_filebuf<_CharT, _Traits>&
339 basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
345 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
347 template <class _CharT, class _Traits>
348 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
350 #ifndef _LIBCPP_NO_EXCEPTIONS
353 #endif // _LIBCPP_NO_EXCEPTIONS
355 #ifndef _LIBCPP_NO_EXCEPTIONS
360 #endif // _LIBCPP_NO_EXCEPTIONS
367 template <class _CharT, class _Traits>
369 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
371 basic_streambuf<char_type, traits_type>::swap(__rhs);
372 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
374 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
375 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
376 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
380 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
381 ptrdiff_t __le = __extbufend_ - __extbuf_;
382 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
383 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
384 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
386 __extbuf_ = __rhs.__extbuf_;
387 __rhs.__extbuf_ = __rhs.__extbuf_min_;
389 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
391 __rhs.__extbuf_ = __extbuf_;
392 __extbuf_ = __extbuf_min_;
394 __extbufnext_ = __extbuf_ + __rn;
395 __extbufend_ = __extbuf_ + __re;
396 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
397 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
399 _VSTD::swap(__ebs_, __rhs.__ebs_);
400 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
401 _VSTD::swap(__ibs_, __rhs.__ibs_);
402 _VSTD::swap(__file_, __rhs.__file_);
403 _VSTD::swap(__cv_, __rhs.__cv_);
404 _VSTD::swap(__st_, __rhs.__st_);
405 _VSTD::swap(__om_, __rhs.__om_);
406 _VSTD::swap(__cm_, __rhs.__cm_);
407 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
408 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
409 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
410 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
412 ptrdiff_t __n = this->gptr() - this->eback();
413 ptrdiff_t __e = this->egptr() - this->eback();
414 this->setg((char_type*)__extbuf_min_,
415 (char_type*)__extbuf_min_ + __n,
416 (char_type*)__extbuf_min_ + __e);
418 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
420 ptrdiff_t __n = this->pptr() - this->pbase();
421 ptrdiff_t __e = this->epptr() - this->pbase();
422 this->setp((char_type*)__extbuf_min_,
423 (char_type*)__extbuf_min_ + __e);
426 if (__rhs.eback() == (char_type*)__extbuf_min_)
428 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
429 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
430 __rhs.setg((char_type*)__rhs.__extbuf_min_,
431 (char_type*)__rhs.__extbuf_min_ + __n,
432 (char_type*)__rhs.__extbuf_min_ + __e);
434 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
436 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
437 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
438 __rhs.setp((char_type*)__rhs.__extbuf_min_,
439 (char_type*)__rhs.__extbuf_min_ + __e);
444 template <class _CharT, class _Traits>
445 inline _LIBCPP_INLINE_VISIBILITY
447 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
452 template <class _CharT, class _Traits>
453 inline _LIBCPP_INLINE_VISIBILITY
455 basic_filebuf<_CharT, _Traits>::is_open() const
460 template <class _CharT, class _Traits>
461 basic_filebuf<_CharT, _Traits>*
462 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
464 basic_filebuf<_CharT, _Traits>* __rt = 0;
469 switch (__mode & ~ios_base::ate)
472 case ios_base::out | ios_base::trunc:
475 case ios_base::out | ios_base::app:
482 case ios_base::in | ios_base::out:
485 case ios_base::in | ios_base::out | ios_base::trunc:
488 case ios_base::in | ios_base::out | ios_base::app:
489 case ios_base::in | ios_base::app:
492 case ios_base::out | ios_base::binary:
493 case ios_base::out | ios_base::trunc | ios_base::binary:
496 case ios_base::out | ios_base::app | ios_base::binary:
497 case ios_base::app | ios_base::binary:
500 case ios_base::in | ios_base::binary:
503 case ios_base::in | ios_base::out | ios_base::binary:
506 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
509 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
510 case ios_base::in | ios_base::app | ios_base::binary:
519 __file_ = fopen(__s, __mdstr);
523 if (__mode & ios_base::ate)
525 if (fseek(__file_, 0, SEEK_END))
540 template <class _CharT, class _Traits>
541 inline _LIBCPP_INLINE_VISIBILITY
542 basic_filebuf<_CharT, _Traits>*
543 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
545 return open(__s.c_str(), __mode);
548 template <class _CharT, class _Traits>
549 basic_filebuf<_CharT, _Traits>*
550 basic_filebuf<_CharT, _Traits>::close()
552 basic_filebuf<_CharT, _Traits>* __rt = 0;
556 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
559 if (fclose(__h.release()) == 0)
567 template <class _CharT, class _Traits>
568 typename basic_filebuf<_CharT, _Traits>::int_type
569 basic_filebuf<_CharT, _Traits>::underflow()
572 return traits_type::eof();
573 bool __initial = __read_mode();
575 if (this->gptr() == 0)
576 this->setg(&__1buf, &__1buf+1, &__1buf+1);
577 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
578 int_type __c = traits_type::eof();
579 if (this->gptr() == this->egptr())
581 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
582 if (__always_noconv_)
584 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
585 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
588 this->setg(this->eback(),
589 this->eback() + __unget_sz,
590 this->eback() + __unget_sz + __nmemb);
591 __c = traits_type::to_int_type(*this->gptr());
596 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
597 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
598 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
599 size_t __nmemb = _VSTD::min(static_cast<size_t>(this->egptr() - this->eback() - __unget_sz),
600 static_cast<size_t>(__extbufend_ - __extbufnext_));
601 codecvt_base::result __r;
602 state_type __svs = __st_;
603 size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
606 #ifndef _LIBCPP_NO_EXCEPTIONS
610 __extbufend_ = __extbufnext_ + __nr;
612 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
613 this->eback() + __unget_sz,
614 this->egptr(), __inext);
615 if (__r == codecvt_base::noconv)
617 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
618 __c = traits_type::to_int_type(*this->gptr());
620 else if (__inext != this->eback() + __unget_sz)
622 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
623 __c = traits_type::to_int_type(*this->gptr());
629 __c = traits_type::to_int_type(*this->gptr());
630 if (this->eback() == &__1buf)
635 template <class _CharT, class _Traits>
636 typename basic_filebuf<_CharT, _Traits>::int_type
637 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
639 if (__file_ && this->eback() < this->gptr())
641 if (traits_type::eq_int_type(__c, traits_type::eof()))
644 return traits_type::not_eof(__c);
646 if ((__om_ & ios_base::out) ||
647 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
650 *this->gptr() = traits_type::to_char_type(__c);
654 return traits_type::eof();
657 template <class _CharT, class _Traits>
658 typename basic_filebuf<_CharT, _Traits>::int_type
659 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
662 return traits_type::eof();
665 char_type* __pb_save = this->pbase();
666 char_type* __epb_save = this->epptr();
667 if (!traits_type::eq_int_type(__c, traits_type::eof()))
669 if (this->pptr() == 0)
670 this->setp(&__1buf, &__1buf+1);
671 *this->pptr() = traits_type::to_char_type(__c);
674 if (this->pptr() != this->pbase())
676 if (__always_noconv_)
678 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
679 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
680 return traits_type::eof();
684 char* __extbe = __extbuf_;
685 codecvt_base::result __r;
688 #ifndef _LIBCPP_NO_EXCEPTIONS
692 const char_type* __e;
693 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
694 __extbuf_, __extbuf_ + __ebs_, __extbe);
695 if (__e == this->pbase())
696 return traits_type::eof();
697 if (__r == codecvt_base::noconv)
699 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
700 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
701 return traits_type::eof();
703 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
705 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
706 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
707 return traits_type::eof();
708 if (__r == codecvt_base::partial)
710 this->setp((char_type*)__e, this->pptr());
711 this->pbump(this->epptr() - this->pbase());
715 return traits_type::eof();
716 } while (__r == codecvt_base::partial);
718 this->setp(__pb_save, __epb_save);
720 return traits_type::not_eof(__c);
723 template <class _CharT, class _Traits>
724 basic_streambuf<_CharT, _Traits>*
725 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
734 if (__ebs_ > sizeof(__extbuf_min_))
736 if (__always_noconv_ && __s)
738 __extbuf_ = (char*)__s;
743 __extbuf_ = new char[__ebs_];
749 __extbuf_ = __extbuf_min_;
750 __ebs_ = sizeof(__extbuf_min_);
753 if (!__always_noconv_)
755 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
756 if (__s && __ibs_ >= sizeof(__extbuf_min_))
763 __intbuf_ = new char_type[__ibs_];
776 template <class _CharT, class _Traits>
777 typename basic_filebuf<_CharT, _Traits>::pos_type
778 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
781 #ifndef _LIBCPP_NO_EXCEPTIONS
785 int __width = __cv_->encoding();
786 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
787 return pos_type(off_type(-1));
788 // __width > 0 || __off == 0
802 return pos_type(off_type(-1));
804 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
805 return pos_type(off_type(-1));
806 pos_type __r = ftello(__file_);
811 template <class _CharT, class _Traits>
812 typename basic_filebuf<_CharT, _Traits>::pos_type
813 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
815 if (__file_ == 0 || sync())
816 return pos_type(off_type(-1));
817 if (fseeko(__file_, __sp, SEEK_SET))
818 return pos_type(off_type(-1));
822 template <class _CharT, class _Traits>
824 basic_filebuf<_CharT, _Traits>::sync()
828 #ifndef _LIBCPP_NO_EXCEPTIONS
832 if (__cm_ & ios_base::out)
834 if (this->pptr() != this->pbase())
835 if (overflow() == traits_type::eof())
837 codecvt_base::result __r;
841 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
842 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
843 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
845 } while (__r == codecvt_base::partial);
846 if (__r == codecvt_base::error)
851 else if (__cm_ & ios_base::in)
854 if (__always_noconv_)
855 __c = this->egptr() - this->gptr();
858 int __width = __cv_->encoding();
859 __c = __extbufend_ - __extbufnext_;
861 __c += __width * (this->egptr() - this->gptr());
864 if (this->gptr() != this->egptr())
866 reverse(this->gptr(), this->egptr());
867 codecvt_base::result __r;
868 const char_type* __e = this->gptr();
872 __r = __cv_->out(__st_, __e, this->egptr(), __e,
873 __extbuf_, __extbuf_ + __ebs_, __extbe);
876 case codecvt_base::noconv:
877 __c += this->egptr() - this->gptr();
879 case codecvt_base::ok:
880 case codecvt_base::partial:
881 __c += __extbe - __extbuf_;
886 } while (__r == codecvt_base::partial);
890 if (fseeko(__file_, -__c, SEEK_CUR))
898 template <class _CharT, class _Traits>
900 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
903 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
904 bool __old_anc = __always_noconv_;
905 __always_noconv_ = __cv_->always_noconv();
906 if (__old_anc != __always_noconv_)
910 // invariant, char_type is char, else we couldn't get here
911 if (__always_noconv_) // need to dump __intbuf_
915 __owns_eb_ = __owns_ib_;
917 __extbuf_ = (char*)__intbuf_;
922 else // need to obtain an __intbuf_.
923 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
924 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
927 __intbuf_ = (char_type*)__extbuf_;
929 __extbuf_ = new char[__ebs_];
935 __intbuf_ = new char_type[__ibs_];
942 template <class _CharT, class _Traits>
944 basic_filebuf<_CharT, _Traits>::__read_mode()
946 if (!(__cm_ & ios_base::in))
949 if (__always_noconv_)
950 this->setg((char_type*)__extbuf_,
951 (char_type*)__extbuf_ + __ebs_,
952 (char_type*)__extbuf_ + __ebs_);
954 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
955 __cm_ = ios_base::in;
961 template <class _CharT, class _Traits>
963 basic_filebuf<_CharT, _Traits>::__write_mode()
965 if (!(__cm_ & ios_base::out))
968 if (__ebs_ > sizeof(__extbuf_min_))
970 if (__always_noconv_)
971 this->setp((char_type*)__extbuf_,
972 (char_type*)__extbuf_ + (__ebs_ - 1));
974 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
978 __cm_ = ios_base::out;
984 template <class _CharT, class _Traits>
985 class _LIBCPP_VISIBLE basic_ifstream
986 : public basic_istream<_CharT, _Traits>
989 typedef _CharT char_type;
990 typedef _Traits traits_type;
991 typedef typename traits_type::int_type int_type;
992 typedef typename traits_type::pos_type pos_type;
993 typedef typename traits_type::off_type off_type;
996 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
997 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
998 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
999 basic_ifstream(basic_ifstream&& __rhs);
1002 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1003 basic_ifstream& operator=(basic_ifstream&& __rhs);
1005 void swap(basic_ifstream& __rhs);
1007 basic_filebuf<char_type, traits_type>* rdbuf() const;
1008 bool is_open() const;
1009 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1010 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1014 basic_filebuf<char_type, traits_type> __sb_;
1017 template <class _CharT, class _Traits>
1018 inline _LIBCPP_INLINE_VISIBILITY
1019 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1020 : basic_istream<char_type, traits_type>(&__sb_)
1024 template <class _CharT, class _Traits>
1025 inline _LIBCPP_INLINE_VISIBILITY
1026 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1027 : basic_istream<char_type, traits_type>(&__sb_)
1029 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1030 this->setstate(ios_base::failbit);
1033 template <class _CharT, class _Traits>
1034 inline _LIBCPP_INLINE_VISIBILITY
1035 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1036 : basic_istream<char_type, traits_type>(&__sb_)
1038 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1039 this->setstate(ios_base::failbit);
1042 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1044 template <class _CharT, class _Traits>
1045 inline _LIBCPP_INLINE_VISIBILITY
1046 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1047 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1048 __sb_(_VSTD::move(__rhs.__sb_))
1050 this->set_rdbuf(&__sb_);
1053 template <class _CharT, class _Traits>
1054 inline _LIBCPP_INLINE_VISIBILITY
1055 basic_ifstream<_CharT, _Traits>&
1056 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1058 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1059 __sb_ = _VSTD::move(__rhs.__sb_);
1063 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1065 template <class _CharT, class _Traits>
1066 inline _LIBCPP_INLINE_VISIBILITY
1068 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1070 basic_istream<char_type, traits_type>::swap(__rhs);
1071 __sb_.swap(__rhs.__sb_);
1074 template <class _CharT, class _Traits>
1075 inline _LIBCPP_INLINE_VISIBILITY
1077 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1082 template <class _CharT, class _Traits>
1083 inline _LIBCPP_INLINE_VISIBILITY
1084 basic_filebuf<_CharT, _Traits>*
1085 basic_ifstream<_CharT, _Traits>::rdbuf() const
1087 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1090 template <class _CharT, class _Traits>
1091 inline _LIBCPP_INLINE_VISIBILITY
1093 basic_ifstream<_CharT, _Traits>::is_open() const
1095 return __sb_.is_open();
1098 template <class _CharT, class _Traits>
1100 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1102 if (__sb_.open(__s, __mode | ios_base::in))
1105 this->setstate(ios_base::failbit);
1108 template <class _CharT, class _Traits>
1110 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1112 if (__sb_.open(__s, __mode | ios_base::in))
1115 this->setstate(ios_base::failbit);
1118 template <class _CharT, class _Traits>
1119 inline _LIBCPP_INLINE_VISIBILITY
1121 basic_ifstream<_CharT, _Traits>::close()
1123 if (__sb_.close() == 0)
1124 this->setstate(ios_base::failbit);
1129 template <class _CharT, class _Traits>
1130 class _LIBCPP_VISIBLE basic_ofstream
1131 : public basic_ostream<_CharT, _Traits>
1134 typedef _CharT char_type;
1135 typedef _Traits traits_type;
1136 typedef typename traits_type::int_type int_type;
1137 typedef typename traits_type::pos_type pos_type;
1138 typedef typename traits_type::off_type off_type;
1141 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1142 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1143 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1144 basic_ofstream(basic_ofstream&& __rhs);
1147 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1148 basic_ofstream& operator=(basic_ofstream&& __rhs);
1150 void swap(basic_ofstream& __rhs);
1152 basic_filebuf<char_type, traits_type>* rdbuf() const;
1153 bool is_open() const;
1154 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1155 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1159 basic_filebuf<char_type, traits_type> __sb_;
1162 template <class _CharT, class _Traits>
1163 inline _LIBCPP_INLINE_VISIBILITY
1164 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1165 : basic_ostream<char_type, traits_type>(&__sb_)
1169 template <class _CharT, class _Traits>
1170 inline _LIBCPP_INLINE_VISIBILITY
1171 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1172 : basic_ostream<char_type, traits_type>(&__sb_)
1174 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1175 this->setstate(ios_base::failbit);
1178 template <class _CharT, class _Traits>
1179 inline _LIBCPP_INLINE_VISIBILITY
1180 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1181 : basic_ostream<char_type, traits_type>(&__sb_)
1183 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1184 this->setstate(ios_base::failbit);
1187 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1189 template <class _CharT, class _Traits>
1190 inline _LIBCPP_INLINE_VISIBILITY
1191 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1192 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1193 __sb_(_VSTD::move(__rhs.__sb_))
1195 this->set_rdbuf(&__sb_);
1198 template <class _CharT, class _Traits>
1199 inline _LIBCPP_INLINE_VISIBILITY
1200 basic_ofstream<_CharT, _Traits>&
1201 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1203 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1204 __sb_ = _VSTD::move(__rhs.__sb_);
1208 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1210 template <class _CharT, class _Traits>
1211 inline _LIBCPP_INLINE_VISIBILITY
1213 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1215 basic_ostream<char_type, traits_type>::swap(__rhs);
1216 __sb_.swap(__rhs.__sb_);
1219 template <class _CharT, class _Traits>
1220 inline _LIBCPP_INLINE_VISIBILITY
1222 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1227 template <class _CharT, class _Traits>
1228 inline _LIBCPP_INLINE_VISIBILITY
1229 basic_filebuf<_CharT, _Traits>*
1230 basic_ofstream<_CharT, _Traits>::rdbuf() const
1232 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1235 template <class _CharT, class _Traits>
1236 inline _LIBCPP_INLINE_VISIBILITY
1238 basic_ofstream<_CharT, _Traits>::is_open() const
1240 return __sb_.is_open();
1243 template <class _CharT, class _Traits>
1245 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1247 if (__sb_.open(__s, __mode | ios_base::out))
1250 this->setstate(ios_base::failbit);
1253 template <class _CharT, class _Traits>
1255 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1257 if (__sb_.open(__s, __mode | ios_base::out))
1260 this->setstate(ios_base::failbit);
1263 template <class _CharT, class _Traits>
1264 inline _LIBCPP_INLINE_VISIBILITY
1266 basic_ofstream<_CharT, _Traits>::close()
1268 if (__sb_.close() == 0)
1269 this->setstate(ios_base::failbit);
1274 template <class _CharT, class _Traits>
1275 class _LIBCPP_VISIBLE basic_fstream
1276 : public basic_iostream<_CharT, _Traits>
1279 typedef _CharT char_type;
1280 typedef _Traits traits_type;
1281 typedef typename traits_type::int_type int_type;
1282 typedef typename traits_type::pos_type pos_type;
1283 typedef typename traits_type::off_type off_type;
1286 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1287 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1288 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1289 basic_fstream(basic_fstream&& __rhs);
1292 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1293 basic_fstream& operator=(basic_fstream&& __rhs);
1295 void swap(basic_fstream& __rhs);
1297 basic_filebuf<char_type, traits_type>* rdbuf() const;
1298 bool is_open() const;
1299 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1300 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1304 basic_filebuf<char_type, traits_type> __sb_;
1307 template <class _CharT, class _Traits>
1308 inline _LIBCPP_INLINE_VISIBILITY
1309 basic_fstream<_CharT, _Traits>::basic_fstream()
1310 : basic_iostream<char_type, traits_type>(&__sb_)
1314 template <class _CharT, class _Traits>
1315 inline _LIBCPP_INLINE_VISIBILITY
1316 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1317 : basic_iostream<char_type, traits_type>(&__sb_)
1319 if (__sb_.open(__s, __mode) == 0)
1320 this->setstate(ios_base::failbit);
1323 template <class _CharT, class _Traits>
1324 inline _LIBCPP_INLINE_VISIBILITY
1325 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1326 : basic_iostream<char_type, traits_type>(&__sb_)
1328 if (__sb_.open(__s, __mode) == 0)
1329 this->setstate(ios_base::failbit);
1332 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1334 template <class _CharT, class _Traits>
1335 inline _LIBCPP_INLINE_VISIBILITY
1336 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1337 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1338 __sb_(_VSTD::move(__rhs.__sb_))
1340 this->set_rdbuf(&__sb_);
1343 template <class _CharT, class _Traits>
1344 inline _LIBCPP_INLINE_VISIBILITY
1345 basic_fstream<_CharT, _Traits>&
1346 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1348 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1349 __sb_ = _VSTD::move(__rhs.__sb_);
1353 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1355 template <class _CharT, class _Traits>
1356 inline _LIBCPP_INLINE_VISIBILITY
1358 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1360 basic_iostream<char_type, traits_type>::swap(__rhs);
1361 __sb_.swap(__rhs.__sb_);
1364 template <class _CharT, class _Traits>
1365 inline _LIBCPP_INLINE_VISIBILITY
1367 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1372 template <class _CharT, class _Traits>
1373 inline _LIBCPP_INLINE_VISIBILITY
1374 basic_filebuf<_CharT, _Traits>*
1375 basic_fstream<_CharT, _Traits>::rdbuf() const
1377 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1380 template <class _CharT, class _Traits>
1381 inline _LIBCPP_INLINE_VISIBILITY
1383 basic_fstream<_CharT, _Traits>::is_open() const
1385 return __sb_.is_open();
1388 template <class _CharT, class _Traits>
1390 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1392 if (__sb_.open(__s, __mode))
1395 this->setstate(ios_base::failbit);
1398 template <class _CharT, class _Traits>
1400 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1402 if (__sb_.open(__s, __mode))
1405 this->setstate(ios_base::failbit);
1408 template <class _CharT, class _Traits>
1409 inline _LIBCPP_INLINE_VISIBILITY
1411 basic_fstream<_CharT, _Traits>::close()
1413 if (__sb_.close() == 0)
1414 this->setstate(ios_base::failbit);
1417 _LIBCPP_END_NAMESPACE_STD
1419 #endif // _LIBCPP_FSTREAM