-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileMapView.h
164 lines (149 loc) · 3.88 KB
/
FileMapView.h
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
#pragma once
#include <Windows.h>
#include <string>
#include <iomanip>
#include <sstream>
#include <stdexcept>
class FileMapView final
{
public:
enum class Mode
{
Read,
WriteNew
};
public:
FileMapView(const std::wstring& filepath, Mode mode) : filepath_(filepath), mode_(mode)
{
OpenFile();
InitializeMapping();
}
~FileMapView()
{
Close();
}
FileMapView(const FileMapView&) = delete;
FileMapView(FileMapView&& other)
{
*this = std::move(other);
}
FileMapView& operator=(FileMapView&& other)
{
if (this != &other)
{
Close();
filepath_ = std::move(other.filepath_);
mode_ = other.mode_;
file_ = other.file_;
other.file_ = INVALID_HANDLE_VALUE;
filesize_.QuadPart = other.filesize_.QuadPart;
other.filesize_.QuadPart = 0;
mapfile_ = other.mapfile_;
other.mapfile_ = INVALID_HANDLE_VALUE;
data_ = other.data_;
other.data_ = nullptr;
}
return *this;
}
LARGE_INTEGER filesize() const { return filesize_; }
PBYTE data() const { return data_; }
void Open(const std::wstring& filepath, Mode mode);
void Close();
private:
std::wstring filepath_;
Mode mode_{ Mode::Read };
HANDLE file_{ INVALID_HANDLE_VALUE };
LARGE_INTEGER filesize_{ 0 };
HANDLE mapfile_{ INVALID_HANDLE_VALUE };
PBYTE data_{ nullptr };
private:
void InitializeMapping();
void OpenFile();
void GetFileSize();
void OpenMapping();
void MapView();
};
inline void FileMapView::Open(const std::wstring& filepath, Mode mode)
{
if (file_ != INVALID_HANDLE_VALUE)
{
Close();
}
filepath_ = filepath;
mode_ = mode;
OpenFile();
InitializeMapping();
}
inline void FileMapView::Close()
{
if (data_ != nullptr)
{
UnmapViewOfFile(data_);
data_ = nullptr;
}
if (mapfile_ != INVALID_HANDLE_VALUE)
{
CloseHandle(mapfile_);
mapfile_ = INVALID_HANDLE_VALUE;
}
if (file_ != INVALID_HANDLE_VALUE)
{
CloseHandle(file_);
file_ = INVALID_HANDLE_VALUE;
}
}
inline void FileMapView::InitializeMapping()
{
try
{
GetFileSize();
OpenMapping();
MapView();
}
catch (const std::exception&)
{
Close();
throw;
}
}
inline void FileMapView::OpenFile()
{
if (mode_ == Mode::Read)
{
file_ = CreateFile(filepath_.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
}
else if (mode_ == Mode::WriteNew)
{
file_ = CreateFile(filepath_.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
}
if (file_ == INVALID_HANDLE_VALUE)
{
std::stringstream ss;
ss << "FileMapView.OpenFile(CreateFile()) failed with error ";
ss << "0x" << std::hex << std::setw(8) << std::setfill('0') << std::uppercase;
ss << GetLastError();
throw std::runtime_error(ss.str());
}
}
inline void FileMapView::GetFileSize()
{
GetFileSizeEx(file_, &filesize_);
}
inline void FileMapView::OpenMapping()
{
const auto flProtect = mode_ == Mode::Read ? PAGE_READONLY : PAGE_READWRITE;
mapfile_ = CreateFileMapping(file_, NULL, flProtect, 0, 0, NULL);
if (mapfile_ == NULL)
{
std::stringstream ss;
ss << "FileMapView.OpenMapping(CreateFileMapping()) failed with error ";
ss << "0x" << std::hex << std::setw(8) << std::setfill('0') << std::uppercase;
ss << GetLastError();
throw std::runtime_error(ss.str());
}
}
inline void FileMapView::MapView()
{
const auto desiredAccess = mode_ == Mode::Read ? FILE_MAP_READ : FILE_MAP_WRITE;
data_ = reinterpret_cast<PBYTE>(MapViewOfFile(mapfile_, desiredAccess, 0, 0, 0));
}