1 // Stream buffer classes -*- 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.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 __ret = traits_type::to_int_type(__c);
52 __ret = this->uflow();
56 template<typename _CharT, typename _Traits>
57 basic_streambuf<_CharT, _Traits>::int_type
58 basic_streambuf<_CharT, _Traits>::
59 sputbackc(char_type __c)
62 bool __testpos = _M_in_cur && _M_in_beg < _M_in_cur;
63 bool __testne = _M_in_cur && !traits_type::eq(__c, this->gptr()[-1]);
64 if (!__testpos || __testne)
65 __ret = pbackfail(traits_type::to_int_type(__c));
69 __ret = traits_type::to_int_type(*this->gptr());
74 template<typename _CharT, typename _Traits>
75 basic_streambuf<_CharT, _Traits>::int_type
76 basic_streambuf<_CharT, _Traits>::
80 if (_M_in_cur && _M_in_beg < _M_in_cur)
83 __ret = traits_type::to_int_type(*_M_in_cur);
86 __ret = this->pbackfail();
90 // Don't test against _M_buf + _M_buf_size, because _M_buf reflects
91 // allocated space, and on certain (rare but entirely legal)
92 // situations, there will be no allocated space yet the internal
93 // buffers will still be valid. (This happens if setp is used to set
94 // the internal buffer to say some externally-allocated sequence.)
95 template<typename _CharT, typename _Traits>
96 basic_streambuf<_CharT, _Traits>::int_type
97 basic_streambuf<_CharT, _Traits>::
101 if (_M_out_buf_size())
105 __ret = traits_type::to_int_type(__c);
108 __ret = this->overflow(traits_type::to_int_type(__c));
112 template<typename _CharT, typename _Traits>
114 basic_streambuf<_CharT, _Traits>::
115 xsgetn(char_type* __s, streamsize __n)
117 streamsize __ret = 0;
120 size_t __buf_len = _M_in_end - _M_in_cur;
123 size_t __remaining = __n - __ret;
124 size_t __len = min(__buf_len, __remaining);
125 traits_type::copy(__s, _M_in_cur, __len);
128 _M_in_cur_move(__len);
133 int_type __c = this->uflow();
134 if (__c != traits_type::eof())
136 traits_type::assign(*__s++, traits_type::to_char_type(__c));
146 // Don't test against _M_buf + _M_buf_size, because _M_buf reflects
147 // allocated space, and on certain (rare but entirely legal)
148 // situations, there will be no allocated space yet the internal
149 // buffers will still be valid. (This happens if setp is used to set
150 // the internal buffer to say some externally-allocated sequence.)
151 template<typename _CharT, typename _Traits>
153 basic_streambuf<_CharT, _Traits>::
154 xsputn(const char_type* __s, streamsize __n)
156 streamsize __ret = 0;
159 off_type __buf_len = _M_out_buf_size();
162 off_type __remaining = __n - __ret;
163 off_type __len = min(__buf_len, __remaining);
164 traits_type::copy(_M_out_cur, __s, __len);
167 _M_out_cur_move(__len);
172 int_type __c = this->overflow(traits_type::to_int_type(*__s));
173 if (__c != traits_type::eof())
185 // Conceivably, this could be used to implement buffer-to-buffer
186 // copies, if this was ever desired in an un-ambiguous way by the
187 // standard. If so, then checks for __ios being zero would be
189 template<typename _CharT, typename _Traits>
191 __copy_streambufs(basic_ios<_CharT, _Traits>& __ios,
192 basic_streambuf<_CharT, _Traits>* __sbin,
193 basic_streambuf<_CharT, _Traits>* __sbout)
195 typedef typename _Traits::int_type int_type;
197 streamsize __ret = 0;
198 streamsize __bufsize = __sbin->in_avail();
200 bool __testput = __sbout->_M_mode & ios_base::out;
202 while (__testput && __bufsize != -1)
204 __xtrct = __sbout->sputn(__sbin->gptr(), __bufsize);
206 __sbin->_M_in_cur_move(__xtrct);
207 if (__xtrct == __bufsize)
209 int_type __c = __sbin->sgetc();
210 if (__c == _Traits::eof())
212 __ios.setstate(ios_base::eofbit);
215 __bufsize = __sbin->in_avail();
221 catch(exception& __fail) {
222 if ((__ios.exceptions() & ios_base::failbit) != 0)
223 __throw_exception_again;
229 #endif // _CPP_BITS_STREAMBUF_TCC