| 1 |
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */ |
| 2 |
|
| 3 |
#ifndef MPT_IO_FILE_INPUTFILE_HPP |
| 4 |
#define MPT_IO_FILE_INPUTFILE_HPP |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "mpt/base/alloc.hpp" |
| 9 |
#include "mpt/base/namespace.hpp" |
| 10 |
#include "mpt/base/saturate_cast.hpp" |
| 11 |
#include "mpt/base/span.hpp" |
| 12 |
#include "mpt/io/io.hpp" |
| 13 |
#include "mpt/io/io_stdstream.hpp" |
| 14 |
#include "mpt/io_file/fstream.hpp" |
| 15 |
#include "mpt/path/os_path.hpp" |
| 16 |
|
| 17 |
#include <ios> |
| 18 |
#include <istream> |
| 19 |
#include <variant> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
#include <cassert> |
| 23 |
#include <cstddef> |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
namespace mpt { |
| 28 |
inline namespace MPT_INLINE_NS { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
namespace IO { |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class UncachedInputFile { |
| 37 |
|
| 38 |
private: |
| 39 |
mpt::os_path m_Filename; |
| 40 |
mpt::IO::ifstream m_File; |
| 41 |
bool m_IsValid; |
| 42 |
|
| 43 |
public: |
| 44 |
UncachedInputFile(const mpt::os_path & filename) |
| 45 |
: m_Filename(filename) |
| 46 |
, m_File(m_Filename, std::ios::binary | std::ios::in) |
| 47 |
, m_IsValid(true) { |
| 48 |
assert(!m_Filename.empty()); |
| 49 |
} |
| 50 |
|
| 51 |
~UncachedInputFile() = default; |
| 52 |
|
| 53 |
bool IsValid() const { |
| 54 |
return m_IsValid && m_File.good(); |
| 55 |
} |
| 56 |
|
| 57 |
mpt::os_path GetFilename() const { |
| 58 |
return m_Filename; |
| 59 |
} |
| 60 |
|
| 61 |
std::istream & GetStream() { |
| 62 |
return m_File; |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
class CachedInputFile { |
| 69 |
|
| 70 |
private: |
| 71 |
mpt::os_path m_Filename; |
| 72 |
mpt::IO::ifstream m_File; |
| 73 |
bool m_IsValid; |
| 74 |
bool m_IsCached; |
| 75 |
std::vector<std::byte> m_Cache; |
| 76 |
|
| 77 |
public: |
| 78 |
CachedInputFile(const mpt::os_path & filename) |
| 79 |
: m_Filename(filename) |
| 80 |
, m_File(m_Filename, std::ios::binary | std::ios::in) |
| 81 |
, m_IsValid(false) |
| 82 |
, m_IsCached(false) { |
| 83 |
assert(!filename.empty()); |
| 84 |
if (mpt::IO::IsReadSeekable(m_File)) { |
| 85 |
if (!mpt::IO::SeekEnd(m_File)) { |
| 86 |
m_File.close(); |
| 87 |
return; |
| 88 |
} |
| 89 |
mpt::IO::Offset filesize = mpt::IO::TellRead(m_File); |
| 90 |
if (!mpt::IO::SeekBegin(m_File)) { |
| 91 |
m_File.close(); |
| 92 |
return; |
| 93 |
} |
| 94 |
if (mpt::in_range<std::size_t>(filesize)) { |
| 95 |
std::size_t buffersize = mpt::saturate_cast<std::size_t>(filesize); |
| 96 |
m_Cache.resize(buffersize); |
| 97 |
if (mpt::IO::ReadRaw(m_File, mpt::as_span(m_Cache)).size() != mpt::saturate_cast<std::size_t>(filesize)) { |
| 98 |
m_File.close(); |
| 99 |
return; |
| 100 |
} |
| 101 |
if (!mpt::IO::SeekBegin(m_File)) { |
| 102 |
m_File.close(); |
| 103 |
return; |
| 104 |
} |
| 105 |
m_IsCached = true; |
| 106 |
m_IsValid = true; |
| 107 |
return; |
| 108 |
} |
| 109 |
} |
| 110 |
m_IsValid = true; |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
~CachedInputFile() = default; |
| 115 |
|
| 116 |
bool IsValid() const { |
| 117 |
return m_IsValid && m_File.good(); |
| 118 |
} |
| 119 |
|
| 120 |
bool IsCached() const { |
| 121 |
return m_IsCached; |
| 122 |
} |
| 123 |
|
| 124 |
mpt::os_path GetFilename() const { |
| 125 |
return m_Filename; |
| 126 |
} |
| 127 |
|
| 128 |
std::istream & GetStream() { |
| 129 |
assert(!m_IsCached); |
| 130 |
return m_File; |
| 131 |
} |
| 132 |
|
| 133 |
mpt::const_byte_span GetCache() { |
| 134 |
assert(m_IsCached); |
| 135 |
return mpt::as_span(m_Cache); |
| 136 |
} |
| 137 |
}; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
class InputFile { |
| 142 |
|
| 143 |
private: |
| 144 |
std::variant<std::monostate, UncachedInputFile, CachedInputFile> m_impl; |
| 145 |
|
| 146 |
public: |
| 147 |
InputFile(const mpt::os_path & filename, bool enable_cache = false) { |
| 148 |
if (enable_cache) { |
| 149 |
m_impl.emplace<CachedInputFile>(filename); |
| 150 |
} else { |
| 151 |
m_impl.emplace<UncachedInputFile>(filename); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
~InputFile() = default; |
| 156 |
|
| 157 |
bool IsValid() const { |
| 158 |
if (std::holds_alternative<UncachedInputFile>(m_impl)) { |
| 159 |
return std::get<UncachedInputFile>(m_impl).IsValid(); |
| 160 |
} else if (std::holds_alternative<CachedInputFile>(m_impl)) { |
| 161 |
return std::get<CachedInputFile>(m_impl).IsValid(); |
| 162 |
} |
| 163 |
return false; |
| 164 |
} |
| 165 |
|
| 166 |
bool IsCached() const { |
| 167 |
if (std::holds_alternative<UncachedInputFile>(m_impl)) { |
| 168 |
return false; |
| 169 |
} else if (std::holds_alternative<CachedInputFile>(m_impl)) { |
| 170 |
return std::get<CachedInputFile>(m_impl).IsCached(); |
| 171 |
} |
| 172 |
throw false; |
| 173 |
} |
| 174 |
|
| 175 |
mpt::os_path GetFilename() const { |
| 176 |
if (std::holds_alternative<UncachedInputFile>(m_impl)) { |
| 177 |
return std::get<UncachedInputFile>(m_impl).GetFilename(); |
| 178 |
} else if (std::holds_alternative<CachedInputFile>(m_impl)) { |
| 179 |
return std::get<CachedInputFile>(m_impl).GetFilename(); |
| 180 |
} |
| 181 |
return {}; |
| 182 |
} |
| 183 |
|
| 184 |
std::istream & GetStream() { |
| 185 |
if (std::holds_alternative<UncachedInputFile>(m_impl)) { |
| 186 |
return std::get<UncachedInputFile>(m_impl).GetStream(); |
| 187 |
} else if (std::holds_alternative<CachedInputFile>(m_impl)) { |
| 188 |
return std::get<CachedInputFile>(m_impl).GetStream(); |
| 189 |
} |
| 190 |
throw std::bad_variant_access(); |
| 191 |
} |
| 192 |
|
| 193 |
mpt::const_byte_span GetCache() { |
| 194 |
if (std::holds_alternative<UncachedInputFile>(m_impl)) { |
| 195 |
throw std::bad_variant_access(); |
| 196 |
} else if (std::holds_alternative<CachedInputFile>(m_impl)) { |
| 197 |
return std::get<CachedInputFile>(m_impl).GetCache(); |
| 198 |
} |
| 199 |
throw std::bad_variant_access(); |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
} // namespace IO |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
} // namespace MPT_INLINE_NS |
| 210 |
} // namespace mpt |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
#endif // MPT_IO_FILE_INPUTFILE_HPP |