-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapping.hpp
More file actions
137 lines (111 loc) · 2.04 KB
/
mapping.hpp
File metadata and controls
137 lines (111 loc) · 2.04 KB
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
#pragma once
#include <windows.h>
class KFile {
private:
HANDLE hFile;
public:
KFile() : hFile(INVALID_HANDLE_VALUE) {}
~KFile() {
close();
}
HANDLE handle() const {
return hFile;
}
bool isOpened() const {
return hFile != INVALID_HANDLE_VALUE;
}
bool open(const wchar_t* path) {
hFile = CreateFile (path,
GENERIC_READ,
FILE_SHARE_DELETE|FILE_SHARE_READ|FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
return isOpened();
}
void close() {
if(hFile != INVALID_HANDLE_VALUE)
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
LONGLONG getSize() const {
LARGE_INTEGER size={0};
if (isOpened())
GetFileSizeEx(hFile, &size);
return size.QuadPart;
}
};
class KMapping{
private:
HANDLE hMapping;
public:
KMapping() : hMapping(0) {}
~KMapping() {
close();
}
HANDLE handle() const {
return hMapping;
}
bool open(const KFile& file) {
hMapping = CreateFileMapping(file.handle(), 0, PAGE_READONLY, 0, 0, 0);
return hMapping!=0;
}
void close() {
if(hMapping != 0)
CloseHandle(hMapping);
hMapping = 0;
}
};
class KViewOfFile {
private:
const char* MappingAddr;
public:
KViewOfFile() : MappingAddr(nullptr) {}
~KViewOfFile() {
close();
}
const char* value() const {
return MappingAddr;
}
bool isOpened() {
return MappingAddr != nullptr;
}
bool open(const KMapping& mapping) {
MappingAddr = (const char*)MapViewOfFile(mapping.handle(), FILE_MAP_READ, 0, 0, 0);
return isOpened();
}
void close() {
if(MappingAddr != nullptr)
UnmapViewOfFile(MappingAddr);
MappingAddr = nullptr;
}
};
class KFileMapping {
private:
KFile file;
KMapping mapping;
KViewOfFile view;
public:
KFileMapping() {}
~KFileMapping() {
close();
}
void close() {
view.close();
mapping.close();
file.close();
}
bool open(const wchar_t* path) {
if(file.open(path) && mapping.open(file) && view.open(mapping))
return true;
close();
return false;
}
const char* value() const {
return view.value();
}
LONGLONG getSize() const {
return file.getSize();
}
};