29 #ifndef _STDIO_SYNC_FILEBUF_H
30 #define _STDIO_SYNC_FILEBUF_H 1
32 #pragma GCC system_header
37 #include <bits/c++io.h>
39 #ifdef _GLIBCXX_USE_WCHAR_T
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 template<
typename _CharT,
typename _Traits = std::
char_traits<_CharT> >
56 class stdio_sync_filebuf :
public std::basic_streambuf<_CharT, _Traits>
60 typedef _CharT char_type;
61 typedef _Traits traits_type;
62 typedef typename traits_type::int_type int_type;
63 typedef typename traits_type::pos_type pos_type;
64 typedef typename traits_type::off_type off_type;
68 std::__c_file*
const _M_file;
72 int_type _M_unget_buf;
76 stdio_sync_filebuf(std::__c_file* __f)
77 : _M_file(__f), _M_unget_buf(traits_type::eof())
88 file() {
return this->_M_file; }
95 syncungetc(int_type __c);
98 syncputc(int_type __c);
103 int_type __c = this->syncgetc();
104 return this->syncungetc(__c);
111 _M_unget_buf = this->syncgetc();
116 pbackfail(int_type __c = traits_type::eof())
119 const int_type __eof = traits_type::eof();
122 if (traits_type::eq_int_type(__c, __eof))
124 if (!traits_type::eq_int_type(_M_unget_buf, __eof))
125 __ret = this->syncungetc(_M_unget_buf);
130 __ret = this->syncungetc(__c);
133 _M_unget_buf = __eof;
137 virtual std::streamsize
138 xsgetn(char_type* __s, std::streamsize __n);
141 overflow(int_type __c = traits_type::eof())
144 if (traits_type::eq_int_type(__c, traits_type::eof()))
146 if (std::fflush(_M_file))
147 __ret = traits_type::eof();
149 __ret = traits_type::not_eof(__c);
152 __ret = this->syncputc(__c);
156 virtual std::streamsize
157 xsputn(
const char_type* __s, std::streamsize __n);
161 {
return std::fflush(_M_file); }
163 virtual std::streampos
164 seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
165 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
167 std::streampos __ret(std::streamoff(-1));
169 if (__dir == std::ios_base::beg)
171 else if (__dir == std::ios_base::cur)
175 #ifdef _GLIBCXX_USE_LFS
176 if (!fseeko64(_M_file, __off, __whence))
177 __ret = std::streampos(ftello64(_M_file));
179 if (!fseek(_M_file, __off, __whence))
180 __ret = std::streampos(std::ftell(_M_file));
185 virtual std::streampos
186 seekpos(std::streampos __pos,
187 std::ios_base::openmode __mode =
188 std::ios_base::in | std::ios_base::out)
189 {
return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
193 inline stdio_sync_filebuf<char>::int_type
194 stdio_sync_filebuf<char>::syncgetc()
195 {
return std::getc(_M_file); }
198 inline stdio_sync_filebuf<char>::int_type
199 stdio_sync_filebuf<char>::syncungetc(int_type __c)
200 {
return std::ungetc(__c, _M_file); }
203 inline stdio_sync_filebuf<char>::int_type
204 stdio_sync_filebuf<char>::syncputc(int_type __c)
205 {
return std::putc(__c, _M_file); }
208 inline std::streamsize
209 stdio_sync_filebuf<char>::xsgetn(
char* __s, std::streamsize __n)
211 std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
213 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
215 _M_unget_buf = traits_type::eof();
220 inline std::streamsize
221 stdio_sync_filebuf<char>::xsputn(
const char* __s, std::streamsize __n)
222 {
return std::fwrite(__s, 1, __n, _M_file); }
224 #ifdef _GLIBCXX_USE_WCHAR_T
226 inline stdio_sync_filebuf<wchar_t>::int_type
227 stdio_sync_filebuf<wchar_t>::syncgetc()
228 {
return std::getwc(_M_file); }
231 inline stdio_sync_filebuf<wchar_t>::int_type
232 stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
233 {
return std::ungetwc(__c, _M_file); }
236 inline stdio_sync_filebuf<wchar_t>::int_type
237 stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
238 {
return std::putwc(__c, _M_file); }
241 inline std::streamsize
242 stdio_sync_filebuf<wchar_t>::xsgetn(
wchar_t* __s, std::streamsize __n)
244 std::streamsize __ret = 0;
245 const int_type __eof = traits_type::eof();
248 int_type __c = this->syncgetc();
249 if (traits_type::eq_int_type(__c, __eof))
251 __s[__ret] = traits_type::to_char_type(__c);
256 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
258 _M_unget_buf = traits_type::eof();
263 inline std::streamsize
264 stdio_sync_filebuf<wchar_t>::xsputn(
const wchar_t* __s,
267 std::streamsize __ret = 0;
268 const int_type __eof = traits_type::eof();
271 if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
279 #if _GLIBCXX_EXTERN_TEMPLATE
280 extern template class stdio_sync_filebuf<char>;
281 #ifdef _GLIBCXX_USE_WCHAR_T
282 extern template class stdio_sync_filebuf<wchar_t>;
286 _GLIBCXX_END_NAMESPACE_VERSION
namespace std _GLIBCXX_VISIBILITY(default)
Definition: auto_ptr.h:36