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