]>
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
>
61 std::__c_file
* const _M_file
;
65 typedef _CharT char_type
;
66 typedef _Traits traits_type
;
67 typedef typename
traits_type::int_type int_type
;
68 typedef typename
traits_type::pos_type pos_type
;
69 typedef typename
traits_type::off_type off_type
;
72 stdio_sync_filebuf(std::__c_file
* __f
) : _M_file(__f
) { }
80 syncungetc(int_type __c
);
83 syncputc(int_type __c
);
88 int_type __c
= this->syncgetc();
89 return this->syncungetc(__c
);
94 { return this->syncgetc(); }
97 pbackfail(int_type __c
= traits_type::eof())
98 { return this->syncungetc(__c
); }
100 virtual std::streamsize
101 xsgetn(char_type
* __s
, std::streamsize __n
);
103 virtual std::streamsize
106 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
108 struct stat __buffer
;
109 int __ret
= fstat(fileno(_M_file
), &__buffer
);
110 if (!__ret
&& _GLIBCXX_ISREG(__buffer
.st_mode
))
111 return __buffer
.st_size
- ftell(_M_file
);
117 overflow(int_type __c
= traits_type::eof())
120 if (traits_type::eq_int_type(__c
, traits_type::eof()))
122 if (std::fflush(_M_file
))
123 __ret
= traits_type::eof();
125 __ret
= traits_type::not_eof(__c
);
128 __ret
= this->syncputc(__c
);
132 virtual std::streamsize
133 xsputn(const char_type
* __s
, std::streamsize __n
);
137 { return std::fflush(_M_file
); }
139 virtual std::streampos
140 seekoff(std::streamoff __off
, std::ios_base::seekdir __dir
,
141 std::ios_base::openmode
= std::ios_base::in
| std::ios_base::out
)
143 std::streampos
__ret(std::streamoff(-1));
145 if (__dir
== std::ios_base::beg
)
147 else if (__dir
== std::ios_base::cur
)
152 if (!fseek(_M_file
, __off
, __whence
))
153 __ret
= std::streampos(std::ftell(_M_file
));
157 virtual std::streampos
158 seekpos(std::streampos __pos
,
159 std::ios_base::openmode __mode
=
160 std::ios_base::in
| std::ios_base::out
)
161 { return seekoff(std::streamoff(__pos
), std::ios_base::beg
, __mode
); }
165 inline stdio_sync_filebuf
<char>::int_type
166 stdio_sync_filebuf
<char>::syncgetc()
167 { return std::getc(_M_file
); }
170 inline stdio_sync_filebuf
<char>::int_type
171 stdio_sync_filebuf
<char>::syncungetc(int_type __c
)
172 { return std::ungetc(__c
, _M_file
); }
175 inline stdio_sync_filebuf
<char>::int_type
176 stdio_sync_filebuf
<char>::syncputc(int_type __c
)
177 { return std::putc(__c
, _M_file
); }
180 inline std::streamsize
181 stdio_sync_filebuf
<char>::xsgetn(char* __s
, std::streamsize __n
)
182 { return std::fread(__s
, 1, __n
, _M_file
); }
185 inline std::streamsize
186 stdio_sync_filebuf
<char>::xsputn(const char* __s
, std::streamsize __n
)
187 { return std::fwrite(__s
, 1, __n
, _M_file
); }
189 #ifdef _GLIBCXX_USE_WCHAR_T
191 inline stdio_sync_filebuf
<wchar_t>::int_type
192 stdio_sync_filebuf
<wchar_t>::syncgetc()
193 { return std::getwc(_M_file
); }
196 inline stdio_sync_filebuf
<wchar_t>::int_type
197 stdio_sync_filebuf
<wchar_t>::syncungetc(int_type __c
)
198 { return std::ungetwc(__c
, _M_file
); }
201 inline stdio_sync_filebuf
<wchar_t>::int_type
202 stdio_sync_filebuf
<wchar_t>::syncputc(int_type __c
)
203 { return std::putwc(__c
, _M_file
); }
206 inline std::streamsize
207 stdio_sync_filebuf
<wchar_t>::xsgetn(wchar_t* __s
, std::streamsize __n
)
209 std::streamsize __ret
= 0;
210 const int_type __eof
= traits_type::eof();
213 int_type __c
= this->syncgetc();
214 if (traits_type::eq_int_type(__c
, __eof
))
223 inline std::streamsize
224 stdio_sync_filebuf
<wchar_t>::xsputn(const wchar_t* __s
,
227 std::streamsize __ret
= 0;
228 const int_type __eof
= traits_type::eof();
231 if (traits_type::eq_int_type(this->syncputc(*__s
++), __eof
))
239 #if _GLIBCXX_EXTERN_TEMPLATE
240 extern template class stdio_sync_filebuf
<char>;
241 #ifdef _GLIBCXX_USE_WCHAR_T
242 extern template class stdio_sync_filebuf
<wchar_t>;
245 } // namespace __gnu_cxx