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 (!this->_M_buf && 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()
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()),
75 _M_buf_allocated(false), _M_last_overflowed(false)
76 { this->_M_buf_unified = true; }
78 template<typename _CharT, typename _Traits>
79 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
80 basic_filebuf<_CharT, _Traits>::
81 open(const char* __s, ios_base::openmode __mode)
83 __filebuf_type *__ret = NULL;
86 _M_file.open(__s, __mode);
89 _M_allocate_internal_buffer();
90 this->_M_mode = __mode;
92 // Setup initial position of buffer.
93 _M_set_indeterminate();
95 if ((__mode & ios_base::ate)
96 && this->seekoff(0, ios_base::end, __mode) < 0)
109 template<typename _CharT, typename _Traits>
110 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
111 basic_filebuf<_CharT, _Traits>::
114 __filebuf_type* __ret = NULL;
117 bool __testfail = false;
118 const int_type __eof = traits_type::eof();
119 const bool __testput = this->_M_out_beg < this->_M_out_lim;
122 && traits_type::eq_int_type(_M_really_overflow(__eof), __eof))
127 if (_M_last_overflowed)
130 _M_really_overflow(__eof);
134 // NB: Do this here so that re-opened filebufs will be cool...
135 this->_M_mode = ios_base::openmode(0);
136 _M_destroy_internal_buffer();
139 if (!_M_file.close())
145 _M_last_overflowed = false;
149 template<typename _CharT, typename _Traits>
151 basic_filebuf<_CharT, _Traits>::
154 streamsize __ret = -1;
155 const bool __testin = this->_M_mode & ios_base::in;
156 const locale __loc = this->getloc();
157 const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
159 const bool __sync = this->_M_buf_size <= 1;
161 if (__testin && this->is_open())
163 __ret = this->_M_in_end - this->_M_in_cur;
165 // For a stateful encoding (-1) the pending sequence might be just
166 // shift and unshift prefixes with no actual character.
167 if (__cvt.encoding() >= 0)
168 __ret += _M_file.showmanyc_helper(__sync) / __cvt.max_length();
171 _M_last_overflowed = false;
175 template<typename _CharT, typename _Traits>
176 typename basic_filebuf<_CharT, _Traits>::int_type
177 basic_filebuf<_CharT, _Traits>::
178 pbackfail(int_type __i)
180 int_type __ret = traits_type::eof();
181 const bool __testin = this->_M_mode & ios_base::in;
185 const bool __testpb = this->_M_in_beg < this->_M_in_cur;
186 char_type __c = traits_type::to_char_type(__i);
187 const bool __testeof = traits_type::eq_int_type(__i, __ret);
191 const bool __testout = this->_M_mode & ios_base::out;
192 const bool __testeq = traits_type::eq(__c, this->gptr()[-1]);
194 // Try to put back __c into input sequence in one of three ways.
195 // Order these tests done in is unspecified by the standard.
196 if (!__testeof && __testeq)
208 __ret = traits_type::not_eof(__i);
216 *this->_M_in_cur = __c;
222 // At the beginning of the buffer, need to make a
223 // putback position available.
224 // But the seek may fail (f.i., at the beginning of
225 // a file, see libstdc++/9439) and in that case
226 // we return traits_type::eof()
227 if (this->seekoff(-1, ios_base::cur) >= 0)
232 if (!traits_type::eq(__c, *this->_M_in_cur))
235 *this->_M_in_cur = __c;
240 __ret = traits_type::not_eof(__i);
244 _M_last_overflowed = false;
248 template<typename _CharT, typename _Traits>
249 typename basic_filebuf<_CharT, _Traits>::int_type
250 basic_filebuf<_CharT, _Traits>::
251 overflow(int_type __c)
253 int_type __ret = traits_type::eof();
254 const bool __testput = this->_M_out_cur < this->_M_out_end;
255 const bool __testout = this->_M_mode & ios_base::out;
259 if (traits_type::eq_int_type(__c, traits_type::eof()))
260 __ret = traits_type::not_eof(__c);
263 *this->_M_out_cur = traits_type::to_char_type(__c);
265 __ret = traits_type::not_eof(__c);
268 __ret = this->_M_really_overflow(__c);
271 _M_last_overflowed = false; // Set in _M_really_overflow, below.
275 template<typename _CharT, typename _Traits>
277 basic_filebuf<_CharT, _Traits>::
278 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen,
279 streamsize& __elen, streamsize& __plen)
281 const locale __loc = this->getloc();
282 const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
284 const bool __sync = this->_M_buf_size <= 1;
286 if (__cvt.always_noconv() && __ilen)
289 _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen, __sync);
294 // Worst-case number of external bytes needed.
295 int __ext_multiplier = __cvt.encoding();
296 if (__ext_multiplier == -1 || __ext_multiplier == 0)
297 __ext_multiplier = sizeof(char_type);
298 streamsize __blen = __ilen * __ext_multiplier;
299 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
301 const char_type* __iend;
302 __res_type __r = __cvt.out(_M_state_cur, __ibuf, __ibuf + __ilen,
303 __iend, __buf, __buf + __blen, __bend);
305 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
306 __blen = __bend - __buf;
307 // Similarly to the always_noconv case above.
308 else if (__r == codecvt_base::noconv)
310 __buf = reinterpret_cast<char*>(__ibuf);
319 __elen += _M_file.xsputn(__buf, __blen, __sync);
323 // Try once more for partial conversions.
324 if (__r == codecvt_base::partial)
326 const char_type* __iresume = __iend;
327 streamsize __rlen = this->_M_out_lim - __iend;
328 __r = __cvt.out(_M_state_cur, __iresume, __iresume + __rlen,
329 __iend, __buf, __buf + __blen, __bend);
330 if (__r != codecvt_base::error)
331 __rlen = __bend - __buf;
335 // Signal to the caller (_M_really_overflow) that
336 // codecvt::out eventually failed.
341 __elen += _M_file.xsputn(__buf, __rlen, __sync);
348 template<typename _CharT, typename _Traits>
349 typename basic_filebuf<_CharT, _Traits>::int_type
350 basic_filebuf<_CharT, _Traits>::
351 _M_really_overflow(int_type __c)
353 int_type __ret = traits_type::eof();
354 const bool __testput = this->_M_out_beg < this->_M_out_lim;
355 const bool __testunbuffered = _M_file.is_open() && !this->_M_buf_size;
357 const bool __sync = this->_M_buf_size <= 1;
359 if (__testput || __testunbuffered)
361 // Sizes of external and pending output.
362 streamsize __elen = 0;
363 streamsize __plen = 0;
365 // Need to restore current position. The position of the external
366 // byte sequence (_M_file) corresponds to _M_filepos, and we need
367 // to move it to _M_out_beg for the write.
368 if (_M_filepos && _M_filepos != this->_M_out_beg)
370 off_type __off = this->_M_out_beg - _M_filepos;
371 _M_file.seekoff(__off, ios_base::cur, __sync);
374 // Convert internal buffer to external representation, output.
375 // NB: In the unbuffered case, no internal buffer exists.
376 if (!__testunbuffered)
377 _M_convert_to_external(this->_M_out_beg,
378 this->_M_out_lim - this->_M_out_beg,
381 // Checks for codecvt.out failures and _M_file.xsputn failures,
382 // respectively, inside _M_convert_to_external.
383 if (__testunbuffered || (__elen && __elen == __plen))
385 // Convert pending sequence to external representation, output.
386 // If eof, then just attempt sync.
387 if (!traits_type::eq_int_type(__c, traits_type::eof()))
389 char_type __pending = traits_type::to_char_type(__c);
390 _M_convert_to_external(&__pending, 1, __elen, __plen);
392 // User code must flush when switching modes (thus don't sync).
393 if (__elen == __plen && __elen)
395 _M_set_indeterminate();
396 __ret = traits_type::not_eof(__c);
399 else if (!_M_file.sync())
401 _M_set_indeterminate();
402 __ret = traits_type::not_eof(__c);
406 _M_last_overflowed = true;
410 template<typename _CharT, typename _Traits>
411 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
412 basic_filebuf<_CharT, _Traits>::
413 setbuf(char_type* __s, streamsize __n)
415 if (!this->is_open() && __s == 0 && __n == 0)
416 this->_M_buf_size = 0;
419 // This is implementation-defined behavior, and assumes
420 // that an external char_type array of length (__s + __n)
421 // exists and has been pre-allocated. If this is not the
422 // case, things will quickly blow up.
424 // Step 1: Destroy the current internal array.
425 _M_destroy_internal_buffer();
427 // Step 2: Use the external array.
429 this->_M_buf_size = __n;
430 _M_set_indeterminate();
432 _M_last_overflowed = false;
436 template<typename _CharT, typename _Traits>
437 typename basic_filebuf<_CharT, _Traits>::pos_type
438 basic_filebuf<_CharT, _Traits>::
439 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
441 pos_type __ret = pos_type(off_type(-1));
442 const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
443 const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
445 const bool __sync = this->_M_buf_size <= 1;
447 // Should probably do has_facet checks here.
448 int __width = use_facet<__codecvt_type>(this->_M_buf_locale).encoding();
451 const bool __testfail = __off != 0 && __width <= 0;
453 if (this->is_open() && !__testfail && (__testin || __testout))
455 // Ditch any pback buffers to avoid confusion.
458 if (__way != ios_base::cur || __off != 0)
460 off_type __computed_off = __width * __off;
462 const bool __testget = this->_M_in_beg < this->_M_in_end;
463 const bool __testput = this->_M_out_beg < this->_M_out_lim;
464 // Sync the internal and external streams.
466 if (__testput || _M_last_overflowed)
468 // Part one: update the output sequence.
470 // Part two: output unshift sequence.
474 else if (__testget && __way == ios_base::cur)
475 __computed_off += this->_M_in_cur - _M_filepos;
477 // Return pos_type(off_type(-1)) in case of failure.
478 __ret = _M_file.seekoff(__computed_off, __way, __sync, __mode);
479 _M_set_indeterminate();
481 // NB: Need to do this in case _M_file in indeterminate
482 // state, ie _M_file._offset == -1
486 _M_file.seekoff(__off, ios_base::cur,
493 std::max(this->_M_out_cur, this->_M_in_cur) - _M_filepos;
497 _M_last_overflowed = false;
501 template<typename _CharT, typename _Traits>
502 typename basic_filebuf<_CharT, _Traits>::pos_type
503 basic_filebuf<_CharT, _Traits>::
504 seekpos(pos_type __pos, ios_base::openmode __mode)
506 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
507 // 171. Strange seekpos() semantics due to joint position
508 return this->seekoff(off_type(__pos), ios_base::beg, __mode);
512 template<typename _CharT, typename _Traits>
514 basic_filebuf<_CharT, _Traits>::
518 template<typename _CharT, typename _Traits>
520 basic_filebuf<_CharT, _Traits>::
521 imbue(const locale& __loc)
523 const bool __testbeg = gptr() == eback() && pptr() == pbase();
525 if (__testbeg && this->_M_buf_locale != __loc)
526 this->_M_buf_locale = __loc;
528 // NB this may require the reconversion of previously
529 // converted chars. This in turn may cause the reconstruction
530 // of the original file. YIKES!!
531 // XXX The part in the above comment is not done.
532 _M_last_overflowed = false;
535 // Inhibit implicit instantiations for required instantiations,
536 // which are defined via explicit instantiations elsewhere.
537 // NB: This syntax is a GNU extension.
538 #if _GLIBCPP_EXTERN_TEMPLATE
539 extern template class basic_filebuf<char>;
540 extern template class basic_ifstream<char>;
541 extern template class basic_ofstream<char>;
542 extern template class basic_fstream<char>;
544 #ifdef _GLIBCPP_USE_WCHAR_T
545 extern template class basic_filebuf<wchar_t>;
546 extern template class basic_ifstream<wchar_t>;
547 extern template class basic_ofstream<wchar_t>;
548 extern template class basic_fstream<wchar_t>;