1 // Iostreams wrapper for stdio FILE* -*- C++ -*-
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2009, 2010
4 // Free Software Foundation, Inc.
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)
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.
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.
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/>.
26 /** @file ext/stdio_sync_filebuf.h
27 * This file is a GNU extension to the Standard C++ Library.
30 #ifndef _STDIO_SYNC_FILEBUF_H
31 #define _STDIO_SYNC_FILEBUF_H 1
33 #pragma GCC system_header
38 #include <bits/c++io.h> // For __c_file
40 #ifdef _GLIBCXX_USE_WCHAR_T
44 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx
)
47 * @brief Provides a layer of compatibility for C.
50 * This GNU extension provides extensions for working with standard
51 * C FILE*'s. It must be instantiated by the user with the type of
52 * character used in the file stream, e.g., stdio_filebuf<char>.
54 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
> >
55 class stdio_sync_filebuf
: public std::basic_streambuf
<_CharT
, _Traits
>
59 typedef _CharT char_type
;
60 typedef _Traits traits_type
;
61 typedef typename
traits_type::int_type int_type
;
62 typedef typename
traits_type::pos_type pos_type
;
63 typedef typename
traits_type::off_type off_type
;
66 // Underlying stdio FILE
67 std::__c_file
* const _M_file
;
69 // Last character gotten. This is used when pbackfail is
70 // called from basic_streambuf::sungetc()
71 int_type _M_unget_buf
;
75 stdio_sync_filebuf(std::__c_file
* __f
)
76 : _M_file(__f
), _M_unget_buf(traits_type::eof())
80 * @return The underlying FILE*.
82 * This function can be used to access the underlying C file pointer.
83 * Note that there is no way for the library to track what you do
84 * with the file, so be careful.
87 file() { return this->_M_file
; }
94 syncungetc(int_type __c
);
97 syncputc(int_type __c
);
102 int_type __c
= this->syncgetc();
103 return this->syncungetc(__c
);
109 // Store the gotten character in case we need to unget it.
110 _M_unget_buf
= this->syncgetc();
115 pbackfail(int_type __c
= traits_type::eof())
118 const int_type __eof
= traits_type::eof();
120 // Check if the unget or putback was requested
121 if (traits_type::eq_int_type(__c
, __eof
)) // unget
123 if (!traits_type::eq_int_type(_M_unget_buf
, __eof
))
124 __ret
= this->syncungetc(_M_unget_buf
);
125 else // buffer invalid, fail.
129 __ret
= this->syncungetc(__c
);
131 // The buffered character is no longer valid, discard it.
132 _M_unget_buf
= __eof
;
136 virtual std::streamsize
137 xsgetn(char_type
* __s
, std::streamsize __n
);
140 overflow(int_type __c
= traits_type::eof())
143 if (traits_type::eq_int_type(__c
, traits_type::eof()))
145 if (std::fflush(_M_file
))
146 __ret
= traits_type::eof();
148 __ret
= traits_type::not_eof(__c
);
151 __ret
= this->syncputc(__c
);
155 virtual std::streamsize
156 xsputn(const char_type
* __s
, std::streamsize __n
);
160 { return std::fflush(_M_file
); }
162 virtual std::streampos
163 seekoff(std::streamoff __off
, std::ios_base::seekdir __dir
,
164 std::ios_base::openmode
= std::ios_base::in
| std::ios_base::out
)
166 std::streampos
__ret(std::streamoff(-1));
168 if (__dir
== std::ios_base::beg
)
170 else if (__dir
== std::ios_base::cur
)
174 #ifdef _GLIBCXX_USE_LFS
175 if (!fseeko64(_M_file
, __off
, __whence
))
176 __ret
= std::streampos(ftello64(_M_file
));
178 if (!fseek(_M_file
, __off
, __whence
))
179 __ret
= std::streampos(std::ftell(_M_file
));
184 virtual std::streampos
185 seekpos(std::streampos __pos
,
186 std::ios_base::openmode __mode
=
187 std::ios_base::in
| std::ios_base::out
)
188 { return seekoff(std::streamoff(__pos
), std::ios_base::beg
, __mode
); }
192 inline stdio_sync_filebuf
<char>::int_type
193 stdio_sync_filebuf
<char>::syncgetc()
194 { return std::getc(_M_file
); }
197 inline stdio_sync_filebuf
<char>::int_type
198 stdio_sync_filebuf
<char>::syncungetc(int_type __c
)
199 { return std::ungetc(__c
, _M_file
); }
202 inline stdio_sync_filebuf
<char>::int_type
203 stdio_sync_filebuf
<char>::syncputc(int_type __c
)
204 { return std::putc(__c
, _M_file
); }
207 inline std::streamsize
208 stdio_sync_filebuf
<char>::xsgetn(char* __s
, std::streamsize __n
)
210 std::streamsize __ret
= std::fread(__s
, 1, __n
, _M_file
);
212 _M_unget_buf
= traits_type::to_int_type(__s
[__ret
- 1]);
214 _M_unget_buf
= traits_type::eof();
219 inline std::streamsize
220 stdio_sync_filebuf
<char>::xsputn(const char* __s
, std::streamsize __n
)
221 { return std::fwrite(__s
, 1, __n
, _M_file
); }
223 #ifdef _GLIBCXX_USE_WCHAR_T
225 inline stdio_sync_filebuf
<wchar_t>::int_type
226 stdio_sync_filebuf
<wchar_t>::syncgetc()
227 { return std::getwc(_M_file
); }
230 inline stdio_sync_filebuf
<wchar_t>::int_type
231 stdio_sync_filebuf
<wchar_t>::syncungetc(int_type __c
)
232 { return std::ungetwc(__c
, _M_file
); }
235 inline stdio_sync_filebuf
<wchar_t>::int_type
236 stdio_sync_filebuf
<wchar_t>::syncputc(int_type __c
)
237 { return std::putwc(__c
, _M_file
); }
240 inline std::streamsize
241 stdio_sync_filebuf
<wchar_t>::xsgetn(wchar_t* __s
, std::streamsize __n
)
243 std::streamsize __ret
= 0;
244 const int_type __eof
= traits_type::eof();
247 int_type __c
= this->syncgetc();
248 if (traits_type::eq_int_type(__c
, __eof
))
250 __s
[__ret
] = traits_type::to_char_type(__c
);
255 _M_unget_buf
= traits_type::to_int_type(__s
[__ret
- 1]);
257 _M_unget_buf
= traits_type::eof();
262 inline std::streamsize
263 stdio_sync_filebuf
<wchar_t>::xsputn(const wchar_t* __s
,
266 std::streamsize __ret
= 0;
267 const int_type __eof
= traits_type::eof();
270 if (traits_type::eq_int_type(this->syncputc(*__s
++), __eof
))
278 #if _GLIBCXX_EXTERN_TEMPLATE
279 extern template class stdio_sync_filebuf
<char>;
280 #ifdef _GLIBCXX_USE_WCHAR_T
281 extern template class stdio_sync_filebuf
<wchar_t>;
285 _GLIBCXX_END_NAMESPACE