epoc32/include/tools/stlport/stl/_fstream.c
branchSymbian3
changeset 4 837f303aceeb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/tools/stlport/stl/_fstream.c	Wed Mar 31 12:33:34 2010 +0100
     1.3 @@ -0,0 +1,737 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996,1997
     1.6 + * Silicon Graphics Computer Systems, Inc.
     1.7 + *
     1.8 + * Copyright (c) 1999
     1.9 + * Boris Fomitchev
    1.10 + *
    1.11 + * This material is provided "as is", with absolutely no warranty expressed
    1.12 + * or implied. Any use is at your own risk.
    1.13 + *
    1.14 + * Permission to use or copy this software for any purpose is hereby granted
    1.15 + * without fee, provided the above notices are retained on all copies.
    1.16 + * Permission to modify the code and to distribute modified code is granted,
    1.17 + * provided the above notices are retained, and a notice that the code was
    1.18 + * modified is included with the above copyright notice.
    1.19 + *
    1.20 + */
    1.21 +#ifndef _STLP_FSTREAM_C
    1.22 +#define _STLP_FSTREAM_C
    1.23 +
    1.24 +#ifndef _STLP_INTERNAL_FSTREAM_H
    1.25 +#  include <stl/_fstream.h>
    1.26 +#endif
    1.27 +
    1.28 +#ifndef _STLP_INTERNAL_LIMITS
    1.29 +#  include <stl/_limits.h>
    1.30 +#endif
    1.31 +
    1.32 +_STLP_BEGIN_NAMESPACE
    1.33 +
    1.34 +# if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
    1.35 +// no wchar_t is supported for this mode
    1.36 +# define __BF_int_type__ int
    1.37 +# define __BF_pos_type__ streampos
    1.38 +# define __BF_off_type__ streamoff
    1.39 +# else
    1.40 +# define __BF_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
    1.41 +# define __BF_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::pos_type
    1.42 +# define __BF_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::off_type
    1.43 +# endif
    1.44 +
    1.45 +
    1.46 +//----------------------------------------------------------------------
    1.47 +// Public basic_filebuf<> member functions
    1.48 +
    1.49 +template <class _CharT, class _Traits>
    1.50 +basic_filebuf<_CharT, _Traits>::basic_filebuf()
    1.51 +     :  basic_streambuf<_CharT, _Traits>(), _M_base(),
    1.52 +    _M_constant_width(false), _M_always_noconv(false),
    1.53 +    _M_int_buf_dynamic(false),
    1.54 +    _M_in_input_mode(false), _M_in_output_mode(false),
    1.55 +    _M_in_error_mode(false), _M_in_putback_mode(false),
    1.56 +    _M_int_buf(0), _M_int_buf_EOS(0),
    1.57 +    _M_ext_buf(0), _M_ext_buf_EOS(0),
    1.58 +    _M_ext_buf_converted(0), _M_ext_buf_end(0),
    1.59 +    _M_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
    1.60 +    _M_end_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
    1.61 +    _M_mmap_base(0), _M_mmap_len(0),
    1.62 +    _M_saved_eback(0), _M_saved_gptr(0), _M_saved_egptr(0),
    1.63 +    _M_codecvt(0),
    1.64 +    _M_width(1), _M_max_width(1)
    1.65 +{
    1.66 +  this->_M_setup_codecvt(locale(), false);
    1.67 +}
    1.68 +
    1.69 +template <class _CharT, class _Traits>
    1.70 +basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
    1.71 +  this->close();
    1.72 +  _M_deallocate_buffers();
    1.73 +}
    1.74 +
    1.75 +
    1.76 +template <class _CharT, class _Traits>
    1.77 +_STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
    1.78 +basic_filebuf<_CharT, _Traits>::underflow() {
    1.79 +  return _Underflow<_CharT, _Traits>::_M_doit(this);
    1.80 +}
    1.81 +
    1.82 +template <class _CharT, class _Traits>
    1.83 +basic_filebuf<_CharT, _Traits>*
    1.84 +basic_filebuf<_CharT, _Traits>::close() {
    1.85 +  bool __ok = this->is_open();
    1.86 +
    1.87 +  if (_M_in_output_mode) {
    1.88 +    __ok = __ok && !_Traits::eq_int_type(this->overflow(traits_type::eof()),
    1.89 +                                         traits_type::eof());
    1.90 +    __ok == __ok && this->_M_unshift();
    1.91 +  }
    1.92 +  else if (_M_in_input_mode)
    1.93 +      this->_M_exit_input_mode();
    1.94 +
    1.95 +  // Note order of arguments.  We close the file even if __ok is false.
    1.96 +  __ok = _M_base._M_close() && __ok;
    1.97 +
    1.98 +  // Restore the initial state, except that we don't deallocate the buffer
    1.99 +  // or mess with the cached codecvt information.
   1.100 +  _M_state = _M_end_state = _State_type();
   1.101 +  _M_ext_buf_converted = _M_ext_buf_end = 0;
   1.102 +
   1.103 +  _M_mmap_base = 0;
   1.104 +  _M_mmap_len = 0;
   1.105 +
   1.106 +  this->setg(0, 0, 0);
   1.107 +  this->setp(0, 0);
   1.108 +
   1.109 +  _M_saved_eback = _M_saved_gptr = _M_saved_egptr = 0;
   1.110 +
   1.111 +  _M_in_input_mode = _M_in_output_mode = _M_in_error_mode = _M_in_putback_mode
   1.112 +    = false;
   1.113 +
   1.114 +  return __ok ? this : 0;
   1.115 +}
   1.116 +
   1.117 +// This member function is called whenever we exit input mode.
   1.118 +// It unmaps the memory-mapped file, if any, and sets
   1.119 +// _M_in_input_mode to false.
   1.120 +template <class _CharT, class _Traits>
   1.121 +void basic_filebuf<_CharT, _Traits>::_M_exit_input_mode() {
   1.122 +   if (_M_mmap_base != 0)
   1.123 +     _M_base._M_unmap(_M_mmap_base, _M_mmap_len);
   1.124 +   _M_in_input_mode = false;
   1.125 +   _M_mmap_base = 0;
   1.126 +}
   1.127 +
   1.128 +
   1.129 +//----------------------------------------------------------------------
   1.130 +// basic_filebuf<> overridden protected virtual member functions
   1.131 +
   1.132 +template <class _CharT, class _Traits>
   1.133 +streamsize basic_filebuf<_CharT, _Traits>::showmanyc() {
   1.134 +  // Is there any possibility that reads can succeed?
   1.135 +  if (!this->is_open() || _M_in_output_mode || _M_in_error_mode)
   1.136 +    return -1;
   1.137 +  else if (_M_in_putback_mode)
   1.138 +    return this->egptr() - this->gptr();
   1.139 +  else if (_M_constant_width) {
   1.140 +    streamoff __pos  = _M_base._M_seek(0, ios_base::cur);
   1.141 +    streamoff __size = _M_base._M_file_size();
   1.142 +    return __pos >= 0 && __size > __pos ? __size - __pos : 0;
   1.143 +  }
   1.144 +  else
   1.145 +    return 0;
   1.146 +}
   1.147 +
   1.148 +
   1.149 +// Make a putback position available, if necessary, by switching to a
   1.150 +// special internal buffer used only for putback.  The buffer is
   1.151 +// [_M_pback_buf, _M_pback_buf + _S_pback_buf_size), but the base
   1.152 +// class only sees a piece of it at a time.  (We want to make sure
   1.153 +// that we don't try to read a character that hasn't been initialized.)
   1.154 +// The end of the putback buffer is always _M_pback_buf + _S_pback_buf_size,
   1.155 +// but the beginning is usually not _M_pback_buf.
   1.156 +template <class _CharT, class _Traits>
   1.157 +__BF_int_type__
   1.158 +basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
   1.159 +  const int_type __eof = traits_type::eof();
   1.160 +
   1.161 +  // If we aren't already in input mode, pushback is impossible.
   1.162 +  if (!_M_in_input_mode)
   1.163 +    return __eof;
   1.164 +
   1.165 +  // We can use the ordinary get buffer if there's enough space, and
   1.166 +  // if it's a buffer that we're allowed to write to.
   1.167 +  if (this->gptr() != this->eback() &&
   1.168 +      (traits_type::eq_int_type(__c, __eof) ||
   1.169 +       traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]) ||
   1.170 +       !_M_mmap_base)) {
   1.171 +    this->gbump(-1);
   1.172 +    if (traits_type::eq_int_type(__c, __eof) ||
   1.173 +        traits_type::eq(traits_type::to_char_type(__c), *this->gptr()))
   1.174 +      return traits_type::to_int_type(*this->gptr());
   1.175 +  }
   1.176 +  else if (!traits_type::eq_int_type(__c, __eof)) {
   1.177 +    // Are we in the putback buffer already?
   1.178 +    _CharT* __pback_end = _M_pback_buf + __STATIC_CAST(int,_S_pback_buf_size);
   1.179 +    if (_M_in_putback_mode) {
   1.180 +      // Do we have more room in the putback buffer?
   1.181 +      if (this->eback() != _M_pback_buf)
   1.182 +        this->setg(this->egptr() - 1, this->egptr() - 1, __pback_end);
   1.183 +      else
   1.184 +        return __eof;           // No more room in the buffer, so fail.
   1.185 +    }
   1.186 +    else {                      // We're not yet in the putback buffer.
   1.187 +      _M_saved_eback = this->eback();
   1.188 +      _M_saved_gptr  = this->gptr();
   1.189 +      _M_saved_egptr = this->egptr();
   1.190 +      this->setg(__pback_end - 1, __pback_end - 1, __pback_end);
   1.191 +      _M_in_putback_mode = true;
   1.192 +    }
   1.193 +  }
   1.194 +  else
   1.195 +    return __eof;
   1.196 +
   1.197 +  // We have made a putback position available.  Assign to it, and return.
   1.198 +  *this->gptr() = traits_type::to_char_type(__c);
   1.199 +  return __c;
   1.200 +}
   1.201 +
   1.202 +// This member function flushes the put area, and also outputs the
   1.203 +// character __c (unless __c is eof).  Invariant: we always leave room
   1.204 +// in the internal buffer for one character more than the base class knows
   1.205 +// about.  We see the internal buffer as [_M_int_buf, _M_int_buf_EOS), but
   1.206 +// the base class only sees [_M_int_buf, _M_int_buf_EOS - 1).
   1.207 +template <class _CharT, class _Traits>
   1.208 +__BF_int_type__
   1.209 +basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
   1.210 +  // Switch to output mode, if necessary.
   1.211 +  if (!_M_in_output_mode)
   1.212 +    if (!_M_switch_to_output_mode())
   1.213 +      return traits_type::eof();
   1.214 +
   1.215 +  _CharT* __ibegin = this->_M_int_buf;
   1.216 +  _CharT* __iend   = this->pptr();
   1.217 +  this->setp(_M_int_buf, _M_int_buf_EOS - 1);
   1.218 +
   1.219 +  // Put __c at the end of the internal buffer.
   1.220 +  if (!traits_type::eq_int_type(__c, traits_type::eof()))
   1.221 +    *__iend++ = _Traits::to_char_type(__c);
   1.222 +
   1.223 +  // For variable-width encodings, output may take more than one pass.
   1.224 +  while (__ibegin != __iend) {
   1.225 +    const _CharT* __inext = __ibegin;
   1.226 +    char* __enext         = _M_ext_buf;
   1.227 +    typename _Codecvt::result __status
   1.228 +      = _M_codecvt->out(_M_state, __ibegin, __iend, __inext,
   1.229 +                        _M_ext_buf, _M_ext_buf_EOS, __enext);
   1.230 +    if (__status == _Codecvt::noconv) {
   1.231 +      return _Noconv_output<_Traits>::_M_doit(this, __ibegin, __iend)
   1.232 +        ? traits_type::not_eof(__c)
   1.233 +        : _M_output_error();
   1.234 +    }
   1.235 +
   1.236 +    // For a constant-width encoding we know that the external buffer
   1.237 +    // is large enough, so failure to consume the entire internal buffer
   1.238 +    // or to produce the correct number of external characters, is an error.
   1.239 +    // For a variable-width encoding, however, we require only that we
   1.240 +    // consume at least one internal character
   1.241 +    else if (__status != _Codecvt::error &&
   1.242 +             (((__inext == __iend) &&
   1.243 +               (__enext - _M_ext_buf == _M_width * (__iend - __ibegin))) ||
   1.244 +              (!_M_constant_width && __inext != __ibegin))) {
   1.245 +        // We successfully converted part or all of the internal buffer.
   1.246 +      ptrdiff_t __n = __enext - _M_ext_buf;
   1.247 +      if (_M_write(_M_ext_buf, __n))
   1.248 +        __ibegin += __inext - __ibegin;
   1.249 +      else
   1.250 +        return _M_output_error();
   1.251 +    }
   1.252 +    else
   1.253 +      return _M_output_error();
   1.254 +  }
   1.255 +
   1.256 +  return traits_type::not_eof(__c);
   1.257 +}
   1.258 +
   1.259 +// This member function must be called before any I/O has been
   1.260 +// performed on the stream, otherwise it has no effect.
   1.261 +//
   1.262 +// __buf == 0 && __n == 0 means to make this stream unbuffered.
   1.263 +// __buf != 0 && __n > 0 means to use __buf as the stream's internal
   1.264 +// buffer, rather than the buffer that would otherwise be allocated
   1.265 +// automatically.  __buf must be a pointer to an array of _CharT whose
   1.266 +// size is at least __n.
   1.267 +template <class _CharT, class _Traits>
   1.268 +basic_streambuf<_CharT, _Traits>*
   1.269 +basic_filebuf<_CharT, _Traits>::setbuf(_CharT* __buf, streamsize __n) {
   1.270 +  if (!_M_in_input_mode &&! _M_in_output_mode && !_M_in_error_mode &&
   1.271 +      _M_int_buf == 0) {
   1.272 +    if (__buf == 0 && __n == 0)
   1.273 +      _M_allocate_buffers(0, 1);
   1.274 +    else if (__buf != 0 && __n > 0)
   1.275 +      _M_allocate_buffers(__buf, __n);
   1.276 +  }
   1.277 +  return this;
   1.278 +}
   1.279 +
   1.280 +template <class _CharT, class _Traits>
   1.281 +__BF_pos_type__
   1.282 +basic_filebuf<_CharT, _Traits>::seekoff(off_type __off,
   1.283 +                                        ios_base::seekdir __whence,
   1.284 +                                        ios_base::openmode /* dummy */) {
   1.285 +  if (this->is_open() &&
   1.286 +      (__off == 0 || (_M_constant_width && this->_M_base._M_in_binary_mode()))) {
   1.287 +
   1.288 +    if (!_M_seek_init(__off != 0 || __whence != ios_base::cur))
   1.289 +      return pos_type(-1);
   1.290 +
   1.291 +    // Seek to beginning or end, regardless of whether we're in input mode.
   1.292 +    if (__whence == ios_base::beg || __whence == ios_base::end)
   1.293 +      return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
   1.294 +                            _State_type());
   1.295 +
   1.296 +    // Seek relative to current position. Complicated if we're in input mode.
   1.297 +    else if (__whence == ios_base::cur) {
   1.298 +      if (!_M_in_input_mode)
   1.299 +        return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
   1.300 +                              _State_type());
   1.301 +      else if (_M_mmap_base != 0) {
   1.302 +        // __off is relative to gptr().  We need to do a bit of arithmetic
   1.303 +        // to get an offset relative to the external file pointer.
   1.304 +        streamoff __adjust = _M_mmap_len - (this->gptr() - (_CharT*) _M_mmap_base);
   1.305 +
   1.306 +        // if __off == 0, we do not need to exit input mode and to shift file pointer
   1.307 +        return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __adjust)
   1.308 +                          : _M_seek_return(_M_base._M_seek(__off - __adjust, ios_base::cur), _State_type());
   1.309 +      }
   1.310 +      else if (_M_constant_width) { // Get or set the position.
   1.311 +        streamoff __iadj = _M_width * (this->gptr() - this->eback());
   1.312 +
   1.313 +        // Compensate for offset relative to gptr versus offset relative
   1.314 +        // to external pointer.  For a text-oriented stream, where the
   1.315 +        // compensation is more than just pointer arithmetic, we may get
   1.316 +        // but not set the current position.
   1.317 +
   1.318 +        if (__iadj <= _M_ext_buf_end - _M_ext_buf) {
   1.319 +          streamoff __eadj =  _M_base._M_get_offset(_M_ext_buf + __STATIC_CAST(ptrdiff_t, __iadj), _M_ext_buf_end);
   1.320 +
   1.321 +          return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __eadj)
   1.322 +                            : _M_seek_return(_M_base._M_seek(__off - __eadj, ios_base::cur), _State_type());
   1.323 +        }
   1.324 +      } else {                    // Get the position.  Encoding is var width.
   1.325 +        // Get position in internal buffer.
   1.326 +        ptrdiff_t __ipos = this->gptr() - this->eback();
   1.327 +
   1.328 +        // Get corresponding position in external buffer.
   1.329 +        _State_type __state = _M_state;
   1.330 +        int __epos = _M_codecvt->length(__state, _M_ext_buf, _M_ext_buf_end,
   1.331 +                                        __ipos);
   1.332 +
   1.333 +        if (__epos >= 0) {
   1.334 +          // Sanity check (expensive): make sure __epos is the right answer.
   1.335 +          _State_type __tmp_state = _M_state;
   1.336 +          _Filebuf_Tmp_Buf<_CharT> __buf(__ipos);
   1.337 +          _CharT* __ibegin = __buf._M_ptr;
   1.338 +          _CharT* __inext  = __ibegin;
   1.339 +
   1.340 +          const char* __dummy;
   1.341 +          typename _Codecvt::result __status
   1.342 +            = _M_codecvt->in(__tmp_state,
   1.343 +                             _M_ext_buf, _M_ext_buf + __epos, __dummy,
   1.344 +                             __ibegin, __ibegin + __ipos, __inext);
   1.345 +          if (__status != _Codecvt::error &&
   1.346 +              (__status == _Codecvt::noconv ||
   1.347 +               (__inext == __ibegin + __ipos &&
   1.348 +                equal(this->eback(), this->gptr(), __ibegin, _STLP_PRIV _Eq_traits<traits_type>())))) {
   1.349 +            // Get the current position (at the end of the external buffer),
   1.350 +            // then adjust it.  Again, it might be a text-oriented stream.
   1.351 +            streamoff __cur = _M_base._M_seek(0, ios_base::cur);
   1.352 +            streamoff __adj =
   1.353 +              _M_base._M_get_offset(_M_ext_buf, _M_ext_buf + __epos) -
   1.354 +              _M_base._M_get_offset(_M_ext_buf, _M_ext_buf_end);
   1.355 +            if (__cur != -1 && __cur + __adj >= 0)
   1.356 +              return __off == 0 ? pos_type(__cur + __adj)
   1.357 +                                : _M_seek_return(__cur + __adj, __state);
   1.358 +              //return _M_seek_return(__cur + __adj, __state);
   1.359 +          }
   1.360 +          // We failed the sanity check here.
   1.361 +        }
   1.362 +      }
   1.363 +    }
   1.364 +    // Unrecognized value for __whence here.
   1.365 +  }
   1.366 +
   1.367 +  return pos_type(-1);
   1.368 +}
   1.369 +
   1.370 +
   1.371 +template <class _CharT, class _Traits>
   1.372 +__BF_pos_type__
   1.373 +basic_filebuf<_CharT, _Traits>::seekpos(pos_type __pos,
   1.374 +                                        ios_base::openmode /* dummy */) {
   1.375 +  if (this->is_open()) {
   1.376 +    if (!_M_seek_init(true))
   1.377 +      return pos_type(-1);
   1.378 +
   1.379 +    streamoff __off = off_type(__pos);
   1.380 +    if (__off != -1 && _M_base._M_seek(__off, ios_base::beg) != -1) {
   1.381 +      _M_state = __pos.state();
   1.382 +      return _M_seek_return(__off, __pos.state());
   1.383 +    }
   1.384 +  }
   1.385 +
   1.386 +  return pos_type(-1);
   1.387 +}
   1.388 +
   1.389 +
   1.390 +template <class _CharT, class _Traits>
   1.391 +int basic_filebuf<_CharT, _Traits>::sync() {
   1.392 +  if (_M_in_output_mode)
   1.393 +    return traits_type::eq_int_type(this->overflow(traits_type::eof()),
   1.394 +                                    traits_type::eof()) ? -1 : 0;
   1.395 +  return 0;
   1.396 +}
   1.397 +
   1.398 +
   1.399 +// Change the filebuf's locale.  This member function has no effect
   1.400 +// unless it is called before any I/O is performed on the stream.
   1.401 +template <class _CharT, class _Traits>
   1.402 +void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
   1.403 +  if (!_M_in_input_mode && !_M_in_output_mode && !_M_in_error_mode) {
   1.404 +    this->_M_setup_codecvt(__loc);
   1.405 +  }
   1.406 +}
   1.407 +
   1.408 +//----------------------------------------------------------------------
   1.409 +// basic_filebuf<> helper functions.
   1.410 +
   1.411 +//----------------------------------------
   1.412 +// Helper functions for switching between modes.
   1.413 +
   1.414 +// This member function is called if we're performing the first I/O
   1.415 +// operation on a filebuf, or if we're performing an input operation
   1.416 +// immediately after a seek.
   1.417 +template <class _CharT, class _Traits>
   1.418 +bool basic_filebuf<_CharT, _Traits>::_M_switch_to_input_mode() {
   1.419 +  if (this->is_open() && (((int)_M_base.__o_mode() & (int)ios_base::in) != 0)
   1.420 +      && (_M_in_output_mode == 0) && (_M_in_error_mode == 0)) {
   1.421 +    if (!_M_int_buf && !_M_allocate_buffers())
   1.422 +      return false;
   1.423 +
   1.424 +    _M_ext_buf_converted = _M_ext_buf;
   1.425 +    _M_ext_buf_end       = _M_ext_buf;
   1.426 +
   1.427 +    _M_end_state    = _M_state;
   1.428 +
   1.429 +    _M_in_input_mode = true;
   1.430 +    return true;
   1.431 +  }
   1.432 +
   1.433 +  return false;
   1.434 +}
   1.435 +
   1.436 +
   1.437 +// This member function is called if we're performing the first I/O
   1.438 +// operation on a filebuf, or if we're performing an output operation
   1.439 +// immediately after a seek.
   1.440 +template <class _CharT, class _Traits>
   1.441 +bool basic_filebuf<_CharT, _Traits>::_M_switch_to_output_mode() {
   1.442 +  if (this->is_open() && (_M_base.__o_mode() & (int)ios_base::out) &&
   1.443 +      _M_in_input_mode == 0 && _M_in_error_mode == 0) {
   1.444 +
   1.445 +    if (!_M_int_buf && !_M_allocate_buffers())
   1.446 +      return false;
   1.447 +
   1.448 +    // In append mode, every write does an implicit seek to the end
   1.449 +    // of the file.  Whenever leaving output mode, the end of file
   1.450 +    // get put in the initial shift state.
   1.451 +    if (_M_base.__o_mode() & ios_base::app)
   1.452 +      _M_state = _State_type();
   1.453 +
   1.454 +    this->setp(_M_int_buf, _M_int_buf_EOS - 1);
   1.455 +    _M_in_output_mode = true;
   1.456 +    return true;
   1.457 +  }
   1.458 +
   1.459 +  return false;
   1.460 +}
   1.461 +
   1.462 +
   1.463 +//----------------------------------------
   1.464 +// Helper functions for input
   1.465 +
   1.466 +// This member function is called if there is an error during input.
   1.467 +// It puts the filebuf in error mode, clear the get area buffer, and
   1.468 +// returns eof.
   1.469 +// returns eof.  Error mode is sticky; it is cleared only by close or
   1.470 +// seek.
   1.471 +
   1.472 +template <class _CharT, class _Traits>
   1.473 +__BF_int_type__
   1.474 +basic_filebuf<_CharT, _Traits>::_M_input_error() {
   1.475 +   this->_M_exit_input_mode();
   1.476 +  _M_in_output_mode = false;
   1.477 +  _M_in_error_mode = true;
   1.478 +  this->setg(0, 0, 0);
   1.479 +  return traits_type::eof();
   1.480 +}
   1.481 +
   1.482 +template <class _CharT, class _Traits>
   1.483 +__BF_int_type__
   1.484 +basic_filebuf<_CharT, _Traits>::_M_underflow_aux() {
   1.485 +  // We have the state and file position from the end of the internal
   1.486 +  // buffer.  This round, they become the beginning of the internal buffer.
   1.487 +  _M_state    = _M_end_state;
   1.488 +
   1.489 +  // Fill the external buffer.  Start with any leftover characters that
   1.490 +  // didn't get converted last time.
   1.491 +  if (_M_ext_buf_end > _M_ext_buf_converted)
   1.492 +
   1.493 +    _M_ext_buf_end = copy(_M_ext_buf_converted, _M_ext_buf_end, _M_ext_buf);
   1.494 +    // boris : copy_backward did not work
   1.495 +    //_M_ext_buf_end = copy_backward(_M_ext_buf_converted, _M_ext_buf_end,
   1.496 +    //_M_ext_buf+ (_M_ext_buf_end - _M_ext_buf_converted));
   1.497 +  else
   1.498 +    _M_ext_buf_end = _M_ext_buf;
   1.499 +
   1.500 +  // Now fill the external buffer with characters from the file.  This is
   1.501 +  // a loop because occasionally we don't get enough external characters
   1.502 +  // to make progress.
   1.503 +  for (;;) {
   1.504 +    ptrdiff_t __n = _M_base._M_read(_M_ext_buf_end, _M_ext_buf_EOS - _M_ext_buf_end);
   1.505 +
   1.506 +    // Don't enter error mode for a failed read.  Error mode is sticky,
   1.507 +    // and we might succeed if we try again.
   1.508 +    if (__n <= 0)
   1.509 +      return traits_type::eof();
   1.510 +
   1.511 +    // Convert the external buffer to internal characters.
   1.512 +    _M_ext_buf_end += __n;
   1.513 +    const char*   __enext;
   1.514 +    _CharT* __inext;
   1.515 +
   1.516 +    typename _Codecvt::result __status
   1.517 +      = _M_codecvt->in(_M_end_state,
   1.518 +                       _M_ext_buf, _M_ext_buf_end, __enext,
   1.519 +                       _M_int_buf, _M_int_buf_EOS, __inext);
   1.520 +
   1.521 +    // Error conditions: (1) Return value of error.  (2) Producing internal
   1.522 +    // characters without consuming external characters.  (3) In fixed-width
   1.523 +    // encodings, producing an internal sequence whose length is inconsistent
   1.524 +    // with that of the internal sequence.  (4) Failure to produce any
   1.525 +    // characters if we have enough characters in the external buffer, where
   1.526 +    // "enough" means the largest possible width of a single character.
   1.527 +    if (__status == _Codecvt::noconv)
   1.528 +      return _Noconv_input<_Traits>::_M_doit(this);
   1.529 +    else if (__status == _Codecvt::error ||
   1.530 +             (__inext != _M_int_buf && __enext == _M_ext_buf) ||
   1.531 +             (_M_constant_width &&
   1.532 +              //         __inext - _M_int_buf != _M_width * (__enext - _M_ext_buf)) ||
   1.533 +              (__inext - _M_int_buf) *  _M_width != (__enext - _M_ext_buf)) ||
   1.534 +             (__inext == _M_int_buf && __enext - _M_ext_buf >= _M_max_width))
   1.535 +      return _M_input_error();
   1.536 +    else if (__inext != _M_int_buf) {
   1.537 +      _M_ext_buf_converted = _M_ext_buf + (__enext - _M_ext_buf);
   1.538 +      this->setg(_M_int_buf, _M_int_buf, __inext);
   1.539 +      return traits_type::to_int_type(*_M_int_buf);
   1.540 +    }
   1.541 +    // We need to go around the loop again to get more external characters.
   1.542 +  }
   1.543 +}
   1.544 +
   1.545 +//----------------------------------------
   1.546 +// Helper functions for output
   1.547 +
   1.548 +// This member function is called if there is an error during output.
   1.549 +// It puts the filebuf in error mode, clear the put area buffer, and
   1.550 +// returns eof.  Error mode is sticky; it is cleared only by close or
   1.551 +// seek.
   1.552 +template <class _CharT, class _Traits>
   1.553 +__BF_int_type__
   1.554 +basic_filebuf<_CharT, _Traits>::_M_output_error() {
   1.555 +  _M_in_output_mode = false;
   1.556 +  _M_in_input_mode = false;
   1.557 +  _M_in_error_mode = true;
   1.558 +  this->setp(0, 0);
   1.559 +  return traits_type::eof();
   1.560 +}
   1.561 +
   1.562 +
   1.563 +// Write whatever sequence of characters is necessary to get back to
   1.564 +// the initial shift state.  This function overwrites the external
   1.565 +// buffer, changes the external file position, and changes the state.
   1.566 +// Precondition: the internal buffer is empty.
   1.567 +template <class _CharT, class _Traits>
   1.568 +bool basic_filebuf<_CharT, _Traits>::_M_unshift() {
   1.569 +  if (_M_in_output_mode && !_M_constant_width) {
   1.570 +    typename _Codecvt::result __status;
   1.571 +    do {
   1.572 +      char* __enext = _M_ext_buf;
   1.573 +      __status = _M_codecvt->unshift(_M_state,
   1.574 +                                     _M_ext_buf, _M_ext_buf_EOS, __enext);
   1.575 +      if (__status == _Codecvt::noconv ||
   1.576 +          (__enext == _M_ext_buf && __status == _Codecvt::ok))
   1.577 +        return true;
   1.578 +      else if (__status == _Codecvt::error)
   1.579 +        return false;
   1.580 +      else if (!_M_write(_M_ext_buf, __enext - _M_ext_buf))
   1.581 +        return false;
   1.582 +    } while (__status == _Codecvt::partial);
   1.583 +  }
   1.584 +
   1.585 +  return true;
   1.586 +}
   1.587 +
   1.588 +
   1.589 +//----------------------------------------
   1.590 +// Helper functions for buffer allocation and deallocation
   1.591 +
   1.592 +// This member function is called when we're initializing a filebuf's
   1.593 +// internal and external buffers.  The argument is the size of the
   1.594 +// internal buffer; the external buffer is sized using the character
   1.595 +// width in the current encoding.  Preconditions: the buffers are currently
   1.596 +// null.  __n >= 1.  __buf is either a null pointer or a pointer to an
   1.597 +// array show size is at least __n.
   1.598 +
   1.599 +// We need __n >= 1 for two different reasons.  For input, the base
   1.600 +// class always needs a buffer because of the semantics of underflow().
   1.601 +// For output, we want to have an internal buffer that's larger by one
   1.602 +// element than the buffer that the base class knows about.  (See
   1.603 +// basic_filebuf<>::overflow() for the reason.)
   1.604 +template <class _CharT, class _Traits>
   1.605 +bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers(_CharT* __buf, streamsize __n) {
   1.606 +  //The major hypothesis in the following implementation is that size_t is unsigned.
   1.607 +  //We also need streamsize byte representation to be larger or equal to the int
   1.608 +  //representation to correctly store the encoding information.
   1.609 +  _STLP_STATIC_ASSERT(!numeric_limits<size_t>::is_signed &&
   1.610 +                      sizeof(streamsize) >= sizeof(int))
   1.611 +
   1.612 +  if (__buf == 0) {
   1.613 +    streamsize __bufsize = __n * sizeof(_CharT);
   1.614 +    //We first check that the streamsize representation can't overflow a size_t one.
   1.615 +    //If it can, we check that __bufsize is not higher than the size_t max value.
   1.616 +    if ((sizeof(streamsize) > sizeof(size_t)) &&
   1.617 +        (__bufsize > __STATIC_CAST(streamsize, (numeric_limits<size_t>::max)())))
   1.618 +      return false;
   1.619 +    _M_int_buf = __STATIC_CAST(_CharT*, malloc(__STATIC_CAST(size_t, __bufsize)));
   1.620 +    if (!_M_int_buf)
   1.621 +      return false;
   1.622 +    _M_int_buf_dynamic = true;
   1.623 +  }
   1.624 +  else {
   1.625 +    _M_int_buf = __buf;
   1.626 +    _M_int_buf_dynamic = false;
   1.627 +  }
   1.628 +
   1.629 +  streamsize __ebufsiz = (max)(__n * __STATIC_CAST(streamsize, _M_width),
   1.630 +                               __STATIC_CAST(streamsize, _M_codecvt->max_length()));
   1.631 +  _M_ext_buf = 0;
   1.632 +  if ((sizeof(streamsize) < sizeof(size_t)) ||
   1.633 +      ((sizeof(streamsize) == sizeof(size_t)) && numeric_limits<streamsize>::is_signed) ||
   1.634 +      (__ebufsiz <= __STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()))) {
   1.635 +    _M_ext_buf = __STATIC_CAST(char*, malloc(__STATIC_CAST(size_t, __ebufsiz)));
   1.636 +  }
   1.637 +
   1.638 +  if (!_M_ext_buf) {
   1.639 +    _M_deallocate_buffers();
   1.640 +    return false;
   1.641 +  }
   1.642 +
   1.643 +  _M_int_buf_EOS = _M_int_buf + __STATIC_CAST(ptrdiff_t, __n);
   1.644 +  _M_ext_buf_EOS = _M_ext_buf + __STATIC_CAST(ptrdiff_t, __ebufsiz);
   1.645 +  return true;
   1.646 +}
   1.647 +
   1.648 +// Abbreviation for the most common case.
   1.649 +template <class _CharT, class _Traits>
   1.650 +bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers() {
   1.651 +  // Choose a buffer that's at least 4096 characters long and that's a
   1.652 +  // multiple of the page size.
   1.653 +  streamsize __default_bufsiz =
   1.654 +    ((_M_base.__page_size() + 4095UL) / _M_base.__page_size()) * _M_base.__page_size();
   1.655 +  return _M_allocate_buffers(0, __default_bufsiz);
   1.656 +}
   1.657 +
   1.658 +template <class _CharT, class _Traits>
   1.659 +void basic_filebuf<_CharT, _Traits>::_M_deallocate_buffers() {
   1.660 +  if (_M_int_buf_dynamic)
   1.661 +    free(_M_int_buf);
   1.662 +  free(_M_ext_buf);
   1.663 +  _M_int_buf     = 0;
   1.664 +  _M_int_buf_EOS = 0;
   1.665 +  _M_ext_buf     = 0;
   1.666 +  _M_ext_buf_EOS = 0;
   1.667 +}
   1.668 +
   1.669 +
   1.670 +//----------------------------------------
   1.671 +// Helper functiosn for seek and imbue
   1.672 +
   1.673 +template <class _CharT, class _Traits>
   1.674 +bool basic_filebuf<_CharT, _Traits>::_M_seek_init(bool __do_unshift) {
   1.675 +  // If we're in error mode, leave it.
   1.676 +   _M_in_error_mode = false;
   1.677 +
   1.678 +  // Flush the output buffer if we're in output mode, and (conditionally)
   1.679 +  // emit an unshift sequence.
   1.680 +  if (_M_in_output_mode) {
   1.681 +    bool __ok = !traits_type::eq_int_type(this->overflow(traits_type::eof()),
   1.682 +                                          traits_type::eof());
   1.683 +    if (__do_unshift)
   1.684 +      __ok = __ok && this->_M_unshift();
   1.685 +    if (!__ok) {
   1.686 +      _M_in_output_mode = false;
   1.687 +      _M_in_error_mode = true;
   1.688 +      this->setp(0, 0);
   1.689 +      return false;
   1.690 +    }
   1.691 +  }
   1.692 +
   1.693 +  // Discard putback characters, if any.
   1.694 +  if (_M_in_input_mode && _M_in_putback_mode)
   1.695 +    _M_exit_putback_mode();
   1.696 +
   1.697 +  return true;
   1.698 +}
   1.699 +
   1.700 +
   1.701 +/* Change the filebuf's locale.  This member function has no effect
   1.702 + * unless it is called before any I/O is performed on the stream.
   1.703 + * This function is called on construction and on an imbue call. In the
   1.704 + * case of the construction the codecvt facet might be a custom one if
   1.705 + * the basic_filebuf user has instanciate it with a custom char_traits.
   1.706 + * The user will have to call imbue before any I/O operation.
   1.707 + */
   1.708 +template <class _CharT, class _Traits>
   1.709 +void basic_filebuf<_CharT, _Traits>::_M_setup_codecvt(const locale& __loc, bool __on_imbue) {
   1.710 +  if (has_facet<_Codecvt>(__loc)) {
   1.711 +    _M_codecvt = &use_facet<_Codecvt>(__loc) ;
   1.712 +    int __encoding    = _M_codecvt->encoding();
   1.713 +
   1.714 +    _M_width          = (max)(__encoding, 1);
   1.715 +    _M_max_width      = _M_codecvt->max_length();
   1.716 +    _M_constant_width = __encoding > 0;
   1.717 +    _M_always_noconv  = _M_codecvt->always_noconv();
   1.718 +  }
   1.719 +  else {
   1.720 +    _M_codecvt = 0;
   1.721 +    _M_width = _M_max_width = 1;
   1.722 +    _M_constant_width = _M_always_noconv  = false;
   1.723 +    if (__on_imbue) {
   1.724 +      //This call will generate an exception reporting the problem.
   1.725 +      use_facet<_Codecvt>(__loc);
   1.726 +    }
   1.727 +  }
   1.728 +}
   1.729 +
   1.730 +_STLP_END_NAMESPACE
   1.731 +
   1.732 +# undef __BF_int_type__
   1.733 +# undef __BF_pos_type__
   1.734 +# undef __BF_off_type__
   1.735 +
   1.736 +#endif /* _STLP_FSTREAM_C */
   1.737 +
   1.738 +// Local Variables:
   1.739 +// mode:C++
   1.740 +// End: