1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4 // Free Software Foundation, Inc.
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)
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.
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,
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.
32 // ISO C++ 14882: 27.8 File-based streams
35 #ifndef _CPP_BITS_FSTREAM_TCC
36 #define _CPP_BITS_FSTREAM_TCC 1
38 #pragma GCC system_header
42 template<typename _CharT, typename _Traits>
44 basic_filebuf<_CharT, _Traits>::
45 _M_allocate_internal_buffer()
47 if (!_M_buf && _M_buf_size_opt)
49 _M_buf_size = _M_buf_size_opt;
53 // Allocate internal buffer.
54 try { _M_buf = new char_type[_M_buf_size]; }
58 __throw_exception_again;
60 _M_buf_allocated = true;
67 // Both close and setbuf need to deallocate internal buffers, if it exists.
68 template<typename _CharT, typename _Traits>
70 basic_filebuf<_CharT, _Traits>::
71 _M_destroy_internal_buffer()
77 _M_buf_allocated = false;
78 this->setg(NULL, NULL, NULL);
79 this->setp(NULL, NULL);
83 if (_M_buf == _M_unbuf)
86 this->setg(NULL, NULL, NULL);
87 this->setp(NULL, NULL);
92 template<typename _CharT, typename _Traits>
93 basic_filebuf<_CharT, _Traits>::
95 : __streambuf_type(), _M_file(&_M_lock), _M_state_cur(__state_type()),
96 _M_state_beg(__state_type()), _M_buf_allocated(false),
97 _M_last_overflowed(false)
98 { _M_buf_unified = true; }
100 template<typename _CharT, typename _Traits>
101 basic_filebuf<_CharT, _Traits>::
102 basic_filebuf(__c_file* __f, ios_base::openmode __mode, int_type __s)
103 : __streambuf_type(), _M_file(&_M_lock), _M_state_cur(__state_type()),
104 _M_state_beg(__state_type()), _M_buf_allocated(false),
105 _M_last_overflowed(false)
107 _M_buf_unified = true;
108 _M_file.sys_open(__f, __mode);
114 _M_buf_size_opt = __s;
115 _M_allocate_internal_buffer();
116 _M_set_indeterminate();
121 template<typename _CharT, typename _Traits>
123 basic_filebuf<_CharT, _Traits>::
125 { return _M_file.fd(); }
127 template<typename _CharT, typename _Traits>
128 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
129 basic_filebuf<_CharT, _Traits>::
130 open(const char* __s, ios_base::openmode __mode)
132 __filebuf_type *__ret = NULL;
133 if (!this->is_open())
135 _M_file.open(__s, __mode);
138 _M_allocate_internal_buffer();
141 // For time being, set both (in/out) sets of pointers.
142 _M_set_indeterminate();
143 if ((__mode & ios_base::ate)
144 && this->seekoff(0, ios_base::end, __mode) < 0)
152 template<typename _CharT, typename _Traits>
153 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
154 basic_filebuf<_CharT, _Traits>::
157 __filebuf_type *__ret = NULL;
160 const int_type __eof = traits_type::eof();
161 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
162 if (__testput && _M_really_overflow(__eof) == __eof)
165 // NB: Do this here so that re-opened filebufs will be cool...
166 _M_mode = ios_base::openmode(0);
167 _M_destroy_internal_buffer();
172 if (_M_last_overflowed)
175 _M_really_overflow(__eof);
183 _M_last_overflowed = false;
187 template<typename _CharT, typename _Traits>
189 basic_filebuf<_CharT, _Traits>::
192 streamsize __ret = -1;
193 bool __testin = _M_mode & ios_base::in;
195 if (__testin && this->is_open())
197 if (_M_in_cur < _M_in_end)
198 __ret = _M_in_end - _M_in_cur;
202 _M_last_overflowed = false;
206 template<typename _CharT, typename _Traits>
207 typename basic_filebuf<_CharT, _Traits>::int_type
208 basic_filebuf<_CharT, _Traits>::
209 _M_underflow_common(bool __bump)
211 int_type __ret = traits_type::eof();
212 bool __testin = _M_mode & ios_base::in;
213 bool __testout = _M_mode & ios_base::out;
217 // Check for pback madness, and if so swich back to the
218 // normal buffers and jet outta here before expensive
223 if (_M_in_cur < _M_in_end)
224 return traits_type::to_int_type(*_M_in_cur);
227 // Check for unbuffered stream.
228 if (_M_buf_size == 1)
230 int_type __c = _M_file.sys_getc();
234 *_M_in_cur = traits_type::to_char_type(__c);
235 _M_set_determinate(1);
237 _M_out_cur = _M_in_cur;
242 // Sync internal and external buffers.
243 // NB: __testget -> __testput as _M_buf_unified here.
244 bool __testget = _M_in_cur && _M_in_beg < _M_in_cur;
245 bool __testinit = _M_is_indeterminate();
249 _M_really_overflow();
250 else if (_M_in_cur != _M_filepos)
251 _M_file.seekoff(_M_in_cur - _M_filepos,
252 ios_base::cur, ios_base::in);
255 if (__testinit || __testget)
257 const locale __loc = this->getloc();
258 const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
260 streamsize __elen = 0;
261 streamsize __ilen = 0;
262 if (__cvt.always_noconv())
264 __elen = _M_file.xsgetn(reinterpret_cast<char*>(_M_in_beg),
270 char* __buf = static_cast<char*>(__builtin_alloca(_M_buf_size));
271 __elen = _M_file.xsgetn(__buf, _M_buf_size);
275 __res_type __r = __cvt.in(_M_state_cur, __buf,
276 __buf + __elen, __eend, _M_in_beg,
277 _M_in_beg + _M_buf_size, __iend);
278 if (__r == codecvt_base::ok)
279 __ilen = __iend - _M_in_beg;
284 _M_file.seekoff(-__elen, ios_base::cur, ios_base::in);
290 _M_set_determinate(__ilen);
292 _M_out_cur = _M_in_cur;
293 __ret = traits_type::to_int_type(*_M_in_cur);
299 _M_last_overflowed = false;
303 template<typename _CharT, typename _Traits>
304 typename basic_filebuf<_CharT, _Traits>::int_type
305 basic_filebuf<_CharT, _Traits>::
306 pbackfail(int_type __i)
308 int_type __ret = traits_type::eof();
309 bool __testin = _M_mode & ios_base::in;
313 bool __testpb = _M_in_beg < _M_in_cur;
314 char_type __c = traits_type::to_char_type(__i);
315 bool __testeof = traits_type::eq_int_type(__i, __ret);
319 bool __testout = _M_mode & ios_base::out;
320 bool __testeq = traits_type::eq(__c, this->gptr()[-1]);
322 // Try to put back __c into input sequence in one of three ways.
323 // Order these tests done in is unspecified by the standard.
324 if (!__testeof && __testeq)
336 __ret = traits_type::not_eof(__i);
350 // At the beginning of the buffer, need to make a
351 // putback position available.
352 this->seekoff(-1, ios_base::cur);
356 if (!traits_type::eq(__c, *_M_in_cur))
364 __ret = traits_type::not_eof(__i);
367 _M_last_overflowed = false;
371 template<typename _CharT, typename _Traits>
372 typename basic_filebuf<_CharT, _Traits>::int_type
373 basic_filebuf<_CharT, _Traits>::
374 overflow(int_type __c)
376 int_type __ret = traits_type::eof();
377 bool __testput = _M_out_cur && _M_out_cur < _M_buf + _M_buf_size;
378 bool __testout = _M_mode & ios_base::out;
384 *_M_out_cur = traits_type::to_char_type(__c);
386 __ret = traits_type::not_eof(__c);
389 __ret = this->_M_really_overflow(__c);
392 _M_last_overflowed = false; // Set in _M_really_overflow, below.
396 template<typename _CharT, typename _Traits>
398 basic_filebuf<_CharT, _Traits>::
399 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen,
400 streamsize& __elen, streamsize& __plen)
402 const locale __loc = this->getloc();
403 const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
405 if (__cvt.always_noconv() && __ilen)
407 __elen += _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
412 // Worst-case number of external bytes needed.
413 int __ext_multiplier = __cvt.encoding();
414 if (__ext_multiplier == -1 || __ext_multiplier == 0)
415 __ext_multiplier = sizeof(char_type);
416 streamsize __blen = __ilen * __ext_multiplier;
417 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
419 const char_type* __iend;
420 __res_type __r = __cvt.out(_M_state_cur, __ibuf, __ibuf + __ilen,
421 __iend, __buf, __buf + __blen, __bend);
422 // Result == ok, partial, noconv
423 if (__r != codecvt_base::error)
424 __blen = __bend - __buf;
431 __elen += _M_file.xsputn(__buf, __blen);
435 // Try once more for partial conversions.
436 if (__r == codecvt_base::partial)
438 const char_type* __iresume = __iend;
439 streamsize __rlen = _M_out_end - __iend;
440 __r = __cvt.out(_M_state_cur, __iresume, __iresume + __rlen,
441 __iend, __buf, __buf + __blen, __bend);
442 if (__r != codecvt_base::error)
443 __rlen = __bend - __buf;
448 __elen += _M_file.xsputn(__buf, __rlen);
455 template<typename _CharT, typename _Traits>
456 typename basic_filebuf<_CharT, _Traits>::int_type
457 basic_filebuf<_CharT, _Traits>::
458 _M_really_overflow(int_type __c)
460 int_type __ret = traits_type::eof();
461 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
462 bool __testunbuffered = _M_file.is_open() && !_M_buf_size;
464 if (__testput || __testunbuffered)
466 // Sizes of external and pending output.
467 streamsize __elen = 0;
468 streamsize __plen = 0;
470 // Need to restore current position. The position of the external
471 // byte sequence (_M_file) corresponds to _M_filepos, and we need
472 // to move it to _M_out_beg for the write.
473 if (_M_filepos && _M_filepos != _M_out_beg)
475 off_type __off = _M_out_beg - _M_filepos;
476 _M_file.seekoff(__off, ios_base::cur);
479 // Convert internal buffer to external representation, output.
480 // NB: In the unbuffered case, no internal buffer exists.
481 if (!__testunbuffered)
482 _M_convert_to_external(_M_out_beg, _M_out_end - _M_out_beg,
485 // Convert pending sequence to external representation, output.
486 // If eof, then just attempt sync.
487 if (!traits_type::eq_int_type(__c, traits_type::eof()))
489 char_type __pending = traits_type::to_char_type(__c);
490 _M_convert_to_external(&__pending, 1, __elen, __plen);
492 // User code must flush when switching modes (thus don't sync).
493 if (__elen == __plen)
495 _M_set_indeterminate();
496 __ret = traits_type::not_eof(__c);
499 else if (!_M_file.sync())
501 _M_set_indeterminate();
502 __ret = traits_type::not_eof(__c);
505 _M_last_overflowed = true;
509 template<typename _CharT, typename _Traits>
510 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
511 basic_filebuf<_CharT, _Traits>::
512 setbuf(char_type* __s, streamsize __n)
514 if (!this->is_open() && __s == 0 && __n == 0)
518 // This is implementation-defined behavior, and assumes
519 // that an external char_type array of length (__s + __n)
520 // exists and has been pre-allocated. If this is not the
521 // case, things will quickly blow up.
522 // Step 1: Destroy the current internal array.
523 _M_destroy_internal_buffer();
525 // Step 2: Use the external array.
527 _M_buf_size_opt = _M_buf_size = __n;
528 _M_set_indeterminate();
530 _M_last_overflowed = false;
534 template<typename _CharT, typename _Traits>
535 typename basic_filebuf<_CharT, _Traits>::pos_type
536 basic_filebuf<_CharT, _Traits>::
537 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
539 pos_type __ret = pos_type(off_type(-1));
540 bool __testin = (ios_base::in & _M_mode & __mode) != 0;
541 bool __testout = (ios_base::out & _M_mode & __mode) != 0;
543 // Should probably do has_facet checks here.
544 int __width = use_facet<__codecvt_type>(_M_buf_locale).encoding();
547 bool __testfail = __off != 0 && __width <= 0;
549 if (this->is_open() && !__testfail && (__testin || __testout))
551 // Ditch any pback buffers to avoid confusion.
554 if (__way != ios_base::cur || __off != 0)
556 off_type __computed_off = __width * __off;
558 bool __testget = _M_in_cur && _M_in_beg < _M_in_end;
559 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
560 // Sync the internal and external streams.
562 if (__testput || _M_last_overflowed)
564 // Part one: update the output sequence.
566 // Part two: output unshift sequence.
570 else if (__testget && __way == ios_base::cur)
571 __computed_off += _M_in_cur - _M_filepos;
573 __ret = _M_file.seekoff(__computed_off, __way, __mode);
574 _M_set_indeterminate();
576 // NB: Need to do this in case _M_file in indeterminate
577 // state, ie _M_file._offset == -1
580 __ret = _M_file.seekoff(__off, ios_base::cur, __mode);
581 __ret += max(_M_out_cur, _M_in_cur) - _M_filepos;
584 _M_last_overflowed = false;
588 template<typename _CharT, typename _Traits>
589 typename basic_filebuf<_CharT, _Traits>::pos_type
590 basic_filebuf<_CharT, _Traits>::
591 seekpos(pos_type __pos, ios_base::openmode __mode)
593 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
594 // 171. Strange seekpos() semantics due to joint position
595 return this->seekoff(off_type(__pos), ios_base::beg, __mode);
599 template<typename _CharT, typename _Traits>
601 basic_filebuf<_CharT, _Traits>::
605 template<typename _CharT, typename _Traits>
607 basic_filebuf<_CharT, _Traits>::
608 imbue(const locale& __loc)
610 bool __testbeg = gptr() == eback() && pptr() == pbase();
612 if (__testbeg && _M_buf_locale != __loc)
614 _M_buf_locale = __loc;
615 _M_buf_locale_init = true;
618 // NB this may require the reconversion of previously
619 // converted chars. This in turn may cause the reconstruction
620 // of the original file. YIKES!!
621 // XXX The part in the above comment is not done.
622 _M_last_overflowed = false;
625 // Inhibit implicit instantiations for required instantiations,
626 // which are defined via explicit instantiations elsewhere.
627 // NB: This syntax is a GNU extension.
628 extern template class basic_filebuf<char>;
629 extern template class basic_filebuf<wchar_t>;
630 extern template class basic_ifstream<char>;
631 extern template class basic_ifstream<wchar_t>;
632 extern template class basic_ofstream<char>;
633 extern template class basic_ofstream<wchar_t>;
634 extern template class basic_fstream<char>;
635 extern template class basic_fstream<wchar_t>;