libstdc++
stdio_sync_filebuf.h
Go to the documentation of this file.
1 // Iostreams wrapper for stdio FILE* -*- C++ -*-
2 
3 // Copyright (C) 2003-2013 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 <unistd.h>
36 #include <cstdio>
37 #include <bits/c++io.h> // For __c_file
38 
39 #ifdef _GLIBCXX_USE_WCHAR_T
40 #include <cwchar>
41 #endif
42 
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  // Underlying stdio FILE
68  std::__c_file* const _M_file;
69 
70  // Last character gotten. This is used when pbackfail is
71  // called from basic_streambuf::sungetc()
72  int_type _M_unget_buf;
73 
74  public:
75  explicit
76  stdio_sync_filebuf(std::__c_file* __f)
77  : _M_file(__f), _M_unget_buf(traits_type::eof())
78  { }
79 
80 #ifdef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL
81  ~stdio_sync_filebuf() {}
82 #endif
83  /**
84  * @return The underlying FILE*.
85  *
86  * This function can be used to access the underlying C file pointer.
87  * Note that there is no way for the library to track what you do
88  * with the file, so be careful.
89  */
90  std::__c_file* const
91  file() { return this->_M_file; }
92 
93  protected:
94  int_type
95  syncgetc();
96 
97  int_type
98  syncungetc(int_type __c);
99 
100  int_type
101  syncputc(int_type __c);
102 
103  virtual int_type
105  {
106  int_type __c = this->syncgetc();
107  return this->syncungetc(__c);
108  }
109 
110  virtual int_type
112  {
113  // Store the gotten character in case we need to unget it.
114  _M_unget_buf = this->syncgetc();
115  return _M_unget_buf;
116  }
117 
118  virtual int_type
119  pbackfail(int_type __c = traits_type::eof())
120  {
121  int_type __ret;
122  const int_type __eof = traits_type::eof();
123 
124  // Check if the unget or putback was requested
125  if (traits_type::eq_int_type(__c, __eof)) // unget
126  {
127  if (!traits_type::eq_int_type(_M_unget_buf, __eof))
128  __ret = this->syncungetc(_M_unget_buf);
129  else // buffer invalid, fail.
130  __ret = __eof;
131  }
132  else // putback
133  __ret = this->syncungetc(__c);
134 
135  // The buffered character is no longer valid, discard it.
136  _M_unget_buf = __eof;
137  return __ret;
138  }
139 
140  virtual std::streamsize
141  xsgetn(char_type* __s, std::streamsize __n);
142 
143  virtual int_type
144  overflow(int_type __c = traits_type::eof())
145  {
146  int_type __ret;
147  if (traits_type::eq_int_type(__c, traits_type::eof()))
148  {
149  if (std::fflush(_M_file))
150  __ret = traits_type::eof();
151  else
152  __ret = traits_type::not_eof(__c);
153  }
154  else
155  __ret = this->syncputc(__c);
156  return __ret;
157  }
158 
159  virtual std::streamsize
160  xsputn(const char_type* __s, std::streamsize __n);
161 
162  virtual int
164  { return std::fflush(_M_file); }
165 
166  virtual std::streampos
167  seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
168  std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
169  {
170  std::streampos __ret(std::streamoff(-1));
171  int __whence;
172  if (__dir == std::ios_base::beg)
173  __whence = SEEK_SET;
174  else if (__dir == std::ios_base::cur)
175  __whence = SEEK_CUR;
176  else
177  __whence = SEEK_END;
178 #ifdef _GLIBCXX_USE_LFS
179  if (!fseeko64(_M_file, __off, __whence))
180  __ret = std::streampos(ftello64(_M_file));
181 #else
182  if (!fseek(_M_file, __off, __whence))
183  __ret = std::streampos(std::ftell(_M_file));
184 #endif
185  return __ret;
186  }
187 
188  virtual std::streampos
189  seekpos(std::streampos __pos,
190  std::ios_base::openmode __mode =
192  { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
193  };
194 
195  template<>
196  inline stdio_sync_filebuf<char>::int_type
197  stdio_sync_filebuf<char>::syncgetc()
198  { return std::getc(_M_file); }
199 
200  template<>
201  inline stdio_sync_filebuf<char>::int_type
202  stdio_sync_filebuf<char>::syncungetc(int_type __c)
203  { return std::ungetc(__c, _M_file); }
204 
205  template<>
206  inline stdio_sync_filebuf<char>::int_type
207  stdio_sync_filebuf<char>::syncputc(int_type __c)
208  { return std::putc(__c, _M_file); }
209 
210  template<>
211 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL
212  inline
213 #endif
215  stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
216  {
217  std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
218  if (__ret > 0)
219  _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
220  else
221  _M_unget_buf = traits_type::eof();
222  return __ret;
223  }
224 
225  template<>
226 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL
227  inline
228 #endif
230  stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
231  { return std::fwrite(__s, 1, __n, _M_file); }
232 
233 #ifdef _GLIBCXX_USE_WCHAR_T
234  template<>
235  inline stdio_sync_filebuf<wchar_t>::int_type
236  stdio_sync_filebuf<wchar_t>::syncgetc()
237  { return std::getwc(_M_file); }
238 
239  template<>
240  inline stdio_sync_filebuf<wchar_t>::int_type
241  stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
242  { return std::ungetwc(__c, _M_file); }
243 
244  template<>
245  inline stdio_sync_filebuf<wchar_t>::int_type
246  stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
247  { return std::putwc(__c, _M_file); }
248 
249  template<>
250 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL
251  inline
252 #endif
254  stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
255  {
256  std::streamsize __ret = 0;
257  const int_type __eof = traits_type::eof();
258  while (__n--)
259  {
260  int_type __c = this->syncgetc();
261  if (traits_type::eq_int_type(__c, __eof))
262  break;
263  __s[__ret] = traits_type::to_char_type(__c);
264  ++__ret;
265  }
266 
267  if (__ret > 0)
268  _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
269  else
270  _M_unget_buf = traits_type::eof();
271  return __ret;
272  }
273 
274  template<>
275 #ifndef _GLIBCXX_STDIO_SYNC_FILEBUF_IMPL
276  inline
277 #endif
279  stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s,
280  std::streamsize __n)
281  {
282  std::streamsize __ret = 0;
283  const int_type __eof = traits_type::eof();
284  while (__n--)
285  {
286  if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
287  break;
288  ++__ret;
289  }
290  return __ret;
291  }
292 #endif
293 
294 #if _GLIBCXX_EXTERN_TEMPLATE
295  extern template class stdio_sync_filebuf<char>;
296 #ifdef _GLIBCXX_USE_WCHAR_T
297  extern template class stdio_sync_filebuf<wchar_t>;
298 #endif
299 #endif
300 
301 _GLIBCXX_END_NAMESPACE_VERSION
302 } // namespace
303 
304 #endif
static const openmode out
Open for output. Default for ofstream and fstream.
Definition: ios_base.h:378
traits_type::pos_type pos_type
Definition: streambuf:132
static const seekdir cur
Request a seek relative to the current position within the sequence.
Definition: ios_base.h:399
std::__c_file *const file()
fpos< mbstate_t > streampos
File position for char streams.
Definition: postypes.h:228
static const seekdir beg
Request a seek relative to the beginning of the stream.
Definition: ios_base.h:396
_Traits traits_type
Definition: streambuf:130
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition: postypes.h:98
virtual int_type underflow()
Fetches more data from the controlled sequence.
long long streamoff
Type used by fpos, char_traits&lt;char&gt;, and char_traits&lt;wchar_t&gt;.
Definition: postypes.h:94
virtual int_type uflow()
Fetches more data from the controlled sequence.
static const openmode in
Open for input. Default for ifstream and fstream.
Definition: ios_base.h:375
The actual work of input and output (interface).
Definition: iosfwd:80
virtual int_type return traits_type::eof()
Tries to back up the input sequence.
virtual int sync()
Synchronizes the buffer arrays with the controlled sequences.
Class representing stream positions.
Definition: postypes.h:112
traits_type::off_type off_type
Definition: streambuf:133
Provides a layer of compatibility for C.This GNU extension provides extensions for working with stand...