OSDN Git Service

2003-04-18 Paolo Carlini <pcarlini@unitus.it>
[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 _CPP_BITS_FSTREAM_TCC
36 #define _CPP_BITS_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 (!this->_M_buf && 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()
60     {
61       if (_M_buf_allocated)
62         {
63           delete [] this->_M_buf;
64           this->_M_buf = NULL;
65           _M_buf_allocated = false;
66           this->setg(NULL, NULL, NULL);
67           this->setp(NULL, NULL);
68         }
69     }
70
71   template<typename _CharT, typename _Traits>
72     basic_filebuf<_CharT, _Traits>::
73     basic_filebuf() : __streambuf_type(), _M_file(&_M_lock), 
74     _M_state_cur(__state_type()), _M_state_beg(__state_type()), 
75     _M_buf_allocated(false), _M_last_overflowed(false)
76     { this->_M_buf_unified = true; }
77
78   template<typename _CharT, typename _Traits>
79     typename basic_filebuf<_CharT, _Traits>::__filebuf_type* 
80     basic_filebuf<_CharT, _Traits>::
81     open(const char* __s, ios_base::openmode __mode)
82     {
83       __filebuf_type *__ret = NULL;
84       if (!this->is_open())
85         {
86           _M_file.open(__s, __mode);
87           if (this->is_open())
88             {
89               _M_allocate_internal_buffer();
90               this->_M_mode = __mode;
91
92               // Setup initial position of buffer.
93               _M_set_indeterminate();
94
95               if ((__mode & ios_base::ate)
96                   && this->seekoff(0, ios_base::end, __mode) < 0)
97                 {
98                   // 27.8.1.3,4
99                   this->close();
100                   return __ret;
101                 }
102
103               __ret = this;
104             }
105         }
106       return __ret;
107     }
108
109   template<typename _CharT, typename _Traits>
110     typename basic_filebuf<_CharT, _Traits>::__filebuf_type* 
111     basic_filebuf<_CharT, _Traits>::
112     close()
113     {
114       __filebuf_type* __ret = NULL;
115       if (this->is_open())
116         {
117           bool __testfail = false;
118           const int_type __eof = traits_type::eof();
119           const bool __testput = this->_M_out_beg < this->_M_out_lim;
120
121           if (__testput 
122               && traits_type::eq_int_type(_M_really_overflow(__eof), __eof))
123             __testfail = true;
124
125 #if 0
126           // XXX not done
127           if (_M_last_overflowed)
128             {
129               _M_output_unshift();
130               _M_really_overflow(__eof);
131             }
132 #endif
133
134           // NB: Do this here so that re-opened filebufs will be cool...
135           this->_M_mode = ios_base::openmode(0);
136           _M_destroy_internal_buffer();
137           _M_pback_destroy();
138
139           if (!_M_file.close())
140             __testfail = true;
141
142           if (!__testfail)
143             __ret = this;
144         }
145       _M_last_overflowed = false;       
146       return __ret;
147     }
148
149   template<typename _CharT, typename _Traits>
150     streamsize 
151     basic_filebuf<_CharT, _Traits>::
152     showmanyc()
153     {
154       streamsize __ret = -1;
155       const bool __testin = this->_M_mode & ios_base::in;
156       const locale __loc = this->getloc();
157       const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
158       // Sync with stdio.
159       const bool __sync = this->_M_buf_size <= 1;
160
161       if (__testin && this->is_open())
162         {
163           __ret = this->_M_in_end - this->_M_in_cur;
164
165           // For a stateful encoding (-1) the pending sequence might be just
166           // shift and unshift prefixes with no actual character.
167           if (__cvt.encoding() >= 0)
168             __ret += _M_file.showmanyc_helper(__sync) / __cvt.max_length();
169         }
170
171       _M_last_overflowed = false;       
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     pbackfail(int_type __i)
179     {
180       int_type __ret = traits_type::eof();
181       const bool __testin = this->_M_mode & ios_base::in;
182
183       if (__testin)
184         {
185           const bool __testpb = this->_M_in_beg < this->_M_in_cur;
186           char_type __c = traits_type::to_char_type(__i);
187           const bool __testeof = traits_type::eq_int_type(__i, __ret);
188
189           if (__testpb)
190             {
191               const bool __testout = this->_M_mode & ios_base::out;
192               const bool __testeq = traits_type::eq(__c, this->gptr()[-1]);
193
194               // Try to put back __c into input sequence in one of three ways.
195               // Order these tests done in is unspecified by the standard.
196               if (!__testeof && __testeq)
197                 {
198                   --this->_M_in_cur;
199                   if (__testout)
200                     --this->_M_out_cur;
201                   __ret = __i;
202                 }
203               else if (__testeof)
204                 {
205                   --this->_M_in_cur;
206                   if (__testout)
207                     --this->_M_out_cur;
208                   __ret = traits_type::not_eof(__i);
209                 }
210               else if (!__testeof)
211                 {
212                   --this->_M_in_cur;
213                   if (__testout)
214                     --this->_M_out_cur;
215                   _M_pback_create();
216                   *this->_M_in_cur = __c; 
217                   __ret = __i;
218                 }
219             }
220           else
221             {    
222               // At the beginning of the buffer, need to make a
223               // putback position available.
224               // But the seek may fail (f.i., at the beginning of
225               // a file, see libstdc++/9439) and in that case
226               // we return traits_type::eof()
227               if (this->seekoff(-1, ios_base::cur) >= 0)
228                 {
229                   this->underflow();
230                   if (!__testeof)
231                     {
232                       if (!traits_type::eq(__c, *this->_M_in_cur))
233                         {
234                           _M_pback_create();
235                           *this->_M_in_cur = __c;
236                         }
237                       __ret = __i;
238                     }
239                   else
240                     __ret = traits_type::not_eof(__i);
241                 }
242             }
243         }
244       _M_last_overflowed = false;       
245       return __ret;
246     }
247
248   template<typename _CharT, typename _Traits>
249     typename basic_filebuf<_CharT, _Traits>::int_type 
250     basic_filebuf<_CharT, _Traits>::
251     overflow(int_type __c)
252     {
253       int_type __ret = traits_type::eof();
254       const bool __testput = this->_M_out_cur < this->_M_out_end;
255       const bool __testout = this->_M_mode & ios_base::out;
256       
257       if (__testout)
258         {
259           if (traits_type::eq_int_type(__c, traits_type::eof()))
260             __ret = traits_type::not_eof(__c);
261           else if (__testput)
262             {
263               *this->_M_out_cur = traits_type::to_char_type(__c);
264               _M_out_cur_move(1);
265               __ret = traits_type::not_eof(__c);
266             }
267           else 
268             __ret = this->_M_really_overflow(__c);
269         }
270
271       _M_last_overflowed = false;    // Set in _M_really_overflow, below.
272       return __ret;
273     }
274   
275   template<typename _CharT, typename _Traits>
276     void
277     basic_filebuf<_CharT, _Traits>::
278     _M_convert_to_external(_CharT* __ibuf, streamsize __ilen,
279                            streamsize& __elen, streamsize& __plen)
280     {
281       const locale __loc = this->getloc();
282       const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
283       // Sync with stdio.
284       const bool __sync = this->_M_buf_size <= 1;
285
286       if (__cvt.always_noconv() && __ilen)
287         {
288           __elen +=
289             _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen, __sync);
290           __plen += __ilen;
291         }
292       else
293         {
294           // Worst-case number of external bytes needed.
295           int __ext_multiplier = __cvt.encoding();
296           if (__ext_multiplier ==  -1 || __ext_multiplier == 0)
297             __ext_multiplier = sizeof(char_type);
298           streamsize __blen = __ilen * __ext_multiplier;
299           char* __buf = static_cast<char*>(__builtin_alloca(__blen));
300           char* __bend;
301           const char_type* __iend;
302           __res_type __r = __cvt.out(_M_state_cur, __ibuf, __ibuf + __ilen, 
303                                      __iend, __buf, __buf + __blen, __bend);
304
305           if (__r == codecvt_base::ok || __r == codecvt_base::partial)
306             __blen = __bend - __buf;
307           // Similarly to the always_noconv case above.
308           else if (__r == codecvt_base::noconv)
309             {
310               __buf = reinterpret_cast<char*>(__ibuf);
311               __blen = __ilen;
312             }
313           // Result == error
314           else 
315             __blen = 0;
316           
317           if (__blen)
318             {
319               __elen += _M_file.xsputn(__buf, __blen, __sync);
320               __plen += __blen;
321             }
322
323           // Try once more for partial conversions.
324           if (__r == codecvt_base::partial)
325             {
326               const char_type* __iresume = __iend;
327               streamsize __rlen = this->_M_out_lim - __iend;
328               __r = __cvt.out(_M_state_cur, __iresume, __iresume + __rlen, 
329                               __iend, __buf, __buf + __blen, __bend);
330               if (__r != codecvt_base::error)
331                 __rlen = __bend - __buf;
332               else
333                 {
334                   __rlen = 0;
335                   // Signal to the caller (_M_really_overflow) that
336                   // codecvt::out eventually failed.
337                   __elen = 0;             
338                 }
339               if (__rlen)
340                 {
341                   __elen += _M_file.xsputn(__buf, __rlen, __sync);
342                   __plen += __rlen;
343                 }
344             }
345         }
346     }
347
348   template<typename _CharT, typename _Traits>
349     typename basic_filebuf<_CharT, _Traits>::int_type 
350     basic_filebuf<_CharT, _Traits>::
351     _M_really_overflow(int_type __c)
352     {
353       int_type __ret = traits_type::eof();
354       const bool __testput = this->_M_out_beg < this->_M_out_lim;
355       const bool __testunbuffered = _M_file.is_open() && !this->_M_buf_size;
356       // Sync with stdio.
357       const bool __sync = this->_M_buf_size <= 1;
358
359       if (__testput || __testunbuffered)
360         {
361           // Sizes of external and pending output.
362           streamsize __elen = 0;
363           streamsize __plen = 0;
364
365           // Need to restore current position. The position of the external
366           // byte sequence (_M_file) corresponds to _M_filepos, and we need
367           // to move it to _M_out_beg for the write.
368           if (_M_filepos && _M_filepos != this->_M_out_beg)
369             {
370               off_type __off = this->_M_out_beg - _M_filepos;
371               _M_file.seekoff(__off, ios_base::cur, __sync);
372             }
373
374           // Convert internal buffer to external representation, output.
375           // NB: In the unbuffered case, no internal buffer exists. 
376           if (!__testunbuffered)
377             _M_convert_to_external(this->_M_out_beg,
378                                    this->_M_out_lim - this->_M_out_beg, 
379                                    __elen, __plen);
380
381           // Checks for codecvt.out failures and _M_file.xsputn failures,
382           // respectively, inside _M_convert_to_external.
383           if (__testunbuffered || (__elen && __elen == __plen))
384             {
385               // Convert pending sequence to external representation, output.
386               // If eof, then just attempt sync.
387               if (!traits_type::eq_int_type(__c, traits_type::eof()))
388                 {
389                   char_type __pending = traits_type::to_char_type(__c);
390                   _M_convert_to_external(&__pending, 1, __elen, __plen);
391
392                   // User code must flush when switching modes (thus don't sync).
393                   if (__elen == __plen && __elen)
394                     {
395                       _M_set_indeterminate();
396                       __ret = traits_type::not_eof(__c);
397                     }
398                 }
399               else if (!_M_file.sync())
400                 {
401                   _M_set_indeterminate();
402                   __ret = traits_type::not_eof(__c);
403                 }
404             }
405         }
406       _M_last_overflowed = true;        
407       return __ret;
408     }
409
410   template<typename _CharT, typename _Traits>
411     typename basic_filebuf<_CharT, _Traits>::__streambuf_type* 
412     basic_filebuf<_CharT, _Traits>::
413     setbuf(char_type* __s, streamsize __n)
414     {
415       if (!this->is_open() && __s == 0 && __n == 0)
416         this->_M_buf_size = 0;
417       else if (__s && __n)
418         {
419           // This is implementation-defined behavior, and assumes
420           // that an external char_type array of length (__s + __n)
421           // exists and has been pre-allocated. If this is not the
422           // case, things will quickly blow up.
423
424           // Step 1: Destroy the current internal array.
425           _M_destroy_internal_buffer();
426           
427           // Step 2: Use the external array.
428           this->_M_buf = __s;
429           this->_M_buf_size = __n;
430           _M_set_indeterminate();
431         }
432       _M_last_overflowed = false;       
433       return this; 
434     }
435   
436   template<typename _CharT, typename _Traits>
437     typename basic_filebuf<_CharT, _Traits>::pos_type
438     basic_filebuf<_CharT, _Traits>::
439     seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
440     {
441       pos_type __ret =  pos_type(off_type(-1)); 
442       const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
443       const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
444       // Sync with stdio.
445       const bool __sync = this->_M_buf_size <= 1;
446       
447       // Should probably do has_facet checks here.
448       int __width = use_facet<__codecvt_type>(this->_M_buf_locale).encoding();
449       if (__width < 0)
450         __width = 0;
451       const bool __testfail = __off != 0 && __width <= 0;
452       
453       if (this->is_open() && !__testfail && (__testin || __testout)) 
454         {
455           // Ditch any pback buffers to avoid confusion.
456           _M_pback_destroy();
457
458           if (__way != ios_base::cur || __off != 0)
459             { 
460               off_type __computed_off = __width * __off;
461               
462               const bool __testget = this->_M_in_beg < this->_M_in_end;
463               const bool __testput = this->_M_out_beg < this->_M_out_lim;
464               // Sync the internal and external streams.
465               // out
466               if (__testput || _M_last_overflowed)
467                 {
468                   // Part one: update the output sequence.
469                   this->sync();
470                   // Part two: output unshift sequence.
471                   _M_output_unshift();
472                 }
473               //in
474               else if (__testget && __way == ios_base::cur)
475                 __computed_off += this->_M_in_cur - _M_filepos;
476
477               // Return pos_type(off_type(-1)) in case of failure.
478               __ret = _M_file.seekoff(__computed_off, __way, __sync, __mode);
479               _M_set_indeterminate();
480             }
481           // NB: Need to do this in case _M_file in indeterminate
482           // state, ie _M_file._offset == -1
483           else
484             {
485               pos_type __tmp =
486                 _M_file.seekoff(__off, ios_base::cur,
487                                 __sync, __mode);
488               if (__tmp >= 0)
489                 {
490                   // Seek successful.
491                   __ret = __tmp;
492                   __ret +=
493                     std::max(this->_M_out_cur, this->_M_in_cur) - _M_filepos;
494                 }
495             }
496         }
497       _M_last_overflowed = false;       
498       return __ret;
499     }
500
501   template<typename _CharT, typename _Traits>
502     typename basic_filebuf<_CharT, _Traits>::pos_type
503     basic_filebuf<_CharT, _Traits>::
504     seekpos(pos_type __pos, ios_base::openmode __mode)
505     {
506 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
507 // 171. Strange seekpos() semantics due to joint position
508       return this->seekoff(off_type(__pos), ios_base::beg, __mode);
509 #endif
510     }
511
512   template<typename _CharT, typename _Traits>
513     void 
514     basic_filebuf<_CharT, _Traits>::
515     _M_output_unshift()
516     { }
517
518   template<typename _CharT, typename _Traits>
519     void
520     basic_filebuf<_CharT, _Traits>::
521     imbue(const locale& __loc)
522     {
523       const bool __testbeg = gptr() == eback() && pptr() == pbase();
524
525       if (__testbeg && this->_M_buf_locale != __loc)
526         this->_M_buf_locale = __loc;
527
528       // NB this may require the reconversion of previously
529       // converted chars. This in turn may cause the reconstruction
530       // of the original file. YIKES!!
531       // XXX The part in the above comment is not done.
532       _M_last_overflowed = false;       
533     }
534
535   // Inhibit implicit instantiations for required instantiations,
536   // which are defined via explicit instantiations elsewhere.  
537   // NB:  This syntax is a GNU extension.
538 #if _GLIBCPP_EXTERN_TEMPLATE
539   extern template class basic_filebuf<char>;
540   extern template class basic_ifstream<char>;
541   extern template class basic_ofstream<char>;
542   extern template class basic_fstream<char>;
543
544 #ifdef _GLIBCPP_USE_WCHAR_T
545   extern template class basic_filebuf<wchar_t>;
546   extern template class basic_ifstream<wchar_t>;
547   extern template class basic_ofstream<wchar_t>;
548   extern template class basic_fstream<wchar_t>;
549 #endif
550 #endif
551 } // namespace std
552
553 #endif