1 // Stream buffer classes -*- C++ -*-
3 // Copyright (C) 1997-1999 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.5 Stream buffers
34 #ifndef _CPP_BITS_STREAMBUF_TCC
35 #define _CPP_BITS_STREAMBUF_TCC 1
39 template<typename _CharT, typename _Traits>
40 basic_streambuf<_CharT, _Traits>::int_type
41 basic_streambuf<_CharT, _Traits>::
45 if (_M_in_cur && _M_in_cur < _M_in_end)
47 char_type __c = *gptr();
49 if (_M_buf_unified && _M_mode & ios_base::out)
51 __retval = traits_type::to_int_type(__c);
54 __retval = this->uflow();
58 template<typename _CharT, typename _Traits>
59 basic_streambuf<_CharT, _Traits>::int_type
60 basic_streambuf<_CharT, _Traits>::
61 sputbackc(char_type __c)
64 bool __testpos = _M_in_cur && _M_in_beg < _M_in_cur;
65 bool __testne = _M_in_cur && !traits_type::eq(__c, this->gptr()[-1]);
66 if (!__testpos || __testne)
67 __retval = pbackfail(traits_type::to_int_type(__c));
71 if (_M_buf_unified && _M_mode & ios_base::out)
73 __retval = traits_type::to_int_type(*this->gptr());
78 template<typename _CharT, typename _Traits>
79 basic_streambuf<_CharT, _Traits>::int_type
80 basic_streambuf<_CharT, _Traits>::
84 if (_M_in_cur && _M_in_beg < _M_in_cur)
87 if (_M_buf_unified && _M_mode & ios_base::out)
89 __retval = traits_type::to_int_type(*_M_in_cur);
92 __retval = this->pbackfail();
96 // Don't test against _M_buf + _M_buf_size, because _M_buf reflects
97 // allocated space, and on certain (rare but entirely legal)
98 // situations, there will be no allocated space yet the internal
99 // buffers will still be valid. (This happens if setp is used to set
100 // the internal buffer to say some externally-allocated sequence.)
101 template<typename _CharT, typename _Traits>
102 basic_streambuf<_CharT, _Traits>::int_type
103 basic_streambuf<_CharT, _Traits>::
108 if (_M_out_cur && _M_out_cur < _M_out_beg + _M_buf_size)
112 __retval = traits_type::to_int_type(__c);
115 __retval = this->overflow(traits_type::to_int_type(__c));
119 template<typename _CharT, typename _Traits>
121 basic_streambuf<_CharT, _Traits>::
122 xsgetn(char_type* __s, streamsize __n)
124 bool __testout = _M_mode & ios_base::out;
125 streamsize __retval = 0;
129 while (__retval < __n)
131 bool __testget = _M_in_cur < _M_in_end;
135 if (_M_in_cur + __n - __retval <= _M_in_end)
136 __len = __n - __retval;
138 __len = _M_in_end - _M_in_cur;
139 traits_type::copy(__s, _M_in_cur, __len);
143 if (_M_buf_unified && __testout)
149 if (this->uflow() != traits_type::eof())
159 // Don't test against _M_buf + _M_buf_size, because _M_buf reflects
160 // allocated space, and on certain (rare but entirely legal)
161 // situations, there will be no allocated space yet the internal
162 // buffers will still be valid. (This happens if setp is used to set
163 // the internal buffer to say some externally-allocated sequence.)
164 template<typename _CharT, typename _Traits>
166 basic_streambuf<_CharT, _Traits>::
167 xsputn(const char_type* __s, streamsize __n)
169 streamsize __retval = 0;
173 while (__retval < __n)
175 bool __testput = _M_out_cur < _M_out_beg + _M_buf_size;
176 bool __testout = _M_mode & ios_base::out;
177 if (!(__testput && __testout))
179 char_type __c = *__s;
180 char_type __overfc = this->overflow(__c);
193 if (_M_out_cur + __n - __retval <= _M_out_beg + _M_buf_size)
194 __len = __n - __retval;
196 __len = _M_out_beg + _M_buf_size - _M_out_cur;
197 traits_type::copy(_M_out_cur, __s, __len);
208 // Conceivably, this could be used to implement buffer-to-buffer
209 // copies, if this was ever desired in an un-ambiguous way by the
210 // standard. If so, then checks for __ios being zero would be
212 template<typename _CharT, typename _Traits>
214 _S_copy_streambufs(basic_ios<_CharT, _Traits>& __ios,
215 basic_streambuf<_CharT, _Traits>* __sbin,
216 basic_streambuf<_CharT, _Traits>* __sbout)
218 typedef typename _Traits::int_type int_type;
220 streamsize __retval = 0;
221 streamsize __bufsize = __sbin->in_avail();
223 bool __testout = __sbin->_M_mode & ios_base::out;
224 bool __testput = __sbout->_M_mode & ios_base::out;
226 while (__testput && __bufsize != -1)
228 __xtrct = __sbout->sputn(__sbin->gptr(), __bufsize);
230 __sbin->_M_in_cur += __xtrct;
231 if (__testout && __sbin->_M_buf_unified)
232 __sbin->_M_out_cur += __xtrct;
233 if (__xtrct == __bufsize)
235 int_type __c = __sbin->sgetc();
236 if (__c == _Traits::eof())
238 __ios.setstate(ios_base::eofbit);
246 catch(exception& __fail) {
247 if ((__ios.exceptions() & ios_base::failbit) != 0)
255 #endif // _CPP_BITS_STREAMBUF_TCC