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)
53 #ifdef _GLIBCXX_USE_WCHAR_T
59 template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
60 class stdio_sync_filebuf : public std::basic_streambuf<_CharT, _Traits>
64 typedef _CharT char_type;
65 typedef _Traits traits_type;
66 typedef typename traits_type::int_type int_type;
67 typedef typename traits_type::pos_type pos_type;
68 typedef typename traits_type::off_type off_type;
71 // Underlying stdio FILE
72 std::__c_file* const _M_file;
74 // Last character gotten. This is used when pbackfail is
75 // called from basic_streambuf::sungetc()
76 int_type _M_unget_buf;
80 stdio_sync_filebuf(std::__c_file* __f)
81 : _M_file(__f), _M_unget_buf(traits_type::eof())
90 syncungetc(int_type __c);
93 syncputc(int_type __c);
98 int_type __c = this->syncgetc();
99 return this->syncungetc(__c);
105 // Store the gotten character in case we need to unget it.
106 _M_unget_buf = this->syncgetc();
111 pbackfail(int_type __c = traits_type::eof())
114 const int_type __eof = traits_type::eof();
116 // Check if the unget or putback was requested
117 if (traits_type::eq_int_type(__c, __eof)) // unget
119 if (!traits_type::eq_int_type(_M_unget_buf, __eof))
120 __ret = this->syncungetc(_M_unget_buf);
121 else // buffer invalid, fail.
125 __ret = this->syncungetc(__c);
127 // The buffered character is no longer valid, discard it.
128 _M_unget_buf = __eof;
132 virtual std::streamsize
133 xsgetn(char_type* __s, std::streamsize __n);
135 virtual std::streamsize
138 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
140 struct stat __buffer;
141 int __ret = fstat(fileno(_M_file), &__buffer);
142 if (!__ret && _GLIBCXX_ISREG(__buffer.st_mode))
143 return __buffer.st_size - ftell(_M_file);
149 overflow(int_type __c = traits_type::eof())
152 if (traits_type::eq_int_type(__c, traits_type::eof()))
154 if (std::fflush(_M_file))
155 __ret = traits_type::eof();
157 __ret = traits_type::not_eof(__c);
160 __ret = this->syncputc(__c);
164 virtual std::streamsize
165 xsputn(const char_type* __s, std::streamsize __n);
169 { return std::fflush(_M_file); }
171 virtual std::streampos
172 seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
173 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
175 std::streampos __ret(std::streamoff(-1));
177 if (__dir == std::ios_base::beg)
179 else if (__dir == std::ios_base::cur)
183 #ifdef _GLIBCXX_USE_LFS
184 if (!fseeko64(_M_file, __off, __whence))
185 __ret = std::streampos(ftello64(_M_file));
187 if (!fseek(_M_file, __off, __whence))
188 __ret = std::streampos(std::ftell(_M_file));
193 virtual std::streampos
194 seekpos(std::streampos __pos,
195 std::ios_base::openmode __mode =
196 std::ios_base::in | std::ios_base::out)
197 { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
201 inline stdio_sync_filebuf<char>::int_type
202 stdio_sync_filebuf<char>::syncgetc()
203 { return std::getc(_M_file); }
206 inline stdio_sync_filebuf<char>::int_type
207 stdio_sync_filebuf<char>::syncungetc(int_type __c)
208 { return std::ungetc(__c, _M_file); }
211 inline stdio_sync_filebuf<char>::int_type
212 stdio_sync_filebuf<char>::syncputc(int_type __c)
213 { return std::putc(__c, _M_file); }
216 inline std::streamsize
217 stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
219 std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
221 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
223 _M_unget_buf = traits_type::eof();
228 inline std::streamsize
229 stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
230 { return std::fwrite(__s, 1, __n, _M_file); }
232 #ifdef _GLIBCXX_USE_WCHAR_T
234 inline stdio_sync_filebuf<wchar_t>::int_type
235 stdio_sync_filebuf<wchar_t>::syncgetc()
236 { return std::getwc(_M_file); }
239 inline stdio_sync_filebuf<wchar_t>::int_type
240 stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
241 { return std::ungetwc(__c, _M_file); }
244 inline stdio_sync_filebuf<wchar_t>::int_type
245 stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
246 { return std::putwc(__c, _M_file); }
249 inline std::streamsize
250 stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
252 std::streamsize __ret = 0;
253 const int_type __eof = traits_type::eof();
256 int_type __c = this->syncgetc();
257 if (traits_type::eq_int_type(__c, __eof))
259 __s[__ret] = traits_type::to_char_type(__c);
264 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
266 _M_unget_buf = traits_type::eof();
271 inline std::streamsize
272 stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s,
275 std::streamsize __ret = 0;
276 const int_type __eof = traits_type::eof();
279 if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
287 #if _GLIBCXX_EXTERN_TEMPLATE
288 extern template class stdio_sync_filebuf<char>;
289 #ifdef _GLIBCXX_USE_WCHAR_T
290 extern template class stdio_sync_filebuf<wchar_t>;
293 } // namespace __gnu_cxx