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
36 #define _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;
74 template<typename _CharT, typename _Traits>
75 basic_filebuf<_CharT, _Traits>::
76 basic_filebuf() : __streambuf_type(), _M_file(&_M_lock),
77 _M_mode(ios_base::openmode(0)), _M_state_cur(__state_type()),
78 _M_state_beg(__state_type()), _M_buf(NULL), _M_buf_size(BUFSIZ),
79 _M_buf_allocated(false), _M_reading(false), _M_writing(false),
80 _M_last_overflowed(false), _M_pback_cur_save(0), _M_pback_end_save(0),
81 _M_pback_init(false), _M_codecvt(0), _M_ext_buf(0), _M_ext_buf_size(0),
82 _M_ext_next(0), _M_ext_end(0)
84 if (has_facet<__codecvt_type>(this->_M_buf_locale))
85 _M_codecvt = &use_facet<__codecvt_type>(this->_M_buf_locale);
88 template<typename _CharT, typename _Traits>
89 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
90 basic_filebuf<_CharT, _Traits>::
91 open(const char* __s, ios_base::openmode __mode)
93 __filebuf_type *__ret = NULL;
96 _M_file.open(__s, __mode);
99 _M_allocate_internal_buffer();
100 this->_M_mode = __mode;
102 // Setup initial buffer to 'uncommitted' mode.
108 if ((__mode & ios_base::ate)
109 && this->seekoff(0, ios_base::end, __mode) < 0)
118 template<typename _CharT, typename _Traits>
119 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
120 basic_filebuf<_CharT, _Traits>::
123 __filebuf_type* __ret = NULL;
126 bool __testfail = false;
129 if (this->pbase() < this->pptr()
130 && traits_type::eq_int_type(this->overflow(),
135 if (_M_last_overflowed)
147 // NB: Do this here so that re-opened filebufs will be cool...
148 this->_M_mode = ios_base::openmode(0);
149 this->_M_pback_init = false;
150 _M_destroy_internal_buffer();
155 if (!_M_file.close())
161 _M_last_overflowed = false;
165 template<typename _CharT, typename _Traits>
167 basic_filebuf<_CharT, _Traits>::
170 streamsize __ret = -1;
171 const bool __testin = this->_M_mode & ios_base::in;
173 if (__testin && this->is_open())
175 // For a stateful encoding (-1) the pending sequence might be just
176 // shift and unshift prefixes with no actual character.
177 __ret = this->egptr() - this->gptr();
178 if (__check_facet(_M_codecvt).encoding() >= 0)
179 __ret += _M_file.showmanyc() / _M_codecvt->max_length();
182 _M_last_overflowed = false;
186 template<typename _CharT, typename _Traits>
187 typename basic_filebuf<_CharT, _Traits>::int_type
188 basic_filebuf<_CharT, _Traits>::
191 int_type __ret = traits_type::eof();
192 const bool __testin = this->_M_mode & ios_base::in;
193 const bool __testout = this->_M_mode & ios_base::out;
195 if (__testin && !_M_writing)
197 // Check for pback madness, and if so swich back to the
198 // normal buffers and jet outta here before expensive
202 if (this->gptr() < this->egptr())
203 return traits_type::to_int_type(*this->gptr());
205 // Get and convert input sequence.
206 const size_t __buflen = this->_M_buf_size > 1
207 ? this->_M_buf_size - 1 : 1;
209 // Will be set to true if ::read() returns 0 indicating EOF.
210 bool __got_eof = false;
211 // Number of internal characters produced.
212 streamsize __ilen = 0;
213 if (__check_facet(_M_codecvt).always_noconv())
215 __ilen = _M_file.xsgetn(reinterpret_cast<char*>(this->eback()),
222 // Worst-case number of external bytes.
223 // XXX Not done encoding() == -1.
224 const int __enc = _M_codecvt->encoding();
225 streamsize __blen; // Minimum buffer size.
226 streamsize __rlen; // Number of chars to read.
228 __blen = __rlen = __buflen * __enc;
231 __blen = __buflen + _M_codecvt->max_length() - 1;
234 const streamsize __remainder = _M_ext_end - _M_ext_next;
235 __rlen = __rlen > __remainder ? __rlen - __remainder : 0;
237 // Allocate buffer if necessary and move unconverted
239 if (_M_ext_buf_size < __blen)
241 char* __buf = new char[__blen];
243 std::memcpy(__buf, _M_ext_next, __remainder);
245 delete [] _M_ext_buf;
247 _M_ext_buf_size = __blen;
249 else if (__remainder > 0)
250 std::memmove(_M_ext_buf, _M_ext_next, __remainder);
252 _M_ext_next = _M_ext_buf;
253 _M_ext_end = _M_ext_buf + __remainder;
260 // This may fail if the return value of
261 // codecvt::max_length() is bogus.
262 if (_M_ext_end - _M_ext_buf + __rlen > _M_ext_buf_size)
264 streamsize __elen = _M_file.xsgetn(_M_ext_end, __rlen);
267 _M_ext_end += __elen;
271 codecvt_base::result __r;
272 __r = _M_codecvt->in(_M_state_cur, _M_ext_next,
273 _M_ext_end, _M_ext_next, this->eback(),
274 this->eback() + __buflen, __iend);
275 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
276 __ilen = __iend - this->eback();
277 else if (__r == codecvt_base::noconv)
279 size_t __avail = _M_ext_end - _M_ext_buf;
280 __ilen = std::min(__avail, __buflen);
281 traits_type::copy(this->eback(),
282 reinterpret_cast<char_type*>(_M_ext_buf),
284 _M_ext_next = _M_ext_buf + __ilen;
293 while (!__got_eof && __ilen == 0);
298 _M_set_buffer(__ilen);
300 __ret = traits_type::to_int_type(*this->gptr());
304 // If the actual end of file is reached, set 'uncommitted'
305 // mode, thus allowing an immediate write without an
311 _M_last_overflowed = false;
315 template<typename _CharT, typename _Traits>
316 typename basic_filebuf<_CharT, _Traits>::int_type
317 basic_filebuf<_CharT, _Traits>::
318 pbackfail(int_type __i)
320 int_type __ret = traits_type::eof();
321 const bool __testin = this->_M_mode & ios_base::in;
323 if (__testin && !_M_writing)
325 // Remember whether the pback buffer is active, otherwise below
326 // we may try to store in it a second char (libstdc++/9761).
327 const bool __testpb = this->_M_pback_init;
328 const bool __testeof = traits_type::eq_int_type(__i, __ret);
331 if (this->eback() < this->gptr())
334 __tmp = traits_type::to_int_type(*this->gptr());
336 else if (this->seekoff(-1, ios_base::cur) >= 0)
338 __tmp = this->underflow();
339 if (traits_type::eq_int_type(__tmp, __ret))
344 // At the beginning of the buffer, need to make a
345 // putback position available. But the seek may fail
346 // (f.i., at the beginning of a file, see
347 // libstdc++/9439) and in that case we return
348 // traits_type::eof().
352 // Try to put back __i into input sequence in one of three ways.
353 // Order these tests done in is unspecified by the standard.
354 if (!__testeof && traits_type::eq_int_type(__i, __tmp))
357 __ret = traits_type::not_eof(__i);
362 *this->gptr() = traits_type::to_char_type(__i);
366 _M_last_overflowed = false;
370 template<typename _CharT, typename _Traits>
371 typename basic_filebuf<_CharT, _Traits>::int_type
372 basic_filebuf<_CharT, _Traits>::
373 overflow(int_type __c)
375 int_type __ret = traits_type::eof();
376 const bool __testeof = traits_type::eq_int_type(__c, __ret);
377 const bool __testout = this->_M_mode & ios_base::out;
379 if (__testout && !_M_reading)
381 if (this->pbase() < this->pptr())
383 // If appropriate, append the overflow char.
386 *this->pptr() = traits_type::to_char_type(__c);
390 // Convert pending sequence to external representation,
392 if (_M_convert_to_external(this->pbase(),
393 this->pptr() - this->pbase())
394 && (!__testeof || (__testeof && !_M_file.sync())))
397 __ret = traits_type::not_eof(__c);
400 else if (this->_M_buf_size > 1)
402 // Overflow in 'uncommitted' mode: set _M_writing, set
403 // the buffer to the initial 'write' mode, and put __c
409 *this->pptr() = traits_type::to_char_type(__c);
412 __ret = traits_type::not_eof(__c);
417 char_type __conv = traits_type::to_char_type(__c);
418 if (__testeof || _M_convert_to_external(&__conv, 1))
421 __ret = traits_type::not_eof(__c);
425 _M_last_overflowed = true;
429 template<typename _CharT, typename _Traits>
431 basic_filebuf<_CharT, _Traits>::
432 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen)
434 // Sizes of external and pending output.
435 streamsize __elen = 0;
436 streamsize __plen = 0;
438 if (__check_facet(_M_codecvt).always_noconv())
440 __elen += _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
445 // Worst-case number of external bytes needed.
446 // XXX Not done encoding() == -1.
447 streamsize __blen = __ilen * _M_codecvt->max_length();
448 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
451 const char_type* __iend;
452 codecvt_base::result __r;
453 __r = _M_codecvt->out(_M_state_cur, __ibuf, __ibuf + __ilen,
454 __iend, __buf, __buf + __blen, __bend);
456 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
457 __blen = __bend - __buf;
458 else if (__r == codecvt_base::noconv)
460 // Same as the always_noconv case above.
461 __buf = reinterpret_cast<char*>(__ibuf);
472 __elen += _M_file.xsputn(__buf, __blen);
476 // Try once more for partial conversions.
477 if (__r == codecvt_base::partial)
479 const char_type* __iresume = __iend;
480 streamsize __rlen = this->pptr() - __iend;
481 __r = _M_codecvt->out(_M_state_cur, __iresume,
482 __iresume + __rlen, __iend, __buf,
483 __buf + __blen, __bend);
484 if (__r != codecvt_base::error)
486 __rlen = __bend - __buf;
487 __elen += _M_file.xsputn(__buf, __rlen);
492 return __elen && __elen == __plen;
495 template<typename _CharT, typename _Traits>
497 basic_filebuf<_CharT, _Traits>::
498 xsputn(const _CharT* __s, streamsize __n)
500 streamsize __ret = 0;
502 // Optimization in the always_noconv() case, to be generalized in the
503 // future: when __n is sufficiently large we write directly instead of
505 const bool __testout = this->_M_mode & ios_base::out;
506 if (__testout && !_M_reading
507 && __check_facet(_M_codecvt).always_noconv())
509 // Measurement would reveal the best choice.
510 const streamsize __chunk = 1ul << 10;
511 streamsize __bufavail = this->epptr() - this->pptr();
513 // Don't mistake 'uncommitted' mode buffered with unbuffered.
514 if (!_M_writing && this->_M_buf_size > 1)
515 __bufavail = this->_M_buf_size - 1;
517 const streamsize __limit = std::min(__chunk, __bufavail);
520 const streamsize __buffill = this->pptr() - this->pbase();
521 const char* __buf = reinterpret_cast<const char*>(this->pbase());
522 __ret = _M_file.xsputn_2(__buf, __buffill,
523 reinterpret_cast<const char*>(__s), __n);
524 if (__ret == __buffill + __n)
529 if (__ret > __buffill)
535 __ret = __streambuf_type::xsputn(__s, __n);
538 __ret = __streambuf_type::xsputn(__s, __n);
543 template<typename _CharT, typename _Traits>
544 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
545 basic_filebuf<_CharT, _Traits>::
546 setbuf(char_type* __s, streamsize __n)
548 if (!this->is_open() && __s == 0 && __n == 0)
549 this->_M_buf_size = 1;
550 else if (__s && __n > 0)
552 // This is implementation-defined behavior, and assumes that
553 // an external char_type array of length __n exists and has
554 // been pre-allocated. If this is not the case, things will
555 // quickly blow up. When __n > 1, __n - 1 positions will be
556 // used for the get area, __n - 1 for the put area and 1
557 // position to host the overflow char of a full put area.
558 // When __n == 1, 1 position will be used for the get area
559 // and 0 for the put area, as in the unbuffered case above.
561 // Step 1: Destroy the current internal array.
562 _M_destroy_internal_buffer();
564 // Step 2: Use the external array.
566 this->_M_buf_size = __n;
571 _M_last_overflowed = false;
576 // According to 27.8.1.4 p11 - 13, seekoff should ignore the last
577 // argument (of type openmode).
578 template<typename _CharT, typename _Traits>
579 typename basic_filebuf<_CharT, _Traits>::pos_type
580 basic_filebuf<_CharT, _Traits>::
581 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode)
583 pos_type __ret = pos_type(off_type(-1));
587 __width = _M_codecvt->encoding();
591 const bool __testfail = __off != 0 && __width <= 0;
592 if (this->is_open() && !__testfail)
594 // Ditch any pback buffers to avoid confusion.
597 off_type __computed_off = __off * __width;
598 if (_M_reading && __way == ios_base::cur)
600 if (_M_codecvt->always_noconv())
601 __computed_off += this->gptr() - this->egptr();
604 // Calculate offset from _M_ext_buf that corresponds
606 const int __gptr_off =
607 _M_codecvt->length(_M_state_cur, _M_ext_buf, _M_ext_next,
608 this->gptr() - this->eback());
609 __computed_off += _M_ext_buf + __gptr_off - _M_ext_end;
613 __ret = _M_seek(__computed_off, __way);
615 _M_last_overflowed = false;
619 // _GLIBCXX_RESOLVE_LIB_DEFECTS
620 // 171. Strange seekpos() semantics due to joint position
621 // According to the resolution of DR 171, seekpos should ignore the last
622 // argument (of type openmode).
623 template<typename _CharT, typename _Traits>
624 typename basic_filebuf<_CharT, _Traits>::pos_type
625 basic_filebuf<_CharT, _Traits>::
626 seekpos(pos_type __pos, ios_base::openmode)
628 pos_type __ret = pos_type(off_type(-1));
632 // Ditch any pback buffers to avoid confusion.
635 __ret = _M_seek(off_type(__pos), ios_base::beg);
637 _M_last_overflowed = false;
641 template<typename _CharT, typename _Traits>
642 typename basic_filebuf<_CharT, _Traits>::pos_type
643 basic_filebuf<_CharT, _Traits>::
644 _M_seek(off_type __off, ios_base::seekdir __way)
646 if (this->pbase() < this->pptr())
648 // Part one: update the output sequence.
651 // Part two: output unshift sequence.
655 // Returns pos_type(off_type(-1)) in case of failure.
656 pos_type __ret = _M_file.seekoff(__off, __way);
660 _M_ext_next = _M_ext_end = _M_ext_buf;
665 template<typename _CharT, typename _Traits>
667 basic_filebuf<_CharT, _Traits>::
671 template<typename _CharT, typename _Traits>
673 basic_filebuf<_CharT, _Traits>::
674 imbue(const locale& __loc)
676 const bool __testbeg = !this->seekoff(0, ios_base::cur, this->_M_mode);
677 const bool __teststate = __check_facet(_M_codecvt).encoding() == -1;
679 if (this->_M_buf_locale != __loc
680 && (!this->is_open() || (__testbeg && !__teststate)))
682 this->_M_buf_locale = __loc;
683 if (__builtin_expect(has_facet<__codecvt_type>(__loc), true))
684 _M_codecvt = &use_facet<__codecvt_type>(__loc);
688 // NB This may require the reconversion of previously
689 // converted chars. This in turn may cause the
690 // reconstruction of the original file. YIKES!! This
691 // implementation interprets this requirement as requiring
692 // the file position be at the beginning, and a stateless
693 // encoding, or that the filebuf be closed. Opinions may differ.
695 _M_last_overflowed = false;
698 // Inhibit implicit instantiations for required instantiations,
699 // which are defined via explicit instantiations elsewhere.
700 // NB: This syntax is a GNU extension.
701 #if _GLIBCXX_EXTERN_TEMPLATE
702 extern template class basic_filebuf<char>;
703 extern template class basic_ifstream<char>;
704 extern template class basic_ofstream<char>;
705 extern template class basic_fstream<char>;
707 #ifdef _GLIBCXX_USE_WCHAR_T
708 extern template class basic_filebuf<wchar_t>;
709 extern template class basic_ifstream<wchar_t>;
710 extern template class basic_ofstream<wchar_t>;
711 extern template class basic_fstream<wchar_t>;