re PR libstdc++/26181 (istream::operator>>(streambuf*) fails to set eofbit)
[gcc.git] / libstdc++-v3 / include / bits / streambuf.tcc
1 // Stream buffer classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 /** @file streambuf.tcc
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
34 */
35
36 //
37 // ISO C++ 14882: 27.5 Stream buffers
38 //
39
40 #ifndef _STREAMBUF_TCC
41 #define _STREAMBUF_TCC 1
42
43 #pragma GCC system_header
44
45 _GLIBCXX_BEGIN_NAMESPACE(std)
46
47 template<typename _CharT, typename _Traits>
48 streamsize
49 basic_streambuf<_CharT, _Traits>::
50 xsgetn(char_type* __s, streamsize __n)
51 {
52 streamsize __ret = 0;
53 while (__ret < __n)
54 {
55 const streamsize __buf_len = this->egptr() - this->gptr();
56 if (__buf_len)
57 {
58 const streamsize __remaining = __n - __ret;
59 const streamsize __len = std::min(__buf_len, __remaining);
60 traits_type::copy(__s, this->gptr(), __len);
61 __ret += __len;
62 __s += __len;
63 this->gbump(__len);
64 }
65
66 if (__ret < __n)
67 {
68 const int_type __c = this->uflow();
69 if (!traits_type::eq_int_type(__c, traits_type::eof()))
70 {
71 traits_type::assign(*__s++, traits_type::to_char_type(__c));
72 ++__ret;
73 }
74 else
75 break;
76 }
77 }
78 return __ret;
79 }
80
81 template<typename _CharT, typename _Traits>
82 streamsize
83 basic_streambuf<_CharT, _Traits>::
84 xsputn(const char_type* __s, streamsize __n)
85 {
86 streamsize __ret = 0;
87 while (__ret < __n)
88 {
89 const streamsize __buf_len = this->epptr() - this->pptr();
90 if (__buf_len)
91 {
92 const streamsize __remaining = __n - __ret;
93 const streamsize __len = std::min(__buf_len, __remaining);
94 traits_type::copy(this->pptr(), __s, __len);
95 __ret += __len;
96 __s += __len;
97 this->pbump(__len);
98 }
99
100 if (__ret < __n)
101 {
102 int_type __c = this->overflow(traits_type::to_int_type(*__s));
103 if (!traits_type::eq_int_type(__c, traits_type::eof()))
104 {
105 ++__ret;
106 ++__s;
107 }
108 else
109 break;
110 }
111 }
112 return __ret;
113 }
114
115 // Conceivably, this could be used to implement buffer-to-buffer
116 // copies, if this was ever desired in an un-ambiguous way by the
117 // standard.
118 template<typename _CharT, typename _Traits>
119 streamsize
120 __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>* __sbin,
121 basic_streambuf<_CharT, _Traits>* __sbout,
122 bool& __ineof)
123 {
124 streamsize __ret = 0;
125 __ineof = true;
126 typename _Traits::int_type __c = __sbin->sgetc();
127 while (!_Traits::eq_int_type(__c, _Traits::eof()))
128 {
129 __c = __sbout->sputc(_Traits::to_char_type(__c));
130 if (_Traits::eq_int_type(__c, _Traits::eof()))
131 {
132 __ineof = false;
133 break;
134 }
135 ++__ret;
136 __c = __sbin->snextc();
137 }
138 return __ret;
139 }
140
141 template<typename _CharT, typename _Traits>
142 inline streamsize
143 __copy_streambufs(basic_streambuf<_CharT, _Traits>* __sbin,
144 basic_streambuf<_CharT, _Traits>* __sbout)
145 {
146 bool __ineof;
147 return __copy_streambufs_eof(__sbin, __sbout, __ineof);
148 }
149
150 // Inhibit implicit instantiations for required instantiations,
151 // which are defined via explicit instantiations elsewhere.
152 // NB: This syntax is a GNU extension.
153 #if _GLIBCXX_EXTERN_TEMPLATE
154 extern template class basic_streambuf<char>;
155 extern template
156 streamsize
157 __copy_streambufs(basic_streambuf<char>*,
158 basic_streambuf<char>*);
159 extern template
160 streamsize
161 __copy_streambufs_eof(basic_streambuf<char>*,
162 basic_streambuf<char>*, bool&);
163
164 #ifdef _GLIBCXX_USE_WCHAR_T
165 extern template class basic_streambuf<wchar_t>;
166 extern template
167 streamsize
168 __copy_streambufs(basic_streambuf<wchar_t>*,
169 basic_streambuf<wchar_t>*);
170 extern template
171 streamsize
172 __copy_streambufs_eof(basic_streambuf<wchar_t>*,
173 basic_streambuf<wchar_t>*, bool&);
174 #endif
175 #endif
176
177 _GLIBCXX_END_NAMESPACE
178
179 #endif