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>::_S_pback_size;
46 template<typename _CharT, typename _Traits>
48 basic_filebuf<_CharT, _Traits>::
49 _M_allocate_internal_buffer()
51 if (!this->_M_buf && this->_M_buf_size)
53 // Allocate internal buffer.
54 this->_M_buf = new char_type[this->_M_buf_size];
55 _M_buf_allocated = true;
59 // Both close and setbuf need to deallocate internal buffers, if it exists.
60 template<typename _CharT, typename _Traits>
62 basic_filebuf<_CharT, _Traits>::
63 _M_destroy_internal_buffer() throw()
67 delete [] this->_M_buf;
69 _M_buf_allocated = false;
70 this->setg(NULL, NULL, NULL);
71 this->setp(NULL, NULL);
75 template<typename _CharT, typename _Traits>
76 basic_filebuf<_CharT, _Traits>::
77 basic_filebuf() : __streambuf_type(), _M_file(&_M_lock),
78 _M_state_cur(__state_type()), _M_state_beg(__state_type()),
79 _M_buf_allocated(false), _M_last_overflowed(false),
80 _M_pback_cur_save(0), _M_pback_end_save(0), _M_pback_init(false)
81 { this->_M_buf_unified = true; }
83 template<typename _CharT, typename _Traits>
84 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
85 basic_filebuf<_CharT, _Traits>::
86 open(const char* __s, ios_base::openmode __mode)
88 __filebuf_type *__ret = NULL;
91 _M_file.open(__s, __mode);
94 _M_allocate_internal_buffer();
95 this->_M_mode = __mode;
97 // Setup initial position of buffer.
98 _M_set_indeterminate();
100 if ((__mode & ios_base::ate)
101 && this->seekoff(0, ios_base::end, __mode) < 0)
114 template<typename _CharT, typename _Traits>
115 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
116 basic_filebuf<_CharT, _Traits>::
119 __filebuf_type* __ret = NULL;
122 bool __testfail = false;
125 const int_type __eof = traits_type::eof();
126 const bool __testput = this->_M_out_beg < this->_M_out_lim;
129 && traits_type::eq_int_type(_M_really_overflow(__eof),
135 if (_M_last_overflowed)
138 _M_really_overflow(__eof);
147 // NB: Do this here so that re-opened filebufs will be cool...
148 this->_M_mode = ios_base::openmode(0);
149 _M_destroy_internal_buffer();
152 if (!_M_file.close())
158 _M_last_overflowed = false;
162 template<typename _CharT, typename _Traits>
164 basic_filebuf<_CharT, _Traits>::
167 streamsize __ret = -1;
168 const bool __testin = this->_M_mode & ios_base::in;
169 const locale __loc = this->getloc();
170 const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
171 const bool __testsync = this->_M_buf_size <= 1;
173 if (__testin && this->is_open())
175 __ret = this->_M_in_end - this->_M_in_cur;
177 // For a stateful encoding (-1) the pending sequence might be just
178 // shift and unshift prefixes with no actual character.
179 if (__cvt.encoding() >= 0)
180 __ret += _M_file.showmanyc_helper(__testsync) / __cvt.max_length();
183 _M_last_overflowed = false;
187 template<typename _CharT, typename _Traits>
188 typename basic_filebuf<_CharT, _Traits>::int_type
189 basic_filebuf<_CharT, _Traits>::
190 pbackfail(int_type __i)
192 int_type __ret = traits_type::eof();
193 const bool __testin = this->_M_mode & ios_base::in;
197 const bool __testpb = this->_M_in_beg < this->_M_in_cur;
198 char_type __c = traits_type::to_char_type(__i);
199 const bool __testeof = traits_type::eq_int_type(__i, __ret);
203 const bool __testout = this->_M_mode & ios_base::out;
204 const bool __testeq = traits_type::eq(__c, this->_M_in_cur[-1]);
206 // Try to put back __c into input sequence in one of three ways.
207 // Order these tests done in is unspecified by the standard.
208 if (!__testeof && __testeq)
220 __ret = traits_type::not_eof(__i);
228 *this->_M_in_cur = __c;
234 // At the beginning of the buffer, need to make a
235 // putback position available.
236 // But the seek may fail (f.i., at the beginning of
237 // a file, see libstdc++/9439) and in that case
238 // we return traits_type::eof()
239 if (this->seekoff(-1, ios_base::cur) >= 0)
244 if (!traits_type::eq(__c, *this->_M_in_cur))
247 *this->_M_in_cur = __c;
252 __ret = traits_type::not_eof(__i);
256 _M_last_overflowed = false;
260 template<typename _CharT, typename _Traits>
261 typename basic_filebuf<_CharT, _Traits>::int_type
262 basic_filebuf<_CharT, _Traits>::
263 overflow(int_type __c)
265 int_type __ret = traits_type::eof();
266 const bool __testput = this->_M_out_cur < this->_M_out_end;
267 const bool __testout = this->_M_mode & ios_base::out;
271 if (traits_type::eq_int_type(__c, traits_type::eof()))
272 __ret = traits_type::not_eof(__c);
275 *this->_M_out_cur = traits_type::to_char_type(__c);
277 __ret = traits_type::not_eof(__c);
280 __ret = this->_M_really_overflow(__c);
283 _M_last_overflowed = false; // Set in _M_really_overflow, below.
287 template<typename _CharT, typename _Traits>
289 basic_filebuf<_CharT, _Traits>::
290 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen,
291 streamsize& __elen, streamsize& __plen)
293 const bool __testsync = this->_M_buf_size <= 1;
294 const locale __loc = this->getloc();
295 const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
297 if (__cvt.always_noconv() && __ilen)
299 __elen += _M_file.xsputn(reinterpret_cast<char*>(__ibuf),
305 // Worst-case number of external bytes needed.
306 int __ext_multiplier = __cvt.encoding();
307 if (__ext_multiplier == -1 || __ext_multiplier == 0)
308 __ext_multiplier = sizeof(char_type);
309 streamsize __blen = __ilen * __ext_multiplier;
310 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
312 const char_type* __iend;
313 codecvt_base::result __r;
314 __r = __cvt.out(_M_state_cur, __ibuf, __ibuf + __ilen,
315 __iend, __buf, __buf + __blen, __bend);
317 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
318 __blen = __bend - __buf;
319 else if (__r == codecvt_base::noconv)
321 // Same as the always_noconv case above.
322 __buf = reinterpret_cast<char*>(__ibuf);
333 __elen += _M_file.xsputn(__buf, __blen, __testsync);
337 // Try once more for partial conversions.
338 if (__r == codecvt_base::partial)
340 const char_type* __iresume = __iend;
341 streamsize __rlen = this->_M_out_lim - __iend;
342 __r = __cvt.out(_M_state_cur, __iresume, __iresume + __rlen,
343 __iend, __buf, __buf + __blen, __bend);
344 if (__r != codecvt_base::error)
346 __rlen = __bend - __buf;
347 __elen += _M_file.xsputn(__buf, __rlen, __testsync);
354 template<typename _CharT, typename _Traits>
355 typename basic_filebuf<_CharT, _Traits>::int_type
356 basic_filebuf<_CharT, _Traits>::
357 _M_really_overflow(int_type __c)
359 int_type __ret = traits_type::eof();
360 const bool __testput = this->_M_out_beg < this->_M_out_lim;
361 const bool __testunbuffered = _M_file.is_open() && !this->_M_buf_size;
362 const bool __testsync = this->_M_buf_size <= 1;
364 if (__testput || __testunbuffered)
366 // Sizes of external and pending output.
367 streamsize __elen = 0;
368 streamsize __plen = 0;
370 // Need to restore current position. The position of the external
371 // byte sequence (_M_file) corresponds to _M_filepos, and we need
372 // to move it to _M_out_beg for the write.
373 if (_M_filepos && _M_filepos != this->_M_out_beg)
375 off_type __off = this->_M_out_beg - _M_filepos;
376 _M_file.seekoff(__off, ios_base::cur, __testsync);
379 // Convert internal buffer to external representation, output.
380 // NB: In the unbuffered case, no internal buffer exists.
381 if (!__testunbuffered)
382 _M_convert_to_external(this->_M_out_beg,
383 this->_M_out_lim - this->_M_out_beg,
386 // Checks for codecvt.out failures and _M_file.xsputn failures,
387 // respectively, inside _M_convert_to_external.
388 if (__testunbuffered || (__elen && __elen == __plen))
390 // Convert pending sequence to external representation, output.
391 // If eof, then just attempt sync.
392 if (!traits_type::eq_int_type(__c, traits_type::eof()))
394 char_type __pending = traits_type::to_char_type(__c);
395 _M_convert_to_external(&__pending, 1, __elen, __plen);
397 // User code must flush when switching modes (thus
399 if (__elen == __plen && __elen)
401 _M_set_indeterminate();
402 __ret = traits_type::not_eof(__c);
405 else if (!_M_file.sync())
407 _M_set_indeterminate();
408 __ret = traits_type::not_eof(__c);
412 _M_last_overflowed = true;
416 template<typename _CharT, typename _Traits>
417 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
418 basic_filebuf<_CharT, _Traits>::
419 setbuf(char_type* __s, streamsize __n)
421 if (!this->is_open() && __s == 0 && __n == 0)
422 this->_M_buf_size = 0;
425 // This is implementation-defined behavior, and assumes
426 // that an external char_type array of length (__s + __n)
427 // exists and has been pre-allocated. If this is not the
428 // case, things will quickly blow up.
430 // Step 1: Destroy the current internal array.
431 _M_destroy_internal_buffer();
433 // Step 2: Use the external array.
435 this->_M_buf_size = __n;
436 _M_set_indeterminate();
438 _M_last_overflowed = false;
442 template<typename _CharT, typename _Traits>
443 typename basic_filebuf<_CharT, _Traits>::pos_type
444 basic_filebuf<_CharT, _Traits>::
445 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
447 pos_type __ret = pos_type(off_type(-1));
448 const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
449 const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
450 const bool __testsync = this->_M_buf_size <= 1;
453 if (has_facet<__codecvt_type>(this->_M_buf_locale))
454 __width = use_facet<__codecvt_type>(this->_M_buf_locale).encoding();
458 const bool __testfail = __off != 0 && __width <= 0;
459 if (this->is_open() && !__testfail && (__testin || __testout))
461 // Ditch any pback buffers to avoid confusion.
464 if (__way != ios_base::cur || __off != 0)
466 off_type __computed_off = __width * __off;
468 const bool __testget = this->_M_in_beg < this->_M_in_end;
469 const bool __testput = this->_M_out_beg < this->_M_out_lim;
470 // Sync the internal and external streams.
472 if (__testput || _M_last_overflowed)
474 // Part one: update the output sequence.
476 // Part two: output unshift sequence.
480 else if (__testget && __way == ios_base::cur)
481 __computed_off += this->_M_in_cur - _M_filepos;
483 // Return pos_type(off_type(-1)) in case of failure.
484 __ret = _M_file.seekoff(__computed_off, __way, __testsync,
486 _M_set_indeterminate();
488 // NB: Need to do this in case _M_file in indeterminate
489 // state, ie _M_file._offset == -1
492 pos_type __tmp = _M_file.seekoff(__off, ios_base::cur,
498 __ret += std::max(this->_M_out_cur, this->_M_in_cur)
503 _M_last_overflowed = false;
507 template<typename _CharT, typename _Traits>
508 typename basic_filebuf<_CharT, _Traits>::pos_type
509 basic_filebuf<_CharT, _Traits>::
510 seekpos(pos_type __pos, ios_base::openmode __mode)
512 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
513 // 171. Strange seekpos() semantics due to joint position
514 return this->seekoff(off_type(__pos), ios_base::beg, __mode);
518 template<typename _CharT, typename _Traits>
520 basic_filebuf<_CharT, _Traits>::
524 template<typename _CharT, typename _Traits>
526 basic_filebuf<_CharT, _Traits>::
527 imbue(const locale& __loc)
529 const bool __testbeg = this->_M_in_cur == this->_M_in_beg
530 && this->_M_out_cur == this->_M_out_beg;
532 if (__testbeg && this->_M_buf_locale != __loc)
533 this->_M_buf_locale = __loc;
535 // NB this may require the reconversion of previously
536 // converted chars. This in turn may cause the reconstruction
537 // of the original file. YIKES!!
538 // XXX The part in the above comment is not done.
539 _M_last_overflowed = false;
542 // Inhibit implicit instantiations for required instantiations,
543 // which are defined via explicit instantiations elsewhere.
544 // NB: This syntax is a GNU extension.
545 #if _GLIBCPP_EXTERN_TEMPLATE
546 extern template class basic_filebuf<char>;
547 extern template class basic_ifstream<char>;
548 extern template class basic_ofstream<char>;
549 extern template class basic_fstream<char>;
551 #ifdef _GLIBCPP_USE_WCHAR_T
552 extern template class basic_filebuf<wchar_t>;
553 extern template class basic_ifstream<wchar_t>;
554 extern template class basic_ofstream<wchar_t>;
555 extern template class basic_fstream<wchar_t>;