include: New directory.
[gcc.git] / libstdc++-v3 / include / bits / streambuf.tcc
1 // Stream buffer classes -*- C++ -*-
2
3 // Copyright (C) 1997-1999, 2000 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 __ret;
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 __ret = traits_type::to_int_type(__c);
52 }
53 else
54 __ret = this->uflow();
55 return __ret;
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 __ret;
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));
68 else
69 {
70 --_M_in_cur;
71 if (_M_buf_unified && _M_mode & ios_base::out)
72 --_M_out_cur;
73 __ret = traits_type::to_int_type(*this->gptr());
74 }
75 return __ret;
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 __ret;
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 __ret = traits_type::to_int_type(*_M_in_cur);
90 }
91 else
92 __ret = this->pbackfail();
93 return __ret;
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 __ret;
107
108 if (_M_out_cur && _M_out_cur < _M_out_beg + _M_buf_size)
109 {
110 *_M_out_cur = __c;
111 _M_out_cur_move(1);
112 __ret = traits_type::to_int_type(__c);
113 }
114 else
115 __ret = this->overflow(traits_type::to_int_type(__c));
116 return __ret;
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 __ret = 0;
126
127 if (__n)
128 {
129 while (__ret < __n)
130 {
131 if (_M_in_cur < _M_in_end)
132 {
133 size_t __len;
134 if (_M_in_cur + __n - __ret <= _M_in_end)
135 __len = __n - __ret;
136 else
137 __len = _M_in_end - _M_in_cur;
138 traits_type::copy(__s, _M_in_cur, __len);
139 __ret += __len;
140 __s += __len;
141 _M_in_cur += __len;
142 if (_M_buf_unified && __testout)
143 _M_out_cur += __len;
144 }
145
146 if (__ret != __n)
147 {
148 int_type __c = this->uflow();
149 if (traits_type::eq_int_type(__c, traits_type::eof()))
150 break;
151
152 traits_type::assign(*__s++, traits_type::to_char_type(__c));
153 ++__ret;
154 }
155 }
156 }
157 return __ret;
158 }
159
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>
166 streamsize
167 basic_streambuf<_CharT, _Traits>::
168 xsputn(const char_type* __s, streamsize __n)
169 {
170 streamsize __ret = 0;
171
172 if (__n)
173 {
174 while (__ret < __n)
175 {
176 bool __testput = _M_out_cur < _M_out_beg + _M_buf_size;
177 bool __testout = _M_mode & ios_base::out;
178 if (!(__testput && __testout))
179 {
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))
183 {
184 ++__ret;
185 ++__s;
186 }
187 else
188 break;
189 }
190
191 if (__ret != __n)
192 {
193 size_t __len;
194 if (_M_out_cur + __n - __ret <= _M_out_beg + _M_buf_size)
195 __len = __n - __ret;
196 else
197 __len = _M_out_beg + _M_buf_size - _M_out_cur;
198 traits_type::copy(_M_out_cur, __s, __len);
199 __ret += __len;
200 __s += __len;
201 _M_out_cur_move(__len);
202 }
203 }
204 }
205 return __ret;
206 }
207
208
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
212 // necessary.
213 template<typename _CharT, typename _Traits>
214 streamsize
215 _S_copy_streambufs(basic_ios<_CharT, _Traits>& __ios,
216 basic_streambuf<_CharT, _Traits>* __sbin,
217 basic_streambuf<_CharT, _Traits>* __sbout)
218 {
219 typedef typename _Traits::int_type int_type;
220
221 streamsize __ret = 0;
222 streamsize __bufsize = __sbin->in_avail();
223 streamsize __xtrct;
224 bool __testout = __sbin->_M_mode & ios_base::out;
225 bool __testput = __sbout->_M_mode & ios_base::out;
226 try {
227 while (__testput && __bufsize != -1)
228 {
229 __xtrct = __sbout->sputn(__sbin->gptr(), __bufsize);
230 __ret += __xtrct;
231 __sbin->_M_in_cur += __xtrct;
232 if (__testout && __sbin->_M_buf_unified)
233 __sbin->_M_out_cur += __xtrct;
234 if (__xtrct == __bufsize)
235 {
236 int_type __c = __sbin->sgetc();
237 if (__c == _Traits::eof())
238 {
239 __ios.setstate(ios_base::eofbit);
240 break;
241 }
242 __bufsize = __sbin->in_avail();
243 }
244 else
245 break;
246 }
247 }
248 catch(exception& __fail) {
249 if ((__ios.exceptions() & ios_base::failbit) != 0)
250 throw;
251 }
252 return __ret;
253 }
254
255 } // namespace std
256
257 #endif // _CPP_BITS_STREAMBUF_TCC
258
259
260
261