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_TYPE_VIS basic_filebuf
184 : public basic_streambuf<_CharT, _Traits>
187 typedef _CharT char_type;
188 typedef _Traits traits_type;
189 typedef typename traits_type::int_type int_type;
190 typedef typename traits_type::pos_type pos_type;
191 typedef typename traits_type::off_type off_type;
192 typedef typename traits_type::state_type state_type;
194 // 27.9.1.2 Constructors/destructor:
196 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
197 basic_filebuf(basic_filebuf&& __rhs);
199 virtual ~basic_filebuf();
201 // 27.9.1.3 Assign/swap:
202 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
203 basic_filebuf& operator=(basic_filebuf&& __rhs);
205 void swap(basic_filebuf& __rhs);
208 bool is_open() const;
209 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
210 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
211 basic_filebuf* close();
214 // 27.9.1.5 Overridden virtual functions:
215 virtual int_type underflow();
216 virtual int_type pbackfail(int_type __c = traits_type::eof());
217 virtual int_type overflow (int_type __c = traits_type::eof());
218 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
219 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
220 ios_base::openmode __wch = ios_base::in | ios_base::out);
221 virtual pos_type seekpos(pos_type __sp,
222 ios_base::openmode __wch = ios_base::in | ios_base::out);
224 virtual void imbue(const locale& __loc);
228 const char* __extbufnext_;
229 const char* __extbufend_;
230 char __extbuf_min_[8];
232 char_type* __intbuf_;
235 const codecvt<char_type, char, state_type>* __cv_;
237 state_type __st_last_;
238 ios_base::openmode __om_;
239 ios_base::openmode __cm_;
242 bool __always_noconv_;
248 template <class _CharT, class _Traits>
249 basic_filebuf<_CharT, _Traits>::basic_filebuf()
264 __always_noconv_(false)
266 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
268 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
269 __always_noconv_ = __cv_->always_noconv();
274 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
276 template <class _CharT, class _Traits>
277 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
278 : basic_streambuf<_CharT, _Traits>(__rhs)
280 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
282 __extbuf_ = __extbuf_min_;
283 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
284 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
288 __extbuf_ = __rhs.__extbuf_;
289 __extbufnext_ = __rhs.__extbufnext_;
290 __extbufend_ = __rhs.__extbufend_;
292 __ebs_ = __rhs.__ebs_;
293 __intbuf_ = __rhs.__intbuf_;
294 __ibs_ = __rhs.__ibs_;
295 __file_ = __rhs.__file_;
298 __st_last_ = __rhs.__st_last_;
301 __owns_eb_ = __rhs.__owns_eb_;
302 __owns_ib_ = __rhs.__owns_ib_;
303 __always_noconv_ = __rhs.__always_noconv_;
306 if (__rhs.pbase() == __rhs.__intbuf_)
307 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
309 this->setp((char_type*)__extbuf_,
310 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
311 this->pbump(__rhs. pptr() - __rhs.pbase());
313 else if (__rhs.eback())
315 if (__rhs.eback() == __rhs.__intbuf_)
316 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
317 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
319 this->setg((char_type*)__extbuf_,
320 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
321 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
324 __rhs.__extbufnext_ = 0;
325 __rhs.__extbufend_ = 0;
330 __rhs.__st_ = state_type();
331 __rhs.__st_last_ = state_type();
334 __rhs.__owns_eb_ = false;
335 __rhs.__owns_ib_ = false;
340 template <class _CharT, class _Traits>
341 inline _LIBCPP_INLINE_VISIBILITY
342 basic_filebuf<_CharT, _Traits>&
343 basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
350 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
352 template <class _CharT, class _Traits>
353 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
355 #ifndef _LIBCPP_NO_EXCEPTIONS
358 #endif // _LIBCPP_NO_EXCEPTIONS
360 #ifndef _LIBCPP_NO_EXCEPTIONS
365 #endif // _LIBCPP_NO_EXCEPTIONS
372 template <class _CharT, class _Traits>
374 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
376 basic_streambuf<char_type, traits_type>::swap(__rhs);
377 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
379 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
380 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
381 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
385 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
386 ptrdiff_t __le = __extbufend_ - __extbuf_;
387 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
388 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
389 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
391 __extbuf_ = __rhs.__extbuf_;
392 __rhs.__extbuf_ = __rhs.__extbuf_min_;
394 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
396 __rhs.__extbuf_ = __extbuf_;
397 __extbuf_ = __extbuf_min_;
399 __extbufnext_ = __extbuf_ + __rn;
400 __extbufend_ = __extbuf_ + __re;
401 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
402 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
404 _VSTD::swap(__ebs_, __rhs.__ebs_);
405 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
406 _VSTD::swap(__ibs_, __rhs.__ibs_);
407 _VSTD::swap(__file_, __rhs.__file_);
408 _VSTD::swap(__cv_, __rhs.__cv_);
409 _VSTD::swap(__st_, __rhs.__st_);
410 _VSTD::swap(__st_last_, __rhs.__st_last_);
411 _VSTD::swap(__om_, __rhs.__om_);
412 _VSTD::swap(__cm_, __rhs.__cm_);
413 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
414 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
415 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
416 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
418 ptrdiff_t __n = this->gptr() - this->eback();
419 ptrdiff_t __e = this->egptr() - this->eback();
420 this->setg((char_type*)__extbuf_min_,
421 (char_type*)__extbuf_min_ + __n,
422 (char_type*)__extbuf_min_ + __e);
424 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
426 ptrdiff_t __n = this->pptr() - this->pbase();
427 ptrdiff_t __e = this->epptr() - this->pbase();
428 this->setp((char_type*)__extbuf_min_,
429 (char_type*)__extbuf_min_ + __e);
432 if (__rhs.eback() == (char_type*)__extbuf_min_)
434 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
435 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
436 __rhs.setg((char_type*)__rhs.__extbuf_min_,
437 (char_type*)__rhs.__extbuf_min_ + __n,
438 (char_type*)__rhs.__extbuf_min_ + __e);
440 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
442 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
443 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
444 __rhs.setp((char_type*)__rhs.__extbuf_min_,
445 (char_type*)__rhs.__extbuf_min_ + __e);
450 template <class _CharT, class _Traits>
451 inline _LIBCPP_INLINE_VISIBILITY
453 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
458 template <class _CharT, class _Traits>
459 inline _LIBCPP_INLINE_VISIBILITY
461 basic_filebuf<_CharT, _Traits>::is_open() const
466 template <class _CharT, class _Traits>
467 basic_filebuf<_CharT, _Traits>*
468 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
470 basic_filebuf<_CharT, _Traits>* __rt = 0;
475 switch (__mode & ~ios_base::ate)
478 case ios_base::out | ios_base::trunc:
481 case ios_base::out | ios_base::app:
488 case ios_base::in | ios_base::out:
491 case ios_base::in | ios_base::out | ios_base::trunc:
494 case ios_base::in | ios_base::out | ios_base::app:
495 case ios_base::in | ios_base::app:
498 case ios_base::out | ios_base::binary:
499 case ios_base::out | ios_base::trunc | ios_base::binary:
502 case ios_base::out | ios_base::app | ios_base::binary:
503 case ios_base::app | ios_base::binary:
506 case ios_base::in | ios_base::binary:
509 case ios_base::in | ios_base::out | ios_base::binary:
512 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
515 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
516 case ios_base::in | ios_base::app | ios_base::binary:
525 __file_ = fopen(__s, __mdstr);
529 if (__mode & ios_base::ate)
531 if (fseek(__file_, 0, SEEK_END))
546 template <class _CharT, class _Traits>
547 inline _LIBCPP_INLINE_VISIBILITY
548 basic_filebuf<_CharT, _Traits>*
549 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
551 return open(__s.c_str(), __mode);
554 template <class _CharT, class _Traits>
555 basic_filebuf<_CharT, _Traits>*
556 basic_filebuf<_CharT, _Traits>::close()
558 basic_filebuf<_CharT, _Traits>* __rt = 0;
562 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
565 if (fclose(__h.release()) == 0)
573 template <class _CharT, class _Traits>
574 typename basic_filebuf<_CharT, _Traits>::int_type
575 basic_filebuf<_CharT, _Traits>::underflow()
578 return traits_type::eof();
579 bool __initial = __read_mode();
581 if (this->gptr() == 0)
582 this->setg(&__1buf, &__1buf+1, &__1buf+1);
583 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
584 int_type __c = traits_type::eof();
585 if (this->gptr() == this->egptr())
587 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
588 if (__always_noconv_)
590 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
591 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
594 this->setg(this->eback(),
595 this->eback() + __unget_sz,
596 this->eback() + __unget_sz + __nmemb);
597 __c = traits_type::to_int_type(*this->gptr());
602 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
603 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
604 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
605 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
606 static_cast<size_t>(__extbufend_ - __extbufnext_));
607 codecvt_base::result __r;
609 size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
612 #ifndef _LIBCPP_NO_EXCEPTIONS
616 __extbufend_ = __extbufnext_ + __nr;
618 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
619 this->eback() + __unget_sz,
620 this->eback() + __ibs_, __inext);
621 if (__r == codecvt_base::noconv)
623 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
624 __c = traits_type::to_int_type(*this->gptr());
626 else if (__inext != this->eback() + __unget_sz)
628 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
629 __c = traits_type::to_int_type(*this->gptr());
635 __c = traits_type::to_int_type(*this->gptr());
636 if (this->eback() == &__1buf)
641 template <class _CharT, class _Traits>
642 typename basic_filebuf<_CharT, _Traits>::int_type
643 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
645 if (__file_ && this->eback() < this->gptr())
647 if (traits_type::eq_int_type(__c, traits_type::eof()))
650 return traits_type::not_eof(__c);
652 if ((__om_ & ios_base::out) ||
653 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
656 *this->gptr() = traits_type::to_char_type(__c);
660 return traits_type::eof();
663 template <class _CharT, class _Traits>
664 typename basic_filebuf<_CharT, _Traits>::int_type
665 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
668 return traits_type::eof();
671 char_type* __pb_save = this->pbase();
672 char_type* __epb_save = this->epptr();
673 if (!traits_type::eq_int_type(__c, traits_type::eof()))
675 if (this->pptr() == 0)
676 this->setp(&__1buf, &__1buf+1);
677 *this->pptr() = traits_type::to_char_type(__c);
680 if (this->pptr() != this->pbase())
682 if (__always_noconv_)
684 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
685 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
686 return traits_type::eof();
690 char* __extbe = __extbuf_;
691 codecvt_base::result __r;
694 #ifndef _LIBCPP_NO_EXCEPTIONS
698 const char_type* __e;
699 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
700 __extbuf_, __extbuf_ + __ebs_, __extbe);
701 if (__e == this->pbase())
702 return traits_type::eof();
703 if (__r == codecvt_base::noconv)
705 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
706 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
707 return traits_type::eof();
709 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
711 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
712 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
713 return traits_type::eof();
714 if (__r == codecvt_base::partial)
716 this->setp((char_type*)__e, this->pptr());
717 this->pbump(this->epptr() - this->pbase());
721 return traits_type::eof();
722 } while (__r == codecvt_base::partial);
724 this->setp(__pb_save, __epb_save);
726 return traits_type::not_eof(__c);
729 template <class _CharT, class _Traits>
730 basic_streambuf<_CharT, _Traits>*
731 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
740 if (__ebs_ > sizeof(__extbuf_min_))
742 if (__always_noconv_ && __s)
744 __extbuf_ = (char*)__s;
749 __extbuf_ = new char[__ebs_];
755 __extbuf_ = __extbuf_min_;
756 __ebs_ = sizeof(__extbuf_min_);
759 if (!__always_noconv_)
761 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
762 if (__s && __ibs_ >= sizeof(__extbuf_min_))
769 __intbuf_ = new char_type[__ibs_];
782 template <class _CharT, class _Traits>
783 typename basic_filebuf<_CharT, _Traits>::pos_type
784 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
787 #ifndef _LIBCPP_NO_EXCEPTIONS
791 int __width = __cv_->encoding();
792 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
793 return pos_type(off_type(-1));
794 // __width > 0 || __off == 0
808 return pos_type(off_type(-1));
811 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
812 return pos_type(off_type(-1));
813 pos_type __r = ftell(__file_);
815 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
816 return pos_type(off_type(-1));
817 pos_type __r = ftello(__file_);
823 template <class _CharT, class _Traits>
824 typename basic_filebuf<_CharT, _Traits>::pos_type
825 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
827 if (__file_ == 0 || sync())
828 return pos_type(off_type(-1));
830 if (fseek(__file_, __sp, SEEK_SET))
831 return pos_type(off_type(-1));
833 if (fseeko(__file_, __sp, SEEK_SET))
834 return pos_type(off_type(-1));
836 __st_ = __sp.state();
840 template <class _CharT, class _Traits>
842 basic_filebuf<_CharT, _Traits>::sync()
846 #ifndef _LIBCPP_NO_EXCEPTIONS
850 if (__cm_ & ios_base::out)
852 if (this->pptr() != this->pbase())
853 if (overflow() == traits_type::eof())
855 codecvt_base::result __r;
859 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
860 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
861 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
863 } while (__r == codecvt_base::partial);
864 if (__r == codecvt_base::error)
869 else if (__cm_ & ios_base::in)
872 state_type __state = __st_last_;
873 bool __update_st = false;
874 if (__always_noconv_)
875 __c = this->egptr() - this->gptr();
878 int __width = __cv_->encoding();
879 __c = __extbufend_ - __extbufnext_;
881 __c += __width * (this->egptr() - this->gptr());
884 if (this->gptr() != this->egptr())
886 const int __off = __cv_->length(__state, __extbuf_,
888 this->gptr() - this->eback());
889 __c += __extbufnext_ - __extbuf_ - __off;
895 if (fseek(__file_, -__c, SEEK_CUR))
898 if (fseeko(__file_, -__c, SEEK_CUR))
903 __extbufnext_ = __extbufend_ = __extbuf_;
910 template <class _CharT, class _Traits>
912 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
915 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
916 bool __old_anc = __always_noconv_;
917 __always_noconv_ = __cv_->always_noconv();
918 if (__old_anc != __always_noconv_)
922 // invariant, char_type is char, else we couldn't get here
923 if (__always_noconv_) // need to dump __intbuf_
927 __owns_eb_ = __owns_ib_;
929 __extbuf_ = (char*)__intbuf_;
934 else // need to obtain an __intbuf_.
935 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
936 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
939 __intbuf_ = (char_type*)__extbuf_;
941 __extbuf_ = new char[__ebs_];
947 __intbuf_ = new char_type[__ibs_];
954 template <class _CharT, class _Traits>
956 basic_filebuf<_CharT, _Traits>::__read_mode()
958 if (!(__cm_ & ios_base::in))
961 if (__always_noconv_)
962 this->setg((char_type*)__extbuf_,
963 (char_type*)__extbuf_ + __ebs_,
964 (char_type*)__extbuf_ + __ebs_);
966 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
967 __cm_ = ios_base::in;
973 template <class _CharT, class _Traits>
975 basic_filebuf<_CharT, _Traits>::__write_mode()
977 if (!(__cm_ & ios_base::out))
980 if (__ebs_ > sizeof(__extbuf_min_))
982 if (__always_noconv_)
983 this->setp((char_type*)__extbuf_,
984 (char_type*)__extbuf_ + (__ebs_ - 1));
986 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
990 __cm_ = ios_base::out;
996 template <class _CharT, class _Traits>
997 class _LIBCPP_TYPE_VIS basic_ifstream
998 : public basic_istream<_CharT, _Traits>
1001 typedef _CharT char_type;
1002 typedef _Traits traits_type;
1003 typedef typename traits_type::int_type int_type;
1004 typedef typename traits_type::pos_type pos_type;
1005 typedef typename traits_type::off_type off_type;
1008 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1009 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1010 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1011 basic_ifstream(basic_ifstream&& __rhs);
1014 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1015 basic_ifstream& operator=(basic_ifstream&& __rhs);
1017 void swap(basic_ifstream& __rhs);
1019 basic_filebuf<char_type, traits_type>* rdbuf() const;
1020 bool is_open() const;
1021 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1022 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1026 basic_filebuf<char_type, traits_type> __sb_;
1029 template <class _CharT, class _Traits>
1030 inline _LIBCPP_INLINE_VISIBILITY
1031 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1032 : basic_istream<char_type, traits_type>(&__sb_)
1036 template <class _CharT, class _Traits>
1037 inline _LIBCPP_INLINE_VISIBILITY
1038 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1039 : basic_istream<char_type, traits_type>(&__sb_)
1041 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1042 this->setstate(ios_base::failbit);
1045 template <class _CharT, class _Traits>
1046 inline _LIBCPP_INLINE_VISIBILITY
1047 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1048 : basic_istream<char_type, traits_type>(&__sb_)
1050 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1051 this->setstate(ios_base::failbit);
1054 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1056 template <class _CharT, class _Traits>
1057 inline _LIBCPP_INLINE_VISIBILITY
1058 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1059 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1060 __sb_(_VSTD::move(__rhs.__sb_))
1062 this->set_rdbuf(&__sb_);
1065 template <class _CharT, class _Traits>
1066 inline _LIBCPP_INLINE_VISIBILITY
1067 basic_ifstream<_CharT, _Traits>&
1068 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1070 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1071 __sb_ = _VSTD::move(__rhs.__sb_);
1075 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1077 template <class _CharT, class _Traits>
1078 inline _LIBCPP_INLINE_VISIBILITY
1080 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1082 basic_istream<char_type, traits_type>::swap(__rhs);
1083 __sb_.swap(__rhs.__sb_);
1086 template <class _CharT, class _Traits>
1087 inline _LIBCPP_INLINE_VISIBILITY
1089 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1094 template <class _CharT, class _Traits>
1095 inline _LIBCPP_INLINE_VISIBILITY
1096 basic_filebuf<_CharT, _Traits>*
1097 basic_ifstream<_CharT, _Traits>::rdbuf() const
1099 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1102 template <class _CharT, class _Traits>
1103 inline _LIBCPP_INLINE_VISIBILITY
1105 basic_ifstream<_CharT, _Traits>::is_open() const
1107 return __sb_.is_open();
1110 template <class _CharT, class _Traits>
1112 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1114 if (__sb_.open(__s, __mode | ios_base::in))
1117 this->setstate(ios_base::failbit);
1120 template <class _CharT, class _Traits>
1122 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1124 if (__sb_.open(__s, __mode | ios_base::in))
1127 this->setstate(ios_base::failbit);
1130 template <class _CharT, class _Traits>
1131 inline _LIBCPP_INLINE_VISIBILITY
1133 basic_ifstream<_CharT, _Traits>::close()
1135 if (__sb_.close() == 0)
1136 this->setstate(ios_base::failbit);
1141 template <class _CharT, class _Traits>
1142 class _LIBCPP_TYPE_VIS basic_ofstream
1143 : public basic_ostream<_CharT, _Traits>
1146 typedef _CharT char_type;
1147 typedef _Traits traits_type;
1148 typedef typename traits_type::int_type int_type;
1149 typedef typename traits_type::pos_type pos_type;
1150 typedef typename traits_type::off_type off_type;
1153 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1154 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1155 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1156 basic_ofstream(basic_ofstream&& __rhs);
1159 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1160 basic_ofstream& operator=(basic_ofstream&& __rhs);
1162 void swap(basic_ofstream& __rhs);
1164 basic_filebuf<char_type, traits_type>* rdbuf() const;
1165 bool is_open() const;
1166 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1167 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1171 basic_filebuf<char_type, traits_type> __sb_;
1174 template <class _CharT, class _Traits>
1175 inline _LIBCPP_INLINE_VISIBILITY
1176 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1177 : basic_ostream<char_type, traits_type>(&__sb_)
1181 template <class _CharT, class _Traits>
1182 inline _LIBCPP_INLINE_VISIBILITY
1183 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1184 : basic_ostream<char_type, traits_type>(&__sb_)
1186 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1187 this->setstate(ios_base::failbit);
1190 template <class _CharT, class _Traits>
1191 inline _LIBCPP_INLINE_VISIBILITY
1192 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1193 : basic_ostream<char_type, traits_type>(&__sb_)
1195 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1196 this->setstate(ios_base::failbit);
1199 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1201 template <class _CharT, class _Traits>
1202 inline _LIBCPP_INLINE_VISIBILITY
1203 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1204 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1205 __sb_(_VSTD::move(__rhs.__sb_))
1207 this->set_rdbuf(&__sb_);
1210 template <class _CharT, class _Traits>
1211 inline _LIBCPP_INLINE_VISIBILITY
1212 basic_ofstream<_CharT, _Traits>&
1213 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1215 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1216 __sb_ = _VSTD::move(__rhs.__sb_);
1220 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1222 template <class _CharT, class _Traits>
1223 inline _LIBCPP_INLINE_VISIBILITY
1225 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1227 basic_ostream<char_type, traits_type>::swap(__rhs);
1228 __sb_.swap(__rhs.__sb_);
1231 template <class _CharT, class _Traits>
1232 inline _LIBCPP_INLINE_VISIBILITY
1234 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1239 template <class _CharT, class _Traits>
1240 inline _LIBCPP_INLINE_VISIBILITY
1241 basic_filebuf<_CharT, _Traits>*
1242 basic_ofstream<_CharT, _Traits>::rdbuf() const
1244 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1247 template <class _CharT, class _Traits>
1248 inline _LIBCPP_INLINE_VISIBILITY
1250 basic_ofstream<_CharT, _Traits>::is_open() const
1252 return __sb_.is_open();
1255 template <class _CharT, class _Traits>
1257 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1259 if (__sb_.open(__s, __mode | ios_base::out))
1262 this->setstate(ios_base::failbit);
1265 template <class _CharT, class _Traits>
1267 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1269 if (__sb_.open(__s, __mode | ios_base::out))
1272 this->setstate(ios_base::failbit);
1275 template <class _CharT, class _Traits>
1276 inline _LIBCPP_INLINE_VISIBILITY
1278 basic_ofstream<_CharT, _Traits>::close()
1280 if (__sb_.close() == 0)
1281 this->setstate(ios_base::failbit);
1286 template <class _CharT, class _Traits>
1287 class _LIBCPP_TYPE_VIS basic_fstream
1288 : public basic_iostream<_CharT, _Traits>
1291 typedef _CharT char_type;
1292 typedef _Traits traits_type;
1293 typedef typename traits_type::int_type int_type;
1294 typedef typename traits_type::pos_type pos_type;
1295 typedef typename traits_type::off_type off_type;
1298 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1299 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1300 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1301 basic_fstream(basic_fstream&& __rhs);
1304 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1305 basic_fstream& operator=(basic_fstream&& __rhs);
1307 void swap(basic_fstream& __rhs);
1309 basic_filebuf<char_type, traits_type>* rdbuf() const;
1310 bool is_open() const;
1311 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1312 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1316 basic_filebuf<char_type, traits_type> __sb_;
1319 template <class _CharT, class _Traits>
1320 inline _LIBCPP_INLINE_VISIBILITY
1321 basic_fstream<_CharT, _Traits>::basic_fstream()
1322 : basic_iostream<char_type, traits_type>(&__sb_)
1326 template <class _CharT, class _Traits>
1327 inline _LIBCPP_INLINE_VISIBILITY
1328 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1329 : basic_iostream<char_type, traits_type>(&__sb_)
1331 if (__sb_.open(__s, __mode) == 0)
1332 this->setstate(ios_base::failbit);
1335 template <class _CharT, class _Traits>
1336 inline _LIBCPP_INLINE_VISIBILITY
1337 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1338 : basic_iostream<char_type, traits_type>(&__sb_)
1340 if (__sb_.open(__s, __mode) == 0)
1341 this->setstate(ios_base::failbit);
1344 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1346 template <class _CharT, class _Traits>
1347 inline _LIBCPP_INLINE_VISIBILITY
1348 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1349 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1350 __sb_(_VSTD::move(__rhs.__sb_))
1352 this->set_rdbuf(&__sb_);
1355 template <class _CharT, class _Traits>
1356 inline _LIBCPP_INLINE_VISIBILITY
1357 basic_fstream<_CharT, _Traits>&
1358 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1360 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1361 __sb_ = _VSTD::move(__rhs.__sb_);
1365 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1367 template <class _CharT, class _Traits>
1368 inline _LIBCPP_INLINE_VISIBILITY
1370 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1372 basic_iostream<char_type, traits_type>::swap(__rhs);
1373 __sb_.swap(__rhs.__sb_);
1376 template <class _CharT, class _Traits>
1377 inline _LIBCPP_INLINE_VISIBILITY
1379 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1384 template <class _CharT, class _Traits>
1385 inline _LIBCPP_INLINE_VISIBILITY
1386 basic_filebuf<_CharT, _Traits>*
1387 basic_fstream<_CharT, _Traits>::rdbuf() const
1389 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1392 template <class _CharT, class _Traits>
1393 inline _LIBCPP_INLINE_VISIBILITY
1395 basic_fstream<_CharT, _Traits>::is_open() const
1397 return __sb_.is_open();
1400 template <class _CharT, class _Traits>
1402 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1404 if (__sb_.open(__s, __mode))
1407 this->setstate(ios_base::failbit);
1410 template <class _CharT, class _Traits>
1412 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1414 if (__sb_.open(__s, __mode))
1417 this->setstate(ios_base::failbit);
1420 template <class _CharT, class _Traits>
1421 inline _LIBCPP_INLINE_VISIBILITY
1423 basic_fstream<_CharT, _Traits>::close()
1425 if (__sb_.close() == 0)
1426 this->setstate(ios_base::failbit);
1429 _LIBCPP_END_NAMESPACE_STD
1431 #endif // _LIBCPP_FSTREAM