]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/include/ext/stdio_sync_filebuf.h
run_doxygen: Remove html_output_dir.
[gcc.git] / libstdc++-v3 / include / ext / stdio_sync_filebuf.h
1 // Iostreams wrapper for stdio FILE* -*- C++ -*-
2
3 // Copyright (C) 2003, 2004, 2005, 2006, 2007 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 2, 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 // 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 // USA.
20
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.
29
30 /** @file ext/stdio_sync_filebuf.h
31 * This file is a GNU extension to the Standard C++ Library.
32 */
33
34 #ifndef _STDIO_SYNC_FILEBUF_H
35 #define _STDIO_SYNC_FILEBUF_H 1
36
37 #pragma GCC system_header
38
39 #include <streambuf>
40 #include <unistd.h>
41 #include <cstdio>
42 #include <bits/c++io.h> // For __c_file
43
44 #ifdef _GLIBCXX_USE_WCHAR_T
45 #include <cwchar>
46 #endif
47
48 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
49
50 /// class stdio_sync_filebuf.
51 template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
52 class stdio_sync_filebuf : public std::basic_streambuf<_CharT, _Traits>
53 {
54 public:
55 // Types:
56 typedef _CharT char_type;
57 typedef _Traits traits_type;
58 typedef typename traits_type::int_type int_type;
59 typedef typename traits_type::pos_type pos_type;
60 typedef typename traits_type::off_type off_type;
61
62 private:
63 // Underlying stdio FILE
64 std::__c_file* const _M_file;
65
66 // Last character gotten. This is used when pbackfail is
67 // called from basic_streambuf::sungetc()
68 int_type _M_unget_buf;
69
70 public:
71 explicit
72 stdio_sync_filebuf(std::__c_file* __f)
73 : _M_file(__f), _M_unget_buf(traits_type::eof())
74 { }
75
76 /**
77 * @return The underlying FILE*.
78 *
79 * This function can be used to access the underlying "C" file pointer.
80 * Note that there is no way for the library to track what you do
81 * with the file, so be careful.
82 */
83 std::__c_file* const
84 file() { return this->_M_file; }
85
86 protected:
87 int_type
88 syncgetc();
89
90 int_type
91 syncungetc(int_type __c);
92
93 int_type
94 syncputc(int_type __c);
95
96 virtual int_type
97 underflow()
98 {
99 int_type __c = this->syncgetc();
100 return this->syncungetc(__c);
101 }
102
103 virtual int_type
104 uflow()
105 {
106 // Store the gotten character in case we need to unget it.
107 _M_unget_buf = this->syncgetc();
108 return _M_unget_buf;
109 }
110
111 virtual int_type
112 pbackfail(int_type __c = traits_type::eof())
113 {
114 int_type __ret;
115 const int_type __eof = traits_type::eof();
116
117 // Check if the unget or putback was requested
118 if (traits_type::eq_int_type(__c, __eof)) // unget
119 {
120 if (!traits_type::eq_int_type(_M_unget_buf, __eof))
121 __ret = this->syncungetc(_M_unget_buf);
122 else // buffer invalid, fail.
123 __ret = __eof;
124 }
125 else // putback
126 __ret = this->syncungetc(__c);
127
128 // The buffered character is no longer valid, discard it.
129 _M_unget_buf = __eof;
130 return __ret;
131 }
132
133 virtual std::streamsize
134 xsgetn(char_type* __s, std::streamsize __n);
135
136 virtual int_type
137 overflow(int_type __c = traits_type::eof())
138 {
139 int_type __ret;
140 if (traits_type::eq_int_type(__c, traits_type::eof()))
141 {
142 if (std::fflush(_M_file))
143 __ret = traits_type::eof();
144 else
145 __ret = traits_type::not_eof(__c);
146 }
147 else
148 __ret = this->syncputc(__c);
149 return __ret;
150 }
151
152 virtual std::streamsize
153 xsputn(const char_type* __s, std::streamsize __n);
154
155 virtual int
156 sync()
157 { return std::fflush(_M_file); }
158
159 virtual std::streampos
160 seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
161 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
162 {
163 std::streampos __ret(std::streamoff(-1));
164 int __whence;
165 if (__dir == std::ios_base::beg)
166 __whence = SEEK_SET;
167 else if (__dir == std::ios_base::cur)
168 __whence = SEEK_CUR;
169 else
170 __whence = SEEK_END;
171 #ifdef _GLIBCXX_USE_LFS
172 if (!fseeko64(_M_file, __off, __whence))
173 __ret = std::streampos(ftello64(_M_file));
174 #else
175 if (!fseek(_M_file, __off, __whence))
176 __ret = std::streampos(std::ftell(_M_file));
177 #endif
178 return __ret;
179 }
180
181 virtual std::streampos
182 seekpos(std::streampos __pos,
183 std::ios_base::openmode __mode =
184 std::ios_base::in | std::ios_base::out)
185 { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
186 };
187
188 template<>
189 inline stdio_sync_filebuf<char>::int_type
190 stdio_sync_filebuf<char>::syncgetc()
191 { return std::getc(_M_file); }
192
193 template<>
194 inline stdio_sync_filebuf<char>::int_type
195 stdio_sync_filebuf<char>::syncungetc(int_type __c)
196 { return std::ungetc(__c, _M_file); }
197
198 template<>
199 inline stdio_sync_filebuf<char>::int_type
200 stdio_sync_filebuf<char>::syncputc(int_type __c)
201 { return std::putc(__c, _M_file); }
202
203 template<>
204 inline std::streamsize
205 stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
206 {
207 std::streamsize __ret = std::fread(__s, 1, __n, _M_file);
208 if (__ret > 0)
209 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
210 else
211 _M_unget_buf = traits_type::eof();
212 return __ret;
213 }
214
215 template<>
216 inline std::streamsize
217 stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
218 { return std::fwrite(__s, 1, __n, _M_file); }
219
220 #ifdef _GLIBCXX_USE_WCHAR_T
221 template<>
222 inline stdio_sync_filebuf<wchar_t>::int_type
223 stdio_sync_filebuf<wchar_t>::syncgetc()
224 { return std::getwc(_M_file); }
225
226 template<>
227 inline stdio_sync_filebuf<wchar_t>::int_type
228 stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
229 { return std::ungetwc(__c, _M_file); }
230
231 template<>
232 inline stdio_sync_filebuf<wchar_t>::int_type
233 stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
234 { return std::putwc(__c, _M_file); }
235
236 template<>
237 inline std::streamsize
238 stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
239 {
240 std::streamsize __ret = 0;
241 const int_type __eof = traits_type::eof();
242 while (__n--)
243 {
244 int_type __c = this->syncgetc();
245 if (traits_type::eq_int_type(__c, __eof))
246 break;
247 __s[__ret] = traits_type::to_char_type(__c);
248 ++__ret;
249 }
250
251 if (__ret > 0)
252 _M_unget_buf = traits_type::to_int_type(__s[__ret - 1]);
253 else
254 _M_unget_buf = traits_type::eof();
255 return __ret;
256 }
257
258 template<>
259 inline std::streamsize
260 stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s,
261 std::streamsize __n)
262 {
263 std::streamsize __ret = 0;
264 const int_type __eof = traits_type::eof();
265 while (__n--)
266 {
267 if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
268 break;
269 ++__ret;
270 }
271 return __ret;
272 }
273 #endif
274
275 #if _GLIBCXX_EXTERN_TEMPLATE
276 extern template class stdio_sync_filebuf<char>;
277 #ifdef _GLIBCXX_USE_WCHAR_T
278 extern template class stdio_sync_filebuf<wchar_t>;
279 #endif
280 #endif
281
282 _GLIBCXX_END_NAMESPACE
283
284 #endif
This page took 0.051346 seconds and 5 git commands to generate.