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>::
49 _M_buf_unified = true; // Tie input to output for basic_filebuf.
51 { _M_file = new __file_type(&_M_lock); }
55 __throw_exception_again;
60 template<typename _CharT, typename _Traits>
62 basic_filebuf<_CharT, _Traits>::
63 _M_allocate_internal_buffer()
65 if (!_M_buf && _M_buf_size_opt)
67 _M_buf_size = _M_buf_size_opt;
69 // Allocate internal buffer.
70 try { _M_buf = new char_type[_M_buf_size]; }
74 __throw_exception_again;
76 _M_buf_allocated = true;
80 // Both close and setbuf need to deallocate internal buffers, if it exists.
81 template<typename _CharT, typename _Traits>
83 basic_filebuf<_CharT, _Traits>::
84 _M_destroy_internal_buffer()
90 _M_buf_allocated = false;
91 this->setg(NULL, NULL, NULL);
92 this->setp(NULL, NULL);
96 template<typename _CharT, typename _Traits>
98 basic_filebuf<_CharT, _Traits>::
99 _M_allocate_pback_buffer()
101 if (!_M_pback && _M_pback_size)
103 // Allocate pback buffer.
105 { _M_pback = new char_type[_M_pback_size]; }
109 __throw_exception_again;
114 template<typename _CharT, typename _Traits>
115 basic_filebuf<_CharT, _Traits>::
117 : __streambuf_type(), _M_file(NULL), _M_state_cur(__state_type()),
118 _M_state_beg(__state_type()), _M_buf_allocated(false),
119 _M_last_overflowed(false)
122 template<typename _CharT, typename _Traits>
123 basic_filebuf<_CharT, _Traits>::
124 basic_filebuf(__c_file_type* __f, ios_base::openmode __mode, int_type __s)
125 : __streambuf_type(), _M_file(NULL), _M_state_cur(__state_type()),
126 _M_state_beg(__state_type()), _M_buf_allocated(false),
127 _M_last_overflowed(false)
130 _M_file->sys_open(__f, __mode);
136 _M_buf_size_opt = __s;
137 _M_allocate_internal_buffer();
138 _M_set_indeterminate();
140 _M_allocate_pback_buffer();
144 template<typename _CharT, typename _Traits>
146 basic_filebuf<_CharT, _Traits>::
148 { return _M_file->fd(); }
150 template<typename _CharT, typename _Traits>
151 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
152 basic_filebuf<_CharT, _Traits>::
153 open(const char* __s, ios_base::openmode __mode)
155 __filebuf_type *__ret = NULL;
156 if (!this->is_open())
159 _M_file->open(__s, __mode);
162 _M_allocate_internal_buffer();
163 _M_allocate_pback_buffer();
166 // For time being, set both (in/out) sets of pointers.
167 _M_set_indeterminate();
168 if (__mode & ios_base::ate
169 && this->seekoff(0, ios_base::end, __mode) < 0)
177 template<typename _CharT, typename _Traits>
178 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
179 basic_filebuf<_CharT, _Traits>::
182 __filebuf_type *__ret = NULL;
185 const int_type __eof = traits_type::eof();
186 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
187 if (__testput && _M_really_overflow(__eof) == __eof)
190 // NB: Do this here so that re-opened filebufs will be cool...
191 _M_mode = ios_base::openmode(0);
192 _M_destroy_internal_buffer();
203 if (_M_last_overflowed)
206 _M_really_overflow(__eof);
212 // Can actually allocate this file as part of an open and never
213 // have it be opened.....
219 _M_last_overflowed = false;
223 template<typename _CharT, typename _Traits>
225 basic_filebuf<_CharT, _Traits>::
228 streamsize __ret = -1;
229 bool __testin = _M_mode & ios_base::in;
231 if (__testin && this->is_open())
233 if (_M_in_cur < _M_in_end)
234 __ret = _M_in_end - _M_in_cur;
238 _M_last_overflowed = false;
242 template<typename _CharT, typename _Traits>
243 typename basic_filebuf<_CharT, _Traits>::int_type
244 basic_filebuf<_CharT, _Traits>::
247 int_type __ret = traits_type::eof();
248 bool __testin = _M_mode & ios_base::in;
249 bool __testout = _M_mode & ios_base::out;
253 // Check for pback madness, and if so swich back to the
254 // normal buffers and jet outta here before expensive
259 if (_M_in_cur < _M_in_end)
260 return traits_type::to_int_type(*_M_in_cur);
263 // Sync internal and external buffers.
264 // NB: __testget -> __testput as _M_buf_unified here.
265 bool __testget = _M_in_cur && _M_in_beg < _M_in_cur;
266 bool __testinit = _M_is_indeterminate();
270 _M_really_overflow();
271 #if _GLIBCPP_AVOID_FSEEK
272 else if ((_M_in_cur - _M_in_beg) == 1)
276 _M_file->seekoff(_M_in_cur - _M_in_beg,
277 ios_base::cur, ios_base::in);
280 if (__testinit || __testget)
282 const locale __loc = this->getloc();
283 const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
285 streamsize __elen = 0;
286 streamsize __ilen = 0;
287 if (__cvt.always_noconv())
289 __elen = _M_file->xsgetn(reinterpret_cast<char*>(_M_in_beg),
295 char* __buf = static_cast<char*>(__builtin_alloca(_M_buf_size));
296 __elen = _M_file->xsgetn(__buf, _M_buf_size);
300 __res_type __r = __cvt.in(_M_state_cur, __buf,
301 __buf + __elen, __eend, _M_in_beg,
302 _M_in_beg + _M_buf_size, __iend);
303 if (__r == codecvt_base::ok)
304 __ilen = __iend - _M_in_beg;
309 _M_file->seekoff(-__elen, ios_base::cur, ios_base::in);
315 _M_set_determinate(__ilen);
317 _M_out_cur = _M_in_cur;
318 __ret = traits_type::to_int_type(*_M_in_cur);
319 #if _GLIBCPP_AVOID_FSEEK
321 _M_file->sys_ungetc(*_M_in_cur);
325 _M_file->seekoff(-__elen, ios_base::cur, ios_base::in);
326 #if _GLIBCPP_AVOID_FSEEK
332 _M_last_overflowed = false;
336 template<typename _CharT, typename _Traits>
337 typename basic_filebuf<_CharT, _Traits>::int_type
338 basic_filebuf<_CharT, _Traits>::
339 pbackfail(int_type __i)
341 int_type __ret = traits_type::eof();
342 bool __testin = _M_mode & ios_base::in;
346 bool __testpb = _M_in_beg < _M_in_cur;
347 char_type __c = traits_type::to_char_type(__i);
348 bool __testeof = traits_type::eq_int_type(__i, __ret);
352 bool __testout = _M_mode & ios_base::out;
353 bool __testeq = traits_type::eq(__c, this->gptr()[-1]);
355 // Try to put back __c into input sequence in one of three ways.
356 // Order these tests done in is unspecified by the standard.
357 if (!__testeof && __testeq)
369 __ret = traits_type::not_eof(__i);
383 // At the beginning of the buffer, need to make a
384 // putback position available.
385 this->seekoff(-1, ios_base::cur);
389 if (!traits_type::eq(__c, *_M_in_cur))
397 __ret = traits_type::not_eof(__i);
400 _M_last_overflowed = false;
404 template<typename _CharT, typename _Traits>
405 typename basic_filebuf<_CharT, _Traits>::int_type
406 basic_filebuf<_CharT, _Traits>::
407 overflow(int_type __c)
409 int_type __ret = traits_type::eof();
410 bool __testput = _M_out_cur && _M_out_cur < _M_buf + _M_buf_size;
411 bool __testout = _M_mode & ios_base::out;
417 *_M_out_cur = traits_type::to_char_type(__c);
419 __ret = traits_type::not_eof(__c);
422 __ret = this->_M_really_overflow(__c);
425 _M_last_overflowed = false; // Set in _M_really_overflow, below.
429 template<typename _CharT, typename _Traits>
431 basic_filebuf<_CharT, _Traits>::
432 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen,
433 streamsize& __elen, streamsize& __plen)
435 const locale __loc = this->getloc();
436 const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
438 if (__cvt.always_noconv() && __ilen)
440 __elen += _M_file->xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
445 // Worst-case number of external bytes needed.
446 int __ext_multiplier = __cvt.encoding();
447 if (__ext_multiplier == -1 || __ext_multiplier == 0)
448 __ext_multiplier = sizeof(char_type);
449 streamsize __blen = __ilen * __ext_multiplier;
450 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
452 const char_type* __iend;
453 __res_type __r = __cvt.out(_M_state_cur, __ibuf, __ibuf + __ilen,
454 __iend, __buf, __buf + __blen, __bend);
455 // Result == ok, partial, noconv
456 if (__r != codecvt_base::error)
457 __blen = __bend - __buf;
464 __elen += _M_file->xsputn(__buf, __blen);
468 // Try once more for partial conversions.
469 if (__r == codecvt_base::partial)
471 const char_type* __iresume = __iend;
472 streamsize __rlen = _M_out_end - __iend;
473 __r = __cvt.out(_M_state_cur, __iresume, __iresume + __rlen,
474 __iend, __buf, __buf + __blen, __bend);
475 if (__r != codecvt_base::error)
476 __rlen = __bend - __buf;
481 __elen += _M_file->xsputn(__buf, __rlen);
488 template<typename _CharT, typename _Traits>
489 typename basic_filebuf<_CharT, _Traits>::int_type
490 basic_filebuf<_CharT, _Traits>::
491 _M_really_overflow(int_type __c)
493 int_type __ret = traits_type::eof();
494 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
495 bool __testunbuffered = _M_file && !_M_buf_size;
497 if (__testput || __testunbuffered)
499 // Sizes of external and pending output.
500 streamsize __elen = 0;
501 streamsize __plen = 0;
503 // Convert internal buffer to external representation, output.
504 // NB: In the unbuffered case, no internal buffer exists.
505 if (!__testunbuffered)
506 _M_convert_to_external(_M_out_beg, _M_out_end - _M_out_beg,
509 // Convert pending sequence to external representation, output.
510 if (!traits_type::eq_int_type(__c, traits_type::eof()))
512 char_type __pending = traits_type::to_char_type(__c);
513 _M_convert_to_external(&__pending, 1, __elen, __plen);
516 // Last, sync internal and external buffers.
517 // NB: Need this so that external byte sequence reflects
518 // internal buffer plus pending sequence.
519 if (__elen == __plen && !_M_file->sync())
521 _M_set_indeterminate();
522 __ret = traits_type::not_eof(__c);
525 _M_last_overflowed = true;
529 template<typename _CharT, typename _Traits>
530 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
531 basic_filebuf<_CharT, _Traits>::
532 setbuf(char_type* __s, streamsize __n)
534 if (!this->is_open() && __s == 0 && __n == 0)
538 // This is implementation-defined behavior, and assumes
539 // that an external char_type array of length (__s + __n)
540 // exists and has been pre-allocated. If this is not the
541 // case, things will quickly blow up.
542 // Step 1: Destroy the current internal array.
543 _M_destroy_internal_buffer();
545 // Step 2: Use the external array.
547 _M_buf_size_opt = _M_buf_size = __n;
548 _M_set_indeterminate();
550 // Step 3: Make sure a pback buffer is allocated.
551 _M_allocate_pback_buffer();
553 _M_last_overflowed = false;
557 template<typename _CharT, typename _Traits>
558 typename basic_filebuf<_CharT, _Traits>::pos_type
559 basic_filebuf<_CharT, _Traits>::
560 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
562 pos_type __ret = pos_type(off_type(-1));
563 bool __testopen = this->is_open();
564 bool __testin = __mode & ios_base::in && _M_mode & ios_base::in;
565 bool __testout = __mode & ios_base::out && _M_mode & ios_base::out;
567 // Should probably do has_facet checks here.
568 int __width = use_facet<__codecvt_type>(_M_buf_locale).encoding();
571 bool __testfail = __off != 0 && __width <= 0;
573 if (__testopen && !__testfail && (__testin || __testout))
575 // Ditch any pback buffers to avoid confusion.
578 if (__way != ios_base::cur || __off != 0)
580 off_type __computed_off = __width * __off;
582 bool __testget = _M_in_cur && _M_in_beg < _M_in_end;
583 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
584 // Sync the internal and external streams.
586 if (__testput || _M_last_overflowed)
588 // Part one: update the output sequence.
590 // Part two: output unshift sequence.
594 // NB: underflow() rewinds the external buffer.
595 else if (__testget && __way == ios_base::cur)
596 __computed_off += _M_in_cur - _M_in_beg;
598 __ret = _M_file->seekoff(__computed_off, __way, __mode);
599 _M_set_indeterminate();
601 // NB: Need to do this in case _M_file in indeterminate
602 // state, ie _M_file->_offset == -1
605 __ret = _M_file->seekoff(__off, ios_base::cur, __mode);
606 __ret += max(_M_out_cur, _M_in_cur) - _M_buf;
609 _M_last_overflowed = false;
613 template<typename _CharT, typename _Traits>
614 typename basic_filebuf<_CharT, _Traits>::pos_type
615 basic_filebuf<_CharT, _Traits>::
616 seekpos(pos_type __pos, ios_base::openmode __mode)
619 off_type __off = __pos;
621 __ret = this->seekoff(__off, ios_base::beg, __mode);
623 _M_last_overflowed = false;
627 template<typename _CharT, typename _Traits>
629 basic_filebuf<_CharT, _Traits>::
633 template<typename _CharT, typename _Traits>
635 basic_filebuf<_CharT, _Traits>::
636 imbue(const locale& __loc)
638 bool __testbeg = gptr() == eback() && pptr() == pbase();
640 if (__testbeg && _M_buf_locale != __loc)
642 _M_buf_locale = __loc;
643 _M_buf_locale_init = true;
646 // NB this may require the reconversion of previously
647 // converted chars. This in turn may cause the reconstruction
648 // of the original file. YIKES!!
649 // XXX The part in the above comment is not done.
650 _M_last_overflowed = false;
653 // Inhibit implicit instantiations for required instantiations,
654 // which are defined via explicit instantiations elsewhere.
655 // NB: This syntax is a GNU extension.
656 extern template class basic_filebuf<char>;
657 extern template class basic_filebuf<wchar_t>;
658 extern template class basic_ifstream<char>;
659 extern template class basic_ifstream<wchar_t>;
660 extern template class basic_ofstream<char>;
661 extern template class basic_ofstream<wchar_t>;
662 extern template class basic_fstream<char>;
663 extern template class basic_fstream<wchar_t>;