]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/bits/streambuf.tcc
libstdc++-v3: New directory.
[thirdparty/gcc.git] / libstdc++-v3 / bits / streambuf.tcc
1 // Stream buffer classes -*- C++ -*-
2
3 // Copyright (C) 1997-1999 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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,
19 // USA.
20
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.
29
30 //
31 // ISO C++ 14882: 27.5 Stream buffers
32 //
33
34 #ifndef _CPP_BITS_STREAMBUF_TCC
35 #define _CPP_BITS_STREAMBUF_TCC 1
36
37 namespace std {
38
39 template<typename _CharT, typename _Traits>
40 basic_streambuf<_CharT, _Traits>::int_type
41 basic_streambuf<_CharT, _Traits>::
42 sbumpc()
43 {
44 int_type __retval;
45 if (_M_in_cur && _M_in_cur < _M_in_end)
46 {
47 char_type __c = *gptr();
48 ++_M_in_cur;
49 if (_M_buf_unified && _M_mode & ios_base::out)
50 ++_M_out_cur;
51 __retval = traits_type::to_int_type(__c);
52 }
53 else
54 __retval = this->uflow();
55 return __retval;
56 }
57
58 template<typename _CharT, typename _Traits>
59 basic_streambuf<_CharT, _Traits>::int_type
60 basic_streambuf<_CharT, _Traits>::
61 sputbackc(char_type __c)
62 {
63 int_type __retval;
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));
68 else
69 {
70 --_M_in_cur;
71 if (_M_buf_unified && _M_mode & ios_base::out)
72 --_M_out_cur;
73 __retval = traits_type::to_int_type(*this->gptr());
74 }
75 return __retval;
76 }
77
78 template<typename _CharT, typename _Traits>
79 basic_streambuf<_CharT, _Traits>::int_type
80 basic_streambuf<_CharT, _Traits>::
81 sungetc()
82 {
83 int_type __retval;
84 if (_M_in_cur && _M_in_beg < _M_in_cur)
85 {
86 --_M_in_cur;
87 if (_M_buf_unified && _M_mode & ios_base::out)
88 --_M_out_cur;
89 __retval = traits_type::to_int_type(*_M_in_cur);
90 }
91 else
92 __retval = this->pbackfail();
93 return __retval;
94 }
95
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>::
104 sputc(char_type __c)
105 {
106 int_type __retval;
107
108 if (_M_out_cur && _M_out_cur < _M_out_beg + _M_buf_size)
109 {
110 *_M_out_cur = __c;
111 _M_buf_bump(1);
112 __retval = traits_type::to_int_type(__c);
113 }
114 else
115 __retval = this->overflow(traits_type::to_int_type(__c));
116 return __retval;
117 }
118
119 template<typename _CharT, typename _Traits>
120 streamsize
121 basic_streambuf<_CharT, _Traits>::
122 xsgetn(char_type* __s, streamsize __n)
123 {
124 bool __testout = _M_mode & ios_base::out;
125 streamsize __retval = 0;
126
127 if (__n)
128 {
129 while (__retval < __n)
130 {
131 bool __testget = _M_in_cur < _M_in_end;
132 if (__testget)
133 {
134 size_t __len;
135 if (_M_in_cur + __n - __retval <= _M_in_end)
136 __len = __n - __retval;
137 else
138 __len = _M_in_end - _M_in_cur;
139 traits_type::copy(__s, _M_in_cur, __len);
140 __retval += __len;
141 __s += __len;
142 _M_in_cur += __len;
143 if (_M_buf_unified && __testout)
144 _M_out_cur += __len;
145 }
146
147 if (__retval != __n)
148 {
149 if (this->uflow() != traits_type::eof())
150 ++__retval;
151 else
152 break;
153 }
154 }
155 }
156 return __retval;
157 }
158
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>
165 streamsize
166 basic_streambuf<_CharT, _Traits>::
167 xsputn(const char_type* __s, streamsize __n)
168 {
169 streamsize __retval = 0;
170
171 if (__n)
172 {
173 while (__retval < __n)
174 {
175 bool __testput = _M_out_cur < _M_out_beg + _M_buf_size;
176 bool __testout = _M_mode & ios_base::out;
177 if (!(__testput && __testout))
178 {
179 char_type __c = *__s;
180 char_type __overfc = this->overflow(__c);
181 if (__c == __overfc)
182 {
183 ++__retval;
184 ++__s;
185 }
186 else
187 break;
188 }
189
190 if (__retval != __n)
191 {
192 size_t __len;
193 if (_M_out_cur + __n - __retval <= _M_out_beg + _M_buf_size)
194 __len = __n - __retval;
195 else
196 __len = _M_out_beg + _M_buf_size - _M_out_cur;
197 traits_type::copy(_M_out_cur, __s, __len);
198 __retval += __len;
199 __s += __len;
200 _M_buf_bump(__len);
201 }
202 }
203 }
204 return __retval;
205 }
206
207
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
211 // necessary.
212 template<typename _CharT, typename _Traits>
213 static streamsize
214 _S_copy_streambufs(basic_ios<_CharT, _Traits>& __ios,
215 basic_streambuf<_CharT, _Traits>* __sbin,
216 basic_streambuf<_CharT, _Traits>* __sbout)
217 {
218 typedef typename _Traits::int_type int_type;
219
220 streamsize __retval = 0;
221 streamsize __bufsize = __sbin->in_avail();
222 streamsize __xtrct;
223 bool __testout = __sbin->_M_mode & ios_base::out;
224 bool __testput = __sbout->_M_mode & ios_base::out;
225 try {
226 while (__testput && __bufsize != -1)
227 {
228 __xtrct = __sbout->sputn(__sbin->gptr(), __bufsize);
229 __retval += __xtrct;
230 __sbin->_M_in_cur += __xtrct;
231 if (__testout && __sbin->_M_buf_unified)
232 __sbin->_M_out_cur += __xtrct;
233 if (__xtrct == __bufsize)
234 {
235 int_type __c = __sbin->sgetc();
236 if (__c == _Traits::eof())
237 {
238 __ios.setstate(ios_base::eofbit);
239 break;
240 }
241 }
242 else
243 break;
244 }
245 }
246 catch(exception& __fail) {
247 if ((__ios.exceptions() & ios_base::failbit) != 0)
248 throw;
249 }
250 return __retval;
251 }
252
253 } // namespace std
254
255 #endif // _CPP_BITS_STREAMBUF_TCC
256
257
258
259