1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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_allocated && this->_M_buf_size)
49 // Allocate internal buffer.
50 this->_M_buf = new char_type[this->_M_buf_size];
51 _M_buf_allocated = true;
55 // Both close and setbuf need to deallocate internal buffers, if it exists.
56 template<typename _CharT, typename _Traits>
58 basic_filebuf<_CharT, _Traits>::
59 _M_destroy_internal_buffer() throw()
63 delete [] this->_M_buf;
65 _M_buf_allocated = false;
66 this->setg(NULL, NULL, NULL);
67 this->setp(NULL, NULL);
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()), _M_buf(NULL),
75 _M_buf_allocated(false),_M_last_overflowed(false),
76 _M_filepos(0), _M_pback(char_type()), _M_pback_cur_save(0),
77 _M_pback_end_save(0), _M_pback_init(false), _M_codecvt(0)
79 this->_M_buf_unified = true;
80 if (has_facet<__codecvt_type>(this->_M_buf_locale))
81 _M_codecvt = &use_facet<__codecvt_type>(this->_M_buf_locale);
84 template<typename _CharT, typename _Traits>
85 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
86 basic_filebuf<_CharT, _Traits>::
87 open(const char* __s, ios_base::openmode __mode)
89 __filebuf_type *__ret = NULL;
92 _M_file.open(__s, __mode);
95 _M_allocate_internal_buffer();
96 this->_M_mode = __mode;
98 // Setup initial position of buffer.
101 if ((__mode & ios_base::ate)
102 && this->seekoff(0, ios_base::end, __mode) < 0)
112 template<typename _CharT, typename _Traits>
113 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
114 basic_filebuf<_CharT, _Traits>::
117 __filebuf_type* __ret = NULL;
120 bool __testfail = false;
123 const int_type __eof = traits_type::eof();
124 const bool __testput = this->_M_out_beg < this->_M_out_lim;
127 && traits_type::eq_int_type(this->overflow(), __eof))
132 if (_M_last_overflowed)
144 // NB: Do this here so that re-opened filebufs will be cool...
145 this->_M_mode = ios_base::openmode(0);
146 this->_M_pback_init = false;
147 _M_destroy_internal_buffer();
149 if (!_M_file.close())
155 _M_last_overflowed = false;
159 template<typename _CharT, typename _Traits>
161 basic_filebuf<_CharT, _Traits>::
164 streamsize __ret = -1;
165 const bool __testin = this->_M_mode & ios_base::in;
167 if (__testin && this->is_open())
169 // For a stateful encoding (-1) the pending sequence might be just
170 // shift and unshift prefixes with no actual character.
171 __ret = this->_M_in_end - this->_M_in_cur;
172 if (__check_facet(_M_codecvt).encoding() >= 0)
173 __ret += _M_file.showmanyc() / _M_codecvt->max_length();
176 _M_last_overflowed = false;
180 template<typename _CharT, typename _Traits>
181 typename basic_filebuf<_CharT, _Traits>::int_type
182 basic_filebuf<_CharT, _Traits>::
183 _M_underflow(bool __bump)
185 int_type __ret = traits_type::eof();
186 const bool __testin = this->_M_mode & ios_base::in;
187 const bool __testout = this->_M_mode & ios_base::out;
191 // Check for pback madness, and if so swich back to the
192 // normal buffers and jet outta here before expensive
196 const size_t __buflen = this->_M_buf_size
197 ? this->_M_buf_size - 1 : 0;
200 if (this->_M_in_cur < this->_M_in_end)
202 __ret = traits_type::to_int_type(*this->_M_in_cur);
208 // Sync internal and external buffers.
209 if (__testout && this->_M_out_beg < this->_M_out_lim)
212 // Get and convert input sequence.
213 streamsize __elen = 0;
214 streamsize __ilen = 0;
215 if (__check_facet(_M_codecvt).always_noconv())
217 __elen = _M_file.xsgetn(reinterpret_cast<char*>(this->_M_in_beg), __buflen);
222 char* __buf = static_cast<char*>(__builtin_alloca(__buflen));
223 __elen = _M_file.xsgetn(__buf, __buflen);
227 codecvt_base::result __r;
228 __r = _M_codecvt->in(_M_state_cur, __buf, __buf + __elen,
229 __eend, this->_M_in_beg,
230 this->_M_in_beg + __buflen, __iend);
231 if (__r == codecvt_base::ok)
232 __ilen = __iend - this->_M_in_beg;
233 else if (__r == codecvt_base::noconv)
235 traits_type::copy(this->_M_in_beg,
236 reinterpret_cast<char_type*>(__buf),
244 _M_file.seekoff(-__elen, ios_base::cur, ios_base::in);
250 _M_set_buffer(__ilen);
251 __ret = traits_type::to_int_type(*this->_M_in_cur);
260 if (_M_file.xsgetn(&__buf, 1) > 0)
262 if (__check_facet(_M_codecvt).always_noconv())
264 char_type* __cp = reinterpret_cast<char_type*>(&__buf);
265 __ret = traits_type::to_int_type(*__cp);
272 codecvt_base::result __r;
273 __r = _M_codecvt->in(_M_state_cur, &__buf, &__buf + 1,
274 __eend, &__c, &__c + 1, __iend);
275 if (__r == codecvt_base::ok
276 || __r == codecvt_base::noconv)
277 __ret = traits_type::to_int_type(__c);
280 // Need to put back this extracted character so that
281 // sgetc will not advance the input stream iff
282 // underflow, but cannot call pbackfail directly as
283 // it calls underflow... which leads to a recursive
288 *this->_M_in_cur = traits_type::to_char_type(__ret);
293 _M_last_overflowed = false;
297 template<typename _CharT, typename _Traits>
298 typename basic_filebuf<_CharT, _Traits>::int_type
299 basic_filebuf<_CharT, _Traits>::
300 pbackfail(int_type __i)
302 int_type __ret = traits_type::eof();
303 const bool __testin = this->_M_mode & ios_base::in;
307 // Remember whether the pback buffer is active, otherwise below
308 // we may try to store in it a second char (libstdc++/9761).
309 const bool __testpb = this->_M_pback_init;
310 const bool __testeof = traits_type::eq_int_type(__i, __ret);
313 if (this->_M_in_beg < this->_M_in_cur)
316 __tmp = traits_type::to_int_type(*this->_M_in_cur);
318 else if (this->seekoff(-1, ios_base::cur) >= 0)
320 __tmp = this->underflow();
321 if (traits_type::eq_int_type(__tmp, __ret))
326 // At the beginning of the buffer, need to make a
327 // putback position available. But the seek may fail
328 // (f.i., at the beginning of a file, see
329 // libstdc++/9439) and in that case we return
330 // traits_type::eof().
334 // Try to put back __i into input sequence in one of three ways.
335 // Order these tests done in is unspecified by the standard.
336 if (!__testeof && traits_type::eq_int_type(__i, __tmp))
339 __ret = traits_type::not_eof(__i);
343 *this->_M_in_cur = traits_type::to_char_type(__i);
347 _M_last_overflowed = false;
351 template<typename _CharT, typename _Traits>
352 typename basic_filebuf<_CharT, _Traits>::int_type
353 basic_filebuf<_CharT, _Traits>::
354 overflow(int_type __c)
356 int_type __ret = traits_type::eof();
357 const bool __testeof = traits_type::eq_int_type(__c, __ret);
358 const bool __testout = this->_M_mode & ios_base::out;
362 if (this->_M_out_beg < this->_M_out_lim)
364 // Need to restore current position. The position of the
365 // external byte sequence (_M_file) corresponds to
366 // _M_filepos, and we need to move it to _M_out_beg for
368 if (_M_filepos != this->_M_out_beg)
369 _M_file.seekoff(this->_M_out_beg - _M_filepos, ios_base::cur);
371 // If appropriate, append the overflow char.
373 *this->_M_out_lim++ = traits_type::to_char_type(__c);
375 // Convert pending sequence to external representation,
377 if (_M_convert_to_external(this->_M_out_beg,
378 this->_M_out_lim - this->_M_out_beg)
379 && (!__testeof || (__testeof && !_M_file.sync())))
382 __ret = traits_type::not_eof(__c);
388 char_type __conv = traits_type::to_char_type(__c);
389 if (!__testeof && _M_convert_to_external(&__conv, 1))
393 _M_last_overflowed = true;
397 template<typename _CharT, typename _Traits>
399 basic_filebuf<_CharT, _Traits>::
400 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
402 // Sizes of external and pending output.
403 streamsize __elen = 0;
404 streamsize __plen = 0;
406 if (__check_facet(_M_codecvt).always_noconv())
408 __elen += _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
413 // Worst-case number of external bytes needed.
414 int __ext_multiplier = _M_codecvt->encoding();
415 if (__ext_multiplier == -1 || __ext_multiplier == 0)
416 __ext_multiplier = sizeof(char_type);
417 streamsize __blen = __ilen * __ext_multiplier;
418 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
420 const char_type* __iend;
421 codecvt_base::result __r;
422 __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
423 __iend, __buf, __buf + __blen, __bend);
425 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
426 __blen = __bend - __buf;
427 else if (__r == codecvt_base::noconv)
429 // Same as the always_noconv case above.
430 __buf = reinterpret_cast<char*>(__ibuf);
441 __elen += _M_file.xsputn(__buf, __blen);
445 // Try once more for partial conversions.
446 if (__r == codecvt_base::partial)
448 const char_type* __iresume = __iend;
449 streamsize __rlen = this->_M_out_lim - __iend;
450 __r = _M_codecvt->out(_M_state_cur, __iresume,
451 __iresume + __rlen, __iend, __buf,
452 __buf + __blen, __bend);
453 if (__r != codecvt_base::error)
455 __rlen = __bend - __buf;
456 __elen += _M_file.xsputn(__buf, __rlen);
461 return __elen && __elen == __plen;
464 template<typename _CharT, typename _Traits>
465 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
466 basic_filebuf<_CharT, _Traits>::
467 setbuf(char_type* __s, streamsize __n)
469 if (!this->is_open() && __s == 0 && __n == 0)
470 this->_M_buf_size = 0;
471 else if (__s && __n > 1)
473 // This is implementation-defined behavior, and assumes that
474 // an external char_type array of length (__s + __n) exists
475 // and has been pre-allocated. If this is not the case,
476 // things will quickly blow up. The length argument __n must
477 // be greater than 1 because __n - 1 positions will be used
478 // for the get and put areas, and 1 position is needed to
479 // host the overflow char of a full put area.
481 // Step 1: Destroy the current internal array.
482 _M_destroy_internal_buffer();
484 // Step 2: Use the external array.
486 this->_M_buf_size = __n;
489 _M_last_overflowed = false;
493 template<typename _CharT, typename _Traits>
494 typename basic_filebuf<_CharT, _Traits>::pos_type
495 basic_filebuf<_CharT, _Traits>::
496 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
498 pos_type __ret = pos_type(off_type(-1));
499 const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
500 const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
504 __width = _M_codecvt->encoding();
508 const bool __testfail = __off != 0 && __width <= 0;
509 if (this->is_open() && !__testfail && (__testin || __testout))
511 // Ditch any pback buffers to avoid confusion.
514 if (__way != ios_base::cur || __off != 0)
516 // Sync the internal and external streams.
517 const bool __testget = this->_M_in_beg < this->_M_in_end;
518 const bool __testput = this->_M_out_beg < this->_M_out_lim;
519 off_type __computed_off = __width * __off;
521 if (__testput || _M_last_overflowed)
523 // Part one: update the output sequence.
526 // Part two: output unshift sequence.
529 else if (__testget && __way == ios_base::cur)
530 __computed_off += this->_M_in_cur - _M_filepos;
532 // Return pos_type(off_type(-1)) in case of failure.
533 __ret = _M_file.seekoff(__computed_off, __way, __mode);
538 // NB: Need to do this in case _M_file in indeterminate
539 // state, ie _M_file._offset == -1
540 pos_type __tmp = _M_file.seekoff(__off, ios_base::cur, __mode);
545 __ret += std::max(this->_M_out_cur, this->_M_in_cur)
550 _M_last_overflowed = false;
554 template<typename _CharT, typename _Traits>
555 typename basic_filebuf<_CharT, _Traits>::pos_type
556 basic_filebuf<_CharT, _Traits>::
557 seekpos(pos_type __pos, ios_base::openmode __mode)
559 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
560 // 171. Strange seekpos() semantics due to joint position
561 return this->seekoff(off_type(__pos), ios_base::beg, __mode);
565 template<typename _CharT, typename _Traits>
567 basic_filebuf<_CharT, _Traits>::
571 template<typename _CharT, typename _Traits>
573 basic_filebuf<_CharT, _Traits>::
574 imbue(const locale& __loc)
576 const bool __testbeg = !this->seekoff(0, ios_base::cur, this->_M_mode);
577 const bool __teststate = __check_facet(_M_codecvt).encoding() == -1;
579 if (this->_M_buf_locale != __loc
580 && (!this->is_open() || (__testbeg && !__teststate)))
582 this->_M_buf_locale = __loc;
583 if (__builtin_expect(has_facet<__codecvt_type>(__loc), true))
584 _M_codecvt = &use_facet<__codecvt_type>(__loc);
586 // NB This may require the reconversion of previously
587 // converted chars. This in turn may cause the
588 // reconstruction of the original file. YIKES!! This
589 // implementation interprets this requirement as requiring
590 // the file position be at the beginning, and a stateless
591 // encoding, or that the filebuf be closed. Opinions may differ.
593 _M_last_overflowed = false;
596 // Inhibit implicit instantiations for required instantiations,
597 // which are defined via explicit instantiations elsewhere.
598 // NB: This syntax is a GNU extension.
599 #if _GLIBCPP_EXTERN_TEMPLATE
600 extern template class basic_filebuf<char>;
601 extern template class basic_ifstream<char>;
602 extern template class basic_ofstream<char>;
603 extern template class basic_fstream<char>;
605 #ifdef _GLIBCPP_USE_WCHAR_T
606 extern template class basic_filebuf<wchar_t>;
607 extern template class basic_ifstream<wchar_t>;
608 extern template class basic_ofstream<wchar_t>;
609 extern template class basic_fstream<wchar_t>;