1 // File based streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 // ISO C++ 14882: 27.8 File-based streams
34 #ifndef _CPP_BITS_FSTREAM_TCC
35 #define _CPP_BITS_FSTREAM_TCC 1
39 template<typename _CharT, typename _Traits>
41 basic_filebuf<_CharT, _Traits>::
46 _M_buf_unified = true; // Tie input to output for basic_filebuf.
48 { _M_file = new __file_type(&_M_lock); }
52 __throw_exception_again;
57 template<typename _CharT, typename _Traits>
59 basic_filebuf<_CharT, _Traits>::
60 _M_allocate_internal_buffer()
62 if (!_M_buf && _M_buf_size_opt)
64 _M_buf_size = _M_buf_size_opt;
66 // Allocate internal buffer.
67 try { _M_buf = new char_type[_M_buf_size]; }
71 __throw_exception_again;
73 _M_buf_allocated = true;
77 // Both close and setbuf need to deallocate internal buffers, if it exists.
78 template<typename _CharT, typename _Traits>
80 basic_filebuf<_CharT, _Traits>::
81 _M_destroy_internal_buffer()
87 _M_buf_allocated = false;
88 this->setg(NULL, NULL, NULL);
89 this->setp(NULL, NULL);
93 template<typename _CharT, typename _Traits>
95 basic_filebuf<_CharT, _Traits>::
96 _M_allocate_pback_buffer()
98 if (!_M_pback && _M_pback_size)
100 // Allocate pback buffer.
102 { _M_pback = new char_type[_M_pback_size]; }
106 __throw_exception_again;
111 template<typename _CharT, typename _Traits>
112 basic_filebuf<_CharT, _Traits>::
114 : __streambuf_type(), _M_file(NULL), _M_state_cur(__state_type()),
115 _M_state_beg(__state_type()), _M_buf_allocated(false),
116 _M_last_overflowed(false)
119 template<typename _CharT, typename _Traits>
120 basic_filebuf<_CharT, _Traits>::
121 basic_filebuf(__c_file_type* __f, ios_base::openmode __mode, int_type __s)
122 : __streambuf_type(), _M_file(NULL), _M_state_cur(__state_type()),
123 _M_state_beg(__state_type()), _M_buf_allocated(false),
124 _M_last_overflowed(false)
127 _M_file->sys_open(__f, __mode);
133 _M_buf_size_opt = __s;
134 _M_allocate_internal_buffer();
135 _M_set_indeterminate();
137 _M_allocate_pback_buffer();
141 template<typename _CharT, typename _Traits>
142 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
143 basic_filebuf<_CharT, _Traits>::
144 open(const char* __s, ios_base::openmode __mode)
146 __filebuf_type *__ret = NULL;
147 if (!this->is_open())
150 _M_file->open(__s, __mode);
153 _M_allocate_internal_buffer();
154 _M_allocate_pback_buffer();
157 // For time being, set both (in/out) sets of pointers.
158 _M_set_indeterminate();
159 if (__mode & ios_base::ate
160 && this->seekoff(0, ios_base::end, __mode) < 0)
168 template<typename _CharT, typename _Traits>
169 typename basic_filebuf<_CharT, _Traits>::__filebuf_type*
170 basic_filebuf<_CharT, _Traits>::
173 __filebuf_type *__ret = NULL;
176 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
178 _M_really_overflow(traits_type::eof());
180 // NB: Do this here so that re-opened filebufs will be cool...
185 if (_M_last_overflowed)
188 _M_really_overflow(traits_type::eof());
192 _M_mode = ios_base::openmode(0);
193 _M_destroy_internal_buffer();
203 // Can actually allocate this file as part of an open and never
204 // have it be opened.....
210 _M_last_overflowed = false;
214 template<typename _CharT, typename _Traits>
216 basic_filebuf<_CharT, _Traits>::
219 streamsize __ret = -1;
220 bool __testin = _M_mode & ios_base::in;
224 bool __testeof = false;
225 if (_M_in_cur >= _M_in_end)
226 __testeof = this->underflow() == traits_type::eof();
228 __ret = _M_in_end - _M_in_cur;
230 _M_last_overflowed = false;
234 template<typename _CharT, typename _Traits>
235 typename basic_filebuf<_CharT, _Traits>::int_type
236 basic_filebuf<_CharT, _Traits>::
239 int_type __ret = traits_type::eof();
240 bool __testin = _M_mode & ios_base::in;
241 bool __testout = _M_mode & ios_base::out;
243 // XXX Should re-enable codecvt bits disabled after 2.90.8.
246 // Check for pback madness, and if so swich back to the
247 // normal buffers and jet outta here before expensive
252 if (_M_in_cur < _M_in_end)
253 return traits_type::to_int_type(*_M_in_cur);
256 bool __testget = _M_in_cur && _M_in_beg < _M_in_cur;
257 bool __testinit = _M_is_indeterminate();
258 // Sync internal and external buffers.
259 // NB: __testget -> __testput as _M_buf_unified here.
263 _M_really_overflow();
264 #if _GLIBCPP_AVOID_FSEEK
265 else if ((_M_in_cur - _M_in_beg) == 1)
269 _M_file->seekoff(_M_in_cur - _M_in_beg,
270 ios_base::cur, ios_base::in);
273 if (__testinit || __testget)
275 // Assume buffered case, need to refill internal buffers.
276 streamsize __size = _M_file->xsgetn(_M_in_beg, _M_buf_size);
279 _M_set_determinate(__size);
281 _M_out_cur = _M_in_cur;
282 __ret = traits_type::to_int_type(*_M_in_cur);
283 #if _GLIBCPP_AVOID_FSEEK
285 _M_file->sys_ungetc(*_M_in_cur);
289 streamoff __p = _M_file->seekoff(0 - __size, ios_base::cur,
293 // XXX Something is wrong, do error checking.
295 #if _GLIBCPP_AVOID_FSEEK
301 _M_last_overflowed = false;
305 template<typename _CharT, typename _Traits>
306 typename basic_filebuf<_CharT, _Traits>::int_type
307 basic_filebuf<_CharT, _Traits>::
308 pbackfail(int_type __i)
310 int_type __ret = traits_type::eof();
311 bool __testin = _M_mode & ios_base::in;
315 bool __testpb = _M_in_beg < _M_in_cur;
316 char_type __c = traits_type::to_char_type(__i);
317 bool __testeof = traits_type::eq_int_type(__i, __ret);
321 bool __testout = _M_mode & ios_base::out;
322 bool __testeq = traits_type::eq(__c, this->gptr()[-1]);
324 // Try to put back __c into input sequence in one of three ways.
325 // Order these tests done in is unspecified by the standard.
326 if (!__testeof && __testeq)
338 __ret = traits_type::not_eof(__i);
352 // At the beginning of the buffer, need to make a
353 // putback position available.
354 this->seekoff(-1, ios_base::cur);
358 if (!traits_type::eq(__c, *_M_in_cur))
366 __ret = traits_type::not_eof(__i);
369 _M_last_overflowed = false;
373 template<typename _CharT, typename _Traits>
374 typename basic_filebuf<_CharT, _Traits>::int_type
375 basic_filebuf<_CharT, _Traits>::
376 overflow(int_type __c)
378 int_type __ret = traits_type::eof();
379 bool __testput = _M_out_cur && _M_out_cur < _M_buf + _M_buf_size;
380 bool __testout = _M_mode & ios_base::out;
386 *_M_out_cur = traits_type::to_char_type(__c);
388 __ret = traits_type::not_eof(__c);
391 __ret = this->_M_really_overflow(__c);
394 _M_last_overflowed = false; // Set in _M_really_overflow, below.
398 template<typename _CharT, typename _Traits>
399 typename basic_filebuf<_CharT, _Traits>::int_type
400 basic_filebuf<_CharT, _Traits>::
401 _M_really_overflow(int_type __c)
403 int_type __ret = traits_type::eof();
404 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
405 bool __testunbuffered = _M_file && !_M_buf_size;
407 if (__testput || __testunbuffered)
410 int __plen = _M_out_end - _M_out_beg;
411 streamsize __len = 0;
414 __len = _M_file->xsputn(_M_out_beg, __plen);
416 if (__c !=traits_type::eof())
418 char_type __pending = traits_type::to_char_type(__c);
419 __len += _M_file->xsputn(&__pending, 1);
423 // NB: Need this so that external byte sequence reflects
428 _M_set_indeterminate();
429 __ret = traits_type::not_eof(__c);
432 // Part one: Allocate temporary conversion buffer on
433 // stack. Convert internal buffer plus __c (ie,
434 // "pending sequence") to temporary conversion buffer.
435 int __plen = _M_out_end - _M_out_beg;
436 char_type* __pbuf = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __plen + 1));
437 traits_type::copy(__pbuf, this->pbase(), __plen);
440 __pbuf[__plen] = traits_type::to_char_type(__c);
445 char* __conv_buf = static_cast<char*>(__builtin_alloca(__plen));
447 _M_state_beg = _M_state_cur;
449 __res_type __r = _M_fcvt->out(_M_state_cur,
450 __pbuf, __pbuf + __plen,
451 const_cast<const char_type*&>(__pend),
452 __conv_buf, __conv_buf + __plen,
455 // Part two: (Re)spill converted "pending sequence"
456 // contents (now in temporary conversion buffer) to
457 // external buffer (_M_file->_IO_*) using
458 // _M_file->sys_write(), and do error (minimal) checking.
459 if (__r != codecvt_base::error)
461 streamsize __len = _M_file->xsputn(__conv_buf, __plen);
462 // NB: Need this so that external byte sequence reflects
467 _M_set_indeterminate();
468 __ret = traits_type::not_eof(__c);
473 _M_last_overflowed = true;
477 template<typename _CharT, typename _Traits>
478 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
479 basic_filebuf<_CharT, _Traits>::
480 setbuf(char_type* __s, streamsize __n)
482 if (!this->is_open() && __s == 0 && __n == 0)
486 // This is implementation-defined behavior, and assumes
487 // that an external char_type array of length (__s + __n)
488 // exists and has been pre-allocated. If this is not the
489 // case, things will quickly blow up.
490 // Step 1: Destroy the current internal array.
491 _M_destroy_internal_buffer();
493 // Step 2: Use the external array.
495 _M_buf_size_opt = _M_buf_size = __n;
496 _M_set_indeterminate();
498 // Step 3: Make sure a pback buffer is allocated.
499 _M_allocate_pback_buffer();
501 _M_last_overflowed = false;
505 template<typename _CharT, typename _Traits>
506 typename basic_filebuf<_CharT, _Traits>::pos_type
507 basic_filebuf<_CharT, _Traits>::
508 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
510 pos_type __ret = pos_type(off_type(-1));
511 bool __testopen = this->is_open();
512 bool __testin = __mode & ios_base::in && _M_mode & ios_base::in;
513 bool __testout = __mode & ios_base::out && _M_mode & ios_base::out;
515 // Should probably do has_facet checks here.
516 int __width = use_facet<__codecvt_type>(_M_buf_locale).encoding();
519 bool __testfail = __off != 0 && __width <= 0;
521 if (__testopen && !__testfail && (__testin || __testout))
523 // Ditch any pback buffers to avoid confusion.
526 if (__way != ios_base::cur || __off != 0)
528 off_type __computed_off = __width * __off;
530 bool __testget = _M_in_cur && _M_in_beg < _M_in_end;
531 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
532 // Sync the internal and external streams.
534 if (__testput || _M_last_overflowed)
536 // Part one: update the output sequence.
538 // Part two: output unshift sequence.
542 // NB: underflow() rewinds the external buffer.
543 else if (__testget && __way == ios_base::cur)
544 __computed_off += _M_in_cur - _M_in_beg;
546 __ret = _M_file->seekoff(__computed_off, __way, __mode);
547 _M_set_indeterminate();
549 // NB: Need to do this in case _M_file in indeterminate
550 // state, ie _M_file->_offset == -1
553 __ret = _M_file->seekoff(__off, ios_base::cur, __mode);
554 __ret += max(_M_out_cur, _M_in_cur) - _M_buf;
557 _M_last_overflowed = false;
561 template<typename _CharT, typename _Traits>
562 typename basic_filebuf<_CharT, _Traits>::pos_type
563 basic_filebuf<_CharT, _Traits>::
564 seekpos(pos_type __pos, ios_base::openmode __mode)
567 off_type __off = __pos;
569 __ret = this->seekoff(__off, ios_base::beg, __mode);
571 _M_last_overflowed = false;
575 template<typename _CharT, typename _Traits>
577 basic_filebuf<_CharT, _Traits>::
581 template<typename _CharT, typename _Traits>
583 basic_filebuf<_CharT, _Traits>::
584 imbue(const locale& __loc)
586 bool __testbeg = gptr() == eback() && pptr() == pbase();
588 if (__testbeg && _M_buf_locale != __loc)
590 _M_buf_locale = __loc;
591 _M_buf_locale_init = true;
594 // NB this may require the reconversion of previously
595 // converted chars. This in turn may cause the reconstruction
596 // of the original file. YIKES!!
597 // XXX The part in the above comment is not done.
598 _M_last_overflowed = false;
603 #endif // _CPP_BITS_FSTREAM_TCC