1 // File based streams -*- C++ -*-
 
    3 // Copyright (C) 1997-2014 Free Software Foundation, Inc.
 
    5 // This file is part of the GNU ISO C++ Library.  This library is free
 
    6 // software; you can redistribute it and/or modify it under the
 
    7 // terms of the GNU General Public License as published by the
 
    8 // Free Software Foundation; either version 3, or (at your option)
 
   11 // This library is distributed in the hope that it will be useful,
 
   12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
   14 // GNU General Public License for more details.
 
   16 // Under Section 7 of GPL version 3, you are granted additional
 
   17 // permissions described in the GCC Runtime Library Exception, version
 
   18 // 3.1, as published by the Free Software Foundation.
 
   20 // You should have received a copy of the GNU General Public License and
 
   21 // a copy of the GCC Runtime Library Exception along with this program;
 
   22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 
   23 // <http://www.gnu.org/licenses/>.
 
   25 /** @file bits/fstream.tcc
 
   26  *  This is an internal header file, included by other library headers.
 
   27  *  Do not attempt to use it directly. @headername{fstream}
 
   31 // ISO C++ 14882: 27.8  File-based streams
 
   35 #define _FSTREAM_TCC 1
 
   37 #pragma GCC system_header
 
   39 #include <bits/cxxabi_forced.h>
 
   41 namespace std _GLIBCXX_VISIBILITY(default)
 
   43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   45   template<typename _CharT, typename _Traits>
 
   47     basic_filebuf<_CharT, _Traits>::
 
   48     _M_allocate_internal_buffer()
 
   50       // Allocate internal buffer only if one doesn't already exist
 
   51       // (either allocated or provided by the user via setbuf).
 
   52       if (!_M_buf_allocated && !_M_buf)
 
   54      _M_buf = new char_type[_M_buf_size];
 
   55      _M_buf_allocated = true;
 
   59   template<typename _CharT, typename _Traits>
 
   61     basic_filebuf<_CharT, _Traits>::
 
   62     _M_destroy_internal_buffer() throw()
 
   68      _M_buf_allocated = false;
 
   77   template<typename _CharT, typename _Traits>
 
   78     basic_filebuf<_CharT, _Traits>::
 
   79     basic_filebuf() : __streambuf_type(), _M_lock(), _M_file(&_M_lock),
 
   80     _M_mode(ios_base::openmode(0)), _M_state_beg(), _M_state_cur(),
 
   81     _M_state_last(), _M_buf(0), _M_buf_size(BUFSIZ),
 
   82     _M_buf_allocated(false), _M_reading(false), _M_writing(false), _M_pback(), 
 
   83     _M_pback_cur_save(0), _M_pback_end_save(0), _M_pback_init(false),
 
   84     _M_codecvt(0), _M_ext_buf(0), _M_ext_buf_size(0), _M_ext_next(0),
 
   87       if (has_facet<__codecvt_type>(this->_M_buf_locale))
 
   88    _M_codecvt = &use_facet<__codecvt_type>(this->_M_buf_locale);
 
   91   template<typename _CharT, typename _Traits>
 
   92     typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
 
   93     basic_filebuf<_CharT, _Traits>::
 
   94     open(const char* __s, ios_base::openmode __mode)
 
   96       __filebuf_type *__ret = 0;
 
   99      _M_file.open(__s, __mode);
 
  102          _M_allocate_internal_buffer();
 
  105          // Setup initial buffer to 'uncommitted' mode.
 
  110          // Reset to initial state.
 
  111          _M_state_last = _M_state_cur = _M_state_beg;
 
  114          if ((__mode & ios_base::ate)
 
  115          && this->seekoff(0, ios_base::end, __mode)
 
  116          == pos_type(off_type(-1)))
 
  125   template<typename _CharT, typename _Traits>
 
  126     typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
 
  127     basic_filebuf<_CharT, _Traits>::
 
  130       if (!this->is_open())
 
  133       bool __testfail = false;
 
  135    // NB: Do this here so that re-opened filebufs will be cool...
 
  136    struct __close_sentry
 
  139      __close_sentry (basic_filebuf *__fbi): __fb(__fbi) { }
 
  142        __fb->_M_mode = ios_base::openmode(0);
 
  143        __fb->_M_pback_init = false;
 
  144        __fb->_M_destroy_internal_buffer();
 
  145        __fb->_M_reading = false;
 
  146        __fb->_M_writing = false;
 
  147        __fb->_M_set_buffer(-1);
 
  148        __fb->_M_state_last = __fb->_M_state_cur = __fb->_M_state_beg;
 
  154        if (!_M_terminate_output())
 
  157    __catch(__cxxabiv1::__forced_unwind&)
 
  160        __throw_exception_again;
 
  163      { __testfail = true; }
 
  166       if (!_M_file.close())
 
  175   template<typename _CharT, typename _Traits>
 
  177     basic_filebuf<_CharT, _Traits>::
 
  180       streamsize __ret = -1;
 
  181       const bool __testin = _M_mode & ios_base::in;
 
  182       if (__testin && this->is_open())
 
  184      // For a stateful encoding (-1) the pending sequence might be just
 
  185      // shift and unshift prefixes with no actual character.
 
  186      __ret = this->egptr() - this->gptr();
 
  188 #if _GLIBCXX_HAVE_DOS_BASED_FILESYSTEM
 
  189      // About this workaround, see libstdc++/20806.
 
  190      const bool __testbinary = _M_mode & ios_base::binary;
 
  191      if (__check_facet(_M_codecvt).encoding() >= 0
 
  194      if (__check_facet(_M_codecvt).encoding() >= 0)
 
  196        __ret += _M_file.showmanyc() / _M_codecvt->max_length();
 
  201   template<typename _CharT, typename _Traits>
 
  202     typename basic_filebuf<_CharT, _Traits>::int_type
 
  203     basic_filebuf<_CharT, _Traits>::
 
  206       int_type __ret = traits_type::eof();
 
  207       const bool __testin = _M_mode & ios_base::in;
 
  212          if (overflow() == traits_type::eof())
 
  217      // Check for pback madness, and if so switch back to the
 
  218      // normal buffers and jet outta here before expensive
 
  222      if (this->gptr() < this->egptr())
 
  223        return traits_type::to_int_type(*this->gptr());
 
  225      // Get and convert input sequence.
 
  226      const size_t __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1;
 
  228      // Will be set to true if ::read() returns 0 indicating EOF.
 
  229      bool __got_eof = false;
 
  230      // Number of internal characters produced.
 
  231      streamsize __ilen = 0;
 
  232      codecvt_base::result __r = codecvt_base::ok;
 
  233      if (__check_facet(_M_codecvt).always_noconv())
 
  235          __ilen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()),
 
  242               // Worst-case number of external bytes.
 
  243          // XXX Not done encoding() == -1.
 
  244          const int __enc = _M_codecvt->encoding();
 
  245          streamsize __blen; // Minimum buffer size.
 
  246          streamsize __rlen; // Number of chars to read.
 
  248        __blen = __rlen = __buflen * __enc;
 
  251          __blen = __buflen + _M_codecvt->max_length() - 1;
 
  254          const streamsize __remainder = _M_ext_end - _M_ext_next;
 
  255          __rlen = __rlen > __remainder ? __rlen - __remainder : 0;
 
  257          // An imbue in 'read' mode implies first converting the external
 
  258          // chars already present.
 
  259          if (_M_reading && this->egptr() == this->eback() && __remainder)
 
  262          // Allocate buffer if necessary and move unconverted
 
  264          if (_M_ext_buf_size < __blen)
 
  266          char* __buf = new char[__blen];
 
  268            __builtin_memcpy(__buf, _M_ext_next, __remainder);
 
  270          delete [] _M_ext_buf;
 
  272          _M_ext_buf_size = __blen;
 
  274          else if (__remainder)
 
  275        __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder);
 
  277          _M_ext_next = _M_ext_buf;
 
  278          _M_ext_end = _M_ext_buf + __remainder;
 
  279          _M_state_last = _M_state_cur;
 
  286              // This may fail if the return value of
 
  287              // codecvt::max_length() is bogus.
 
  288              if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size)
 
  290              __throw_ios_failure(__N("basic_filebuf::underflow "
 
  291                          "codecvt::max_length() "
 
  294              streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
 
  297              else if (__elen == -1)
 
  299              _M_ext_end += __elen;
 
  302          char_type* __iend = this->eback();
 
  303          if (_M_ext_next < _M_ext_end)
 
  304            __r = _M_codecvt->in(_M_state_cur, _M_ext_next,
 
  305                     _M_ext_end, _M_ext_next,
 
  307                     this->eback() + __buflen, __iend);
 
  308          if (__r == codecvt_base::noconv)
 
  310              size_t __avail = _M_ext_end - _M_ext_buf;
 
  311              __ilen = std::min(__avail, __buflen);
 
  312              traits_type::copy(this->eback(),
 
  313                    reinterpret_cast<char_type*>
 
  314                    (_M_ext_buf), __ilen);
 
  315              _M_ext_next = _M_ext_buf + __ilen;
 
  318            __ilen = __iend - this->eback();
 
  320          // _M_codecvt->in may return error while __ilen > 0: this is
 
  321          // ok, and actually occurs in case of mixed encodings (e.g.,
 
  323          if (__r == codecvt_base::error)
 
  328          while (__ilen == 0 && !__got_eof);
 
  333          _M_set_buffer(__ilen);
 
  335          __ret = traits_type::to_int_type(*this->gptr());
 
  339          // If the actual end of file is reached, set 'uncommitted'
 
  340          // mode, thus allowing an immediate write without an
 
  344          // However, reaching it while looping on partial means that
 
  345          // the file has got an incomplete character.
 
  346          if (__r == codecvt_base::partial)
 
  347        __throw_ios_failure(__N("basic_filebuf::underflow "
 
  348                    "incomplete character in file"));
 
  350      else if (__r == codecvt_base::error)
 
  351        __throw_ios_failure(__N("basic_filebuf::underflow "
 
  352                "invalid byte sequence in file"));
 
  354        __throw_ios_failure(__N("basic_filebuf::underflow "
 
  355                "error reading the file"));
 
  360   template<typename _CharT, typename _Traits>
 
  361     typename basic_filebuf<_CharT, _Traits>::int_type
 
  362     basic_filebuf<_CharT, _Traits>::
 
  363     pbackfail(int_type __i)
 
  365       int_type __ret = traits_type::eof();
 
  366       const bool __testin = _M_mode & ios_base::in;
 
  371          if (overflow() == traits_type::eof())
 
  376      // Remember whether the pback buffer is active, otherwise below
 
  377      // we may try to store in it a second char (libstdc++/9761).
 
  378      const bool __testpb = _M_pback_init;
 
  379      const bool __testeof = traits_type::eq_int_type(__i, __ret);
 
  381      if (this->eback() < this->gptr())
 
  384          __tmp = traits_type::to_int_type(*this->gptr());
 
  386      else if (this->seekoff(-1, ios_base::cur) != pos_type(off_type(-1)))
 
  388          __tmp = this->underflow();
 
  389          if (traits_type::eq_int_type(__tmp, __ret))
 
  394          // At the beginning of the buffer, need to make a
 
  395          // putback position available.  But the seek may fail
 
  396          // (f.i., at the beginning of a file, see
 
  397          // libstdc++/9439) and in that case we return
 
  398          // traits_type::eof().
 
  402      // Try to put back __i into input sequence in one of three ways.
 
  403      // Order these tests done in is unspecified by the standard.
 
  404      if (!__testeof && traits_type::eq_int_type(__i, __tmp))
 
  407        __ret = traits_type::not_eof(__i);
 
  412          *this->gptr() = traits_type::to_char_type(__i);
 
  419   template<typename _CharT, typename _Traits>
 
  420     typename basic_filebuf<_CharT, _Traits>::int_type
 
  421     basic_filebuf<_CharT, _Traits>::
 
  422     overflow(int_type __c)
 
  424       int_type __ret = traits_type::eof();
 
  425       const bool __testeof = traits_type::eq_int_type(__c, __ret);
 
  426       const bool __testout = (_M_mode & ios_base::out
 
  427                  || _M_mode & ios_base::app);
 
  433               const int __gptr_off = _M_get_ext_pos(_M_state_last);
 
  434               if (_M_seek(__gptr_off, ios_base::cur, _M_state_last)
 
  435                   == pos_type(off_type(-1)))
 
  438      if (this->pbase() < this->pptr())
 
  440          // If appropriate, append the overflow char.
 
  443          *this->pptr() = traits_type::to_char_type(__c);
 
  447          // Convert pending sequence to external representation,
 
  449          if (_M_convert_to_external(this->pbase(),
 
  450                     this->pptr() - this->pbase()))
 
  453          __ret = traits_type::not_eof(__c);
 
  456      else if (_M_buf_size > 1)
 
  458          // Overflow in 'uncommitted' mode: set _M_writing, set
 
  459          // the buffer to the initial 'write' mode, and put __c
 
  465          *this->pptr() = traits_type::to_char_type(__c);
 
  468          __ret = traits_type::not_eof(__c);
 
  473          char_type __conv = traits_type::to_char_type(__c);
 
  474          if (__testeof || _M_convert_to_external(&__conv, 1))
 
  477          __ret = traits_type::not_eof(__c);
 
  484   template<typename _CharT, typename _Traits>
 
  486     basic_filebuf<_CharT, _Traits>::
 
  487     _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
 
  489       // Sizes of external and pending output.
 
  492       if (__check_facet(_M_codecvt).always_noconv())
 
  494      __elen = _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
 
  499      // Worst-case number of external bytes needed.
 
  500      // XXX Not done encoding() == -1.
 
  501      streamsize __blen = __ilen * _M_codecvt->max_length();
 
  502      char* __buf = static_cast<char*>(__builtin_alloca(__blen));
 
  505      const char_type* __iend;
 
  506      codecvt_base::result __r;
 
  507      __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
 
  508                __iend, __buf, __buf + __blen, __bend);
 
  510      if (__r == codecvt_base::ok || __r == codecvt_base::partial)
 
  511        __blen = __bend - __buf;
 
  512      else if (__r == codecvt_base::noconv)
 
  514          // Same as the always_noconv case above.
 
  515          __buf = reinterpret_cast<char*>(__ibuf);
 
  519        __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
 
  520                    "conversion error"));
 
  522      __elen = _M_file.xsputn(__buf, __blen);
 
  525      // Try once more for partial conversions.
 
  526      if (__r == codecvt_base::partial && __elen == __plen)
 
  528          const char_type* __iresume = __iend;
 
  529          streamsize __rlen = this->pptr() - __iend;
 
  530          __r = _M_codecvt->out(_M_state_cur, __iresume,
 
  531                    __iresume + __rlen, __iend, __buf,
 
  532                    __buf + __blen, __bend);
 
  533          if (__r != codecvt_base::error)
 
  535          __rlen = __bend - __buf;
 
  536          __elen = _M_file.xsputn(__buf, __rlen);
 
  540        __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
 
  541                    "conversion error"));
 
  544       return __elen == __plen;
 
  547   template<typename _CharT, typename _Traits>
 
  549     basic_filebuf<_CharT, _Traits>::
 
  550     xsgetn(_CharT* __s, streamsize __n)
 
  552       // Clear out pback buffer before going on to the real deal...
 
  553       streamsize __ret = 0;
 
  556      if (__n > 0 && this->gptr() == this->eback())
 
  558          *__s++ = *this->gptr(); // emulate non-underflowing sbumpc
 
  567      if (overflow() == traits_type::eof())
 
  573       // Optimization in the always_noconv() case, to be generalized in the
 
  574       // future: when __n > __buflen we read directly instead of using the
 
  575       // buffer repeatedly.
 
  576       const bool __testin = _M_mode & ios_base::in;
 
  577       const streamsize __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1;
 
  579       if (__n > __buflen && __check_facet(_M_codecvt).always_noconv()
 
  582       // First, copy the chars already present in the buffer.
 
  583       const streamsize __avail = this->egptr() - this->gptr();
 
  586           traits_type::copy(__s, this->gptr(), __avail);
 
  588           this->setg(this->eback(), this->gptr() + __avail,
 
  594       // Need to loop in case of short reads (relatively common
 
  599           __len = _M_file.xsgetn(reinterpret_cast<char*>(__s),
 
  602         __throw_ios_failure(__N("basic_filebuf::xsgetn "
 
  603                     "error reading the file"));
 
  622           // If end of file is reached, set 'uncommitted'
 
  623           // mode, thus allowing an immediate write without
 
  624           // an intervening seek.
 
  630     __ret += __streambuf_type::xsgetn(__s, __n);
 
  635   template<typename _CharT, typename _Traits>
 
  637     basic_filebuf<_CharT, _Traits>::
 
  638     xsputn(const _CharT* __s, streamsize __n)
 
  640       streamsize __ret = 0;
 
  641       // Optimization in the always_noconv() case, to be generalized in the
 
  642       // future: when __n is sufficiently large we write directly instead of
 
  644       const bool __testout = (_M_mode & ios_base::out
 
  645                  || _M_mode & ios_base::app);
 
  646       if (__check_facet(_M_codecvt).always_noconv()
 
  647       && __testout && !_M_reading)
 
  649      // Measurement would reveal the best choice.
 
  650      const streamsize __chunk = 1ul << 10;
 
  651      streamsize __bufavail = this->epptr() - this->pptr();
 
  653      // Don't mistake 'uncommitted' mode buffered with unbuffered.
 
  654      if (!_M_writing && _M_buf_size > 1)
 
  655        __bufavail = _M_buf_size - 1;
 
  657      const streamsize __limit = std::min(__chunk, __bufavail);
 
  660          const streamsize __buffill = this->pptr() - this->pbase();
 
  661          const char* __buf = reinterpret_cast<const char*>(this->pbase());
 
  662          __ret = _M_file.xsputn_2(__buf, __buffill,
 
  663                       reinterpret_cast<const char*>(__s),
 
  665          if (__ret == __buffill + __n)
 
  670          if (__ret > __buffill)
 
  676        __ret = __streambuf_type::xsputn(__s, __n);
 
  679     __ret = __streambuf_type::xsputn(__s, __n);
 
  683   template<typename _CharT, typename _Traits>
 
  684     typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
 
  685     basic_filebuf<_CharT, _Traits>::
 
  686     setbuf(char_type* __s, streamsize __n)
 
  688       if (!this->is_open())
 
  690      if (__s == 0 && __n == 0)
 
  692      else if (__s && __n > 0)
 
  694          // This is implementation-defined behavior, and assumes that
 
  695          // an external char_type array of length __n exists and has
 
  696          // been pre-allocated. If this is not the case, things will
 
  697          // quickly blow up. When __n > 1, __n - 1 positions will be
 
  698          // used for the get area, __n - 1 for the put area and 1
 
  699          // position to host the overflow char of a full put area.
 
  700          // When __n == 1, 1 position will be used for the get area
 
  701          // and 0 for the put area, as in the unbuffered case above.
 
  710   // According to 27.8.1.4 p11 - 13, seekoff should ignore the last
 
  711   // argument (of type openmode).
 
  712   template<typename _CharT, typename _Traits>
 
  713     typename basic_filebuf<_CharT, _Traits>::pos_type
 
  714     basic_filebuf<_CharT, _Traits>::
 
  715     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode)
 
  719    __width = _M_codecvt->encoding();
 
  723       pos_type __ret = pos_type(off_type(-1));
 
  724       const bool __testfail = __off != 0 && __width <= 0;
 
  725       if (this->is_open() && !__testfail)
 
  727      // tellg and tellp queries do not affect any state, unless
 
  728      // ! always_noconv and the put sequence is not empty.
 
  729      // In that case, determining the position requires converting the
 
  730      // put sequence. That doesn't use ext_buf, so requires a flush.
 
  731      bool __no_movement = __way == ios_base::cur && __off == 0
 
  732        && (!_M_writing || _M_codecvt->always_noconv());
 
  734      // Ditch any pback buffers to avoid confusion.
 
  738      // Correct state at destination. Note that this is the correct
 
  739      // state for the current position during output, because
 
  740      // codecvt::unshift() returns the state to the initial state.
 
  741      // This is also the correct state at the end of the file because
 
  742      // an unshift sequence should have been written at the end.
 
  743      __state_type __state = _M_state_beg;
 
  744      off_type __computed_off = __off * __width;
 
  745      if (_M_reading && __way == ios_base::cur)
 
  747          __state = _M_state_last;
 
  748          __computed_off += _M_get_ext_pos(__state);
 
  751        __ret = _M_seek(__computed_off, __way, __state);
 
  755        __computed_off = this->pptr() - this->pbase();
 
  757          off_type __file_off = _M_file.seekoff(0, ios_base::cur);
 
  758          if (__file_off != off_type(-1))
 
  760          __ret = __file_off + __computed_off;
 
  761          __ret.state(__state);
 
  768   // _GLIBCXX_RESOLVE_LIB_DEFECTS
 
  769   // 171. Strange seekpos() semantics due to joint position
 
  770   // According to the resolution of DR 171, seekpos should ignore the last
 
  771   // argument (of type openmode).
 
  772   template<typename _CharT, typename _Traits>
 
  773     typename basic_filebuf<_CharT, _Traits>::pos_type
 
  774     basic_filebuf<_CharT, _Traits>::
 
  775     seekpos(pos_type __pos, ios_base::openmode)
 
  777       pos_type __ret =  pos_type(off_type(-1));
 
  780      // Ditch any pback buffers to avoid confusion.
 
  782      __ret = _M_seek(off_type(__pos), ios_base::beg, __pos.state());
 
  787   template<typename _CharT, typename _Traits>
 
  788     typename basic_filebuf<_CharT, _Traits>::pos_type
 
  789     basic_filebuf<_CharT, _Traits>::
 
  790     _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state)
 
  792       pos_type __ret = pos_type(off_type(-1));
 
  793       if (_M_terminate_output())
 
  795      off_type __file_off = _M_file.seekoff(__off, __way);
 
  796      if (__file_off != off_type(-1))
 
  800          _M_ext_next = _M_ext_end = _M_ext_buf;
 
  802          _M_state_cur = __state;
 
  804          __ret.state(_M_state_cur);
 
  810   // Returns the distance from the end of the ext buffer to the point
 
  811   // corresponding to gptr(). This is a negative value. Updates __state
 
  812   // from eback() correspondence to gptr().
 
  813   template<typename _CharT, typename _Traits>
 
  814     int basic_filebuf<_CharT, _Traits>::
 
  815     _M_get_ext_pos(__state_type& __state)
 
  817       if (_M_codecvt->always_noconv())
 
  818         return this->gptr() - this->egptr();
 
  821           // Calculate offset from _M_ext_buf that corresponds to
 
  822           // gptr(). Precondition: __state == _M_state_last, which
 
  823           // corresponds to eback().
 
  824           const int __gptr_off =
 
  825             _M_codecvt->length(__state, _M_ext_buf, _M_ext_next,
 
  826                                this->gptr() - this->eback());
 
  827           return _M_ext_buf + __gptr_off - _M_ext_end;
 
  831   template<typename _CharT, typename _Traits>
 
  833     basic_filebuf<_CharT, _Traits>::
 
  834     _M_terminate_output()
 
  836       // Part one: update the output sequence.
 
  837       bool __testvalid = true;
 
  838       if (this->pbase() < this->pptr())
 
  840      const int_type __tmp = this->overflow();
 
  841      if (traits_type::eq_int_type(__tmp, traits_type::eof()))
 
  845       // Part two: output unshift sequence.
 
  846       if (_M_writing && !__check_facet(_M_codecvt).always_noconv()
 
  849      // Note: this value is arbitrary, since there is no way to
 
  850      // get the length of the unshift sequence from codecvt,
 
  851      // without calling unshift.
 
  852      const size_t __blen = 128;
 
  854      codecvt_base::result __r;
 
  855      streamsize __ilen = 0;
 
  860          __r = _M_codecvt->unshift(_M_state_cur, __buf,
 
  861                    __buf + __blen, __next);
 
  862          if (__r == codecvt_base::error)
 
  864          else if (__r == codecvt_base::ok ||
 
  865               __r == codecvt_base::partial)
 
  867          __ilen = __next - __buf;
 
  870              const streamsize __elen = _M_file.xsputn(__buf, __ilen);
 
  871              if (__elen != __ilen)
 
  876      while (__r == codecvt_base::partial && __ilen > 0 && __testvalid);
 
  880          // This second call to overflow() is required by the standard,
 
  881          // but it's not clear why it's needed, since the output buffer
 
  882          // should be empty by this point (it should have been emptied
 
  883          // in the first call to overflow()).
 
  884          const int_type __tmp = this->overflow();
 
  885          if (traits_type::eq_int_type(__tmp, traits_type::eof()))
 
  892   template<typename _CharT, typename _Traits>
 
  894     basic_filebuf<_CharT, _Traits>::
 
  897       // Make sure that the internal buffer resyncs its idea of
 
  898       // the file position with the external file.
 
  900       if (this->pbase() < this->pptr())
 
  902      const int_type __tmp = this->overflow();
 
  903      if (traits_type::eq_int_type(__tmp, traits_type::eof()))
 
  909   template<typename _CharT, typename _Traits>
 
  911     basic_filebuf<_CharT, _Traits>::
 
  912     imbue(const locale& __loc)
 
  914       bool __testvalid = true;
 
  916       const __codecvt_type* _M_codecvt_tmp = 0;
 
  917       if (__builtin_expect(has_facet<__codecvt_type>(__loc), true))
 
  918    _M_codecvt_tmp = &use_facet<__codecvt_type>(__loc);
 
  922      // encoding() == -1 is ok only at the beginning.
 
  923      if ((_M_reading || _M_writing)
 
  924          && __check_facet(_M_codecvt).encoding() == -1)
 
  930          if (__check_facet(_M_codecvt).always_noconv())
 
  933              && !__check_facet(_M_codecvt_tmp).always_noconv())
 
  934            __testvalid = this->seekoff(0, ios_base::cur, _M_mode)
 
  935                          != pos_type(off_type(-1));
 
  939              // External position corresponding to gptr().
 
  940              _M_ext_next = _M_ext_buf
 
  941            + _M_codecvt->length(_M_state_last, _M_ext_buf,
 
  943                         this->gptr() - this->eback());
 
  944              const streamsize __remainder = _M_ext_end - _M_ext_next;
 
  946            __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder);
 
  948              _M_ext_next = _M_ext_buf;
 
  949              _M_ext_end = _M_ext_buf + __remainder;
 
  951              _M_state_last = _M_state_cur = _M_state_beg;
 
  954          else if (_M_writing && (__testvalid = _M_terminate_output()))
 
  960    _M_codecvt = _M_codecvt_tmp;
 
  965   // Inhibit implicit instantiations for required instantiations,
 
  966   // which are defined via explicit instantiations elsewhere.
 
  967 #if _GLIBCXX_EXTERN_TEMPLATE
 
  968   extern template class basic_filebuf<char>;
 
  969   extern template class basic_ifstream<char>;
 
  970   extern template class basic_ofstream<char>;
 
  971   extern template class basic_fstream<char>;
 
  973 #ifdef _GLIBCXX_USE_WCHAR_T
 
  974   extern template class basic_filebuf<wchar_t>;
 
  975   extern template class basic_ifstream<wchar_t>;
 
  976   extern template class basic_ofstream<wchar_t>;
 
  977   extern template class basic_fstream<wchar_t>;
 
  981 _GLIBCXX_END_NAMESPACE_VERSION