]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/ext/stdio_sync_filebuf.h
Move from CPP to CXX.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / stdio_sync_filebuf.h
1 // Iostreams wrapper for stdio FILE* -*- C++ -*-
2
3 // Copyright (C) 2003 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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/stdiostream.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 <fstream>
40 #include <unistd.h>
41
42 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
43 # include <sys/stat.h>
44 # ifdef _GLIBCXX_HAVE_S_ISREG
45 # define _GLIBCXX_ISREG(x) S_ISREG(x)
46 # else
47 # define _GLIBCXX_ISREG(x) (((x) & S_IFMT) == S_IFREG)
48 # endif
49 #endif
50
51 #ifdef _GLIBCXX_USE_WCHAR_T
52 #include <cwchar>
53 #endif
54
55 namespace __gnu_cxx
56 {
57 template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
58 class stdio_sync_filebuf : public std::basic_streambuf<_CharT, _Traits>
59 {
60 private:
61 std::__c_file* const _M_file;
62
63 public:
64 // Types:
65 typedef _CharT char_type;
66 typedef _Traits traits_type;
67 typedef typename traits_type::int_type int_type;
68 typedef typename traits_type::pos_type pos_type;
69 typedef typename traits_type::off_type off_type;
70
71 explicit
72 stdio_sync_filebuf(std::__c_file* __f) : _M_file(__f) { }
73
74 protected:
75
76 int_type
77 syncgetc();
78
79 int_type
80 syncungetc(int_type __c);
81
82 int_type
83 syncputc(int_type __c);
84
85 virtual int_type
86 underflow()
87 {
88 int_type __c = this->syncgetc();
89 return this->syncungetc(__c);
90 }
91
92 virtual int_type
93 uflow()
94 { return this->syncgetc(); }
95
96 virtual int_type
97 pbackfail(int_type __c = traits_type::eof())
98 { return this->syncungetc(__c); }
99
100 virtual std::streamsize
101 xsgetn(char_type* __s, std::streamsize __n);
102
103 virtual std::streamsize
104 showmanyc()
105 {
106 #if defined(_GLIBCXX_HAVE_S_ISREG) || defined(_GLIBCXX_HAVE_S_IFREG)
107 // Regular files.
108 struct stat __buffer;
109 int __ret = fstat(fileno(_M_file), &__buffer);
110 if (!__ret && _GLIBCXX_ISREG(__buffer.st_mode))
111 return __buffer.st_size - ftell(_M_file);
112 #endif
113 return 0;
114 }
115
116 virtual int_type
117 overflow(int_type __c = traits_type::eof())
118 {
119 int_type __ret;
120 if (traits_type::eq_int_type(__c, traits_type::eof()))
121 {
122 if (std::fflush(_M_file))
123 __ret = traits_type::eof();
124 else
125 __ret = traits_type::not_eof(__c);
126 }
127 else
128 __ret = this->syncputc(__c);
129 return __ret;
130 }
131
132 virtual std::streamsize
133 xsputn(const char_type* __s, std::streamsize __n);
134
135 virtual int
136 sync()
137 { return std::fflush(_M_file); }
138
139 virtual std::streampos
140 seekoff(std::streamoff __off, std::ios_base::seekdir __dir,
141 std::ios_base::openmode = std::ios_base::in | std::ios_base::out)
142 {
143 std::streampos __ret(std::streamoff(-1));
144 int __whence;
145 if (__dir == std::ios_base::beg)
146 __whence = SEEK_SET;
147 else if (__dir == std::ios_base::cur)
148 __whence = SEEK_CUR;
149 else
150 __whence = SEEK_END;
151
152 if (!fseek(_M_file, __off, __whence))
153 __ret = std::streampos(std::ftell(_M_file));
154 return __ret;
155 }
156
157 virtual std::streampos
158 seekpos(std::streampos __pos,
159 std::ios_base::openmode __mode =
160 std::ios_base::in | std::ios_base::out)
161 { return seekoff(std::streamoff(__pos), std::ios_base::beg, __mode); }
162 };
163
164 template<>
165 inline stdio_sync_filebuf<char>::int_type
166 stdio_sync_filebuf<char>::syncgetc()
167 { return std::getc(_M_file); }
168
169 template<>
170 inline stdio_sync_filebuf<char>::int_type
171 stdio_sync_filebuf<char>::syncungetc(int_type __c)
172 { return std::ungetc(__c, _M_file); }
173
174 template<>
175 inline stdio_sync_filebuf<char>::int_type
176 stdio_sync_filebuf<char>::syncputc(int_type __c)
177 { return std::putc(__c, _M_file); }
178
179 template<>
180 inline std::streamsize
181 stdio_sync_filebuf<char>::xsgetn(char* __s, std::streamsize __n)
182 { return std::fread(__s, 1, __n, _M_file); }
183
184 template<>
185 inline std::streamsize
186 stdio_sync_filebuf<char>::xsputn(const char* __s, std::streamsize __n)
187 { return std::fwrite(__s, 1, __n, _M_file); }
188
189 #ifdef _GLIBCXX_USE_WCHAR_T
190 template<>
191 inline stdio_sync_filebuf<wchar_t>::int_type
192 stdio_sync_filebuf<wchar_t>::syncgetc()
193 { return std::getwc(_M_file); }
194
195 template<>
196 inline stdio_sync_filebuf<wchar_t>::int_type
197 stdio_sync_filebuf<wchar_t>::syncungetc(int_type __c)
198 { return std::ungetwc(__c, _M_file); }
199
200 template<>
201 inline stdio_sync_filebuf<wchar_t>::int_type
202 stdio_sync_filebuf<wchar_t>::syncputc(int_type __c)
203 { return std::putwc(__c, _M_file); }
204
205 template<>
206 inline std::streamsize
207 stdio_sync_filebuf<wchar_t>::xsgetn(wchar_t* __s, std::streamsize __n)
208 {
209 std::streamsize __ret = 0;
210 const int_type __eof = traits_type::eof();
211 while (__n--)
212 {
213 int_type __c = this->syncgetc();
214 if (traits_type::eq_int_type(__c, __eof))
215 break;
216 *__s++ = __c;
217 ++__ret;
218 }
219 return __ret;
220 }
221
222 template<>
223 inline std::streamsize
224 stdio_sync_filebuf<wchar_t>::xsputn(const wchar_t* __s,
225 std::streamsize __n)
226 {
227 std::streamsize __ret = 0;
228 const int_type __eof = traits_type::eof();
229 while (__n--)
230 {
231 if (traits_type::eq_int_type(this->syncputc(*__s++), __eof))
232 break;
233 ++__ret;
234 }
235 return __ret;
236 }
237 #endif
238
239 #if _GLIBCXX_EXTERN_TEMPLATE
240 extern template class stdio_sync_filebuf<char>;
241 #ifdef _GLIBCXX_USE_WCHAR_T
242 extern template class stdio_sync_filebuf<wchar_t>;
243 #endif
244 #endif
245 } // namespace __gnu_cxx
246
247 #endif