Debian Codegraph @

Utilities/KWSys/FStream.hxx.in

raw
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
3 #ifndef @KWSYS_NAMESPACE@_FStream_hxx
4 #define @KWSYS_NAMESPACE@_FStream_hxx
5
6 #include <@KWSYS_NAMESPACE@/Configure.hxx>
7
8 #include <@KWSYS_NAMESPACE@/Encoding.hxx>
9
10 #include <fstream>
11 #if defined(_WIN32)
12 # if !defined(_MSC_VER) && @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H
13 # include <ext/stdio_filebuf.h>
14 # endif
15 #endif
16
17 namespace @KWSYS_NAMESPACE@ {
18 #if defined(_WIN32) && \
19 (defined(_MSC_VER) || @KWSYS_NAMESPACE@_CXX_HAS_EXT_STDIO_FILEBUF_H)
20 # if defined(_NOEXCEPT)
21 # define @KWSYS_NAMESPACE@_FStream_NOEXCEPT _NOEXCEPT
22 # else
23 # define @KWSYS_NAMESPACE@_FStream_NOEXCEPT
24 # endif
25
26 # if defined(_MSC_VER)
27
28 template <typename CharType, typename Traits>
29 class basic_filebuf : public std::basic_filebuf<CharType, Traits>
30 {
31 # if _MSC_VER >= 1400
32 public:
33 typedef std::basic_filebuf<CharType, Traits> my_base_type;
34 basic_filebuf* open(char const* s, std::ios_base::openmode mode)
35 {
36 const std::wstring wstr = Encoding::ToWindowsExtendedPath(s);
37 return static_cast<basic_filebuf*>(my_base_type::open(wstr.c_str(), mode));
38 }
39 # endif
40 };
41
42 # else
43
44 inline std::wstring getcmode(const std::ios_base::openmode mode)
45 {
46 std::wstring cmode;
47 bool plus = false;
48 if (mode & std::ios_base::app) {
49 cmode += L"a";
50 plus = mode & std::ios_base::in ? true : false;
51 } else if (mode & std::ios_base::trunc ||
52 (mode & std::ios_base::out && (mode & std::ios_base::in) == 0)) {
53 cmode += L"w";
54 plus = mode & std::ios_base::in ? true : false;
55 } else {
56 cmode += L"r";
57 plus = mode & std::ios_base::out ? true : false;
58 }
59 if (plus) {
60 cmode += L"+";
61 }
62 if (mode & std::ios_base::binary) {
63 cmode += L"b";
64 } else {
65 cmode += L"t";
66 }
67 return cmode;
68 };
69
70 # endif
71
72 template <typename CharType, typename Traits = std::char_traits<CharType> >
73 class basic_efilebuf
74 {
75 public:
76 # if defined(_MSC_VER)
77 typedef basic_filebuf<CharType, Traits> internal_buffer_type;
78 # else
79 typedef __gnu_cxx::stdio_filebuf<CharType, Traits> internal_buffer_type;
80 # endif
81
82 basic_efilebuf()
83 : file_(0)
84 {
85 buf_ = 0;
86 }
87
88 bool _open(char const* file_name, std::ios_base::openmode mode)
89 {
90 if (_is_open() || file_) {
91 return false;
92 }
93 # if defined(_MSC_VER)
94 const bool success = buf_->open(file_name, mode) != 0;
95 # else
96 const std::wstring wstr = Encoding::ToWindowsExtendedPath(file_name);
97 bool success = false;
98 std::wstring cmode = getcmode(mode);
99 file_ = _wfopen(wstr.c_str(), cmode.c_str());
100 if (file_) {
101 if (buf_) {
102 delete buf_;
103 }
104 buf_ = new internal_buffer_type(file_, mode);
105 success = true;
106 }
107 # endif
108 return success;
109 }
110
111 bool _is_open()
112 {
113 if (!buf_) {
114 return false;
115 }
116 return buf_->is_open();
117 }
118
119 bool _is_open() const
120 {
121 if (!buf_) {
122 return false;
123 }
124 return buf_->is_open();
125 }
126
127 bool _close()
128 {
129 bool success = false;
130 if (buf_) {
131 success = buf_->close() != 0;
132 # if !defined(_MSC_VER)
133 if (file_) {
134 success = fclose(file_) == 0 ? success : false;
135 file_ = 0;
136 }
137 # endif
138 }
139 return success;
140 }
141
142 static void _set_state(bool success, std::basic_ios<CharType, Traits>* ios,
143 basic_efilebuf* efilebuf)
144 {
145 # if !defined(_MSC_VER)
146 ios->rdbuf(efilebuf->buf_);
147 # else
148 static_cast<void>(efilebuf);
149 # endif
150 if (!success) {
151 ios->setstate(std::ios_base::failbit);
152 } else {
153 ios->clear();
154 }
155 }
156
157 ~basic_efilebuf()
158 {
159 if (buf_) {
160 delete buf_;
161 }
162 }
163
164 protected:
165 internal_buffer_type* buf_;
166 FILE* file_;
167 };
168
169 template <typename CharType, typename Traits = std::char_traits<CharType> >
170 class basic_fstream
171 : public std::basic_iostream<CharType, Traits>
172 , public basic_efilebuf<CharType, Traits>
173 {
174 public:
175 typedef typename basic_efilebuf<CharType, Traits>::internal_buffer_type
176 internal_buffer_type;
177 typedef std::basic_iostream<CharType, Traits> internal_stream_type;
178
179 basic_fstream()
180 : internal_stream_type(new internal_buffer_type())
181 {
182 this->buf_ =
183 static_cast<internal_buffer_type*>(internal_stream_type::rdbuf());
184 }
185 explicit basic_fstream(char const* file_name,
186 std::ios_base::openmode mode = std::ios_base::in |
187 std::ios_base::out)
188 : internal_stream_type(new internal_buffer_type())
189 {
190 this->buf_ =
191 static_cast<internal_buffer_type*>(internal_stream_type::rdbuf());
192 open(file_name, mode);
193 }
194
195 void open(char const* file_name,
196 std::ios_base::openmode mode = std::ios_base::in |
197 std::ios_base::out)
198 {
199 this->_set_state(this->_open(file_name, mode), this, this);
200 }
201
202 bool is_open() { return this->_is_open(); }
203
204 void close() { this->_set_state(this->_close(), this, this); }
205
206 using basic_efilebuf<CharType, Traits>::_is_open;
207
208 internal_buffer_type* rdbuf() const { return this->buf_; }
209
210 ~basic_fstream() @KWSYS_NAMESPACE@_FStream_NOEXCEPT { close(); }
211 };
212
213 template <typename CharType, typename Traits = std::char_traits<CharType> >
214 class basic_ifstream
215 : public std::basic_istream<CharType, Traits>
216 , public basic_efilebuf<CharType, Traits>
217 {
218 public:
219 typedef typename basic_efilebuf<CharType, Traits>::internal_buffer_type
220 internal_buffer_type;
221 typedef std::basic_istream<CharType, Traits> internal_stream_type;
222
223 basic_ifstream()
224 : internal_stream_type(new internal_buffer_type())
225 {
226 this->buf_ =
227 static_cast<internal_buffer_type*>(internal_stream_type::rdbuf());
228 }
229 explicit basic_ifstream(char const* file_name,
230 std::ios_base::openmode mode = std::ios_base::in)
231 : internal_stream_type(new internal_buffer_type())
232 {
233 this->buf_ =
234 static_cast<internal_buffer_type*>(internal_stream_type::rdbuf());
235 open(file_name, mode);
236 }
237
238 void open(char const* file_name,
239 std::ios_base::openmode mode = std::ios_base::in)
240 {
241 mode = mode | std::ios_base::in;
242 this->_set_state(this->_open(file_name, mode), this, this);
243 }
244
245 bool is_open() { return this->_is_open(); }
246
247 void close() { this->_set_state(this->_close(), this, this); }
248
249 using basic_efilebuf<CharType, Traits>::_is_open;
250
251 internal_buffer_type* rdbuf() const { return this->buf_; }
252
253 ~basic_ifstream() @KWSYS_NAMESPACE@_FStream_NOEXCEPT { close(); }
254 };
255
256 template <typename CharType, typename Traits = std::char_traits<CharType> >
257 class basic_ofstream
258 : public std::basic_ostream<CharType, Traits>
259 , public basic_efilebuf<CharType, Traits>
260 {
261 using basic_efilebuf<CharType, Traits>::_is_open;
262
263 public:
264 typedef typename basic_efilebuf<CharType, Traits>::internal_buffer_type
265 internal_buffer_type;
266 typedef std::basic_ostream<CharType, Traits> internal_stream_type;
267
268 basic_ofstream()
269 : internal_stream_type(new internal_buffer_type())
270 {
271 this->buf_ =
272 static_cast<internal_buffer_type*>(internal_stream_type::rdbuf());
273 }
274 explicit basic_ofstream(char const* file_name,
275 std::ios_base::openmode mode = std::ios_base::out)
276 : internal_stream_type(new internal_buffer_type())
277 {
278 this->buf_ =
279 static_cast<internal_buffer_type*>(internal_stream_type::rdbuf());
280 open(file_name, mode);
281 }
282 void open(char const* file_name,
283 std::ios_base::openmode mode = std::ios_base::out)
284 {
285 mode = mode | std::ios_base::out;
286 this->_set_state(this->_open(file_name, mode), this, this);
287 }
288
289 void close() { this->_set_state(this->_close(), this, this); }
290
291 bool is_open() { return this->_is_open(); }
292
293 internal_buffer_type* rdbuf() const { return this->buf_; }
294
295 ~basic_ofstream() @KWSYS_NAMESPACE@_FStream_NOEXCEPT { close(); }
296 };
297
298 typedef basic_fstream<char> fstream;
299 typedef basic_ifstream<char> ifstream;
300 typedef basic_ofstream<char> ofstream;
301
302 # undef @KWSYS_NAMESPACE@_FStream_NOEXCEPT
303 #else
304 using std::fstream;
305 using std::ofstream;
306 using std::ifstream;
307 #endif
308
309 namespace FStream {
310 enum BOM
311 {
312 BOM_None,
313 BOM_UTF8,
314 BOM_UTF16BE,
315 BOM_UTF16LE,
316 BOM_UTF32BE,
317 BOM_UTF32LE
318 };
319
320 // Read a BOM, if one exists.
321 // If a BOM exists, the stream is advanced to after the BOM.
322 // This function requires a seekable stream (but not a relative
323 // seekable stream).
324 @KWSYS_NAMESPACE@_EXPORT BOM ReadBOM(std::istream& in);
325 }
326 }
327
328 #endif