| 1 |
#ifndef FILE_HH |
| 2 |
#define FILE_HH |
| 3 |
|
| 4 |
#include "FileBase.hh" |
| 5 |
#include "MappedFile.hh" |
| 6 |
|
| 7 |
#include "zstring_view.hh" |
| 8 |
|
| 9 |
#include <bit> |
| 10 |
#include <cstdint> |
| 11 |
#include <ctime> |
| 12 |
#include <memory> |
| 13 |
#include <span> |
| 14 |
#include <type_traits> |
| 15 |
|
| 16 |
namespace openmsx { |
| 17 |
|
| 18 |
class File |
| 19 |
{ |
| 20 |
public: |
| 21 |
enum class OpenMode : uint8_t { |
| 22 |
NORMAL, |
| 23 |
TRUNCATE, |
| 24 |
CREATE, |
| 25 |
PRE_CACHE, |
| 26 |
}; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
File(); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
explicit File(zstring_view filename, OpenMode mode = OpenMode::NORMAL); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
File(zstring_view filename, const char* mode); |
| 50 |
|
| 51 |
File(File&& other) noexcept; |
| 52 |
|
| 53 |
|
| 54 |
explicit File(std::unique_ptr<FileBase> file_); |
| 55 |
|
| 56 |
~File(); |
| 57 |
|
| 58 |
File& operator=(File&& other) noexcept; |
| 59 |
|
| 60 |
|
| 61 |
[[nodiscard]] bool is_open() const { return file != nullptr; } |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
void close(); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
void read(std::span<uint8_t> buffer); |
| 73 |
|
| 74 |
template<typename T> |
| 75 |
void read(std::span<T> buffer) { |
| 76 |
read(std::span<uint8_t>{std::bit_cast<uint8_t*>(buffer.data()), buffer.size_bytes()}); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
void write(std::span<const uint8_t> buffer); |
| 84 |
|
| 85 |
template<typename T> |
| 86 |
void write(std::span<T> buffer) { |
| 87 |
write(std::span<const uint8_t>{std::bit_cast<const uint8_t*>(buffer.data()), buffer.size_bytes()}); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
template<typename T> |
| 107 |
[[nodiscard]] MappedFile<T> mmap(size_t extra = 0) { |
| 108 |
return MappedFile<T>(file->mmap(extra * sizeof(T), std::is_const_v<T>)); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
[[nodiscard]] size_t getSize(); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
void seek(size_t pos); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
[[nodiscard]] size_t getPos(); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
void truncate(size_t size); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
void flush(); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
[[nodiscard]] zstring_view getOriginalName(); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
[[nodiscard]] bool isReadOnly() const; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
[[nodiscard]] time_t getModificationDate(); |
| 158 |
|
| 159 |
private: |
| 160 |
friend class LocalFileReference; |
| 161 |
|
| 162 |
[[nodiscard]] bool isLocalFile() const; |
| 163 |
|
| 164 |
std::unique_ptr<FileBase> file; |
| 165 |
}; |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
#endif |