]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/include/bits/streambuf.tcc
a16169ad5efbfd6aa638ea68018594daf523e8b2
[gcc.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 streamsize
44 basic_streambuf<_CharT, _Traits>::
45 xsgetn(char_type* __s, streamsize __n)
46 {
47 streamsize __ret = 0;
48 while (__ret < __n)
49 {
50 const size_t __buf_len = this->egptr() - this->gptr();
51 if (__buf_len)
52 {
53 const size_t __remaining = __n - __ret;
54 const size_t __len = std::min(__buf_len, __remaining);
55 traits_type::copy(__s, this->gptr(), __len);
56 __ret += __len;
57 __s += __len;
58 this->gbump(__len);
59 }
60
61 if (__ret < __n)
62 {
63 const int_type __c = this->uflow();
64 if (!traits_type::eq_int_type(__c, traits_type::eof()))
65 {
66 traits_type::assign(*__s++, traits_type::to_char_type(__c));
67 ++__ret;
68 }
69 else
70 break;
71 }
72 }
73 return __ret;
74 }
75
76 template<typename _CharT, typename _Traits>
77 streamsize
78 basic_streambuf<_CharT, _Traits>::
79 xsputn(const char_type* __s, streamsize __n)
80 {
81 streamsize __ret = 0;
82 while (__ret < __n)
83 {
84 const size_t __buf_len = this->epptr() - this->pptr();
85 if (__buf_len)
86 {
87 const size_t __remaining = __n - __ret;
88 const size_t __len = std::min(__buf_len, __remaining);
89 traits_type::copy(this->pptr(), __s, __len);
90 __ret += __len;
91 __s += __len;
92 this->pbump(__len);
93 }
94
95 if (__ret < __n)
96 {
97 int_type __c = this->overflow(traits_type::to_int_type(*__s));
98 if (!traits_type::eq_int_type(__c, traits_type::eof()))
99 {
100 ++__ret;
101 ++__s;
102 }
103 else
104 break;
105 }
106 }
107 return __ret;
108 }
109
110 // Conceivably, this could be used to implement buffer-to-buffer
111 // copies, if this was ever desired in an un-ambiguous way by the
112 // standard. If so, then checks for __ios being zero would be
113 // necessary.
114 template<typename _CharT, typename _Traits>
115 streamsize
116 __copy_streambufs(basic_ios<_CharT, _Traits>& __ios,
117 basic_streambuf<_CharT, _Traits>* __sbin,
118 basic_streambuf<_CharT, _Traits>* __sbout)
119 {
120 streamsize __ret = 0;
121 try
122 {
123 typename _Traits::int_type __c = __sbin->sgetc();
124 while (!_Traits::eq_int_type(__c, _Traits::eof()))
125 {
126 const size_t __n = __sbin->egptr() - __sbin->gptr();
127 if (__n > 1)
128 {
129 const size_t __wrote = __sbout->sputn(__sbin->gptr(),
130 __n);
131 __sbin->gbump(__wrote);
132 __ret += __wrote;
133 if (__wrote < __n)
134 break;
135 __c = __sbin->underflow();
136 }
137 else
138 {
139 __c = __sbout->sputc(_Traits::to_char_type(__c));
140 if (_Traits::eq_int_type(__c, _Traits::eof()))
141 break;
142 ++__ret;
143 __c = __sbin->snextc();
144 }
145 }
146 }
147 catch(exception& __fail)
148 {
149 __ios.setstate(ios_base::failbit);
150 if ((__ios.exceptions() & ios_base::failbit) != 0)
151 __throw_exception_again;
152 }
153 return __ret;
154 }
155
156 // Inhibit implicit instantiations for required instantiations,
157 // which are defined via explicit instantiations elsewhere.
158 // NB: This syntax is a GNU extension.
159 #if _GLIBCPP_EXTERN_TEMPLATE
160 extern template class basic_streambuf<char>;
161 extern template
162 streamsize
163 __copy_streambufs(basic_ios<char>&, basic_streambuf<char>*,
164 basic_streambuf<char>*);
165
166 #ifdef _GLIBCPP_USE_WCHAR_T
167 extern template class basic_streambuf<wchar_t>;
168 extern template
169 streamsize
170 __copy_streambufs(basic_ios<wchar_t>&, basic_streambuf<wchar_t>*,
171 basic_streambuf<wchar_t>*);
172 #endif
173 #endif
174 } // namespace std
175
176 #endif
This page took 0.043937 seconds and 4 git commands to generate.