williamr@2: /* williamr@2: * Copyright (c) 1996,1997 williamr@2: * Silicon Graphics Computer Systems, Inc. williamr@2: * williamr@4: * Copyright (c) 1999 williamr@2: * Boris Fomitchev williamr@2: * williamr@2: * This material is provided "as is", with absolutely no warranty expressed williamr@2: * or implied. Any use is at your own risk. williamr@2: * williamr@4: * Permission to use or copy this software for any purpose is hereby granted williamr@2: * without fee, provided the above notices are retained on all copies. williamr@2: * Permission to modify the code and to distribute modified code is granted, williamr@2: * provided the above notices are retained, and a notice that the code was williamr@2: * modified is included with the above copyright notice. williamr@2: * williamr@2: */ williamr@2: #ifndef _STLP_FSTREAM_C williamr@2: #define _STLP_FSTREAM_C williamr@2: williamr@4: #ifndef _STLP_INTERNAL_FSTREAM_H williamr@2: # include williamr@4: #endif williamr@2: williamr@4: #ifndef _STLP_INTERNAL_LIMITS williamr@4: # include williamr@4: #endif williamr@2: williamr@2: _STLP_BEGIN_NAMESPACE williamr@2: williamr@2: # if defined ( _STLP_NESTED_TYPE_PARAM_BUG ) williamr@2: // no wchar_t is supported for this mode williamr@2: # define __BF_int_type__ int williamr@2: # define __BF_pos_type__ streampos williamr@2: # define __BF_off_type__ streamoff williamr@2: # else williamr@2: # define __BF_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type williamr@2: # define __BF_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::pos_type williamr@2: # define __BF_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::off_type williamr@2: # endif williamr@2: williamr@2: williamr@2: //---------------------------------------------------------------------- williamr@2: // Public basic_filebuf<> member functions williamr@2: williamr@2: template williamr@2: basic_filebuf<_CharT, _Traits>::basic_filebuf() williamr@2: : basic_streambuf<_CharT, _Traits>(), _M_base(), williamr@2: _M_constant_width(false), _M_always_noconv(false), williamr@2: _M_int_buf_dynamic(false), williamr@2: _M_in_input_mode(false), _M_in_output_mode(false), williamr@2: _M_in_error_mode(false), _M_in_putback_mode(false), williamr@2: _M_int_buf(0), _M_int_buf_EOS(0), williamr@2: _M_ext_buf(0), _M_ext_buf_EOS(0), williamr@2: _M_ext_buf_converted(0), _M_ext_buf_end(0), williamr@2: _M_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)), williamr@2: _M_end_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)), williamr@2: _M_mmap_base(0), _M_mmap_len(0), williamr@2: _M_saved_eback(0), _M_saved_gptr(0), _M_saved_egptr(0), williamr@2: _M_codecvt(0), williamr@2: _M_width(1), _M_max_width(1) williamr@2: { williamr@4: this->_M_setup_codecvt(locale(), false); williamr@2: } williamr@2: williamr@2: template williamr@2: basic_filebuf<_CharT, _Traits>::~basic_filebuf() { williamr@2: this->close(); williamr@2: _M_deallocate_buffers(); williamr@2: } williamr@2: williamr@2: williamr@2: template williamr@4: _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type williamr@4: basic_filebuf<_CharT, _Traits>::underflow() { williamr@2: return _Underflow<_CharT, _Traits>::_M_doit(this); williamr@2: } williamr@2: williamr@2: template williamr@4: basic_filebuf<_CharT, _Traits>* williamr@4: basic_filebuf<_CharT, _Traits>::close() { williamr@2: bool __ok = this->is_open(); williamr@2: williamr@2: if (_M_in_output_mode) { williamr@2: __ok = __ok && !_Traits::eq_int_type(this->overflow(traits_type::eof()), williamr@2: traits_type::eof()); williamr@2: __ok == __ok && this->_M_unshift(); williamr@2: } williamr@2: else if (_M_in_input_mode) williamr@2: this->_M_exit_input_mode(); williamr@2: williamr@2: // Note order of arguments. We close the file even if __ok is false. williamr@2: __ok = _M_base._M_close() && __ok; williamr@2: williamr@2: // Restore the initial state, except that we don't deallocate the buffer williamr@2: // or mess with the cached codecvt information. williamr@2: _M_state = _M_end_state = _State_type(); williamr@2: _M_ext_buf_converted = _M_ext_buf_end = 0; williamr@2: williamr@2: _M_mmap_base = 0; williamr@2: _M_mmap_len = 0; williamr@2: williamr@2: this->setg(0, 0, 0); williamr@2: this->setp(0, 0); williamr@2: williamr@2: _M_saved_eback = _M_saved_gptr = _M_saved_egptr = 0; williamr@2: williamr@2: _M_in_input_mode = _M_in_output_mode = _M_in_error_mode = _M_in_putback_mode williamr@2: = false; williamr@2: williamr@2: return __ok ? this : 0; williamr@2: } williamr@2: williamr@2: // This member function is called whenever we exit input mode. williamr@2: // It unmaps the memory-mapped file, if any, and sets williamr@4: // _M_in_input_mode to false. williamr@2: template williamr@4: void basic_filebuf<_CharT, _Traits>::_M_exit_input_mode() { williamr@2: if (_M_mmap_base != 0) williamr@4: _M_base._M_unmap(_M_mmap_base, _M_mmap_len); williamr@2: _M_in_input_mode = false; williamr@2: _M_mmap_base = 0; williamr@2: } williamr@2: williamr@2: williamr@2: //---------------------------------------------------------------------- williamr@2: // basic_filebuf<> overridden protected virtual member functions williamr@2: williamr@2: template williamr@4: streamsize basic_filebuf<_CharT, _Traits>::showmanyc() { williamr@2: // Is there any possibility that reads can succeed? williamr@2: if (!this->is_open() || _M_in_output_mode || _M_in_error_mode) williamr@2: return -1; williamr@2: else if (_M_in_putback_mode) williamr@2: return this->egptr() - this->gptr(); williamr@2: else if (_M_constant_width) { williamr@2: streamoff __pos = _M_base._M_seek(0, ios_base::cur); williamr@2: streamoff __size = _M_base._M_file_size(); williamr@4: return __pos >= 0 && __size > __pos ? __size - __pos : 0; williamr@2: } williamr@4: else williamr@2: return 0; williamr@2: } williamr@2: williamr@2: williamr@4: // Make a putback position available, if necessary, by switching to a williamr@2: // special internal buffer used only for putback. The buffer is williamr@2: // [_M_pback_buf, _M_pback_buf + _S_pback_buf_size), but the base williamr@2: // class only sees a piece of it at a time. (We want to make sure williamr@2: // that we don't try to read a character that hasn't been initialized.) williamr@2: // The end of the putback buffer is always _M_pback_buf + _S_pback_buf_size, williamr@2: // but the beginning is usually not _M_pback_buf. williamr@2: template williamr@4: __BF_int_type__ williamr@4: basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) { williamr@2: const int_type __eof = traits_type::eof(); williamr@2: williamr@2: // If we aren't already in input mode, pushback is impossible. williamr@2: if (!_M_in_input_mode) williamr@2: return __eof; williamr@2: williamr@2: // We can use the ordinary get buffer if there's enough space, and williamr@2: // if it's a buffer that we're allowed to write to. williamr@2: if (this->gptr() != this->eback() && williamr@2: (traits_type::eq_int_type(__c, __eof) || williamr@2: traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]) || williamr@2: !_M_mmap_base)) { williamr@2: this->gbump(-1); williamr@2: if (traits_type::eq_int_type(__c, __eof) || williamr@2: traits_type::eq(traits_type::to_char_type(__c), *this->gptr())) williamr@2: return traits_type::to_int_type(*this->gptr()); williamr@2: } williamr@2: else if (!traits_type::eq_int_type(__c, __eof)) { williamr@2: // Are we in the putback buffer already? williamr@2: _CharT* __pback_end = _M_pback_buf + __STATIC_CAST(int,_S_pback_buf_size); williamr@2: if (_M_in_putback_mode) { williamr@2: // Do we have more room in the putback buffer? williamr@4: if (this->eback() != _M_pback_buf) williamr@2: this->setg(this->egptr() - 1, this->egptr() - 1, __pback_end); williamr@2: else williamr@2: return __eof; // No more room in the buffer, so fail. williamr@2: } williamr@2: else { // We're not yet in the putback buffer. williamr@2: _M_saved_eback = this->eback(); williamr@2: _M_saved_gptr = this->gptr(); williamr@2: _M_saved_egptr = this->egptr(); williamr@2: this->setg(__pback_end - 1, __pback_end - 1, __pback_end); williamr@2: _M_in_putback_mode = true; williamr@2: } williamr@2: } williamr@2: else williamr@2: return __eof; williamr@4: williamr@2: // We have made a putback position available. Assign to it, and return. williamr@2: *this->gptr() = traits_type::to_char_type(__c); williamr@2: return __c; williamr@2: } williamr@2: williamr@2: // This member function flushes the put area, and also outputs the williamr@2: // character __c (unless __c is eof). Invariant: we always leave room williamr@2: // in the internal buffer for one character more than the base class knows williamr@2: // about. We see the internal buffer as [_M_int_buf, _M_int_buf_EOS), but williamr@2: // the base class only sees [_M_int_buf, _M_int_buf_EOS - 1). williamr@2: template williamr@2: __BF_int_type__ williamr@4: basic_filebuf<_CharT, _Traits>::overflow(int_type __c) { williamr@2: // Switch to output mode, if necessary. williamr@2: if (!_M_in_output_mode) williamr@2: if (!_M_switch_to_output_mode()) williamr@2: return traits_type::eof(); williamr@4: williamr@2: _CharT* __ibegin = this->_M_int_buf; williamr@2: _CharT* __iend = this->pptr(); williamr@2: this->setp(_M_int_buf, _M_int_buf_EOS - 1); williamr@2: williamr@2: // Put __c at the end of the internal buffer. williamr@2: if (!traits_type::eq_int_type(__c, traits_type::eof())) williamr@4: *__iend++ = _Traits::to_char_type(__c); williamr@2: williamr@2: // For variable-width encodings, output may take more than one pass. williamr@2: while (__ibegin != __iend) { williamr@2: const _CharT* __inext = __ibegin; williamr@2: char* __enext = _M_ext_buf; williamr@2: typename _Codecvt::result __status williamr@2: = _M_codecvt->out(_M_state, __ibegin, __iend, __inext, williamr@4: _M_ext_buf, _M_ext_buf_EOS, __enext); williamr@4: if (__status == _Codecvt::noconv) { williamr@2: return _Noconv_output<_Traits>::_M_doit(this, __ibegin, __iend) williamr@2: ? traits_type::not_eof(__c) williamr@2: : _M_output_error(); williamr@4: } williamr@4: williamr@2: // For a constant-width encoding we know that the external buffer williamr@2: // is large enough, so failure to consume the entire internal buffer williamr@2: // or to produce the correct number of external characters, is an error. williamr@4: // For a variable-width encoding, however, we require only that we williamr@2: // consume at least one internal character williamr@4: else if (__status != _Codecvt::error && williamr@4: (((__inext == __iend) && williamr@4: (__enext - _M_ext_buf == _M_width * (__iend - __ibegin))) || williamr@2: (!_M_constant_width && __inext != __ibegin))) { williamr@2: // We successfully converted part or all of the internal buffer. williamr@2: ptrdiff_t __n = __enext - _M_ext_buf; williamr@2: if (_M_write(_M_ext_buf, __n)) williamr@2: __ibegin += __inext - __ibegin; williamr@2: else williamr@2: return _M_output_error(); williamr@2: } williamr@2: else williamr@2: return _M_output_error(); williamr@2: } williamr@2: williamr@2: return traits_type::not_eof(__c); williamr@2: } williamr@2: williamr@2: // This member function must be called before any I/O has been williamr@2: // performed on the stream, otherwise it has no effect. williamr@2: // williamr@4: // __buf == 0 && __n == 0 means to make this stream unbuffered. williamr@2: // __buf != 0 && __n > 0 means to use __buf as the stream's internal williamr@2: // buffer, rather than the buffer that would otherwise be allocated williamr@2: // automatically. __buf must be a pointer to an array of _CharT whose williamr@2: // size is at least __n. williamr@2: template williamr@2: basic_streambuf<_CharT, _Traits>* williamr@4: basic_filebuf<_CharT, _Traits>::setbuf(_CharT* __buf, streamsize __n) { williamr@2: if (!_M_in_input_mode &&! _M_in_output_mode && !_M_in_error_mode && williamr@2: _M_int_buf == 0) { williamr@2: if (__buf == 0 && __n == 0) williamr@2: _M_allocate_buffers(0, 1); williamr@2: else if (__buf != 0 && __n > 0) williamr@2: _M_allocate_buffers(__buf, __n); williamr@2: } williamr@2: return this; williamr@2: } williamr@2: williamr@2: template williamr@2: __BF_pos_type__ williamr@2: basic_filebuf<_CharT, _Traits>::seekoff(off_type __off, williamr@2: ios_base::seekdir __whence, williamr@4: ios_base::openmode /* dummy */) { williamr@2: if (this->is_open() && williamr@2: (__off == 0 || (_M_constant_width && this->_M_base._M_in_binary_mode()))) { williamr@2: williamr@2: if (!_M_seek_init(__off != 0 || __whence != ios_base::cur)) williamr@2: return pos_type(-1); williamr@2: williamr@2: // Seek to beginning or end, regardless of whether we're in input mode. williamr@2: if (__whence == ios_base::beg || __whence == ios_base::end) williamr@2: return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence), williamr@2: _State_type()); williamr@2: williamr@4: // Seek relative to current position. Complicated if we're in input mode. williamr@2: else if (__whence == ios_base::cur) { williamr@2: if (!_M_in_input_mode) williamr@2: return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence), williamr@2: _State_type()); williamr@2: else if (_M_mmap_base != 0) { williamr@2: // __off is relative to gptr(). We need to do a bit of arithmetic williamr@2: // to get an offset relative to the external file pointer. williamr@2: streamoff __adjust = _M_mmap_len - (this->gptr() - (_CharT*) _M_mmap_base); williamr@2: williamr@2: // if __off == 0, we do not need to exit input mode and to shift file pointer williamr@4: return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __adjust) williamr@4: : _M_seek_return(_M_base._M_seek(__off - __adjust, ios_base::cur), _State_type()); williamr@2: } williamr@4: else if (_M_constant_width) { // Get or set the position. williamr@4: streamoff __iadj = _M_width * (this->gptr() - this->eback()); williamr@2: williamr@2: // Compensate for offset relative to gptr versus offset relative williamr@4: // to external pointer. For a text-oriented stream, where the williamr@2: // compensation is more than just pointer arithmetic, we may get williamr@2: // but not set the current position. williamr@4: williamr@2: if (__iadj <= _M_ext_buf_end - _M_ext_buf) { williamr@4: streamoff __eadj = _M_base._M_get_offset(_M_ext_buf + __STATIC_CAST(ptrdiff_t, __iadj), _M_ext_buf_end); williamr@2: williamr@4: return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __eadj) williamr@4: : _M_seek_return(_M_base._M_seek(__off - __eadj, ios_base::cur), _State_type()); williamr@2: } williamr@4: } else { // Get the position. Encoding is var width. williamr@2: // Get position in internal buffer. williamr@2: ptrdiff_t __ipos = this->gptr() - this->eback(); williamr@4: williamr@2: // Get corresponding position in external buffer. williamr@2: _State_type __state = _M_state; williamr@2: int __epos = _M_codecvt->length(__state, _M_ext_buf, _M_ext_buf_end, williamr@2: __ipos); williamr@2: williamr@4: if (__epos >= 0) { williamr@4: // Sanity check (expensive): make sure __epos is the right answer. williamr@4: _State_type __tmp_state = _M_state; williamr@4: _Filebuf_Tmp_Buf<_CharT> __buf(__ipos); williamr@4: _CharT* __ibegin = __buf._M_ptr; williamr@4: _CharT* __inext = __ibegin; williamr@2: williamr@4: const char* __dummy; williamr@4: typename _Codecvt::result __status williamr@4: = _M_codecvt->in(__tmp_state, williamr@4: _M_ext_buf, _M_ext_buf + __epos, __dummy, williamr@4: __ibegin, __ibegin + __ipos, __inext); williamr@4: if (__status != _Codecvt::error && williamr@4: (__status == _Codecvt::noconv || williamr@4: (__inext == __ibegin + __ipos && williamr@4: equal(this->eback(), this->gptr(), __ibegin, _STLP_PRIV _Eq_traits())))) { williamr@4: // Get the current position (at the end of the external buffer), williamr@4: // then adjust it. Again, it might be a text-oriented stream. williamr@4: streamoff __cur = _M_base._M_seek(0, ios_base::cur); williamr@4: streamoff __adj = williamr@4: _M_base._M_get_offset(_M_ext_buf, _M_ext_buf + __epos) - williamr@4: _M_base._M_get_offset(_M_ext_buf, _M_ext_buf_end); williamr@4: if (__cur != -1 && __cur + __adj >= 0) williamr@4: return __off == 0 ? pos_type(__cur + __adj) williamr@4: : _M_seek_return(__cur + __adj, __state); williamr@4: //return _M_seek_return(__cur + __adj, __state); williamr@4: } williamr@4: // We failed the sanity check here. williamr@2: } williamr@2: } williamr@2: } williamr@4: // Unrecognized value for __whence here. williamr@2: } williamr@4: williamr@4: return pos_type(-1); williamr@2: } williamr@2: williamr@2: williamr@2: template williamr@2: __BF_pos_type__ williamr@2: basic_filebuf<_CharT, _Traits>::seekpos(pos_type __pos, williamr@4: ios_base::openmode /* dummy */) { williamr@2: if (this->is_open()) { williamr@2: if (!_M_seek_init(true)) williamr@2: return pos_type(-1); williamr@2: williamr@2: streamoff __off = off_type(__pos); williamr@2: if (__off != -1 && _M_base._M_seek(__off, ios_base::beg) != -1) { williamr@2: _M_state = __pos.state(); williamr@2: return _M_seek_return(__off, __pos.state()); williamr@2: } williamr@2: } williamr@4: williamr@4: return pos_type(-1); williamr@2: } williamr@2: williamr@2: williamr@2: template williamr@4: int basic_filebuf<_CharT, _Traits>::sync() { williamr@2: if (_M_in_output_mode) williamr@2: return traits_type::eq_int_type(this->overflow(traits_type::eof()), williamr@4: traits_type::eof()) ? -1 : 0; williamr@4: return 0; williamr@2: } williamr@2: williamr@2: williamr@2: // Change the filebuf's locale. This member function has no effect williamr@2: // unless it is called before any I/O is performed on the stream. williamr@2: template williamr@4: void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) { williamr@4: if (!_M_in_input_mode && !_M_in_output_mode && !_M_in_error_mode) { williamr@2: this->_M_setup_codecvt(__loc); williamr@2: } williamr@2: } williamr@2: williamr@2: //---------------------------------------------------------------------- williamr@2: // basic_filebuf<> helper functions. williamr@2: williamr@2: //---------------------------------------- williamr@2: // Helper functions for switching between modes. williamr@2: williamr@2: // This member function is called if we're performing the first I/O williamr@4: // operation on a filebuf, or if we're performing an input operation williamr@2: // immediately after a seek. williamr@2: template williamr@4: bool basic_filebuf<_CharT, _Traits>::_M_switch_to_input_mode() { williamr@4: if (this->is_open() && (((int)_M_base.__o_mode() & (int)ios_base::in) != 0) williamr@4: && (_M_in_output_mode == 0) && (_M_in_error_mode == 0)) { williamr@2: if (!_M_int_buf && !_M_allocate_buffers()) williamr@2: return false; williamr@2: williamr@2: _M_ext_buf_converted = _M_ext_buf; williamr@2: _M_ext_buf_end = _M_ext_buf; williamr@2: williamr@2: _M_end_state = _M_state; williamr@2: williamr@2: _M_in_input_mode = true; williamr@2: return true; williamr@2: } williamr@2: williamr@4: return false; williamr@2: } williamr@2: williamr@2: williamr@2: // This member function is called if we're performing the first I/O williamr@4: // operation on a filebuf, or if we're performing an output operation williamr@2: // immediately after a seek. williamr@2: template williamr@4: bool basic_filebuf<_CharT, _Traits>::_M_switch_to_output_mode() { williamr@2: if (this->is_open() && (_M_base.__o_mode() & (int)ios_base::out) && williamr@2: _M_in_input_mode == 0 && _M_in_error_mode == 0) { williamr@2: williamr@2: if (!_M_int_buf && !_M_allocate_buffers()) williamr@2: return false; williamr@2: williamr@2: // In append mode, every write does an implicit seek to the end williamr@2: // of the file. Whenever leaving output mode, the end of file williamr@2: // get put in the initial shift state. williamr@2: if (_M_base.__o_mode() & ios_base::app) williamr@2: _M_state = _State_type(); williamr@2: williamr@2: this->setp(_M_int_buf, _M_int_buf_EOS - 1); williamr@2: _M_in_output_mode = true; williamr@2: return true; williamr@2: } williamr@4: williamr@4: return false; williamr@2: } williamr@2: williamr@2: williamr@2: //---------------------------------------- williamr@2: // Helper functions for input williamr@2: williamr@2: // This member function is called if there is an error during input. williamr@2: // It puts the filebuf in error mode, clear the get area buffer, and williamr@2: // returns eof. williamr@2: // returns eof. Error mode is sticky; it is cleared only by close or williamr@2: // seek. williamr@2: williamr@2: template williamr@2: __BF_int_type__ williamr@4: basic_filebuf<_CharT, _Traits>::_M_input_error() { williamr@4: this->_M_exit_input_mode(); williamr@2: _M_in_output_mode = false; williamr@2: _M_in_error_mode = true; williamr@2: this->setg(0, 0, 0); williamr@2: return traits_type::eof(); williamr@2: } williamr@2: williamr@2: template williamr@4: __BF_int_type__ williamr@4: basic_filebuf<_CharT, _Traits>::_M_underflow_aux() { williamr@2: // We have the state and file position from the end of the internal williamr@2: // buffer. This round, they become the beginning of the internal buffer. williamr@2: _M_state = _M_end_state; williamr@2: williamr@2: // Fill the external buffer. Start with any leftover characters that williamr@2: // didn't get converted last time. williamr@2: if (_M_ext_buf_end > _M_ext_buf_converted) williamr@2: williamr@2: _M_ext_buf_end = copy(_M_ext_buf_converted, _M_ext_buf_end, _M_ext_buf); williamr@2: // boris : copy_backward did not work williamr@4: //_M_ext_buf_end = copy_backward(_M_ext_buf_converted, _M_ext_buf_end, williamr@2: //_M_ext_buf+ (_M_ext_buf_end - _M_ext_buf_converted)); williamr@2: else williamr@2: _M_ext_buf_end = _M_ext_buf; williamr@4: williamr@2: // Now fill the external buffer with characters from the file. This is williamr@4: // a loop because occasionally we don't get enough external characters williamr@2: // to make progress. williamr@4: for (;;) { williamr@2: ptrdiff_t __n = _M_base._M_read(_M_ext_buf_end, _M_ext_buf_EOS - _M_ext_buf_end); williamr@4: williamr@2: // Don't enter error mode for a failed read. Error mode is sticky, williamr@2: // and we might succeed if we try again. williamr@4: if (__n <= 0) williamr@2: return traits_type::eof(); williamr@2: williamr@4: // Convert the external buffer to internal characters. williamr@2: _M_ext_buf_end += __n; williamr@2: const char* __enext; williamr@2: _CharT* __inext; williamr@2: williamr@2: typename _Codecvt::result __status williamr@2: = _M_codecvt->in(_M_end_state, williamr@2: _M_ext_buf, _M_ext_buf_end, __enext, williamr@2: _M_int_buf, _M_int_buf_EOS, __inext); williamr@2: williamr@2: // Error conditions: (1) Return value of error. (2) Producing internal williamr@2: // characters without consuming external characters. (3) In fixed-width williamr@2: // encodings, producing an internal sequence whose length is inconsistent williamr@4: // with that of the internal sequence. (4) Failure to produce any williamr@2: // characters if we have enough characters in the external buffer, where williamr@2: // "enough" means the largest possible width of a single character. williamr@2: if (__status == _Codecvt::noconv) williamr@2: return _Noconv_input<_Traits>::_M_doit(this); williamr@2: else if (__status == _Codecvt::error || williamr@2: (__inext != _M_int_buf && __enext == _M_ext_buf) || williamr@2: (_M_constant_width && williamr@2: // __inext - _M_int_buf != _M_width * (__enext - _M_ext_buf)) || williamr@2: (__inext - _M_int_buf) * _M_width != (__enext - _M_ext_buf)) || williamr@2: (__inext == _M_int_buf && __enext - _M_ext_buf >= _M_max_width)) williamr@2: return _M_input_error(); williamr@2: else if (__inext != _M_int_buf) { williamr@2: _M_ext_buf_converted = _M_ext_buf + (__enext - _M_ext_buf); williamr@2: this->setg(_M_int_buf, _M_int_buf, __inext); williamr@2: return traits_type::to_int_type(*_M_int_buf); williamr@2: } williamr@2: // We need to go around the loop again to get more external characters. williamr@4: } williamr@2: } williamr@2: williamr@2: //---------------------------------------- williamr@2: // Helper functions for output williamr@2: williamr@2: // This member function is called if there is an error during output. williamr@2: // It puts the filebuf in error mode, clear the put area buffer, and williamr@2: // returns eof. Error mode is sticky; it is cleared only by close or williamr@2: // seek. williamr@2: template williamr@2: __BF_int_type__ williamr@4: basic_filebuf<_CharT, _Traits>::_M_output_error() { williamr@2: _M_in_output_mode = false; williamr@2: _M_in_input_mode = false; williamr@2: _M_in_error_mode = true; williamr@2: this->setp(0, 0); williamr@2: return traits_type::eof(); williamr@2: } williamr@2: williamr@2: williamr@2: // Write whatever sequence of characters is necessary to get back to williamr@2: // the initial shift state. This function overwrites the external williamr@2: // buffer, changes the external file position, and changes the state. williamr@2: // Precondition: the internal buffer is empty. williamr@2: template williamr@4: bool basic_filebuf<_CharT, _Traits>::_M_unshift() { williamr@2: if (_M_in_output_mode && !_M_constant_width) { williamr@2: typename _Codecvt::result __status; williamr@2: do { williamr@2: char* __enext = _M_ext_buf; williamr@2: __status = _M_codecvt->unshift(_M_state, williamr@2: _M_ext_buf, _M_ext_buf_EOS, __enext); williamr@2: if (__status == _Codecvt::noconv || williamr@2: (__enext == _M_ext_buf && __status == _Codecvt::ok)) williamr@2: return true; williamr@2: else if (__status == _Codecvt::error) williamr@2: return false; williamr@2: else if (!_M_write(_M_ext_buf, __enext - _M_ext_buf)) williamr@2: return false; williamr@4: } while (__status == _Codecvt::partial); williamr@2: } williamr@2: williamr@2: return true; williamr@2: } williamr@2: williamr@2: williamr@2: //---------------------------------------- williamr@2: // Helper functions for buffer allocation and deallocation williamr@2: williamr@2: // This member function is called when we're initializing a filebuf's williamr@2: // internal and external buffers. The argument is the size of the williamr@2: // internal buffer; the external buffer is sized using the character williamr@2: // width in the current encoding. Preconditions: the buffers are currently williamr@4: // null. __n >= 1. __buf is either a null pointer or a pointer to an williamr@2: // array show size is at least __n. williamr@2: williamr@2: // We need __n >= 1 for two different reasons. For input, the base williamr@4: // class always needs a buffer because of the semantics of underflow(). williamr@2: // For output, we want to have an internal buffer that's larger by one williamr@4: // element than the buffer that the base class knows about. (See williamr@2: // basic_filebuf<>::overflow() for the reason.) williamr@2: template williamr@4: bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers(_CharT* __buf, streamsize __n) { williamr@4: //The major hypothesis in the following implementation is that size_t is unsigned. williamr@4: //We also need streamsize byte representation to be larger or equal to the int williamr@4: //representation to correctly store the encoding information. williamr@4: _STLP_STATIC_ASSERT(!numeric_limits::is_signed && williamr@4: sizeof(streamsize) >= sizeof(int)) williamr@2: williamr@2: if (__buf == 0) { williamr@4: streamsize __bufsize = __n * sizeof(_CharT); williamr@4: //We first check that the streamsize representation can't overflow a size_t one. williamr@4: //If it can, we check that __bufsize is not higher than the size_t max value. williamr@4: if ((sizeof(streamsize) > sizeof(size_t)) && williamr@4: (__bufsize > __STATIC_CAST(streamsize, (numeric_limits::max)()))) williamr@4: return false; williamr@4: _M_int_buf = __STATIC_CAST(_CharT*, malloc(__STATIC_CAST(size_t, __bufsize))); williamr@4: if (!_M_int_buf) williamr@2: return false; williamr@2: _M_int_buf_dynamic = true; williamr@2: } williamr@2: else { williamr@2: _M_int_buf = __buf; williamr@2: _M_int_buf_dynamic = false; williamr@2: } williamr@2: williamr@4: streamsize __ebufsiz = (max)(__n * __STATIC_CAST(streamsize, _M_width), williamr@4: __STATIC_CAST(streamsize, _M_codecvt->max_length())); williamr@4: _M_ext_buf = 0; williamr@4: if ((sizeof(streamsize) < sizeof(size_t)) || williamr@4: ((sizeof(streamsize) == sizeof(size_t)) && numeric_limits::is_signed) || williamr@4: (__ebufsiz <= __STATIC_CAST(streamsize, (numeric_limits::max)()))) { williamr@4: _M_ext_buf = __STATIC_CAST(char*, malloc(__STATIC_CAST(size_t, __ebufsiz))); williamr@4: } williamr@4: williamr@2: if (!_M_ext_buf) { williamr@2: _M_deallocate_buffers(); williamr@2: return false; williamr@2: } williamr@2: williamr@4: _M_int_buf_EOS = _M_int_buf + __STATIC_CAST(ptrdiff_t, __n); williamr@4: _M_ext_buf_EOS = _M_ext_buf + __STATIC_CAST(ptrdiff_t, __ebufsiz); williamr@2: return true; williamr@2: } williamr@2: williamr@2: // Abbreviation for the most common case. williamr@2: template williamr@4: bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers() { williamr@2: // Choose a buffer that's at least 4096 characters long and that's a williamr@2: // multiple of the page size. williamr@2: streamsize __default_bufsiz = williamr@2: ((_M_base.__page_size() + 4095UL) / _M_base.__page_size()) * _M_base.__page_size(); williamr@2: return _M_allocate_buffers(0, __default_bufsiz); williamr@2: } williamr@2: williamr@2: template williamr@4: void basic_filebuf<_CharT, _Traits>::_M_deallocate_buffers() { williamr@2: if (_M_int_buf_dynamic) williamr@2: free(_M_int_buf); williamr@2: free(_M_ext_buf); williamr@2: _M_int_buf = 0; williamr@2: _M_int_buf_EOS = 0; williamr@2: _M_ext_buf = 0; williamr@2: _M_ext_buf_EOS = 0; williamr@2: } williamr@2: williamr@2: williamr@2: //---------------------------------------- williamr@2: // Helper functiosn for seek and imbue williamr@2: williamr@2: template williamr@2: bool basic_filebuf<_CharT, _Traits>::_M_seek_init(bool __do_unshift) { williamr@2: // If we're in error mode, leave it. williamr@2: _M_in_error_mode = false; williamr@4: williamr@2: // Flush the output buffer if we're in output mode, and (conditionally) williamr@2: // emit an unshift sequence. williamr@2: if (_M_in_output_mode) { williamr@2: bool __ok = !traits_type::eq_int_type(this->overflow(traits_type::eof()), williamr@2: traits_type::eof()); williamr@2: if (__do_unshift) williamr@2: __ok = __ok && this->_M_unshift(); williamr@2: if (!__ok) { williamr@2: _M_in_output_mode = false; williamr@2: _M_in_error_mode = true; williamr@2: this->setp(0, 0); williamr@2: return false; williamr@2: } williamr@2: } williamr@2: williamr@2: // Discard putback characters, if any. williamr@2: if (_M_in_input_mode && _M_in_putback_mode) williamr@2: _M_exit_putback_mode(); williamr@2: williamr@2: return true; williamr@2: } williamr@2: williamr@2: williamr@4: /* Change the filebuf's locale. This member function has no effect williamr@4: * unless it is called before any I/O is performed on the stream. williamr@4: * This function is called on construction and on an imbue call. In the williamr@4: * case of the construction the codecvt facet might be a custom one if williamr@4: * the basic_filebuf user has instanciate it with a custom char_traits. williamr@4: * The user will have to call imbue before any I/O operation. williamr@4: */ williamr@2: template williamr@4: void basic_filebuf<_CharT, _Traits>::_M_setup_codecvt(const locale& __loc, bool __on_imbue) { williamr@4: if (has_facet<_Codecvt>(__loc)) { williamr@4: _M_codecvt = &use_facet<_Codecvt>(__loc) ; williamr@4: int __encoding = _M_codecvt->encoding(); williamr@2: williamr@4: _M_width = (max)(__encoding, 1); williamr@4: _M_max_width = _M_codecvt->max_length(); williamr@4: _M_constant_width = __encoding > 0; williamr@4: _M_always_noconv = _M_codecvt->always_noconv(); williamr@4: } williamr@4: else { williamr@4: _M_codecvt = 0; williamr@4: _M_width = _M_max_width = 1; williamr@4: _M_constant_width = _M_always_noconv = false; williamr@4: if (__on_imbue) { williamr@4: //This call will generate an exception reporting the problem. williamr@4: use_facet<_Codecvt>(__loc); williamr@4: } williamr@4: } williamr@2: } williamr@2: williamr@2: _STLP_END_NAMESPACE williamr@2: williamr@2: # undef __BF_int_type__ williamr@2: # undef __BF_pos_type__ williamr@2: # undef __BF_off_type__ williamr@2: williamr@2: #endif /* _STLP_FSTREAM_C */ williamr@2: williamr@4: // Local Variables: williamr@4: // mode:C++ williamr@4: // End: