OSDN Git Service

2003-11-03 Petur Runolfsson <peturr02@ru.is>
[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       if (!_M_buf_allocated && this->_M_buf_size)
48         {
49           // Allocate internal buffer.
50           this->_M_buf = new char_type[this->_M_buf_size];
51           _M_buf_allocated = true;
52         }
53     }
54
55   // Both close and setbuf need to deallocate internal buffers, if it exists.
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             {
138               __testfail = true;
139             }
140               
141           // NB: Do this here so that re-opened filebufs will be cool...
142           this->_M_mode = ios_base::openmode(0);
143           this->_M_pback_init = false;
144           _M_destroy_internal_buffer();
145           _M_reading = false;
146           _M_writing = false;
147           _M_set_buffer(-1);
148           _M_state_last = _M_state_cur = _M_state_beg;
149           
150           if (!_M_file.close())
151             __testfail = true;
152
153           if (!__testfail)
154             __ret = this;
155         }
156       return __ret;
157     }
158
159   template<typename _CharT, typename _Traits>
160     streamsize 
161     basic_filebuf<_CharT, _Traits>::
162     showmanyc()
163     {
164       streamsize __ret = -1;
165       const bool __testin = this->_M_mode & ios_base::in;
166
167       if (__testin && this->is_open())
168         {
169           // For a stateful encoding (-1) the pending sequence might be just
170           // shift and unshift prefixes with no actual character.
171           __ret = this->egptr() - this->gptr();
172           if (__check_facet(_M_codecvt).encoding() >= 0)
173             __ret += _M_file.showmanyc() / _M_codecvt->max_length();
174         }
175
176       return __ret;
177     }
178   
179   template<typename _CharT, typename _Traits>
180     typename basic_filebuf<_CharT, _Traits>::int_type 
181     basic_filebuf<_CharT, _Traits>::
182     underflow()
183     {
184       int_type __ret = traits_type::eof();
185       const bool __testin = this->_M_mode & ios_base::in;
186       const bool __testout = this->_M_mode & ios_base::out;
187
188       if (__testin && !_M_writing)
189         {
190           // Check for pback madness, and if so swich back to the
191           // normal buffers and jet outta here before expensive
192           // fileops happen...
193           _M_destroy_pback();
194
195           if (this->gptr() < this->egptr())
196             return traits_type::to_int_type(*this->gptr());
197
198           // Get and convert input sequence.
199           const size_t __buflen = this->_M_buf_size > 1
200                                   ? this->_M_buf_size - 1 : 1;
201           
202           // Will be set to true if ::read() returns 0 indicating EOF.
203           bool __got_eof = false;
204           // Number of internal characters produced.
205           streamsize __ilen = 0;
206           if (__check_facet(_M_codecvt).always_noconv())
207             {
208               __ilen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()), 
209                                       __buflen);
210               if (__ilen == 0)
211                 __got_eof = true;
212             }
213           else
214             {
215               // Worst-case number of external bytes.
216               // XXX Not done encoding() == -1.
217               const int __enc = _M_codecvt->encoding();
218               streamsize __blen; // Minimum buffer size.
219               streamsize __rlen; // Number of chars to read.
220               if (__enc > 0)
221                 __blen = __rlen = __buflen * __enc;
222               else
223                 {
224                   __blen = __buflen + _M_codecvt->max_length() - 1;
225                   __rlen = __buflen;
226                 }
227               const streamsize __remainder = _M_ext_end - _M_ext_next;
228               __rlen = __rlen > __remainder ? __rlen - __remainder : 0;
229               
230               // Allocate buffer if necessary and move unconverted
231               // bytes to front.
232               if (_M_ext_buf_size < __blen)
233                 {
234                   char* __buf = new char[__blen];
235                   if (__remainder > 0)
236                     std::memcpy(__buf, _M_ext_next, __remainder);
237
238                   delete [] _M_ext_buf;
239                   _M_ext_buf = __buf;
240                   _M_ext_buf_size = __blen;
241                 }
242               else if (__remainder > 0)
243                 std::memmove(_M_ext_buf, _M_ext_next, __remainder);
244
245               _M_ext_next = _M_ext_buf;
246               _M_ext_end = _M_ext_buf + __remainder;
247               _M_state_last = _M_state_cur;
248
249               do
250                 {
251                   if (__rlen > 0)
252                     {
253                       // Sanity check!
254                       // This may fail if the return value of
255                       // codecvt::max_length() is bogus.
256                       if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size)
257                         {
258                           __throw_ios_failure("codecvt::max_length() "
259                                               "is not valid");
260                         }
261                       streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
262                       if (__elen == 0)
263                         __got_eof = true;
264                       _M_ext_end += __elen;
265                     }
266                   
267                   char_type* __iend;
268                   codecvt_base::result __r;
269                   __r = _M_codecvt->in(_M_state_cur, _M_ext_next,
270                                        _M_ext_end, _M_ext_next, this->eback(), 
271                                        this->eback() + __buflen, __iend);
272                   if (__r == codecvt_base::ok || __r == codecvt_base::partial)
273                     __ilen = __iend - this->eback();
274                   else if (__r == codecvt_base::noconv)
275                     {
276                       size_t __avail = _M_ext_end - _M_ext_buf;
277                       __ilen = std::min(__avail, __buflen);
278                       traits_type::copy(this->eback(),
279                                         reinterpret_cast<char_type*>(_M_ext_buf), __ilen);
280                       _M_ext_next = _M_ext_buf + __ilen;
281                     }
282                   else 
283                     {
284                       __ilen = 0;
285                       break;
286                     }
287                   __rlen = 1;
288                 }
289               while (!__got_eof && __ilen == 0);
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             }
306         }
307       return __ret;
308     }
309
310   template<typename _CharT, typename _Traits>
311     typename basic_filebuf<_CharT, _Traits>::int_type 
312     basic_filebuf<_CharT, _Traits>::
313     pbackfail(int_type __i)
314     {
315       int_type __ret = traits_type::eof();
316       const bool __testin = this->_M_mode & ios_base::in;
317
318       if (__testin && !_M_writing)
319         {
320           // Remember whether the pback buffer is active, otherwise below
321           // we may try to store in it a second char (libstdc++/9761).
322           const bool __testpb = this->_M_pback_init;       
323           const bool __testeof = traits_type::eq_int_type(__i, __ret);
324           
325           int_type __tmp;
326           if (this->eback() < this->gptr())
327             {
328               this->gbump(-1);
329               __tmp = traits_type::to_int_type(*this->gptr());
330             }
331           else if (this->seekoff(-1, ios_base::cur) != pos_type(off_type(-1)))
332             {
333               __tmp = this->underflow();
334               if (traits_type::eq_int_type(__tmp, __ret))
335                 return __ret;
336             }
337           else
338             {
339               // At the beginning of the buffer, need to make a
340               // putback position available.  But the seek may fail
341               // (f.i., at the beginning of a file, see
342               // libstdc++/9439) and in that case we return
343               // traits_type::eof().
344               return __ret;
345             }
346
347           // Try to put back __i into input sequence in one of three ways.
348           // Order these tests done in is unspecified by the standard.
349           if (!__testeof && traits_type::eq_int_type(__i, __tmp))
350             __ret = __i;
351           else if (__testeof)
352             __ret = traits_type::not_eof(__i);
353           else if (!__testpb)
354             {
355               _M_create_pback();
356               _M_reading = true;
357               *this->gptr() = traits_type::to_char_type(__i); 
358               __ret = __i;
359             }
360         }
361       return __ret;
362     }
363
364   template<typename _CharT, typename _Traits>
365     typename basic_filebuf<_CharT, _Traits>::int_type 
366     basic_filebuf<_CharT, _Traits>::
367     overflow(int_type __c)
368     {
369       int_type __ret = traits_type::eof();
370       const bool __testeof = traits_type::eq_int_type(__c, __ret);
371       const bool __testout = this->_M_mode & ios_base::out;
372       
373       if (__testout && !_M_reading)
374         {
375           if (this->pbase() < this->pptr())
376             {
377               // If appropriate, append the overflow char.
378               if (!__testeof)
379                 {
380                   *this->pptr() = traits_type::to_char_type(__c);
381                   this->pbump(1);
382                 }
383               
384               // Convert pending sequence to external representation,
385               // output.
386               if (_M_convert_to_external(this->pbase(),
387                                          this->pptr() - this->pbase())
388                   && (!__testeof || (__testeof && !_M_file.sync())))
389                 {
390                   _M_set_buffer(0);
391                   __ret = traits_type::not_eof(__c);
392                 }
393             }
394           else if (this->_M_buf_size > 1)
395             {
396               // Overflow in 'uncommitted' mode: set _M_writing, set
397               // the buffer to the initial 'write' mode, and put __c
398               // into the buffer.
399               _M_set_buffer(0);
400               _M_writing = true;
401               if (!__testeof)
402                 {
403                   *this->pptr() = traits_type::to_char_type(__c);
404                   this->pbump(1);
405                 }
406               __ret = traits_type::not_eof(__c);
407             }
408           else
409             {
410               // Unbuffered.
411               char_type __conv = traits_type::to_char_type(__c);
412               if (__testeof || _M_convert_to_external(&__conv, 1))
413                 {                 
414                   _M_writing = true;
415                   __ret = traits_type::not_eof(__c);
416                 }
417             }
418         }
419       return __ret;
420     }
421   
422   template<typename _CharT, typename _Traits>
423     bool
424     basic_filebuf<_CharT, _Traits>::
425     _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
426     {
427       // Sizes of external and pending output.
428       streamsize __elen = 0;
429       streamsize __plen = 0;
430
431       if (__check_facet(_M_codecvt).always_noconv())
432         {
433           __elen += _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
434           __plen += __ilen;
435         }
436       else
437         {
438           // Worst-case number of external bytes needed.
439           // XXX Not done encoding() == -1.
440           streamsize __blen = __ilen * _M_codecvt->max_length();
441           char* __buf = static_cast<char*>(__builtin_alloca(__blen));
442
443           char* __bend;
444           const char_type* __iend;
445           codecvt_base::result __r;
446           __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
447                                 __iend, __buf, __buf + __blen, __bend);
448           
449           if (__r == codecvt_base::ok || __r == codecvt_base::partial)
450             __blen = __bend - __buf;
451           else if (__r == codecvt_base::noconv)
452             {
453               // Same as the always_noconv case above.
454               __buf = reinterpret_cast<char*>(__ibuf);
455               __blen = __ilen;
456             }
457           else
458             {
459               // Result == error.
460               __blen = 0;
461             }
462           
463           if (__blen)
464             {
465               __elen += _M_file.xsputn(__buf, __blen);
466               __plen += __blen;
467             }
468           
469           // Try once more for partial conversions.
470           if (__r == codecvt_base::partial)
471             {
472               const char_type* __iresume = __iend;
473               streamsize __rlen = this->pptr() - __iend;
474               __r = _M_codecvt->out(_M_state_cur, __iresume,
475                                     __iresume + __rlen, __iend, __buf, 
476                                     __buf + __blen, __bend);
477               if (__r != codecvt_base::error)
478                 {
479                   __rlen = __bend - __buf;
480                   __elen += _M_file.xsputn(__buf, __rlen);
481                   __plen += __rlen;
482                 }
483             }
484         }
485       return __elen && __elen == __plen;
486     }
487
488    template<typename _CharT, typename _Traits>
489      streamsize
490      basic_filebuf<_CharT, _Traits>::
491      xsputn(const _CharT* __s, streamsize __n)
492      { 
493        streamsize __ret = 0;
494       
495        // Optimization in the always_noconv() case, to be generalized in the
496        // future: when __n is sufficiently large we write directly instead of
497        // using the buffer.
498        const bool __testout = this->_M_mode & ios_base::out;
499        if (__testout && !_M_reading
500            && __check_facet(_M_codecvt).always_noconv())
501         {
502           // Measurement would reveal the best choice.
503           const streamsize __chunk = 1ul << 10;
504           streamsize __bufavail = this->epptr() - this->pptr();
505
506           // Don't mistake 'uncommitted' mode buffered with unbuffered.
507           if (!_M_writing && this->_M_buf_size > 1)
508             __bufavail = this->_M_buf_size - 1;
509
510           const streamsize __limit = std::min(__chunk, __bufavail);
511           if (__n >= __limit)
512             {
513               const streamsize __buffill = this->pptr() - this->pbase();
514               const char* __buf = reinterpret_cast<const char*>(this->pbase());
515               __ret = _M_file.xsputn_2(__buf, __buffill,
516                                        reinterpret_cast<const char*>(__s), 
517                                        __n);
518               if (__ret == __buffill + __n)
519                 {
520                   _M_set_buffer(0);
521                   _M_writing = true;
522                 }
523               if (__ret > __buffill)
524                 __ret -= __buffill;
525               else
526                 __ret = 0;
527             }
528           else
529             __ret = __streambuf_type::xsputn(__s, __n);
530         }
531        else
532          __ret = __streambuf_type::xsputn(__s, __n);
533       
534        return __ret;
535     }
536
537   template<typename _CharT, typename _Traits>
538     typename basic_filebuf<_CharT, _Traits>::__streambuf_type* 
539     basic_filebuf<_CharT, _Traits>::
540     setbuf(char_type* __s, streamsize __n)
541     {
542       if (!this->is_open() && __s == 0 && __n == 0)
543         this->_M_buf_size = 1;
544       else if (__s && __n > 0)
545         {
546           // This is implementation-defined behavior, and assumes that
547           // an external char_type array of length __n exists and has
548           // been pre-allocated. If this is not the case, things will
549           // quickly blow up. When __n > 1, __n - 1 positions will be
550           // used for the get area, __n - 1 for the put area and 1
551           // position to host the overflow char of a full put area.
552           // When __n == 1, 1 position will be used for the get area
553           // and 0 for the put area, as in the unbuffered case above.
554
555           // Step 1: Destroy the current internal array.
556           _M_destroy_internal_buffer();
557           
558           // Step 2: Use the external array.
559           this->_M_buf = __s;
560           this->_M_buf_size = __n;
561           _M_reading = false;
562           _M_writing = false;
563           _M_set_buffer(-1);
564         }
565       return this; 
566     }
567   
568
569   // According to 27.8.1.4 p11 - 13, seekoff should ignore the last
570   // argument (of type openmode).
571   template<typename _CharT, typename _Traits>
572     typename basic_filebuf<_CharT, _Traits>::pos_type
573     basic_filebuf<_CharT, _Traits>::
574     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode)
575     {
576       pos_type __ret =  pos_type(off_type(-1)); 
577
578       int __width = 0;
579       if (_M_codecvt)
580         __width = _M_codecvt->encoding();
581       if (__width < 0)
582         __width = 0;
583
584       const bool __testfail = __off != 0 && __width <= 0;
585       if (this->is_open() && !__testfail) 
586         {
587           // Ditch any pback buffers to avoid confusion.
588           _M_destroy_pback();
589
590           // Correct state at destination. Note that this is the correct
591           // state for the current position during output, because
592           // codecvt::unshift() returns the state to the initial state.
593           // This is also the correct state at the end of the file because
594           // an unshift sequence should have been written at the end.
595           __state_type __state = _M_state_beg;
596           off_type __computed_off = __off * __width;
597           if (_M_reading && __way == ios_base::cur)
598             {
599               if (_M_codecvt->always_noconv())
600                 __computed_off += this->gptr() - this->egptr();
601               else
602                 {
603                   // Calculate offset from _M_ext_buf that corresponds
604                   // to gptr(). Note: uses _M_state_last, which
605                   // corresponds to eback().
606                   const int __gptr_off =
607                     _M_codecvt->length(_M_state_last, _M_ext_buf, _M_ext_next,
608                                        this->gptr() - this->eback());
609                   __computed_off += _M_ext_buf + __gptr_off - _M_ext_end;
610                   
611                   // _M_state_last is modified by codecvt::length() so
612                   // it now corresponds to gptr().
613                   __state = _M_state_last;
614                 }
615             }
616           __ret = _M_seek(__computed_off, __way, __state);
617         }
618       return __ret;
619     }
620
621   // _GLIBCXX_RESOLVE_LIB_DEFECTS
622   // 171. Strange seekpos() semantics due to joint position
623   // According to the resolution of DR 171, seekpos should ignore the last
624   // argument (of type openmode).
625   template<typename _CharT, typename _Traits>
626     typename basic_filebuf<_CharT, _Traits>::pos_type
627     basic_filebuf<_CharT, _Traits>::
628     seekpos(pos_type __pos, ios_base::openmode)
629     {
630       pos_type __ret =  pos_type(off_type(-1)); 
631
632       if (this->is_open()) 
633         {
634           // Ditch any pback buffers to avoid confusion.
635           _M_destroy_pback();
636
637           __ret = _M_seek(off_type(__pos), ios_base::beg, __pos.state());
638         }
639       return __ret;
640     }
641
642   template<typename _CharT, typename _Traits>
643     typename basic_filebuf<_CharT, _Traits>::pos_type
644     basic_filebuf<_CharT, _Traits>::
645     _M_seek(off_type __off, ios_base::seekdir __way, __state_type __state)
646     {
647       pos_type __ret = pos_type(off_type(-1));
648       if (_M_terminate_output())
649         {         
650           // Returns pos_type(off_type(-1)) in case of failure.
651           __ret = pos_type(_M_file.seekoff(__off, __way));
652           
653           _M_reading = false;
654           _M_writing = false;
655           _M_ext_next = _M_ext_end = _M_ext_buf;
656           _M_set_buffer(-1);
657           _M_state_cur = __state;
658           __ret.state(_M_state_cur);
659         }
660       return __ret;
661     }
662
663   template<typename _CharT, typename _Traits>
664     bool
665     basic_filebuf<_CharT, _Traits>::
666     _M_terminate_output()
667     {
668       bool __testvalid = true;
669
670       // Part one: update the output sequence.
671       if (this->pbase() < this->pptr())
672         {
673           const int_type __tmp = this->overflow();
674           if (traits_type::eq_int_type(__tmp, traits_type::eof()))
675             __testvalid = false;
676         }
677               
678       // Part two: output unshift sequence.
679       if (_M_writing && !__check_facet(_M_codecvt).always_noconv() 
680           && __testvalid)
681         {
682           // Note: this value is arbitrary, since there is no way to
683           // get the length of the unshift sequence from codecvt,
684           // without calling unshift.
685           const size_t __blen = 128;
686
687           char __buf[__blen];
688           codecvt_base::result __r;
689           streamsize __ilen = 0;
690
691           do
692             {
693               char* __next;
694               __r = _M_codecvt->unshift(_M_state_cur, __buf,
695                                         __buf + __blen, __next);
696               if (__r == codecvt_base::error)
697                 __testvalid = false;
698               else if (__r == codecvt_base::ok ||
699                        __r == codecvt_base::partial)
700                 {
701                   __ilen = __next - __buf;
702                   
703                   if (__ilen > 0)
704                     {
705                       const streamsize __elen = _M_file.xsputn(__buf, __ilen);
706                       if (__elen != __ilen)
707                         __testvalid = false;
708                     }
709                 }
710             }
711           while (__r == codecvt_base::partial && __ilen > 0 && __testvalid);
712
713           if (__testvalid)
714             {
715               // This second call to overflow() is required by the standard,
716               // but it's not clear why it's needed, since the output buffer
717               // should be empty by this point (it should have been emptied
718               // in the first call to overflow()).
719               const int_type __tmp = this->overflow();
720               if (traits_type::eq_int_type(__tmp, traits_type::eof()))
721                 __testvalid = false;
722             }
723         }
724       return __testvalid;
725     }
726
727   template<typename _CharT, typename _Traits>
728     int
729     basic_filebuf<_CharT, _Traits>::
730     sync()
731     {
732       int __ret = 0;
733
734       // Make sure that the internal buffer resyncs its idea of
735       // the file position with the external file.
736       // NB: _M_file.sync() will be called within.
737       if (this->pbase() < this->pptr())
738         {
739           const int_type __tmp = this->overflow();
740           if (traits_type::eq_int_type(__tmp, traits_type::eof()))
741             __ret = -1;
742         }
743       
744       return __ret;
745     }
746
747   template<typename _CharT, typename _Traits>
748     void
749     basic_filebuf<_CharT, _Traits>::
750     imbue(const locale& __loc)
751     {
752       if (this->_M_buf_locale != __loc)
753         {
754           bool __testfail = false;
755           if (this->is_open())
756             {
757               const bool __testbeg =
758                 this->seekoff(0, ios_base::cur, this->_M_mode) ==
759                 pos_type(off_type(0));
760               const bool __teststate =
761                 __check_facet(_M_codecvt).encoding() == -1;
762
763               __testfail = !__testbeg || __teststate;
764             }
765
766           if (!__testfail)
767             {
768               this->_M_buf_locale = __loc;
769               if (__builtin_expect(has_facet<__codecvt_type>(__loc), true))
770                 _M_codecvt = &use_facet<__codecvt_type>(__loc);
771               else
772                 _M_codecvt = 0;
773
774               // NB This may require the reconversion of previously
775               // converted chars. This in turn may cause the
776               // reconstruction of the original file. YIKES!!  This
777               // implementation interprets this requirement as requiring
778               // the file position be at the beginning, and a stateless
779               // encoding, or that the filebuf be closed. Opinions may differ.
780             }
781         }
782     }
783
784   // Inhibit implicit instantiations for required instantiations,
785   // which are defined via explicit instantiations elsewhere.  
786   // NB:  This syntax is a GNU extension.
787 #if _GLIBCXX_EXTERN_TEMPLATE
788   extern template class basic_filebuf<char>;
789   extern template class basic_ifstream<char>;
790   extern template class basic_ofstream<char>;
791   extern template class basic_fstream<char>;
792
793 #ifdef _GLIBCXX_USE_WCHAR_T
794   extern template class basic_filebuf<wchar_t>;
795   extern template class basic_ifstream<wchar_t>;
796   extern template class basic_ofstream<wchar_t>;
797   extern template class basic_fstream<wchar_t>;
798 #endif
799 #endif
800 } // namespace std
801
802 #endif