]>
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 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file ext/stdiostream.h
31 * This file is a GNU extension to the Standard C++ Library.
34 #ifndef _STDIO_SYNC_FILEBUF_H
35 #define _STDIO_SYNC_FILEBUF_H 1
37 #pragma GCC system_header
42 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
43 # include <sys/stat.h>
44 # ifdef _GLIBCXX_HAVE_S_ISREG
45 # define _GLIBCXX_ISREG(x) S_ISREG(x)
47 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
51 #ifdef _GLIBCXX_USE_WCHAR_T
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 // Underlying stdio FILE
70 std::__c_file
* const _M_file
;
72 // Last character gotten. This is used when pbackfail is
73 // called from basic_streambuf::sungetc()
74 int_type _M_unget_buf
;
78 stdio_sync_filebuf(std::__c_file
* __f
)
79 : _M_file(__f
), _M_unget_buf(traits_type::eof())
88 syncungetc(int_type __c
);
91 syncputc(int_type __c
);
96 int_type __c
= this->syncgetc();
97 return this->syncungetc(__c
);
103 // Store the gotten character in case we need to unget it.
104 _M_unget_buf
= this->syncgetc();
109 pbackfail(int_type __c
= traits_type::eof())
112 const int_type __eof
= traits_type::eof();
114 // Check if the unget or putback was requested
115 if (traits_type::eq_int_type(__c
, __eof
)) // unget
117 if (!traits_type::eq_int_type(_M_unget_buf
, __eof
))
118 __ret
= this->syncungetc(_M_unget_buf
);
119 else // buffer invalid, fail.
123 __ret
= this->syncungetc(__c
);
125 // The buffered character is no longer valid, discard it.
126 _M_unget_buf
= __eof
;
130 virtual std::streamsize
131 xsgetn(char_type
* __s
, std::streamsize __n
);
133 virtual std::streamsize
136 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
138 struct stat __buffer
;
139 int __ret
= fstat(fileno(_M_file
), &__buffer
);
140 if (!__ret
&& _GLIBCXX_ISREG(__buffer
.st_mode
))
141 return __buffer
.st_size
- ftell(_M_file
);
147 overflow(int_type __c
= traits_type::eof())
150 if (traits_type::eq_int_type(__c
, traits_type::eof()))
152 if (std::fflush(_M_file
))
153 __ret
= traits_type::eof();
155 __ret
= traits_type::not_eof(__c
);
158 __ret
= this->syncputc(__c
);
162 virtual std::streamsize
163 xsputn(const char_type
* __s
, std::streamsize __n
);
167 { return std::fflush(_M_file
); }
169 virtual std::streampos
170 seekoff(std::streamoff __off
, std::ios_base::seekdir __dir
,
171 std::ios_base::openmode
= std::ios_base::in
| std::ios_base::out
)
173 std::streampos
__ret(std::streamoff(-1));
175 if (__dir
== std::ios_base::beg
)
177 else if (__dir
== std::ios_base::cur
)
182 if (!fseek(_M_file
, __off
, __whence
))
183 __ret
= std::streampos(std::ftell(_M_file
));
187 virtual std::streampos
188 seekpos(std::streampos __pos
,
189 std::ios_base::openmode __mode
=
190 std::ios_base::in
| std::ios_base::out
)
191 { return seekoff(std::streamoff(__pos
), std::ios_base::beg
, __mode
); }
195 inline stdio_sync_filebuf
<char>::int_type
196 stdio_sync_filebuf
<char>::syncgetc()
197 { return std::getc(_M_file
); }
200 inline stdio_sync_filebuf
<char>::int_type
201 stdio_sync_filebuf
<char>::syncungetc(int_type __c
)
202 { return std::ungetc(__c
, _M_file
); }
205 inline stdio_sync_filebuf
<char>::int_type
206 stdio_sync_filebuf
<char>::syncputc(int_type __c
)
207 { return std::putc(__c
, _M_file
); }
210 inline std::streamsize
211 stdio_sync_filebuf
<char>::xsgetn(char* __s
, std::streamsize __n
)
213 std::streamsize __ret
= std::fread(__s
, 1, __n
, _M_file
);
215 _M_unget_buf
= traits_type::to_int_type(__s
[__ret
- 1]);
217 _M_unget_buf
= traits_type::eof();
222 inline std::streamsize
223 stdio_sync_filebuf
<char>::xsputn(const char* __s
, std::streamsize __n
)
224 { return std::fwrite(__s
, 1, __n
, _M_file
); }
226 #ifdef _GLIBCXX_USE_WCHAR_T
228 inline stdio_sync_filebuf
<wchar_t>::int_type
229 stdio_sync_filebuf
<wchar_t>::syncgetc()
230 { return std::getwc(_M_file
); }
233 inline stdio_sync_filebuf
<wchar_t>::int_type
234 stdio_sync_filebuf
<wchar_t>::syncungetc(int_type __c
)
235 { return std::ungetwc(__c
, _M_file
); }
238 inline stdio_sync_filebuf
<wchar_t>::int_type
239 stdio_sync_filebuf
<wchar_t>::syncputc(int_type __c
)
240 { return std::putwc(__c
, _M_file
); }
243 inline std::streamsize
244 stdio_sync_filebuf
<wchar_t>::xsgetn(wchar_t* __s
, std::streamsize __n
)
246 std::streamsize __ret
= 0;
247 const int_type __eof
= traits_type::eof();
250 int_type __c
= this->syncgetc();
251 if (traits_type::eq_int_type(__c
, __eof
))
253 __s
[__ret
] = traits_type::to_char_type(__c
);
258 _M_unget_buf
= traits_type::to_int_type(__s
[__ret
- 1]);
260 _M_unget_buf
= traits_type::eof();
265 inline std::streamsize
266 stdio_sync_filebuf
<wchar_t>::xsputn(const wchar_t* __s
,
269 std::streamsize __ret
= 0;
270 const int_type __eof
= traits_type::eof();
273 if (traits_type::eq_int_type(this->syncputc(*__s
++), __eof
))
281 #if _GLIBCXX_EXTERN_TEMPLATE
282 extern template class stdio_sync_filebuf
<char>;
283 #ifdef _GLIBCXX_USE_WCHAR_T
284 extern template class stdio_sync_filebuf
<wchar_t>;
287 } // namespace __gnu_cxx