xref: /openbsd-src/gnu/gcc/libstdc++-v3/include/ext/stdio_sync_filebuf.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // Iostreams wrapper for stdio FILE* -*- C++ -*-
2*404b540aSrobert 
3*404b540aSrobert // Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
4*404b540aSrobert //
5*404b540aSrobert // This file is part of the GNU ISO C++ Library.  This library is free
6*404b540aSrobert // software; you can redistribute it and/or modify it under the
7*404b540aSrobert // terms of the GNU General Public License as published by the
8*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert // any later version.
10*404b540aSrobert 
11*404b540aSrobert // This library is distributed in the hope that it will be useful,
12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*404b540aSrobert // GNU General Public License for more details.
15*404b540aSrobert 
16*404b540aSrobert // You should have received a copy of the GNU General Public License along
17*404b540aSrobert // with this library; see the file COPYING.  If not, write to the Free
18*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert // USA.
20*404b540aSrobert 
21*404b540aSrobert // As a special exception, you may use this file as part of a free software
22*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert // this file and link it with other files to produce an executable, this
25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert // the GNU General Public License.  This exception does not however
27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert // the GNU General Public License.
29*404b540aSrobert 
30*404b540aSrobert /** @file ext/stdio_sync_filebuf.h
31*404b540aSrobert  *  This file is a GNU extension to the Standard C++ Library.
32*404b540aSrobert  */
33*404b540aSrobert 
34*404b540aSrobert #ifndef _STDIO_SYNC_FILEBUF_H
35*404b540aSrobert #define _STDIO_SYNC_FILEBUF_H 1
36*404b540aSrobert 
37*404b540aSrobert #pragma GCC system_header
38*404b540aSrobert 
39*404b540aSrobert #include <streambuf>
40*404b540aSrobert #include <unistd.h>
41*404b540aSrobert #include <cstdio>
42*404b540aSrobert 
43*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
44*404b540aSrobert #include <cwchar>
45*404b540aSrobert #endif
46*404b540aSrobert 
_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)47*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
48*404b540aSrobert 
49*404b540aSrobert   /// @brief  class stdio_sync_filebuf.
50*404b540aSrobert   template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
51*404b540aSrobert     class stdio_sync_filebuf : public std::basic_streambuf<_CharT, _Traits>
52*404b540aSrobert     {
53*404b540aSrobert     public:
54*404b540aSrobert       // Types:
55*404b540aSrobert       typedef _CharT					char_type;
56*404b540aSrobert       typedef _Traits					traits_type;
57*404b540aSrobert       typedef typename traits_type::int_type		int_type;
58*404b540aSrobert       typedef typename traits_type::pos_type		pos_type;
59*404b540aSrobert       typedef typename traits_type::off_type		off_type;
60*404b540aSrobert 
61*404b540aSrobert     private:
62*404b540aSrobert       // Underlying stdio FILE
63*404b540aSrobert       std::__c_file* const _M_file;
64*404b540aSrobert 
65*404b540aSrobert       // Last character gotten. This is used when pbackfail is
66*404b540aSrobert       // called from basic_streambuf::sungetc()
67*404b540aSrobert       int_type _M_unget_buf;
68*404b540aSrobert 
69*404b540aSrobert     public:
70*404b540aSrobert       explicit
71*404b540aSrobert       stdio_sync_filebuf(std::__c_file* __f)
72*404b540aSrobert       : _M_file(__f), _M_unget_buf(traits_type::eof())
73*404b540aSrobert       { }
74*404b540aSrobert 
75*404b540aSrobert       /**
76*404b540aSrobert        *  @return  The underlying FILE*.
77*404b540aSrobert        *
78*404b540aSrobert        *  This function can be used to access the underlying "C" file pointer.
79*404b540aSrobert        *  Note that there is no way for the library to track what you do
80*404b540aSrobert        *  with the file, so be careful.
81*404b540aSrobert        */
82*404b540aSrobert       std::__c_file* const
83*404b540aSrobert       file() { return this->_M_file; }
84*404b540aSrobert 
85*404b540aSrobert     protected:
86*404b540aSrobert       int_type
87*404b540aSrobert       syncgetc();
88*404b540aSrobert 
89*404b540aSrobert       int_type
90*404b540aSrobert       syncungetc(int_type __c);
91*404b540aSrobert 
92*404b540aSrobert       int_type
93*404b540aSrobert       syncputc(int_type __c);
94*404b540aSrobert 
95*404b540aSrobert       virtual int_type
96*404b540aSrobert       underflow()
97*404b540aSrobert       {
98*404b540aSrobert 	int_type __c = this->syncgetc();
99*404b540aSrobert 	return this->syncungetc(__c);
100*404b540aSrobert       }
101*404b540aSrobert 
102*404b540aSrobert       virtual int_type
103*404b540aSrobert       uflow()
104*404b540aSrobert       {
105*404b540aSrobert 	// Store the gotten character in case we need to unget it.
106*404b540aSrobert 	_M_unget_buf = this->syncgetc();
107*404b540aSrobert 	return _M_unget_buf;
108*404b540aSrobert       }
109*404b540aSrobert 
110*404b540aSrobert       virtual int_type
111*404b540aSrobert       pbackfail(int_type __c = traits_type::eof())
112*404b540aSrobert       {
113*404b540aSrobert 	int_type __ret;
114*404b540aSrobert 	const int_type __eof = traits_type::eof();
115*404b540aSrobert 
116*404b540aSrobert 	// Check if the unget or putback was requested
117*404b540aSrobert 	if (traits_type::eq_int_type(__c, __eof)) // unget
118*404b540aSrobert 	  {
119*404b540aSrobert 	    if (!traits_type::eq_int_type(_M_unget_buf, __eof))
120*404b540aSrobert 	      __ret = this->syncungetc(_M_unget_buf);
121*404b540aSrobert 	    else // buffer invalid, fail.
122*404b540aSrobert 	      __ret = __eof;
123*404b540aSrobert 	  }
124*404b540aSrobert 	else // putback
125*404b540aSrobert 	  __ret = this->syncungetc(__c);
126*404b540aSrobert 
127*404b540aSrobert 	// The buffered character is no longer valid, discard it.
128*404b540aSrobert 	_M_unget_buf = __eof;
129*404b540aSrobert 	return __ret;
130*404b540aSrobert       }
131*404b540aSrobert 
132*404b540aSrobert       virtual std::streamsize
133*404b540aSrobert       xsgetn(char_type* __s, std::streamsize __n);
134*404b540aSrobert 
135*404b540aSrobert       virtual int_type
136*404b540aSrobert       overflow(int_type __c = traits_type::eof())
137*404b540aSrobert       {
138*404b540aSrobert 	int_type __ret;
139*404b540aSrobert 	if (traits_type::eq_int_type(__c, traits_type::eof()))
140*404b540aSrobert 	  {
141*404b540aSrobert 	    if (std::fflush(_M_file))
142*404b540aSrobert 	      __ret = traits_type::eof();
143*404b540aSrobert 	    else
144*404b540aSrobert 	      __ret = traits_type::not_eof(__c);
145*404b540aSrobert 	  }
146*404b540aSrobert 	else
147*404b540aSrobert 	  __ret = this->syncputc(__c);
148*404b540aSrobert 	return __ret;
149*404b540aSrobert       }
150*404b540aSrobert 
151*404b540aSrobert       virtual std::streamsize
152*404b540aSrobert       xsputn(const char_type* __s, std::streamsize __n);
153*404b540aSrobert 
154*404b540aSrobert       virtual int
155*404b540aSrobert       sync()
156*404b540aSrobert       { return std::fflush(_M_file); }
157*404b540aSrobert 
158*404b540aSrobert       virtual std::streampos
159*404b540aSrobert       seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
160*404b540aSrobert 	      std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
161*404b540aSrobert       {
162*404b540aSrobert 	std::streampos __ret(std::streamoff(-1));
163*404b540aSrobert 	int __whence;
164*404b540aSrobert 	if (__dir == std::ios_base::beg)
165*404b540aSrobert 	  __whence = SEEK_SET;
166*404b540aSrobert 	else if (__dir == std::ios_base::cur)
167*404b540aSrobert 	  __whence = SEEK_CUR;
168*404b540aSrobert 	else
169*404b540aSrobert 	  __whence = SEEK_END;
170*404b540aSrobert #ifdef _GLIBCXX_USE_LFS
171*404b540aSrobert 	if (!fseeko64(_M_file, __off, __whence))
172*404b540aSrobert 	  __ret = std::streampos(ftello64(_M_file));
173*404b540aSrobert #else
174*404b540aSrobert 	if (!fseek(_M_file, __off, __whence))
175*404b540aSrobert 	  __ret = std::streampos(std::ftell(_M_file));
176*404b540aSrobert #endif
177*404b540aSrobert 	return __ret;
178*404b540aSrobert       }
179*404b540aSrobert 
180*404b540aSrobert       virtual std::streampos
181*404b540aSrobert       seekpos(std::streampos __pos,
182*404b540aSrobert 	      std::ios_base::openmode __mode =
183*404b540aSrobert 	      std::ios_base::in | std::ios_base::out)
184*404b540aSrobert       { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
185*404b540aSrobert     };
186*404b540aSrobert 
187*404b540aSrobert   template<>
188*404b540aSrobert     inline stdio_sync_filebuf<char>::int_type
syncgetc()189*404b540aSrobert     stdio_sync_filebuf<char>::syncgetc()
190*404b540aSrobert     { return std::getc(_M_file); }
191*404b540aSrobert 
192*404b540aSrobert   template<>
193*404b540aSrobert     inline stdio_sync_filebuf<char>::int_type
syncungetc(int_type __c)194*404b540aSrobert     stdio_sync_filebuf<char>::syncungetc(int_type __c)
195*404b540aSrobert     { return std::ungetc(__c, _M_file); }
196*404b540aSrobert 
197*404b540aSrobert   template<>
198*404b540aSrobert     inline stdio_sync_filebuf<char>::int_type
syncputc(int_type __c)199*404b540aSrobert     stdio_sync_filebuf<char>::syncputc(int_type __c)
200*404b540aSrobert     { return std::putc(__c, _M_file); }
201*404b540aSrobert 
202*404b540aSrobert   template<>
203*404b540aSrobert     inline std::streamsize
xsgetn(char * __s,std::streamsize __n)204*404b540aSrobert     stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
205*404b540aSrobert     {
206*404b540aSrobert       std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
207*404b540aSrobert       if (__ret > 0)
208*404b540aSrobert 	_M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
209*404b540aSrobert       else
210*404b540aSrobert 	_M_unget_buf = traits_type::eof();
211*404b540aSrobert       return __ret;
212*404b540aSrobert     }
213*404b540aSrobert 
214*404b540aSrobert   template<>
215*404b540aSrobert     inline std::streamsize
xsputn(const char * __s,std::streamsize __n)216*404b540aSrobert     stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
217*404b540aSrobert     { return std::fwrite(__s, 1, __n, _M_file); }
218*404b540aSrobert 
219*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
220*404b540aSrobert   template<>
221*404b540aSrobert     inline stdio_sync_filebuf<wchar_t>::int_type
syncgetc()222*404b540aSrobert     stdio_sync_filebuf<wchar_t>::syncgetc()
223*404b540aSrobert     { return std::getwc(_M_file); }
224*404b540aSrobert 
225*404b540aSrobert   template<>
226*404b540aSrobert     inline stdio_sync_filebuf<wchar_t>::int_type
syncungetc(int_type __c)227*404b540aSrobert     stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
228*404b540aSrobert     { return std::ungetwc(__c, _M_file); }
229*404b540aSrobert 
230*404b540aSrobert   template<>
231*404b540aSrobert     inline stdio_sync_filebuf<wchar_t>::int_type
syncputc(int_type __c)232*404b540aSrobert     stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
233*404b540aSrobert     { return std::putwc(__c, _M_file); }
234*404b540aSrobert 
235*404b540aSrobert   template<>
236*404b540aSrobert     inline std::streamsize
xsgetn(wchar_t * __s,std::streamsize __n)237*404b540aSrobert     stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
238*404b540aSrobert     {
239*404b540aSrobert       std::streamsize __ret = 0;
240*404b540aSrobert       const int_type __eof = traits_type::eof();
241*404b540aSrobert       while (__n--)
242*404b540aSrobert 	{
243*404b540aSrobert 	  int_type __c = this->syncgetc();
244*404b540aSrobert 	  if (traits_type::eq_int_type(__c, __eof))
245*404b540aSrobert 	    break;
246*404b540aSrobert 	  __s[__ret] = traits_type::to_char_type(__c);
247*404b540aSrobert 	  ++__ret;
248*404b540aSrobert 	}
249*404b540aSrobert 
250*404b540aSrobert       if (__ret > 0)
251*404b540aSrobert 	_M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
252*404b540aSrobert       else
253*404b540aSrobert 	_M_unget_buf = traits_type::eof();
254*404b540aSrobert       return __ret;
255*404b540aSrobert     }
256*404b540aSrobert 
257*404b540aSrobert   template<>
258*404b540aSrobert     inline std::streamsize
xsputn(const wchar_t * __s,std::streamsize __n)259*404b540aSrobert     stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s,
260*404b540aSrobert 					std::streamsize __n)
261*404b540aSrobert     {
262*404b540aSrobert       std::streamsize __ret = 0;
263*404b540aSrobert       const int_type __eof = traits_type::eof();
264*404b540aSrobert       while (__n--)
265*404b540aSrobert 	{
266*404b540aSrobert 	  if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
267*404b540aSrobert 	    break;
268*404b540aSrobert 	  ++__ret;
269*404b540aSrobert 	}
270*404b540aSrobert       return __ret;
271*404b540aSrobert     }
272*404b540aSrobert #endif
273*404b540aSrobert 
274*404b540aSrobert #if _GLIBCXX_EXTERN_TEMPLATE
275*404b540aSrobert   extern template class stdio_sync_filebuf<char>;
276*404b540aSrobert #ifdef _GLIBCXX_USE_WCHAR_T
277*404b540aSrobert   extern template class stdio_sync_filebuf<wchar_t>;
278*404b540aSrobert #endif
279*404b540aSrobert #endif
280*404b540aSrobert 
281*404b540aSrobert _GLIBCXX_END_NAMESPACE
282*404b540aSrobert 
283*404b540aSrobert #endif
284