| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_FILE_OUTPUTFILE_HPP |
| 4 |
#define MPT_IO_FILE_OUTPUTFILE_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/namespace.hpp" |
| 9 |
#include "mpt/io/io.hpp" |
| 10 |
#include "mpt/io/io_stdstream.hpp" |
| 11 |
#include "mpt/io_file/fstream.hpp" |
| 12 |
#include "mpt/path/os_path.hpp" |
| 13 |
#include "mpt/string/types.hpp" |
| 14 |
|
| 15 |
#include <exception> |
| 16 |
#include <ios> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
#if MPT_LIBCXX_MS |
| 20 |
#include <cstdio> |
| 21 |
#endif // MPT_LIBCXX_MS |
| 22 |
|
| 23 |
#if MPT_LIBCXX_MS |
| 24 |
#include <tchar.h> |
| 25 |
#endif // MPT_LIBCXX_MS |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
namespace mpt { |
| 30 |
inline namespace MPT_INLINE_NS { |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
namespace IO { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
enum class FlushMode { |
| 39 |
None = 0, // no explicit flushes at all |
| 40 |
Single = 1, // explicitly flush higher-leverl API layers |
| 41 |
Full = 2, // explicitly flush *all* layers, up to and including disk write caches |
| 42 |
}; |
| 43 |
|
| 44 |
inline FlushMode FlushModeFromBool(bool flush) { |
| 45 |
return flush ? FlushMode::Full : FlushMode::None; |
| 46 |
} |
| 47 |
|
| 48 |
class SafeOutputFile { |
| 49 |
|
| 50 |
private: |
| 51 |
FlushMode m_FlushMode; |
| 52 |
#if MPT_LIBCXX_MS |
| 53 |
std::FILE * m_f = nullptr; |
| 54 |
#else // !MPT_LIBCXX_MS |
| 55 |
mpt::IO::ofstream m_s; |
| 56 |
#endif // MPT_LIBCXX_MS |
| 57 |
#if MPT_LIBCXX_MS |
| 58 |
class FILEostream |
| 59 |
: public mpt::IO::ofstream { |
| 60 |
public: |
| 61 |
FILEostream(std::FILE * file) |
| 62 |
: mpt::IO::ofstream(file) { |
| 63 |
return; |
| 64 |
} |
| 65 |
}; |
| 66 |
|
| 67 |
FILEostream m_s; |
| 68 |
|
| 69 |
static mpt::tstring convert_mode(std::ios_base::openmode mode, FlushMode flushMode) { |
| 70 |
mpt::tstring fopen_mode; |
| 71 |
switch (mode & ~(std::ios_base::ate | std::ios_base::binary)) { |
| 72 |
case std::ios_base::in: |
| 73 |
fopen_mode = _T("r"); |
| 74 |
break; |
| 75 |
case std::ios_base::out: |
| 76 |
[[fallthrough]]; |
| 77 |
case std::ios_base::out | std::ios_base::trunc: |
| 78 |
fopen_mode = _T("w"); |
| 79 |
break; |
| 80 |
case std::ios_base::app: |
| 81 |
[[fallthrough]]; |
| 82 |
case std::ios_base::out | std::ios_base::app: |
| 83 |
fopen_mode = _T("a"); |
| 84 |
break; |
| 85 |
case std::ios_base::out | std::ios_base::in: |
| 86 |
fopen_mode = _T("r+"); |
| 87 |
break; |
| 88 |
case std::ios_base::out | std::ios_base::in | std::ios_base::trunc: |
| 89 |
fopen_mode = _T("w+"); |
| 90 |
break; |
| 91 |
case std::ios_base::out | std::ios_base::in | std::ios_base::app: |
| 92 |
[[fallthrough]]; |
| 93 |
case std::ios_base::in | std::ios_base::app: |
| 94 |
fopen_mode = _T("a+"); |
| 95 |
break; |
| 96 |
} |
| 97 |
if (fopen_mode.empty()) { |
| 98 |
return fopen_mode; |
| 99 |
} |
| 100 |
if (mode & std::ios_base::binary) { |
| 101 |
fopen_mode += _T("b"); |
| 102 |
} |
| 103 |
if (flushMode == FlushMode::Full) { |
| 104 |
fopen_mode += _T("c"); // force commit on fflush (MSVC specific) |
| 105 |
} |
| 106 |
return fopen_mode; |
| 107 |
} |
| 108 |
|
| 109 |
std::FILE * internal_fopen(const mpt::os_path & filename, std::ios_base::openmode mode, FlushMode flushMode) { |
| 110 |
m_f = nullptr; |
| 111 |
mpt::tstring fopen_mode = convert_mode(mode, flushMode); |
| 112 |
if (fopen_mode.empty()) { |
| 113 |
return nullptr; |
| 114 |
} |
| 115 |
std::FILE * f = |
| 116 |
#ifdef UNICODE |
| 117 |
_wfopen(mpt::support_long_path(mpt::transcode<mpt::tstring>(filename)).c_str(), fopen_mode.c_str()) |
| 118 |
#else |
| 119 |
std::fopen(mpt::support_long_path(mpt::transcode<mpt::tstring>(filename)).c_str(), fopen_mode.c_str()) |
| 120 |
#endif |
| 121 |
; |
| 122 |
if (!f) { |
| 123 |
return nullptr; |
| 124 |
} |
| 125 |
if (mode & std::ios_base::ate) { |
| 126 |
if (std::fseek(f, 0, SEEK_END) != 0) { |
| 127 |
std::fclose(f); |
| 128 |
f = nullptr; |
| 129 |
return nullptr; |
| 130 |
} |
| 131 |
} |
| 132 |
m_f = f; |
| 133 |
return f; |
| 134 |
} |
| 135 |
|
| 136 |
#endif // MPT_LIBCXX_MS |
| 137 |
|
| 138 |
public: |
| 139 |
SafeOutputFile() = delete; |
| 140 |
|
| 141 |
explicit SafeOutputFile(const mpt::os_path & filename, std::ios_base::openmode mode = std::ios_base::out, FlushMode flushMode = FlushMode::Full) |
| 142 |
: m_FlushMode(flushMode) |
| 143 |
#if MPT_LIBCXX_MS |
| 144 |
, m_s(internal_fopen(filename, mode | std::ios_base::out, flushMode)) |
| 145 |
#else // !MPT_LIBCXX_MS |
| 146 |
, m_s(filename, mode) |
| 147 |
#endif // MPT_LIBCXX_MS |
| 148 |
{ |
| 149 |
if (!stream().is_open()) { |
| 150 |
stream().setstate(mpt::IO::ofstream::failbit); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
mpt::IO::ofstream & stream() { |
| 155 |
return m_s; |
| 156 |
} |
| 157 |
|
| 158 |
operator mpt::IO::ofstream &() { |
| 159 |
return stream(); |
| 160 |
} |
| 161 |
|
| 162 |
const mpt::IO::ofstream & stream() const { |
| 163 |
return m_s; |
| 164 |
} |
| 165 |
|
| 166 |
operator const mpt::IO::ofstream &() const { |
| 167 |
return stream(); |
| 168 |
} |
| 169 |
|
| 170 |
operator bool() const { |
| 171 |
return stream() ? true : false; |
| 172 |
} |
| 173 |
|
| 174 |
bool operator!() const { |
| 175 |
return stream().operator!(); |
| 176 |
} |
| 177 |
|
| 178 |
// cppcheck-suppress exceptThrowInDestructor |
| 179 |
~SafeOutputFile() noexcept(false) { |
| 180 |
const bool mayThrow = (std::uncaught_exceptions() == 0); |
| 181 |
if (!stream()) { |
| 182 |
#if MPT_LIBCXX_MS |
| 183 |
if (m_f) { |
| 184 |
std::fclose(m_f); |
| 185 |
} |
| 186 |
#endif // MPT_LIBCXX_MS |
| 187 |
if (mayThrow && (stream().exceptions() & (std::ios::badbit | std::ios::failbit))) { |
| 188 |
// cppcheck-suppress exceptThrowInDestructor |
| 189 |
throw std::ios_base::failure(std::string("Error before flushing file buffers.")); |
| 190 |
} |
| 191 |
return; |
| 192 |
} |
| 193 |
if (!stream().rdbuf()) { |
| 194 |
#if MPT_LIBCXX_MS |
| 195 |
if (m_f) { |
| 196 |
std::fclose(m_f); |
| 197 |
} |
| 198 |
#endif // MPT_LIBCXX_MS |
| 199 |
if (mayThrow && (stream().exceptions() & (std::ios::badbit | std::ios::failbit))) { |
| 200 |
// cppcheck-suppress exceptThrowInDestructor |
| 201 |
throw std::ios_base::failure(std::string("Error before flushing file buffers.")); |
| 202 |
} |
| 203 |
return; |
| 204 |
} |
| 205 |
#if MPT_LIBCXX_MS |
| 206 |
if (!m_f) { |
| 207 |
return; |
| 208 |
} |
| 209 |
#endif // MPT_LIBCXX_MS |
| 210 |
bool errorOnFlush = false; |
| 211 |
if (m_FlushMode != FlushMode::None) { |
| 212 |
try { |
| 213 |
if (stream().rdbuf()->pubsync() != 0) { |
| 214 |
errorOnFlush = true; |
| 215 |
} |
| 216 |
} catch (const std::exception &) { |
| 217 |
errorOnFlush = true; |
| 218 |
#if MPT_LIBCXX_MS |
| 219 |
if (m_FlushMode != FlushMode::None) { |
| 220 |
if (std::fflush(m_f) != 0) { |
| 221 |
errorOnFlush = true; |
| 222 |
} |
| 223 |
} |
| 224 |
if (std::fclose(m_f) != 0) { |
| 225 |
errorOnFlush = true; |
| 226 |
} |
| 227 |
#endif // MPT_LIBCXX_MS |
| 228 |
if (mayThrow) { |
| 229 |
// ignore errorOnFlush here, and re-throw the earlier exception |
| 230 |
// cppcheck-suppress exceptThrowInDestructor |
| 231 |
throw; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
#if MPT_LIBCXX_MS |
| 236 |
if (m_FlushMode != FlushMode::None) { |
| 237 |
if (std::fflush(m_f) != 0) { |
| 238 |
errorOnFlush = true; |
| 239 |
} |
| 240 |
} |
| 241 |
if (std::fclose(m_f) != 0) { |
| 242 |
errorOnFlush = true; |
| 243 |
} |
| 244 |
#endif // MPT_LIBCXX_MS |
| 245 |
if (mayThrow && errorOnFlush && (stream().exceptions() & (std::ios::badbit | std::ios::failbit))) { |
| 246 |
// cppcheck-suppress exceptThrowInDestructor |
| 247 |
throw std::ios_base::failure(std::string("Error flushing file buffers.")); |
| 248 |
} |
| 249 |
} |
| 250 |
}; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
} // namespace IO |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
} // namespace MPT_INLINE_NS |
| 259 |
} // namespace mpt |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
#endif // MPT_IO_FILE_OUTPUTFILE_HPP |