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 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
175 #pragma GCC system_header
179 #include <__undef_macros>
182 _LIBCPP_BEGIN_NAMESPACE_STD
184 template <class _CharT, class _Traits>
185 class _LIBCPP_TEMPLATE_VIS basic_filebuf
186 : public basic_streambuf<_CharT, _Traits>
189 typedef _CharT char_type;
190 typedef _Traits traits_type;
191 typedef typename traits_type::int_type int_type;
192 typedef typename traits_type::pos_type pos_type;
193 typedef typename traits_type::off_type off_type;
194 typedef typename traits_type::state_type state_type;
196 // 27.9.1.2 Constructors/destructor:
198 #ifndef _LIBCPP_CXX03_LANG
199 basic_filebuf(basic_filebuf&& __rhs);
201 virtual ~basic_filebuf();
203 // 27.9.1.3 Assign/swap:
204 #ifndef _LIBCPP_CXX03_LANG
205 _LIBCPP_INLINE_VISIBILITY
206 basic_filebuf& operator=(basic_filebuf&& __rhs);
208 void swap(basic_filebuf& __rhs);
211 _LIBCPP_INLINE_VISIBILITY
212 bool is_open() const;
213 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
214 basic_filebuf* open(const char* __s, ios_base::openmode __mode);
215 _LIBCPP_INLINE_VISIBILITY
216 basic_filebuf* open(const string& __s, ios_base::openmode __mode);
218 basic_filebuf* close();
221 // 27.9.1.5 Overridden virtual functions:
222 virtual int_type underflow();
223 virtual int_type pbackfail(int_type __c = traits_type::eof());
224 virtual int_type overflow (int_type __c = traits_type::eof());
225 virtual basic_streambuf<char_type, traits_type>* setbuf(char_type* __s, streamsize __n);
226 virtual pos_type seekoff(off_type __off, ios_base::seekdir __way,
227 ios_base::openmode __wch = ios_base::in | ios_base::out);
228 virtual pos_type seekpos(pos_type __sp,
229 ios_base::openmode __wch = ios_base::in | ios_base::out);
231 virtual void imbue(const locale& __loc);
235 const char* __extbufnext_;
236 const char* __extbufend_;
237 char __extbuf_min_[8];
239 char_type* __intbuf_;
242 const codecvt<char_type, char, state_type>* __cv_;
244 state_type __st_last_;
245 ios_base::openmode __om_;
246 ios_base::openmode __cm_;
249 bool __always_noconv_;
255 template <class _CharT, class _Traits>
256 basic_filebuf<_CharT, _Traits>::basic_filebuf()
271 __always_noconv_(false)
273 if (has_facet<codecvt<char_type, char, state_type> >(this->getloc()))
275 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(this->getloc());
276 __always_noconv_ = __cv_->always_noconv();
281 #ifndef _LIBCPP_CXX03_LANG
283 template <class _CharT, class _Traits>
284 basic_filebuf<_CharT, _Traits>::basic_filebuf(basic_filebuf&& __rhs)
285 : basic_streambuf<_CharT, _Traits>(__rhs)
287 if (__rhs.__extbuf_ == __rhs.__extbuf_min_)
289 __extbuf_ = __extbuf_min_;
290 __extbufnext_ = __extbuf_ + (__rhs.__extbufnext_ - __rhs.__extbuf_);
291 __extbufend_ = __extbuf_ + (__rhs.__extbufend_ - __rhs.__extbuf_);
295 __extbuf_ = __rhs.__extbuf_;
296 __extbufnext_ = __rhs.__extbufnext_;
297 __extbufend_ = __rhs.__extbufend_;
299 __ebs_ = __rhs.__ebs_;
300 __intbuf_ = __rhs.__intbuf_;
301 __ibs_ = __rhs.__ibs_;
302 __file_ = __rhs.__file_;
305 __st_last_ = __rhs.__st_last_;
308 __owns_eb_ = __rhs.__owns_eb_;
309 __owns_ib_ = __rhs.__owns_ib_;
310 __always_noconv_ = __rhs.__always_noconv_;
313 if (__rhs.pbase() == __rhs.__intbuf_)
314 this->setp(__intbuf_, __intbuf_ + (__rhs. epptr() - __rhs.pbase()));
316 this->setp((char_type*)__extbuf_,
317 (char_type*)__extbuf_ + (__rhs. epptr() - __rhs.pbase()));
318 this->__pbump(__rhs. pptr() - __rhs.pbase());
320 else if (__rhs.eback())
322 if (__rhs.eback() == __rhs.__intbuf_)
323 this->setg(__intbuf_, __intbuf_ + (__rhs.gptr() - __rhs.eback()),
324 __intbuf_ + (__rhs.egptr() - __rhs.eback()));
326 this->setg((char_type*)__extbuf_,
327 (char_type*)__extbuf_ + (__rhs.gptr() - __rhs.eback()),
328 (char_type*)__extbuf_ + (__rhs.egptr() - __rhs.eback()));
331 __rhs.__extbufnext_ = 0;
332 __rhs.__extbufend_ = 0;
337 __rhs.__st_ = state_type();
338 __rhs.__st_last_ = state_type();
341 __rhs.__owns_eb_ = false;
342 __rhs.__owns_ib_ = false;
347 template <class _CharT, class _Traits>
349 basic_filebuf<_CharT, _Traits>&
350 basic_filebuf<_CharT, _Traits>::operator=(basic_filebuf&& __rhs)
357 #endif // _LIBCPP_CXX03_LANG
359 template <class _CharT, class _Traits>
360 basic_filebuf<_CharT, _Traits>::~basic_filebuf()
362 #ifndef _LIBCPP_NO_EXCEPTIONS
365 #endif // _LIBCPP_NO_EXCEPTIONS
367 #ifndef _LIBCPP_NO_EXCEPTIONS
372 #endif // _LIBCPP_NO_EXCEPTIONS
379 template <class _CharT, class _Traits>
381 basic_filebuf<_CharT, _Traits>::swap(basic_filebuf& __rhs)
383 basic_streambuf<char_type, traits_type>::swap(__rhs);
384 if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
386 _VSTD::swap(__extbuf_, __rhs.__extbuf_);
387 _VSTD::swap(__extbufnext_, __rhs.__extbufnext_);
388 _VSTD::swap(__extbufend_, __rhs.__extbufend_);
392 ptrdiff_t __ln = __extbufnext_ - __extbuf_;
393 ptrdiff_t __le = __extbufend_ - __extbuf_;
394 ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_;
395 ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_;
396 if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_)
398 __extbuf_ = __rhs.__extbuf_;
399 __rhs.__extbuf_ = __rhs.__extbuf_min_;
401 else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_)
403 __rhs.__extbuf_ = __extbuf_;
404 __extbuf_ = __extbuf_min_;
406 __extbufnext_ = __extbuf_ + __rn;
407 __extbufend_ = __extbuf_ + __re;
408 __rhs.__extbufnext_ = __rhs.__extbuf_ + __ln;
409 __rhs.__extbufend_ = __rhs.__extbuf_ + __le;
411 _VSTD::swap(__ebs_, __rhs.__ebs_);
412 _VSTD::swap(__intbuf_, __rhs.__intbuf_);
413 _VSTD::swap(__ibs_, __rhs.__ibs_);
414 _VSTD::swap(__file_, __rhs.__file_);
415 _VSTD::swap(__cv_, __rhs.__cv_);
416 _VSTD::swap(__st_, __rhs.__st_);
417 _VSTD::swap(__st_last_, __rhs.__st_last_);
418 _VSTD::swap(__om_, __rhs.__om_);
419 _VSTD::swap(__cm_, __rhs.__cm_);
420 _VSTD::swap(__owns_eb_, __rhs.__owns_eb_);
421 _VSTD::swap(__owns_ib_, __rhs.__owns_ib_);
422 _VSTD::swap(__always_noconv_, __rhs.__always_noconv_);
423 if (this->eback() == (char_type*)__rhs.__extbuf_min_)
425 ptrdiff_t __n = this->gptr() - this->eback();
426 ptrdiff_t __e = this->egptr() - this->eback();
427 this->setg((char_type*)__extbuf_min_,
428 (char_type*)__extbuf_min_ + __n,
429 (char_type*)__extbuf_min_ + __e);
431 else if (this->pbase() == (char_type*)__rhs.__extbuf_min_)
433 ptrdiff_t __n = this->pptr() - this->pbase();
434 ptrdiff_t __e = this->epptr() - this->pbase();
435 this->setp((char_type*)__extbuf_min_,
436 (char_type*)__extbuf_min_ + __e);
439 if (__rhs.eback() == (char_type*)__extbuf_min_)
441 ptrdiff_t __n = __rhs.gptr() - __rhs.eback();
442 ptrdiff_t __e = __rhs.egptr() - __rhs.eback();
443 __rhs.setg((char_type*)__rhs.__extbuf_min_,
444 (char_type*)__rhs.__extbuf_min_ + __n,
445 (char_type*)__rhs.__extbuf_min_ + __e);
447 else if (__rhs.pbase() == (char_type*)__extbuf_min_)
449 ptrdiff_t __n = __rhs.pptr() - __rhs.pbase();
450 ptrdiff_t __e = __rhs.epptr() - __rhs.pbase();
451 __rhs.setp((char_type*)__rhs.__extbuf_min_,
452 (char_type*)__rhs.__extbuf_min_ + __e);
457 template <class _CharT, class _Traits>
458 inline _LIBCPP_INLINE_VISIBILITY
460 swap(basic_filebuf<_CharT, _Traits>& __x, basic_filebuf<_CharT, _Traits>& __y)
465 template <class _CharT, class _Traits>
468 basic_filebuf<_CharT, _Traits>::is_open() const
473 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
474 template <class _CharT, class _Traits>
475 basic_filebuf<_CharT, _Traits>*
476 basic_filebuf<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
478 basic_filebuf<_CharT, _Traits>* __rt = 0;
483 switch (__mode & ~ios_base::ate)
486 case ios_base::out | ios_base::trunc:
489 case ios_base::out | ios_base::app:
496 case ios_base::in | ios_base::out:
499 case ios_base::in | ios_base::out | ios_base::trunc:
502 case ios_base::in | ios_base::out | ios_base::app:
503 case ios_base::in | ios_base::app:
506 case ios_base::out | ios_base::binary:
507 case ios_base::out | ios_base::trunc | ios_base::binary:
510 case ios_base::out | ios_base::app | ios_base::binary:
511 case ios_base::app | ios_base::binary:
514 case ios_base::in | ios_base::binary:
517 case ios_base::in | ios_base::out | ios_base::binary:
520 case ios_base::in | ios_base::out | ios_base::trunc | ios_base::binary:
523 case ios_base::in | ios_base::out | ios_base::app | ios_base::binary:
524 case ios_base::in | ios_base::app | ios_base::binary:
533 __file_ = fopen(__s, __mdstr);
537 if (__mode & ios_base::ate)
539 if (fseek(__file_, 0, SEEK_END))
554 template <class _CharT, class _Traits>
556 basic_filebuf<_CharT, _Traits>*
557 basic_filebuf<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
559 return open(__s.c_str(), __mode);
563 template <class _CharT, class _Traits>
564 basic_filebuf<_CharT, _Traits>*
565 basic_filebuf<_CharT, _Traits>::close()
567 basic_filebuf<_CharT, _Traits>* __rt = 0;
571 unique_ptr<FILE, int(*)(FILE*)> __h(__file_, fclose);
574 if (fclose(__h.release()) == 0)
582 template <class _CharT, class _Traits>
583 typename basic_filebuf<_CharT, _Traits>::int_type
584 basic_filebuf<_CharT, _Traits>::underflow()
587 return traits_type::eof();
588 bool __initial = __read_mode();
590 if (this->gptr() == 0)
591 this->setg(&__1buf, &__1buf+1, &__1buf+1);
592 const size_t __unget_sz = __initial ? 0 : min<size_t>((this->egptr() - this->eback()) / 2, 4);
593 int_type __c = traits_type::eof();
594 if (this->gptr() == this->egptr())
596 memmove(this->eback(), this->egptr() - __unget_sz, __unget_sz * sizeof(char_type));
597 if (__always_noconv_)
599 size_t __nmemb = static_cast<size_t>(this->egptr() - this->eback() - __unget_sz);
600 __nmemb = fread(this->eback() + __unget_sz, 1, __nmemb, __file_);
603 this->setg(this->eback(),
604 this->eback() + __unget_sz,
605 this->eback() + __unget_sz + __nmemb);
606 __c = traits_type::to_int_type(*this->gptr());
611 _LIBCPP_ASSERT ( !(__extbufnext_ == NULL && (__extbufend_ != __extbufnext_)), "underflow moving from NULL" );
612 if (__extbufend_ != __extbufnext_)
613 memmove(__extbuf_, __extbufnext_, __extbufend_ - __extbufnext_);
614 __extbufnext_ = __extbuf_ + (__extbufend_ - __extbufnext_);
615 __extbufend_ = __extbuf_ + (__extbuf_ == __extbuf_min_ ? sizeof(__extbuf_min_) : __ebs_);
616 size_t __nmemb = _VSTD::min(static_cast<size_t>(__ibs_ - __unget_sz),
617 static_cast<size_t>(__extbufend_ - __extbufnext_));
618 codecvt_base::result __r;
620 size_t __nr = fread((void*) const_cast<char *>(__extbufnext_), 1, __nmemb, __file_);
626 __extbufend_ = __extbufnext_ + __nr;
628 __r = __cv_->in(__st_, __extbuf_, __extbufend_, __extbufnext_,
629 this->eback() + __unget_sz,
630 this->eback() + __ibs_, __inext);
631 if (__r == codecvt_base::noconv)
633 this->setg((char_type*)__extbuf_, (char_type*)__extbuf_,
634 (char_type*)const_cast<char *>(__extbufend_));
635 __c = traits_type::to_int_type(*this->gptr());
637 else if (__inext != this->eback() + __unget_sz)
639 this->setg(this->eback(), this->eback() + __unget_sz, __inext);
640 __c = traits_type::to_int_type(*this->gptr());
646 __c = traits_type::to_int_type(*this->gptr());
647 if (this->eback() == &__1buf)
652 template <class _CharT, class _Traits>
653 typename basic_filebuf<_CharT, _Traits>::int_type
654 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c)
656 if (__file_ && this->eback() < this->gptr())
658 if (traits_type::eq_int_type(__c, traits_type::eof()))
661 return traits_type::not_eof(__c);
663 if ((__om_ & ios_base::out) ||
664 traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]))
667 *this->gptr() = traits_type::to_char_type(__c);
671 return traits_type::eof();
674 template <class _CharT, class _Traits>
675 typename basic_filebuf<_CharT, _Traits>::int_type
676 basic_filebuf<_CharT, _Traits>::overflow(int_type __c)
679 return traits_type::eof();
682 char_type* __pb_save = this->pbase();
683 char_type* __epb_save = this->epptr();
684 if (!traits_type::eq_int_type(__c, traits_type::eof()))
686 if (this->pptr() == 0)
687 this->setp(&__1buf, &__1buf+1);
688 *this->pptr() = traits_type::to_char_type(__c);
691 if (this->pptr() != this->pbase())
693 if (__always_noconv_)
695 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
696 if (fwrite(this->pbase(), sizeof(char_type), __nmemb, __file_) != __nmemb)
697 return traits_type::eof();
701 char* __extbe = __extbuf_;
702 codecvt_base::result __r;
708 const char_type* __e;
709 __r = __cv_->out(__st_, this->pbase(), this->pptr(), __e,
710 __extbuf_, __extbuf_ + __ebs_, __extbe);
711 if (__e == this->pbase())
712 return traits_type::eof();
713 if (__r == codecvt_base::noconv)
715 size_t __nmemb = static_cast<size_t>(this->pptr() - this->pbase());
716 if (fwrite(this->pbase(), 1, __nmemb, __file_) != __nmemb)
717 return traits_type::eof();
719 else if (__r == codecvt_base::ok || __r == codecvt_base::partial)
721 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
722 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
723 return traits_type::eof();
724 if (__r == codecvt_base::partial)
726 this->setp(const_cast<char_type*>(__e), this->pptr());
727 this->__pbump(this->epptr() - this->pbase());
731 return traits_type::eof();
732 } while (__r == codecvt_base::partial);
734 this->setp(__pb_save, __epb_save);
736 return traits_type::not_eof(__c);
739 template <class _CharT, class _Traits>
740 basic_streambuf<_CharT, _Traits>*
741 basic_filebuf<_CharT, _Traits>::setbuf(char_type* __s, streamsize __n)
750 if (__ebs_ > sizeof(__extbuf_min_))
752 if (__always_noconv_ && __s)
754 __extbuf_ = (char*)__s;
759 __extbuf_ = new char[__ebs_];
765 __extbuf_ = __extbuf_min_;
766 __ebs_ = sizeof(__extbuf_min_);
769 if (!__always_noconv_)
771 __ibs_ = max<streamsize>(__n, sizeof(__extbuf_min_));
772 if (__s && __ibs_ >= sizeof(__extbuf_min_))
779 __intbuf_ = new char_type[__ibs_];
792 template <class _CharT, class _Traits>
793 typename basic_filebuf<_CharT, _Traits>::pos_type
794 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, ios_base::seekdir __way,
800 int __width = __cv_->encoding();
801 if (__file_ == 0 || (__width <= 0 && __off != 0) || sync())
802 return pos_type(off_type(-1));
803 // __width > 0 || __off == 0
817 return pos_type(off_type(-1));
819 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
820 if (fseek(__file_, __width > 0 ? __width * __off : 0, __whence))
821 return pos_type(off_type(-1));
822 pos_type __r = ftell(__file_);
824 if (fseeko(__file_, __width > 0 ? __width * __off : 0, __whence))
825 return pos_type(off_type(-1));
826 pos_type __r = ftello(__file_);
832 template <class _CharT, class _Traits>
833 typename basic_filebuf<_CharT, _Traits>::pos_type
834 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __sp, ios_base::openmode)
836 if (__file_ == 0 || sync())
837 return pos_type(off_type(-1));
838 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
839 if (fseek(__file_, __sp, SEEK_SET))
840 return pos_type(off_type(-1));
842 if (fseeko(__file_, __sp, SEEK_SET))
843 return pos_type(off_type(-1));
845 __st_ = __sp.state();
849 template <class _CharT, class _Traits>
851 basic_filebuf<_CharT, _Traits>::sync()
858 if (__cm_ & ios_base::out)
860 if (this->pptr() != this->pbase())
861 if (overflow() == traits_type::eof())
863 codecvt_base::result __r;
867 __r = __cv_->unshift(__st_, __extbuf_, __extbuf_ + __ebs_, __extbe);
868 size_t __nmemb = static_cast<size_t>(__extbe - __extbuf_);
869 if (fwrite(__extbuf_, 1, __nmemb, __file_) != __nmemb)
871 } while (__r == codecvt_base::partial);
872 if (__r == codecvt_base::error)
877 else if (__cm_ & ios_base::in)
880 state_type __state = __st_last_;
881 bool __update_st = false;
882 if (__always_noconv_)
883 __c = this->egptr() - this->gptr();
886 int __width = __cv_->encoding();
887 __c = __extbufend_ - __extbufnext_;
889 __c += __width * (this->egptr() - this->gptr());
892 if (this->gptr() != this->egptr())
894 const int __off = __cv_->length(__state, __extbuf_,
896 this->gptr() - this->eback());
897 __c += __extbufnext_ - __extbuf_ - __off;
902 #if defined(_LIBCPP_HAS_NO_OFF_T_FUNCTIONS)
903 if (fseek(__file_, -__c, SEEK_CUR))
906 if (fseeko(__file_, -__c, SEEK_CUR))
911 __extbufnext_ = __extbufend_ = __extbuf_;
918 template <class _CharT, class _Traits>
920 basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc)
923 __cv_ = &use_facet<codecvt<char_type, char, state_type> >(__loc);
924 bool __old_anc = __always_noconv_;
925 __always_noconv_ = __cv_->always_noconv();
926 if (__old_anc != __always_noconv_)
930 // invariant, char_type is char, else we couldn't get here
931 if (__always_noconv_) // need to dump __intbuf_
935 __owns_eb_ = __owns_ib_;
937 __extbuf_ = (char*)__intbuf_;
942 else // need to obtain an __intbuf_.
943 { // If __extbuf_ is user-supplied, use it, else new __intbuf_
944 if (!__owns_eb_ && __extbuf_ != __extbuf_min_)
947 __intbuf_ = (char_type*)__extbuf_;
949 __extbuf_ = new char[__ebs_];
955 __intbuf_ = new char_type[__ibs_];
962 template <class _CharT, class _Traits>
964 basic_filebuf<_CharT, _Traits>::__read_mode()
966 if (!(__cm_ & ios_base::in))
969 if (__always_noconv_)
970 this->setg((char_type*)__extbuf_,
971 (char_type*)__extbuf_ + __ebs_,
972 (char_type*)__extbuf_ + __ebs_);
974 this->setg(__intbuf_, __intbuf_ + __ibs_, __intbuf_ + __ibs_);
975 __cm_ = ios_base::in;
981 template <class _CharT, class _Traits>
983 basic_filebuf<_CharT, _Traits>::__write_mode()
985 if (!(__cm_ & ios_base::out))
988 if (__ebs_ > sizeof(__extbuf_min_))
990 if (__always_noconv_)
991 this->setp((char_type*)__extbuf_,
992 (char_type*)__extbuf_ + (__ebs_ - 1));
994 this->setp(__intbuf_, __intbuf_ + (__ibs_ - 1));
998 __cm_ = ios_base::out;
1004 template <class _CharT, class _Traits>
1005 class _LIBCPP_TEMPLATE_VIS basic_ifstream
1006 : public basic_istream<_CharT, _Traits>
1009 typedef _CharT char_type;
1010 typedef _Traits traits_type;
1011 typedef typename traits_type::int_type int_type;
1012 typedef typename traits_type::pos_type pos_type;
1013 typedef typename traits_type::off_type off_type;
1015 _LIBCPP_INLINE_VISIBILITY
1017 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1018 _LIBCPP_INLINE_VISIBILITY
1019 explicit basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in);
1020 _LIBCPP_INLINE_VISIBILITY
1021 explicit basic_ifstream(const string& __s, ios_base::openmode __mode = ios_base::in);
1023 #ifndef _LIBCPP_CXX03_LANG
1024 _LIBCPP_INLINE_VISIBILITY
1025 basic_ifstream(basic_ifstream&& __rhs);
1027 _LIBCPP_INLINE_VISIBILITY
1028 basic_ifstream& operator=(basic_ifstream&& __rhs);
1030 _LIBCPP_INLINE_VISIBILITY
1031 void swap(basic_ifstream& __rhs);
1033 _LIBCPP_INLINE_VISIBILITY
1034 basic_filebuf<char_type, traits_type>* rdbuf() const;
1035 _LIBCPP_INLINE_VISIBILITY
1036 bool is_open() const;
1037 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1038 void open(const char* __s, ios_base::openmode __mode = ios_base::in);
1039 void open(const string& __s, ios_base::openmode __mode = ios_base::in);
1041 _LIBCPP_INLINE_VISIBILITY
1045 basic_filebuf<char_type, traits_type> __sb_;
1048 template <class _CharT, class _Traits>
1050 basic_ifstream<_CharT, _Traits>::basic_ifstream()
1051 : basic_istream<char_type, traits_type>(&__sb_)
1055 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1056 template <class _CharT, class _Traits>
1058 basic_ifstream<_CharT, _Traits>::basic_ifstream(const char* __s, ios_base::openmode __mode)
1059 : basic_istream<char_type, traits_type>(&__sb_)
1061 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1062 this->setstate(ios_base::failbit);
1065 template <class _CharT, class _Traits>
1067 basic_ifstream<_CharT, _Traits>::basic_ifstream(const string& __s, ios_base::openmode __mode)
1068 : basic_istream<char_type, traits_type>(&__sb_)
1070 if (__sb_.open(__s, __mode | ios_base::in) == 0)
1071 this->setstate(ios_base::failbit);
1075 #ifndef _LIBCPP_CXX03_LANG
1077 template <class _CharT, class _Traits>
1079 basic_ifstream<_CharT, _Traits>::basic_ifstream(basic_ifstream&& __rhs)
1080 : basic_istream<char_type, traits_type>(_VSTD::move(__rhs)),
1081 __sb_(_VSTD::move(__rhs.__sb_))
1083 this->set_rdbuf(&__sb_);
1086 template <class _CharT, class _Traits>
1088 basic_ifstream<_CharT, _Traits>&
1089 basic_ifstream<_CharT, _Traits>::operator=(basic_ifstream&& __rhs)
1091 basic_istream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1092 __sb_ = _VSTD::move(__rhs.__sb_);
1096 #endif // _LIBCPP_CXX03_LANG
1098 template <class _CharT, class _Traits>
1101 basic_ifstream<_CharT, _Traits>::swap(basic_ifstream& __rhs)
1103 basic_istream<char_type, traits_type>::swap(__rhs);
1104 __sb_.swap(__rhs.__sb_);
1107 template <class _CharT, class _Traits>
1108 inline _LIBCPP_INLINE_VISIBILITY
1110 swap(basic_ifstream<_CharT, _Traits>& __x, basic_ifstream<_CharT, _Traits>& __y)
1115 template <class _CharT, class _Traits>
1117 basic_filebuf<_CharT, _Traits>*
1118 basic_ifstream<_CharT, _Traits>::rdbuf() const
1120 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1123 template <class _CharT, class _Traits>
1126 basic_ifstream<_CharT, _Traits>::is_open() const
1128 return __sb_.is_open();
1131 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1132 template <class _CharT, class _Traits>
1134 basic_ifstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1136 if (__sb_.open(__s, __mode | ios_base::in))
1139 this->setstate(ios_base::failbit);
1142 template <class _CharT, class _Traits>
1144 basic_ifstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1146 if (__sb_.open(__s, __mode | ios_base::in))
1149 this->setstate(ios_base::failbit);
1153 template <class _CharT, class _Traits>
1156 basic_ifstream<_CharT, _Traits>::close()
1158 if (__sb_.close() == 0)
1159 this->setstate(ios_base::failbit);
1164 template <class _CharT, class _Traits>
1165 class _LIBCPP_TEMPLATE_VIS basic_ofstream
1166 : public basic_ostream<_CharT, _Traits>
1169 typedef _CharT char_type;
1170 typedef _Traits traits_type;
1171 typedef typename traits_type::int_type int_type;
1172 typedef typename traits_type::pos_type pos_type;
1173 typedef typename traits_type::off_type off_type;
1175 _LIBCPP_INLINE_VISIBILITY
1177 _LIBCPP_INLINE_VISIBILITY
1178 explicit basic_ofstream(const char* __s, ios_base::openmode __mode = ios_base::out);
1179 _LIBCPP_INLINE_VISIBILITY
1180 explicit basic_ofstream(const string& __s, ios_base::openmode __mode = ios_base::out);
1181 #ifndef _LIBCPP_CXX03_LANG
1182 _LIBCPP_INLINE_VISIBILITY
1183 basic_ofstream(basic_ofstream&& __rhs);
1185 _LIBCPP_INLINE_VISIBILITY
1186 basic_ofstream& operator=(basic_ofstream&& __rhs);
1188 _LIBCPP_INLINE_VISIBILITY
1189 void swap(basic_ofstream& __rhs);
1191 _LIBCPP_INLINE_VISIBILITY
1192 basic_filebuf<char_type, traits_type>* rdbuf() const;
1193 _LIBCPP_INLINE_VISIBILITY
1194 bool is_open() const;
1195 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1196 void open(const char* __s, ios_base::openmode __mode = ios_base::out);
1197 void open(const string& __s, ios_base::openmode __mode = ios_base::out);
1199 _LIBCPP_INLINE_VISIBILITY
1203 basic_filebuf<char_type, traits_type> __sb_;
1206 template <class _CharT, class _Traits>
1208 basic_ofstream<_CharT, _Traits>::basic_ofstream()
1209 : basic_ostream<char_type, traits_type>(&__sb_)
1213 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1214 template <class _CharT, class _Traits>
1216 basic_ofstream<_CharT, _Traits>::basic_ofstream(const char* __s, ios_base::openmode __mode)
1217 : basic_ostream<char_type, traits_type>(&__sb_)
1219 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1220 this->setstate(ios_base::failbit);
1223 template <class _CharT, class _Traits>
1225 basic_ofstream<_CharT, _Traits>::basic_ofstream(const string& __s, ios_base::openmode __mode)
1226 : basic_ostream<char_type, traits_type>(&__sb_)
1228 if (__sb_.open(__s, __mode | ios_base::out) == 0)
1229 this->setstate(ios_base::failbit);
1233 #ifndef _LIBCPP_CXX03_LANG
1235 template <class _CharT, class _Traits>
1237 basic_ofstream<_CharT, _Traits>::basic_ofstream(basic_ofstream&& __rhs)
1238 : basic_ostream<char_type, traits_type>(_VSTD::move(__rhs)),
1239 __sb_(_VSTD::move(__rhs.__sb_))
1241 this->set_rdbuf(&__sb_);
1244 template <class _CharT, class _Traits>
1246 basic_ofstream<_CharT, _Traits>&
1247 basic_ofstream<_CharT, _Traits>::operator=(basic_ofstream&& __rhs)
1249 basic_ostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1250 __sb_ = _VSTD::move(__rhs.__sb_);
1254 #endif // _LIBCPP_CXX03_LANG
1256 template <class _CharT, class _Traits>
1259 basic_ofstream<_CharT, _Traits>::swap(basic_ofstream& __rhs)
1261 basic_ostream<char_type, traits_type>::swap(__rhs);
1262 __sb_.swap(__rhs.__sb_);
1265 template <class _CharT, class _Traits>
1266 inline _LIBCPP_INLINE_VISIBILITY
1268 swap(basic_ofstream<_CharT, _Traits>& __x, basic_ofstream<_CharT, _Traits>& __y)
1273 template <class _CharT, class _Traits>
1275 basic_filebuf<_CharT, _Traits>*
1276 basic_ofstream<_CharT, _Traits>::rdbuf() const
1278 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1281 template <class _CharT, class _Traits>
1284 basic_ofstream<_CharT, _Traits>::is_open() const
1286 return __sb_.is_open();
1289 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1290 template <class _CharT, class _Traits>
1292 basic_ofstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1294 if (__sb_.open(__s, __mode | ios_base::out))
1297 this->setstate(ios_base::failbit);
1300 template <class _CharT, class _Traits>
1302 basic_ofstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1304 if (__sb_.open(__s, __mode | ios_base::out))
1307 this->setstate(ios_base::failbit);
1311 template <class _CharT, class _Traits>
1314 basic_ofstream<_CharT, _Traits>::close()
1316 if (__sb_.close() == 0)
1317 this->setstate(ios_base::failbit);
1322 template <class _CharT, class _Traits>
1323 class _LIBCPP_TEMPLATE_VIS basic_fstream
1324 : public basic_iostream<_CharT, _Traits>
1327 typedef _CharT char_type;
1328 typedef _Traits traits_type;
1329 typedef typename traits_type::int_type int_type;
1330 typedef typename traits_type::pos_type pos_type;
1331 typedef typename traits_type::off_type off_type;
1333 _LIBCPP_INLINE_VISIBILITY
1335 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1336 _LIBCPP_INLINE_VISIBILITY
1337 explicit basic_fstream(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1338 _LIBCPP_INLINE_VISIBILITY
1339 explicit basic_fstream(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1341 #ifndef _LIBCPP_CXX03_LANG
1342 _LIBCPP_INLINE_VISIBILITY
1343 basic_fstream(basic_fstream&& __rhs);
1345 _LIBCPP_INLINE_VISIBILITY
1346 basic_fstream& operator=(basic_fstream&& __rhs);
1348 _LIBCPP_INLINE_VISIBILITY
1349 void swap(basic_fstream& __rhs);
1351 _LIBCPP_INLINE_VISIBILITY
1352 basic_filebuf<char_type, traits_type>* rdbuf() const;
1353 _LIBCPP_INLINE_VISIBILITY
1354 bool is_open() const;
1355 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1356 void open(const char* __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1357 void open(const string& __s, ios_base::openmode __mode = ios_base::in | ios_base::out);
1359 _LIBCPP_INLINE_VISIBILITY
1363 basic_filebuf<char_type, traits_type> __sb_;
1366 template <class _CharT, class _Traits>
1368 basic_fstream<_CharT, _Traits>::basic_fstream()
1369 : basic_iostream<char_type, traits_type>(&__sb_)
1373 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1374 template <class _CharT, class _Traits>
1376 basic_fstream<_CharT, _Traits>::basic_fstream(const char* __s, ios_base::openmode __mode)
1377 : basic_iostream<char_type, traits_type>(&__sb_)
1379 if (__sb_.open(__s, __mode) == 0)
1380 this->setstate(ios_base::failbit);
1383 template <class _CharT, class _Traits>
1385 basic_fstream<_CharT, _Traits>::basic_fstream(const string& __s, ios_base::openmode __mode)
1386 : basic_iostream<char_type, traits_type>(&__sb_)
1388 if (__sb_.open(__s, __mode) == 0)
1389 this->setstate(ios_base::failbit);
1393 #ifndef _LIBCPP_CXX03_LANG
1395 template <class _CharT, class _Traits>
1397 basic_fstream<_CharT, _Traits>::basic_fstream(basic_fstream&& __rhs)
1398 : basic_iostream<char_type, traits_type>(_VSTD::move(__rhs)),
1399 __sb_(_VSTD::move(__rhs.__sb_))
1401 this->set_rdbuf(&__sb_);
1404 template <class _CharT, class _Traits>
1406 basic_fstream<_CharT, _Traits>&
1407 basic_fstream<_CharT, _Traits>::operator=(basic_fstream&& __rhs)
1409 basic_iostream<char_type, traits_type>::operator=(_VSTD::move(__rhs));
1410 __sb_ = _VSTD::move(__rhs.__sb_);
1414 #endif // _LIBCPP_CXX03_LANG
1416 template <class _CharT, class _Traits>
1419 basic_fstream<_CharT, _Traits>::swap(basic_fstream& __rhs)
1421 basic_iostream<char_type, traits_type>::swap(__rhs);
1422 __sb_.swap(__rhs.__sb_);
1425 template <class _CharT, class _Traits>
1426 inline _LIBCPP_INLINE_VISIBILITY
1428 swap(basic_fstream<_CharT, _Traits>& __x, basic_fstream<_CharT, _Traits>& __y)
1433 template <class _CharT, class _Traits>
1435 basic_filebuf<_CharT, _Traits>*
1436 basic_fstream<_CharT, _Traits>::rdbuf() const
1438 return const_cast<basic_filebuf<char_type, traits_type>*>(&__sb_);
1441 template <class _CharT, class _Traits>
1444 basic_fstream<_CharT, _Traits>::is_open() const
1446 return __sb_.is_open();
1449 #ifndef _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE
1450 template <class _CharT, class _Traits>
1452 basic_fstream<_CharT, _Traits>::open(const char* __s, ios_base::openmode __mode)
1454 if (__sb_.open(__s, __mode))
1457 this->setstate(ios_base::failbit);
1460 template <class _CharT, class _Traits>
1462 basic_fstream<_CharT, _Traits>::open(const string& __s, ios_base::openmode __mode)
1464 if (__sb_.open(__s, __mode))
1467 this->setstate(ios_base::failbit);
1471 template <class _CharT, class _Traits>
1474 basic_fstream<_CharT, _Traits>::close()
1476 if (__sb_.close() == 0)
1477 this->setstate(ios_base::failbit);
1480 _LIBCPP_END_NAMESPACE_STD
1484 #endif // _LIBCPP_FSTREAM