1 // Stream buffer classes -*- C++ -*-
3 // Copyright (C) 1997-1999, 2000 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 __ret = traits_type::to_int_type(__c);
54 __ret = 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 __ret = pbackfail(traits_type::to_int_type(__c));
71 if (_M_buf_unified && _M_mode & ios_base::out)
73 __ret = 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 __ret = traits_type::to_int_type(*_M_in_cur);
92 __ret = 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 __ret = traits_type::to_int_type(__c);
115 __ret = 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 __ret = 0;
131 if (_M_in_cur < _M_in_end)
134 if (_M_in_cur + __n - __ret <= _M_in_end)
137 __len = _M_in_end - _M_in_cur;
138 traits_type::copy(__s, _M_in_cur, __len);
142 if (_M_buf_unified && __testout)
148 int_type __c = this->uflow();
149 if (traits_type::eq_int_type(__c, traits_type::eof()))
152 traits_type::assign(*__s++, traits_type::to_char_type(__c));
160 // Don't test against _M_buf + _M_buf_size, because _M_buf reflects
161 // allocated space, and on certain (rare but entirely legal)
162 // situations, there will be no allocated space yet the internal
163 // buffers will still be valid. (This happens if setp is used to set
164 // the internal buffer to say some externally-allocated sequence.)
165 template<typename _CharT, typename _Traits>
167 basic_streambuf<_CharT, _Traits>::
168 xsputn(const char_type* __s, streamsize __n)
170 streamsize __ret = 0;
176 bool __testput = _M_out_cur < _M_out_beg + _M_buf_size;
177 bool __testout = _M_mode & ios_base::out;
178 if (!(__testput && __testout))
180 int_type __c = traits_type::to_int_type(*__s);
181 int_type __overfc = this->overflow(__c);
182 if (traits_type::eq_int_type(__c, __overfc))
194 if (_M_out_cur + __n - __ret <= _M_out_beg + _M_buf_size)
197 __len = _M_out_beg + _M_buf_size - _M_out_cur;
198 traits_type::copy(_M_out_cur, __s, __len);
201 _M_out_cur_move(__len);
209 // Conceivably, this could be used to implement buffer-to-buffer
210 // copies, if this was ever desired in an un-ambiguous way by the
211 // standard. If so, then checks for __ios being zero would be
213 template<typename _CharT, typename _Traits>
215 _S_copy_streambufs(basic_ios<_CharT, _Traits>& __ios,
216 basic_streambuf<_CharT, _Traits>* __sbin,
217 basic_streambuf<_CharT, _Traits>* __sbout)
219 typedef typename _Traits::int_type int_type;
221 streamsize __ret = 0;
222 streamsize __bufsize = __sbin->in_avail();
224 bool __testout = __sbin->_M_mode & ios_base::out;
225 bool __testput = __sbout->_M_mode & ios_base::out;
227 while (__testput && __bufsize != -1)
229 __xtrct = __sbout->sputn(__sbin->gptr(), __bufsize);
231 __sbin->_M_in_cur += __xtrct;
232 if (__testout && __sbin->_M_buf_unified)
233 __sbin->_M_out_cur += __xtrct;
234 if (__xtrct == __bufsize)
236 int_type __c = __sbin->sgetc();
237 if (__c == _Traits::eof())
239 __ios.setstate(ios_base::eofbit);
242 __bufsize = __sbin->in_avail();
248 catch(exception& __fail) {
249 if ((__ios.exceptions() & ios_base::failbit) != 0)
257 #endif // _CPP_BITS_STREAMBUF_TCC