]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/stdio_sync_filebuf.h
re PR libstdc++/12077 ([3.4 only] wcin.rdbuf()->in_avail() return value too high)
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / stdio_sync_filebuf.h
1 // Iostreams wrapper for stdio FILE* -*- C++ -*-
2
3 // Copyright (C) 2003, 2004 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 /** @file ext/stdiostream.h
31 * This file is a GNU extension to the Standard C++ Library.
32 */
33
34 #ifndef _STDIO_SYNC_FILEBUF_H
35 #define _STDIO_SYNC_FILEBUF_H 1
36
37 #pragma GCC system_header
38
39 #include <streambuf>
40 #include <unistd.h>
41 #include <cstdio>
42
43 #ifdef _GLIBCXX_USE_WCHAR_T
44 #include <cwchar>
45 #endif
46
47 namespace __gnu_cxx
48 {
49 template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
50 class stdio_sync_filebuf : public std::basic_streambuf<_CharT, _Traits>
51 {
52 public:
53 // Types:
54 typedef _CharT char_type;
55 typedef _Traits traits_type;
56 typedef typename traits_type::int_type int_type;
57 typedef typename traits_type::pos_type pos_type;
58 typedef typename traits_type::off_type off_type;
59
60 private:
61 // Underlying stdio FILE
62 std::__c_file* const _M_file;
63
64 // Last character gotten. This is used when pbackfail is
65 // called from basic_streambuf::sungetc()
66 int_type _M_unget_buf;
67
68 public:
69 explicit
70 stdio_sync_filebuf(std::__c_file* __f)
71 : _M_file(__f), _M_unget_buf(traits_type::eof())
72 { }
73
74 protected:
75 int_type
76 syncgetc();
77
78 int_type
79 syncungetc(int_type __c);
80
81 int_type
82 syncputc(int_type __c);
83
84 virtual int_type
85 underflow()
86 {
87 int_type __c = this->syncgetc();
88 return this->syncungetc(__c);
89 }
90
91 virtual int_type
92 uflow()
93 {
94 // Store the gotten character in case we need to unget it.
95 _M_unget_buf = this->syncgetc();
96 return _M_unget_buf;
97 }
98
99 virtual int_type
100 pbackfail(int_type __c = traits_type::eof())
101 {
102 int_type __ret;
103 const int_type __eof = traits_type::eof();
104
105 // Check if the unget or putback was requested
106 if (traits_type::eq_int_type(__c, __eof)) // unget
107 {
108 if (!traits_type::eq_int_type(_M_unget_buf, __eof))
109 __ret = this->syncungetc(_M_unget_buf);
110 else // buffer invalid, fail.
111 __ret = __eof;
112 }
113 else // putback
114 __ret = this->syncungetc(__c);
115
116 // The buffered character is no longer valid, discard it.
117 _M_unget_buf = __eof;
118 return __ret;
119 }
120
121 virtual std::streamsize
122 xsgetn(char_type* __s, std::streamsize __n);
123
124 virtual int_type
125 overflow(int_type __c = traits_type::eof())
126 {
127 int_type __ret;
128 if (traits_type::eq_int_type(__c, traits_type::eof()))
129 {
130 if (std::fflush(_M_file))
131 __ret = traits_type::eof();
132 else
133 __ret = traits_type::not_eof(__c);
134 }
135 else
136 __ret = this->syncputc(__c);
137 return __ret;
138 }
139
140 virtual std::streamsize
141 xsputn(const char_type* __s, std::streamsize __n);
142
143 virtual int
144 sync()
145 { return std::fflush(_M_file); }
146
147 virtual std::streampos
148 seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
149 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
150 {
151 std::streampos __ret(std::streamoff(-1));
152 int __whence;
153 if (__dir == std::ios_base::beg)
154 __whence = SEEK_SET;
155 else if (__dir == std::ios_base::cur)
156 __whence = SEEK_CUR;
157 else
158 __whence = SEEK_END;
159 #ifdef _GLIBCXX_USE_LFS
160 if (!fseeko64(_M_file, __off, __whence))
161 __ret = std::streampos(ftello64(_M_file));
162 #else
163 if (!fseek(_M_file, __off, __whence))
164 __ret = std::streampos(std::ftell(_M_file));
165 #endif
166 return __ret;
167 }
168
169 virtual std::streampos
170 seekpos(std::streampos __pos,
171 std::ios_base::openmode __mode =
172 std::ios_base::in | std::ios_base::out)
173 { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
174 };
175
176 template<>
177 inline stdio_sync_filebuf<char>::int_type
178 stdio_sync_filebuf<char>::syncgetc()
179 { return std::getc(_M_file); }
180
181 template<>
182 inline stdio_sync_filebuf<char>::int_type
183 stdio_sync_filebuf<char>::syncungetc(int_type __c)
184 { return std::ungetc(__c, _M_file); }
185
186 template<>
187 inline stdio_sync_filebuf<char>::int_type
188 stdio_sync_filebuf<char>::syncputc(int_type __c)
189 { return std::putc(__c, _M_file); }
190
191 template<>
192 inline std::streamsize
193 stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
194 {
195 std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
196 if (__ret > 0)
197 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
198 else
199 _M_unget_buf = traits_type::eof();
200 return __ret;
201 }
202
203 template<>
204 inline std::streamsize
205 stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
206 { return std::fwrite(__s, 1, __n, _M_file); }
207
208 #ifdef _GLIBCXX_USE_WCHAR_T
209 template<>
210 inline stdio_sync_filebuf<wchar_t>::int_type
211 stdio_sync_filebuf<wchar_t>::syncgetc()
212 { return std::getwc(_M_file); }
213
214 template<>
215 inline stdio_sync_filebuf<wchar_t>::int_type
216 stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
217 { return std::ungetwc(__c, _M_file); }
218
219 template<>
220 inline stdio_sync_filebuf<wchar_t>::int_type
221 stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
222 { return std::putwc(__c, _M_file); }
223
224 template<>
225 inline std::streamsize
226 stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
227 {
228 std::streamsize __ret = 0;
229 const int_type __eof = traits_type::eof();
230 while (__n--)
231 {
232 int_type __c = this->syncgetc();
233 if (traits_type::eq_int_type(__c, __eof))
234 break;
235 __s[__ret] = traits_type::to_char_type(__c);
236 ++__ret;
237 }
238
239 if (__ret > 0)
240 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
241 else
242 _M_unget_buf = traits_type::eof();
243 return __ret;
244 }
245
246 template<>
247 inline std::streamsize
248 stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s,
249 std::streamsize __n)
250 {
251 std::streamsize __ret = 0;
252 const int_type __eof = traits_type::eof();
253 while (__n--)
254 {
255 if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
256 break;
257 ++__ret;
258 }
259 return __ret;
260 }
261 #endif
262
263 #if _GLIBCXX_EXTERN_TEMPLATE
264 extern template class stdio_sync_filebuf<char>;
265 #ifdef _GLIBCXX_USE_WCHAR_T
266 extern template class stdio_sync_filebuf<wchar_t>;
267 #endif
268 #endif
269 } // namespace __gnu_cxx
270
271 #endif