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