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()
256 __cv_(&use_facet<codecvt<char_type, char, state_type> >(this->getloc())),
262 __always_noconv_(__cv_->always_noconv())
267 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
269 template <class _CharT, class _Traits>
270 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
271 : basic_streambuf<_CharT, _Traits>(__rhs)
273 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
275 __extbuf_ = __extbuf_min_;
276 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
277 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
281 __extbuf_ = __rhs.__extbuf_;
282 __extbufnext_ = __rhs.__extbufnext_;
283 __extbufend_ = __rhs.__extbufend_;
285 __ebs_ = __rhs.__ebs_;
286 __intbuf_ = __rhs.__intbuf_;
287 __ibs_ = __rhs.__ibs_;
288 __file_ = __rhs.__file_;
293 __owns_eb_ = __rhs.__owns_eb_;
294 __owns_ib_ = __rhs.__owns_ib_;
295 __always_noconv_ = __rhs.__always_noconv_;
298 if (__rhs.pbase() == __rhs.__intbuf_)
299 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
301 this->setp((char_type*)__extbuf_,
302 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
303 this->pbump(__rhs. pptr() - __rhs.pbase());
305 else if (__rhs.eback())
307 if (__rhs.eback() == __rhs.__intbuf_)
308 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
309 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
311 this->setg((char_type*)__extbuf_,
312 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
313 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
316 __rhs.__extbufnext_ = 0;
317 __rhs.__extbufend_ = 0;
322 __rhs.__st_ = state_type();
325 __rhs.__owns_eb_ = false;
326 __rhs.__owns_ib_ = false;
331 template <class _CharT, class _Traits>
332 inline _LIBCPP_INLINE_VISIBILITY
333 basic_filebuf<_CharT, _Traits>&
334 basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
340 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
342 template <class _CharT, class _Traits>
343 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
345 #ifndef _LIBCPP_NO_EXCEPTIONS
348 #endif // _LIBCPP_NO_EXCEPTIONS
350 #ifndef _LIBCPP_NO_EXCEPTIONS
355 #endif // _LIBCPP_NO_EXCEPTIONS
362 template <class _CharT, class _Traits>
364 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
366 basic_streambuf<char_type, traits_type>::swap(__rhs);
367 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
369 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
370 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
371 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
375 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
376 ptrdiff_t __le = __extbufend_ - __extbuf_;
377 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
378 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
379 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
381 __extbuf_ = __rhs.__extbuf_;
382 __rhs.__extbuf_ = __rhs.__extbuf_min_;
384 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
386 __rhs.__extbuf_ = __extbuf_;
387 __extbuf_ = __extbuf_min_;
389 __extbufnext_ = __extbuf_ + __rn;
390 __extbufend_ = __extbuf_ + __re;
391 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
392 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
394 _VSTD::swap(__ebs_, __rhs.__ebs_);
395 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
396 _VSTD::swap(__ibs_, __rhs.__ibs_);
397 _VSTD::swap(__file_, __rhs.__file_);
398 _VSTD::swap(__cv_, __rhs.__cv_);
399 _VSTD::swap(__st_, __rhs.__st_);
400 _VSTD::swap(__om_, __rhs.__om_);
401 _VSTD::swap(__cm_, __rhs.__cm_);
402 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
403 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
404 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
405 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
407 ptrdiff_t __n = this->gptr() - this->eback();
408 ptrdiff_t __e = this->egptr() - this->eback();
409 this->setg((char_type*)__extbuf_min_,
410 (char_type*)__extbuf_min_ + __n,
411 (char_type*)__extbuf_min_ + __e);
413 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
415 ptrdiff_t __n = this->pptr() - this->pbase();
416 ptrdiff_t __e = this->epptr() - this->pbase();
417 this->setp((char_type*)__extbuf_min_,
418 (char_type*)__extbuf_min_ + __e);
421 if (__rhs.eback() == (char_type*)__extbuf_min_)
423 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
424 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
425 __rhs.setg((char_type*)__rhs.__extbuf_min_,
426 (char_type*)__rhs.__extbuf_min_ + __n,
427 (char_type*)__rhs.__extbuf_min_ + __e);
429 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
431 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
432 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
433 __rhs.setp((char_type*)__rhs.__extbuf_min_,
434 (char_type*)__rhs.__extbuf_min_ + __e);
439 template <class _CharT, class _Traits>
440 inline _LIBCPP_INLINE_VISIBILITY
442 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
447 template <class _CharT, class _Traits>
448 inline _LIBCPP_INLINE_VISIBILITY
450 basic_filebuf<_CharT, _Traits>::is_open() const
455 template <class _CharT, class _Traits>
456 basic_filebuf<_CharT, _Traits>*
457 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
459 basic_filebuf<_CharT, _Traits>* __rt = 0;
464 switch (__mode & ~ios_base::ate)
467 case ios_base::out | ios_base::trunc:
470 case ios_base::out | ios_base::app:
477 case ios_base::in | ios_base::out:
480 case ios_base::in | ios_base::out | ios_base::trunc:
483 case ios_base::in | ios_base::out | ios_base::app:
484 case ios_base::in | ios_base::app:
487 case ios_base::out | ios_base::binary:
488 case ios_base::out | ios_base::trunc | ios_base::binary:
491 case ios_base::out | ios_base::app | ios_base::binary:
492 case ios_base::app | ios_base::binary:
495 case ios_base::in | ios_base::binary:
498 case ios_base::in | ios_base::out | ios_base::binary:
501 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
504 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
505 case ios_base::in | ios_base::app | ios_base::binary:
514 __file_ = fopen(__s, __mdstr);
518 if (__mode & ios_base::ate)
520 if (fseek(__file_, 0, SEEK_END))
535 template <class _CharT, class _Traits>
536 inline _LIBCPP_INLINE_VISIBILITY
537 basic_filebuf<_CharT, _Traits>*
538 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
540 return open(__s.c_str(), __mode);
543 template <class _CharT, class _Traits>
544 basic_filebuf<_CharT, _Traits>*
545 basic_filebuf<_CharT, _Traits>::close()
547 basic_filebuf<_CharT, _Traits>* __rt = 0;
551 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
554 if (fclose(__h.release()) == 0)
562 template <class _CharT, class _Traits>
563 typename basic_filebuf<_CharT, _Traits>::int_type
564 basic_filebuf<_CharT, _Traits>::underflow()
567 return traits_type::eof();
568 bool __initial = __read_mode();
570 if (this->gptr() == 0)
571 this->setg(&__1buf, &__1buf+1, &__1buf+1);
572 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
573 int_type __c = traits_type::eof();
574 if (this->gptr() == this->egptr())
576 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
577 if (__always_noconv_)
579 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
580 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
583 this->setg(this->eback(),
584 this->eback() + __unget_sz,
585 this->eback() + __unget_sz + __nmemb);
586 __c = traits_type::to_int_type(*this->gptr());
591 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
592 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
593 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
594 size_t __nmemb = _VSTD::min(static_cast<size_t>(this->egptr() - this->eback() - __unget_sz),
595 static_cast<size_t>(__extbufend_ - __extbufnext_));
596 codecvt_base::result __r;
597 state_type __svs = __st_;
598 size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
601 __extbufend_ = __extbufnext_ + __nr;
603 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
604 this->eback() + __unget_sz,
605 this->egptr(), __inext);
606 if (__r == codecvt_base::noconv)
608 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
609 __c = traits_type::to_int_type(*this->gptr());
611 else if (__inext != this->eback() + __unget_sz)
613 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
614 __c = traits_type::to_int_type(*this->gptr());
620 __c = traits_type::to_int_type(*this->gptr());
621 if (this->eback() == &__1buf)
626 template <class _CharT, class _Traits>
627 typename basic_filebuf<_CharT, _Traits>::int_type
628 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
630 if (__file_ && this->eback() < this->gptr())
632 if (traits_type::eq_int_type(__c, traits_type::eof()))
635 return traits_type::not_eof(__c);
637 if ((__om_ & ios_base::out) ||
638 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
641 *this->gptr() = traits_type::to_char_type(__c);
645 return traits_type::eof();
648 template <class _CharT, class _Traits>
649 typename basic_filebuf<_CharT, _Traits>::int_type
650 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
653 return traits_type::eof();
656 char_type* __pb_save = this->pbase();
657 char_type* __epb_save = this->epptr();
658 if (!traits_type::eq_int_type(__c, traits_type::eof()))
660 if (this->pptr() == 0)
661 this->setp(&__1buf, &__1buf+1);
662 *this->pptr() = traits_type::to_char_type(__c);
665 if (this->pptr() != this->pbase())
667 if (__always_noconv_)
669 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
670 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
671 return traits_type::eof();
675 char* __extbe = __extbuf_;
676 codecvt_base::result __r;
679 const char_type* __e;
680 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
681 __extbuf_, __extbuf_ + __ebs_, __extbe);
682 if (__e == this->pbase())
683 return traits_type::eof();
684 if (__r == codecvt_base::noconv)
686 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
687 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
688 return traits_type::eof();
690 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
692 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
693 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
694 return traits_type::eof();
695 if (__r == codecvt_base::partial)
697 this->setp((char_type*)__e, this->pptr());
698 this->pbump(this->epptr() - this->pbase());
702 return traits_type::eof();
703 } while (__r == codecvt_base::partial);
705 this->setp(__pb_save, __epb_save);
707 return traits_type::not_eof(__c);
710 template <class _CharT, class _Traits>
711 basic_streambuf<_CharT, _Traits>*
712 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
721 if (__ebs_ > sizeof(__extbuf_min_))
723 if (__always_noconv_ && __s)
725 __extbuf_ = (char*)__s;
730 __extbuf_ = new char[__ebs_];
736 __extbuf_ = __extbuf_min_;
737 __ebs_ = sizeof(__extbuf_min_);
740 if (!__always_noconv_)
742 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
743 if (__s && __ibs_ >= sizeof(__extbuf_min_))
750 __intbuf_ = new char_type[__ibs_];
763 template <class _CharT, class _Traits>
764 typename basic_filebuf<_CharT, _Traits>::pos_type
765 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
768 int __width = __cv_->encoding();
769 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
770 return pos_type(off_type(-1));
771 // __width > 0 || __off == 0
785 return pos_type(off_type(-1));
787 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
788 return pos_type(off_type(-1));
789 pos_type __r = ftello(__file_);
794 template <class _CharT, class _Traits>
795 typename basic_filebuf<_CharT, _Traits>::pos_type
796 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
798 if (__file_ == 0 || sync())
799 return pos_type(off_type(-1));
800 if (fseeko(__file_, __sp, SEEK_SET))
801 return pos_type(off_type(-1));
805 template <class _CharT, class _Traits>
807 basic_filebuf<_CharT, _Traits>::sync()
811 if (__cm_ & ios_base::out)
813 if (this->pptr() != this->pbase())
814 if (overflow() == traits_type::eof())
816 codecvt_base::result __r;
820 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
821 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
822 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
824 } while (__r == codecvt_base::partial);
825 if (__r == codecvt_base::error)
830 else if (__cm_ & ios_base::in)
833 if (__always_noconv_)
834 __c = this->egptr() - this->gptr();
837 int __width = __cv_->encoding();
838 __c = __extbufend_ - __extbufnext_;
840 __c += __width * (this->egptr() - this->gptr());
843 if (this->gptr() != this->egptr())
845 reverse(this->gptr(), this->egptr());
846 codecvt_base::result __r;
847 const char_type* __e = this->gptr();
851 __r = __cv_->out(__st_, __e, this->egptr(), __e,
852 __extbuf_, __extbuf_ + __ebs_, __extbe);
855 case codecvt_base::noconv:
856 __c += this->egptr() - this->gptr();
858 case codecvt_base::ok:
859 case codecvt_base::partial:
860 __c += __extbe - __extbuf_;
865 } while (__r == codecvt_base::partial);
869 if (fseeko(__file_, -__c, SEEK_CUR))
877 template <class _CharT, class _Traits>
879 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
882 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
883 bool __old_anc = __always_noconv_;
884 __always_noconv_ = __cv_->always_noconv();
885 if (__old_anc != __always_noconv_)
889 // invariant, char_type is char, else we couldn't get here
890 if (__always_noconv_) // need to dump __intbuf_
894 __owns_eb_ = __owns_ib_;
896 __extbuf_ = (char*)__intbuf_;
901 else // need to obtain an __intbuf_.
902 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
903 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
906 __intbuf_ = (char_type*)__extbuf_;
908 __extbuf_ = new char[__ebs_];
914 __intbuf_ = new char_type[__ibs_];
921 template <class _CharT, class _Traits>
923 basic_filebuf<_CharT, _Traits>::__read_mode()
925 if (!(__cm_ & ios_base::in))
928 if (__always_noconv_)
929 this->setg((char_type*)__extbuf_,
930 (char_type*)__extbuf_ + __ebs_,
931 (char_type*)__extbuf_ + __ebs_);
933 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
934 __cm_ = ios_base::in;
940 template <class _CharT, class _Traits>
942 basic_filebuf<_CharT, _Traits>::__write_mode()
944 if (!(__cm_ & ios_base::out))
947 if (__ebs_ > sizeof(__extbuf_min_))
949 if (__always_noconv_)
950 this->setp((char_type*)__extbuf_,
951 (char_type*)__extbuf_ + (__ebs_ - 1));
953 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
957 __cm_ = ios_base::out;
963 template <class _CharT, class _Traits>
964 class _LIBCPP_VISIBLE basic_ifstream
965 : public basic_istream<_CharT, _Traits>
968 typedef _CharT char_type;
969 typedef _Traits traits_type;
970 typedef typename traits_type::int_type int_type;
971 typedef typename traits_type::pos_type pos_type;
972 typedef typename traits_type::off_type off_type;
975 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
976 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
977 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
978 basic_ifstream(basic_ifstream&& __rhs);
981 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
982 basic_ifstream& operator=(basic_ifstream&& __rhs);
984 void swap(basic_ifstream& __rhs);
986 basic_filebuf<char_type, traits_type>* rdbuf() const;
987 bool is_open() const;
988 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
989 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
993 basic_filebuf<char_type, traits_type> __sb_;
996 template <class _CharT, class _Traits>
997 inline _LIBCPP_INLINE_VISIBILITY
998 basic_ifstream<_CharT, _Traits>::basic_ifstream()
999 : basic_istream<char_type, traits_type>(&__sb_)
1003 template <class _CharT, class _Traits>
1004 inline _LIBCPP_INLINE_VISIBILITY
1005 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1006 : basic_istream<char_type, traits_type>(&__sb_)
1008 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1009 this->setstate(ios_base::failbit);
1012 template <class _CharT, class _Traits>
1013 inline _LIBCPP_INLINE_VISIBILITY
1014 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1015 : basic_istream<char_type, traits_type>(&__sb_)
1017 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1018 this->setstate(ios_base::failbit);
1021 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1023 template <class _CharT, class _Traits>
1024 inline _LIBCPP_INLINE_VISIBILITY
1025 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1026 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1027 __sb_(_VSTD::move(__rhs.__sb_))
1029 this->set_rdbuf(&__sb_);
1032 template <class _CharT, class _Traits>
1033 inline _LIBCPP_INLINE_VISIBILITY
1034 basic_ifstream<_CharT, _Traits>&
1035 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1037 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1038 __sb_ = _VSTD::move(__rhs.__sb_);
1042 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1044 template <class _CharT, class _Traits>
1045 inline _LIBCPP_INLINE_VISIBILITY
1047 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1049 basic_istream<char_type, traits_type>::swap(__rhs);
1050 __sb_.swap(__rhs.__sb_);
1053 template <class _CharT, class _Traits>
1054 inline _LIBCPP_INLINE_VISIBILITY
1056 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1061 template <class _CharT, class _Traits>
1062 inline _LIBCPP_INLINE_VISIBILITY
1063 basic_filebuf<_CharT, _Traits>*
1064 basic_ifstream<_CharT, _Traits>::rdbuf() const
1066 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1069 template <class _CharT, class _Traits>
1070 inline _LIBCPP_INLINE_VISIBILITY
1072 basic_ifstream<_CharT, _Traits>::is_open() const
1074 return __sb_.is_open();
1077 template <class _CharT, class _Traits>
1079 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1081 if (__sb_.open(__s, __mode | ios_base::in))
1084 this->setstate(ios_base::failbit);
1087 template <class _CharT, class _Traits>
1089 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1091 if (__sb_.open(__s, __mode | ios_base::in))
1094 this->setstate(ios_base::failbit);
1097 template <class _CharT, class _Traits>
1098 inline _LIBCPP_INLINE_VISIBILITY
1100 basic_ifstream<_CharT, _Traits>::close()
1102 if (__sb_.close() == 0)
1103 this->setstate(ios_base::failbit);
1108 template <class _CharT, class _Traits>
1109 class _LIBCPP_VISIBLE basic_ofstream
1110 : public basic_ostream<_CharT, _Traits>
1113 typedef _CharT char_type;
1114 typedef _Traits traits_type;
1115 typedef typename traits_type::int_type int_type;
1116 typedef typename traits_type::pos_type pos_type;
1117 typedef typename traits_type::off_type off_type;
1120 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1121 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1122 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1123 basic_ofstream(basic_ofstream&& __rhs);
1126 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1127 basic_ofstream& operator=(basic_ofstream&& __rhs);
1129 void swap(basic_ofstream& __rhs);
1131 basic_filebuf<char_type, traits_type>* rdbuf() const;
1132 bool is_open() const;
1133 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1134 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1138 basic_filebuf<char_type, traits_type> __sb_;
1141 template <class _CharT, class _Traits>
1142 inline _LIBCPP_INLINE_VISIBILITY
1143 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1144 : basic_ostream<char_type, traits_type>(&__sb_)
1148 template <class _CharT, class _Traits>
1149 inline _LIBCPP_INLINE_VISIBILITY
1150 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1151 : basic_ostream<char_type, traits_type>(&__sb_)
1153 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1154 this->setstate(ios_base::failbit);
1157 template <class _CharT, class _Traits>
1158 inline _LIBCPP_INLINE_VISIBILITY
1159 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1160 : basic_ostream<char_type, traits_type>(&__sb_)
1162 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1163 this->setstate(ios_base::failbit);
1166 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1168 template <class _CharT, class _Traits>
1169 inline _LIBCPP_INLINE_VISIBILITY
1170 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1171 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1172 __sb_(_VSTD::move(__rhs.__sb_))
1174 this->set_rdbuf(&__sb_);
1177 template <class _CharT, class _Traits>
1178 inline _LIBCPP_INLINE_VISIBILITY
1179 basic_ofstream<_CharT, _Traits>&
1180 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1182 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1183 __sb_ = _VSTD::move(__rhs.__sb_);
1187 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1189 template <class _CharT, class _Traits>
1190 inline _LIBCPP_INLINE_VISIBILITY
1192 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1194 basic_ostream<char_type, traits_type>::swap(__rhs);
1195 __sb_.swap(__rhs.__sb_);
1198 template <class _CharT, class _Traits>
1199 inline _LIBCPP_INLINE_VISIBILITY
1201 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1206 template <class _CharT, class _Traits>
1207 inline _LIBCPP_INLINE_VISIBILITY
1208 basic_filebuf<_CharT, _Traits>*
1209 basic_ofstream<_CharT, _Traits>::rdbuf() const
1211 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1214 template <class _CharT, class _Traits>
1215 inline _LIBCPP_INLINE_VISIBILITY
1217 basic_ofstream<_CharT, _Traits>::is_open() const
1219 return __sb_.is_open();
1222 template <class _CharT, class _Traits>
1224 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1226 if (__sb_.open(__s, __mode | ios_base::out))
1229 this->setstate(ios_base::failbit);
1232 template <class _CharT, class _Traits>
1234 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1236 if (__sb_.open(__s, __mode | ios_base::out))
1239 this->setstate(ios_base::failbit);
1242 template <class _CharT, class _Traits>
1243 inline _LIBCPP_INLINE_VISIBILITY
1245 basic_ofstream<_CharT, _Traits>::close()
1247 if (__sb_.close() == 0)
1248 this->setstate(ios_base::failbit);
1253 template <class _CharT, class _Traits>
1254 class _LIBCPP_VISIBLE basic_fstream
1255 : public basic_iostream<_CharT, _Traits>
1258 typedef _CharT char_type;
1259 typedef _Traits traits_type;
1260 typedef typename traits_type::int_type int_type;
1261 typedef typename traits_type::pos_type pos_type;
1262 typedef typename traits_type::off_type off_type;
1265 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1266 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1267 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1268 basic_fstream(basic_fstream&& __rhs);
1271 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1272 basic_fstream& operator=(basic_fstream&& __rhs);
1274 void swap(basic_fstream& __rhs);
1276 basic_filebuf<char_type, traits_type>* rdbuf() const;
1277 bool is_open() const;
1278 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1279 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1283 basic_filebuf<char_type, traits_type> __sb_;
1286 template <class _CharT, class _Traits>
1287 inline _LIBCPP_INLINE_VISIBILITY
1288 basic_fstream<_CharT, _Traits>::basic_fstream()
1289 : basic_iostream<char_type, traits_type>(&__sb_)
1293 template <class _CharT, class _Traits>
1294 inline _LIBCPP_INLINE_VISIBILITY
1295 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1296 : basic_iostream<char_type, traits_type>(&__sb_)
1298 if (__sb_.open(__s, __mode) == 0)
1299 this->setstate(ios_base::failbit);
1302 template <class _CharT, class _Traits>
1303 inline _LIBCPP_INLINE_VISIBILITY
1304 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1305 : basic_iostream<char_type, traits_type>(&__sb_)
1307 if (__sb_.open(__s, __mode) == 0)
1308 this->setstate(ios_base::failbit);
1311 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1313 template <class _CharT, class _Traits>
1314 inline _LIBCPP_INLINE_VISIBILITY
1315 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1316 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1317 __sb_(_VSTD::move(__rhs.__sb_))
1319 this->set_rdbuf(&__sb_);
1322 template <class _CharT, class _Traits>
1323 inline _LIBCPP_INLINE_VISIBILITY
1324 basic_fstream<_CharT, _Traits>&
1325 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1327 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1328 __sb_ = _VSTD::move(__rhs.__sb_);
1332 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1334 template <class _CharT, class _Traits>
1335 inline _LIBCPP_INLINE_VISIBILITY
1337 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1339 basic_iostream<char_type, traits_type>::swap(__rhs);
1340 __sb_.swap(__rhs.__sb_);
1343 template <class _CharT, class _Traits>
1344 inline _LIBCPP_INLINE_VISIBILITY
1346 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1351 template <class _CharT, class _Traits>
1352 inline _LIBCPP_INLINE_VISIBILITY
1353 basic_filebuf<_CharT, _Traits>*
1354 basic_fstream<_CharT, _Traits>::rdbuf() const
1356 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1359 template <class _CharT, class _Traits>
1360 inline _LIBCPP_INLINE_VISIBILITY
1362 basic_fstream<_CharT, _Traits>::is_open() const
1364 return __sb_.is_open();
1367 template <class _CharT, class _Traits>
1369 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1371 if (__sb_.open(__s, __mode))
1374 this->setstate(ios_base::failbit);
1377 template <class _CharT, class _Traits>
1379 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1381 if (__sb_.open(__s, __mode))
1384 this->setstate(ios_base::failbit);
1387 template <class _CharT, class _Traits>
1388 inline _LIBCPP_INLINE_VISIBILITY
1390 basic_fstream<_CharT, _Traits>::close()
1392 if (__sb_.close() == 0)
1393 this->setstate(ios_base::failbit);
1396 _LIBCPP_END_NAMESPACE_STD
1398 #endif // _LIBCPP_FSTREAM