1 // Iostreams wrapper for stdio FILE* -*- C++ -*-
3 // Copyright (C) 2003-2015 Free Software Foundation, Inc.
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 3, or (at your option)
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.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file ext/stdio_sync_filebuf.h
26 * This file is a GNU extension to the Standard C++ Library.
29 #ifndef _STDIO_SYNC_FILEBUF_H
30 #define _STDIO_SYNC_FILEBUF_H 1
32 #pragma GCC system_header
37 #include <bits/c++io.h> // For __c_file
38 #include <bits/move.h> // For __exchange
40 #ifdef _GLIBCXX_USE_WCHAR_T
44 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 * @brief Provides a layer of compatibility for C.
52 * This GNU extension provides extensions for working with standard
53 * C FILE*'s. It must be instantiated by the user with the type of
54 * character used in the file stream, e.g., stdio_filebuf<char>.
56 template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
57 class stdio_sync_filebuf : public std::basic_streambuf<_CharT, _Traits>
61 typedef _CharT char_type;
62 typedef _Traits traits_type;
63 typedef typename traits_type::int_type int_type;
64 typedef typename traits_type::pos_type pos_type;
65 typedef typename traits_type::off_type off_type;
68 typedef std::basic_streambuf<_CharT, _Traits> __streambuf_type;
70 // Underlying stdio FILE
71 std::__c_file* _M_file;
73 // Last character gotten. This is used when pbackfail is
74 // called from basic_streambuf::sungetc()
75 int_type _M_unget_buf;
79 stdio_sync_filebuf(std::__c_file* __f)
80 : _M_file(__f), _M_unget_buf(traits_type::eof())
83 #if __cplusplus >= 201103L
84 stdio_sync_filebuf(stdio_sync_filebuf&& __fb) noexcept
85 : __streambuf_type(std::move(__fb)),
86 _M_file(__fb._M_file), _M_unget_buf(__fb._M_unget_buf)
88 __fb._M_file = nullptr;
89 __fb._M_unget_buf = traits_type::eof();
93 operator=(stdio_sync_filebuf&& __fb) noexcept
95 __streambuf_type::operator=(__fb);
96 _M_file = std::__exchange(__fb._M_file, nullptr);
97 _M_unget_buf = std::__exchange(__fb._M_unget_buf, traits_type::eof());
102 swap(stdio_sync_filebuf& __fb)
104 __streambuf_type::swap(__fb);
105 std::swap(_M_file, __fb._M_file);
106 std::swap(_M_unget_buf, __fb._M_unget_buf);
111 * @return The underlying FILE*.
113 * This function can be used to access the underlying C file pointer.
114 * Note that there is no way for the library to track what you do
115 * with the file, so be careful.
118 file() { return this->_M_file; }
125 syncungetc(int_type __c);
128 syncputc(int_type __c);
133 int_type __c = this->syncgetc();
134 return this->syncungetc(__c);
140 // Store the gotten character in case we need to unget it.
141 _M_unget_buf = this->syncgetc();
146 pbackfail(int_type __c = traits_type::eof())
149 const int_type __eof = traits_type::eof();
151 // Check if the unget or putback was requested
152 if (traits_type::eq_int_type(__c, __eof)) // unget
154 if (!traits_type::eq_int_type(_M_unget_buf, __eof))
155 __ret = this->syncungetc(_M_unget_buf);
156 else // buffer invalid, fail.
160 __ret = this->syncungetc(__c);
162 // The buffered character is no longer valid, discard it.
163 _M_unget_buf = __eof;
167 virtual std::streamsize
168 xsgetn(char_type* __s, std::streamsize __n);
171 overflow(int_type __c = traits_type::eof())
174 if (traits_type::eq_int_type(__c, traits_type::eof()))
176 if (std::fflush(_M_file))
177 __ret = traits_type::eof();
179 __ret = traits_type::not_eof(__c);
182 __ret = this->syncputc(__c);
186 virtual std::streamsize
187 xsputn(const char_type* __s, std::streamsize __n);
191 { return std::fflush(_M_file); }
193 virtual std::streampos
194 seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
195 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
197 std::streampos __ret(std::streamoff(-1));
199 if (__dir == std::ios_base::beg)
201 else if (__dir == std::ios_base::cur)
205 #ifdef _GLIBCXX_USE_LFS
206 if (!fseeko64(_M_file, __off, __whence))
207 __ret = std::streampos(ftello64(_M_file));
209 if (!fseek(_M_file, __off, __whence))
210 __ret = std::streampos(std::ftell(_M_file));
215 virtual std::streampos
216 seekpos(std::streampos __pos,
217 std::ios_base::openmode __mode =
218 std::ios_base::in | std::ios_base::out)
219 { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
223 inline stdio_sync_filebuf<char>::int_type
224 stdio_sync_filebuf<char>::syncgetc()
225 { return std::getc(_M_file); }
228 inline stdio_sync_filebuf<char>::int_type
229 stdio_sync_filebuf<char>::syncungetc(int_type __c)
230 { return std::ungetc(__c, _M_file); }
233 inline stdio_sync_filebuf<char>::int_type
234 stdio_sync_filebuf<char>::syncputc(int_type __c)
235 { return std::putc(__c, _M_file); }
238 inline std::streamsize
239 stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
241 std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
243 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
245 _M_unget_buf = traits_type::eof();
250 inline std::streamsize
251 stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
252 { return std::fwrite(__s, 1, __n, _M_file); }
254 #ifdef _GLIBCXX_USE_WCHAR_T
256 inline stdio_sync_filebuf<wchar_t>::int_type
257 stdio_sync_filebuf<wchar_t>::syncgetc()
258 { return std::getwc(_M_file); }
261 inline stdio_sync_filebuf<wchar_t>::int_type
262 stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
263 { return std::ungetwc(__c, _M_file); }
266 inline stdio_sync_filebuf<wchar_t>::int_type
267 stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
268 { return std::putwc(__c, _M_file); }
271 inline std::streamsize
272 stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
274 std::streamsize __ret = 0;
275 const int_type __eof = traits_type::eof();
278 int_type __c = this->syncgetc();
279 if (traits_type::eq_int_type(__c, __eof))
281 __s[__ret] = traits_type::to_char_type(__c);
286 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
288 _M_unget_buf = traits_type::eof();
293 inline std::streamsize
294 stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s,
297 std::streamsize __ret = 0;
298 const int_type __eof = traits_type::eof();
301 if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
309 #if _GLIBCXX_EXTERN_TEMPLATE
310 extern template class stdio_sync_filebuf<char>;
311 #ifdef _GLIBCXX_USE_WCHAR_T
312 extern template class stdio_sync_filebuf<wchar_t>;
316 _GLIBCXX_END_NAMESPACE_VERSION