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 if (_M_in_cur < _M_in_end)
225 __ret = _M_in_end - _M_in_cur;
229 _M_last_overflowed = false;
233 template<typename _CharT, typename _Traits>
234 typename basic_filebuf<_CharT, _Traits>::int_type
235 basic_filebuf<_CharT, _Traits>::
238 int_type __ret = traits_type::eof();
239 bool __testin = _M_mode & ios_base::in;
240 bool __testout = _M_mode & ios_base::out;
242 // XXX Should re-enable codecvt bits disabled after 2.90.8.
245 // Check for pback madness, and if so swich back to the
246 // normal buffers and jet outta here before expensive
251 if (_M_in_cur < _M_in_end)
252 return traits_type::to_int_type(*_M_in_cur);
255 bool __testget = _M_in_cur && _M_in_beg < _M_in_cur;
256 bool __testinit = _M_is_indeterminate();
257 // Sync internal and external buffers.
258 // NB: __testget -> __testput as _M_buf_unified here.
262 _M_really_overflow();
263 #if _GLIBCPP_AVOID_FSEEK
264 else if ((_M_in_cur - _M_in_beg) == 1)
268 _M_file->seekoff(_M_in_cur - _M_in_beg,
269 ios_base::cur, ios_base::in);
272 if (__testinit || __testget)
274 // Assume buffered case, need to refill internal buffers.
275 streamsize __size = _M_file->xsgetn(_M_in_beg, _M_buf_size);
278 _M_set_determinate(__size);
280 _M_out_cur = _M_in_cur;
281 __ret = traits_type::to_int_type(*_M_in_cur);
282 #if _GLIBCPP_AVOID_FSEEK
284 _M_file->sys_ungetc(*_M_in_cur);
288 streamoff __p = _M_file->seekoff(0 - __size, ios_base::cur,
292 // XXX Something is wrong, do error checking.
294 #if _GLIBCPP_AVOID_FSEEK
300 _M_last_overflowed = false;
304 template<typename _CharT, typename _Traits>
305 typename basic_filebuf<_CharT, _Traits>::int_type
306 basic_filebuf<_CharT, _Traits>::
307 pbackfail(int_type __i)
309 int_type __ret = traits_type::eof();
310 bool __testin = _M_mode & ios_base::in;
314 bool __testpb = _M_in_beg < _M_in_cur;
315 char_type __c = traits_type::to_char_type(__i);
316 bool __testeof = traits_type::eq_int_type(__i, __ret);
320 bool __testout = _M_mode & ios_base::out;
321 bool __testeq = traits_type::eq(__c, this->gptr()[-1]);
323 // Try to put back __c into input sequence in one of three ways.
324 // Order these tests done in is unspecified by the standard.
325 if (!__testeof && __testeq)
337 __ret = traits_type::not_eof(__i);
351 // At the beginning of the buffer, need to make a
352 // putback position available.
353 this->seekoff(-1, ios_base::cur);
357 if (!traits_type::eq(__c, *_M_in_cur))
365 __ret = traits_type::not_eof(__i);
368 _M_last_overflowed = false;
372 template<typename _CharT, typename _Traits>
373 typename basic_filebuf<_CharT, _Traits>::int_type
374 basic_filebuf<_CharT, _Traits>::
375 overflow(int_type __c)
377 int_type __ret = traits_type::eof();
378 bool __testput = _M_out_cur && _M_out_cur < _M_buf + _M_buf_size;
379 bool __testout = _M_mode & ios_base::out;
385 *_M_out_cur = traits_type::to_char_type(__c);
387 __ret = traits_type::not_eof(__c);
390 __ret = this->_M_really_overflow(__c);
393 _M_last_overflowed = false; // Set in _M_really_overflow, below.
397 template<typename _CharT, typename _Traits>
398 typename basic_filebuf<_CharT, _Traits>::int_type
399 basic_filebuf<_CharT, _Traits>::
400 _M_really_overflow(int_type __c)
402 int_type __ret = traits_type::eof();
403 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
404 bool __testunbuffered = _M_file && !_M_buf_size;
406 if (__testput || __testunbuffered)
409 int __plen = _M_out_end - _M_out_beg;
410 streamsize __len = 0;
413 __len = _M_file->xsputn(_M_out_beg, __plen);
415 if (__c !=traits_type::eof())
417 char_type __pending = traits_type::to_char_type(__c);
418 __len += _M_file->xsputn(&__pending, 1);
422 // NB: Need this so that external byte sequence reflects
427 _M_set_indeterminate();
428 __ret = traits_type::not_eof(__c);
431 // Part one: Allocate temporary conversion buffer on
432 // stack. Convert internal buffer plus __c (ie,
433 // "pending sequence") to temporary conversion buffer.
434 int __plen = _M_out_end - _M_out_beg;
435 char_type* __pbuf = static_cast<char_type*>(__builtin_alloca(sizeof(char_type) * __plen + 1));
436 traits_type::copy(__pbuf, this->pbase(), __plen);
439 __pbuf[__plen] = traits_type::to_char_type(__c);
444 char* __conv_buf = static_cast<char*>(__builtin_alloca(__plen));
446 _M_state_beg = _M_state_cur;
448 __res_type __r = _M_fcvt->out(_M_state_cur,
449 __pbuf, __pbuf + __plen,
450 const_cast<const char_type*&>(__pend),
451 __conv_buf, __conv_buf + __plen,
454 // Part two: (Re)spill converted "pending sequence"
455 // contents (now in temporary conversion buffer) to
456 // external buffer (_M_file->_IO_*) using
457 // _M_file->sys_write(), and do error (minimal) checking.
458 if (__r != codecvt_base::error)
460 streamsize __len = _M_file->xsputn(__conv_buf, __plen);
461 // NB: Need this so that external byte sequence reflects
466 _M_set_indeterminate();
467 __ret = traits_type::not_eof(__c);
472 _M_last_overflowed = true;
476 template<typename _CharT, typename _Traits>
477 typename basic_filebuf<_CharT, _Traits>::__streambuf_type*
478 basic_filebuf<_CharT, _Traits>::
479 setbuf(char_type* __s, streamsize __n)
481 if (!this->is_open() && __s == 0 && __n == 0)
485 // This is implementation-defined behavior, and assumes
486 // that an external char_type array of length (__s + __n)
487 // exists and has been pre-allocated. If this is not the
488 // case, things will quickly blow up.
489 // Step 1: Destroy the current internal array.
490 _M_destroy_internal_buffer();
492 // Step 2: Use the external array.
494 _M_buf_size_opt = _M_buf_size = __n;
495 _M_set_indeterminate();
497 // Step 3: Make sure a pback buffer is allocated.
498 _M_allocate_pback_buffer();
500 _M_last_overflowed = false;
504 template<typename _CharT, typename _Traits>
505 typename basic_filebuf<_CharT, _Traits>::pos_type
506 basic_filebuf<_CharT, _Traits>::
507 seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
509 pos_type __ret = pos_type(off_type(-1));
510 bool __testopen = this->is_open();
511 bool __testin = __mode & ios_base::in && _M_mode & ios_base::in;
512 bool __testout = __mode & ios_base::out && _M_mode & ios_base::out;
514 // Should probably do has_facet checks here.
515 int __width = use_facet<__codecvt_type>(_M_buf_locale).encoding();
518 bool __testfail = __off != 0 && __width <= 0;
520 if (__testopen && !__testfail && (__testin || __testout))
522 // Ditch any pback buffers to avoid confusion.
525 if (__way != ios_base::cur || __off != 0)
527 off_type __computed_off = __width * __off;
529 bool __testget = _M_in_cur && _M_in_beg < _M_in_end;
530 bool __testput = _M_out_cur && _M_out_beg < _M_out_end;
531 // Sync the internal and external streams.
533 if (__testput || _M_last_overflowed)
535 // Part one: update the output sequence.
537 // Part two: output unshift sequence.
541 // NB: underflow() rewinds the external buffer.
542 else if (__testget && __way == ios_base::cur)
543 __computed_off += _M_in_cur - _M_in_beg;
545 __ret = _M_file->seekoff(__computed_off, __way, __mode);
546 _M_set_indeterminate();
548 // NB: Need to do this in case _M_file in indeterminate
549 // state, ie _M_file->_offset == -1
552 __ret = _M_file->seekoff(__off, ios_base::cur, __mode);
553 __ret += max(_M_out_cur, _M_in_cur) - _M_buf;
556 _M_last_overflowed = false;
560 template<typename _CharT, typename _Traits>
561 typename basic_filebuf<_CharT, _Traits>::pos_type
562 basic_filebuf<_CharT, _Traits>::
563 seekpos(pos_type __pos, ios_base::openmode __mode)
566 off_type __off = __pos;
568 __ret = this->seekoff(__off, ios_base::beg, __mode);
570 _M_last_overflowed = false;
574 template<typename _CharT, typename _Traits>
576 basic_filebuf<_CharT, _Traits>::
580 template<typename _CharT, typename _Traits>
582 basic_filebuf<_CharT, _Traits>::
583 imbue(const locale& __loc)
585 bool __testbeg = gptr() == eback() && pptr() == pbase();
587 if (__testbeg && _M_buf_locale != __loc)
589 _M_buf_locale = __loc;
590 _M_buf_locale_init = true;
593 // NB this may require the reconversion of previously
594 // converted chars. This in turn may cause the reconstruction
595 // of the original file. YIKES!!
596 // XXX The part in the above comment is not done.
597 _M_last_overflowed = false;
602 #endif // _CPP_BITS_FSTREAM_TCC