1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/*
* mptFileIO.h
* -----------
* Purpose: A wrapper around std::fstream, enforcing usage of mpt::PathString.
* Notes : You should only ever use these wrappers instead of plain std::fstream classes.
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#pragma once
#include "openmpt/all/BuildSettings.hpp"
#if defined(MPT_ENABLE_FILEIO)
#include "mpt/io_read/filecursor_memory.hpp"
#include "mpt/io_read/filecursor_stdstream.hpp"
#include "../common/mptString.h"
#include "../common/mptPathString.h"
#include "../common/FileReaderFwd.h"
#if defined(MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR)
#if MPT_GCC_AT_LEAST(9,1,0)
#include <filesystem>
#endif // MPT_GCC_AT_LEAST(9,1,0)
#endif // MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR
#include <fstream>
#include <ios>
#include <ostream>
#include <streambuf>
#include <utility>
#if MPT_COMPILER_MSVC
#include <cstdio>
#endif // !MPT_COMPILER_MSVC
#ifdef MODPLUG_TRACKER
#if MPT_OS_WINDOWS
#include <windows.h>
#endif // MPT_OS_WINDOWS
#endif // MODPLUG_TRACKER
#endif // MPT_ENABLE_FILEIO
OPENMPT_NAMESPACE_BEGIN
#if defined(MPT_ENABLE_FILEIO)
// Sets the NTFS compression attribute on the file or directory.
// Requires read and write permissions for already opened files.
// Returns true if the attribute has been set.
// In almost all cases, the return value should be ignored because most filesystems other than NTFS do not support compression.
#ifdef MODPLUG_TRACKER
#if MPT_OS_WINDOWS
bool SetFilesystemCompression(HANDLE hFile);
bool SetFilesystemCompression(int fd);
bool SetFilesystemCompression(const mpt::PathString &filename);
#endif // MPT_OS_WINDOWS
#endif // MODPLUG_TRACKER
namespace mpt
{
namespace detail
{
template<typename Tbase>
inline void fstream_open(Tbase & base, const mpt::PathString & filename, std::ios_base::openmode mode)
{
#if defined(MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR)
#if MPT_GCC_AT_LEAST(9,1,0)
base.open(static_cast<std::filesystem::path>(filename.AsNative()), mode);
#else // !MPT_GCC_AT_LEAST(9,1,0)
// Warning: MinGW with GCC earlier than 9.1 detected. Standard library does neither provide std::fstream wchar_t overloads nor std::filesystem with wchar_t support. Unicode filename support is thus unavailable.
base.open(mpt::ToCharset(mpt::Charset::Locale, filename.AsNative()).c_str(), mode);
#endif // MPT_GCC_AT_LEAST(9,1,0)
#else // !MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR
base.open(filename.AsNativePrefixed().c_str(), mode);
#endif // MPT_COMPILER_QUIRK_WINDOWS_FSTREAM_NO_WCHAR
}
} // namespace detail
// We cannot rely on implicit conversion of mpt::PathString to std::filesystem::path when constructing std::fstream
// because of broken overload implementation in GCC libstdc++ 8, 9, 10.
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95642
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90704
class fstream
: public std::fstream
{
private:
typedef std::fstream Tbase;
public:
fstream() {}
fstream(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
{
detail::fstream_open<Tbase>(*this, filename, mode);
}
#if MPT_COMPILER_MSVC
protected:
fstream(std::FILE * file)
: std::fstream(file)
{
}
#endif // MPT_COMPILER_MSVC
public:
void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out)
{
detail::fstream_open<Tbase>(*this, filename, mode);
}
void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
#if MPT_OS_WINDOWS
void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out) = delete;
#endif
};
class ifstream
: public std::ifstream
{
private:
typedef std::ifstream Tbase;
public:
ifstream() {}
ifstream(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in)
{
detail::fstream_open<Tbase>(*this, filename, mode);
}
#if MPT_COMPILER_MSVC
protected:
ifstream(std::FILE * file)
: std::ifstream(file)
{
}
#endif // MPT_COMPILER_MSVC
public:
void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::in)
{
detail::fstream_open<Tbase>(*this, filename, mode);
}
void open(const char * filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
#if MPT_OS_WINDOWS
void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::in) = delete;
#endif
};
class ofstream
: public std::ofstream
{
private:
typedef std::ofstream Tbase;
public:
ofstream() {}
ofstream(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::out)
{
detail::fstream_open<Tbase>(*this, filename, mode);
}
#if MPT_COMPILER_MSVC
protected:
ofstream(std::FILE * file)
: std::ofstream(file)
{
}
#endif // MPT_COMPILER_MSVC
public:
void open(const mpt::PathString & filename, std::ios_base::openmode mode = std::ios_base::out)
{
detail::fstream_open<Tbase>(*this, filename, mode);
}
void open(const char * filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
void open(const std::string & filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
#if MPT_OS_WINDOWS
void open(const wchar_t * filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
void open(const std::wstring & filename, std::ios_base::openmode mode = std::ios_base::out) = delete;
#endif
};
enum class FlushMode
{
None = 0, // no explicit flushes at all
Single = 1, // explicitly flush higher-leverl API layers
Full = 2, // explicitly flush *all* layers, up to and including disk write caches
};
inline FlushMode FlushModeFromBool(bool flush)
{
return flush ? FlushMode::Full : FlushMode::None;
}
#ifdef MODPLUG_TRACKER
class SafeOutputFile
{
private:
FlushMode m_FlushMode;
#if MPT_COMPILER_MSVC
std::FILE *m_f = nullptr;
#else // !MPT_COMPILER_MSVC
mpt::ofstream m_s;
#endif // MPT_COMPILER_MSVC
#if MPT_COMPILER_MSVC
class FILEostream
: public mpt::ofstream
{
public:
FILEostream(std::FILE * file)
: mpt::ofstream(file)
{
return;
}
};
FILEostream m_s;
static mpt::tstring convert_mode(std::ios_base::openmode mode, FlushMode flushMode);
std::FILE * internal_fopen(const mpt::PathString &filename, std::ios_base::openmode mode, FlushMode flushMode);
#endif // MPT_COMPILER_MSVC
public:
SafeOutputFile() = delete;
explicit SafeOutputFile(const mpt::PathString &filename, std::ios_base::openmode mode = std::ios_base::out, FlushMode flushMode = FlushMode::Full)
: m_FlushMode(flushMode)
#if MPT_COMPILER_MSVC
, m_s(internal_fopen(filename, mode | std::ios_base::out, flushMode))
#else // !MPT_COMPILER_MSVC
, m_s(filename, mode)
#endif // MPT_COMPILER_MSVC
{
if(!stream().is_open())
{
stream().setstate(mpt::ofstream::failbit);
}
}
mpt::ofstream& stream()
{
return m_s;
}
operator mpt::ofstream& ()
{
return stream();
}
const mpt::ofstream& stream() const
{
return m_s;
}
operator const mpt::ofstream& () const
{
return stream();
}
operator bool() const
{
return stream() ? true : false;
}
bool operator!() const
{
return stream().operator!();
}
~SafeOutputFile() noexcept(false);
};
#endif // MODPLUG_TRACKER
#ifdef MODPLUG_TRACKER
// LazyFileRef is a simple reference to an on-disk file by the means of a
// filename which allows easy assignment of the whole file contents to and from
// byte buffers.
class LazyFileRef {
private:
const mpt::PathString m_Filename;
public:
LazyFileRef(const mpt::PathString &filename)
: m_Filename(filename)
{
return;
}
public:
LazyFileRef & operator = (const std::vector<std::byte> &data);
LazyFileRef & operator = (const std::vector<char> &data);
LazyFileRef & operator = (const std::string &data);
operator std::vector<std::byte> () const;
operator std::vector<char> () const;
operator std::string () const;
};
#endif // MODPLUG_TRACKER
} // namespace mpt
class InputFile
{
private:
mpt::PathString m_Filename;
mpt::ifstream m_File;
bool m_IsValid;
bool m_IsCached;
std::vector<std::byte> m_Cache;
public:
InputFile(const mpt::PathString &filename, bool allowWholeFileCaching = false);
~InputFile();
bool IsValid() const;
bool IsCached() const;
mpt::PathString GetFilename() const;
std::istream& GetStream();
mpt::const_byte_span GetCache();
private:
bool Open(const mpt::PathString &filename, bool allowWholeFileCaching = false);
};
template <typename Targ1>
inline FileCursor make_FileCursor(Targ1 &&arg1)
{
return mpt::IO::make_FileCursor<mpt::PathString>(std::forward<Targ1>(arg1));
}
template <typename Targ1, typename Targ2>
inline FileCursor make_FileCursor(Targ1 &&arg1, Targ2 &&arg2)
{
return mpt::IO::make_FileCursor<mpt::PathString>(std::forward<Targ1>(arg1), std::forward<Targ2>(arg2));
}
// templated in order to reduce header inter-dependencies
class InputFile;
template <typename TInputFile, std::enable_if_t<std::is_same<TInputFile, InputFile>::value, bool> = true>
inline FileCursor make_FileCursor(TInputFile &file)
{
if(!file.IsValid())
{
return FileCursor();
}
if(file.IsCached())
{
return mpt::IO::make_FileCursor<mpt::PathString>(file.GetCache(), std::make_shared<mpt::PathString>(file.GetFilename()));
} else
{
return mpt::IO::make_FileCursor<mpt::PathString>(file.GetStream(), std::make_shared<mpt::PathString>(file.GetFilename()));
}
}
template <typename Targ1>
inline FileCursor GetFileReader(Targ1 &&arg1)
{
return make_FileCursor(std::forward<Targ1>(arg1));
}
template <typename Targ1, typename Targ2>
inline FileCursor GetFileReader(Targ1 &&arg1, Targ2 &&arg2)
{
return make_FileCursor(std::forward<Targ1>(arg1), std::forward<Targ2>(arg2));
}
#if defined(MODPLUG_TRACKER) && MPT_OS_WINDOWS
class OnDiskFileWrapper
{
private:
mpt::PathString m_Filename;
bool m_IsTempFile;
public:
OnDiskFileWrapper(FileCursor& file, const mpt::PathString& fileNameExtension = P_("tmp"));
~OnDiskFileWrapper();
public:
bool IsValid() const;
mpt::PathString GetFilename() const;
}; // class OnDiskFileWrapper
#endif // MODPLUG_TRACKER && MPT_OS_WINDOWS
#endif // MPT_ENABLE_FILEIO
OPENMPT_NAMESPACE_END
|