1 // Stream buffer classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 27.5 Stream buffers
35 #ifndef _CPP_BITS_STREAMBUF_TCC
36 #define _CPP_BITS_STREAMBUF_TCC 1
38 #pragma GCC system_header
42 template<typename _CharT, typename _Traits>
43 typename basic_streambuf<_CharT, _Traits>::int_type
44 basic_streambuf<_CharT, _Traits>::
48 if (_M_in_cur < _M_in_end)
50 char_type __c = *this->_M_in_cur;
52 __ret = traits_type::to_int_type(__c);
55 __ret = this->uflow();
59 template<typename _CharT, typename _Traits>
60 typename basic_streambuf<_CharT, _Traits>::int_type
61 basic_streambuf<_CharT, _Traits>::
62 sputbackc(char_type __c)
65 const bool __testpos = _M_in_beg < _M_in_cur;
66 if (!__testpos || !traits_type::eq(__c, this->_M_in_cur[-1]))
67 __ret = this->pbackfail(traits_type::to_int_type(__c));
71 __ret = traits_type::to_int_type(*this->_M_in_cur);
76 template<typename _CharT, typename _Traits>
77 typename basic_streambuf<_CharT, _Traits>::int_type
78 basic_streambuf<_CharT, _Traits>::
82 if (_M_in_beg < _M_in_cur)
85 __ret = traits_type::to_int_type(*_M_in_cur);
88 __ret = this->pbackfail();
92 template<typename _CharT, typename _Traits>
93 typename basic_streambuf<_CharT, _Traits>::int_type
94 basic_streambuf<_CharT, _Traits>::
98 if (_M_out_cur < _M_out_end)
102 __ret = traits_type::to_int_type(__c);
105 __ret = this->overflow(traits_type::to_int_type(__c));
109 template<typename _CharT, typename _Traits>
111 basic_streambuf<_CharT, _Traits>::
112 xsgetn(char_type* __s, streamsize __n)
114 streamsize __ret = 0;
117 const size_t __buf_len = _M_in_end - _M_in_cur;
120 const size_t __remaining = __n - __ret;
121 const size_t __len = std::min(__buf_len, __remaining);
122 traits_type::copy(__s, _M_in_cur, __len);
125 _M_move_in_cur(__len);
130 const int_type __c = this->uflow();
131 if (!traits_type::eq_int_type(__c, traits_type::eof()))
133 traits_type::assign(*__s++, traits_type::to_char_type(__c));
143 template<typename _CharT, typename _Traits>
145 basic_streambuf<_CharT, _Traits>::
146 xsputn(const char_type* __s, streamsize __n)
148 streamsize __ret = 0;
151 const size_t __buf_len = _M_out_end - _M_out_cur;
154 const size_t __remaining = __n - __ret;
155 const size_t __len = std::min(__buf_len, __remaining);
156 traits_type::copy(_M_out_cur, __s, __len);
159 _M_move_out_cur(__len);
164 const int_type __c = this->overflow(traits_type::to_int_type(*__s));
165 if (!traits_type::eq_int_type(__c, traits_type::eof()))
177 // Conceivably, this could be used to implement buffer-to-buffer
178 // copies, if this was ever desired in an un-ambiguous way by the
179 // standard. If so, then checks for __ios being zero would be
181 template<typename _CharT, typename _Traits>
183 __copy_streambufs(basic_ios<_CharT, _Traits>& __ios,
184 basic_streambuf<_CharT, _Traits>* __sbin,
185 basic_streambuf<_CharT, _Traits>* __sbout)
187 typedef typename _Traits::int_type int_type;
189 streamsize __ret = 0;
195 const size_t __avail = __sbin->_M_in_end
199 __xtrct = __sbout->sputn(__sbin->_M_in_cur, __avail);
201 __sbin->_M_move_in_cur(__xtrct);
202 if (__xtrct != __avail)
207 streamsize __charsread;
208 const size_t __size = __sbout->_M_out_end
209 - __sbout->_M_out_cur;
213 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
215 // Since the next sputn cannot fail sgetn can be
217 __charsread = __sbin->sgetn(__buf, __size);
218 __xtrct = __sbout->sputn(__buf, __charsread);
222 __xtrct = __charsread = 0;
223 const int_type __c = __sbin->sgetc();
224 if (!_Traits::eq_int_type(__c, _Traits::eof()))
227 if (_Traits::eq_int_type(__sbout->overflow(__c),
235 if (__xtrct != __charsread)
238 if (_Traits::eq_int_type(__sbin->sgetc(), _Traits::eof()))
242 catch(exception& __fail)
244 __ios.setstate(ios_base::failbit);
245 if ((__ios.exceptions() & ios_base::failbit) != 0)
246 __throw_exception_again;
251 // Inhibit implicit instantiations for required instantiations,
252 // which are defined via explicit instantiations elsewhere.
253 // NB: This syntax is a GNU extension.
254 #if _GLIBCPP_EXTERN_TEMPLATE
255 extern template class basic_streambuf<char>;
258 __copy_streambufs(basic_ios<char>&, basic_streambuf<char>*,
259 basic_streambuf<char>*);
261 #ifdef _GLIBCPP_USE_WCHAR_T
262 extern template class basic_streambuf<wchar_t>;
265 __copy_streambufs(basic_ios<wchar_t>&, basic_streambuf<wchar_t>*,
266 basic_streambuf<wchar_t>*);