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)
264 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
266 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
267 __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>(__ibs_ - __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->eback() + __ibs_, __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));
823 template <class _CharT, class _Traits>
825 basic_filebuf<_CharT, _Traits>::sync()
829 #ifndef _LIBCPP_NO_EXCEPTIONS
833 if (__cm_ & ios_base::out)
835 if (this->pptr() != this->pbase())
836 if (overflow() == traits_type::eof())
838 codecvt_base::result __r;
842 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
843 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
844 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
846 } while (__r == codecvt_base::partial);
847 if (__r == codecvt_base::error)
852 else if (__cm_ & ios_base::in)
855 if (__always_noconv_)
856 __c = this->egptr() - this->gptr();
859 int __width = __cv_->encoding();
860 __c = __extbufend_ - __extbufnext_;
862 __c += __width * (this->egptr() - this->gptr());
865 if (this->gptr() != this->egptr())
867 reverse(this->gptr(), this->egptr());
868 codecvt_base::result __r;
869 const char_type* __e = this->gptr();
873 __r = __cv_->out(__st_, __e, this->egptr(), __e,
874 __extbuf_, __extbuf_ + __ebs_, __extbe);
877 case codecvt_base::noconv:
878 __c += this->egptr() - this->gptr();
880 case codecvt_base::ok:
881 case codecvt_base::partial:
882 __c += __extbe - __extbuf_;
887 } while (__r == codecvt_base::partial);
891 if (fseeko(__file_, -__c, SEEK_CUR))
899 template <class _CharT, class _Traits>
901 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
904 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
905 bool __old_anc = __always_noconv_;
906 __always_noconv_ = __cv_->always_noconv();
907 if (__old_anc != __always_noconv_)
911 // invariant, char_type is char, else we couldn't get here
912 if (__always_noconv_) // need to dump __intbuf_
916 __owns_eb_ = __owns_ib_;
918 __extbuf_ = (char*)__intbuf_;
923 else // need to obtain an __intbuf_.
924 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
925 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
928 __intbuf_ = (char_type*)__extbuf_;
930 __extbuf_ = new char[__ebs_];
936 __intbuf_ = new char_type[__ibs_];
943 template <class _CharT, class _Traits>
945 basic_filebuf<_CharT, _Traits>::__read_mode()
947 if (!(__cm_ & ios_base::in))
950 if (__always_noconv_)
951 this->setg((char_type*)__extbuf_,
952 (char_type*)__extbuf_ + __ebs_,
953 (char_type*)__extbuf_ + __ebs_);
955 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
956 __cm_ = ios_base::in;
962 template <class _CharT, class _Traits>
964 basic_filebuf<_CharT, _Traits>::__write_mode()
966 if (!(__cm_ & ios_base::out))
969 if (__ebs_ > sizeof(__extbuf_min_))
971 if (__always_noconv_)
972 this->setp((char_type*)__extbuf_,
973 (char_type*)__extbuf_ + (__ebs_ - 1));
975 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
979 __cm_ = ios_base::out;
985 template <class _CharT, class _Traits>
986 class _LIBCPP_VISIBLE basic_ifstream
987 : public basic_istream<_CharT, _Traits>
990 typedef _CharT char_type;
991 typedef _Traits traits_type;
992 typedef typename traits_type::int_type int_type;
993 typedef typename traits_type::pos_type pos_type;
994 typedef typename traits_type::off_type off_type;
997 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
998 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
999 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1000 basic_ifstream(basic_ifstream&& __rhs);
1003 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1004 basic_ifstream& operator=(basic_ifstream&& __rhs);
1006 void swap(basic_ifstream& __rhs);
1008 basic_filebuf<char_type, traits_type>* rdbuf() const;
1009 bool is_open() const;
1010 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1011 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1015 basic_filebuf<char_type, traits_type> __sb_;
1018 template <class _CharT, class _Traits>
1019 inline _LIBCPP_INLINE_VISIBILITY
1020 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1021 : basic_istream<char_type, traits_type>(&__sb_)
1025 template <class _CharT, class _Traits>
1026 inline _LIBCPP_INLINE_VISIBILITY
1027 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1028 : basic_istream<char_type, traits_type>(&__sb_)
1030 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1031 this->setstate(ios_base::failbit);
1034 template <class _CharT, class _Traits>
1035 inline _LIBCPP_INLINE_VISIBILITY
1036 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1037 : basic_istream<char_type, traits_type>(&__sb_)
1039 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1040 this->setstate(ios_base::failbit);
1043 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1045 template <class _CharT, class _Traits>
1046 inline _LIBCPP_INLINE_VISIBILITY
1047 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1048 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1049 __sb_(_VSTD::move(__rhs.__sb_))
1051 this->set_rdbuf(&__sb_);
1054 template <class _CharT, class _Traits>
1055 inline _LIBCPP_INLINE_VISIBILITY
1056 basic_ifstream<_CharT, _Traits>&
1057 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1059 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1060 __sb_ = _VSTD::move(__rhs.__sb_);
1064 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1066 template <class _CharT, class _Traits>
1067 inline _LIBCPP_INLINE_VISIBILITY
1069 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1071 basic_istream<char_type, traits_type>::swap(__rhs);
1072 __sb_.swap(__rhs.__sb_);
1075 template <class _CharT, class _Traits>
1076 inline _LIBCPP_INLINE_VISIBILITY
1078 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1083 template <class _CharT, class _Traits>
1084 inline _LIBCPP_INLINE_VISIBILITY
1085 basic_filebuf<_CharT, _Traits>*
1086 basic_ifstream<_CharT, _Traits>::rdbuf() const
1088 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1091 template <class _CharT, class _Traits>
1092 inline _LIBCPP_INLINE_VISIBILITY
1094 basic_ifstream<_CharT, _Traits>::is_open() const
1096 return __sb_.is_open();
1099 template <class _CharT, class _Traits>
1101 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1103 if (__sb_.open(__s, __mode | ios_base::in))
1106 this->setstate(ios_base::failbit);
1109 template <class _CharT, class _Traits>
1111 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1113 if (__sb_.open(__s, __mode | ios_base::in))
1116 this->setstate(ios_base::failbit);
1119 template <class _CharT, class _Traits>
1120 inline _LIBCPP_INLINE_VISIBILITY
1122 basic_ifstream<_CharT, _Traits>::close()
1124 if (__sb_.close() == 0)
1125 this->setstate(ios_base::failbit);
1130 template <class _CharT, class _Traits>
1131 class _LIBCPP_VISIBLE basic_ofstream
1132 : public basic_ostream<_CharT, _Traits>
1135 typedef _CharT char_type;
1136 typedef _Traits traits_type;
1137 typedef typename traits_type::int_type int_type;
1138 typedef typename traits_type::pos_type pos_type;
1139 typedef typename traits_type::off_type off_type;
1142 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1143 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1144 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1145 basic_ofstream(basic_ofstream&& __rhs);
1148 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1149 basic_ofstream& operator=(basic_ofstream&& __rhs);
1151 void swap(basic_ofstream& __rhs);
1153 basic_filebuf<char_type, traits_type>* rdbuf() const;
1154 bool is_open() const;
1155 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1156 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1160 basic_filebuf<char_type, traits_type> __sb_;
1163 template <class _CharT, class _Traits>
1164 inline _LIBCPP_INLINE_VISIBILITY
1165 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1166 : basic_ostream<char_type, traits_type>(&__sb_)
1170 template <class _CharT, class _Traits>
1171 inline _LIBCPP_INLINE_VISIBILITY
1172 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1173 : basic_ostream<char_type, traits_type>(&__sb_)
1175 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1176 this->setstate(ios_base::failbit);
1179 template <class _CharT, class _Traits>
1180 inline _LIBCPP_INLINE_VISIBILITY
1181 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1182 : basic_ostream<char_type, traits_type>(&__sb_)
1184 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1185 this->setstate(ios_base::failbit);
1188 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1190 template <class _CharT, class _Traits>
1191 inline _LIBCPP_INLINE_VISIBILITY
1192 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1193 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1194 __sb_(_VSTD::move(__rhs.__sb_))
1196 this->set_rdbuf(&__sb_);
1199 template <class _CharT, class _Traits>
1200 inline _LIBCPP_INLINE_VISIBILITY
1201 basic_ofstream<_CharT, _Traits>&
1202 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1204 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1205 __sb_ = _VSTD::move(__rhs.__sb_);
1209 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1211 template <class _CharT, class _Traits>
1212 inline _LIBCPP_INLINE_VISIBILITY
1214 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1216 basic_ostream<char_type, traits_type>::swap(__rhs);
1217 __sb_.swap(__rhs.__sb_);
1220 template <class _CharT, class _Traits>
1221 inline _LIBCPP_INLINE_VISIBILITY
1223 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1228 template <class _CharT, class _Traits>
1229 inline _LIBCPP_INLINE_VISIBILITY
1230 basic_filebuf<_CharT, _Traits>*
1231 basic_ofstream<_CharT, _Traits>::rdbuf() const
1233 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1236 template <class _CharT, class _Traits>
1237 inline _LIBCPP_INLINE_VISIBILITY
1239 basic_ofstream<_CharT, _Traits>::is_open() const
1241 return __sb_.is_open();
1244 template <class _CharT, class _Traits>
1246 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1248 if (__sb_.open(__s, __mode | ios_base::out))
1251 this->setstate(ios_base::failbit);
1254 template <class _CharT, class _Traits>
1256 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1258 if (__sb_.open(__s, __mode | ios_base::out))
1261 this->setstate(ios_base::failbit);
1264 template <class _CharT, class _Traits>
1265 inline _LIBCPP_INLINE_VISIBILITY
1267 basic_ofstream<_CharT, _Traits>::close()
1269 if (__sb_.close() == 0)
1270 this->setstate(ios_base::failbit);
1275 template <class _CharT, class _Traits>
1276 class _LIBCPP_VISIBLE basic_fstream
1277 : public basic_iostream<_CharT, _Traits>
1280 typedef _CharT char_type;
1281 typedef _Traits traits_type;
1282 typedef typename traits_type::int_type int_type;
1283 typedef typename traits_type::pos_type pos_type;
1284 typedef typename traits_type::off_type off_type;
1287 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1288 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1289 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1290 basic_fstream(basic_fstream&& __rhs);
1293 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1294 basic_fstream& operator=(basic_fstream&& __rhs);
1296 void swap(basic_fstream& __rhs);
1298 basic_filebuf<char_type, traits_type>* rdbuf() const;
1299 bool is_open() const;
1300 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1301 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1305 basic_filebuf<char_type, traits_type> __sb_;
1308 template <class _CharT, class _Traits>
1309 inline _LIBCPP_INLINE_VISIBILITY
1310 basic_fstream<_CharT, _Traits>::basic_fstream()
1311 : basic_iostream<char_type, traits_type>(&__sb_)
1315 template <class _CharT, class _Traits>
1316 inline _LIBCPP_INLINE_VISIBILITY
1317 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1318 : basic_iostream<char_type, traits_type>(&__sb_)
1320 if (__sb_.open(__s, __mode) == 0)
1321 this->setstate(ios_base::failbit);
1324 template <class _CharT, class _Traits>
1325 inline _LIBCPP_INLINE_VISIBILITY
1326 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1327 : basic_iostream<char_type, traits_type>(&__sb_)
1329 if (__sb_.open(__s, __mode) == 0)
1330 this->setstate(ios_base::failbit);
1333 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1335 template <class _CharT, class _Traits>
1336 inline _LIBCPP_INLINE_VISIBILITY
1337 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1338 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1339 __sb_(_VSTD::move(__rhs.__sb_))
1341 this->set_rdbuf(&__sb_);
1344 template <class _CharT, class _Traits>
1345 inline _LIBCPP_INLINE_VISIBILITY
1346 basic_fstream<_CharT, _Traits>&
1347 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1349 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1350 __sb_ = _VSTD::move(__rhs.__sb_);
1354 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1356 template <class _CharT, class _Traits>
1357 inline _LIBCPP_INLINE_VISIBILITY
1359 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1361 basic_iostream<char_type, traits_type>::swap(__rhs);
1362 __sb_.swap(__rhs.__sb_);
1365 template <class _CharT, class _Traits>
1366 inline _LIBCPP_INLINE_VISIBILITY
1368 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1373 template <class _CharT, class _Traits>
1374 inline _LIBCPP_INLINE_VISIBILITY
1375 basic_filebuf<_CharT, _Traits>*
1376 basic_fstream<_CharT, _Traits>::rdbuf() const
1378 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1381 template <class _CharT, class _Traits>
1382 inline _LIBCPP_INLINE_VISIBILITY
1384 basic_fstream<_CharT, _Traits>::is_open() const
1386 return __sb_.is_open();
1389 template <class _CharT, class _Traits>
1391 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1393 if (__sb_.open(__s, __mode))
1396 this->setstate(ios_base::failbit);
1399 template <class _CharT, class _Traits>
1401 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1403 if (__sb_.open(__s, __mode))
1406 this->setstate(ios_base::failbit);
1409 template <class _CharT, class _Traits>
1410 inline _LIBCPP_INLINE_VISIBILITY
1412 basic_fstream<_CharT, _Traits>::close()
1414 if (__sb_.close() == 0)
1415 this->setstate(ios_base::failbit);
1418 _LIBCPP_END_NAMESPACE_STD
1420 #endif // _LIBCPP_FSTREAM