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