]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/streambuf.tcc
std_streambuf.h (_M_out_buf_size()): Remove.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / streambuf.tcc
1 // Stream buffer classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
11
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.
16
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,
20 // USA.
21
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.
30
31 //
32 // ISO C++ 14882: 27.5 Stream buffers
33 //
34
35 #ifndef _CPP_BITS_STREAMBUF_TCC
36 #define _CPP_BITS_STREAMBUF_TCC 1
37
38 #pragma GCC system_header
39
40 namespace std
41 {
42 template<typename _CharT, typename _Traits>
43 const size_t
44 basic_streambuf<_CharT, _Traits>::_S_pback_size;
45
46 template<typename _CharT, typename _Traits>
47 typename basic_streambuf<_CharT, _Traits>::int_type
48 basic_streambuf<_CharT, _Traits>::
49 sbumpc()
50 {
51 int_type __ret;
52 if (_M_in_cur && _M_in_cur < _M_in_end)
53 {
54 char_type __c = *(this->gptr());
55 _M_in_cur_move(1);
56 __ret = traits_type::to_int_type(__c);
57 }
58 else
59 __ret = this->uflow();
60 return __ret;
61 }
62
63 template<typename _CharT, typename _Traits>
64 typename basic_streambuf<_CharT, _Traits>::int_type
65 basic_streambuf<_CharT, _Traits>::
66 sputbackc(char_type __c)
67 {
68 int_type __ret;
69 bool __testpos = _M_in_cur && _M_in_beg < _M_in_cur;
70 if (!__testpos || !traits_type::eq(__c, this->gptr()[-1]))
71 __ret = this->pbackfail(traits_type::to_int_type(__c));
72 else
73 {
74 _M_in_cur_move(-1);
75 __ret = traits_type::to_int_type(*this->gptr());
76 }
77 return __ret;
78 }
79
80 template<typename _CharT, typename _Traits>
81 typename basic_streambuf<_CharT, _Traits>::int_type
82 basic_streambuf<_CharT, _Traits>::
83 sungetc()
84 {
85 int_type __ret;
86 if (_M_in_cur && _M_in_beg < _M_in_cur)
87 {
88 _M_in_cur_move(-1);
89 __ret = traits_type::to_int_type(*_M_in_cur);
90 }
91 else
92 __ret = this->pbackfail();
93 return __ret;
94 }
95
96 template<typename _CharT, typename _Traits>
97 typename basic_streambuf<_CharT, _Traits>::int_type
98 basic_streambuf<_CharT, _Traits>::
99 sputc(char_type __c)
100 {
101 int_type __ret;
102 if (_M_out_cur && _M_out_cur < _M_out_end)
103 {
104 *_M_out_cur = __c;
105 _M_out_cur_move(1);
106 __ret = traits_type::to_int_type(__c);
107 }
108 else
109 __ret = this->overflow(traits_type::to_int_type(__c));
110 return __ret;
111 }
112
113 template<typename _CharT, typename _Traits>
114 streamsize
115 basic_streambuf<_CharT, _Traits>::
116 xsgetn(char_type* __s, streamsize __n)
117 {
118 streamsize __ret = 0;
119 while (__ret < __n)
120 {
121 size_t __buf_len = _M_in_end - _M_in_cur;
122 if (__buf_len > 0)
123 {
124 size_t __remaining = __n - __ret;
125 size_t __len = std::min(__buf_len, __remaining);
126 traits_type::copy(__s, _M_in_cur, __len);
127 __ret += __len;
128 __s += __len;
129 _M_in_cur_move(__len);
130 }
131
132 if (__ret < __n)
133 {
134 int_type __c = this->uflow();
135 if (!traits_type::eq_int_type(__c, traits_type::eof()))
136 {
137 traits_type::assign(*__s++, traits_type::to_char_type(__c));
138 ++__ret;
139 }
140 else
141 break;
142 }
143 }
144 return __ret;
145 }
146
147 template<typename _CharT, typename _Traits>
148 streamsize
149 basic_streambuf<_CharT, _Traits>::
150 xsputn(const char_type* __s, streamsize __n)
151 {
152 streamsize __ret = 0;
153 while (__ret < __n)
154 {
155 off_type __buf_len = _M_out_end - _M_out_cur;
156 if (__buf_len > 0)
157 {
158 off_type __remaining = __n - __ret;
159 off_type __len = std::min(__buf_len, __remaining);
160 traits_type::copy(_M_out_cur, __s, __len);
161 __ret += __len;
162 __s += __len;
163 _M_out_cur_move(__len);
164 }
165
166 if (__ret < __n)
167 {
168 int_type __c = this->overflow(traits_type::to_int_type(*__s));
169 if (!traits_type::eq_int_type(__c, traits_type::eof()))
170 {
171 ++__ret;
172 ++__s;
173 }
174 else
175 break;
176 }
177 }
178 return __ret;
179 }
180
181 // Conceivably, this could be used to implement buffer-to-buffer
182 // copies, if this was ever desired in an un-ambiguous way by the
183 // standard. If so, then checks for __ios being zero would be
184 // necessary.
185 template<typename _CharT, typename _Traits>
186 streamsize
187 __copy_streambufs(basic_ios<_CharT, _Traits>& __ios,
188 basic_streambuf<_CharT, _Traits>* __sbin,
189 basic_streambuf<_CharT, _Traits>* __sbout)
190 {
191 typedef typename _Traits::int_type int_type;
192 typedef typename _Traits::off_type off_type;
193
194 streamsize __ret = 0;
195 streamsize __bufsize = __sbin->in_avail();
196 streamsize __xtrct;
197 const off_type __size_opt =
198 __sbin->_M_buf_size_opt > 0 ? __sbin->_M_buf_size_opt : 1;
199
200 try
201 {
202 while (__bufsize != -1)
203 {
204 if (__bufsize != 0 && __sbin->gptr() != NULL
205 && __sbin->gptr() + __bufsize <= __sbin->egptr())
206 {
207 __xtrct = __sbout->sputn(__sbin->gptr(), __bufsize);
208 __ret += __xtrct;
209 __sbin->_M_in_cur_move(__xtrct);
210 if (__xtrct != __bufsize)
211 break;
212 }
213 else
214 {
215 streamsize __charsread;
216 const streamsize __size =
217 std::min(__size_opt, off_type(__sbout->_M_out_end -
218 __sbout->_M_out_cur));
219 if (__size > 1)
220 {
221 _CharT* __buf =
222 static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
223 * __size));
224 // Since the next sputn cannot fail sgetn can be
225 // safely used.
226 __charsread = __sbin->sgetn(__buf, __size);
227 __xtrct = __sbout->sputn(__buf, __charsread);
228 }
229 else
230 {
231 __xtrct = __charsread = 0;
232 int_type __c = __sbin->sgetc();
233 while (!_Traits::eq_int_type(__c, _Traits::eof()))
234 {
235 ++__charsread;
236 if (_Traits::eq_int_type(__sbout->sputc(_Traits::to_char_type(__c)),
237 _Traits::eof()))
238 break;
239 ++__xtrct;
240 __c = __sbin->snextc();
241 }
242 }
243 __ret += __xtrct;
244 if (__xtrct != __charsread)
245 break;
246 }
247 if (_Traits::eq_int_type(__sbin->sgetc(), _Traits::eof()))
248 break;
249 __bufsize = __sbin->in_avail();
250 }
251 }
252 catch(exception& __fail)
253 {
254 __ios.setstate(ios_base::failbit);
255 if ((__ios.exceptions() & ios_base::failbit) != 0)
256 __throw_exception_again;
257 }
258 return __ret;
259 }
260
261 // Inhibit implicit instantiations for required instantiations,
262 // which are defined via explicit instantiations elsewhere.
263 // NB: This syntax is a GNU extension.
264 #if _GLIBCPP_EXTERN_TEMPLATE
265 extern template class basic_streambuf<char>;
266 extern template
267 streamsize
268 __copy_streambufs(basic_ios<char>&, basic_streambuf<char>*,
269 basic_streambuf<char>*);
270
271 #ifdef _GLIBCPP_USE_WCHAR_T
272 extern template class basic_streambuf<wchar_t>;
273 extern template
274 streamsize
275 __copy_streambufs(basic_ios<wchar_t>&, basic_streambuf<wchar_t>*,
276 basic_streambuf<wchar_t>*);
277 #endif
278 #endif
279 } // namespace std
280
281 #endif