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_ONLY 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 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
210 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
211 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
213 basic_filebuf* close();
216 // 27.9.1.5 Overridden virtual functions:
217 virtual int_type underflow();
218 virtual int_type pbackfail(int_type __c = traits_type::eof());
219 virtual int_type overflow (int_type __c = traits_type::eof());
220 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
221 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
222 ios_base::openmode __wch = ios_base::in | ios_base::out);
223 virtual pos_type seekpos(pos_type __sp,
224 ios_base::openmode __wch = ios_base::in | ios_base::out);
226 virtual void imbue(const locale& __loc);
230 const char* __extbufnext_;
231 const char* __extbufend_;
232 char __extbuf_min_[8];
234 char_type* __intbuf_;
237 const codecvt<char_type, char, state_type>* __cv_;
239 state_type __st_last_;
240 ios_base::openmode __om_;
241 ios_base::openmode __cm_;
244 bool __always_noconv_;
250 template <class _CharT, class _Traits>
251 basic_filebuf<_CharT, _Traits>::basic_filebuf()
266 __always_noconv_(false)
268 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
270 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
271 __always_noconv_ = __cv_->always_noconv();
276 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
278 template <class _CharT, class _Traits>
279 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
280 : basic_streambuf<_CharT, _Traits>(__rhs)
282 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
284 __extbuf_ = __extbuf_min_;
285 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
286 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
290 __extbuf_ = __rhs.__extbuf_;
291 __extbufnext_ = __rhs.__extbufnext_;
292 __extbufend_ = __rhs.__extbufend_;
294 __ebs_ = __rhs.__ebs_;
295 __intbuf_ = __rhs.__intbuf_;
296 __ibs_ = __rhs.__ibs_;
297 __file_ = __rhs.__file_;
300 __st_last_ = __rhs.__st_last_;
303 __owns_eb_ = __rhs.__owns_eb_;
304 __owns_ib_ = __rhs.__owns_ib_;
305 __always_noconv_ = __rhs.__always_noconv_;
308 if (__rhs.pbase() == __rhs.__intbuf_)
309 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
311 this->setp((char_type*)__extbuf_,
312 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
313 this->pbump(__rhs. pptr() - __rhs.pbase());
315 else if (__rhs.eback())
317 if (__rhs.eback() == __rhs.__intbuf_)
318 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
319 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
321 this->setg((char_type*)__extbuf_,
322 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
323 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
326 __rhs.__extbufnext_ = 0;
327 __rhs.__extbufend_ = 0;
332 __rhs.__st_ = state_type();
333 __rhs.__st_last_ = state_type();
336 __rhs.__owns_eb_ = false;
337 __rhs.__owns_ib_ = false;
342 template <class _CharT, class _Traits>
343 inline _LIBCPP_INLINE_VISIBILITY
344 basic_filebuf<_CharT, _Traits>&
345 basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
352 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
354 template <class _CharT, class _Traits>
355 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
357 #ifndef _LIBCPP_NO_EXCEPTIONS
360 #endif // _LIBCPP_NO_EXCEPTIONS
362 #ifndef _LIBCPP_NO_EXCEPTIONS
367 #endif // _LIBCPP_NO_EXCEPTIONS
374 template <class _CharT, class _Traits>
376 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
378 basic_streambuf<char_type, traits_type>::swap(__rhs);
379 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
381 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
382 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
383 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
387 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
388 ptrdiff_t __le = __extbufend_ - __extbuf_;
389 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
390 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
391 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
393 __extbuf_ = __rhs.__extbuf_;
394 __rhs.__extbuf_ = __rhs.__extbuf_min_;
396 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
398 __rhs.__extbuf_ = __extbuf_;
399 __extbuf_ = __extbuf_min_;
401 __extbufnext_ = __extbuf_ + __rn;
402 __extbufend_ = __extbuf_ + __re;
403 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
404 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
406 _VSTD::swap(__ebs_, __rhs.__ebs_);
407 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
408 _VSTD::swap(__ibs_, __rhs.__ibs_);
409 _VSTD::swap(__file_, __rhs.__file_);
410 _VSTD::swap(__cv_, __rhs.__cv_);
411 _VSTD::swap(__st_, __rhs.__st_);
412 _VSTD::swap(__st_last_, __rhs.__st_last_);
413 _VSTD::swap(__om_, __rhs.__om_);
414 _VSTD::swap(__cm_, __rhs.__cm_);
415 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
416 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
417 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
418 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
420 ptrdiff_t __n = this->gptr() - this->eback();
421 ptrdiff_t __e = this->egptr() - this->eback();
422 this->setg((char_type*)__extbuf_min_,
423 (char_type*)__extbuf_min_ + __n,
424 (char_type*)__extbuf_min_ + __e);
426 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
428 ptrdiff_t __n = this->pptr() - this->pbase();
429 ptrdiff_t __e = this->epptr() - this->pbase();
430 this->setp((char_type*)__extbuf_min_,
431 (char_type*)__extbuf_min_ + __e);
434 if (__rhs.eback() == (char_type*)__extbuf_min_)
436 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
437 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
438 __rhs.setg((char_type*)__rhs.__extbuf_min_,
439 (char_type*)__rhs.__extbuf_min_ + __n,
440 (char_type*)__rhs.__extbuf_min_ + __e);
442 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
444 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
445 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
446 __rhs.setp((char_type*)__rhs.__extbuf_min_,
447 (char_type*)__rhs.__extbuf_min_ + __e);
452 template <class _CharT, class _Traits>
453 inline _LIBCPP_INLINE_VISIBILITY
455 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
460 template <class _CharT, class _Traits>
461 inline _LIBCPP_INLINE_VISIBILITY
463 basic_filebuf<_CharT, _Traits>::is_open() const
468 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
469 template <class _CharT, class _Traits>
470 basic_filebuf<_CharT, _Traits>*
471 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
473 basic_filebuf<_CharT, _Traits>* __rt = 0;
478 switch (__mode & ~ios_base::ate)
481 case ios_base::out | ios_base::trunc:
484 case ios_base::out | ios_base::app:
491 case ios_base::in | ios_base::out:
494 case ios_base::in | ios_base::out | ios_base::trunc:
497 case ios_base::in | ios_base::out | ios_base::app:
498 case ios_base::in | ios_base::app:
501 case ios_base::out | ios_base::binary:
502 case ios_base::out | ios_base::trunc | ios_base::binary:
505 case ios_base::out | ios_base::app | ios_base::binary:
506 case ios_base::app | ios_base::binary:
509 case ios_base::in | ios_base::binary:
512 case ios_base::in | ios_base::out | ios_base::binary:
515 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
518 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
519 case ios_base::in | ios_base::app | ios_base::binary:
528 __file_ = fopen(__s, __mdstr);
532 if (__mode & ios_base::ate)
534 if (fseek(__file_, 0, SEEK_END))
549 template <class _CharT, class _Traits>
550 inline _LIBCPP_INLINE_VISIBILITY
551 basic_filebuf<_CharT, _Traits>*
552 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
554 return open(__s.c_str(), __mode);
558 template <class _CharT, class _Traits>
559 basic_filebuf<_CharT, _Traits>*
560 basic_filebuf<_CharT, _Traits>::close()
562 basic_filebuf<_CharT, _Traits>* __rt = 0;
566 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
569 if (fclose(__h.release()) == 0)
577 template <class _CharT, class _Traits>
578 typename basic_filebuf<_CharT, _Traits>::int_type
579 basic_filebuf<_CharT, _Traits>::underflow()
582 return traits_type::eof();
583 bool __initial = __read_mode();
585 if (this->gptr() == 0)
586 this->setg(&__1buf, &__1buf+1, &__1buf+1);
587 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
588 int_type __c = traits_type::eof();
589 if (this->gptr() == this->egptr())
591 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
592 if (__always_noconv_)
594 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
595 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
598 this->setg(this->eback(),
599 this->eback() + __unget_sz,
600 this->eback() + __unget_sz + __nmemb);
601 __c = traits_type::to_int_type(*this->gptr());
606 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
607 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
608 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
609 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
610 static_cast<size_t>(__extbufend_ - __extbufnext_));
611 codecvt_base::result __r;
613 size_t __nr = fread((void*)__extbufnext_, 1, __nmemb, __file_);
616 #ifndef _LIBCPP_NO_EXCEPTIONS
620 __extbufend_ = __extbufnext_ + __nr;
622 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
623 this->eback() + __unget_sz,
624 this->eback() + __ibs_, __inext);
625 if (__r == codecvt_base::noconv)
627 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_, (char_type*)__extbufend_);
628 __c = traits_type::to_int_type(*this->gptr());
630 else if (__inext != this->eback() + __unget_sz)
632 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
633 __c = traits_type::to_int_type(*this->gptr());
639 __c = traits_type::to_int_type(*this->gptr());
640 if (this->eback() == &__1buf)
645 template <class _CharT, class _Traits>
646 typename basic_filebuf<_CharT, _Traits>::int_type
647 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
649 if (__file_ && this->eback() < this->gptr())
651 if (traits_type::eq_int_type(__c, traits_type::eof()))
654 return traits_type::not_eof(__c);
656 if ((__om_ & ios_base::out) ||
657 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
660 *this->gptr() = traits_type::to_char_type(__c);
664 return traits_type::eof();
667 template <class _CharT, class _Traits>
668 typename basic_filebuf<_CharT, _Traits>::int_type
669 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
672 return traits_type::eof();
675 char_type* __pb_save = this->pbase();
676 char_type* __epb_save = this->epptr();
677 if (!traits_type::eq_int_type(__c, traits_type::eof()))
679 if (this->pptr() == 0)
680 this->setp(&__1buf, &__1buf+1);
681 *this->pptr() = traits_type::to_char_type(__c);
684 if (this->pptr() != this->pbase())
686 if (__always_noconv_)
688 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
689 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
690 return traits_type::eof();
694 char* __extbe = __extbuf_;
695 codecvt_base::result __r;
698 #ifndef _LIBCPP_NO_EXCEPTIONS
702 const char_type* __e;
703 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
704 __extbuf_, __extbuf_ + __ebs_, __extbe);
705 if (__e == this->pbase())
706 return traits_type::eof();
707 if (__r == codecvt_base::noconv)
709 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
710 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
711 return traits_type::eof();
713 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
715 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
716 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
717 return traits_type::eof();
718 if (__r == codecvt_base::partial)
720 this->setp((char_type*)__e, this->pptr());
721 this->pbump(this->epptr() - this->pbase());
725 return traits_type::eof();
726 } while (__r == codecvt_base::partial);
728 this->setp(__pb_save, __epb_save);
730 return traits_type::not_eof(__c);
733 template <class _CharT, class _Traits>
734 basic_streambuf<_CharT, _Traits>*
735 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
744 if (__ebs_ > sizeof(__extbuf_min_))
746 if (__always_noconv_ && __s)
748 __extbuf_ = (char*)__s;
753 __extbuf_ = new char[__ebs_];
759 __extbuf_ = __extbuf_min_;
760 __ebs_ = sizeof(__extbuf_min_);
763 if (!__always_noconv_)
765 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
766 if (__s && __ibs_ >= sizeof(__extbuf_min_))
773 __intbuf_ = new char_type[__ibs_];
786 template <class _CharT, class _Traits>
787 typename basic_filebuf<_CharT, _Traits>::pos_type
788 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
791 #ifndef _LIBCPP_NO_EXCEPTIONS
795 int __width = __cv_->encoding();
796 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
797 return pos_type(off_type(-1));
798 // __width > 0 || __off == 0
812 return pos_type(off_type(-1));
814 #if defined(_WIN32) || defined(_NEWLIB_VERSION)
815 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
816 return pos_type(off_type(-1));
817 pos_type __r = ftell(__file_);
819 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
820 return pos_type(off_type(-1));
821 pos_type __r = ftello(__file_);
827 template <class _CharT, class _Traits>
828 typename basic_filebuf<_CharT, _Traits>::pos_type
829 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
831 if (__file_ == 0 || sync())
832 return pos_type(off_type(-1));
833 #if defined(_WIN32) || defined(_NEWLIB_VERSION)
834 if (fseek(__file_, __sp, SEEK_SET))
835 return pos_type(off_type(-1));
837 if (fseeko(__file_, __sp, SEEK_SET))
838 return pos_type(off_type(-1));
840 __st_ = __sp.state();
844 template <class _CharT, class _Traits>
846 basic_filebuf<_CharT, _Traits>::sync()
850 #ifndef _LIBCPP_NO_EXCEPTIONS
854 if (__cm_ & ios_base::out)
856 if (this->pptr() != this->pbase())
857 if (overflow() == traits_type::eof())
859 codecvt_base::result __r;
863 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
864 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
865 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
867 } while (__r == codecvt_base::partial);
868 if (__r == codecvt_base::error)
873 else if (__cm_ & ios_base::in)
876 state_type __state = __st_last_;
877 bool __update_st = false;
878 if (__always_noconv_)
879 __c = this->egptr() - this->gptr();
882 int __width = __cv_->encoding();
883 __c = __extbufend_ - __extbufnext_;
885 __c += __width * (this->egptr() - this->gptr());
888 if (this->gptr() != this->egptr())
890 const int __off = __cv_->length(__state, __extbuf_,
892 this->gptr() - this->eback());
893 __c += __extbufnext_ - __extbuf_ - __off;
898 #if defined(_WIN32) || defined(_NEWLIB_VERSION)
899 if (fseek(__file_, -__c, SEEK_CUR))
902 if (fseeko(__file_, -__c, SEEK_CUR))
907 __extbufnext_ = __extbufend_ = __extbuf_;
914 template <class _CharT, class _Traits>
916 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
919 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
920 bool __old_anc = __always_noconv_;
921 __always_noconv_ = __cv_->always_noconv();
922 if (__old_anc != __always_noconv_)
926 // invariant, char_type is char, else we couldn't get here
927 if (__always_noconv_) // need to dump __intbuf_
931 __owns_eb_ = __owns_ib_;
933 __extbuf_ = (char*)__intbuf_;
938 else // need to obtain an __intbuf_.
939 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
940 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
943 __intbuf_ = (char_type*)__extbuf_;
945 __extbuf_ = new char[__ebs_];
951 __intbuf_ = new char_type[__ibs_];
958 template <class _CharT, class _Traits>
960 basic_filebuf<_CharT, _Traits>::__read_mode()
962 if (!(__cm_ & ios_base::in))
965 if (__always_noconv_)
966 this->setg((char_type*)__extbuf_,
967 (char_type*)__extbuf_ + __ebs_,
968 (char_type*)__extbuf_ + __ebs_);
970 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
971 __cm_ = ios_base::in;
977 template <class _CharT, class _Traits>
979 basic_filebuf<_CharT, _Traits>::__write_mode()
981 if (!(__cm_ & ios_base::out))
984 if (__ebs_ > sizeof(__extbuf_min_))
986 if (__always_noconv_)
987 this->setp((char_type*)__extbuf_,
988 (char_type*)__extbuf_ + (__ebs_ - 1));
990 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
994 __cm_ = ios_base::out;
1000 template <class _CharT, class _Traits>
1001 class _LIBCPP_TYPE_VIS_ONLY basic_ifstream
1002 : public basic_istream<_CharT, _Traits>
1005 typedef _CharT char_type;
1006 typedef _Traits traits_type;
1007 typedef typename traits_type::int_type int_type;
1008 typedef typename traits_type::pos_type pos_type;
1009 typedef typename traits_type::off_type off_type;
1012 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1013 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1014 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1016 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1017 basic_ifstream(basic_ifstream&& __rhs);
1020 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1021 basic_ifstream& operator=(basic_ifstream&& __rhs);
1023 void swap(basic_ifstream& __rhs);
1025 basic_filebuf<char_type, traits_type>* rdbuf() const;
1026 bool is_open() const;
1027 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1028 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1029 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1034 basic_filebuf<char_type, traits_type> __sb_;
1037 template <class _CharT, class _Traits>
1038 inline _LIBCPP_INLINE_VISIBILITY
1039 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1040 : basic_istream<char_type, traits_type>(&__sb_)
1044 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1045 template <class _CharT, class _Traits>
1046 inline _LIBCPP_INLINE_VISIBILITY
1047 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __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 template <class _CharT, class _Traits>
1055 inline _LIBCPP_INLINE_VISIBILITY
1056 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1057 : basic_istream<char_type, traits_type>(&__sb_)
1059 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1060 this->setstate(ios_base::failbit);
1064 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1066 template <class _CharT, class _Traits>
1067 inline _LIBCPP_INLINE_VISIBILITY
1068 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1069 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1070 __sb_(_VSTD::move(__rhs.__sb_))
1072 this->set_rdbuf(&__sb_);
1075 template <class _CharT, class _Traits>
1076 inline _LIBCPP_INLINE_VISIBILITY
1077 basic_ifstream<_CharT, _Traits>&
1078 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1080 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1081 __sb_ = _VSTD::move(__rhs.__sb_);
1085 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1087 template <class _CharT, class _Traits>
1088 inline _LIBCPP_INLINE_VISIBILITY
1090 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1092 basic_istream<char_type, traits_type>::swap(__rhs);
1093 __sb_.swap(__rhs.__sb_);
1096 template <class _CharT, class _Traits>
1097 inline _LIBCPP_INLINE_VISIBILITY
1099 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1104 template <class _CharT, class _Traits>
1105 inline _LIBCPP_INLINE_VISIBILITY
1106 basic_filebuf<_CharT, _Traits>*
1107 basic_ifstream<_CharT, _Traits>::rdbuf() const
1109 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1112 template <class _CharT, class _Traits>
1113 inline _LIBCPP_INLINE_VISIBILITY
1115 basic_ifstream<_CharT, _Traits>::is_open() const
1117 return __sb_.is_open();
1120 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1121 template <class _CharT, class _Traits>
1123 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1125 if (__sb_.open(__s, __mode | ios_base::in))
1128 this->setstate(ios_base::failbit);
1131 template <class _CharT, class _Traits>
1133 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1135 if (__sb_.open(__s, __mode | ios_base::in))
1138 this->setstate(ios_base::failbit);
1142 template <class _CharT, class _Traits>
1143 inline _LIBCPP_INLINE_VISIBILITY
1145 basic_ifstream<_CharT, _Traits>::close()
1147 if (__sb_.close() == 0)
1148 this->setstate(ios_base::failbit);
1153 template <class _CharT, class _Traits>
1154 class _LIBCPP_TYPE_VIS_ONLY basic_ofstream
1155 : public basic_ostream<_CharT, _Traits>
1158 typedef _CharT char_type;
1159 typedef _Traits traits_type;
1160 typedef typename traits_type::int_type int_type;
1161 typedef typename traits_type::pos_type pos_type;
1162 typedef typename traits_type::off_type off_type;
1165 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1166 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1167 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1168 basic_ofstream(basic_ofstream&& __rhs);
1171 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1172 basic_ofstream& operator=(basic_ofstream&& __rhs);
1174 void swap(basic_ofstream& __rhs);
1176 basic_filebuf<char_type, traits_type>* rdbuf() const;
1177 bool is_open() const;
1178 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1179 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1180 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1185 basic_filebuf<char_type, traits_type> __sb_;
1188 template <class _CharT, class _Traits>
1189 inline _LIBCPP_INLINE_VISIBILITY
1190 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1191 : basic_ostream<char_type, traits_type>(&__sb_)
1195 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1196 template <class _CharT, class _Traits>
1197 inline _LIBCPP_INLINE_VISIBILITY
1198 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1199 : basic_ostream<char_type, traits_type>(&__sb_)
1201 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1202 this->setstate(ios_base::failbit);
1205 template <class _CharT, class _Traits>
1206 inline _LIBCPP_INLINE_VISIBILITY
1207 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1208 : basic_ostream<char_type, traits_type>(&__sb_)
1210 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1211 this->setstate(ios_base::failbit);
1215 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1217 template <class _CharT, class _Traits>
1218 inline _LIBCPP_INLINE_VISIBILITY
1219 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1220 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1221 __sb_(_VSTD::move(__rhs.__sb_))
1223 this->set_rdbuf(&__sb_);
1226 template <class _CharT, class _Traits>
1227 inline _LIBCPP_INLINE_VISIBILITY
1228 basic_ofstream<_CharT, _Traits>&
1229 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1231 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1232 __sb_ = _VSTD::move(__rhs.__sb_);
1236 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1238 template <class _CharT, class _Traits>
1239 inline _LIBCPP_INLINE_VISIBILITY
1241 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1243 basic_ostream<char_type, traits_type>::swap(__rhs);
1244 __sb_.swap(__rhs.__sb_);
1247 template <class _CharT, class _Traits>
1248 inline _LIBCPP_INLINE_VISIBILITY
1250 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1255 template <class _CharT, class _Traits>
1256 inline _LIBCPP_INLINE_VISIBILITY
1257 basic_filebuf<_CharT, _Traits>*
1258 basic_ofstream<_CharT, _Traits>::rdbuf() const
1260 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1263 template <class _CharT, class _Traits>
1264 inline _LIBCPP_INLINE_VISIBILITY
1266 basic_ofstream<_CharT, _Traits>::is_open() const
1268 return __sb_.is_open();
1271 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1272 template <class _CharT, class _Traits>
1274 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1276 if (__sb_.open(__s, __mode | ios_base::out))
1279 this->setstate(ios_base::failbit);
1282 template <class _CharT, class _Traits>
1284 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1286 if (__sb_.open(__s, __mode | ios_base::out))
1289 this->setstate(ios_base::failbit);
1293 template <class _CharT, class _Traits>
1294 inline _LIBCPP_INLINE_VISIBILITY
1296 basic_ofstream<_CharT, _Traits>::close()
1298 if (__sb_.close() == 0)
1299 this->setstate(ios_base::failbit);
1304 template <class _CharT, class _Traits>
1305 class _LIBCPP_TYPE_VIS_ONLY basic_fstream
1306 : public basic_iostream<_CharT, _Traits>
1309 typedef _CharT char_type;
1310 typedef _Traits traits_type;
1311 typedef typename traits_type::int_type int_type;
1312 typedef typename traits_type::pos_type pos_type;
1313 typedef typename traits_type::off_type off_type;
1316 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1317 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1318 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1320 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1321 basic_fstream(basic_fstream&& __rhs);
1324 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1325 basic_fstream& operator=(basic_fstream&& __rhs);
1327 void swap(basic_fstream& __rhs);
1329 basic_filebuf<char_type, traits_type>* rdbuf() const;
1330 bool is_open() const;
1331 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1332 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1333 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1338 basic_filebuf<char_type, traits_type> __sb_;
1341 template <class _CharT, class _Traits>
1342 inline _LIBCPP_INLINE_VISIBILITY
1343 basic_fstream<_CharT, _Traits>::basic_fstream()
1344 : basic_iostream<char_type, traits_type>(&__sb_)
1348 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1349 template <class _CharT, class _Traits>
1350 inline _LIBCPP_INLINE_VISIBILITY
1351 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1352 : basic_iostream<char_type, traits_type>(&__sb_)
1354 if (__sb_.open(__s, __mode) == 0)
1355 this->setstate(ios_base::failbit);
1358 template <class _CharT, class _Traits>
1359 inline _LIBCPP_INLINE_VISIBILITY
1360 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1361 : basic_iostream<char_type, traits_type>(&__sb_)
1363 if (__sb_.open(__s, __mode) == 0)
1364 this->setstate(ios_base::failbit);
1368 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1370 template <class _CharT, class _Traits>
1371 inline _LIBCPP_INLINE_VISIBILITY
1372 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1373 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1374 __sb_(_VSTD::move(__rhs.__sb_))
1376 this->set_rdbuf(&__sb_);
1379 template <class _CharT, class _Traits>
1380 inline _LIBCPP_INLINE_VISIBILITY
1381 basic_fstream<_CharT, _Traits>&
1382 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1384 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1385 __sb_ = _VSTD::move(__rhs.__sb_);
1389 #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1391 template <class _CharT, class _Traits>
1392 inline _LIBCPP_INLINE_VISIBILITY
1394 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1396 basic_iostream<char_type, traits_type>::swap(__rhs);
1397 __sb_.swap(__rhs.__sb_);
1400 template <class _CharT, class _Traits>
1401 inline _LIBCPP_INLINE_VISIBILITY
1403 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1408 template <class _CharT, class _Traits>
1409 inline _LIBCPP_INLINE_VISIBILITY
1410 basic_filebuf<_CharT, _Traits>*
1411 basic_fstream<_CharT, _Traits>::rdbuf() const
1413 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1416 template <class _CharT, class _Traits>
1417 inline _LIBCPP_INLINE_VISIBILITY
1419 basic_fstream<_CharT, _Traits>::is_open() const
1421 return __sb_.is_open();
1424 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1425 template <class _CharT, class _Traits>
1427 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1429 if (__sb_.open(__s, __mode))
1432 this->setstate(ios_base::failbit);
1435 template <class _CharT, class _Traits>
1437 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1439 if (__sb_.open(__s, __mode))
1442 this->setstate(ios_base::failbit);
1446 template <class _CharT, class _Traits>
1447 inline _LIBCPP_INLINE_VISIBILITY
1449 basic_fstream<_CharT, _Traits>::close()
1451 if (__sb_.close() == 0)
1452 this->setstate(ios_base::failbit);
1455 _LIBCPP_END_NAMESPACE_STD
1457 #endif // _LIBCPP_FSTREAM