re PR libfortran/69799 (FAIL: gfortran.dg/coarray_allocate_3.f08 at -O1 and above)
[official-gcc.git] / libstdc++-v3 / include / ext / stdio_sync_filebuf.h
blobcba8034470ee55fe1808484893208f0a56c05f61
1 // Iostreams wrapper for stdio FILE* -*- C++ -*-
3 // Copyright (C) 2003-2016 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.
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 // 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.
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/>.
25 /** @file ext/stdio_sync_filebuf.h
26 * This file is a GNU extension to the Standard C++ Library.
29 #ifndef _STDIO_SYNC_FILEBUF_H
30 #define _STDIO_SYNC_FILEBUF_H 1
32 #pragma GCC system_header
34 #include <streambuf>
35 #include <unistd.h>
36 #include <cstdio>
37 #include <bits/c++io.h> // For __c_file
38 #include <bits/move.h> // For __exchange
40 #ifdef _GLIBCXX_USE_WCHAR_T
41 #include <cwchar>
42 #endif
44 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 /**
49 * @brief Provides a layer of compatibility for C.
50 * @ingroup io
52 * This GNU extension provides extensions for working with standard
53 * C FILE*'s. It must be instantiated by the user with the type of
54 * character used in the file stream, e.g., stdio_filebuf<char>.
56 template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
57 class stdio_sync_filebuf : public std::basic_streambuf<_CharT, _Traits>
59 public:
60 // Types:
61 typedef _CharT char_type;
62 typedef _Traits traits_type;
63 typedef typename traits_type::int_type int_type;
64 typedef typename traits_type::pos_type pos_type;
65 typedef typename traits_type::off_type off_type;
67 private:
68 typedef std::basic_streambuf<_CharT, _Traits> __streambuf_type;
70 // Underlying stdio FILE
71 std::__c_file* _M_file;
73 // Last character gotten. This is used when pbackfail is
74 // called from basic_streambuf::sungetc()
75 int_type _M_unget_buf;
77 public:
78 explicit
79 stdio_sync_filebuf(std::__c_file* __f)
80 : _M_file(__f), _M_unget_buf(traits_type::eof())
81 { }
83 #if __cplusplus >= 201103L
84 stdio_sync_filebuf(stdio_sync_filebuf&& __fb) noexcept
85 : __streambuf_type(std::move(__fb)),
86 _M_file(__fb._M_file), _M_unget_buf(__fb._M_unget_buf)
88 __fb._M_file = nullptr;
89 __fb._M_unget_buf = traits_type::eof();
92 stdio_sync_filebuf&
93 operator=(stdio_sync_filebuf&& __fb) noexcept
95 __streambuf_type::operator=(__fb);
96 _M_file = std::__exchange(__fb._M_file, nullptr);
97 _M_unget_buf = std::__exchange(__fb._M_unget_buf, traits_type::eof());
98 return *this;
101 void
102 swap(stdio_sync_filebuf& __fb)
104 __streambuf_type::swap(__fb);
105 std::swap(_M_file, __fb._M_file);
106 std::swap(_M_unget_buf, __fb._M_unget_buf);
108 #endif
111 * @return The underlying FILE*.
113 * This function can be used to access the underlying C file pointer.
114 * Note that there is no way for the library to track what you do
115 * with the file, so be careful.
117 std::__c_file*
118 file() { return this->_M_file; }
120 protected:
121 int_type
122 syncgetc();
124 int_type
125 syncungetc(int_type __c);
127 int_type
128 syncputc(int_type __c);
130 virtual int_type
131 underflow()
133 int_type __c = this->syncgetc();
134 return this->syncungetc(__c);
137 virtual int_type
138 uflow()
140 // Store the gotten character in case we need to unget it.
141 _M_unget_buf = this->syncgetc();
142 return _M_unget_buf;
145 virtual int_type
146 pbackfail(int_type __c = traits_type::eof())
148 int_type __ret;
149 const int_type __eof = traits_type::eof();
151 // Check if the unget or putback was requested
152 if (traits_type::eq_int_type(__c, __eof)) // unget
154 if (!traits_type::eq_int_type(_M_unget_buf, __eof))
155 __ret = this->syncungetc(_M_unget_buf);
156 else // buffer invalid, fail.
157 __ret = __eof;
159 else // putback
160 __ret = this->syncungetc(__c);
162 // The buffered character is no longer valid, discard it.
163 _M_unget_buf = __eof;
164 return __ret;
167 virtual std::streamsize
168 xsgetn(char_type* __s, std::streamsize __n);
170 virtual int_type
171 overflow(int_type __c = traits_type::eof())
173 int_type __ret;
174 if (traits_type::eq_int_type(__c, traits_type::eof()))
176 if (std::fflush(_M_file))
177 __ret = traits_type::eof();
178 else
179 __ret = traits_type::not_eof(__c);
181 else
182 __ret = this->syncputc(__c);
183 return __ret;
186 virtual std::streamsize
187 xsputn(const char_type* __s, std::streamsize __n);
189 virtual int
190 sync()
191 { return std::fflush(_M_file); }
193 virtual std::streampos
194 seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
195 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
197 std::streampos __ret(std::streamoff(-1));
198 int __whence;
199 if (__dir == std::ios_base::beg)
200 __whence = SEEK_SET;
201 else if (__dir == std::ios_base::cur)
202 __whence = SEEK_CUR;
203 else
204 __whence = SEEK_END;
205 #ifdef _GLIBCXX_USE_LFS
206 if (!fseeko64(_M_file, __off, __whence))
207 __ret = std::streampos(ftello64(_M_file));
208 #else
209 if (!fseek(_M_file, __off, __whence))
210 __ret = std::streampos(std::ftell(_M_file));
211 #endif
212 return __ret;
215 virtual std::streampos
216 seekpos(std::streampos __pos,
217 std::ios_base::openmode __mode =
218 std::ios_base::in | std::ios_base::out)
219 { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
222 template<>
223 inline stdio_sync_filebuf<char>::int_type
224 stdio_sync_filebuf<char>::syncgetc()
225 { return std::getc(_M_file); }
227 template<>
228 inline stdio_sync_filebuf<char>::int_type
229 stdio_sync_filebuf<char>::syncungetc(int_type __c)
230 { return std::ungetc(__c, _M_file); }
232 template<>
233 inline stdio_sync_filebuf<char>::int_type
234 stdio_sync_filebuf<char>::syncputc(int_type __c)
235 { return std::putc(__c, _M_file); }
237 template<>
238 inline std::streamsize
239 stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
241 std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
242 if (__ret > 0)
243 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
244 else
245 _M_unget_buf = traits_type::eof();
246 return __ret;
249 template<>
250 inline std::streamsize
251 stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
252 { return std::fwrite(__s, 1, __n, _M_file); }
254 #ifdef _GLIBCXX_USE_WCHAR_T
255 template<>
256 inline stdio_sync_filebuf<wchar_t>::int_type
257 stdio_sync_filebuf<wchar_t>::syncgetc()
258 { return std::getwc(_M_file); }
260 template<>
261 inline stdio_sync_filebuf<wchar_t>::int_type
262 stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
263 { return std::ungetwc(__c, _M_file); }
265 template<>
266 inline stdio_sync_filebuf<wchar_t>::int_type
267 stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
268 { return std::putwc(__c, _M_file); }
270 template<>
271 inline std::streamsize
272 stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
274 std::streamsize __ret = 0;
275 const int_type __eof = traits_type::eof();
276 while (__n--)
278 int_type __c = this->syncgetc();
279 if (traits_type::eq_int_type(__c, __eof))
280 break;
281 __s[__ret] = traits_type::to_char_type(__c);
282 ++__ret;
285 if (__ret > 0)
286 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
287 else
288 _M_unget_buf = traits_type::eof();
289 return __ret;
292 template<>
293 inline std::streamsize
294 stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s,
295 std::streamsize __n)
297 std::streamsize __ret = 0;
298 const int_type __eof = traits_type::eof();
299 while (__n--)
301 if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
302 break;
303 ++__ret;
305 return __ret;
307 #endif
309 #if _GLIBCXX_EXTERN_TEMPLATE
310 extern template class stdio_sync_filebuf<char>;
311 #ifdef _GLIBCXX_USE_WCHAR_T
312 extern template class stdio_sync_filebuf<wchar_t>;
313 #endif
314 #endif
316 _GLIBCXX_END_NAMESPACE_VERSION
317 } // namespace
319 #endif