fstream.tcc

Go to the documentation of this file.
00001 // File based streams -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /** @file fstream.tcc
00032  *  This is an internal header file, included by other library headers.
00033  *  You should not attempt to use it directly.
00034  */
00035 
00036 //
00037 // ISO C++ 14882: 27.8  File-based streams
00038 //
00039 
00040 #ifndef _FSTREAM_TCC
00041 #define _FSTREAM_TCC 1
00042 
00043 #pragma GCC system_header
00044 
00045 namespace std
00046 {
00047   template<typename _CharT, typename _Traits>
00048     void
00049     basic_filebuf<_CharT, _Traits>::
00050     _M_allocate_internal_buffer()
00051     {
00052       // Allocate internal buffer only if one doesn't already exist
00053       // (either allocated or provided by the user via setbuf).
00054       if (!_M_buf_allocated && !_M_buf)
00055     {
00056       _M_buf = new char_type[_M_buf_size];
00057       _M_buf_allocated = true;
00058     }
00059     }
00060 
00061   template<typename _CharT, typename _Traits>
00062     void
00063     basic_filebuf<_CharT, _Traits>::
00064     _M_destroy_internal_buffer() throw()
00065     {
00066       if (_M_buf_allocated)
00067     {
00068       delete [] _M_buf;
00069       _M_buf = NULL;
00070       _M_buf_allocated = false;
00071     }
00072       delete [] _M_ext_buf;
00073       _M_ext_buf = NULL;
00074       _M_ext_buf_size = 0;
00075       _M_ext_next = NULL;
00076       _M_ext_end = NULL;
00077     }
00078 
00079   template<typename _CharT, typename _Traits>
00080     basic_filebuf<_CharT, _Traits>::
00081     basic_filebuf() : __streambuf_type(), _M_lock(), _M_file(&_M_lock),
00082     _M_mode(ios_base::openmode(0)), _M_state_beg(), _M_state_cur(),
00083     _M_state_last(), _M_buf(NULL), _M_buf_size(BUFSIZ),
00084     _M_buf_allocated(false), _M_reading(false), _M_writing(false), _M_pback(), 
00085     _M_pback_cur_save(0), _M_pback_end_save(0), _M_pback_init(false),
00086     _M_codecvt(0), _M_ext_buf(0), _M_ext_buf_size(0), _M_ext_next(0),
00087     _M_ext_end(0)
00088     {
00089       if (has_facet<__codecvt_type>(this->_M_buf_locale))
00090     _M_codecvt = &use_facet<__codecvt_type>(this->_M_buf_locale);
00091     }
00092 
00093   template<typename _CharT, typename _Traits>
00094     typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
00095     basic_filebuf<_CharT, _Traits>::
00096     open(const char* __s, ios_base::openmode __mode)
00097     {
00098       __filebuf_type *__ret = NULL;
00099       if (!this->is_open())
00100     {
00101       _M_file.open(__s, __mode);
00102       if (this->is_open())
00103         {
00104           _M_allocate_internal_buffer();
00105           _M_mode = __mode;
00106 
00107           // Setup initial buffer to 'uncommitted' mode.
00108           _M_reading = false;
00109           _M_writing = false;
00110           _M_set_buffer(-1);
00111 
00112           // Reset to initial state.
00113           _M_state_last = _M_state_cur = _M_state_beg;
00114 
00115           // 27.8.1.3,4
00116           if ((__mode & ios_base::ate)
00117           && this->seekoff(0, ios_base::end, __mode)
00118           == pos_type(off_type(-1)))
00119         this->close();
00120           else
00121         __ret = this;
00122         }
00123     }
00124       return __ret;
00125     }
00126 
00127   template<typename _CharT, typename _Traits>
00128     typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
00129     basic_filebuf<_CharT, _Traits>::
00130     close() throw()
00131     {
00132       __filebuf_type* __ret = NULL;
00133       if (this->is_open())
00134     {
00135       bool __testfail = false;
00136       try
00137         {
00138           if (!_M_terminate_output())
00139         __testfail = true;
00140         }
00141       catch(...)
00142         { __testfail = true; }
00143 
00144       // NB: Do this here so that re-opened filebufs will be cool...
00145       _M_mode = ios_base::openmode(0);
00146       _M_pback_init = false;
00147       _M_destroy_internal_buffer();
00148       _M_reading = false;
00149       _M_writing = false;
00150       _M_set_buffer(-1);
00151       _M_state_last = _M_state_cur = _M_state_beg;
00152 
00153       if (!_M_file.close())
00154         __testfail = true;
00155 
00156       if (!__testfail)
00157         __ret = this;
00158     }
00159       return __ret;
00160     }
00161 
00162   template<typename _CharT, typename _Traits>
00163     streamsize
00164     basic_filebuf<_CharT, _Traits>::
00165     showmanyc()
00166     {
00167       streamsize __ret = -1;
00168       const bool __testin = _M_mode & ios_base::in;
00169       if (__testin && this->is_open())
00170     {
00171       // For a stateful encoding (-1) the pending sequence might be just
00172       // shift and unshift prefixes with no actual character.
00173       __ret = this->egptr() - this->gptr();
00174       if (__check_facet(_M_codecvt).encoding() >= 0)
00175         __ret += _M_file.showmanyc() / _M_codecvt->max_length();
00176     }
00177       return __ret;
00178     }
00179 
00180   template<typename _CharT, typename _Traits>
00181     typename basic_filebuf<_CharT, _Traits>::int_type
00182     basic_filebuf<_CharT, _Traits>::
00183     underflow()
00184     {
00185       int_type __ret = traits_type::eof();
00186       const bool __testin = _M_mode & ios_base::in;
00187       if (__testin && !_M_writing)
00188     {
00189       // Check for pback madness, and if so swich back to the
00190       // normal buffers and jet outta here before expensive
00191       // fileops happen...
00192       _M_destroy_pback();
00193 
00194       if (this->gptr() < this->egptr())
00195         return traits_type::to_int_type(*this->gptr());
00196 
00197       // Get and convert input sequence.
00198       const size_t __buflen = _M_buf_size > 1
00199                               ? _M_buf_size - 1 : 1;
00200 
00201       // Will be set to true if ::read() returns 0 indicating EOF.
00202       bool __got_eof = false;
00203       // Number of internal characters produced.
00204       streamsize __ilen = 0;
00205       codecvt_base::result __r = codecvt_base::ok;
00206       if (__check_facet(_M_codecvt).always_noconv())
00207         {
00208           __ilen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()),
00209                       __buflen);
00210           if (__ilen == 0)
00211         __got_eof = true;
00212         }
00213       else
00214         {
00215               // Worst-case number of external bytes.
00216           // XXX Not done encoding() == -1.
00217           const int __enc = _M_codecvt->encoding();
00218           streamsize __blen; // Minimum buffer size.
00219           streamsize __rlen; // Number of chars to read.
00220           if (__enc > 0)
00221         __blen = __rlen = __buflen * __enc;
00222           else
00223         {
00224           __blen = __buflen + _M_codecvt->max_length() - 1;
00225           __rlen = __buflen;
00226         }
00227           const streamsize __remainder = _M_ext_end - _M_ext_next;
00228           __rlen = __rlen > __remainder ? __rlen - __remainder : 0;
00229 
00230           // An imbue in 'read' mode implies first converting the external
00231           // chars already present.
00232           if (_M_reading && this->egptr() == this->eback() && __remainder)
00233         __rlen = 0;
00234 
00235           // Allocate buffer if necessary and move unconverted
00236           // bytes to front.
00237           if (_M_ext_buf_size < __blen)
00238         {
00239           char* __buf = new char[__blen];
00240           if (__remainder)
00241             std::memcpy(__buf, _M_ext_next, __remainder);
00242 
00243           delete [] _M_ext_buf;
00244           _M_ext_buf = __buf;
00245           _M_ext_buf_size = __blen;
00246         }
00247           else if (__remainder)
00248         std::memmove(_M_ext_buf, _M_ext_next, __remainder);
00249 
00250           _M_ext_next = _M_ext_buf;
00251           _M_ext_end = _M_ext_buf + __remainder;
00252           _M_state_last = _M_state_cur;
00253 
00254           do
00255         {
00256           if (__rlen > 0)
00257             {
00258               // Sanity check!
00259               // This may fail if the return value of
00260               // codecvt::max_length() is bogus.
00261               if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size)
00262             {
00263               __throw_ios_failure(__N("basic_filebuf::underflow "
00264                           "codecvt::max_length() "
00265                           "is not valid"));
00266             }
00267               streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
00268               if (__elen == 0)
00269             __got_eof = true;
00270               else if (__elen == -1)
00271             break;
00272               _M_ext_end += __elen;
00273             }
00274 
00275           char_type* __iend;
00276           __r = _M_codecvt->in(_M_state_cur, _M_ext_next,
00277                        _M_ext_end, _M_ext_next, this->eback(),
00278                        this->eback() + __buflen, __iend);
00279           if (__r == codecvt_base::noconv)
00280             {
00281               size_t __avail = _M_ext_end - _M_ext_buf;
00282               __ilen = std::min(__avail, __buflen);
00283               traits_type::copy(this->eback(),
00284                     reinterpret_cast<char_type*>(_M_ext_buf), __ilen);
00285               _M_ext_next = _M_ext_buf + __ilen;
00286             }
00287           else
00288             __ilen = __iend - this->eback();
00289 
00290           // _M_codecvt->in may return error while __ilen > 0: this is
00291           // ok, and actually occurs in case of mixed encodings (e.g.,
00292           // XML files).
00293           if (__r == codecvt_base::error)
00294             break;
00295 
00296           __rlen = 1;
00297         }
00298           while (__ilen == 0 && !__got_eof);
00299         }
00300 
00301       if (__ilen > 0)
00302         {
00303           _M_set_buffer(__ilen);
00304           _M_reading = true;
00305           __ret = traits_type::to_int_type(*this->gptr());
00306         }
00307       else if (__got_eof)
00308         {
00309           // If the actual end of file is reached, set 'uncommitted'
00310           // mode, thus allowing an immediate write without an
00311           // intervening seek.
00312           _M_set_buffer(-1);
00313           _M_reading = false;
00314           // However, reaching it while looping on partial means that
00315           // the file has got an incomplete character.
00316           if (__r == codecvt_base::partial)
00317         __throw_ios_failure(__N("basic_filebuf::underflow "
00318                     "incomplete character in file"));
00319         }
00320       else if (__r == codecvt_base::error)
00321         __throw_ios_failure(__N("basic_filebuf::underflow "
00322                 "invalid byte sequence in file"));
00323       else
00324         __throw_ios_failure(__N("basic_filebuf::underflow "
00325                 "error reading the file"));
00326     }
00327       return __ret;
00328     }
00329 
00330   template<typename _CharT, typename _Traits>
00331     typename basic_filebuf<_CharT, _Traits>::int_type
00332     basic_filebuf<_CharT, _Traits>::
00333     pbackfail(int_type __i)
00334     {
00335       int_type __ret = traits_type::eof();
00336       const bool __testin = _M_mode & ios_base::in;
00337       if (__testin && !_M_writing)
00338     {
00339       // Remember whether the pback buffer is active, otherwise below
00340       // we may try to store in it a second char (libstdc++/9761).
00341       const bool __testpb = _M_pback_init;
00342       const bool __testeof = traits_type::eq_int_type(__i, __ret);
00343       int_type __tmp;
00344       if (this->eback() < this->gptr())
00345         {
00346           this->gbump(-1);
00347           __tmp = traits_type::to_int_type(*this->gptr());
00348         }
00349       else if (this->seekoff(-1, ios_base::cur) != pos_type(off_type(-1)))
00350         {
00351           __tmp = this->underflow();
00352           if (traits_type::eq_int_type(__tmp, __ret))
00353         return __ret;
00354         }
00355       else
00356         {
00357           // At the beginning of the buffer, need to make a
00358           // putback position available.  But the seek may fail
00359           // (f.i., at the beginning of a file, see
00360           // libstdc++/9439) and in that case we return
00361           // traits_type::eof().
00362           return __ret;
00363         }
00364 
00365       // Try to put back __i into input sequence in one of three ways.
00366       // Order these tests done in is unspecified by the standard.
00367       if (!__testeof && traits_type::eq_int_type(__i, __tmp))
00368         __ret = __i;
00369       else if (__testeof)
00370         __ret = traits_type::not_eof(__i);
00371       else if (!__testpb)
00372         {
00373           _M_create_pback();
00374           _M_reading = true;
00375           *this->gptr() = traits_type::to_char_type(__i);
00376           __ret = __i;
00377         }
00378     }
00379       return __ret;
00380     }
00381 
00382   template<typename _CharT, typename _Traits>
00383     typename basic_filebuf<_CharT, _Traits>::int_type
00384     basic_filebuf<_CharT, _Traits>::
00385     overflow(int_type __c)
00386     {
00387       int_type __ret = traits_type::eof();
00388       const bool __testeof = traits_type::eq_int_type(__c, __ret);
00389       const bool __testout = _M_mode & ios_base::out;
00390       if (__testout && !_M_reading)
00391     {
00392       if (this->pbase() < this->pptr())
00393         {
00394           // If appropriate, append the overflow char.
00395           if (!__testeof)
00396         {
00397           *this->pptr() = traits_type::to_char_type(__c);
00398           this->pbump(1);
00399         }
00400 
00401           // Convert pending sequence to external representation,
00402           // and output.
00403           if (_M_convert_to_external(this->pbase(),
00404                      this->pptr() - this->pbase()))
00405         {
00406           _M_set_buffer(0);
00407           __ret = traits_type::not_eof(__c);
00408         }
00409         }
00410       else if (_M_buf_size > 1)
00411         {
00412           // Overflow in 'uncommitted' mode: set _M_writing, set
00413           // the buffer to the initial 'write' mode, and put __c
00414           // into the buffer.
00415           _M_set_buffer(0);
00416           _M_writing = true;
00417           if (!__testeof)
00418         {
00419           *this->pptr() = traits_type::to_char_type(__c);
00420           this->pbump(1);
00421         }
00422           __ret = traits_type::not_eof(__c);
00423         }
00424       else
00425         {
00426           // Unbuffered.
00427           char_type __conv = traits_type::to_char_type(__c);
00428           if (__testeof || _M_convert_to_external(&__conv, 1))
00429         {
00430           _M_writing = true;
00431           __ret = traits_type::not_eof(__c);
00432         }
00433         }
00434     }
00435       return __ret;
00436     }
00437 
00438   template<typename _CharT, typename _Traits>
00439     bool
00440     basic_filebuf<_CharT, _Traits>::
00441     _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
00442     {
00443       // Sizes of external and pending output.
00444       streamsize __elen;
00445       streamsize __plen;
00446       if (__check_facet(_M_codecvt).always_noconv())
00447     {
00448       __elen = _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
00449       __plen = __ilen;
00450     }
00451       else
00452     {
00453       // Worst-case number of external bytes needed.
00454       // XXX Not done encoding() == -1.
00455       streamsize __blen = __ilen * _M_codecvt->max_length();
00456       char* __buf = static_cast<char*>(__builtin_alloca(__blen));
00457 
00458       char* __bend;
00459       const char_type* __iend;
00460       codecvt_base::result __r;
00461       __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
00462                 __iend, __buf, __buf + __blen, __bend);
00463 
00464       if (__r == codecvt_base::ok || __r == codecvt_base::partial)
00465         __blen = __bend - __buf;
00466       else if (__r == codecvt_base::noconv)
00467         {
00468           // Same as the always_noconv case above.
00469           __buf = reinterpret_cast<char*>(__ibuf);
00470           __blen = __ilen;
00471         }
00472       else
00473         __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
00474                     "conversion error"));
00475   
00476       __elen = _M_file.xsputn(__buf, __blen);
00477       __plen = __blen;
00478 
00479       // Try once more for partial conversions.
00480       if (__r == codecvt_base::partial && __elen == __plen)
00481         {
00482           const char_type* __iresume = __iend;
00483           streamsize __rlen = this->pptr() - __iend;
00484           __r = _M_codecvt->out(_M_state_cur, __iresume,
00485                     __iresume + __rlen, __iend, __buf,
00486                     __buf + __blen, __bend);
00487           if (__r != codecvt_base::error)
00488         {
00489           __rlen = __bend - __buf;
00490           __elen = _M_file.xsputn(__buf, __rlen);
00491           __plen = __rlen;
00492         }
00493           else
00494         __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
00495                     "conversion error"));
00496         }
00497     }
00498       return __elen == __plen;
00499     }
00500 
00501    template<typename _CharT, typename _Traits>
00502      streamsize
00503      basic_filebuf<_CharT, _Traits>::
00504      xsgetn(_CharT* __s, streamsize __n)
00505      {
00506        // Clear out pback buffer before going on to the real deal...
00507        streamsize __ret = 0;
00508        if (_M_pback_init)
00509      {
00510        if (__n > 0 && this->gptr() == this->eback())
00511          {
00512            *__s++ = *this->gptr();
00513            this->gbump(1);
00514            __ret = 1;
00515            --__n;
00516          }
00517        _M_destroy_pback();
00518      }
00519        
00520        // Optimization in the always_noconv() case, to be generalized in the
00521        // future: when __n > __buflen we read directly instead of using the
00522        // buffer repeatedly.
00523        const bool __testin = _M_mode & ios_base::in;
00524        const streamsize __buflen = _M_buf_size > 1 ? _M_buf_size - 1
00525                                                      : 1;
00526        if (__n > __buflen && __check_facet(_M_codecvt).always_noconv()
00527        && __testin && !_M_writing)
00528      {
00529        // First, copy the chars already present in the buffer.
00530        const streamsize __avail = this->egptr() - this->gptr();
00531        if (__avail != 0)
00532          {
00533            if (__avail == 1)
00534          *__s = *this->gptr();
00535            else
00536          traits_type::copy(__s, this->gptr(), __avail);
00537            __s += __avail;
00538            this->gbump(__avail);
00539            __ret += __avail;
00540            __n -= __avail;
00541          }
00542 
00543        const streamsize __len = _M_file.xsgetn(reinterpret_cast<char*>(__s),
00544                            __n);
00545        if (__len == -1)
00546          __throw_ios_failure(__N("basic_filebuf::xsgetn "
00547                      "error reading the file"));
00548        __ret += __len;
00549        if (__len == __n)
00550          {
00551            _M_set_buffer(0);
00552            _M_reading = true;
00553          }
00554        else if (__len == 0)
00555          {
00556            // If end of file is reached, set 'uncommitted'
00557            // mode, thus allowing an immediate write without
00558            // an intervening seek.
00559            _M_set_buffer(-1);
00560            _M_reading = false;
00561          }
00562      }
00563        else
00564      __ret += __streambuf_type::xsgetn(__s, __n);
00565 
00566        return __ret;
00567      }
00568 
00569    template<typename _CharT, typename _Traits>
00570      streamsize
00571      basic_filebuf<_CharT, _Traits>::
00572      xsputn(const _CharT* __s, streamsize __n)
00573      {
00574        // Optimization in the always_noconv() case, to be generalized in the
00575        // future: when __n is sufficiently large we write directly instead of
00576        // using the buffer.
00577        streamsize __ret = 0;
00578        const bool __testout = _M_mode & ios_base::out;
00579        if (__check_facet(_M_codecvt).always_noconv()
00580        && __testout && !_M_reading)
00581     {
00582       // Measurement would reveal the best choice.
00583       const streamsize __chunk = 1ul << 10;
00584       streamsize __bufavail = this->epptr() - this->pptr();
00585 
00586       // Don't mistake 'uncommitted' mode buffered with unbuffered.
00587       if (!_M_writing && _M_buf_size > 1)
00588         __bufavail = _M_buf_size - 1;
00589 
00590       const streamsize __limit = std::min(__chunk, __bufavail);
00591       if (__n >= __limit)
00592         {
00593           const streamsize __buffill = this->pptr() - this->pbase();
00594           const char* __buf = reinterpret_cast<const char*>(this->pbase());
00595           __ret = _M_file.xsputn_2(__buf, __buffill,
00596                        reinterpret_cast<const char*>(__s),
00597                        __n);
00598           if (__ret == __buffill + __n)
00599         {
00600           _M_set_buffer(0);
00601           _M_writing = true;
00602         }
00603           if (__ret > __buffill)
00604         __ret -= __buffill;
00605           else
00606         __ret = 0;
00607         }
00608       else
00609         __ret = __streambuf_type::xsputn(__s, __n);
00610     }
00611        else
00612      __ret = __streambuf_type::xsputn(__s, __n);
00613        return __ret;
00614     }
00615 
00616   template<typename _CharT, typename _Traits>
00617     typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
00618     basic_filebuf<_CharT, _Traits>::
00619     setbuf(char_type* __s, streamsize __n)
00620     {
00621       if (!this->is_open())
00622     if (__s == 0 && __n == 0)
00623       _M_buf_size = 1;
00624     else if (__s && __n > 0)
00625       {
00626         // This is implementation-defined behavior, and assumes that
00627         // an external char_type array of length __n exists and has
00628         // been pre-allocated. If this is not the case, things will
00629         // quickly blow up. When __n > 1, __n - 1 positions will be
00630         // used for the get area, __n - 1 for the put area and 1
00631         // position to host the overflow char of a full put area.
00632         // When __n == 1, 1 position will be used for the get area
00633         // and 0 for the put area, as in the unbuffered case above.
00634         _M_buf = __s;
00635         _M_buf_size = __n;
00636       }
00637       return this;
00638     }
00639 
00640 
00641   // According to 27.8.1.4 p11 - 13, seekoff should ignore the last
00642   // argument (of type openmode).
00643   template<typename _CharT, typename _Traits>
00644     typename basic_filebuf<_CharT, _Traits>::pos_type
00645     basic_filebuf<_CharT, _Traits>::
00646     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode)
00647     {
00648       int __width = 0;
00649       if (_M_codecvt)
00650     __width = _M_codecvt->encoding();
00651       if (__width < 0)
00652     __width = 0;
00653 
00654       pos_type __ret =  pos_type(off_type(-1));
00655       const bool __testfail = __off != 0 && __width <= 0;
00656       if (this->is_open() && !__testfail)
00657     {
00658       // Ditch any pback buffers to avoid confusion.
00659       _M_destroy_pback();
00660 
00661       // Correct state at destination. Note that this is the correct
00662       // state for the current position during output, because
00663       // codecvt::unshift() returns the state to the initial state.
00664       // This is also the correct state at the end of the file because
00665       // an unshift sequence should have been written at the end.
00666       __state_type __state = _M_state_beg;
00667       off_type __computed_off = __off * __width;
00668       if (_M_reading && __way == ios_base::cur)
00669         {
00670           if (_M_codecvt->always_noconv())
00671         __computed_off += this->gptr() - this->egptr();
00672           else
00673         {
00674           // Calculate offset from _M_ext_buf that corresponds
00675           // to gptr(). Note: uses _M_state_last, which
00676           // corresponds to eback().
00677           const int __gptr_off =
00678             _M_codecvt->length(_M_state_last, _M_ext_buf, _M_ext_next,
00679                        this->gptr() - this->eback());
00680           __computed_off += _M_ext_buf + __gptr_off - _M_ext_end;
00681 
00682           // _M_state_last is modified by codecvt::length() so
00683           // it now corresponds to gptr().
00684           __state = _M_state_last;
00685         }
00686         }
00687       __ret = _M_seek(__computed_off, __way, __state);
00688     }
00689       return __ret;
00690     }
00691 
00692   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00693   // 171. Strange seekpos() semantics due to joint position
00694   // According to the resolution of DR 171, seekpos should ignore the last
00695   // argument (of type openmode).
00696   template<typename _CharT, typename _Traits>
00697     typename basic_filebuf<_CharT, _Traits>::pos_type
00698     basic_filebuf<_CharT, _Traits>::
00699     seekpos(pos_type __pos, ios_base::openmode)
00700     {
00701       pos_type __ret =  pos_type(off_type(-1));
00702       if (this->is_open())
00703     {
00704       // Ditch any pback buffers to avoid confusion.
00705       _M_destroy_pback();
00706       __ret = _M_seek(off_type(__pos), ios_base::beg, __pos.state());
00707     }
00708       return __ret;
00709     }
00710 
00711   template<typename _CharT, typename _Traits>
00712     typename basic_filebuf<_CharT, _Traits>::pos_type
00713     basic_filebuf<_CharT, _Traits>::
00714     _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state)
00715     {
00716       pos_type __ret = pos_type(off_type(-1));
00717       if (_M_terminate_output())
00718     {
00719       // Returns pos_type(off_type(-1)) in case of failure.
00720       __ret = pos_type(_M_file.seekoff(__off, __way));
00721       _M_reading = false;
00722       _M_writing = false;
00723       _M_ext_next = _M_ext_end = _M_ext_buf;
00724       _M_set_buffer(-1);
00725       _M_state_cur = __state;
00726       __ret.state(_M_state_cur);
00727     }
00728       return __ret;
00729     }
00730 
00731   template<typename _CharT, typename _Traits>
00732     bool
00733     basic_filebuf<_CharT, _Traits>::
00734     _M_terminate_output()
00735     {
00736       // Part one: update the output sequence.
00737       bool __testvalid = true;
00738       if (this->pbase() < this->pptr())
00739     {
00740       const int_type __tmp = this->overflow();
00741       if (traits_type::eq_int_type(__tmp, traits_type::eof()))
00742         __testvalid = false;
00743     }
00744 
00745       // Part two: output unshift sequence.
00746       if (_M_writing && !__check_facet(_M_codecvt).always_noconv()
00747       && __testvalid)
00748     {
00749       // Note: this value is arbitrary, since there is no way to
00750       // get the length of the unshift sequence from codecvt,
00751       // without calling unshift.
00752       const size_t __blen = 128;
00753       char __buf[__blen];
00754       codecvt_base::result __r;
00755       streamsize __ilen = 0;
00756 
00757       do
00758         {
00759           char* __next;
00760           __r = _M_codecvt->unshift(_M_state_cur, __buf,
00761                     __buf + __blen, __next);
00762           if (__r == codecvt_base::error)
00763         __testvalid = false;
00764           else if (__r == codecvt_base::ok ||
00765                __r == codecvt_base::partial)
00766         {
00767           __ilen = __next - __buf;
00768           if (__ilen > 0)
00769             {
00770               const streamsize __elen = _M_file.xsputn(__buf, __ilen);
00771               if (__elen != __ilen)
00772             __testvalid = false;
00773             }
00774         }
00775         }
00776       while (__r == codecvt_base::partial && __ilen > 0 && __testvalid);
00777 
00778       if (__testvalid)
00779         {
00780           // This second call to overflow() is required by the standard,
00781           // but it's not clear why it's needed, since the output buffer
00782           // should be empty by this point (it should have been emptied
00783           // in the first call to overflow()).
00784           const int_type __tmp = this->overflow();
00785           if (traits_type::eq_int_type(__tmp, traits_type::eof()))
00786         __testvalid = false;
00787         }
00788     }
00789       return __testvalid;
00790     }
00791 
00792   template<typename _CharT, typename _Traits>
00793     int
00794     basic_filebuf<_CharT, _Traits>::
00795     sync()
00796     {
00797       // Make sure that the internal buffer resyncs its idea of
00798       // the file position with the external file.
00799       int __ret = 0;
00800       if (this->pbase() < this->pptr())
00801     {
00802       const int_type __tmp = this->overflow();
00803       if (traits_type::eq_int_type(__tmp, traits_type::eof()))
00804         __ret = -1;
00805     }
00806       return __ret;
00807     }
00808 
00809   template<typename _CharT, typename _Traits>
00810     void
00811     basic_filebuf<_CharT, _Traits>::
00812     imbue(const locale& __loc)
00813     {
00814       bool __testvalid = true;
00815 
00816       const __codecvt_type* _M_codecvt_tmp = 0;
00817       if (__builtin_expect(has_facet<__codecvt_type>(__loc), true))
00818     _M_codecvt_tmp = &use_facet<__codecvt_type>(__loc);
00819 
00820       if (this->is_open())
00821     {
00822       // encoding() == -1 is ok only at the beginning.
00823       if ((_M_reading || _M_writing)
00824           && __check_facet(_M_codecvt).encoding() == -1)
00825         __testvalid = false;
00826       else
00827         {
00828           if (_M_reading)
00829         {
00830           if (__check_facet(_M_codecvt).always_noconv())
00831             {
00832               if (_M_codecvt_tmp
00833               && !__check_facet(_M_codecvt_tmp).always_noconv())
00834             __testvalid = this->seekoff(0, ios_base::cur, _M_mode)
00835                           != pos_type(off_type(-1));
00836             }
00837           else
00838             {
00839               // External position corresponding to gptr().
00840               _M_ext_next = _M_ext_buf
00841             + _M_codecvt->length(_M_state_last, _M_ext_buf, _M_ext_next,
00842                          this->gptr() - this->eback());
00843               const streamsize __remainder = _M_ext_end - _M_ext_next;
00844               if (__remainder)
00845             std::memmove(_M_ext_buf, _M_ext_next, __remainder);
00846 
00847               _M_ext_next = _M_ext_buf;
00848               _M_ext_end = _M_ext_buf + __remainder;
00849               _M_set_buffer(-1);
00850               _M_state_last = _M_state_cur = _M_state_beg;
00851             }
00852         }
00853           else if (_M_writing && (__testvalid = _M_terminate_output()))
00854         _M_set_buffer(-1);
00855         }
00856     }
00857 
00858       if (__testvalid)
00859     _M_codecvt = _M_codecvt_tmp;
00860       else
00861     _M_codecvt = 0;
00862     }
00863 
00864   // Inhibit implicit instantiations for required instantiations,
00865   // which are defined via explicit instantiations elsewhere.
00866   // NB:  This syntax is a GNU extension.
00867 #if _GLIBCXX_EXTERN_TEMPLATE
00868   extern template class basic_filebuf<char>;
00869   extern template class basic_ifstream<char>;
00870   extern template class basic_ofstream<char>;
00871   extern template class basic_fstream<char>;
00872 
00873 #ifdef _GLIBCXX_USE_WCHAR_T
00874   extern template class basic_filebuf<wchar_t>;
00875   extern template class basic_ifstream<wchar_t>;
00876   extern template class basic_ofstream<wchar_t>;
00877   extern template class basic_fstream<wchar_t>;
00878 #endif
00879 #endif
00880 } // namespace std
00881 
00882 #endif

Generated on Sat Apr 2 13:54:41 2005 for libstdc++ source by  doxygen 1.4.0