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_TEMPLATE_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 _LIBCPP_INLINE_VISIBILITY
204 basic_filebuf& operator=(basic_filebuf&& __rhs);
206 void swap(basic_filebuf& __rhs);
209 _LIBCPP_INLINE_VISIBILITY
210 bool is_open() const;
211 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
212 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
213 _LIBCPP_INLINE_VISIBILITY
214 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
216 basic_filebuf* close();
219 // 27.9.1.5 Overridden virtual functions:
220 virtual int_type underflow();
221 virtual int_type pbackfail(int_type __c = traits_type::eof());
222 virtual int_type overflow (int_type __c = traits_type::eof());
223 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
224 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
225 ios_base::openmode __wch = ios_base::in | ios_base::out);
226 virtual pos_type seekpos(pos_type __sp,
227 ios_base::openmode __wch = ios_base::in | ios_base::out);
229 virtual void imbue(const locale& __loc);
233 const char* __extbufnext_;
234 const char* __extbufend_;
235 char __extbuf_min_[8];
237 char_type* __intbuf_;
240 const codecvt<char_type, char, state_type>* __cv_;
242 state_type __st_last_;
243 ios_base::openmode __om_;
244 ios_base::openmode __cm_;
247 bool __always_noconv_;
253 template <class _CharT, class _Traits>
254 basic_filebuf<_CharT, _Traits>::basic_filebuf()
269 __always_noconv_(false)
271 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
273 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
274 __always_noconv_ = __cv_->always_noconv();
279 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
281 template <class _CharT, class _Traits>
282 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
283 : basic_streambuf<_CharT, _Traits>(__rhs)
285 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
287 __extbuf_ = __extbuf_min_;
288 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
289 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
293 __extbuf_ = __rhs.__extbuf_;
294 __extbufnext_ = __rhs.__extbufnext_;
295 __extbufend_ = __rhs.__extbufend_;
297 __ebs_ = __rhs.__ebs_;
298 __intbuf_ = __rhs.__intbuf_;
299 __ibs_ = __rhs.__ibs_;
300 __file_ = __rhs.__file_;
303 __st_last_ = __rhs.__st_last_;
306 __owns_eb_ = __rhs.__owns_eb_;
307 __owns_ib_ = __rhs.__owns_ib_;
308 __always_noconv_ = __rhs.__always_noconv_;
311 if (__rhs.pbase() == __rhs.__intbuf_)
312 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
314 this->setp((char_type*)__extbuf_,
315 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
316 this->pbump(__rhs. pptr() - __rhs.pbase());
318 else if (__rhs.eback())
320 if (__rhs.eback() == __rhs.__intbuf_)
321 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
322 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
324 this->setg((char_type*)__extbuf_,
325 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
326 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
329 __rhs.__extbufnext_ = 0;
330 __rhs.__extbufend_ = 0;
335 __rhs.__st_ = state_type();
336 __rhs.__st_last_ = state_type();
339 __rhs.__owns_eb_ = false;
340 __rhs.__owns_ib_ = false;
345 template <class _CharT, class _Traits>
347 basic_filebuf<_CharT, _Traits>&
348 basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
355 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
357 template <class _CharT, class _Traits>
358 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
360 #ifndef _LIBCPP_NO_EXCEPTIONS
363 #endif // _LIBCPP_NO_EXCEPTIONS
365 #ifndef _LIBCPP_NO_EXCEPTIONS
370 #endif // _LIBCPP_NO_EXCEPTIONS
377 template <class _CharT, class _Traits>
379 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
381 basic_streambuf<char_type, traits_type>::swap(__rhs);
382 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
384 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
385 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
386 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
390 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
391 ptrdiff_t __le = __extbufend_ - __extbuf_;
392 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
393 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
394 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
396 __extbuf_ = __rhs.__extbuf_;
397 __rhs.__extbuf_ = __rhs.__extbuf_min_;
399 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
401 __rhs.__extbuf_ = __extbuf_;
402 __extbuf_ = __extbuf_min_;
404 __extbufnext_ = __extbuf_ + __rn;
405 __extbufend_ = __extbuf_ + __re;
406 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
407 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
409 _VSTD::swap(__ebs_, __rhs.__ebs_);
410 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
411 _VSTD::swap(__ibs_, __rhs.__ibs_);
412 _VSTD::swap(__file_, __rhs.__file_);
413 _VSTD::swap(__cv_, __rhs.__cv_);
414 _VSTD::swap(__st_, __rhs.__st_);
415 _VSTD::swap(__st_last_, __rhs.__st_last_);
416 _VSTD::swap(__om_, __rhs.__om_);
417 _VSTD::swap(__cm_, __rhs.__cm_);
418 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
419 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
420 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
421 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
423 ptrdiff_t __n = this->gptr() - this->eback();
424 ptrdiff_t __e = this->egptr() - this->eback();
425 this->setg((char_type*)__extbuf_min_,
426 (char_type*)__extbuf_min_ + __n,
427 (char_type*)__extbuf_min_ + __e);
429 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
431 ptrdiff_t __n = this->pptr() - this->pbase();
432 ptrdiff_t __e = this->epptr() - this->pbase();
433 this->setp((char_type*)__extbuf_min_,
434 (char_type*)__extbuf_min_ + __e);
437 if (__rhs.eback() == (char_type*)__extbuf_min_)
439 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
440 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
441 __rhs.setg((char_type*)__rhs.__extbuf_min_,
442 (char_type*)__rhs.__extbuf_min_ + __n,
443 (char_type*)__rhs.__extbuf_min_ + __e);
445 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
447 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
448 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
449 __rhs.setp((char_type*)__rhs.__extbuf_min_,
450 (char_type*)__rhs.__extbuf_min_ + __e);
455 template <class _CharT, class _Traits>
456 inline _LIBCPP_INLINE_VISIBILITY
458 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
463 template <class _CharT, class _Traits>
466 basic_filebuf<_CharT, _Traits>::is_open() const
471 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
472 template <class _CharT, class _Traits>
473 basic_filebuf<_CharT, _Traits>*
474 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
476 basic_filebuf<_CharT, _Traits>* __rt = 0;
481 switch (__mode & ~ios_base::ate)
484 case ios_base::out | ios_base::trunc:
487 case ios_base::out | ios_base::app:
494 case ios_base::in | ios_base::out:
497 case ios_base::in | ios_base::out | ios_base::trunc:
500 case ios_base::in | ios_base::out | ios_base::app:
501 case ios_base::in | ios_base::app:
504 case ios_base::out | ios_base::binary:
505 case ios_base::out | ios_base::trunc | ios_base::binary:
508 case ios_base::out | ios_base::app | ios_base::binary:
509 case ios_base::app | ios_base::binary:
512 case ios_base::in | ios_base::binary:
515 case ios_base::in | ios_base::out | ios_base::binary:
518 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
521 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
522 case ios_base::in | ios_base::app | ios_base::binary:
531 __file_ = fopen(__s, __mdstr);
535 if (__mode & ios_base::ate)
537 if (fseek(__file_, 0, SEEK_END))
552 template <class _CharT, class _Traits>
554 basic_filebuf<_CharT, _Traits>*
555 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
557 return open(__s.c_str(), __mode);
561 template <class _CharT, class _Traits>
562 basic_filebuf<_CharT, _Traits>*
563 basic_filebuf<_CharT, _Traits>::close()
565 basic_filebuf<_CharT, _Traits>* __rt = 0;
569 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
572 if (fclose(__h.release()) == 0)
580 template <class _CharT, class _Traits>
581 typename basic_filebuf<_CharT, _Traits>::int_type
582 basic_filebuf<_CharT, _Traits>::underflow()
585 return traits_type::eof();
586 bool __initial = __read_mode();
588 if (this->gptr() == 0)
589 this->setg(&__1buf, &__1buf+1, &__1buf+1);
590 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
591 int_type __c = traits_type::eof();
592 if (this->gptr() == this->egptr())
594 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
595 if (__always_noconv_)
597 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
598 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
601 this->setg(this->eback(),
602 this->eback() + __unget_sz,
603 this->eback() + __unget_sz + __nmemb);
604 __c = traits_type::to_int_type(*this->gptr());
609 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
610 if (__extbufend_ != __extbufnext_)
611 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
612 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
613 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
614 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
615 static_cast<size_t>(__extbufend_ - __extbufnext_));
616 codecvt_base::result __r;
618 size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
624 __extbufend_ = __extbufnext_ + __nr;
626 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
627 this->eback() + __unget_sz,
628 this->eback() + __ibs_, __inext);
629 if (__r == codecvt_base::noconv)
631 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
632 __c = traits_type::to_int_type(*this->gptr());
634 else if (__inext != this->eback() + __unget_sz)
636 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
637 __c = traits_type::to_int_type(*this->gptr());
643 __c = traits_type::to_int_type(*this->gptr());
644 if (this->eback() == &__1buf)
649 template <class _CharT, class _Traits>
650 typename basic_filebuf<_CharT, _Traits>::int_type
651 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
653 if (__file_ && this->eback() < this->gptr())
655 if (traits_type::eq_int_type(__c, traits_type::eof()))
658 return traits_type::not_eof(__c);
660 if ((__om_ & ios_base::out) ||
661 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
664 *this->gptr() = traits_type::to_char_type(__c);
668 return traits_type::eof();
671 template <class _CharT, class _Traits>
672 typename basic_filebuf<_CharT, _Traits>::int_type
673 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
676 return traits_type::eof();
679 char_type* __pb_save = this->pbase();
680 char_type* __epb_save = this->epptr();
681 if (!traits_type::eq_int_type(__c, traits_type::eof()))
683 if (this->pptr() == 0)
684 this->setp(&__1buf, &__1buf+1);
685 *this->pptr() = traits_type::to_char_type(__c);
688 if (this->pptr() != this->pbase())
690 if (__always_noconv_)
692 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
693 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
694 return traits_type::eof();
698 char* __extbe = __extbuf_;
699 codecvt_base::result __r;
705 const char_type* __e;
706 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
707 __extbuf_, __extbuf_ + __ebs_, __extbe);
708 if (__e == this->pbase())
709 return traits_type::eof();
710 if (__r == codecvt_base::noconv)
712 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
713 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
714 return traits_type::eof();
716 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
718 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
719 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
720 return traits_type::eof();
721 if (__r == codecvt_base::partial)
723 this->setp((char_type*)__e, this->pptr());
724 this->pbump(this->epptr() - this->pbase());
728 return traits_type::eof();
729 } while (__r == codecvt_base::partial);
731 this->setp(__pb_save, __epb_save);
733 return traits_type::not_eof(__c);
736 template <class _CharT, class _Traits>
737 basic_streambuf<_CharT, _Traits>*
738 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
747 if (__ebs_ > sizeof(__extbuf_min_))
749 if (__always_noconv_ && __s)
751 __extbuf_ = (char*)__s;
756 __extbuf_ = new char[__ebs_];
762 __extbuf_ = __extbuf_min_;
763 __ebs_ = sizeof(__extbuf_min_);
766 if (!__always_noconv_)
768 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
769 if (__s && __ibs_ >= sizeof(__extbuf_min_))
776 __intbuf_ = new char_type[__ibs_];
789 template <class _CharT, class _Traits>
790 typename basic_filebuf<_CharT, _Traits>::pos_type
791 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
797 int __width = __cv_->encoding();
798 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
799 return pos_type(off_type(-1));
800 // __width > 0 || __off == 0
814 return pos_type(off_type(-1));
816 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
817 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
818 return pos_type(off_type(-1));
819 pos_type __r = ftell(__file_);
821 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
822 return pos_type(off_type(-1));
823 pos_type __r = ftello(__file_);
829 template <class _CharT, class _Traits>
830 typename basic_filebuf<_CharT, _Traits>::pos_type
831 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
833 if (__file_ == 0 || sync())
834 return pos_type(off_type(-1));
835 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
836 if (fseek(__file_, __sp, SEEK_SET))
837 return pos_type(off_type(-1));
839 if (fseeko(__file_, __sp, SEEK_SET))
840 return pos_type(off_type(-1));
842 __st_ = __sp.state();
846 template <class _CharT, class _Traits>
848 basic_filebuf<_CharT, _Traits>::sync()
855 if (__cm_ & ios_base::out)
857 if (this->pptr() != this->pbase())
858 if (overflow() == traits_type::eof())
860 codecvt_base::result __r;
864 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
865 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
866 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
868 } while (__r == codecvt_base::partial);
869 if (__r == codecvt_base::error)
874 else if (__cm_ & ios_base::in)
877 state_type __state = __st_last_;
878 bool __update_st = false;
879 if (__always_noconv_)
880 __c = this->egptr() - this->gptr();
883 int __width = __cv_->encoding();
884 __c = __extbufend_ - __extbufnext_;
886 __c += __width * (this->egptr() - this->gptr());
889 if (this->gptr() != this->egptr())
891 const int __off = __cv_->length(__state, __extbuf_,
893 this->gptr() - this->eback());
894 __c += __extbufnext_ - __extbuf_ - __off;
899 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
900 if (fseek(__file_, -__c, SEEK_CUR))
903 if (fseeko(__file_, -__c, SEEK_CUR))
908 __extbufnext_ = __extbufend_ = __extbuf_;
915 template <class _CharT, class _Traits>
917 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
920 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
921 bool __old_anc = __always_noconv_;
922 __always_noconv_ = __cv_->always_noconv();
923 if (__old_anc != __always_noconv_)
927 // invariant, char_type is char, else we couldn't get here
928 if (__always_noconv_) // need to dump __intbuf_
932 __owns_eb_ = __owns_ib_;
934 __extbuf_ = (char*)__intbuf_;
939 else // need to obtain an __intbuf_.
940 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
941 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
944 __intbuf_ = (char_type*)__extbuf_;
946 __extbuf_ = new char[__ebs_];
952 __intbuf_ = new char_type[__ibs_];
959 template <class _CharT, class _Traits>
961 basic_filebuf<_CharT, _Traits>::__read_mode()
963 if (!(__cm_ & ios_base::in))
966 if (__always_noconv_)
967 this->setg((char_type*)__extbuf_,
968 (char_type*)__extbuf_ + __ebs_,
969 (char_type*)__extbuf_ + __ebs_);
971 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
972 __cm_ = ios_base::in;
978 template <class _CharT, class _Traits>
980 basic_filebuf<_CharT, _Traits>::__write_mode()
982 if (!(__cm_ & ios_base::out))
985 if (__ebs_ > sizeof(__extbuf_min_))
987 if (__always_noconv_)
988 this->setp((char_type*)__extbuf_,
989 (char_type*)__extbuf_ + (__ebs_ - 1));
991 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
995 __cm_ = ios_base::out;
1001 template <class _CharT, class _Traits>
1002 class _LIBCPP_TEMPLATE_VIS basic_ifstream
1003 : public basic_istream<_CharT, _Traits>
1006 typedef _CharT char_type;
1007 typedef _Traits traits_type;
1008 typedef typename traits_type::int_type int_type;
1009 typedef typename traits_type::pos_type pos_type;
1010 typedef typename traits_type::off_type off_type;
1012 _LIBCPP_INLINE_VISIBILITY
1014 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1015 _LIBCPP_INLINE_VISIBILITY
1016 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1017 _LIBCPP_INLINE_VISIBILITY
1018 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1020 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1021 _LIBCPP_INLINE_VISIBILITY
1022 basic_ifstream(basic_ifstream&& __rhs);
1025 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1026 _LIBCPP_INLINE_VISIBILITY
1027 basic_ifstream& operator=(basic_ifstream&& __rhs);
1029 _LIBCPP_INLINE_VISIBILITY
1030 void swap(basic_ifstream& __rhs);
1032 _LIBCPP_INLINE_VISIBILITY
1033 basic_filebuf<char_type, traits_type>* rdbuf() const;
1034 _LIBCPP_INLINE_VISIBILITY
1035 bool is_open() const;
1036 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1037 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1038 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1040 _LIBCPP_INLINE_VISIBILITY
1044 basic_filebuf<char_type, traits_type> __sb_;
1047 template <class _CharT, class _Traits>
1049 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1050 : basic_istream<char_type, traits_type>(&__sb_)
1054 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1055 template <class _CharT, class _Traits>
1057 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1058 : basic_istream<char_type, traits_type>(&__sb_)
1060 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1061 this->setstate(ios_base::failbit);
1064 template <class _CharT, class _Traits>
1066 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1067 : basic_istream<char_type, traits_type>(&__sb_)
1069 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1070 this->setstate(ios_base::failbit);
1074 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1076 template <class _CharT, class _Traits>
1078 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1079 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1080 __sb_(_VSTD::move(__rhs.__sb_))
1082 this->set_rdbuf(&__sb_);
1085 template <class _CharT, class _Traits>
1087 basic_ifstream<_CharT, _Traits>&
1088 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1090 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1091 __sb_ = _VSTD::move(__rhs.__sb_);
1095 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1097 template <class _CharT, class _Traits>
1100 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1102 basic_istream<char_type, traits_type>::swap(__rhs);
1103 __sb_.swap(__rhs.__sb_);
1106 template <class _CharT, class _Traits>
1107 inline _LIBCPP_INLINE_VISIBILITY
1109 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1114 template <class _CharT, class _Traits>
1116 basic_filebuf<_CharT, _Traits>*
1117 basic_ifstream<_CharT, _Traits>::rdbuf() const
1119 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1122 template <class _CharT, class _Traits>
1125 basic_ifstream<_CharT, _Traits>::is_open() const
1127 return __sb_.is_open();
1130 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1131 template <class _CharT, class _Traits>
1133 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1135 if (__sb_.open(__s, __mode | ios_base::in))
1138 this->setstate(ios_base::failbit);
1141 template <class _CharT, class _Traits>
1143 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1145 if (__sb_.open(__s, __mode | ios_base::in))
1148 this->setstate(ios_base::failbit);
1152 template <class _CharT, class _Traits>
1155 basic_ifstream<_CharT, _Traits>::close()
1157 if (__sb_.close() == 0)
1158 this->setstate(ios_base::failbit);
1163 template <class _CharT, class _Traits>
1164 class _LIBCPP_TEMPLATE_VIS basic_ofstream
1165 : public basic_ostream<_CharT, _Traits>
1168 typedef _CharT char_type;
1169 typedef _Traits traits_type;
1170 typedef typename traits_type::int_type int_type;
1171 typedef typename traits_type::pos_type pos_type;
1172 typedef typename traits_type::off_type off_type;
1174 _LIBCPP_INLINE_VISIBILITY
1176 _LIBCPP_INLINE_VISIBILITY
1177 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1178 _LIBCPP_INLINE_VISIBILITY
1179 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1180 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1181 _LIBCPP_INLINE_VISIBILITY
1182 basic_ofstream(basic_ofstream&& __rhs);
1185 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1186 _LIBCPP_INLINE_VISIBILITY
1187 basic_ofstream& operator=(basic_ofstream&& __rhs);
1189 _LIBCPP_INLINE_VISIBILITY
1190 void swap(basic_ofstream& __rhs);
1192 _LIBCPP_INLINE_VISIBILITY
1193 basic_filebuf<char_type, traits_type>* rdbuf() const;
1194 _LIBCPP_INLINE_VISIBILITY
1195 bool is_open() const;
1196 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1197 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1198 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1200 _LIBCPP_INLINE_VISIBILITY
1204 basic_filebuf<char_type, traits_type> __sb_;
1207 template <class _CharT, class _Traits>
1209 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1210 : basic_ostream<char_type, traits_type>(&__sb_)
1214 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1215 template <class _CharT, class _Traits>
1217 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1218 : basic_ostream<char_type, traits_type>(&__sb_)
1220 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1221 this->setstate(ios_base::failbit);
1224 template <class _CharT, class _Traits>
1226 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1227 : basic_ostream<char_type, traits_type>(&__sb_)
1229 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1230 this->setstate(ios_base::failbit);
1234 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1236 template <class _CharT, class _Traits>
1238 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1239 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1240 __sb_(_VSTD::move(__rhs.__sb_))
1242 this->set_rdbuf(&__sb_);
1245 template <class _CharT, class _Traits>
1247 basic_ofstream<_CharT, _Traits>&
1248 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1250 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1251 __sb_ = _VSTD::move(__rhs.__sb_);
1255 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1257 template <class _CharT, class _Traits>
1260 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1262 basic_ostream<char_type, traits_type>::swap(__rhs);
1263 __sb_.swap(__rhs.__sb_);
1266 template <class _CharT, class _Traits>
1267 inline _LIBCPP_INLINE_VISIBILITY
1269 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1274 template <class _CharT, class _Traits>
1276 basic_filebuf<_CharT, _Traits>*
1277 basic_ofstream<_CharT, _Traits>::rdbuf() const
1279 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1282 template <class _CharT, class _Traits>
1285 basic_ofstream<_CharT, _Traits>::is_open() const
1287 return __sb_.is_open();
1290 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1291 template <class _CharT, class _Traits>
1293 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1295 if (__sb_.open(__s, __mode | ios_base::out))
1298 this->setstate(ios_base::failbit);
1301 template <class _CharT, class _Traits>
1303 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1305 if (__sb_.open(__s, __mode | ios_base::out))
1308 this->setstate(ios_base::failbit);
1312 template <class _CharT, class _Traits>
1315 basic_ofstream<_CharT, _Traits>::close()
1317 if (__sb_.close() == 0)
1318 this->setstate(ios_base::failbit);
1323 template <class _CharT, class _Traits>
1324 class _LIBCPP_TEMPLATE_VIS basic_fstream
1325 : public basic_iostream<_CharT, _Traits>
1328 typedef _CharT char_type;
1329 typedef _Traits traits_type;
1330 typedef typename traits_type::int_type int_type;
1331 typedef typename traits_type::pos_type pos_type;
1332 typedef typename traits_type::off_type off_type;
1334 _LIBCPP_INLINE_VISIBILITY
1336 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1337 _LIBCPP_INLINE_VISIBILITY
1338 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1339 _LIBCPP_INLINE_VISIBILITY
1340 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1342 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1343 _LIBCPP_INLINE_VISIBILITY
1344 basic_fstream(basic_fstream&& __rhs);
1347 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1348 _LIBCPP_INLINE_VISIBILITY
1349 basic_fstream& operator=(basic_fstream&& __rhs);
1351 _LIBCPP_INLINE_VISIBILITY
1352 void swap(basic_fstream& __rhs);
1354 _LIBCPP_INLINE_VISIBILITY
1355 basic_filebuf<char_type, traits_type>* rdbuf() const;
1356 _LIBCPP_INLINE_VISIBILITY
1357 bool is_open() const;
1358 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1359 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1360 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1362 _LIBCPP_INLINE_VISIBILITY
1366 basic_filebuf<char_type, traits_type> __sb_;
1369 template <class _CharT, class _Traits>
1371 basic_fstream<_CharT, _Traits>::basic_fstream()
1372 : basic_iostream<char_type, traits_type>(&__sb_)
1376 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1377 template <class _CharT, class _Traits>
1379 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1380 : basic_iostream<char_type, traits_type>(&__sb_)
1382 if (__sb_.open(__s, __mode) == 0)
1383 this->setstate(ios_base::failbit);
1386 template <class _CharT, class _Traits>
1388 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1389 : basic_iostream<char_type, traits_type>(&__sb_)
1391 if (__sb_.open(__s, __mode) == 0)
1392 this->setstate(ios_base::failbit);
1396 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1398 template <class _CharT, class _Traits>
1400 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1401 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1402 __sb_(_VSTD::move(__rhs.__sb_))
1404 this->set_rdbuf(&__sb_);
1407 template <class _CharT, class _Traits>
1409 basic_fstream<_CharT, _Traits>&
1410 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1412 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1413 __sb_ = _VSTD::move(__rhs.__sb_);
1417 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1419 template <class _CharT, class _Traits>
1422 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1424 basic_iostream<char_type, traits_type>::swap(__rhs);
1425 __sb_.swap(__rhs.__sb_);
1428 template <class _CharT, class _Traits>
1429 inline _LIBCPP_INLINE_VISIBILITY
1431 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1436 template <class _CharT, class _Traits>
1438 basic_filebuf<_CharT, _Traits>*
1439 basic_fstream<_CharT, _Traits>::rdbuf() const
1441 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1444 template <class _CharT, class _Traits>
1447 basic_fstream<_CharT, _Traits>::is_open() const
1449 return __sb_.is_open();
1452 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1453 template <class _CharT, class _Traits>
1455 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1457 if (__sb_.open(__s, __mode))
1460 this->setstate(ios_base::failbit);
1463 template <class _CharT, class _Traits>
1465 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1467 if (__sb_.open(__s, __mode))
1470 this->setstate(ios_base::failbit);
1474 template <class _CharT, class _Traits>
1477 basic_fstream<_CharT, _Traits>::close()
1479 if (__sb_.close() == 0)
1480 this->setstate(ios_base::failbit);
1483 _LIBCPP_END_NAMESPACE_STD
1485 #endif // _LIBCPP_FSTREAM