18#ifndef _STLP_FSTREAM_C
19#define _STLP_FSTREAM_C
21#ifndef _STLP_INTERNAL_FSTREAM_H
25#ifndef _STLP_INTERNAL_LIMITS
31# if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
33# define __BF_int_type__ int
34# define __BF_pos_type__ streampos
35# define __BF_off_type__ streamoff
37# define __BF_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
38# define __BF_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::pos_type
39# define __BF_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::off_type
46template <
class _CharT,
class _Traits>
49 _M_constant_width(
false), _M_always_noconv(
false),
50 _M_int_buf_dynamic(
false),
51 _M_in_input_mode(
false), _M_in_output_mode(
false),
52 _M_in_error_mode(
false), _M_in_putback_mode(
false),
53 _M_int_buf(0), _M_int_buf_EOS(0),
54 _M_ext_buf(0), _M_ext_buf_EOS(0),
55 _M_ext_buf_converted(0), _M_ext_buf_end(0),
58 _M_mmap_base(0), _M_mmap_len(0),
59 _M_saved_eback(0), _M_saved_gptr(0), _M_saved_egptr(0),
61 _M_width(1), _M_max_width(1)
66template <
class _CharT,
class _Traits>
69 _M_deallocate_buffers();
73template <
class _CharT,
class _Traits>
79template <
class _CharT,
class _Traits>
82 bool __ok = this->is_open();
84 if (_M_in_output_mode) {
85 __ok = __ok && !_Traits::eq_int_type(this->overflow(traits_type::eof()),
87 __ok = __ok && this->_M_unshift();
89 else if (_M_in_input_mode)
90 this->_M_exit_input_mode();
93 __ok = _M_base._M_close() && __ok;
98 _M_ext_buf_converted = _M_ext_buf_end = 0;
106 _M_saved_eback = _M_saved_gptr = _M_saved_egptr = 0;
108 _M_in_input_mode = _M_in_output_mode = _M_in_error_mode = _M_in_putback_mode
111 return __ok ? this : 0;
117template <
class _CharT,
class _Traits>
119 if (_M_mmap_base != 0) {
120 _M_base._M_unmap(_M_mmap_base, _M_mmap_len);
124 _M_in_input_mode =
false;
131template <
class _CharT,
class _Traits>
134 if (!this->is_open() || _M_in_output_mode || _M_in_error_mode)
136 else if (_M_in_putback_mode)
137 return this->egptr() - this->gptr();
138 else if (_M_constant_width) {
139 streamoff __pos = _M_base._M_seek(0, ios_base::cur);
140 streamoff __size = _M_base._M_file_size();
141 return __pos >= 0 && __size > __pos ? __size - __pos : 0;
155template <
class _CharT,
class _Traits>
158 const int_type __eof = traits_type::eof();
161 if (!_M_in_input_mode)
166 if (this->gptr() != this->eback() &&
167 (traits_type::eq_int_type(
__c, __eof) ||
168 traits_type::eq(traits_type::to_char_type(
__c), this->gptr()[-1]) ||
171 if (traits_type::eq_int_type(
__c, __eof) ||
172 traits_type::eq(traits_type::to_char_type(
__c), *this->gptr()))
173 return traits_type::to_int_type(*this->gptr());
175 else if (!traits_type::eq_int_type(
__c, __eof)) {
177 _CharT* __pback_end = _M_pback_buf +
__STATIC_CAST(
int,_S_pback_buf_size);
178 if (_M_in_putback_mode) {
180 if (this->eback() != _M_pback_buf)
181 this->setg(this->egptr() - 1, this->egptr() - 1, __pback_end);
186 _M_saved_eback = this->eback();
187 _M_saved_gptr = this->gptr();
188 _M_saved_egptr = this->egptr();
189 this->setg(__pback_end - 1, __pback_end - 1, __pback_end);
190 _M_in_putback_mode =
true;
197 *this->gptr() = traits_type::to_char_type(
__c);
206template <
class _CharT,
class _Traits>
210 if (!_M_in_output_mode)
211 if (!_M_switch_to_output_mode())
212 return traits_type::eof();
214 _CharT* __ibegin = this->_M_int_buf;
215 _CharT* __iend = this->pptr();
216 this->setp(_M_int_buf, _M_int_buf_EOS - 1);
219 if (!traits_type::eq_int_type(
__c, traits_type::eof()))
220 *__iend++ = _Traits::to_char_type(
__c);
223 while (__ibegin != __iend) {
224 const _CharT* __inext = __ibegin;
225 char* __enext = _M_ext_buf;
227 = _M_codecvt->out(_M_state, __ibegin, __iend, __inext,
228 _M_ext_buf, _M_ext_buf_EOS, __enext);
229 if (__status == _Codecvt::noconv) {
231 ? traits_type::not_eof(
__c)
240 else if (__status != _Codecvt::error &&
241 (((__inext == __iend) &&
242 (__enext - _M_ext_buf == _M_width * (__iend - __ibegin))) ||
243 (!_M_constant_width && __inext != __ibegin))) {
246 if (_M_write(_M_ext_buf,
__n))
247 __ibegin += __inext - __ibegin;
249 return _M_output_error();
252 return _M_output_error();
255 return traits_type::not_eof(
__c);
266template <
class _CharT,
class _Traits>
269 if (!_M_in_input_mode &&! _M_in_output_mode && !_M_in_error_mode &&
271 if (__buf == 0 &&
__n == 0)
272 _M_allocate_buffers(0, 1);
273 else if (__buf != 0 &&
__n > 0)
274 _M_allocate_buffers(__buf,
__n);
279#if defined (_STLP_ASSERTIONS)
281template <
class _CharT>
282struct _Filebuf_Tmp_Buf {
284 _Filebuf_Tmp_Buf(
ptrdiff_t __n) : _M_ptr(0) { _M_ptr =
new _CharT[
__n]; }
285 ~_Filebuf_Tmp_Buf() {
delete[] _M_ptr; }
289template <
class _CharT,
class _Traits>
294 if (!this->is_open())
297 if (!_M_constant_width && __off != 0)
300 if (!_M_seek_init(__off != 0 || __whence != ios_base::cur))
304 if (__whence == ios_base::beg || __whence == ios_base::end)
305 return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
310 if (!_M_in_input_mode)
311 return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
314 if (_M_mmap_base != 0) {
317 streamoff __adjust = _M_mmap_len - (this->gptr() - (_CharT*) _M_mmap_base);
320 return __off == 0 ?
pos_type(_M_base._M_seek(0, ios_base::cur) - __adjust)
321 : _M_seek_return(_M_base._M_seek(__off - __adjust, ios_base::cur),
_State_type());
324 if (_M_constant_width) {
325 streamoff __iadj = _M_width * (this->gptr() - this->eback());
332 if (__iadj <= _M_ext_buf_end - _M_ext_buf) {
335 return __off == 0 ?
pos_type(_M_base._M_seek(0, ios_base::cur) - __eadj)
336 : _M_seek_return(_M_base._M_seek(__off - __eadj, ios_base::cur),
_State_type());
341 ptrdiff_t __ipos = this->gptr() - this->eback();
345 int __epos = _M_codecvt->length(__state, _M_ext_buf, _M_ext_buf_converted,
347#if defined (_STLP_ASSERTIONS)
351 _Filebuf_Tmp_Buf<_CharT> __buf(__ipos);
352 _CharT* __ibegin = __buf._M_ptr;
353 _CharT* __inext = __ibegin;
356 = _M_codecvt->in(__tmp_state,
357 _M_ext_buf, _M_ext_buf + __epos, __dummy,
358 __ibegin, __ibegin + __ipos, __inext);
370 streamoff __cur = _M_base._M_seek(0, ios_base::cur);
371 streamoff __adj = _M_base._M_get_offset(_M_ext_buf, _M_ext_buf + __epos) -
372 _M_base._M_get_offset(_M_ext_buf, _M_ext_buf_end);
373 if (__cur != -1 && __cur + __adj >= 0)
374 return __off == 0 ?
pos_type(__cur + __adj)
375 : _M_seek_return(__cur + __adj, __state);
382template <
class _CharT,
class _Traits>
386 if (this->is_open()) {
387 if (!_M_seek_init(
true))
391 if (__off != -1 && _M_base._M_seek(__off, ios_base::beg) != -1) {
392 _M_state = __pos.state();
393 return _M_seek_return(__off, __pos.state());
401template <
class _CharT,
class _Traits>
403 if (_M_in_output_mode)
404 return traits_type::eq_int_type(this->overflow(traits_type::eof()),
405 traits_type::eof()) ? -1 : 0;
412template <
class _CharT,
class _Traits>
414 if (!_M_in_input_mode && !_M_in_output_mode && !_M_in_error_mode) {
415 this->_M_setup_codecvt(__loc);
428template <
class _CharT,
class _Traits>
430 if (this->is_open() && (((
int)_M_base.__o_mode() & (
int)ios_base::in) !=0)
431 && (_M_in_output_mode == 0) && (_M_in_error_mode == 0)) {
432 if (!_M_int_buf && !_M_allocate_buffers())
435 _M_ext_buf_converted = _M_ext_buf;
436 _M_ext_buf_end = _M_ext_buf;
438 _M_end_state = _M_state;
440 _M_in_input_mode =
true;
451template <
class _CharT,
class _Traits>
453 if (this->is_open() && (_M_base.__o_mode() & (
int)ios_base::out) &&
454 _M_in_input_mode == 0 && _M_in_error_mode == 0) {
456 if (!_M_int_buf && !_M_allocate_buffers())
462 if (_M_base.__o_mode() & ios_base::app)
465 this->setp(_M_int_buf, _M_int_buf_EOS - 1);
466 _M_in_output_mode =
true;
483template <
class _CharT,
class _Traits>
486 this->_M_exit_input_mode();
487 _M_in_output_mode =
false;
488 _M_in_error_mode =
true;
490 return traits_type::eof();
493template <
class _CharT,
class _Traits>
498 _M_state = _M_end_state;
502 if (_M_ext_buf_end > _M_ext_buf_converted)
504 _M_ext_buf_end = _STLP_STD::copy(_M_ext_buf_converted, _M_ext_buf_end, _M_ext_buf);
509 _M_ext_buf_end = _M_ext_buf;
515 ptrdiff_t __n = _M_base._M_read(_M_ext_buf_end, _M_ext_buf_EOS - _M_ext_buf_end);
519 return traits_type::eof();
522 _M_ext_buf_end +=
__n;
525 if (_M_ext_buf == _M_ext_buf_end) {
527 return traits_type::eof();
535 = _M_codecvt->in(_M_end_state,
536 _M_ext_buf, _M_ext_buf_end, __enext,
537 _M_int_buf, _M_int_buf_EOS, __inext);
547 if (__status == _Codecvt::noconv)
549 else if (__status == _Codecvt::error ||
550 (__inext != _M_int_buf && __enext == _M_ext_buf) ||
551 (_M_constant_width && (__inext - _M_int_buf) * _M_width != (__enext - _M_ext_buf)) ||
552 (__inext == _M_int_buf && __enext - _M_ext_buf >= _M_max_width))
553 return _M_input_error();
554 else if (__inext != _M_int_buf) {
555 _M_ext_buf_converted = _M_ext_buf + (__enext - _M_ext_buf);
556 this->setg(_M_int_buf, _M_int_buf, __inext);
557 return traits_type::to_int_type(*_M_int_buf);
565 return traits_type::eof();
577template <
class _CharT,
class _Traits>
580 _M_in_output_mode =
false;
581 _M_in_input_mode =
false;
582 _M_in_error_mode =
true;
584 return traits_type::eof();
592template <
class _CharT,
class _Traits>
594 if (_M_in_output_mode && !_M_constant_width) {
597 char* __enext = _M_ext_buf;
598 __status = _M_codecvt->unshift(_M_state,
599 _M_ext_buf, _M_ext_buf_EOS, __enext);
600 if (__status == _Codecvt::noconv ||
601 (__enext == _M_ext_buf && __status == _Codecvt::ok))
603 else if (__status == _Codecvt::error)
605 else if (!_M_write(_M_ext_buf, __enext - _M_ext_buf))
607 }
while (__status == _Codecvt::partial);
629template <
class _CharT,
class _Traits>
647 _M_int_buf_dynamic =
true;
651 _M_int_buf_dynamic =
false;
665 _M_deallocate_buffers();
675template <
class _CharT,
class _Traits>
680 ((_M_base.__page_size() + 4095UL) / _M_base.__page_size()) * _M_base.__page_size();
681 return _M_allocate_buffers(0, __default_bufsiz);
684template <
class _CharT,
class _Traits>
686 if (_M_int_buf_dynamic)
699template <
class _CharT,
class _Traits>
702 _M_in_error_mode =
false;
706 if (_M_in_output_mode) {
707 bool __ok = !traits_type::eq_int_type(this->overflow(traits_type::eof()),
710 __ok = __ok && this->_M_unshift();
712 _M_in_output_mode =
false;
713 _M_in_error_mode =
true;
720 if (_M_in_input_mode && _M_in_putback_mode)
721 _M_exit_putback_mode();
734template <
class _CharT,
class _Traits>
736 if (has_facet<_Codecvt>(__loc)) {
737 _M_codecvt = &use_facet<_Codecvt>(__loc) ;
738 int __encoding = _M_codecvt->encoding();
740 _M_width = (
max)(__encoding, 1);
741 _M_max_width = _M_codecvt->max_length();
742 _M_constant_width = __encoding > 0;
743 _M_always_noconv = _M_codecvt->always_noconv();
747 _M_width = _M_max_width = 1;
748 _M_constant_width = _M_always_noconv =
false;
751 use_facet<_Codecvt>(__loc);
758# undef __BF_int_type__
759# undef __BF_pos_type__
760# undef __BF_off_type__
#define _STLP_DEFAULT_CONSTRUCTED(_TTp)
#define _STLP_ASSERT(expr)
static bool _STLP_CALL _M_doit(basic_filebuf< char_type, _Traits > *, char_type *, char_type *)
static int_type _STLP_CALL _M_doit(basic_filebuf< _CharT, _Traits > *__this)
virtual int_type underflow()
void _M_deallocate_buffers()
virtual basic_streambuf< _CharT, _Traits > * setbuf(char_type *, streamsize)
bool _M_switch_to_input_mode()
int_type _M_underflow_aux()
virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode=ios_base::in|ios_base::out)
bool _M_seek_init(bool __do_unshift)
bool _M_allocate_buffers()
bool _M_switch_to_output_mode()
void _M_exit_input_mode()
_Traits::pos_type pos_type
int_type _M_input_error()
virtual streamsize showmanyc()
virtual int_type overflow(int_type=traits_type::eof())
_Traits::int_type int_type
_Traits::off_type off_type
_Traits::state_type _State_type
int_type _M_output_error()
virtual int_type pbackfail(int_type=traits_type::eof())
void _M_setup_codecvt(const locale &, bool __on_imbue=true)
virtual pos_type seekpos(pos_type, ios_base::openmode=ios_base::in|ios_base::out)
virtual void imbue(const locale &)
__kernel_ptrdiff_t ptrdiff_t
#define _STLP_TYPENAME_ON_RETURN_TYPE
#define _STLP_STATIC_ASSERT(expr)
#define __STATIC_CAST(__x, __y)
#define _STLP_BEGIN_NAMESPACE
#define _STLP_END_NAMESPACE