1 // Iostreams wrapper for stdio FILE* -*- C++ -*-
2 
3 // Copyright (C) 2003-2021 Free Software Foundation, Inc.
4 //
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 3, or (at your option)
9 // any later version.
10 
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.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file ext/stdio_sync_filebuf.h
26  *  This file is a GNU extension to the Standard C++ Library.
27  */
28 
29 #ifndef _STDIO_SYNC_FILEBUF_H
30 #define _STDIO_SYNC_FILEBUF_H 1
31 
32 #pragma GCC system_header
33 
34 #include <streambuf>
35 #include <cstdio>
36 #include <bits/c++io.h>  // For __c_file
37 #include <bits/move.h>   // For __exchange
38 
39 #ifdef _GLIBCXX_USE_WCHAR_T
40 #include <cwchar>
41 #endif
42 
_GLIBCXX_VISIBILITY(default)43 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 
47   /**
48    *  @brief Provides a layer of compatibility for C.
49    *  @ingroup io
50    *
51    *  This GNU extension provides extensions for working with standard
52    *  C FILE*'s.  It must be instantiated by the user with the type of
53    *  character used in the file stream, e.g., stdio_filebuf<char>.
54   */
55   template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
56     class stdio_sync_filebuf : public std::basic_streambuf<_CharT, _Traits>
57     {
58     public:
59       // Types:
60       typedef _CharT					char_type;
61       typedef _Traits					traits_type;
62       typedef typename traits_type::int_type		int_type;
63       typedef typename traits_type::pos_type		pos_type;
64       typedef typename traits_type::off_type		off_type;
65 
66     private:
67       typedef std::basic_streambuf<_CharT, _Traits> __streambuf_type;
68 
69       // Underlying stdio FILE
70       std::__c_file* _M_file;
71 
72       // Last character gotten. This is used when pbackfail is
73       // called from basic_streambuf::sungetc()
74       int_type _M_unget_buf;
75 
76     public:
77       explicit
78       stdio_sync_filebuf(std::__c_file* __f)
79       : _M_file(__f), _M_unget_buf(traits_type::eof())
80       { }
81 
82 #if __cplusplus >= 201103L
83       stdio_sync_filebuf(stdio_sync_filebuf&& __fb) noexcept
84       : __streambuf_type(std::move(__fb)),
85       _M_file(__fb._M_file), _M_unget_buf(__fb._M_unget_buf)
86       {
87 	__fb._M_file = nullptr;
88 	__fb._M_unget_buf = traits_type::eof();
89       }
90 
91       stdio_sync_filebuf&
92       operator=(stdio_sync_filebuf&& __fb) noexcept
93       {
94 	__streambuf_type::operator=(__fb);
95 	_M_file = std::__exchange(__fb._M_file, nullptr);
96 	_M_unget_buf = std::__exchange(__fb._M_unget_buf, traits_type::eof());
97 	return *this;
98       }
99 
100       void
101       swap(stdio_sync_filebuf& __fb)
102       {
103 	__streambuf_type::swap(__fb);
104 	std::swap(_M_file, __fb._M_file);
105 	std::swap(_M_unget_buf, __fb._M_unget_buf);
106       }
107 #endif
108 
109       /**
110        *  @return  The underlying FILE*.
111        *
112        *  This function can be used to access the underlying C file pointer.
113        *  Note that there is no way for the library to track what you do
114        *  with the file, so be careful.
115        */
116       std::__c_file*
117       file() { return this->_M_file; }
118 
119     protected:
120       int_type
121       syncgetc();
122 
123       int_type
124       syncungetc(int_type __c);
125 
126       int_type
127       syncputc(int_type __c);
128 
129       virtual int_type
130       underflow()
131       {
132 	int_type __c = this->syncgetc();
133 	return this->syncungetc(__c);
134       }
135 
136       virtual int_type
137       uflow()
138       {
139 	// Store the gotten character in case we need to unget it.
140 	_M_unget_buf = this->syncgetc();
141 	return _M_unget_buf;
142       }
143 
144       virtual int_type
145       pbackfail(int_type __c = traits_type::eof())
146       {
147 	int_type __ret;
148 	const int_type __eof = traits_type::eof();
149 
150 	// Check if the unget or putback was requested
151 	if (traits_type::eq_int_type(__c, __eof)) // unget
152 	  {
153 	    if (!traits_type::eq_int_type(_M_unget_buf, __eof))
154 	      __ret = this->syncungetc(_M_unget_buf);
155 	    else // buffer invalid, fail.
156 	      __ret = __eof;
157 	  }
158 	else // putback
159 	  __ret = this->syncungetc(__c);
160 
161 	// The buffered character is no longer valid, discard it.
162 	_M_unget_buf = __eof;
163 	return __ret;
164       }
165 
166       virtual std::streamsize
167       xsgetn(char_type* __s, std::streamsize __n);
168 
169       virtual int_type
170       overflow(int_type __c = traits_type::eof())
171       {
172 	int_type __ret;
173 	if (traits_type::eq_int_type(__c, traits_type::eof()))
174 	  {
175 	    if (std::fflush(_M_file))
176 	      __ret = traits_type::eof();
177 	    else
178 	      __ret = traits_type::not_eof(__c);
179 	  }
180 	else
181 	  __ret = this->syncputc(__c);
182 	return __ret;
183       }
184 
185       virtual std::streamsize
186       xsputn(const char_type* __s, std::streamsize __n);
187 
188       virtual int
189       sync()
190       { return std::fflush(_M_file); }
191 
192       virtual std::streampos
193       seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
194 	      std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
195       {
196 	std::streampos __ret(std::streamoff(-1));
197 	int __whence;
198 	if (__dir == std::ios_base::beg)
199 	  __whence = SEEK_SET;
200 	else if (__dir == std::ios_base::cur)
201 	  __whence = SEEK_CUR;
202 	else
203 	  __whence = SEEK_END;
204 #ifdef _GLIBCXX_USE_LFS
205 	if (!fseeko64(_M_file, __off, __whence))
206 	  __ret = std::streampos(ftello64(_M_file));
207 #else
208 	if (!fseek(_M_file, __off, __whence))
209 	  __ret = std::streampos(std::ftell(_M_file));
210 #endif
211 	return __ret;
212       }
213 
214       virtual std::streampos
215       seekpos(std::streampos __pos,
216 	      std::ios_base::openmode __mode =
217 	      std::ios_base::in | std::ios_base::out)
218       { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
219     };
220 
221   template<>
222     inline stdio_sync_filebuf<char>::int_type
223     stdio_sync_filebuf<char>::syncgetc()
224     { return std::getc(_M_file); }
225 
226   template<>
227     inline stdio_sync_filebuf<char>::int_type
228     stdio_sync_filebuf<char>::syncungetc(int_type __c)
229     { return std::ungetc(__c, _M_file); }
230 
231   template<>
232     inline stdio_sync_filebuf<char>::int_type
233     stdio_sync_filebuf<char>::syncputc(int_type __c)
234     { return std::putc(__c, _M_file); }
235 
236   template<>
237     inline std::streamsize
238     stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
239     {
240       std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
241       if (__ret > 0)
242 	_M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
243       else
244 	_M_unget_buf = traits_type::eof();
245       return __ret;
246     }
247 
248   template<>
249     inline std::streamsize
250     stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
251     { return std::fwrite(__s, 1, __n, _M_file); }
252 
253 #ifdef _GLIBCXX_USE_WCHAR_T
254   template<>
255     inline stdio_sync_filebuf<wchar_t>::int_type
256     stdio_sync_filebuf<wchar_t>::syncgetc()
257     { return std::getwc(_M_file); }
258 
259   template<>
260     inline stdio_sync_filebuf<wchar_t>::int_type
261     stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
262     { return std::ungetwc(__c, _M_file); }
263 
264   template<>
265     inline stdio_sync_filebuf<wchar_t>::int_type
266     stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
267     { return std::putwc(__c, _M_file); }
268 
269   template<>
270     inline std::streamsize
271     stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
272     {
273       std::streamsize __ret = 0;
274       const int_type __eof = traits_type::eof();
275       while (__n--)
276 	{
277 	  int_type __c = this->syncgetc();
278 	  if (traits_type::eq_int_type(__c, __eof))
279 	    break;
280 	  __s[__ret] = traits_type::to_char_type(__c);
281 	  ++__ret;
282 	}
283 
284       if (__ret > 0)
285 	_M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
286       else
287 	_M_unget_buf = traits_type::eof();
288       return __ret;
289     }
290 
291   template<>
292     inline std::streamsize
293     stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s,
294 					std::streamsize __n)
295     {
296       std::streamsize __ret = 0;
297       const int_type __eof = traits_type::eof();
298       while (__n--)
299 	{
300 	  if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
301 	    break;
302 	  ++__ret;
303 	}
304       return __ret;
305     }
306 #endif
307 
308 #if _GLIBCXX_EXTERN_TEMPLATE
309   extern template class stdio_sync_filebuf<char>;
310 #ifdef _GLIBCXX_USE_WCHAR_T
311   extern template class stdio_sync_filebuf<wchar_t>;
312 #endif
313 #endif
314 
315 _GLIBCXX_END_NAMESPACE_VERSION
316 } // namespace
317 
318 #endif
319