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_opt)
49 this->_M_buf_size = this->_M_buf_size_opt;
51 // Allocate internal buffer...
52 this->_M_buf = new char_type[this->_M_buf_size];
53 // ... and consistently set the end of buffer pointer.
54 this->_M_out_end = this->_M_buf + 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()
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 { this->_M_buf_unified = true; }
82 template<typename _CharT, typename _Traits>
83 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
84 basic_filebuf<_CharT, _Traits>::
85 open(const char* __s, ios_base::openmode __mode)
87 __filebuf_type *__ret = NULL;
90 _M_file.open(__s, __mode);
93 _M_allocate_internal_buffer();
94 this->_M_mode = __mode;
96 // Setup initial position of buffer.
97 _M_set_indeterminate();
99 // Set input buffer to something real.
100 // NB: Must open in non-blocking way to do this, or must
101 // set the initial position in a different manner than
103 if (__mode & ios_base::in && _M_buf_allocated)
106 if ((__mode & ios_base::ate)
107 && this->seekoff(0, ios_base::end, __mode) < 0)
120 template<typename _CharT, typename _Traits>
121 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
122 basic_filebuf<_CharT, _Traits>::
125 __filebuf_type *__ret = NULL;
128 const int_type __eof = traits_type::eof();
129 bool __testput = this->_M_out_cur
130 && this->_M_out_beg < this->_M_out_lim;
132 && traits_type::eq_int_type(_M_really_overflow(__eof), __eof))
135 // NB: Do this here so that re-opened filebufs will be cool...
136 this->_M_mode = ios_base::openmode(0);
137 _M_destroy_internal_buffer();
142 if (_M_last_overflowed)
145 _M_really_overflow(__eof);
153 _M_last_overflowed = false;
157 template<typename _CharT, typename _Traits>
159 basic_filebuf<_CharT, _Traits>::
162 streamsize __ret = -1;
163 bool __testin = this->_M_mode & ios_base::in;
165 if (__testin && this->is_open())
166 __ret = this->_M_in_end - this->_M_in_cur;
167 _M_last_overflowed = false;
171 template<typename _CharT, typename _Traits>
172 typename basic_filebuf<_CharT, _Traits>::int_type
173 basic_filebuf<_CharT, _Traits>::
174 pbackfail(int_type __i)
176 int_type __ret = traits_type::eof();
177 bool __testin = this->_M_mode & ios_base::in;
181 bool __testpb = this->_M_in_beg < this->_M_in_cur;
182 char_type __c = traits_type::to_char_type(__i);
183 bool __testeof = traits_type::eq_int_type(__i, __ret);
187 bool __testout = this->_M_mode & ios_base::out;
188 bool __testeq = traits_type::eq(__c, this->gptr()[-1]);
190 // Try to put back __c into input sequence in one of three ways.
191 // Order these tests done in is unspecified by the standard.
192 if (!__testeof && __testeq)
204 __ret = traits_type::not_eof(__i);
212 *this->_M_in_cur = __c;
218 // At the beginning of the buffer, need to make a
219 // putback position available.
220 // But the seek may fail (f.i., at the beginning of
221 // a file, see libstdc++/9439) and in that case
222 // we return traits_type::eof()
223 if (this->seekoff(-1, ios_base::cur) >= 0)
228 if (!traits_type::eq(__c, *this->_M_in_cur))
231 *this->_M_in_cur = __c;
236 __ret = traits_type::not_eof(__i);
240 _M_last_overflowed = false;
244 template<typename _CharT, typename _Traits>
245 typename basic_filebuf<_CharT, _Traits>::int_type
246 basic_filebuf<_CharT, _Traits>::
247 overflow(int_type __c)
249 int_type __ret = traits_type::eof();
250 bool __testput = _M_out_buf_size();
251 bool __testout = this->_M_mode & ios_base::out;
257 *this->_M_out_cur = traits_type::to_char_type(__c);
259 __ret = traits_type::not_eof(__c);
262 __ret = this->_M_really_overflow(__c);
265 _M_last_overflowed = false; // Set in _M_really_overflow, below.
269 template<typename _CharT, typename _Traits>
271 basic_filebuf<_CharT, _Traits>::
272 _M_convert_to_external(_CharT* __ibuf, streamsize __ilen,
273 streamsize& __elen, streamsize& __plen)
275 const locale __loc = this->getloc();
276 const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
278 if (__cvt.always_noconv() && __ilen)
280 __elen += _M_file.xsputn(reinterpret_cast<char*>(__ibuf), __ilen);
285 // Worst-case number of external bytes needed.
286 int __ext_multiplier = __cvt.encoding();
287 if (__ext_multiplier == -1 || __ext_multiplier == 0)
288 __ext_multiplier = sizeof(char_type);
289 streamsize __blen = __ilen * __ext_multiplier;
290 char* __buf = static_cast<char*>(__builtin_alloca(__blen));
292 const char_type* __iend;
293 __res_type __r = __cvt.out(_M_state_cur, __ibuf, __ibuf + __ilen,
294 __iend, __buf, __buf + __blen, __bend);
296 if (__r == codecvt_base::ok || __r == codecvt_base::partial)
297 __blen = __bend - __buf;
298 // Similarly to the always_noconv case above.
299 else if (__r == codecvt_base::noconv)
301 __buf = reinterpret_cast<char*>(__ibuf);
310 __elen += _M_file.xsputn(__buf, __blen);
314 // Try once more for partial conversions.
315 if (__r == codecvt_base::partial)
317 const char_type* __iresume = __iend;
318 streamsize __rlen = this->_M_out_lim - __iend;
319 __r = __cvt.out(_M_state_cur, __iresume, __iresume + __rlen,
320 __iend, __buf, __buf + __blen, __bend);
321 if (__r != codecvt_base::error)
322 __rlen = __bend - __buf;
326 // Signal to the caller (_M_really_overflow) that
327 // codecvt::out eventually failed.
332 __elen += _M_file.xsputn(__buf, __rlen);
339 template<typename _CharT, typename _Traits>
340 typename basic_filebuf<_CharT, _Traits>::int_type
341 basic_filebuf<_CharT, _Traits>::
342 _M_really_overflow(int_type __c)
344 int_type __ret = traits_type::eof();
345 bool __testput = this->_M_out_cur && this->_M_out_beg < this->_M_out_lim;
346 bool __testunbuffered = _M_file.is_open() && !this->_M_buf_size_opt;
348 if (__testput || __testunbuffered)
350 // Sizes of external and pending output.
351 streamsize __elen = 0;
352 streamsize __plen = 0;
354 // Need to restore current position. The position of the external
355 // byte sequence (_M_file) corresponds to _M_filepos, and we need
356 // to move it to _M_out_beg for the write.
357 if (_M_filepos && _M_filepos != this->_M_out_beg)
359 off_type __off = this->_M_out_beg - _M_filepos;
360 _M_file.seekoff(__off, ios_base::cur);
363 // Convert internal buffer to external representation, output.
364 // NB: In the unbuffered case, no internal buffer exists.
365 if (!__testunbuffered)
366 _M_convert_to_external(this->_M_out_beg,
367 this->_M_out_lim - this->_M_out_beg,
370 // Checks for codecvt.out failures and _M_file.xsputn failures,
371 // respectively, inside _M_convert_to_external.
372 if (__testunbuffered || (__elen && __elen == __plen))
374 // Convert pending sequence to external representation, output.
375 // If eof, then just attempt sync.
376 if (!traits_type::eq_int_type(__c, traits_type::eof()))
378 char_type __pending = traits_type::to_char_type(__c);
379 _M_convert_to_external(&__pending, 1, __elen, __plen);
381 // User code must flush when switching modes (thus don't sync).
382 if (__elen == __plen && __elen)
384 _M_set_indeterminate();
385 __ret = traits_type::not_eof(__c);
388 else if (!_M_file.sync())
390 _M_set_indeterminate();
391 __ret = traits_type::not_eof(__c);
395 _M_last_overflowed = true;
399 template<typename _CharT, typename _Traits>
400 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
401 basic_filebuf<_CharT, _Traits>::
402 setbuf(char_type* __s, streamsize __n)
404 if (!this->is_open() && __s == 0 && __n == 0)
405 this->_M_buf_size_opt = 0;
408 // This is implementation-defined behavior, and assumes
409 // that an external char_type array of length (__s + __n)
410 // exists and has been pre-allocated. If this is not the
411 // case, things will quickly blow up.
413 // Step 1: Destroy the current internal array.
414 _M_destroy_internal_buffer();
416 // Step 2: Use the external array.
418 this->_M_buf_size_opt = this->_M_buf_size = __n;
419 // Consistently set the end of buffer pointer.
420 this->_M_out_end = this->_M_buf + this->_M_buf_size;
421 _M_set_indeterminate();
423 _M_last_overflowed = false;
427 template<typename _CharT, typename _Traits>
428 typename basic_filebuf<_CharT, _Traits>::pos_type
429 basic_filebuf<_CharT, _Traits>::
430 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
432 pos_type __ret = pos_type(off_type(-1));
433 bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
434 bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
436 // Should probably do has_facet checks here.
437 int __width = use_facet<__codecvt_type>(this->_M_buf_locale).encoding();
440 bool __testfail = __off != 0 && __width <= 0;
442 if (this->is_open() && !__testfail && (__testin || __testout))
444 // Ditch any pback buffers to avoid confusion.
447 if (__way != ios_base::cur || __off != 0)
449 off_type __computed_off = __width * __off;
451 bool __testget = this->_M_in_cur
452 && this->_M_in_beg < this->_M_in_end;
453 bool __testput = this->_M_out_cur
454 && this->_M_out_beg < this->_M_out_lim;
455 // Sync the internal and external streams.
457 if (__testput || _M_last_overflowed)
459 // Part one: update the output sequence.
461 // Part two: output unshift sequence.
465 else if (__testget && __way == ios_base::cur)
466 __computed_off += this->_M_in_cur - _M_filepos;
468 // Return pos_type(off_type(-1)) in case of failure.
469 __ret = _M_file.seekoff(__computed_off, __way, __mode);
470 _M_set_indeterminate();
472 // NB: Need to do this in case _M_file in indeterminate
473 // state, ie _M_file._offset == -1
477 _M_file.seekoff(__off, ios_base::cur, __mode);
483 std::max(this->_M_out_cur, this->_M_in_cur) - _M_filepos;
487 _M_last_overflowed = false;
491 template<typename _CharT, typename _Traits>
492 typename basic_filebuf<_CharT, _Traits>::pos_type
493 basic_filebuf<_CharT, _Traits>::
494 seekpos(pos_type __pos, ios_base::openmode __mode)
496 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
497 // 171. Strange seekpos() semantics due to joint position
498 return this->seekoff(off_type(__pos), ios_base::beg, __mode);
502 template<typename _CharT, typename _Traits>
504 basic_filebuf<_CharT, _Traits>::
508 template<typename _CharT, typename _Traits>
510 basic_filebuf<_CharT, _Traits>::
511 imbue(const locale& __loc)
513 bool __testbeg = gptr() == eback() && pptr() == pbase();
515 if (__testbeg && this->_M_buf_locale != __loc)
516 this->_M_buf_locale = __loc;
518 // NB this may require the reconversion of previously
519 // converted chars. This in turn may cause the reconstruction
520 // of the original file. YIKES!!
521 // XXX The part in the above comment is not done.
522 _M_last_overflowed = false;
525 // Inhibit implicit instantiations for required instantiations,
526 // which are defined via explicit instantiations elsewhere.
527 // NB: This syntax is a GNU extension.
528 #if _GLIBCPP_EXTERN_TEMPLATE
529 extern template class basic_filebuf<char>;
530 extern template class basic_ifstream<char>;
531 extern template class basic_ofstream<char>;
532 extern template class basic_fstream<char>;
534 #ifdef _GLIBCPP_USE_WCHAR_T
535 extern template class basic_filebuf<wchar_t>;
536 extern template class basic_ifstream<wchar_t>;
537 extern template class basic_ofstream<wchar_t>;
538 extern template class basic_fstream<wchar_t>;