33template<
class _CharT,
class _Traits>
34basic_filebuf<_CharT, _Traits>*
35basic_filebuf<_CharT, _Traits>::
36open (
const char *__name, ios_base::openmode __mode,
long __prot)
44 _C_file = _RW::__rw_fopen (__name, __mode, __prot);
49 pos_type __pos = pos_type ();
51 if (__mode & ios_base::ate) {
56 __pos = _RW::__rw_fseek (_C_file, 0, 0, ios_base::end);
59 _RW::__rw_fclose (_C_file, 0);
65 _C_beg_pos = _C_cur_pos = __pos;
67 this->_C_state &= ~_RWSTD_IOS_OPENMODE_MASK;
68 this->_C_state |= __mode;
74template<
class _CharT,
class _Traits>
75basic_filebuf<_CharT, _Traits>*
76basic_filebuf<_CharT, _Traits>::
77close (
bool __close_file )
87 basic_filebuf *__retval =
this;
91 if (this->pptr () != this->pbase () && this->_C_is_eof (overflow ()))
96 if (__retval && this->_C_out_last () && !_C_unshift ())
103 _RW::__rw_fclose (_C_file, this->_C_state);
107 _C_cur_pos = _C_beg_pos = pos_type (off_type (-1));
111 this->setg (0, 0, 0);
120 if (_RW::__rw_fclose (_C_file, this->_C_state))
125 _C_cur_pos = _C_beg_pos = pos_type (off_type (-1));
129 this->setg (0, 0, 0);
137template<
class _CharT,
class _Traits>
139basic_filebuf<_CharT, _Traits>::
144 if ( !this->_C_is_in () || !is_open ()
145 || _C_cur_pos == pos_type (off_type (-1)))
152 const pos_type __end_pos =
153 _RW::__rw_fseek (_C_file, this->_C_state, 0, ios_base::end);
155 if (__end_pos != pos_type (off_type (-1))) {
158 _RW::__rw_fseek (_C_file, this->_C_state, _C_cur_pos, ios_base::beg);
160 typedef typename traits_type::state_type _StateT;
161 typedef codecvt<char_type, char, _StateT> _Codecvt;
163 const _Codecvt &__cvt =
_USE_FACET (_Codecvt, this->getloc ());
165 if (__cvt.always_noconv ())
166 __retval += __end_pos - _C_cur_pos;
169 __retval += (__end_pos - _C_cur_pos) / __cvt.max_length ();
172 return __retval > 0 ? __retval : 0;
176template<
class _CharT,
class _Traits>
177typename basic_filebuf<_CharT, _Traits>::int_type
178basic_filebuf<_CharT, _Traits>::
185 this->_C_out_last (
false);
187 if (!this->_C_is_in () || !is_open())
188 return traits_type::eof ();
190 char_type*
const __to_end = this->_C_buffer + this->_C_bufsize;
192 typedef typename traits_type::int_type _IntType;
194 _IntType __ret = _IntType ();
198 if (this->gptr () == this->egptr()) {
204 const _RWSTD_SIZE_T __pbackavail = this->_C_putback_avail ();
210 if (_C_pbacksize == this->_C_bufsize)
211 _C_pbacksize = this->_C_bufsize - 1;
213 traits_type::move (this->eback(), this->gptr () - _C_pbacksize,
221 _C_beg_pos = _C_cur_pos;
223 typedef typename traits_type::state_type _StateT;
224 typedef codecvt<char_type, char, _StateT> _Codecvt;
226 const _Codecvt &__cvt =
_USE_FACET (_Codecvt, this->getloc ());
228 if (__cvt.always_noconv ()) {
230 __nread = (__to_end - this->_C_buffer) - _C_pbacksize;
231 __nread = _RW::__rw_fread (_C_file, this->_C_state,
232 this->_C_buffer + _C_pbacksize,
233 sizeof (char_type) * __nread);
235 return traits_type::eof ();
237 this->setg (this->_C_buffer,
238 this->_C_buffer + _C_pbacksize,
239 this->_C_buffer + _C_pbacksize + __nread);
243 __nread += _C_crlf_intern_count (this->gptr (),
244 this->gptr () + __nread);
249 char* __from_base = __xbuf;
250 const char* __from_next = 0;
251 char_type* __to_base = this->_C_buffer + _C_pbacksize;
252 char_type* __to_next = __to_base;
254 _StateT __state = _C_cur_pos.state ();
256 codecvt_base::result __res = codecvt_base::ok;
258 const ctype<char_type> &__ctp =
259 _USE_FACET (ctype<char_type>, this->getloc ());
261 while (__to_next != __to_end && codecvt_base::error != __res) {
274 __n = _RW::__rw_fread (_C_file, this->_C_state,
278 return traits_type::eof ();
285 _C_crlf_extern_count (__from_base, __from_base + __n);
288 __n += __from_base - __xbuf;
292 __res = __cvt.in (__state, __xbuf, __xbuf + __n, __from_next,
293 __to_base, __to_end, __to_next);
296 case codecvt_base::ok:
301 case codecvt_base::partial:
303 __n -= __from_next - __xbuf;
308 CharTraits::move (__xbuf, __from_next, __n);
311 __from_base = __xbuf + __n;
314 case codecvt_base::noconv:
326 __ctp.widen (__xbuf, __xbuf + __n, __to_base);
327 __to_next = __to_base + __n;
330 case codecvt_base::error:
334 __ret = traits_type::eof ();
339 return traits_type::eof ();
341 __to_base = __to_next;
344 _C_cur_pos.state (__state);
346 this->setg (this->_C_buffer, this->_C_buffer + _C_pbacksize,
351 return traits_type::eof ();
353 _C_cur_pos += __nread;
356 return traits_type::eq_int_type (__ret, _IntType ()) ?
357 traits_type::to_int_type (*this->gptr ()) : __ret;
361template<
class _CharT,
class _Traits>
362typename basic_filebuf<_CharT, _Traits>::int_type
363basic_filebuf<_CharT, _Traits>::
364overflow (int_type __c )
368 if (!this->_C_is_out () || !is_open ())
369 return traits_type::eof ();
371 this->setg (0, 0, 0);
373 const bool __unbuf = this->_C_is_unbuffered ();
375 const char_type __c_to_char = traits_type::to_char_type (__c);
377 if (this->pptr () == 0 && !__unbuf) {
379 this->setp (this->_C_buffer, this->_C_buf_end ());
381 else if ( this->pptr () == this->epptr ()
382 || this->_C_is_eof (__c)
385 const char_type* __buf;
389 if (this->_C_is_eof (__c)){
394 __buf = &__c_to_char;
402 __nchars = this->pptr () - this->pbase ();
406 typedef basic_filebuf _FileBuf;
408 if (__nchars != _FileBuf::xsputn (__buf, __nchars))
409 return traits_type::eof ();
415 if (!this->_C_is_eof (__c) && !__unbuf)
416 this->sputc (__c_to_char);
418 this->_C_out_last (
true);
420 return traits_type::not_eof (__c);
424template <
class _CharT,
class _Traits>
426basic_filebuf<_CharT, _Traits>::
435 if (__nchars < 0 || !this->_C_is_out () || !is_open ())
438 if (0 == this->pptr () && !this->_C_is_unbuffered ())
440 this->setp (this->_C_buffer, this->_C_buf_end ());
444 const char_type*
const __special =
447 if (__buf == __special) {
448 __buf = this->pbase ();
450 else if (__nchars <= __navail) {
454 traits_type::copy (this->pptr (), __buf, __nchars);
456 this->pbump (__nchars);
465 typedef basic_filebuf _FileBuf;
468 if (__nwrite != _FileBuf::xsputn (__special, __nwrite))
478 const char_type*
const __end = __buf + __nchars;
480 typedef typename traits_type::state_type _StateT;
483 _StateT __state = _C_cur_pos.state ();
485 _C_beg_pos = _C_cur_pos;
487 typedef codecvt<char_type, char, _StateT> _Codecvt;
489 const _Codecvt &__cvt =
_USE_FACET (_Codecvt, this->getloc ());
491 if (__cvt.always_noconv ()) {
495 __nwrote = __end - __buf;
499 if (__nbytes != _RW::__rw_fwrite (_C_file, this->_C_state,
509 char* __xbuf_end = __xbuf +
sizeof __xbuf;
511 const char_type* __from_next = 0;
513 for (
const char_type* __base = __buf; __from_next != __end;
514 __base = __from_next) {
519 __cvt.out (__state, __base, __end, __from_next,
520 __xbuf, __xbuf_end, __to_next);
523 sizeof (char_type) * (__end - __base);
526 case codecvt_base::error:
530 _RW::__rw_fwrite (_C_file, this->_C_state, __base, __nbytes);
531 return traits_type::eof ();
533 case codecvt_base::noconv:
535 if (__nbytes != _RW::__rw_fwrite (_C_file, this->_C_state,
537 return traits_type::eof ();
539 __nwrote += __end - __base
540 + _C_crlf_intern_count (__base, __end);
547 || codecvt_base::partial == __res);
555 __nbytes = __to_next - __xbuf;
557 if (__nbytes != _RW::__rw_fwrite (_C_file, this->_C_state,
562 + _C_crlf_extern_count (__xbuf, __to_next);
568 _C_cur_pos += __nwrote;
569 _C_cur_pos.state (__state);
572 if (!this->_C_is_unbuffered ())
573 this->setp (this->_C_buffer, this->_C_buf_end ());
575 this->_C_out_last (
true);
583template<
class _CharT,
class _Traits>
584typename basic_filebuf<_CharT, _Traits>::int_type
585basic_filebuf<_CharT, _Traits>::
586pbackfail (int_type __c)
591 return traits_type::eof ();
594 if (!this->_C_putback_avail ()) {
597 if (this->seekoff (-1, ios_base::cur) == pos_type (off_type (-1)))
598 return traits_type::eof ();
600 if (this->_C_is_eof (underflow ()))
601 return traits_type::eof ();
606 if (traits_type::eq (traits_type::to_char_type (__c), *(this->gptr () - 1))
607 || this->_C_is_eof (__c)) {
610 return traits_type::not_eof (__c);
612 else if (this->_C_is_out ()) {
615 *this->gptr () = traits_type::to_char_type (__c);
619 return traits_type::eof ();
623template<
class _CharT,
class _Traits>
624basic_streambuf<_CharT, _Traits>*
625basic_filebuf<_CharT, _Traits>::
626setbuf (char_type *__buf, streamsize __ssize)
633 const _RWSTD_SIZE_T __bufsize =
637 if ((this->gptr () || this->pptr ()) && is_open () && sync () != 0)
644 if (!__buf && (this->_C_bufsize < __bufsize || !this->_C_buffer)) {
649 __buf =
new char_type [__bufsize];
652 if (this->_C_own_buf ())
653 delete [] this->_C_buffer;
656 this->_C_own_buf (
true);
658 else if (!__buf && __bufsize <= this->_C_bufsize) {
663 __buf = this->_C_buffer;
666 else if (__buf && __buf != this->_C_buffer) {
671 if (this->_C_own_buf ())
672 delete [] this->_C_buffer;
675 this->_C_own_buf (
false);
678 this->_C_buffer = __buf;
679 this->_C_bufsize = __bufsize;
680 this->_C_set_unbuffered (
false);
692 this->_C_set_unbuffered (
true);
696 this->setg (0, 0, 0);
709template<
class _CharT,
class _Traits>
710typename basic_filebuf<_CharT, _Traits>::pos_type
711basic_filebuf<_CharT, _Traits>::
712seekoff (off_type __off, ios_base::seekdir __way, ios_base::openmode)
717 return pos_type (off_type (-1));
719 typedef typename traits_type::state_type _StateT;
720 typedef codecvt<char_type, char, _StateT> _Codecvt;
722 const int __width =
_USE_FACET (_Codecvt, this->getloc ()).encoding ();
725 if (0 != __off && __width <= 0)
726 return pos_type (off_type (-1));
730 return pos_type (off_type (-1));
733 if (this->_C_out_last ())
740 pos_type __pos = _RW::__rw_fseek (_C_file, this->_C_state, __off, __way);
742 if (__pos != pos_type (off_type (-1))) {
747 if (__way == ios_base::cur)
748 __pos.state (_C_cur_pos.state ());
750 _C_beg_pos = _C_cur_pos = __pos;
753 this->_C_out_last (
false);
759template<
class _CharT,
class _Traits>
760typename basic_filebuf<_CharT, _Traits>::pos_type
761basic_filebuf<_CharT, _Traits>::
762seekpos (pos_type __pos, ios_base::openmode)
767 return pos_type (off_type (-1));
770 if (this->pptr () != 0) {
771 if (this->_C_is_eof (this->overflow (traits_type::eof ())))
772 return pos_type (off_type (-1));
775 if ( _RW::__rw_fseek (_C_file, this->_C_state, __pos, ios_base::beg)
776 == pos_type (off_type (-1)))
777 return pos_type (off_type (-1));
779 _C_cur_pos = _C_beg_pos = __pos;
781 this->setg (0, 0, 0);
784 this->_C_out_last (
false);
790template<
class _CharT,
class _Traits>
792basic_filebuf<_CharT, _Traits>::
798 if (this->pptr () != 0) {
800 if (this->_C_is_eof (overflow (traits_type::eof ())))
804 _RW::__rw_fflush (_C_file, this->_C_state);
808 if (this->gptr () != 0) {
813 const _RWSTD_SIZE_T __pbackavail = this->_C_putback_avail ();
814 if (__pbackavail < _C_pbacksize)
815 _C_pbacksize = __pbackavail;
819 if (_C_pbacksize == this->_C_bufsize)
820 _C_pbacksize = this->_C_bufsize - 1;
822 typedef typename traits_type::state_type _StateT;
823 typedef codecvt<char_type, char, _StateT> _Codecvt;
825 const _Codecvt &__cvt =
_USE_FACET (_Codecvt, this->getloc ());
826 int __width = __cvt.encoding ();
830 const off_type __consumed =
831 this->gptr () - this->eback () - _C_pbacksize;
836 _C_cur_pos = _C_beg_pos;
837 _C_cur_pos += (__width * __consumed)
838 + _C_crlf_intern_count (this->eback () + _C_pbacksize,
853 off_type __ext_chars = 0;
856 char* __xbuf_end = __xbuf +
sizeof __xbuf;
858 const char_type* __from_next = 0;
859 const char_type* __from_end = this->gptr ();
860 _StateT __state = _C_beg_pos.state ();
861 const char_type* __base = this->eback () + _C_pbacksize;
863 while (__from_next != __from_end) {
865 __cvt.out (__state, __base, __from_end, __from_next,
866 __xbuf, __xbuf_end, __to_next);
869 case codecvt_base::error:
872 case codecvt_base::noconv:
873 __ext_chars += __from_end - __base
874 + _C_crlf_intern_count (__base, __from_end);
875 __from_next = __from_end;
880 __ext_chars += __to_next - __xbuf
881 + _C_crlf_extern_count (__xbuf, __to_next);
884 __base = __from_next;
887 _C_cur_pos = _C_beg_pos;
888 _C_cur_pos += __ext_chars;
889 _C_cur_pos.state (__state);
894 const off_type __off =
895 _RW::__rw_fseek (_C_file, this->_C_state,
896 _C_cur_pos, ios_base::beg);
901 traits_type::move (this->eback (), this->gptr () - _C_pbacksize,
904 this->setg (this->eback (), this->eback () + _C_pbacksize,
905 this->eback () + _C_pbacksize);
908 _C_beg_pos = _C_cur_pos;
916template<
class _CharT,
class _Traits>
917basic_filebuf<_CharT, _Traits>*
918basic_filebuf<_CharT, _Traits>::
919_C_open (
int __fd,
void *__file, char_type *__buf, streamsize __n)
921 if (is_open () || !__file && -1 == __fd || !setbuf (__buf, __n))
929 this->_C_state |= this->_C_stdio;
934 _C_file = _RW::__rw_fdopen (__fd);
939 this->_C_state &= ~this->_C_stdio;
942 this->_C_state |= _RW::__rw_fmode (_C_file, this->_C_state);
944 this->setg (0, 0, 0);
947 _C_cur_pos = _C_beg_pos = pos_type ();
954template<
class _CharT,
class _Traits>
956basic_filebuf<_CharT, _Traits>::
959 typedef typename traits_type::state_type _StateT;
960 typedef codecvt<char_type, char, _StateT> _Codecvt;
962 const _Codecvt &__cvt =
_USE_FACET (_Codecvt, this->getloc ());
967 if (__cvt.encoding () >= 0)
976 char* __useq_end = 0;
978 _StateT __state = _C_cur_pos.state ();
981 __cvt.unshift (__state, __useq, __useq +
sizeof __useq, __useq_end);
988 if (__res == codecvt_base::error)
991 if (__res == codecvt_base::noconv)
994 const off_type __nwrote =
995 _RW::__rw_fwrite (_C_file, this->_C_state, __useq, __nbytes);
1000 _C_cur_pos += __nwrote;
1002 return __nwrote == __nbytes;
#define _RWSTD_PBACK_SIZE
#define _RWSTD_DEFAULT_BUFSIZE
#define _RWSTD_REINTERPRET_CAST(T, x)
#define _RWSTD_STATIC_CAST(T, x)
#define _RWSTD_NAMESPACE(name)
#define _RWSTD_STREAMSIZE
#define _USE_FACET(type, arg)
#define _RWSTD_IOS_OPENMODE_MASK
#define _RWSTD_ASSERT(ignore)