Merge from mainline (163495:164578).
[official-gcc/graphite-test-results.git] / libstdc++-v3 / include / bits / fstream.tcc
blob2f661ebd23e99c379b6a57041002f1050c9d23f0
1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 // 2007, 2008, 2009, 2010
5 // Free Software Foundation, Inc.
6 //
7 // This file is part of the GNU ISO C++ Library.  This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file fstream.tcc
28  *  This is an internal header file, included by other library headers.
29  *  You should not attempt to use it directly.
30  */
33 // ISO C++ 14882: 27.8  File-based streams
36 #ifndef _FSTREAM_TCC
37 #define _FSTREAM_TCC 1
39 #pragma GCC system_header
41 #include <cxxabi-forced.h>
43 _GLIBCXX_BEGIN_NAMESPACE(std)
45   template<typename _CharT, typename _Traits>
46     void
47     basic_filebuf<_CharT, _Traits>::
48     _M_allocate_internal_buffer()
49     {
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)
53         {
54           _M_buf = new char_type[_M_buf_size];
55           _M_buf_allocated = true;
56         }
57     }
59   template<typename _CharT, typename _Traits>
60     void
61     basic_filebuf<_CharT, _Traits>::
62     _M_destroy_internal_buffer() throw()
63     {
64       if (_M_buf_allocated)
65         {
66           delete [] _M_buf;
67           _M_buf = 0;
68           _M_buf_allocated = false;
69         }
70       delete [] _M_ext_buf;
71       _M_ext_buf = 0;
72       _M_ext_buf_size = 0;
73       _M_ext_next = 0;
74       _M_ext_end = 0;
75     }
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),
85     _M_ext_end(0)
86     {
87       if (has_facet<__codecvt_type>(this->_M_buf_locale))
88         _M_codecvt = &use_facet<__codecvt_type>(this->_M_buf_locale);
89     }
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)
95     {
96       __filebuf_type *__ret = 0;
97       if (!this->is_open())
98         {
99           _M_file.open(__s, __mode);
100           if (this->is_open())
101             {
102               _M_allocate_internal_buffer();
103               _M_mode = __mode;
105               // Setup initial buffer to 'uncommitted' mode.
106               _M_reading = false;
107               _M_writing = false;
108               _M_set_buffer(-1);
110               // Reset to initial state.
111               _M_state_last = _M_state_cur = _M_state_beg;
113               // 27.8.1.3,4
114               if ((__mode & ios_base::ate)
115                   && this->seekoff(0, ios_base::end, __mode)
116                   == pos_type(off_type(-1)))
117                 this->close();
118               else
119                 __ret = this;
120             }
121         }
122       return __ret;
123     }
125   template<typename _CharT, typename _Traits>
126     typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
127     basic_filebuf<_CharT, _Traits>::
128     close()
129     {
130       if (!this->is_open())
131         return 0;
133       bool __testfail = false;
134       {
135         // NB: Do this here so that re-opened filebufs will be cool...
136         struct __close_sentry
137         {
138           basic_filebuf *__fb;
139           __close_sentry (basic_filebuf *__fbi): __fb(__fbi) { }
140           ~__close_sentry ()
141           {
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;
149           }
150         } __cs (this);
152         __try
153           {
154             if (!_M_terminate_output())
155               __testfail = true;
156           }
157         __catch(__cxxabiv1::__forced_unwind&)
158           {
159             _M_file.close();
160             __throw_exception_again;
161           }
162         __catch(...)
163           { __testfail = true; }
164       }
166       if (!_M_file.close())
167         __testfail = true;
169       if (__testfail)
170         return 0;
171       else
172         return this;
173     }
175   template<typename _CharT, typename _Traits>
176     streamsize
177     basic_filebuf<_CharT, _Traits>::
178     showmanyc()
179     {
180       streamsize __ret = -1;
181       const bool __testin = _M_mode & ios_base::in;
182       if (__testin && this->is_open())
183         {
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
192               && __testbinary)
193 #else
194           if (__check_facet(_M_codecvt).encoding() >= 0)
195 #endif
196             __ret += _M_file.showmanyc() / _M_codecvt->max_length();
197         }
198       return __ret;
199     }
201   template<typename _CharT, typename _Traits>
202     typename basic_filebuf<_CharT, _Traits>::int_type
203     basic_filebuf<_CharT, _Traits>::
204     underflow()
205     {
206       int_type __ret = traits_type::eof();
207       const bool __testin = _M_mode & ios_base::in;
208       if (__testin)
209         {
210          if (_M_writing)
211            {
212              __ret = overflow();
213              if (__ret == traits_type::eof())
214                return __ret;
215              _M_set_buffer(-1);
216              _M_writing = false;
217            }
218           // Check for pback madness, and if so switch back to the
219           // normal buffers and jet outta here before expensive
220           // fileops happen...
221           _M_destroy_pback();
223           if (this->gptr() < this->egptr())
224             return traits_type::to_int_type(*this->gptr());
226           // Get and convert input sequence.
227           const size_t __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1;
229           // Will be set to true if ::read() returns 0 indicating EOF.
230           bool __got_eof = false;
231           // Number of internal characters produced.
232           streamsize __ilen = 0;
233           codecvt_base::result __r = codecvt_base::ok;
234           if (__check_facet(_M_codecvt).always_noconv())
235             {
236               __ilen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()),
237                                       __buflen);
238               if (__ilen == 0)
239                 __got_eof = true;
240             }
241           else
242             {
243               // Worst-case number of external bytes.
244               // XXX Not done encoding() == -1.
245               const int __enc = _M_codecvt->encoding();
246               streamsize __blen; // Minimum buffer size.
247               streamsize __rlen; // Number of chars to read.
248               if (__enc > 0)
249                 __blen = __rlen = __buflen * __enc;
250               else
251                 {
252                   __blen = __buflen + _M_codecvt->max_length() - 1;
253                   __rlen = __buflen;
254                 }
255               const streamsize __remainder = _M_ext_end - _M_ext_next;
256               __rlen = __rlen > __remainder ? __rlen - __remainder : 0;
258               // An imbue in 'read' mode implies first converting the external
259               // chars already present.
260               if (_M_reading && this->egptr() == this->eback() && __remainder)
261                 __rlen = 0;
263               // Allocate buffer if necessary and move unconverted
264               // bytes to front.
265               if (_M_ext_buf_size < __blen)
266                 {
267                   char* __buf = new char[__blen];
268                   if (__remainder)
269                     __builtin_memcpy(__buf, _M_ext_next, __remainder);
271                   delete [] _M_ext_buf;
272                   _M_ext_buf = __buf;
273                   _M_ext_buf_size = __blen;
274                 }
275               else if (__remainder)
276                 __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder);
278               _M_ext_next = _M_ext_buf;
279               _M_ext_end = _M_ext_buf + __remainder;
280               _M_state_last = _M_state_cur;
282               do
283                 {
284                   if (__rlen > 0)
285                     {
286                       // Sanity check!
287                       // This may fail if the return value of
288                       // codecvt::max_length() is bogus.
289                       if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size)
290                         {
291                           __throw_ios_failure(__N("basic_filebuf::underflow "
292                                               "codecvt::max_length() "
293                                               "is not valid"));
294                         }
295                       streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
296                       if (__elen == 0)
297                         __got_eof = true;
298                       else if (__elen == -1)
299                         break;
300                       _M_ext_end += __elen;
301                     }
303                   char_type* __iend = this->eback();
304                   if (_M_ext_next < _M_ext_end)
305                     __r = _M_codecvt->in(_M_state_cur, _M_ext_next,
306                                          _M_ext_end, _M_ext_next,
307                                          this->eback(),
308                                          this->eback() + __buflen, __iend);
309                   if (__r == codecvt_base::noconv)
310                     {
311                       size_t __avail = _M_ext_end - _M_ext_buf;
312                       __ilen = std::min(__avail, __buflen);
313                       traits_type::copy(this->eback(),
314                                         reinterpret_cast<char_type*>
315                                         (_M_ext_buf), __ilen);
316                       _M_ext_next = _M_ext_buf + __ilen;
317                     }
318                   else
319                     __ilen = __iend - this->eback();
321                   // _M_codecvt->in may return error while __ilen > 0: this is
322                   // ok, and actually occurs in case of mixed encodings (e.g.,
323                   // XML files).
324                   if (__r == codecvt_base::error)
325                     break;
327                   __rlen = 1;
328                 }
329               while (__ilen == 0 && !__got_eof);
330             }
332           if (__ilen > 0)
333             {
334               _M_set_buffer(__ilen);
335               _M_reading = true;
336               __ret = traits_type::to_int_type(*this->gptr());
337             }
338           else if (__got_eof)
339             {
340               // If the actual end of file is reached, set 'uncommitted'
341               // mode, thus allowing an immediate write without an
342               // intervening seek.
343               _M_set_buffer(-1);
344               _M_reading = false;
345               // However, reaching it while looping on partial means that
346               // the file has got an incomplete character.
347               if (__r == codecvt_base::partial)
348                 __throw_ios_failure(__N("basic_filebuf::underflow "
349                                     "incomplete character in file"));
350             }
351           else if (__r == codecvt_base::error)
352             __throw_ios_failure(__N("basic_filebuf::underflow "
353                                 "invalid byte sequence in file"));
354           else
355             __throw_ios_failure(__N("basic_filebuf::underflow "
356                                 "error reading the file"));
357         }
358       return __ret;
359     }
361   template<typename _CharT, typename _Traits>
362     typename basic_filebuf<_CharT, _Traits>::int_type
363     basic_filebuf<_CharT, _Traits>::
364     pbackfail(int_type __i)
365     {
366       int_type __ret = traits_type::eof();
367       const bool __testin = _M_mode & ios_base::in;
368       if (__testin)
369         {
370          if (_M_writing)
371            {
372              __ret = overflow();
373              if (__ret == traits_type::eof())
374                return __ret;
375              _M_set_buffer(-1);
376              _M_writing = false;
377            }
378           // Remember whether the pback buffer is active, otherwise below
379           // we may try to store in it a second char (libstdc++/9761).
380           const bool __testpb = _M_pback_init;
381           const bool __testeof = traits_type::eq_int_type(__i, __ret);
382           int_type __tmp;
383           if (this->eback() < this->gptr())
384             {
385               this->gbump(-1);
386               __tmp = traits_type::to_int_type(*this->gptr());
387             }
388           else if (this->seekoff(-1, ios_base::cur) != pos_type(off_type(-1)))
389             {
390               __tmp = this->underflow();
391               if (traits_type::eq_int_type(__tmp, __ret))
392                 return __ret;
393             }
394           else
395             {
396               // At the beginning of the buffer, need to make a
397               // putback position available.  But the seek may fail
398               // (f.i., at the beginning of a file, see
399               // libstdc++/9439) and in that case we return
400               // traits_type::eof().
401               return __ret;
402             }
404           // Try to put back __i into input sequence in one of three ways.
405           // Order these tests done in is unspecified by the standard.
406           if (!__testeof && traits_type::eq_int_type(__i, __tmp))
407             __ret = __i;
408           else if (__testeof)
409             __ret = traits_type::not_eof(__i);
410           else if (!__testpb)
411             {
412               _M_create_pback();
413               _M_reading = true;
414               *this->gptr() = traits_type::to_char_type(__i);
415               __ret = __i;
416             }
417         }
418       return __ret;
419     }
421   template<typename _CharT, typename _Traits>
422     typename basic_filebuf<_CharT, _Traits>::int_type
423     basic_filebuf<_CharT, _Traits>::
424     overflow(int_type __c)
425     {
426       int_type __ret = traits_type::eof();
427       const bool __testeof = traits_type::eq_int_type(__c, __ret);
428       const bool __testout = _M_mode & ios_base::out;
429       if (__testout)
430         {
431           if (_M_reading)
432             {
433               _M_destroy_pback();
434               const int __gptr_off = _M_get_ext_pos(_M_state_last);
435               if (_M_seek(__gptr_off, ios_base::cur, _M_state_last)
436                   == pos_type(off_type(-1)))
437                 return __ret;
438             }
439           if (this->pbase() < this->pptr())
440             {
441               // If appropriate, append the overflow char.
442               if (!__testeof)
443                 {
444                   *this->pptr() = traits_type::to_char_type(__c);
445                   this->pbump(1);
446                 }
448               // Convert pending sequence to external representation,
449               // and output.
450               if (_M_convert_to_external(this->pbase(),
451                                          this->pptr() - this->pbase()))
452                 {
453                   _M_set_buffer(0);
454                   __ret = traits_type::not_eof(__c);
455                 }
456             }
457           else if (_M_buf_size > 1)
458             {
459               // Overflow in 'uncommitted' mode: set _M_writing, set
460               // the buffer to the initial 'write' mode, and put __c
461               // into the buffer.
462               _M_set_buffer(0);
463               _M_writing = true;
464               if (!__testeof)
465                 {
466                   *this->pptr() = traits_type::to_char_type(__c);
467                   this->pbump(1);
468                 }
469               __ret = traits_type::not_eof(__c);
470             }
471           else
472             {
473               // Unbuffered.
474               char_type __conv = traits_type::to_char_type(__c);
475               if (__testeof || _M_convert_to_external(&__conv, 1))
476                 {
477                   _M_writing = true;
478                   __ret = traits_type::not_eof(__c);
479                 }
480             }
481         }
482       return __ret;
483     }
485   template<typename _CharT, typename _Traits>
486     bool
487     basic_filebuf<_CharT, _Traits>::
488     _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
489     {
490       // Sizes of external and pending output.
491       streamsize __elen;
492       streamsize __plen;
493       if (__check_facet(_M_codecvt).always_noconv())
494         {
495           __elen = _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
496           __plen = __ilen;
497         }
498       else
499         {
500           // Worst-case number of external bytes needed.
501           // XXX Not done encoding() == -1.
502           streamsize __blen = __ilen * _M_codecvt->max_length();
503           char* __buf = static_cast<char*>(__builtin_alloca(__blen));
505           char* __bend;
506           const char_type* __iend;
507           codecvt_base::result __r;
508           __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
509                                 __iend, __buf, __buf + __blen, __bend);
511           if (__r == codecvt_base::ok || __r == codecvt_base::partial)
512             __blen = __bend - __buf;
513           else if (__r == codecvt_base::noconv)
514             {
515               // Same as the always_noconv case above.
516               __buf = reinterpret_cast<char*>(__ibuf);
517               __blen = __ilen;
518             }
519           else
520             __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
521                                     "conversion error"));
522   
523           __elen = _M_file.xsputn(__buf, __blen);
524           __plen = __blen;
526           // Try once more for partial conversions.
527           if (__r == codecvt_base::partial && __elen == __plen)
528             {
529               const char_type* __iresume = __iend;
530               streamsize __rlen = this->pptr() - __iend;
531               __r = _M_codecvt->out(_M_state_cur, __iresume,
532                                     __iresume + __rlen, __iend, __buf,
533                                     __buf + __blen, __bend);
534               if (__r != codecvt_base::error)
535                 {
536                   __rlen = __bend - __buf;
537                   __elen = _M_file.xsputn(__buf, __rlen);
538                   __plen = __rlen;
539                 }
540               else
541                 __throw_ios_failure(__N("basic_filebuf::_M_convert_to_external "
542                                         "conversion error"));
543             }
544         }
545       return __elen == __plen;
546     }
548    template<typename _CharT, typename _Traits>
549      streamsize
550      basic_filebuf<_CharT, _Traits>::
551      xsgetn(_CharT* __s, streamsize __n)
552      {
553        // Clear out pback buffer before going on to the real deal...
554        streamsize __ret = 0;
555        if (_M_pback_init)
556          {
557            if (__n > 0 && this->gptr() == this->eback())
558              {
559                *__s++ = *this->gptr();
560                this->gbump(1);
561                __ret = 1;
562                --__n;
563              }
564            _M_destroy_pback();
565          }
566        
567        // Optimization in the always_noconv() case, to be generalized in the
568        // future: when __n > __buflen we read directly instead of using the
569        // buffer repeatedly.
570        const bool __testin = _M_mode & ios_base::in;
571        const streamsize __buflen = _M_buf_size > 1 ? _M_buf_size - 1 : 1;
573        if (__n > __buflen && __check_facet(_M_codecvt).always_noconv()
574            && __testin && !_M_writing)
575          {
576            // First, copy the chars already present in the buffer.
577            const streamsize __avail = this->egptr() - this->gptr();
578            if (__avail != 0)
579              {
580                if (__avail == 1)
581                  *__s = *this->gptr();
582                else
583                  traits_type::copy(__s, this->gptr(), __avail);
584                __s += __avail;
585                this->gbump(__avail);
586                __ret += __avail;
587                __n -= __avail;
588              }
590            // Need to loop in case of short reads (relatively common
591            // with pipes).
592            streamsize __len;
593            for (;;)
594              {
595                __len = _M_file.xsgetn(reinterpret_cast<char*>(__s),
596                                       __n);
597                if (__len == -1)
598                  __throw_ios_failure(__N("basic_filebuf::xsgetn "
599                                          "error reading the file"));
600                if (__len == 0)
601                  break;
603                __n -= __len;
604                __ret += __len;
605                if (__n == 0)
606                  break;
608                __s += __len;
609              }
611            if (__n == 0)
612              {
613                _M_set_buffer(0);
614                _M_reading = true;
615              }
616            else if (__len == 0)
617              {
618                // If end of file is reached, set 'uncommitted'
619                // mode, thus allowing an immediate write without
620                // an intervening seek.
621                _M_set_buffer(-1);
622                _M_reading = false;
623              }
624          }
625        else
626          __ret += __streambuf_type::xsgetn(__s, __n);
628        return __ret;
629      }
631    template<typename _CharT, typename _Traits>
632      streamsize
633      basic_filebuf<_CharT, _Traits>::
634      xsputn(const _CharT* __s, streamsize __n)
635      {
636        // Optimization in the always_noconv() case, to be generalized in the
637        // future: when __n is sufficiently large we write directly instead of
638        // using the buffer.
639        streamsize __ret = 0;
640        const bool __testout = _M_mode & ios_base::out;
641        if (__check_facet(_M_codecvt).always_noconv()
642            && __testout && !_M_reading)
643         {
644           // Measurement would reveal the best choice.
645           const streamsize __chunk = 1ul << 10;
646           streamsize __bufavail = this->epptr() - this->pptr();
648           // Don't mistake 'uncommitted' mode buffered with unbuffered.
649           if (!_M_writing && _M_buf_size > 1)
650             __bufavail = _M_buf_size - 1;
652           const streamsize __limit = std::min(__chunk, __bufavail);
653           if (__n >= __limit)
654             {
655               const streamsize __buffill = this->pptr() - this->pbase();
656               const char* __buf = reinterpret_cast<const char*>(this->pbase());
657               __ret = _M_file.xsputn_2(__buf, __buffill,
658                                        reinterpret_cast<const char*>(__s),
659                                        __n);
660               if (__ret == __buffill + __n)
661                 {
662                   _M_set_buffer(0);
663                   _M_writing = true;
664                 }
665               if (__ret > __buffill)
666                 __ret -= __buffill;
667               else
668                 __ret = 0;
669             }
670           else
671             __ret = __streambuf_type::xsputn(__s, __n);
672         }
673        else
674          __ret = __streambuf_type::xsputn(__s, __n);
675        return __ret;
676     }
678   template<typename _CharT, typename _Traits>
679     typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
680     basic_filebuf<_CharT, _Traits>::
681     setbuf(char_type* __s, streamsize __n)
682     {
683       if (!this->is_open())
684         {
685           if (__s == 0 && __n == 0)
686             _M_buf_size = 1;
687           else if (__s && __n > 0)
688             {
689               // This is implementation-defined behavior, and assumes that
690               // an external char_type array of length __n exists and has
691               // been pre-allocated. If this is not the case, things will
692               // quickly blow up. When __n > 1, __n - 1 positions will be
693               // used for the get area, __n - 1 for the put area and 1
694               // position to host the overflow char of a full put area.
695               // When __n == 1, 1 position will be used for the get area
696               // and 0 for the put area, as in the unbuffered case above.
697               _M_buf = __s;
698               _M_buf_size = __n;
699             }
700         }
701       return this;
702     }
705   // According to 27.8.1.4 p11 - 13, seekoff should ignore the last
706   // argument (of type openmode).
707   template<typename _CharT, typename _Traits>
708     typename basic_filebuf<_CharT, _Traits>::pos_type
709     basic_filebuf<_CharT, _Traits>::
710     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode)
711     {
712       int __width = 0;
713       if (_M_codecvt)
714         __width = _M_codecvt->encoding();
715       if (__width < 0)
716         __width = 0;
718       pos_type __ret = pos_type(off_type(-1));
719       const bool __testfail = __off != 0 && __width <= 0;
720       if (this->is_open() && !__testfail)
721         {
722           // tellg and tellp queries do not affect any state, unless
723           // ! always_noconv and the put sequence is not empty.
724           // In that case, determining the position requires converting the
725           // put sequence. That doesn't use ext_buf, so requires a flush.
726           bool __no_movement = __way == ios_base::cur && __off == 0
727             && (!_M_writing || _M_codecvt->always_noconv());
729           // Ditch any pback buffers to avoid confusion.
730           if (!__no_movement)
731             _M_destroy_pback();
733           // Correct state at destination. Note that this is the correct
734           // state for the current position during output, because
735           // codecvt::unshift() returns the state to the initial state.
736           // This is also the correct state at the end of the file because
737           // an unshift sequence should have been written at the end.
738           __state_type __state = _M_state_beg;
739           off_type __computed_off = __off * __width;
740           if (_M_reading && __way == ios_base::cur)
741             {
742               __state = _M_state_last;
743               __computed_off += _M_get_ext_pos(__state);
744             }
745           if (!__no_movement)
746             __ret = _M_seek(__computed_off, __way, __state);
747           else
748             {
749               if (_M_writing)
750                 __computed_off = this->pptr() - this->pbase();
751               
752               off_type __file_off = _M_file.seekoff(0, ios_base::cur);
753               if (__file_off != off_type(-1))
754                 {
755                   __ret = __file_off + __computed_off;
756                   __ret.state(__state);
757                 }
758             }
759         }
760       return __ret;
761     }
763   // _GLIBCXX_RESOLVE_LIB_DEFECTS
764   // 171. Strange seekpos() semantics due to joint position
765   // According to the resolution of DR 171, seekpos should ignore the last
766   // argument (of type openmode).
767   template<typename _CharT, typename _Traits>
768     typename basic_filebuf<_CharT, _Traits>::pos_type
769     basic_filebuf<_CharT, _Traits>::
770     seekpos(pos_type __pos, ios_base::openmode)
771     {
772       pos_type __ret =  pos_type(off_type(-1));
773       if (this->is_open())
774         {
775           // Ditch any pback buffers to avoid confusion.
776           _M_destroy_pback();
777           __ret = _M_seek(off_type(__pos), ios_base::beg, __pos.state());
778         }
779       return __ret;
780     }
782   template<typename _CharT, typename _Traits>
783     typename basic_filebuf<_CharT, _Traits>::pos_type
784     basic_filebuf<_CharT, _Traits>::
785     _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state)
786     {
787       pos_type __ret = pos_type(off_type(-1));
788       if (_M_terminate_output())
789         {
790           off_type __file_off = _M_file.seekoff(__off, __way);
791           if (__file_off != off_type(-1))
792             {
793               _M_reading = false;
794               _M_writing = false;
795               _M_ext_next = _M_ext_end = _M_ext_buf;
796               _M_set_buffer(-1);
797               _M_state_cur = __state;
798               __ret = __file_off;
799               __ret.state(_M_state_cur);
800             }
801         }
802       return __ret;
803     }
805   // Returns the distance from the end of the ext buffer to the point
806   // corresponding to gptr(). This is a negative value. Updates __state
807   // from eback() correspondence to gptr().
808   template<typename _CharT, typename _Traits>
809     int basic_filebuf<_CharT, _Traits>::
810     _M_get_ext_pos(__state_type& __state)
811     {
812       if (_M_codecvt->always_noconv())
813         return this->gptr() - this->egptr();
814       else
815         {
816           // Calculate offset from _M_ext_buf that corresponds to
817           // gptr(). Precondition: __state == _M_state_last, which
818           // corresponds to eback().
819           const int __gptr_off =
820             _M_codecvt->length(__state, _M_ext_buf, _M_ext_next,
821                                this->gptr() - this->eback());
822           return _M_ext_buf + __gptr_off - _M_ext_end;
823         }
824     }
825     
826   template<typename _CharT, typename _Traits>
827     bool
828     basic_filebuf<_CharT, _Traits>::
829     _M_terminate_output()
830     {
831       // Part one: update the output sequence.
832       bool __testvalid = true;
833       if (this->pbase() < this->pptr())
834         {
835           const int_type __tmp = this->overflow();
836           if (traits_type::eq_int_type(__tmp, traits_type::eof()))
837             __testvalid = false;
838         }
840       // Part two: output unshift sequence.
841       if (_M_writing && !__check_facet(_M_codecvt).always_noconv()
842           && __testvalid)
843         {
844           // Note: this value is arbitrary, since there is no way to
845           // get the length of the unshift sequence from codecvt,
846           // without calling unshift.
847           const size_t __blen = 128;
848           char __buf[__blen];
849           codecvt_base::result __r;
850           streamsize __ilen = 0;
852           do
853             {
854               char* __next;
855               __r = _M_codecvt->unshift(_M_state_cur, __buf,
856                                         __buf + __blen, __next);
857               if (__r == codecvt_base::error)
858                 __testvalid = false;
859               else if (__r == codecvt_base::ok ||
860                        __r == codecvt_base::partial)
861                 {
862                   __ilen = __next - __buf;
863                   if (__ilen > 0)
864                     {
865                       const streamsize __elen = _M_file.xsputn(__buf, __ilen);
866                       if (__elen != __ilen)
867                         __testvalid = false;
868                     }
869                 }
870             }
871           while (__r == codecvt_base::partial && __ilen > 0 && __testvalid);
873           if (__testvalid)
874             {
875               // This second call to overflow() is required by the standard,
876               // but it's not clear why it's needed, since the output buffer
877               // should be empty by this point (it should have been emptied
878               // in the first call to overflow()).
879               const int_type __tmp = this->overflow();
880               if (traits_type::eq_int_type(__tmp, traits_type::eof()))
881                 __testvalid = false;
882             }
883         }
884       return __testvalid;
885     }
887   template<typename _CharT, typename _Traits>
888     int
889     basic_filebuf<_CharT, _Traits>::
890     sync()
891     {
892       // Make sure that the internal buffer resyncs its idea of
893       // the file position with the external file.
894       int __ret = 0;
895       if (this->pbase() < this->pptr())
896         {
897           const int_type __tmp = this->overflow();
898           if (traits_type::eq_int_type(__tmp, traits_type::eof()))
899             __ret = -1;
900         }
901       return __ret;
902     }
904   template<typename _CharT, typename _Traits>
905     void
906     basic_filebuf<_CharT, _Traits>::
907     imbue(const locale& __loc)
908     {
909       bool __testvalid = true;
911       const __codecvt_type* _M_codecvt_tmp = 0;
912       if (__builtin_expect(has_facet<__codecvt_type>(__loc), true))
913         _M_codecvt_tmp = &use_facet<__codecvt_type>(__loc);
915       if (this->is_open())
916         {
917           // encoding() == -1 is ok only at the beginning.
918           if ((_M_reading || _M_writing)
919               && __check_facet(_M_codecvt).encoding() == -1)
920             __testvalid = false;
921           else
922             {
923               if (_M_reading)
924                 {
925                   if (__check_facet(_M_codecvt).always_noconv())
926                     {
927                       if (_M_codecvt_tmp
928                           && !__check_facet(_M_codecvt_tmp).always_noconv())
929                         __testvalid = this->seekoff(0, ios_base::cur, _M_mode)
930                                       != pos_type(off_type(-1));
931                     }
932                   else
933                     {
934                       // External position corresponding to gptr().
935                       _M_ext_next = _M_ext_buf
936                         + _M_codecvt->length(_M_state_last, _M_ext_buf, _M_ext_next,
937                                              this->gptr() - this->eback());
938                       const streamsize __remainder = _M_ext_end - _M_ext_next;
939                       if (__remainder)
940                         __builtin_memmove(_M_ext_buf, _M_ext_next, __remainder);
942                       _M_ext_next = _M_ext_buf;
943                       _M_ext_end = _M_ext_buf + __remainder;
944                       _M_set_buffer(-1);
945                       _M_state_last = _M_state_cur = _M_state_beg;
946                     }
947                 }
948               else if (_M_writing && (__testvalid = _M_terminate_output()))
949                 _M_set_buffer(-1);
950             }
951         }
953       if (__testvalid)
954         _M_codecvt = _M_codecvt_tmp;
955       else
956         _M_codecvt = 0;
957     }
959   // Inhibit implicit instantiations for required instantiations,
960   // which are defined via explicit instantiations elsewhere.
961   // NB:  This syntax is a GNU extension.
962 #if _GLIBCXX_EXTERN_TEMPLATE
963   extern template class basic_filebuf<char>;
964   extern template class basic_ifstream<char>;
965   extern template class basic_ofstream<char>;
966   extern template class basic_fstream<char>;
968 #ifdef _GLIBCXX_USE_WCHAR_T
969   extern template class basic_filebuf<wchar_t>;
970   extern template class basic_ifstream<wchar_t>;
971   extern template class basic_ofstream<wchar_t>;
972   extern template class basic_fstream<wchar_t>;
973 #endif
974 #endif
976 _GLIBCXX_END_NAMESPACE
978 #endif