/home/ntakagi/work/STLport-5.1.5/stlport/stl/_fstream.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1996,1997
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 #ifndef _STLP_FSTREAM_C
00019 #define _STLP_FSTREAM_C
00020 
00021 #ifndef _STLP_INTERNAL_FSTREAM_H
00022 #  include <stl/_fstream.h>
00023 #endif
00024 
00025 #ifndef _STLP_INTERNAL_LIMITS
00026 #  include <stl/_limits.h>
00027 #endif
00028 
00029 _STLP_BEGIN_NAMESPACE
00030 
00031 # if defined ( _STLP_NESTED_TYPE_PARAM_BUG )
00032 // no wchar_t is supported for this mode
00033 # define __BF_int_type__ int
00034 # define __BF_pos_type__ streampos
00035 # define __BF_off_type__ streamoff
00036 # else
00037 # define __BF_int_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
00038 # define __BF_pos_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::pos_type
00039 # define __BF_off_type__ _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::off_type
00040 # endif
00041 
00042 
00043 //----------------------------------------------------------------------
00044 // Public basic_filebuf<> member functions
00045 
00046 template <class _CharT, class _Traits>
00047 basic_filebuf<_CharT, _Traits>::basic_filebuf()
00048      :  basic_streambuf<_CharT, _Traits>(), _M_base(),
00049     _M_constant_width(false), _M_always_noconv(false),
00050     _M_int_buf_dynamic(false),
00051     _M_in_input_mode(false), _M_in_output_mode(false),
00052     _M_in_error_mode(false), _M_in_putback_mode(false),
00053     _M_int_buf(0), _M_int_buf_EOS(0),
00054     _M_ext_buf(0), _M_ext_buf_EOS(0),
00055     _M_ext_buf_converted(0), _M_ext_buf_end(0),
00056     _M_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
00057     _M_end_state(_STLP_DEFAULT_CONSTRUCTED(_State_type)),
00058     _M_mmap_base(0), _M_mmap_len(0),
00059     _M_saved_eback(0), _M_saved_gptr(0), _M_saved_egptr(0),
00060     _M_codecvt(0),
00061     _M_width(1), _M_max_width(1)
00062 {
00063   this->_M_setup_codecvt(locale(), false);
00064 }
00065 
00066 template <class _CharT, class _Traits>
00067 basic_filebuf<_CharT, _Traits>::~basic_filebuf() {
00068   this->close();
00069   _M_deallocate_buffers();
00070 }
00071 
00072 
00073 template <class _CharT, class _Traits>
00074 _STLP_TYPENAME_ON_RETURN_TYPE basic_filebuf<_CharT, _Traits>::int_type
00075 basic_filebuf<_CharT, _Traits>::underflow() {
00076   return _Underflow<_CharT, _Traits>::_M_doit(this);
00077 }
00078 
00079 template <class _CharT, class _Traits>
00080 basic_filebuf<_CharT, _Traits>*
00081 basic_filebuf<_CharT, _Traits>::close() {
00082   bool __ok = this->is_open();
00083 
00084   if (_M_in_output_mode) {
00085     __ok = __ok && !_Traits::eq_int_type(this->overflow(traits_type::eof()),
00086                                          traits_type::eof());
00087     __ok == __ok && this->_M_unshift();
00088   }
00089   else if (_M_in_input_mode)
00090       this->_M_exit_input_mode();
00091 
00092   // Note order of arguments.  We close the file even if __ok is false.
00093   __ok = _M_base._M_close() && __ok;
00094 
00095   // Restore the initial state, except that we don't deallocate the buffer
00096   // or mess with the cached codecvt information.
00097   _M_state = _M_end_state = _State_type();
00098   _M_ext_buf_converted = _M_ext_buf_end = 0;
00099 
00100   _M_mmap_base = 0;
00101   _M_mmap_len = 0;
00102 
00103   this->setg(0, 0, 0);
00104   this->setp(0, 0);
00105 
00106   _M_saved_eback = _M_saved_gptr = _M_saved_egptr = 0;
00107 
00108   _M_in_input_mode = _M_in_output_mode = _M_in_error_mode = _M_in_putback_mode
00109     = false;
00110 
00111   return __ok ? this : 0;
00112 }
00113 
00114 // This member function is called whenever we exit input mode.
00115 // It unmaps the memory-mapped file, if any, and sets
00116 // _M_in_input_mode to false.
00117 template <class _CharT, class _Traits>
00118 void basic_filebuf<_CharT, _Traits>::_M_exit_input_mode() {
00119    if (_M_mmap_base != 0)
00120      _M_base._M_unmap(_M_mmap_base, _M_mmap_len);
00121    _M_in_input_mode = false;
00122    _M_mmap_base = 0;
00123 }
00124 
00125 
00126 //----------------------------------------------------------------------
00127 // basic_filebuf<> overridden protected virtual member functions
00128 
00129 template <class _CharT, class _Traits>
00130 streamsize basic_filebuf<_CharT, _Traits>::showmanyc() {
00131   // Is there any possibility that reads can succeed?
00132   if (!this->is_open() || _M_in_output_mode || _M_in_error_mode)
00133     return -1;
00134   else if (_M_in_putback_mode)
00135     return this->egptr() - this->gptr();
00136   else if (_M_constant_width) {
00137     streamoff __pos  = _M_base._M_seek(0, ios_base::cur);
00138     streamoff __size = _M_base._M_file_size();
00139     return __pos >= 0 && __size > __pos ? __size - __pos : 0;
00140   }
00141   else
00142     return 0;
00143 }
00144 
00145 
00146 // Make a putback position available, if necessary, by switching to a
00147 // special internal buffer used only for putback.  The buffer is
00148 // [_M_pback_buf, _M_pback_buf + _S_pback_buf_size), but the base
00149 // class only sees a piece of it at a time.  (We want to make sure
00150 // that we don't try to read a character that hasn't been initialized.)
00151 // The end of the putback buffer is always _M_pback_buf + _S_pback_buf_size,
00152 // but the beginning is usually not _M_pback_buf.
00153 template <class _CharT, class _Traits>
00154 __BF_int_type__
00155 basic_filebuf<_CharT, _Traits>::pbackfail(int_type __c) {
00156   const int_type __eof = traits_type::eof();
00157 
00158   // If we aren't already in input mode, pushback is impossible.
00159   if (!_M_in_input_mode)
00160     return __eof;
00161 
00162   // We can use the ordinary get buffer if there's enough space, and
00163   // if it's a buffer that we're allowed to write to.
00164   if (this->gptr() != this->eback() &&
00165       (traits_type::eq_int_type(__c, __eof) ||
00166        traits_type::eq(traits_type::to_char_type(__c), this->gptr()[-1]) ||
00167        !_M_mmap_base)) {
00168     this->gbump(-1);
00169     if (traits_type::eq_int_type(__c, __eof) ||
00170         traits_type::eq(traits_type::to_char_type(__c), *this->gptr()))
00171       return traits_type::to_int_type(*this->gptr());
00172   }
00173   else if (!traits_type::eq_int_type(__c, __eof)) {
00174     // Are we in the putback buffer already?
00175     _CharT* __pback_end = _M_pback_buf + __STATIC_CAST(int,_S_pback_buf_size);
00176     if (_M_in_putback_mode) {
00177       // Do we have more room in the putback buffer?
00178       if (this->eback() != _M_pback_buf)
00179         this->setg(this->egptr() - 1, this->egptr() - 1, __pback_end);
00180       else
00181         return __eof;           // No more room in the buffer, so fail.
00182     }
00183     else {                      // We're not yet in the putback buffer.
00184       _M_saved_eback = this->eback();
00185       _M_saved_gptr  = this->gptr();
00186       _M_saved_egptr = this->egptr();
00187       this->setg(__pback_end - 1, __pback_end - 1, __pback_end);
00188       _M_in_putback_mode = true;
00189     }
00190   }
00191   else
00192     return __eof;
00193 
00194   // We have made a putback position available.  Assign to it, and return.
00195   *this->gptr() = traits_type::to_char_type(__c);
00196   return __c;
00197 }
00198 
00199 // This member function flushes the put area, and also outputs the
00200 // character __c (unless __c is eof).  Invariant: we always leave room
00201 // in the internal buffer for one character more than the base class knows
00202 // about.  We see the internal buffer as [_M_int_buf, _M_int_buf_EOS), but
00203 // the base class only sees [_M_int_buf, _M_int_buf_EOS - 1).
00204 template <class _CharT, class _Traits>
00205 __BF_int_type__
00206 basic_filebuf<_CharT, _Traits>::overflow(int_type __c) {
00207   // Switch to output mode, if necessary.
00208   if (!_M_in_output_mode)
00209     if (!_M_switch_to_output_mode())
00210       return traits_type::eof();
00211 
00212   _CharT* __ibegin = this->_M_int_buf;
00213   _CharT* __iend   = this->pptr();
00214   this->setp(_M_int_buf, _M_int_buf_EOS - 1);
00215 
00216   // Put __c at the end of the internal buffer.
00217   if (!traits_type::eq_int_type(__c, traits_type::eof()))
00218     *__iend++ = _Traits::to_char_type(__c);
00219 
00220   // For variable-width encodings, output may take more than one pass.
00221   while (__ibegin != __iend) {
00222     const _CharT* __inext = __ibegin;
00223     char* __enext         = _M_ext_buf;
00224     typename _Codecvt::result __status
00225       = _M_codecvt->out(_M_state, __ibegin, __iend, __inext,
00226                         _M_ext_buf, _M_ext_buf_EOS, __enext);
00227     if (__status == _Codecvt::noconv) {
00228       return _Noconv_output<_Traits>::_M_doit(this, __ibegin, __iend)
00229         ? traits_type::not_eof(__c)
00230         : _M_output_error();
00231     }
00232 
00233     // For a constant-width encoding we know that the external buffer
00234     // is large enough, so failure to consume the entire internal buffer
00235     // or to produce the correct number of external characters, is an error.
00236     // For a variable-width encoding, however, we require only that we
00237     // consume at least one internal character
00238     else if (__status != _Codecvt::error &&
00239              (((__inext == __iend) &&
00240                (__enext - _M_ext_buf == _M_width * (__iend - __ibegin))) ||
00241               (!_M_constant_width && __inext != __ibegin))) {
00242         // We successfully converted part or all of the internal buffer.
00243       ptrdiff_t __n = __enext - _M_ext_buf;
00244       if (_M_write(_M_ext_buf, __n))
00245         __ibegin += __inext - __ibegin;
00246       else
00247         return _M_output_error();
00248     }
00249     else
00250       return _M_output_error();
00251   }
00252 
00253   return traits_type::not_eof(__c);
00254 }
00255 
00256 // This member function must be called before any I/O has been
00257 // performed on the stream, otherwise it has no effect.
00258 //
00259 // __buf == 0 && __n == 0 means to make this stream unbuffered.
00260 // __buf != 0 && __n > 0 means to use __buf as the stream's internal
00261 // buffer, rather than the buffer that would otherwise be allocated
00262 // automatically.  __buf must be a pointer to an array of _CharT whose
00263 // size is at least __n.
00264 template <class _CharT, class _Traits>
00265 basic_streambuf<_CharT, _Traits>*
00266 basic_filebuf<_CharT, _Traits>::setbuf(_CharT* __buf, streamsize __n) {
00267   if (!_M_in_input_mode &&! _M_in_output_mode && !_M_in_error_mode &&
00268       _M_int_buf == 0) {
00269     if (__buf == 0 && __n == 0)
00270       _M_allocate_buffers(0, 1);
00271     else if (__buf != 0 && __n > 0)
00272       _M_allocate_buffers(__buf, __n);
00273   }
00274   return this;
00275 }
00276 
00277 template <class _CharT, class _Traits>
00278 __BF_pos_type__
00279 basic_filebuf<_CharT, _Traits>::seekoff(off_type __off,
00280                                         ios_base::seekdir __whence,
00281                                         ios_base::openmode /* dummy */) {
00282   if (this->is_open() &&
00283       (__off == 0 || (_M_constant_width && this->_M_base._M_in_binary_mode()))) {
00284 
00285     if (!_M_seek_init(__off != 0 || __whence != ios_base::cur))
00286       return pos_type(-1);
00287 
00288     // Seek to beginning or end, regardless of whether we're in input mode.
00289     if (__whence == ios_base::beg || __whence == ios_base::end)
00290       return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
00291                             _State_type());
00292 
00293     // Seek relative to current position. Complicated if we're in input mode.
00294     else if (__whence == ios_base::cur) {
00295       if (!_M_in_input_mode)
00296         return _M_seek_return(_M_base._M_seek(_M_width * __off, __whence),
00297                               _State_type());
00298       else if (_M_mmap_base != 0) {
00299         // __off is relative to gptr().  We need to do a bit of arithmetic
00300         // to get an offset relative to the external file pointer.
00301         streamoff __adjust = _M_mmap_len - (this->gptr() - (_CharT*) _M_mmap_base);
00302 
00303         // if __off == 0, we do not need to exit input mode and to shift file pointer
00304         return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __adjust)
00305                           : _M_seek_return(_M_base._M_seek(__off - __adjust, ios_base::cur), _State_type());
00306       }
00307       else if (_M_constant_width) { // Get or set the position.
00308         streamoff __iadj = _M_width * (this->gptr() - this->eback());
00309 
00310         // Compensate for offset relative to gptr versus offset relative
00311         // to external pointer.  For a text-oriented stream, where the
00312         // compensation is more than just pointer arithmetic, we may get
00313         // but not set the current position.
00314 
00315         if (__iadj <= _M_ext_buf_end - _M_ext_buf) {
00316           streamoff __eadj =  _M_base._M_get_offset(_M_ext_buf + __STATIC_CAST(ptrdiff_t, __iadj), _M_ext_buf_end);
00317 
00318           return __off == 0 ? pos_type(_M_base._M_seek(0, ios_base::cur) - __eadj)
00319                             : _M_seek_return(_M_base._M_seek(__off - __eadj, ios_base::cur), _State_type());
00320         }
00321       } else {                    // Get the position.  Encoding is var width.
00322         // Get position in internal buffer.
00323         ptrdiff_t __ipos = this->gptr() - this->eback();
00324 
00325         // Get corresponding position in external buffer.
00326         _State_type __state = _M_state;
00327         int __epos = _M_codecvt->length(__state, _M_ext_buf, _M_ext_buf_end,
00328                                         __ipos);
00329 
00330         if (__epos >= 0) {
00331           // Sanity check (expensive): make sure __epos is the right answer.
00332           _State_type __tmp_state = _M_state;
00333           _Filebuf_Tmp_Buf<_CharT> __buf(__ipos);
00334           _CharT* __ibegin = __buf._M_ptr;
00335           _CharT* __inext  = __ibegin;
00336 
00337           const char* __dummy;
00338           typename _Codecvt::result __status
00339             = _M_codecvt->in(__tmp_state,
00340                              _M_ext_buf, _M_ext_buf + __epos, __dummy,
00341                              __ibegin, __ibegin + __ipos, __inext);
00342           if (__status != _Codecvt::error &&
00343               (__status == _Codecvt::noconv ||
00344                (__inext == __ibegin + __ipos &&
00345                 equal(this->eback(), this->gptr(), __ibegin, _STLP_PRIV _Eq_traits<traits_type>())))) {
00346             // Get the current position (at the end of the external buffer),
00347             // then adjust it.  Again, it might be a text-oriented stream.
00348             streamoff __cur = _M_base._M_seek(0, ios_base::cur);
00349             streamoff __adj =
00350               _M_base._M_get_offset(_M_ext_buf, _M_ext_buf + __epos) -
00351               _M_base._M_get_offset(_M_ext_buf, _M_ext_buf_end);
00352             if (__cur != -1 && __cur + __adj >= 0)
00353               return __off == 0 ? pos_type(__cur + __adj)
00354                                 : _M_seek_return(__cur + __adj, __state);
00355               //return _M_seek_return(__cur + __adj, __state);
00356           }
00357           // We failed the sanity check here.
00358         }
00359       }
00360     }
00361     // Unrecognized value for __whence here.
00362   }
00363 
00364   return pos_type(-1);
00365 }
00366 
00367 
00368 template <class _CharT, class _Traits>
00369 __BF_pos_type__
00370 basic_filebuf<_CharT, _Traits>::seekpos(pos_type __pos,
00371                                         ios_base::openmode /* dummy */) {
00372   if (this->is_open()) {
00373     if (!_M_seek_init(true))
00374       return pos_type(-1);
00375 
00376     streamoff __off = off_type(__pos);
00377     if (__off != -1 && _M_base._M_seek(__off, ios_base::beg) != -1) {
00378       _M_state = __pos.state();
00379       return _M_seek_return(__off, __pos.state());
00380     }
00381   }
00382 
00383   return pos_type(-1);
00384 }
00385 
00386 
00387 template <class _CharT, class _Traits>
00388 int basic_filebuf<_CharT, _Traits>::sync() {
00389   if (_M_in_output_mode)
00390     return traits_type::eq_int_type(this->overflow(traits_type::eof()),
00391                                     traits_type::eof()) ? -1 : 0;
00392   return 0;
00393 }
00394 
00395 
00396 // Change the filebuf's locale.  This member function has no effect
00397 // unless it is called before any I/O is performed on the stream.
00398 template <class _CharT, class _Traits>
00399 void basic_filebuf<_CharT, _Traits>::imbue(const locale& __loc) {
00400   if (!_M_in_input_mode && !_M_in_output_mode && !_M_in_error_mode) {
00401     this->_M_setup_codecvt(__loc);
00402   }
00403 }
00404 
00405 //----------------------------------------------------------------------
00406 // basic_filebuf<> helper functions.
00407 
00408 //----------------------------------------
00409 // Helper functions for switching between modes.
00410 
00411 // This member function is called if we're performing the first I/O
00412 // operation on a filebuf, or if we're performing an input operation
00413 // immediately after a seek.
00414 template <class _CharT, class _Traits>
00415 bool basic_filebuf<_CharT, _Traits>::_M_switch_to_input_mode() {
00416   if (this->is_open() && (((int)_M_base.__o_mode() & (int)ios_base::in) != 0)
00417       && (_M_in_output_mode == 0) && (_M_in_error_mode == 0)) {
00418     if (!_M_int_buf && !_M_allocate_buffers())
00419       return false;
00420 
00421     _M_ext_buf_converted = _M_ext_buf;
00422     _M_ext_buf_end       = _M_ext_buf;
00423 
00424     _M_end_state    = _M_state;
00425 
00426     _M_in_input_mode = true;
00427     return true;
00428   }
00429 
00430   return false;
00431 }
00432 
00433 
00434 // This member function is called if we're performing the first I/O
00435 // operation on a filebuf, or if we're performing an output operation
00436 // immediately after a seek.
00437 template <class _CharT, class _Traits>
00438 bool basic_filebuf<_CharT, _Traits>::_M_switch_to_output_mode() {
00439   if (this->is_open() && (_M_base.__o_mode() & (int)ios_base::out) &&
00440       _M_in_input_mode == 0 && _M_in_error_mode == 0) {
00441 
00442     if (!_M_int_buf && !_M_allocate_buffers())
00443       return false;
00444 
00445     // In append mode, every write does an implicit seek to the end
00446     // of the file.  Whenever leaving output mode, the end of file
00447     // get put in the initial shift state.
00448     if (_M_base.__o_mode() & ios_base::app)
00449       _M_state = _State_type();
00450 
00451     this->setp(_M_int_buf, _M_int_buf_EOS - 1);
00452     _M_in_output_mode = true;
00453     return true;
00454   }
00455 
00456   return false;
00457 }
00458 
00459 
00460 //----------------------------------------
00461 // Helper functions for input
00462 
00463 // This member function is called if there is an error during input.
00464 // It puts the filebuf in error mode, clear the get area buffer, and
00465 // returns eof.
00466 // returns eof.  Error mode is sticky; it is cleared only by close or
00467 // seek.
00468 
00469 template <class _CharT, class _Traits>
00470 __BF_int_type__
00471 basic_filebuf<_CharT, _Traits>::_M_input_error() {
00472    this->_M_exit_input_mode();
00473   _M_in_output_mode = false;
00474   _M_in_error_mode = true;
00475   this->setg(0, 0, 0);
00476   return traits_type::eof();
00477 }
00478 
00479 template <class _CharT, class _Traits>
00480 __BF_int_type__
00481 basic_filebuf<_CharT, _Traits>::_M_underflow_aux() {
00482   // We have the state and file position from the end of the internal
00483   // buffer.  This round, they become the beginning of the internal buffer.
00484   _M_state    = _M_end_state;
00485 
00486   // Fill the external buffer.  Start with any leftover characters that
00487   // didn't get converted last time.
00488   if (_M_ext_buf_end > _M_ext_buf_converted)
00489 
00490     _M_ext_buf_end = copy(_M_ext_buf_converted, _M_ext_buf_end, _M_ext_buf);
00491     // boris : copy_backward did not work
00492     //_M_ext_buf_end = copy_backward(_M_ext_buf_converted, _M_ext_buf_end,
00493     //_M_ext_buf+ (_M_ext_buf_end - _M_ext_buf_converted));
00494   else
00495     _M_ext_buf_end = _M_ext_buf;
00496 
00497   // Now fill the external buffer with characters from the file.  This is
00498   // a loop because occasionally we don't get enough external characters
00499   // to make progress.
00500   for (;;) {
00501     ptrdiff_t __n = _M_base._M_read(_M_ext_buf_end, _M_ext_buf_EOS - _M_ext_buf_end);
00502     _M_ext_buf_end += __n;
00503 
00504     // If external buffer is empty there is nothing to do. 
00505     if (_M_ext_buf == _M_ext_buf_end) {
00506       this->setg(0, 0, 0);
00507       return traits_type::eof();
00508     }
00509 
00510     // Convert the external buffer to internal characters.
00511     const char* __enext;
00512     _CharT* __inext;
00513 
00514     typename _Codecvt::result __status
00515       = _M_codecvt->in(_M_end_state,
00516                        _M_ext_buf, _M_ext_buf_end, __enext,
00517                        _M_int_buf, _M_int_buf_EOS, __inext);
00518 
00519     /* Error conditions:
00520      * (1) Return value of error.
00521      * (2) Producing internal characters without consuming external characters.
00522      * (3) In fixed-width encodings, producing an internal sequence whose length
00523      * is inconsistent with that of the internal sequence.
00524      * (4) Failure to produce any characters if we have enough characters in
00525      * the external buffer, where "enough" means the largest possible width
00526      * of a single character. */
00527     if (__status == _Codecvt::noconv)
00528       return _Noconv_input<_Traits>::_M_doit(this);
00529     else if (__status == _Codecvt::error ||
00530             (__inext != _M_int_buf && __enext == _M_ext_buf) ||
00531             (_M_constant_width && (__inext - _M_int_buf) *  _M_width != (__enext - _M_ext_buf)) ||
00532             (__inext == _M_int_buf && __enext - _M_ext_buf >= _M_max_width))
00533       return _M_input_error();
00534     else if (__inext != _M_int_buf) {
00535       _M_ext_buf_converted = _M_ext_buf + (__enext - _M_ext_buf);
00536       this->setg(_M_int_buf, _M_int_buf, __inext);
00537       return traits_type::to_int_type(*_M_int_buf);
00538     }
00539     /* We need to go around the loop again to get more external characters.
00540      * But if the previous read failed then don't try again for now.
00541      * Don't enter error mode for a failed read. Error mode is sticky,
00542      * and we might succeed if we try again. */
00543     if (__n <= 0) {
00544       this->setg(0, 0, 0);
00545       return traits_type::eof();
00546     }
00547   }
00548 }
00549 
00550 //----------------------------------------
00551 // Helper functions for output
00552 
00553 // This member function is called if there is an error during output.
00554 // It puts the filebuf in error mode, clear the put area buffer, and
00555 // returns eof.  Error mode is sticky; it is cleared only by close or
00556 // seek.
00557 template <class _CharT, class _Traits>
00558 __BF_int_type__
00559 basic_filebuf<_CharT, _Traits>::_M_output_error() {
00560   _M_in_output_mode = false;
00561   _M_in_input_mode = false;
00562   _M_in_error_mode = true;
00563   this->setp(0, 0);
00564   return traits_type::eof();
00565 }
00566 
00567 
00568 // Write whatever sequence of characters is necessary to get back to
00569 // the initial shift state.  This function overwrites the external
00570 // buffer, changes the external file position, and changes the state.
00571 // Precondition: the internal buffer is empty.
00572 template <class _CharT, class _Traits>
00573 bool basic_filebuf<_CharT, _Traits>::_M_unshift() {
00574   if (_M_in_output_mode && !_M_constant_width) {
00575     typename _Codecvt::result __status;
00576     do {
00577       char* __enext = _M_ext_buf;
00578       __status = _M_codecvt->unshift(_M_state,
00579                                      _M_ext_buf, _M_ext_buf_EOS, __enext);
00580       if (__status == _Codecvt::noconv ||
00581           (__enext == _M_ext_buf && __status == _Codecvt::ok))
00582         return true;
00583       else if (__status == _Codecvt::error)
00584         return false;
00585       else if (!_M_write(_M_ext_buf, __enext - _M_ext_buf))
00586         return false;
00587     } while (__status == _Codecvt::partial);
00588   }
00589 
00590   return true;
00591 }
00592 
00593 
00594 //----------------------------------------
00595 // Helper functions for buffer allocation and deallocation
00596 
00597 // This member function is called when we're initializing a filebuf's
00598 // internal and external buffers.  The argument is the size of the
00599 // internal buffer; the external buffer is sized using the character
00600 // width in the current encoding.  Preconditions: the buffers are currently
00601 // null.  __n >= 1.  __buf is either a null pointer or a pointer to an
00602 // array show size is at least __n.
00603 
00604 // We need __n >= 1 for two different reasons.  For input, the base
00605 // class always needs a buffer because of the semantics of underflow().
00606 // For output, we want to have an internal buffer that's larger by one
00607 // element than the buffer that the base class knows about.  (See
00608 // basic_filebuf<>::overflow() for the reason.)
00609 template <class _CharT, class _Traits>
00610 bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers(_CharT* __buf, streamsize __n) {
00611   //The major hypothesis in the following implementation is that size_t is unsigned.
00612   //We also need streamsize byte representation to be larger or equal to the int
00613   //representation to correctly store the encoding information.
00614   _STLP_STATIC_ASSERT(!numeric_limits<size_t>::is_signed &&
00615                       sizeof(streamsize) >= sizeof(int))
00616 
00617   if (__buf == 0) {
00618     streamsize __bufsize = __n * sizeof(_CharT);
00619     //We first check that the streamsize representation can't overflow a size_t one.
00620     //If it can, we check that __bufsize is not higher than the size_t max value.
00621     if ((sizeof(streamsize) > sizeof(size_t)) &&
00622         (__bufsize > __STATIC_CAST(streamsize, (numeric_limits<size_t>::max)())))
00623       return false;
00624     _M_int_buf = __STATIC_CAST(_CharT*, malloc(__STATIC_CAST(size_t, __bufsize)));
00625     if (!_M_int_buf)
00626       return false;
00627     _M_int_buf_dynamic = true;
00628   }
00629   else {
00630     _M_int_buf = __buf;
00631     _M_int_buf_dynamic = false;
00632   }
00633 
00634   streamsize __ebufsiz = (max)(__n * __STATIC_CAST(streamsize, _M_width),
00635                                __STATIC_CAST(streamsize, _M_codecvt->max_length()));
00636   _M_ext_buf = 0;
00637   if ((sizeof(streamsize) < sizeof(size_t)) ||
00638       ((sizeof(streamsize) == sizeof(size_t)) && numeric_limits<streamsize>::is_signed) ||
00639       (__ebufsiz <= __STATIC_CAST(streamsize, (numeric_limits<size_t>::max)()))) {
00640     _M_ext_buf = __STATIC_CAST(char*, malloc(__STATIC_CAST(size_t, __ebufsiz)));
00641   }
00642 
00643   if (!_M_ext_buf) {
00644     _M_deallocate_buffers();
00645     return false;
00646   }
00647 
00648   _M_int_buf_EOS = _M_int_buf + __STATIC_CAST(ptrdiff_t, __n);
00649   _M_ext_buf_EOS = _M_ext_buf + __STATIC_CAST(ptrdiff_t, __ebufsiz);
00650   return true;
00651 }
00652 
00653 // Abbreviation for the most common case.
00654 template <class _CharT, class _Traits>
00655 bool basic_filebuf<_CharT, _Traits>::_M_allocate_buffers() {
00656   // Choose a buffer that's at least 4096 characters long and that's a
00657   // multiple of the page size.
00658   streamsize __default_bufsiz =
00659     ((_M_base.__page_size() + 4095UL) / _M_base.__page_size()) * _M_base.__page_size();
00660   return _M_allocate_buffers(0, __default_bufsiz);
00661 }
00662 
00663 template <class _CharT, class _Traits>
00664 void basic_filebuf<_CharT, _Traits>::_M_deallocate_buffers() {
00665   if (_M_int_buf_dynamic)
00666     free(_M_int_buf);
00667   free(_M_ext_buf);
00668   _M_int_buf     = 0;
00669   _M_int_buf_EOS = 0;
00670   _M_ext_buf     = 0;
00671   _M_ext_buf_EOS = 0;
00672 }
00673 
00674 
00675 //----------------------------------------
00676 // Helper functiosn for seek and imbue
00677 
00678 template <class _CharT, class _Traits>
00679 bool basic_filebuf<_CharT, _Traits>::_M_seek_init(bool __do_unshift) {
00680   // If we're in error mode, leave it.
00681    _M_in_error_mode = false;
00682 
00683   // Flush the output buffer if we're in output mode, and (conditionally)
00684   // emit an unshift sequence.
00685   if (_M_in_output_mode) {
00686     bool __ok = !traits_type::eq_int_type(this->overflow(traits_type::eof()),
00687                                           traits_type::eof());
00688     if (__do_unshift)
00689       __ok = __ok && this->_M_unshift();
00690     if (!__ok) {
00691       _M_in_output_mode = false;
00692       _M_in_error_mode = true;
00693       this->setp(0, 0);
00694       return false;
00695     }
00696   }
00697 
00698   // Discard putback characters, if any.
00699   if (_M_in_input_mode && _M_in_putback_mode)
00700     _M_exit_putback_mode();
00701 
00702   return true;
00703 }
00704 
00705 
00706 /* Change the filebuf's locale.  This member function has no effect
00707  * unless it is called before any I/O is performed on the stream.
00708  * This function is called on construction and on an imbue call. In the
00709  * case of the construction the codecvt facet might be a custom one if
00710  * the basic_filebuf user has instanciate it with a custom char_traits.
00711  * The user will have to call imbue before any I/O operation.
00712  */
00713 template <class _CharT, class _Traits>
00714 void basic_filebuf<_CharT, _Traits>::_M_setup_codecvt(const locale& __loc, bool __on_imbue) {
00715   if (has_facet<_Codecvt>(__loc)) {
00716     _M_codecvt = &use_facet<_Codecvt>(__loc) ;
00717     int __encoding    = _M_codecvt->encoding();
00718 
00719     _M_width          = (max)(__encoding, 1);
00720     _M_max_width      = _M_codecvt->max_length();
00721     _M_constant_width = __encoding > 0;
00722     _M_always_noconv  = _M_codecvt->always_noconv();
00723   }
00724   else {
00725     _M_codecvt = 0;
00726     _M_width = _M_max_width = 1;
00727     _M_constant_width = _M_always_noconv  = false;
00728     if (__on_imbue) {
00729       //This call will generate an exception reporting the problem.
00730       use_facet<_Codecvt>(__loc);
00731     }
00732   }
00733 }
00734 
00735 _STLP_END_NAMESPACE
00736 
00737 # undef __BF_int_type__
00738 # undef __BF_pos_type__
00739 # undef __BF_off_type__
00740 
00741 #endif /* _STLP_FSTREAM_C */
00742 
00743 // Local Variables:
00744 // mode:C++
00745 // End:



Generated on Mon Mar 10 15:32:21 2008 by  doxygen 1.5.1