]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/stdio_sync_filebuf.h
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
)
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
>
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
;
59 // Underlying stdio FILE
60 std::__c_file
* const _M_file
;
62 // Last character gotten. This is used when pbackfail is
63 // called from basic_streambuf::sungetc()
64 int_type _M_unget_buf
;
68 stdio_sync_filebuf(std::__c_file
* __f
)
69 : _M_file(__f
), _M_unget_buf(traits_type::eof())
73 * @return The underlying FILE*.
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.
80 file() { return this->_M_file
; }
87 syncungetc(int_type __c
);
90 syncputc(int_type __c
);
95 int_type __c
= this->syncgetc();
96 return this->syncungetc(__c
);
102 // Store the gotten character in case we need to unget it.
103 _M_unget_buf
= this->syncgetc();
108 pbackfail(int_type __c
= traits_type::eof())
111 const int_type __eof
= traits_type::eof();
113 // Check if the unget or putback was requested
114 if (traits_type::eq_int_type(__c
, __eof
)) // unget
116 if (!traits_type::eq_int_type(_M_unget_buf
, __eof
))
117 __ret
= this->syncungetc(_M_unget_buf
);
118 else // buffer invalid, fail.
122 __ret
= this->syncungetc(__c
);
124 // The buffered character is no longer valid, discard it.
125 _M_unget_buf
= __eof
;
129 virtual std::streamsize
130 xsgetn(char_type
* __s
, std::streamsize __n
);
133 overflow(int_type __c
= traits_type::eof())
136 if (traits_type::eq_int_type(__c
, traits_type::eof()))
138 if (std::fflush(_M_file
))
139 __ret
= traits_type::eof();
141 __ret
= traits_type::not_eof(__c
);
144 __ret
= this->syncputc(__c
);
148 virtual std::streamsize
149 xsputn(const char_type
* __s
, std::streamsize __n
);
153 { return std::fflush(_M_file
); }
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
)
159 std::streampos
__ret(std::streamoff(-1));
161 if (__dir
== std::ios_base::beg
)
163 else if (__dir
== std::ios_base::cur
)
167 #ifdef _GLIBCXX_USE_LFS
168 if (!fseeko64(_M_file
, __off
, __whence
))
169 __ret
= std::streampos(ftello64(_M_file
));
171 if (!fseek(_M_file
, __off
, __whence
))
172 __ret
= std::streampos(std::ftell(_M_file
));
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
); }
185 inline stdio_sync_filebuf
<char>::int_type
186 stdio_sync_filebuf
<char>::syncgetc()
187 { return std::getc(_M_file
); }
190 inline stdio_sync_filebuf
<char>::int_type
191 stdio_sync_filebuf
<char>::syncungetc(int_type __c
)
192 { return std::ungetc(__c
, _M_file
); }
195 inline stdio_sync_filebuf
<char>::int_type
196 stdio_sync_filebuf
<char>::syncputc(int_type __c
)
197 { return std::putc(__c
, _M_file
); }
200 inline std::streamsize
201 stdio_sync_filebuf
<char>::xsgetn(char* __s
, std::streamsize __n
)
203 std::streamsize __ret
= std::fread(__s
, 1, __n
, _M_file
);
205 _M_unget_buf
= traits_type::to_int_type(__s
[__ret
- 1]);
207 _M_unget_buf
= traits_type::eof();
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
); }
216 #ifdef _GLIBCXX_USE_WCHAR_T
218 inline stdio_sync_filebuf
<wchar_t>::int_type
219 stdio_sync_filebuf
<wchar_t>::syncgetc()
220 { return std::getwc(_M_file
); }
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
); }
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
); }
233 inline std::streamsize
234 stdio_sync_filebuf
<wchar_t>::xsgetn(wchar_t* __s
, std::streamsize __n
)
236 std::streamsize __ret
= 0;
237 const int_type __eof
= traits_type::eof();
240 int_type __c
= this->syncgetc();
241 if (traits_type::eq_int_type(__c
, __eof
))
243 __s
[__ret
] = traits_type::to_char_type(__c
);
248 _M_unget_buf
= traits_type::to_int_type(__s
[__ret
- 1]);
250 _M_unget_buf
= traits_type::eof();
255 inline std::streamsize
256 stdio_sync_filebuf
<wchar_t>::xsputn(const wchar_t* __s
,
259 std::streamsize __ret
= 0;
260 const int_type __eof
= traits_type::eof();
263 if (traits_type::eq_int_type(this->syncputc(*__s
++), __eof
))
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>;
278 _GLIBCXX_END_NAMESPACE