]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/stdio_sync_filebuf.h
re PR libstdc++/42460 (man page errors for generated libstdc++ man pages)
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / stdio_sync_filebuf.h
1 // Iostreams wrapper for stdio FILE* -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /** @file ext/stdio_sync_filebuf.h
27 * This file is a GNU extension to the Standard C++ Library.
28 */
29
30 #ifndef _STDIO_SYNC_FILEBUF_H
31 #define _STDIO_SYNC_FILEBUF_H 1
32
33 #pragma GCC system_header
34
35 #include <streambuf>
36 #include <unistd.h>
37 #include <cstdio>
38 #include <bits/c++io.h> // For __c_file
39
40 #ifdef _GLIBCXX_USE_WCHAR_T
41 #include <cwchar>
42 #endif
43
44 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
45
46 /// class stdio_sync_filebuf.
47 template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
48 class stdio_sync_filebuf : public std::basic_streambuf<_CharT, _Traits>
49 {
50 public:
51 // Types:
52 typedef _CharT char_type;
53 typedef _Traits traits_type;
54 typedef typename traits_type::int_type int_type;
55 typedef typename traits_type::pos_type pos_type;
56 typedef typename traits_type::off_type off_type;
57
58 private:
59 // Underlying stdio FILE
60 std::__c_file* const _M_file;
61
62 // Last character gotten. This is used when pbackfail is
63 // called from basic_streambuf::sungetc()
64 int_type _M_unget_buf;
65
66 public:
67 explicit
68 stdio_sync_filebuf(std::__c_file* __f)
69 : _M_file(__f), _M_unget_buf(traits_type::eof())
70 { }
71
72 /**
73 * @return The underlying FILE*.
74 *
75 * This function can be used to access the underlying C file pointer.
76 * Note that there is no way for the library to track what you do
77 * with the file, so be careful.
78 */
79 std::__c_file* const
80 file() { return this->_M_file; }
81
82 protected:
83 int_type
84 syncgetc();
85
86 int_type
87 syncungetc(int_type __c);
88
89 int_type
90 syncputc(int_type __c);
91
92 virtual int_type
93 underflow()
94 {
95 int_type __c = this->syncgetc();
96 return this->syncungetc(__c);
97 }
98
99 virtual int_type
100 uflow()
101 {
102 // Store the gotten character in case we need to unget it.
103 _M_unget_buf = this->syncgetc();
104 return _M_unget_buf;
105 }
106
107 virtual int_type
108 pbackfail(int_type __c = traits_type::eof())
109 {
110 int_type __ret;
111 const int_type __eof = traits_type::eof();
112
113 // Check if the unget or putback was requested
114 if (traits_type::eq_int_type(__c, __eof)) // unget
115 {
116 if (!traits_type::eq_int_type(_M_unget_buf, __eof))
117 __ret = this->syncungetc(_M_unget_buf);
118 else // buffer invalid, fail.
119 __ret = __eof;
120 }
121 else // putback
122 __ret = this->syncungetc(__c);
123
124 // The buffered character is no longer valid, discard it.
125 _M_unget_buf = __eof;
126 return __ret;
127 }
128
129 virtual std::streamsize
130 xsgetn(char_type* __s, std::streamsize __n);
131
132 virtual int_type
133 overflow(int_type __c = traits_type::eof())
134 {
135 int_type __ret;
136 if (traits_type::eq_int_type(__c, traits_type::eof()))
137 {
138 if (std::fflush(_M_file))
139 __ret = traits_type::eof();
140 else
141 __ret = traits_type::not_eof(__c);
142 }
143 else
144 __ret = this->syncputc(__c);
145 return __ret;
146 }
147
148 virtual std::streamsize
149 xsputn(const char_type* __s, std::streamsize __n);
150
151 virtual int
152 sync()
153 { return std::fflush(_M_file); }
154
155 virtual std::streampos
156 seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
157 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
158 {
159 std::streampos __ret(std::streamoff(-1));
160 int __whence;
161 if (__dir == std::ios_base::beg)
162 __whence = SEEK_SET;
163 else if (__dir == std::ios_base::cur)
164 __whence = SEEK_CUR;
165 else
166 __whence = SEEK_END;
167 #ifdef _GLIBCXX_USE_LFS
168 if (!fseeko64(_M_file, __off, __whence))
169 __ret = std::streampos(ftello64(_M_file));
170 #else
171 if (!fseek(_M_file, __off, __whence))
172 __ret = std::streampos(std::ftell(_M_file));
173 #endif
174 return __ret;
175 }
176
177 virtual std::streampos
178 seekpos(std::streampos __pos,
179 std::ios_base::openmode __mode =
180 std::ios_base::in | std::ios_base::out)
181 { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
182 };
183
184 template<>
185 inline stdio_sync_filebuf<char>::int_type
186 stdio_sync_filebuf<char>::syncgetc()
187 { return std::getc(_M_file); }
188
189 template<>
190 inline stdio_sync_filebuf<char>::int_type
191 stdio_sync_filebuf<char>::syncungetc(int_type __c)
192 { return std::ungetc(__c, _M_file); }
193
194 template<>
195 inline stdio_sync_filebuf<char>::int_type
196 stdio_sync_filebuf<char>::syncputc(int_type __c)
197 { return std::putc(__c, _M_file); }
198
199 template<>
200 inline std::streamsize
201 stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
202 {
203 std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
204 if (__ret > 0)
205 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
206 else
207 _M_unget_buf = traits_type::eof();
208 return __ret;
209 }
210
211 template<>
212 inline std::streamsize
213 stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
214 { return std::fwrite(__s, 1, __n, _M_file); }
215
216 #ifdef _GLIBCXX_USE_WCHAR_T
217 template<>
218 inline stdio_sync_filebuf<wchar_t>::int_type
219 stdio_sync_filebuf<wchar_t>::syncgetc()
220 { return std::getwc(_M_file); }
221
222 template<>
223 inline stdio_sync_filebuf<wchar_t>::int_type
224 stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
225 { return std::ungetwc(__c, _M_file); }
226
227 template<>
228 inline stdio_sync_filebuf<wchar_t>::int_type
229 stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
230 { return std::putwc(__c, _M_file); }
231
232 template<>
233 inline std::streamsize
234 stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
235 {
236 std::streamsize __ret = 0;
237 const int_type __eof = traits_type::eof();
238 while (__n--)
239 {
240 int_type __c = this->syncgetc();
241 if (traits_type::eq_int_type(__c, __eof))
242 break;
243 __s[__ret] = traits_type::to_char_type(__c);
244 ++__ret;
245 }
246
247 if (__ret > 0)
248 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
249 else
250 _M_unget_buf = traits_type::eof();
251 return __ret;
252 }
253
254 template<>
255 inline std::streamsize
256 stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s,
257 std::streamsize __n)
258 {
259 std::streamsize __ret = 0;
260 const int_type __eof = traits_type::eof();
261 while (__n--)
262 {
263 if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
264 break;
265 ++__ret;
266 }
267 return __ret;
268 }
269 #endif
270
271 #if _GLIBCXX_EXTERN_TEMPLATE
272 extern template class stdio_sync_filebuf<char>;
273 #ifdef _GLIBCXX_USE_WCHAR_T
274 extern template class stdio_sync_filebuf<wchar_t>;
275 #endif
276 #endif
277
278 _GLIBCXX_END_NAMESPACE
279
280 #endif