OSDN Git Service

2003-12-10 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / fstream.tcc
1 // File based streams -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library.  This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING.  If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction.  Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License.  This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 //
32 // ISO C++ 14882: 27.8  File-based streams
33 //
34
35 #ifndef _FSTREAM_TCC
36 #define _FSTREAM_TCC 1
37
38 #pragma GCC system_header
39
40 namespace std
41 {
42   template<typename _CharT, typename _Traits>
43     void
44     basic_filebuf<_CharT, _Traits>::
45     _M_allocate_internal_buffer()
46     {
47       // Allocate internal buffer only if one doesn't already exist
48       // (either allocated or provided by the user via setbuf).
49       if (!_M_buf_allocated && !this->_M_buf)
50         {
51           this->_M_buf = new char_type[this->_M_buf_size];
52           _M_buf_allocated = true;
53         }
54     }
55
56   template<typename _CharT, typename _Traits>
57     void
58     basic_filebuf<_CharT, _Traits>::
59     _M_destroy_internal_buffer() throw()
60     {
61       if (_M_buf_allocated)
62         {
63           delete [] this->_M_buf;
64           this->_M_buf = NULL;
65           _M_buf_allocated = false;
66         }
67       delete [] _M_ext_buf;
68       _M_ext_buf = NULL;
69       _M_ext_buf_size = 0;
70       _M_ext_next = NULL;
71       _M_ext_end = NULL;
72     }
73
74   template<typename _CharT, typename _Traits>
75     basic_filebuf<_CharT, _Traits>::
76     basic_filebuf() : __streambuf_type(), _M_file(&_M_lock), 
77     _M_mode(ios_base::openmode(0)), _M_state_beg(), _M_state_cur(),
78     _M_state_last(), _M_buf(NULL), _M_buf_size(BUFSIZ),
79     _M_buf_allocated(false), _M_reading(false), _M_writing(false),
80     _M_pback_cur_save(0), _M_pback_end_save(0), _M_pback_init(false),
81     _M_codecvt(0), _M_ext_buf(0), _M_ext_buf_size(0), _M_ext_next(0),
82     _M_ext_end(0)
83     { 
84       if (has_facet<__codecvt_type>(this->_M_buf_locale))
85         _M_codecvt = &use_facet<__codecvt_type>(this->_M_buf_locale);
86     }
87
88   template<typename _CharT, typename _Traits>
89     typename basic_filebuf<_CharT, _Traits>::__filebuf_type* 
90     basic_filebuf<_CharT, _Traits>::
91     open(const char* __s, ios_base::openmode __mode)
92     {
93       __filebuf_type *__ret = NULL;
94       if (!this->is_open())
95         {
96           _M_file.open(__s, __mode);
97           if (this->is_open())
98             {
99               _M_allocate_internal_buffer();
100               this->_M_mode = __mode;
101
102               // Setup initial buffer to 'uncommitted' mode.
103               _M_reading = false;
104               _M_writing = false;
105               _M_set_buffer(-1);
106
107               // Reset to initial state.
108               _M_state_last = _M_state_cur = _M_state_beg;
109
110               // 27.8.1.3,4
111               if ((__mode & ios_base::ate) 
112                   && this->seekoff(0, ios_base::end, __mode) 
113                   == pos_type(off_type(-1)))
114                 this->close();
115               else
116                 __ret = this;
117             }
118         }
119       return __ret;
120     }
121
122   template<typename _CharT, typename _Traits>
123     typename basic_filebuf<_CharT, _Traits>::__filebuf_type* 
124     basic_filebuf<_CharT, _Traits>::
125     close() throw()
126     {
127       __filebuf_type* __ret = NULL;
128       if (this->is_open())
129         {
130           bool __testfail = false;
131           try
132             {
133               if (!_M_terminate_output())
134                 __testfail = true;
135             }
136           catch(...)
137             { __testfail = true; }
138               
139           // NB: Do this here so that re-opened filebufs will be cool...
140           this->_M_mode = ios_base::openmode(0);
141           this->_M_pback_init = false;
142           _M_destroy_internal_buffer();
143           _M_reading = false;
144           _M_writing = false;
145           _M_set_buffer(-1);
146           _M_state_last = _M_state_cur = _M_state_beg;
147           
148           if (!_M_file.close())
149             __testfail = true;
150
151           if (!__testfail)
152             __ret = this;
153         }
154       return __ret;
155     }
156
157   template<typename _CharT, typename _Traits>
158     streamsize 
159     basic_filebuf<_CharT, _Traits>::
160     showmanyc()
161     {
162       streamsize __ret = -1;
163       const bool __testin = this->_M_mode & ios_base::in;
164       if (__testin && this->is_open())
165         {
166           // For a stateful encoding (-1) the pending sequence might be just
167           // shift and unshift prefixes with no actual character.
168           __ret = this->egptr() - this->gptr();
169           if (__check_facet(_M_codecvt).encoding() >= 0)
170             __ret += _M_file.showmanyc() / _M_codecvt->max_length();
171         }
172       return __ret;
173     }
174   
175   template<typename _CharT, typename _Traits>
176     typename basic_filebuf<_CharT, _Traits>::int_type 
177     basic_filebuf<_CharT, _Traits>::
178     underflow()
179     {
180       int_type __ret = traits_type::eof();
181       const bool __testin = this->_M_mode & ios_base::in;
182       const bool __testout = this->_M_mode & ios_base::out;
183       if (__testin && !_M_writing)
184         {
185           // Check for pback madness, and if so swich back to the
186           // normal buffers and jet outta here before expensive
187           // fileops happen...
188           _M_destroy_pback();
189
190           if (this->gptr() < this->egptr())
191             return traits_type::to_int_type(*this->gptr());
192
193           // Get and convert input sequence.
194           const size_t __buflen = this->_M_buf_size > 1
195                                   ? this->_M_buf_size - 1 : 1;
196           
197           // Will be set to true if ::read() returns 0 indicating EOF.
198           bool __got_eof = false;
199           // Number of internal characters produced.
200           streamsize __ilen = 0;
201           codecvt_base::result __r = codecvt_base::ok;    
202           if (__check_facet(_M_codecvt).always_noconv())
203             {
204               __ilen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()), 
205                                       __buflen);
206               if (__ilen == 0)
207                 __got_eof = true;
208             }
209           else
210             {
211               // Worst-case number of external bytes.
212               // XXX Not done encoding() == -1.
213               const int __enc = _M_codecvt->encoding();
214               streamsize __blen; // Minimum buffer size.
215               streamsize __rlen; // Number of chars to read.
216               if (__enc > 0)
217                 __blen = __rlen = __buflen * __enc;
218               else
219                 {
220                   __blen = __buflen + _M_codecvt->max_length() - 1;
221                   __rlen = __buflen;
222                 }
223               const streamsize __remainder = _M_ext_end - _M_ext_next;
224               __rlen = __rlen > __remainder ? __rlen - __remainder : 0;
225               
226               // Allocate buffer if necessary and move unconverted
227               // bytes to front.
228               if (_M_ext_buf_size < __blen)
229                 {
230                   char* __buf = new char[__blen];
231                   if (__remainder > 0)
232                     std::memcpy(__buf, _M_ext_next, __remainder);
233
234                   delete [] _M_ext_buf;
235                   _M_ext_buf = __buf;
236                   _M_ext_buf_size = __blen;
237                 }
238               else if (__remainder > 0)
239                 std::memmove(_M_ext_buf, _M_ext_next, __remainder);
240
241               _M_ext_next = _M_ext_buf;
242               _M_ext_end = _M_ext_buf + __remainder;
243               _M_state_last = _M_state_cur;
244
245               do
246                 {
247                   if (__rlen > 0)
248                     {
249                       // Sanity check!
250                       // This may fail if the return value of
251                       // codecvt::max_length() is bogus.
252                       if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size)
253                         {
254                           __throw_ios_failure("basic_filebuf::underflow "
255                                               "codecvt::max_length() "
256                                               "is not valid");
257                         }
258                       streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
259                       if (__elen == 0)
260                         __got_eof = true;
261                       else if (__elen == -1)
262                         break;
263                       _M_ext_end += __elen;
264                     }
265
266                   char_type* __iend;
267                   __r = _M_codecvt->in(_M_state_cur, _M_ext_next,
268                                        _M_ext_end, _M_ext_next, this->eback(), 
269                                        this->eback() + __buflen, __iend);
270                   if (__r == codecvt_base::noconv)
271                     {
272                       size_t __avail = _M_ext_end - _M_ext_buf;
273                       __ilen = std::min(__avail, __buflen);
274                       traits_type::copy(this->eback(),
275                                         reinterpret_cast<char_type*>(_M_ext_buf), __ilen);
276                       _M_ext_next = _M_ext_buf + __ilen;
277                     }
278                   else
279                     __ilen = __iend - this->eback();
280
281                   // _M_codecvt->in may return error while __ilen > 0: this is
282                   // ok, and actually occurs in case of mixed encodings (e.g.,
283                   // XML files).
284                   if (__r == codecvt_base::error)
285                     break;
286
287                   __rlen = 1;
288                 }
289               while (__ilen == 0 && !__got_eof);
290             }
291
292           if (__ilen > 0)
293             {
294               _M_set_buffer(__ilen);
295               _M_reading = true;
296               __ret = traits_type::to_int_type(*this->gptr());
297             }
298           else if (__got_eof)
299             {
300               // If the actual end of file is reached, set 'uncommitted'
301               // mode, thus allowing an immediate write without an 
302               // intervening seek.
303               _M_set_buffer(-1);
304               _M_reading = false;
305               // However, reaching it while looping on partial means that
306               // the file has got an incomplete character.
307               if (__r == codecvt_base::partial)
308                 __throw_ios_failure("basic_filebuf::underflow "
309                                     "incomplete character in file");
310             }
311           else if (__r == codecvt_base::error)
312             __throw_ios_failure("basic_filebuf::underflow "
313                                 "invalid byte sequence in file");
314           else
315             __throw_ios_failure("basic_filebuf::underflow "
316                                 "error reading the file");          
317         }
318       return __ret;
319     }
320
321   template<typename _CharT, typename _Traits>
322     typename basic_filebuf<_CharT, _Traits>::int_type 
323     basic_filebuf<_CharT, _Traits>::
324     pbackfail(int_type __i)
325     {
326       int_type __ret = traits_type::eof();
327       const bool __testin = this->_M_mode & ios_base::in;
328       if (__testin && !_M_writing)
329         {
330           // Remember whether the pback buffer is active, otherwise below
331           // we may try to store in it a second char (libstdc++/9761).
332           const bool __testpb = this->_M_pback_init;       
333           const bool __testeof = traits_type::eq_int_type(__i, __ret);
334           int_type __tmp;
335           if (this->eback() < this->gptr())
336             {
337               this->gbump(-1);
338               __tmp = traits_type::to_int_type(*this->gptr());
339             }
340           else if (this->seekoff(-1, ios_base::cur) != pos_type(off_type(-1)))
341             {
342               __tmp = this->underflow();
343               if (traits_type::eq_int_type(__tmp, __ret))
344                 return __ret;
345             }
346           else
347             {
348               // At the beginning of the buffer, need to make a
349               // putback position available.  But the seek may fail
350               // (f.i., at the beginning of a file, see
351               // libstdc++/9439) and in that case we return
352               // traits_type::eof().
353               return __ret;
354             }
355
356           // Try to put back __i into input sequence in one of three ways.
357           // Order these tests done in is unspecified by the standard.
358           if (!__testeof && traits_type::eq_int_type(__i, __tmp))
359             __ret = __i;
360           else if (__testeof)
361             __ret = traits_type::not_eof(__i);
362           else if (!__testpb)
363             {
364               _M_create_pback();
365               _M_reading = true;
366               *this->gptr() = traits_type::to_char_type(__i); 
367               __ret = __i;
368             }
369         }
370       return __ret;
371     }
372
373   template<typename _CharT, typename _Traits>
374     typename basic_filebuf<_CharT, _Traits>::int_type 
375     basic_filebuf<_CharT, _Traits>::
376     overflow(int_type __c)
377     {
378       int_type __ret = traits_type::eof();
379       const bool __testeof = traits_type::eq_int_type(__c, __ret);
380       const bool __testout = this->_M_mode & ios_base::out;
381       if (__testout && !_M_reading)
382         {
383           if (this->pbase() < this->pptr())
384             {
385               // If appropriate, append the overflow char.
386               if (!__testeof)
387                 {
388                   *this->pptr() = traits_type::to_char_type(__c);
389                   this->pbump(1);
390                 }
391               
392               // Convert pending sequence to external representation,
393               // and output.
394               if (_M_convert_to_external(this->pbase(),
395                                          this->pptr() - this->pbase())
396                   && (!__testeof || (__testeof && !_M_file.sync())))
397                 {
398                   _M_set_buffer(0);
399                   __ret = traits_type::not_eof(__c);
400                 }
401             }
402           else if (this->_M_buf_size > 1)
403             {
404               // Overflow in 'uncommitted' mode: set _M_writing, set
405               // the buffer to the initial 'write' mode, and put __c
406               // into the buffer.
407               _M_set_buffer(0);
408               _M_writing = true;
409               if (!__testeof)
410                 {
411                   *this->pptr() = traits_type::to_char_type(__c);
412                   this->pbump(1);
413                 }
414               __ret = traits_type::not_eof(__c);
415             }
416           else
417             {
418               // Unbuffered.
419               char_type __conv = traits_type::to_char_type(__c);
420               if (__testeof || _M_convert_to_external(&__conv, 1))
421                 {                 
422                   _M_writing = true;
423                   __ret = traits_type::not_eof(__c);
424                 }
425             }
426         }
427       return __ret;
428     }
429   
430   template<typename _CharT, typename _Traits>
431     bool
432     basic_filebuf<_CharT, _Traits>::
433     _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
434     {
435       // Sizes of external and pending output.
436       streamsize __elen = 0;
437       streamsize __plen = 0;
438       if (__check_facet(_M_codecvt).always_noconv())
439         {
440           __elen += _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
441           __plen += __ilen;
442         }
443       else
444         {
445           // Worst-case number of external bytes needed.
446           // XXX Not done encoding() == -1.
447           streamsize __blen = __ilen * _M_codecvt->max_length();
448           char* __buf = static_cast<char*>(__builtin_alloca(__blen));
449
450           char* __bend;
451           const char_type* __iend;
452           codecvt_base::result __r;
453           __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
454                                 __iend, __buf, __buf + __blen, __bend);
455           
456           if (__r == codecvt_base::ok || __r == codecvt_base::partial)
457             __blen = __bend - __buf;
458           else if (__r == codecvt_base::noconv)
459             {
460               // Same as the always_noconv case above.
461               __buf = reinterpret_cast<char*>(__ibuf);
462               __blen = __ilen;
463             }
464           else
465             {
466               // Result == error.
467               __blen = 0;
468             }
469           
470           if (__blen)
471             {
472               __elen += _M_file.xsputn(__buf, __blen);
473               __plen += __blen;
474             }
475           
476           // Try once more for partial conversions.
477           if (__r == codecvt_base::partial)
478             {
479               const char_type* __iresume = __iend;
480               streamsize __rlen = this->pptr() - __iend;
481               __r = _M_codecvt->out(_M_state_cur, __iresume,
482                                     __iresume + __rlen, __iend, __buf, 
483                                     __buf + __blen, __bend);
484               if (__r != codecvt_base::error)
485                 {
486                   __rlen = __bend - __buf;
487                   __elen += _M_file.xsputn(__buf, __rlen);
488                   __plen += __rlen;
489                 }
490             }
491         }
492       return __elen && __elen == __plen;
493     }
494
495    template<typename _CharT, typename _Traits>
496      streamsize
497      basic_filebuf<_CharT, _Traits>::
498      xsputn(const _CharT* __s, streamsize __n)
499      { 
500        // Optimization in the always_noconv() case, to be generalized in the
501        // future: when __n is sufficiently large we write directly instead of
502        // using the buffer.
503        streamsize __ret = 0;
504        const bool __testout = this->_M_mode & ios_base::out;
505        if (__testout && !_M_reading
506            && __check_facet(_M_codecvt).always_noconv())
507         {
508           // Measurement would reveal the best choice.
509           const streamsize __chunk = 1ul << 10;
510           streamsize __bufavail = this->epptr() - this->pptr();
511
512           // Don't mistake 'uncommitted' mode buffered with unbuffered.
513           if (!_M_writing && this->_M_buf_size > 1)
514             __bufavail = this->_M_buf_size - 1;
515
516           const streamsize __limit = std::min(__chunk, __bufavail);
517           if (__n >= __limit)
518             {
519               const streamsize __buffill = this->pptr() - this->pbase();
520               const char* __buf = reinterpret_cast<const char*>(this->pbase());
521               __ret = _M_file.xsputn_2(__buf, __buffill,
522                                        reinterpret_cast<const char*>(__s), 
523                                        __n);
524               if (__ret == __buffill + __n)
525                 {
526                   _M_set_buffer(0);
527                   _M_writing = true;
528                 }
529               if (__ret > __buffill)
530                 __ret -= __buffill;
531               else
532                 __ret = 0;
533             }
534           else
535             __ret = __streambuf_type::xsputn(__s, __n);
536         }
537        else
538          __ret = __streambuf_type::xsputn(__s, __n);      
539        return __ret;
540     }
541
542   template<typename _CharT, typename _Traits>
543     typename basic_filebuf<_CharT, _Traits>::__streambuf_type* 
544     basic_filebuf<_CharT, _Traits>::
545     setbuf(char_type* __s, streamsize __n)
546     {
547       if (!this->is_open())
548         if (__s == 0 && __n == 0)
549           this->_M_buf_size = 1;
550         else if (__s && __n > 0)
551           {
552             // This is implementation-defined behavior, and assumes that
553             // an external char_type array of length __n exists and has
554             // been pre-allocated. If this is not the case, things will
555             // quickly blow up. When __n > 1, __n - 1 positions will be
556             // used for the get area, __n - 1 for the put area and 1
557             // position to host the overflow char of a full put area.
558             // When __n == 1, 1 position will be used for the get area
559             // and 0 for the put area, as in the unbuffered case above.
560             this->_M_buf = __s;
561             this->_M_buf_size = __n;
562           }
563       return this; 
564     }
565   
566
567   // According to 27.8.1.4 p11 - 13, seekoff should ignore the last
568   // argument (of type openmode).
569   template<typename _CharT, typename _Traits>
570     typename basic_filebuf<_CharT, _Traits>::pos_type
571     basic_filebuf<_CharT, _Traits>::
572     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode)
573     {
574       int __width = 0;
575       if (_M_codecvt)
576         __width = _M_codecvt->encoding();
577       if (__width < 0)
578         __width = 0;
579
580       pos_type __ret =  pos_type(off_type(-1)); 
581       const bool __testfail = __off != 0 && __width <= 0;
582       if (this->is_open() && !__testfail) 
583         {
584           // Ditch any pback buffers to avoid confusion.
585           _M_destroy_pback();
586
587           // Correct state at destination. Note that this is the correct
588           // state for the current position during output, because
589           // codecvt::unshift() returns the state to the initial state.
590           // This is also the correct state at the end of the file because
591           // an unshift sequence should have been written at the end.
592           __state_type __state = _M_state_beg;
593           off_type __computed_off = __off * __width;
594           if (_M_reading && __way == ios_base::cur)
595             {
596               if (_M_codecvt->always_noconv())
597                 __computed_off += this->gptr() - this->egptr();
598               else
599                 {
600                   // Calculate offset from _M_ext_buf that corresponds
601                   // to gptr(). Note: uses _M_state_last, which
602                   // corresponds to eback().
603                   const int __gptr_off =
604                     _M_codecvt->length(_M_state_last, _M_ext_buf, _M_ext_next,
605                                        this->gptr() - this->eback());
606                   __computed_off += _M_ext_buf + __gptr_off - _M_ext_end;
607                   
608                   // _M_state_last is modified by codecvt::length() so
609                   // it now corresponds to gptr().
610                   __state = _M_state_last;
611                 }
612             }
613           __ret = _M_seek(__computed_off, __way, __state);
614         }
615       return __ret;
616     }
617
618   // _GLIBCXX_RESOLVE_LIB_DEFECTS
619   // 171. Strange seekpos() semantics due to joint position
620   // According to the resolution of DR 171, seekpos should ignore the last
621   // argument (of type openmode).
622   template<typename _CharT, typename _Traits>
623     typename basic_filebuf<_CharT, _Traits>::pos_type
624     basic_filebuf<_CharT, _Traits>::
625     seekpos(pos_type __pos, ios_base::openmode)
626     {
627       pos_type __ret =  pos_type(off_type(-1)); 
628       if (this->is_open()) 
629         {
630           // Ditch any pback buffers to avoid confusion.
631           _M_destroy_pback();
632           __ret = _M_seek(off_type(__pos), ios_base::beg, __pos.state());
633         }
634       return __ret;
635     }
636
637   template<typename _CharT, typename _Traits>
638     typename basic_filebuf<_CharT, _Traits>::pos_type
639     basic_filebuf<_CharT, _Traits>::
640     _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state)
641     {
642       pos_type __ret = pos_type(off_type(-1));
643       if (_M_terminate_output())
644         {         
645           // Returns pos_type(off_type(-1)) in case of failure.
646           __ret = pos_type(_M_file.seekoff(__off, __way));        
647           _M_reading = false;
648           _M_writing = false;
649           _M_ext_next = _M_ext_end = _M_ext_buf;
650           _M_set_buffer(-1);
651           _M_state_cur = __state;
652           __ret.state(_M_state_cur);
653         }
654       return __ret;
655     }
656
657   template<typename _CharT, typename _Traits>
658     bool
659     basic_filebuf<_CharT, _Traits>::
660     _M_terminate_output()
661     {
662       // Part one: update the output sequence.
663       bool __testvalid = true;
664       if (this->pbase() < this->pptr())
665         {
666           const int_type __tmp = this->overflow();
667           if (traits_type::eq_int_type(__tmp, traits_type::eof()))
668             __testvalid = false;
669         }
670               
671       // Part two: output unshift sequence.
672       if (_M_writing && !__check_facet(_M_codecvt).always_noconv() 
673           && __testvalid)
674         {
675           // Note: this value is arbitrary, since there is no way to
676           // get the length of the unshift sequence from codecvt,
677           // without calling unshift.
678           const size_t __blen = 128;
679           char __buf[__blen];
680           codecvt_base::result __r;
681           streamsize __ilen = 0;
682
683           do
684             {
685               char* __next;
686               __r = _M_codecvt->unshift(_M_state_cur, __buf,
687                                         __buf + __blen, __next);
688               if (__r == codecvt_base::error)
689                 __testvalid = false;
690               else if (__r == codecvt_base::ok ||
691                        __r == codecvt_base::partial)
692                 {
693                   __ilen = __next - __buf;                
694                   if (__ilen > 0)
695                     {
696                       const streamsize __elen = _M_file.xsputn(__buf, __ilen);
697                       if (__elen != __ilen)
698                         __testvalid = false;
699                     }
700                 }
701             }
702           while (__r == codecvt_base::partial && __ilen > 0 && __testvalid);
703
704           if (__testvalid)
705             {
706               // This second call to overflow() is required by the standard,
707               // but it's not clear why it's needed, since the output buffer
708               // should be empty by this point (it should have been emptied
709               // in the first call to overflow()).
710               const int_type __tmp = this->overflow();
711               if (traits_type::eq_int_type(__tmp, traits_type::eof()))
712                 __testvalid = false;
713             }
714         }
715       return __testvalid;
716     }
717
718   template<typename _CharT, typename _Traits>
719     int
720     basic_filebuf<_CharT, _Traits>::
721     sync()
722     {
723       // Make sure that the internal buffer resyncs its idea of
724       // the file position with the external file.
725       // NB: _M_file.sync() will be called within.
726       int __ret = 0;
727       if (this->pbase() < this->pptr())
728         {
729           const int_type __tmp = this->overflow();
730           if (traits_type::eq_int_type(__tmp, traits_type::eof()))
731             __ret = -1;
732         }      
733       return __ret;
734     }
735
736   template<typename _CharT, typename _Traits>
737     void
738     basic_filebuf<_CharT, _Traits>::
739     imbue(const locale& __loc)
740     {
741       bool __testfail = false;
742       if (this->is_open())
743         {
744           const pos_type __ret = this->seekoff(0, ios_base::cur,
745                                                this->_M_mode);
746           const bool __teststate = __check_facet(_M_codecvt).encoding() == -1;
747           __testfail = __teststate && __ret != pos_type(off_type(0));
748         }
749
750       if (!__testfail)
751         {
752           if (__builtin_expect(has_facet<__codecvt_type>(__loc), true))
753             _M_codecvt = &use_facet<__codecvt_type>(__loc);
754           else
755             _M_codecvt = 0;
756         }
757     }
758
759   // Inhibit implicit instantiations for required instantiations,
760   // which are defined via explicit instantiations elsewhere.  
761   // NB:  This syntax is a GNU extension.
762 #if _GLIBCXX_EXTERN_TEMPLATE
763   extern template class basic_filebuf<char>;
764   extern template class basic_ifstream<char>;
765   extern template class basic_ofstream<char>;
766   extern template class basic_fstream<char>;
767
768 #ifdef _GLIBCXX_USE_WCHAR_T
769   extern template class basic_filebuf<wchar_t>;
770   extern template class basic_ifstream<wchar_t>;
771   extern template class basic_ofstream<wchar_t>;
772   extern template class basic_fstream<wchar_t>;
773 #endif
774 #endif
775 } // namespace std
776
777 #endif