algo.h: Use std not __STD.
[gcc.git] / libstdc++-v3 / include / bits / streambuf.tcc
1 // Stream buffer classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 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_move(1);
49 __ret = traits_type::to_int_type(__c);
50 }
51 else
52 __ret = this->uflow();
53 return __ret;
54 }
55
56 template<typename _CharT, typename _Traits>
57 basic_streambuf<_CharT, _Traits>::int_type
58 basic_streambuf<_CharT, _Traits>::
59 sputbackc(char_type __c)
60 {
61 int_type __ret;
62 bool __testpos = _M_in_cur && _M_in_beg < _M_in_cur;
63 bool __testne = _M_in_cur && !traits_type::eq(__c, this->gptr()[-1]);
64 if (!__testpos || __testne)
65 __ret = pbackfail(traits_type::to_int_type(__c));
66 else
67 {
68 _M_in_cur_move(-1);
69 __ret = traits_type::to_int_type(*this->gptr());
70 }
71 return __ret;
72 }
73
74 template<typename _CharT, typename _Traits>
75 basic_streambuf<_CharT, _Traits>::int_type
76 basic_streambuf<_CharT, _Traits>::
77 sungetc()
78 {
79 int_type __ret;
80 if (_M_in_cur && _M_in_beg < _M_in_cur)
81 {
82 _M_in_cur_move(-1);
83 __ret = traits_type::to_int_type(*_M_in_cur);
84 }
85 else
86 __ret = this->pbackfail();
87 return __ret;
88 }
89
90 // Don't test against _M_buf + _M_buf_size, because _M_buf reflects
91 // allocated space, and on certain (rare but entirely legal)
92 // situations, there will be no allocated space yet the internal
93 // buffers will still be valid. (This happens if setp is used to set
94 // the internal buffer to say some externally-allocated sequence.)
95 template<typename _CharT, typename _Traits>
96 basic_streambuf<_CharT, _Traits>::int_type
97 basic_streambuf<_CharT, _Traits>::
98 sputc(char_type __c)
99 {
100 int_type __ret;
101 if (_M_out_buf_size())
102 {
103 *_M_out_cur = __c;
104 _M_out_cur_move(1);
105 __ret = traits_type::to_int_type(__c);
106 }
107 else
108 __ret = this->overflow(traits_type::to_int_type(__c));
109 return __ret;
110 }
111
112 template<typename _CharT, typename _Traits>
113 streamsize
114 basic_streambuf<_CharT, _Traits>::
115 xsgetn(char_type* __s, streamsize __n)
116 {
117 streamsize __ret = 0;
118 while (__ret < __n)
119 {
120 size_t __buf_len = _M_in_end - _M_in_cur;
121 if (__buf_len > 0)
122 {
123 size_t __remaining = __n - __ret;
124 size_t __len = min(__buf_len, __remaining);
125 traits_type::copy(__s, _M_in_cur, __len);
126 __ret += __len;
127 __s += __len;
128 _M_in_cur_move(__len);
129 }
130
131 if (__ret < __n)
132 {
133 int_type __c = this->uflow();
134 if (traits_type::eq_int_type(__c, traits_type::eof()))
135 break;
136 traits_type::assign(*__s++, traits_type::to_char_type(__c));
137 ++__ret;
138 }
139 }
140 return __ret;
141 }
142
143 // Don't test against _M_buf + _M_buf_size, because _M_buf reflects
144 // allocated space, and on certain (rare but entirely legal)
145 // situations, there will be no allocated space yet the internal
146 // buffers will still be valid. (This happens if setp is used to set
147 // the internal buffer to say some externally-allocated sequence.)
148 template<typename _CharT, typename _Traits>
149 streamsize
150 basic_streambuf<_CharT, _Traits>::
151 xsputn(const char_type* __s, streamsize __n)
152 {
153 streamsize __ret = 0;
154 while (__ret < __n)
155 {
156 off_type __buf_len = _M_out_buf_size();
157 if (__buf_len > 0)
158 {
159 off_type __remaining = __n - __ret;
160 off_type __len = min(__buf_len, __remaining);
161 traits_type::copy(_M_out_cur, __s, __len);
162 __ret += __len;
163 __s += __len;
164 _M_out_cur_move(__len);
165 }
166
167 if (__ret < __n)
168 {
169 int_type __c = traits_type::to_int_type(*__s);
170 int_type __overfc = this->overflow(__c);
171 if (traits_type::eq_int_type(__overfc, traits_type::eof()))
172 break;
173 ++__ret;
174 ++__s;
175 }
176 }
177 return __ret;
178 }
179
180 // Conceivably, this could be used to implement buffer-to-buffer
181 // copies, if this was ever desired in an un-ambiguous way by the
182 // standard. If so, then checks for __ios being zero would be
183 // necessary.
184 template<typename _CharT, typename _Traits>
185 streamsize
186 __copy_streambufs(basic_ios<_CharT, _Traits>& __ios,
187 basic_streambuf<_CharT, _Traits>* __sbin,
188 basic_streambuf<_CharT, _Traits>* __sbout)
189 {
190 typedef typename _Traits::int_type int_type;
191
192 streamsize __ret = 0;
193 streamsize __bufsize = __sbin->in_avail();
194 streamsize __xtrct;
195 bool __testput = __sbout->_M_mode & ios_base::out;
196 try {
197 while (__testput && __bufsize != -1)
198 {
199 __xtrct = __sbout->sputn(__sbin->gptr(), __bufsize);
200 __ret += __xtrct;
201 __sbin->_M_in_cur_move(__xtrct);
202 if (__xtrct == __bufsize)
203 {
204 int_type __c = __sbin->sgetc();
205 if (__c == _Traits::eof())
206 {
207 __ios.setstate(ios_base::eofbit);
208 break;
209 }
210 __bufsize = __sbin->in_avail();
211 }
212 else
213 break;
214 }
215 }
216 catch(exception& __fail) {
217 if ((__ios.exceptions() & ios_base::failbit) != 0)
218 __throw_exception_again;
219 }
220 return __ret;
221 }
222 } // namespace std
223
224 #endif // _CPP_BITS_STREAMBUF_TCC
225