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