-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.cpp
More file actions
50 lines (45 loc) · 1.38 KB
/
Copy pathfile.cpp
File metadata and controls
50 lines (45 loc) · 1.38 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
#ifdef CBS_WIN32
bool File::exists(const char* path) {
DWORD attributes = ::GetFileAttributesA(path);
return (attributes != INVALID_FILE_ATTRIBUTES);
}
char* File::get_exe_dir() {
char* buffer = alloc_space<char>(MAX_PATH);
::GetModuleFileNameA(nullptr, buffer, MAX_PATH);
char* last_backslash = std::strrchr(buffer, '\\');
if (last_backslash) {
*last_backslash = '\0';
}
return buffer;
}
void MappedFile::create(this MappedFile& self, const char* path) {
self.file = ::CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
if (self.file == INVALID_HANDLE_VALUE) {
ctk::panic("::CreateFileA failed");
}
LARGE_INTEGER sz;
::GetFileSizeEx(self.file, &sz);
self.size = (size_t)sz.QuadPart;
self.mapping = ::CreateFileMappingA(self.file, nullptr, PAGE_READONLY, 0, 0, nullptr);
if (self.mapping == nullptr) {
::CloseHandle(self.file);
ctk::panic("::CreateFileMapping failed");
}
self.data = (u8*)::MapViewOfFile(self.mapping, FILE_MAP_READ, 0, 0, 0);
if (self.data == nullptr) {
::CloseHandle(self.mapping);
::CloseHandle(self.file);
ctk::panic("::MapViewOfFile failed");
}
}
void MappedFile::destroy(this MappedFile& self) {
::UnmapViewOfFile(self.data);
::CloseHandle(self.mapping);
::CloseHandle(self.file);
}
#endif
#ifdef CBS_LINUX
bool File::exists(const char* path) {
return ::access(path, F_OK) == 0;
}
#endif