OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.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     typename basic_streambuf<_CharT, _Traits>::int_type
44     basic_streambuf<_CharT, _Traits>::
45     sbumpc()
46     {
47       int_type __ret;
48       if (_M_in_cur < _M_in_end)
49         {
50           __ret = traits_type::to_int_type(*this->_M_in_cur);
51           _M_move_in_cur(1);
52         }
53       else 
54         __ret = this->uflow();
55       return __ret;
56     }
57
58   template<typename _CharT, typename _Traits>
59     typename basic_streambuf<_CharT, _Traits>::int_type
60     basic_streambuf<_CharT, _Traits>::
61     sputbackc(char_type __c) 
62     {
63       int_type __ret;
64       const bool __testpos = _M_in_beg < _M_in_cur;
65       if (!__testpos || !traits_type::eq(__c, this->_M_in_cur[-1]))
66         __ret = this->pbackfail(traits_type::to_int_type(__c));
67       else 
68         {
69           _M_move_in_cur(-1);
70           __ret = traits_type::to_int_type(*this->_M_in_cur);
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_beg < _M_in_cur)
82         {
83           _M_move_in_cur(-1);
84           __ret = traits_type::to_int_type(*_M_in_cur);
85         }
86       else 
87         __ret = this->pbackfail();
88       return __ret;
89     }
90
91   template<typename _CharT, typename _Traits>
92     typename basic_streambuf<_CharT, _Traits>::int_type
93     basic_streambuf<_CharT, _Traits>::
94     sputc(char_type __c)
95     {
96       int_type __ret;
97       if (_M_out_cur < _M_out_end)
98         {
99           *_M_out_cur = __c;
100           _M_move_out_cur(1);
101           __ret = traits_type::to_int_type(__c);
102         }
103       else
104         __ret = this->overflow(traits_type::to_int_type(__c));
105       return __ret;
106     }
107
108   template<typename _CharT, typename _Traits>
109     streamsize
110     basic_streambuf<_CharT, _Traits>::
111     xsgetn(char_type* __s, streamsize __n)
112     {
113       streamsize __ret = 0;
114       while (__ret < __n)
115         {
116           const size_t __buf_len = _M_in_end - _M_in_cur;
117           if (__buf_len)
118             {
119               const size_t __remaining = __n - __ret;
120               const size_t __len = std::min(__buf_len, __remaining);
121               traits_type::copy(__s, _M_in_cur, __len);
122               __ret += __len;
123               __s += __len;
124               _M_move_in_cur(__len);
125             }
126           
127           if (__ret < __n)
128             {
129               const int_type __c = this->uflow();  
130               if (!traits_type::eq_int_type(__c, traits_type::eof()))
131                 {
132                   traits_type::assign(*__s++, traits_type::to_char_type(__c));
133                   ++__ret;
134                 }
135               else
136                 break;
137             }
138         }
139       return __ret;
140     }
141
142   template<typename _CharT, typename _Traits>
143     streamsize
144     basic_streambuf<_CharT, _Traits>::
145     xsputn(const char_type* __s, streamsize __n)
146     {
147       streamsize __ret = 0;
148       while (__ret < __n)
149         {
150           const size_t __buf_len = _M_out_end - _M_out_cur;
151           if (__buf_len)
152             {
153               const size_t __remaining = __n - __ret;
154               const size_t __len = std::min(__buf_len, __remaining);
155               traits_type::copy(_M_out_cur, __s, __len);
156               __ret += __len;
157               __s += __len;
158               _M_move_out_cur(__len);
159             }
160
161           if (__ret < __n)
162             {
163               int_type __c = this->overflow(traits_type::to_int_type(*__s));
164               if (!traits_type::eq_int_type(__c, traits_type::eof()))
165                 {
166                   ++__ret;
167                   ++__s;
168                 }
169               else
170                 break;
171             }
172         }
173       return __ret;
174     }
175
176   // Conceivably, this could be used to implement buffer-to-buffer
177   // copies, if this was ever desired in an un-ambiguous way by the
178   // standard. If so, then checks for __ios being zero would be
179   // necessary.
180   template<typename _CharT, typename _Traits>
181     streamsize
182     __copy_streambufs(basic_ios<_CharT, _Traits>& __ios,
183                       basic_streambuf<_CharT, _Traits>* __sbin,
184                       basic_streambuf<_CharT, _Traits>* __sbout) 
185     {
186       streamsize __ret = 0;
187       try 
188         {
189           typename _Traits::int_type __c = __sbin->sgetc();
190           while (!_Traits::eq_int_type(__c, _Traits::eof()))
191             {
192               const size_t __n = __sbin->_M_in_end - __sbin->_M_in_cur;
193               if (__n > 1)
194                 {
195                   const size_t __wrote = __sbout->sputn(__sbin->_M_in_cur,
196                                                         __n);
197                   __sbin->_M_move_in_cur(__wrote);
198                   __ret += __wrote;
199                   if (__wrote < __n)
200                     break;
201                   __c = __sbin->underflow();
202                 }
203               else 
204                 {
205                   __c = __sbout->sputc(_Traits::to_char_type(__c));
206                   if (_Traits::eq_int_type(__c, _Traits::eof()))
207                     break;
208                   ++__ret;
209                   __c = __sbin->snextc();
210                 }
211             }
212         }
213       catch(exception& __fail) 
214         {
215           __ios.setstate(ios_base::failbit);
216           if ((__ios.exceptions() & ios_base::failbit) != 0)
217             __throw_exception_again;
218         }
219       return __ret;
220     }
221
222   // Inhibit implicit instantiations for required instantiations,
223   // which are defined via explicit instantiations elsewhere.  
224   // NB:  This syntax is a GNU extension.
225 #if _GLIBCPP_EXTERN_TEMPLATE
226   extern template class basic_streambuf<char>;
227   extern template
228     streamsize
229     __copy_streambufs(basic_ios<char>&, basic_streambuf<char>*,
230                       basic_streambuf<char>*); 
231
232 #ifdef _GLIBCPP_USE_WCHAR_T
233   extern template class basic_streambuf<wchar_t>;
234   extern template
235     streamsize
236     __copy_streambufs(basic_ios<wchar_t>&, basic_streambuf<wchar_t>*,
237                       basic_streambuf<wchar_t>*); 
238 #endif
239 #endif
240 } // namespace std
241
242 #endif