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;
51 // Allocate internal buffer.
52 try { _M_buf = new char_type[_M_buf_size]; }
56 __throw_exception_again;
58 _M_buf_allocated = true;
62 // Both close and setbuf need to deallocate internal buffers, if it exists.
63 template<typename _CharT, typename _Traits>
65 basic_filebuf<_CharT, _Traits>::
66 _M_destroy_internal_buffer()
72 _M_buf_allocated = false;
73 this->setg(NULL, NULL, NULL);
74 this->setp(NULL, NULL);
78 template<typename _CharT, typename _Traits>
79 basic_filebuf<_CharT, _Traits>::
80 basic_filebuf() : __streambuf_type(), _M_file(&_M_lock),
81 _M_state_cur(__state_type()), _M_state_beg(__state_type()),
82 _M_buf_allocated(false), _M_last_overflowed(false)
83 { _M_buf_unified = true; }
85 template<typename _CharT, typename _Traits>
86 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
87 basic_filebuf<_CharT, _Traits>::
88 open(const char* __s, ios_base::openmode __mode)
90 __filebuf_type *__ret = NULL;
93 _M_file.open(__s, __mode);
96 _M_allocate_internal_buffer();
99 // For time being, set both (in/out) sets of pointers.
100 _M_set_indeterminate();
101 if ((__mode & ios_base::ate)
102 && this->seekoff(0, ios_base::end, __mode) < 0)
110 template<typename _CharT, typename _Traits>
111 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
112 basic_filebuf<_CharT, _Traits>::
115 __filebuf_type *__ret = NULL;
118 const int_type __eof = traits_type::eof();
119 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
120 if (__testput && _M_really_overflow(__eof) == __eof)
123 // NB: Do this here so that re-opened filebufs will be cool...
124 _M_mode = ios_base::openmode(0);
125 _M_destroy_internal_buffer();
130 if (_M_last_overflowed)
133 _M_really_overflow(__eof);
141 _M_last_overflowed = false;
145 template<typename _CharT, typename _Traits>
147 basic_filebuf<_CharT, _Traits>::
150 streamsize __ret = -1;
151 bool __testin = _M_mode & ios_base::in;
153 if (__testin && this->is_open())
155 if (_M_in_cur < _M_in_end)
156 __ret = _M_in_end - _M_in_cur;
160 _M_last_overflowed = false;
164 template<typename _CharT, typename _Traits>
165 typename basic_filebuf<_CharT, _Traits>::int_type
166 basic_filebuf<_CharT, _Traits>::
167 _M_underflow_common(bool __bump)
169 int_type __ret = traits_type::eof();
170 bool __testin = _M_mode & ios_base::in;
171 bool __testout = _M_mode & ios_base::out;
175 // Check for pback madness, and if so swich back to the
176 // normal buffers and jet outta here before expensive
181 if (_M_in_cur < _M_in_end)
182 return traits_type::to_int_type(*_M_in_cur);
185 // Check for unbuffered stream.
186 if (_M_buf_size == 1)
188 int_type __c = _M_file.sys_getc();
192 *_M_in_cur = traits_type::to_char_type(__c);
193 _M_set_determinate(1);
195 _M_out_cur = _M_in_cur;
200 // Sync internal and external buffers.
201 // NB: __testget -> __testput as _M_buf_unified here.
202 bool __testget = _M_in_cur && _M_in_beg < _M_in_cur;
203 bool __testinit = _M_is_indeterminate();
207 _M_really_overflow();
208 else if (_M_in_cur != _M_filepos)
209 _M_file.seekoff(_M_in_cur - _M_filepos,
210 ios_base::cur, ios_base::in);
213 if (__testinit || __testget)
215 const locale __loc = this->getloc();
216 const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
218 streamsize __elen = 0;
219 streamsize __ilen = 0;
220 if (__cvt.always_noconv())
222 __elen = _M_file.xsgetn(reinterpret_cast<char*>(_M_in_beg),
228 char* __buf = static_cast<char*>(__builtin_alloca(_M_buf_size));
229 __elen = _M_file.xsgetn(__buf, _M_buf_size);
233 __res_type __r = __cvt.in(_M_state_cur, __buf,
234 __buf + __elen, __eend, _M_in_beg,
235 _M_in_beg + _M_buf_size, __iend);
236 if (__r == codecvt_base::ok)
237 __ilen = __iend - _M_in_beg;
242 _M_file.seekoff(-__elen, ios_base::cur, ios_base::in);
248 _M_set_determinate(__ilen);
250 _M_out_cur = _M_in_cur;
251 __ret = traits_type::to_int_type(*_M_in_cur);
257 _M_last_overflowed = false;
261 template<typename _CharT, typename _Traits>
262 typename basic_filebuf<_CharT, _Traits>::int_type
263 basic_filebuf<_CharT, _Traits>::
264 pbackfail(int_type __i)
266 int_type __ret = traits_type::eof();
267 bool __testin = _M_mode & ios_base::in;
271 bool __testpb = _M_in_beg < _M_in_cur;
272 char_type __c = traits_type::to_char_type(__i);
273 bool __testeof = traits_type::eq_int_type(__i, __ret);
277 bool __testout = _M_mode & ios_base::out;
278 bool __testeq = traits_type::eq(__c, this->gptr()[-1]);
280 // Try to put back __c into input sequence in one of three ways.
281 // Order these tests done in is unspecified by the standard.
282 if (!__testeof && __testeq)
294 __ret = traits_type::not_eof(__i);
308 // At the beginning of the buffer, need to make a
309 // putback position available.
310 this->seekoff(-1, ios_base::cur);
314 if (!traits_type::eq(__c, *_M_in_cur))
322 __ret = traits_type::not_eof(__i);
325 _M_last_overflowed = false;
329 template<typename _CharT, typename _Traits>
330 typename basic_filebuf<_CharT, _Traits>::int_type
331 basic_filebuf<_CharT, _Traits>::
332 overflow(int_type __c)
334 int_type __ret = traits_type::eof();
335 bool __testput = _M_out_cur && _M_out_cur < _M_buf + _M_buf_size;
336 bool __testout = _M_mode & ios_base::out;
342 *_M_out_cur = traits_type::to_char_type(__c);
344 __ret = traits_type::not_eof(__c);
347 __ret = this->_M_really_overflow(__c);
350 _M_last_overflowed = false; // Set in _M_really_overflow, below.
354 template<typename _CharT, typename _Traits>
356 basic_filebuf<_CharT, _Traits>::
357 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen,
358 streamsize& __elen, streamsize& __plen)
360 const locale __loc = this->getloc();
361 const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
363 if (__cvt.always_noconv() && __ilen)
365 __elen += _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
370 // Worst-case number of external bytes needed.
371 int __ext_multiplier = __cvt.encoding();
372 if (__ext_multiplier == -1 || __ext_multiplier == 0)
373 __ext_multiplier = sizeof(char_type);
374 streamsize __blen = __ilen * __ext_multiplier;
375 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
377 const char_type* __iend;
378 __res_type __r = __cvt.out(_M_state_cur, __ibuf, __ibuf + __ilen,
379 __iend, __buf, __buf + __blen, __bend);
380 // Result == ok, partial, noconv
381 if (__r != codecvt_base::error)
382 __blen = __bend - __buf;
389 __elen += _M_file.xsputn(__buf, __blen);
393 // Try once more for partial conversions.
394 if (__r == codecvt_base::partial)
396 const char_type* __iresume = __iend;
397 streamsize __rlen = _M_out_end - __iend;
398 __r = __cvt.out(_M_state_cur, __iresume, __iresume + __rlen,
399 __iend, __buf, __buf + __blen, __bend);
400 if (__r != codecvt_base::error)
401 __rlen = __bend - __buf;
406 __elen += _M_file.xsputn(__buf, __rlen);
413 template<typename _CharT, typename _Traits>
414 typename basic_filebuf<_CharT, _Traits>::int_type
415 basic_filebuf<_CharT, _Traits>::
416 _M_really_overflow(int_type __c)
418 int_type __ret = traits_type::eof();
419 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
420 bool __testunbuffered = _M_file.is_open() && !_M_buf_size;
422 if (__testput || __testunbuffered)
424 // Sizes of external and pending output.
425 streamsize __elen = 0;
426 streamsize __plen = 0;
428 // Need to restore current position. The position of the external
429 // byte sequence (_M_file) corresponds to _M_filepos, and we need
430 // to move it to _M_out_beg for the write.
431 if (_M_filepos && _M_filepos != _M_out_beg)
433 off_type __off = _M_out_beg - _M_filepos;
434 _M_file.seekoff(__off, ios_base::cur);
437 // Convert internal buffer to external representation, output.
438 // NB: In the unbuffered case, no internal buffer exists.
439 if (!__testunbuffered)
440 _M_convert_to_external(_M_out_beg, _M_out_end - _M_out_beg,
443 // Convert pending sequence to external representation, output.
444 // If eof, then just attempt sync.
445 if (!traits_type::eq_int_type(__c, traits_type::eof()))
447 char_type __pending = traits_type::to_char_type(__c);
448 _M_convert_to_external(&__pending, 1, __elen, __plen);
450 // User code must flush when switching modes (thus don't sync).
451 if (__elen == __plen)
453 _M_set_indeterminate();
454 __ret = traits_type::not_eof(__c);
457 else if (!_M_file.sync())
459 _M_set_indeterminate();
460 __ret = traits_type::not_eof(__c);
463 _M_last_overflowed = true;
467 template<typename _CharT, typename _Traits>
468 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
469 basic_filebuf<_CharT, _Traits>::
470 setbuf(char_type* __s, streamsize __n)
472 if (!this->is_open() && __s == 0 && __n == 0)
476 // This is implementation-defined behavior, and assumes
477 // that an external char_type array of length (__s + __n)
478 // exists and has been pre-allocated. If this is not the
479 // case, things will quickly blow up.
480 // Step 1: Destroy the current internal array.
481 _M_destroy_internal_buffer();
483 // Step 2: Use the external array.
485 _M_buf_size_opt = _M_buf_size = __n;
486 _M_set_indeterminate();
488 _M_last_overflowed = false;
492 template<typename _CharT, typename _Traits>
493 typename basic_filebuf<_CharT, _Traits>::pos_type
494 basic_filebuf<_CharT, _Traits>::
495 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
497 pos_type __ret = pos_type(off_type(-1));
498 bool __testin = (ios_base::in & _M_mode & __mode) != 0;
499 bool __testout = (ios_base::out & _M_mode & __mode) != 0;
501 // Should probably do has_facet checks here.
502 int __width = use_facet<__codecvt_type>(_M_buf_locale).encoding();
505 bool __testfail = __off != 0 && __width <= 0;
507 if (this->is_open() && !__testfail && (__testin || __testout))
509 // Ditch any pback buffers to avoid confusion.
512 if (__way != ios_base::cur || __off != 0)
514 off_type __computed_off = __width * __off;
516 bool __testget = _M_in_cur && _M_in_beg < _M_in_end;
517 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
518 // Sync the internal and external streams.
520 if (__testput || _M_last_overflowed)
522 // Part one: update the output sequence.
524 // Part two: output unshift sequence.
528 else if (__testget && __way == ios_base::cur)
529 __computed_off += _M_in_cur - _M_filepos;
531 __ret = _M_file.seekoff(__computed_off, __way, __mode);
532 _M_set_indeterminate();
534 // NB: Need to do this in case _M_file in indeterminate
535 // state, ie _M_file._offset == -1
538 __ret = _M_file.seekoff(__off, ios_base::cur, __mode);
539 __ret += max(_M_out_cur, _M_in_cur) - _M_filepos;
542 _M_last_overflowed = false;
546 template<typename _CharT, typename _Traits>
547 typename basic_filebuf<_CharT, _Traits>::pos_type
548 basic_filebuf<_CharT, _Traits>::
549 seekpos(pos_type __pos, ios_base::openmode __mode)
551 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
552 // 171. Strange seekpos() semantics due to joint position
553 return this->seekoff(off_type(__pos), ios_base::beg, __mode);
557 template<typename _CharT, typename _Traits>
559 basic_filebuf<_CharT, _Traits>::
563 template<typename _CharT, typename _Traits>
565 basic_filebuf<_CharT, _Traits>::
566 imbue(const locale& __loc)
568 bool __testbeg = gptr() == eback() && pptr() == pbase();
570 if (__testbeg && _M_buf_locale != __loc)
572 _M_buf_locale = __loc;
573 _M_buf_locale_init = true;
576 // NB this may require the reconversion of previously
577 // converted chars. This in turn may cause the reconstruction
578 // of the original file. YIKES!!
579 // XXX The part in the above comment is not done.
580 _M_last_overflowed = false;
583 // Inhibit implicit instantiations for required instantiations,
584 // which are defined via explicit instantiations elsewhere.
585 // NB: This syntax is a GNU extension.
586 extern template class basic_filebuf<char>;
587 extern template class basic_filebuf<wchar_t>;
588 extern template class basic_ifstream<char>;
589 extern template class basic_ifstream<wchar_t>;
590 extern template class basic_ofstream<char>;
591 extern template class basic_ofstream<wchar_t>;
592 extern template class basic_fstream<char>;
593 extern template class basic_fstream<wchar_t>;