1 // Iostreams wrapper for stdio FILE* -*- C++ -*-
3 // Copyright (C) 2003-2022 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
34 #include <bits/requires_hosted.h> // GNU extensions are currently omitted
38 #include <bits/c++io.h> // For __c_file
39 #include <bits/move.h> // For __exchange
41 #ifdef _GLIBCXX_USE_WCHAR_T
45 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 * @brief Provides a layer of compatibility for C.
53 * This GNU extension provides extensions for working with standard
54 * C FILE*'s. It must be instantiated by the user with the type of
55 * character used in the file stream, e.g., stdio_filebuf<char>.
57 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
> >
58 class stdio_sync_filebuf
: public std::basic_streambuf
<_CharT
, _Traits
>
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
;
69 typedef std::basic_streambuf
<_CharT
, _Traits
> __streambuf_type
;
71 // Underlying stdio FILE
72 std::__c_file
* _M_file
;
74 // Last character gotten. This is used when pbackfail is
75 // called from basic_streambuf::sungetc()
76 int_type _M_unget_buf
;
80 stdio_sync_filebuf(std::__c_file
* __f
)
81 : _M_file(__f
), _M_unget_buf(traits_type::eof())
84 #if __cplusplus >= 201103L
85 stdio_sync_filebuf(stdio_sync_filebuf
&& __fb
) noexcept
86 : __streambuf_type(std::move(__fb
)),
87 _M_file(__fb
._M_file
), _M_unget_buf(__fb
._M_unget_buf
)
89 __fb
._M_file
= nullptr;
90 __fb
._M_unget_buf
= traits_type::eof();
94 operator=(stdio_sync_filebuf
&& __fb
) noexcept
96 __streambuf_type::operator=(__fb
);
97 _M_file
= std::__exchange(__fb
._M_file
, nullptr);
98 _M_unget_buf
= std::__exchange(__fb
._M_unget_buf
, traits_type::eof());
103 swap(stdio_sync_filebuf
& __fb
)
105 __streambuf_type::swap(__fb
);
106 std::swap(_M_file
, __fb
._M_file
);
107 std::swap(_M_unget_buf
, __fb
._M_unget_buf
);
112 * @return The underlying FILE*.
114 * This function can be used to access the underlying C file pointer.
115 * Note that there is no way for the library to track what you do
116 * with the file, so be careful.
119 file() { return this->_M_file
; }
126 syncungetc(int_type __c
);
129 syncputc(int_type __c
);
134 int_type __c
= this->syncgetc();
135 return this->syncungetc(__c
);
141 // Store the gotten character in case we need to unget it.
142 _M_unget_buf
= this->syncgetc();
147 pbackfail(int_type __c
= traits_type::eof())
150 const int_type __eof
= traits_type::eof();
152 // Check if the unget or putback was requested
153 if (traits_type::eq_int_type(__c
, __eof
)) // unget
155 if (!traits_type::eq_int_type(_M_unget_buf
, __eof
))
156 __ret
= this->syncungetc(_M_unget_buf
);
157 else // buffer invalid, fail.
161 __ret
= this->syncungetc(__c
);
163 // The buffered character is no longer valid, discard it.
164 _M_unget_buf
= __eof
;
168 virtual std::streamsize
169 xsgetn(char_type
* __s
, std::streamsize __n
);
172 overflow(int_type __c
= traits_type::eof())
175 if (traits_type::eq_int_type(__c
, traits_type::eof()))
177 if (std::fflush(_M_file
))
178 __ret
= traits_type::eof();
180 __ret
= traits_type::not_eof(__c
);
183 __ret
= this->syncputc(__c
);
187 virtual std::streamsize
188 xsputn(const char_type
* __s
, std::streamsize __n
);
192 { return std::fflush(_M_file
); }
194 virtual std::streampos
195 seekoff(std::streamoff __off
, std::ios_base::seekdir __dir
,
196 std::ios_base::openmode
= std::ios_base::in
| std::ios_base::out
)
198 std::streampos
__ret(std::streamoff(-1));
200 if (__dir
== std::ios_base::beg
)
202 else if (__dir
== std::ios_base::cur
)
206 #ifdef _GLIBCXX_USE_LFS
207 if (!fseeko64(_M_file
, __off
, __whence
))
208 __ret
= std::streampos(ftello64(_M_file
));
210 if (!fseek(_M_file
, __off
, __whence
))
211 __ret
= std::streampos(std::ftell(_M_file
));
216 virtual std::streampos
217 seekpos(std::streampos __pos
,
218 std::ios_base::openmode __mode
=
219 std::ios_base::in
| std::ios_base::out
)
220 { return seekoff(std::streamoff(__pos
), std::ios_base::beg
, __mode
); }
224 inline stdio_sync_filebuf
<char>::int_type
225 stdio_sync_filebuf
<char>::syncgetc()
226 { return std::getc(_M_file
); }
229 inline stdio_sync_filebuf
<char>::int_type
230 stdio_sync_filebuf
<char>::syncungetc(int_type __c
)
231 { return std::ungetc(__c
, _M_file
); }
234 inline stdio_sync_filebuf
<char>::int_type
235 stdio_sync_filebuf
<char>::syncputc(int_type __c
)
236 { return std::putc(__c
, _M_file
); }
239 inline std::streamsize
240 stdio_sync_filebuf
<char>::xsgetn(char* __s
, std::streamsize __n
)
242 std::streamsize __ret
= std::fread(__s
, 1, __n
, _M_file
);
244 _M_unget_buf
= traits_type::to_int_type(__s
[__ret
- 1]);
246 _M_unget_buf
= traits_type::eof();
251 inline std::streamsize
252 stdio_sync_filebuf
<char>::xsputn(const char* __s
, std::streamsize __n
)
253 { return std::fwrite(__s
, 1, __n
, _M_file
); }
255 #ifdef _GLIBCXX_USE_WCHAR_T
257 inline stdio_sync_filebuf
<wchar_t>::int_type
258 stdio_sync_filebuf
<wchar_t>::syncgetc()
259 { return std::getwc(_M_file
); }
262 inline stdio_sync_filebuf
<wchar_t>::int_type
263 stdio_sync_filebuf
<wchar_t>::syncungetc(int_type __c
)
264 { return std::ungetwc(__c
, _M_file
); }
267 inline stdio_sync_filebuf
<wchar_t>::int_type
268 stdio_sync_filebuf
<wchar_t>::syncputc(int_type __c
)
269 { return std::putwc(__c
, _M_file
); }
272 inline std::streamsize
273 stdio_sync_filebuf
<wchar_t>::xsgetn(wchar_t* __s
, std::streamsize __n
)
275 std::streamsize __ret
= 0;
276 const int_type __eof
= traits_type::eof();
279 int_type __c
= this->syncgetc();
280 if (traits_type::eq_int_type(__c
, __eof
))
282 __s
[__ret
] = traits_type::to_char_type(__c
);
287 _M_unget_buf
= traits_type::to_int_type(__s
[__ret
- 1]);
289 _M_unget_buf
= traits_type::eof();
294 inline std::streamsize
295 stdio_sync_filebuf
<wchar_t>::xsputn(const wchar_t* __s
,
298 std::streamsize __ret
= 0;
299 const int_type __eof
= traits_type::eof();
302 if (traits_type::eq_int_type(this->syncputc(*__s
++), __eof
))
310 #if _GLIBCXX_EXTERN_TEMPLATE
311 extern template class stdio_sync_filebuf
<char>;
312 #ifdef _GLIBCXX_USE_WCHAR_T
313 extern template class stdio_sync_filebuf
<wchar_t>;
317 _GLIBCXX_END_NAMESPACE_VERSION