| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <span> |
| 7 |
#include <string> |
| 8 |
|
| 9 |
#include "Common/CommonTypes.h" |
| 10 |
#include "Common/IOFile.h" |
| 11 |
|
| 12 |
namespace File |
| 13 |
{ |
| 14 |
enum class AccessMode |
| 15 |
{ |
| 16 |
Read, |
| 17 |
Write, |
| 18 |
ReadAndWrite, |
| 19 |
}; |
| 20 |
|
| 21 |
enum class OpenMode |
| 22 |
{ |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
Default, |
| 28 |
|
| 29 |
|
| 30 |
Always, |
| 31 |
|
| 32 |
|
| 33 |
Truncate, |
| 34 |
|
| 35 |
|
| 36 |
Existing, |
| 37 |
|
| 38 |
|
| 39 |
Create, |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class DirectIOFile final |
| 46 |
{ |
| 47 |
public: |
| 48 |
DirectIOFile(); |
| 49 |
~DirectIOFile(); |
| 50 |
|
| 51 |
|
| 52 |
DirectIOFile(const DirectIOFile&); |
| 53 |
DirectIOFile& operator=(const DirectIOFile&); |
| 54 |
DirectIOFile(DirectIOFile&&); |
| 55 |
DirectIOFile& operator=(DirectIOFile&&); |
| 56 |
|
| 57 |
explicit DirectIOFile(const std::string& path, AccessMode access_mode, |
| 58 |
OpenMode open_mode = OpenMode::Default); |
| 59 |
|
| 60 |
bool Open(const std::string& path, AccessMode access_mode, |
| 61 |
OpenMode open_mode = OpenMode::Default); |
| 62 |
|
| 63 |
bool Close(); |
| 64 |
|
| 65 |
bool IsOpen() const; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
bool OffsetRead(u64 offset, u8* out_ptr, u64 size); |
| 72 |
bool OffsetRead(u64 offset, std::span<u8> out_data) |
| 73 |
{ |
| 74 |
return OffsetRead(offset, out_data.data(), out_data.size()); |
| 75 |
} |
| 76 |
bool OffsetWrite(u64 offset, const u8* in_ptr, u64 size); |
| 77 |
bool OffsetWrite(auto offset, std::span<const u8> in_data) |
| 78 |
{ |
| 79 |
return OffsetWrite(offset, in_data.data(), in_data.size()); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
bool Read(u8* out_ptr, u64 size) |
| 85 |
{ |
| 86 |
if (!OffsetRead(m_current_offset, out_ptr, size)) |
| 87 |
return false; |
| 88 |
m_current_offset += size; |
| 89 |
return true; |
| 90 |
} |
| 91 |
bool Read(std::span<u8> out_data) { return Read(out_data.data(), out_data.size()); } |
| 92 |
|
| 93 |
bool Write(const u8* in_ptr, u64 size) |
| 94 |
{ |
| 95 |
if (!OffsetWrite(m_current_offset, in_ptr, size)) |
| 96 |
return false; |
| 97 |
m_current_offset += size; |
| 98 |
return true; |
| 99 |
} |
| 100 |
bool Write(std::span<const u8> in_data) { return Write(in_data.data(), in_data.size()); } |
| 101 |
|
| 102 |
|
| 103 |
u64 GetSize() const; |
| 104 |
|
| 105 |
bool Seek(s64 offset, SeekOrigin origin); |
| 106 |
|
| 107 |
|
| 108 |
u64 Tell() const { return m_current_offset; } |
| 109 |
|
| 110 |
bool Flush(); |
| 111 |
|
| 112 |
auto GetHandle() const |
| 113 |
{ |
| 114 |
#if defined(_WIN32) |
| 115 |
return m_handle; |
| 116 |
#else |
| 117 |
return m_fd; |
| 118 |
#endif |
| 119 |
} |
| 120 |
|
| 121 |
private: |
| 122 |
void Swap(DirectIOFile& other); |
| 123 |
DirectIOFile Duplicate() const; |
| 124 |
|
| 125 |
#if defined(_WIN32) |
| 126 |
|
| 127 |
|
| 128 |
using HandleType = void*; |
| 129 |
HandleType m_handle{HandleType(-1)}; |
| 130 |
#else |
| 131 |
using HandleType = int; |
| 132 |
HandleType m_fd{-1}; |
| 133 |
#endif |
| 134 |
|
| 135 |
u64 m_current_offset{}; |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
bool Resize(DirectIOFile& file, u64 size); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
bool Rename(DirectIOFile& file, const std::string& source_path, |
| 146 |
const std::string& destination_path); |
| 147 |
|
| 148 |
bool Delete(DirectIOFile& file, const std::string& filename); |
| 149 |
|
| 150 |
} |