codecvt_specializations_ieee_1003.1-200x.h: Initialize all data members in copy ctor.
[gcc.git] / libstdc++-v3 / include / bits / streambuf.tcc
1 // Stream buffer classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
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 namespace std
39 {
40 template<typename _CharT, typename _Traits>
41 typename basic_streambuf<_CharT, _Traits>::int_type
42 basic_streambuf<_CharT, _Traits>::
43 sbumpc()
44 {
45 int_type __ret;
46 if (_M_in_cur && _M_in_cur < _M_in_end)
47 {
48 char_type __c = *gptr();
49 _M_in_cur_move(1);
50 __ret = traits_type::to_int_type(__c);
51 }
52 else
53 __ret = this->uflow();
54 return __ret;
55 }
56
57 template<typename _CharT, typename _Traits>
58 typename basic_streambuf<_CharT, _Traits>::int_type
59 basic_streambuf<_CharT, _Traits>::
60 sputbackc(char_type __c)
61 {
62 int_type __ret;
63 bool __testpos = _M_in_cur && _M_in_beg < _M_in_cur;
64 bool __testne = _M_in_cur && !traits_type::eq(__c, this->gptr()[-1]);
65 if (!__testpos || __testne)
66 __ret = pbackfail(traits_type::to_int_type(__c));
67 else
68 {
69 _M_in_cur_move(-1);
70 __ret = traits_type::to_int_type(*this->gptr());
71 }
72 return __ret;
73 }
74
75 template<typename _CharT, typename _Traits>
76 typename basic_streambuf<_CharT, _Traits>::int_type
77 basic_streambuf<_CharT, _Traits>::
78 sungetc()
79 {
80 int_type __ret;
81 if (_M_in_cur && _M_in_beg < _M_in_cur)
82 {
83 _M_in_cur_move(-1);
84 __ret = traits_type::to_int_type(*_M_in_cur);
85 }
86 else
87 __ret = this->pbackfail();
88 return __ret;
89 }
90
91 // Don't test against _M_buf + _M_buf_size, because _M_buf reflects
92 // allocated space, and on certain (rare but entirely legal)
93 // situations, there will be no allocated space yet the internal
94 // buffers will still be valid. (This happens if setp is used to set
95 // the internal buffer to say some externally-allocated sequence.)
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_buf_size())
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 = 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 (__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 // Don't test against _M_buf + _M_buf_size, because _M_buf reflects
148 // allocated space, and on certain (rare but entirely legal)
149 // situations, there will be no allocated space yet the internal
150 // buffers will still be valid. (This happens if setp is used to set
151 // the internal buffer to say some externally-allocated sequence.)
152 template<typename _CharT, typename _Traits>
153 streamsize
154 basic_streambuf<_CharT, _Traits>::
155 xsputn(const char_type* __s, streamsize __n)
156 {
157 streamsize __ret = 0;
158 while (__ret < __n)
159 {
160 off_type __buf_len = _M_out_buf_size();
161 if (__buf_len > 0)
162 {
163 off_type __remaining = __n - __ret;
164 off_type __len = min(__buf_len, __remaining);
165 traits_type::copy(_M_out_cur, __s, __len);
166 __ret += __len;
167 __s += __len;
168 _M_out_cur_move(__len);
169 }
170
171 if (__ret < __n)
172 {
173 int_type __c = this->overflow(traits_type::to_int_type(*__s));
174 if (__c != traits_type::eof())
175 {
176 ++__ret;
177 ++__s;
178 }
179 else
180 break;
181 }
182 }
183 return __ret;
184 }
185
186 // Conceivably, this could be used to implement buffer-to-buffer
187 // copies, if this was ever desired in an un-ambiguous way by the
188 // standard. If so, then checks for __ios being zero would be
189 // necessary.
190 template<typename _CharT, typename _Traits>
191 streamsize
192 __copy_streambufs(basic_ios<_CharT, _Traits>& __ios,
193 basic_streambuf<_CharT, _Traits>* __sbin,
194 basic_streambuf<_CharT, _Traits>* __sbout)
195 {
196 typedef typename _Traits::int_type int_type;
197
198 streamsize __ret = 0;
199 streamsize __bufsize = __sbin->in_avail();
200 streamsize __xtrct;
201 bool __testput = __sbout->_M_mode & ios_base::out;
202 try
203 {
204 while (__testput && __bufsize != -1)
205 {
206 __xtrct = __sbout->sputn(__sbin->gptr(), __bufsize);
207 __ret += __xtrct;
208 __sbin->_M_in_cur_move(__xtrct);
209 if (__xtrct == __bufsize)
210 {
211 if (__sbin->sgetc() == _Traits::eof())
212 break;
213 __bufsize = __sbin->in_avail();
214 }
215 else
216 break;
217 }
218 }
219 catch(exception& __fail)
220 {
221 __ios.setstate(ios_base::failbit);
222 if ((__ios.exceptions() & ios_base::failbit) != 0)
223 __throw_exception_again;
224 }
225 return __ret;
226 }
227 } // namespace std
228
229 #endif