Line |
Branch |
Exec |
Source |
1 |
|
|
// RPG Hacker: We can't include that here, because it leads to crazy |
2 |
|
|
// errors in windows.h - probably related to our string class?!? |
3 |
|
|
//#include "platform/file-helpers.h" |
4 |
|
|
|
5 |
|
|
#if defined(_MSC_VER) |
6 |
|
|
# pragma warning(push) |
7 |
|
|
# pragma warning(disable : 4668) |
8 |
|
|
# pragma warning(disable : 4987) |
9 |
|
|
#endif |
10 |
|
|
|
11 |
|
|
#define NOMINMAX |
12 |
|
|
#include <windows.h> |
13 |
|
|
#include <ctype.h> |
14 |
|
|
|
15 |
|
|
#if defined(_MSC_VER) |
16 |
|
|
# pragma warning(pop) |
17 |
|
|
#endif |
18 |
|
|
|
19 |
|
|
#include <ctype.h> |
20 |
|
|
|
21 |
|
|
#include "../file-helpers.h" |
22 |
|
|
#include "../../unicode.h" |
23 |
|
|
|
24 |
1/2
✓ Branch 0 taken 16446 times.
✗ Branch 1 not taken.
|
16446 |
bool file_exists(const char * filename) |
25 |
|
|
{ |
26 |
|
|
std::wstring u16_str; |
27 |
2/4
✓ Branch 0 taken 16446 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16446 times.
✗ Branch 3 not taken.
|
16446 |
if (!utf8_to_utf16(&u16_str, filename)) return false; |
28 |
1/2
✓ Branch 0 taken 16446 times.
✗ Branch 1 not taken.
|
16446 |
return (GetFileAttributesW(u16_str.c_str()) != INVALID_FILE_ATTRIBUTES); |
29 |
|
|
} |
30 |
|
|
|
31 |
|
34494 |
bool path_is_absolute(const char* path) |
32 |
|
|
{ |
33 |
|
68907 |
return ((isalpha(path[0]) && |
34 |
3/4
✓ Branch 0 taken 3661 times.
✓ Branch 1 taken 30752 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3661 times.
|
34413 |
(':' == path[1]) && (('\\' == path[2]) || ('/' == path[2]))) /* drive letter + root, e.g. C:\dir or C:/dir */ |
35 |
4/6
✓ Branch 0 taken 34413 times.
✓ Branch 1 taken 81 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 30833 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 30833 times.
|
65246 |
|| ('\\' == path[0]) || ('/' == path[0])); /* just root, e.g. \dir or /dir */ |
36 |
|
|
} |
37 |
|
|
|
38 |
|
38 |
char get_native_path_separator() |
39 |
|
|
{ |
40 |
|
38 |
return '\\'; |
41 |
|
|
} |
42 |
|
|
|
43 |
1/2
✓ Branch 0 taken 791 times.
✗ Branch 1 not taken.
|
791 |
bool check_is_regular_file(const char* path) |
44 |
|
|
{ |
45 |
|
|
std::wstring u16_str; |
46 |
2/4
✓ Branch 0 taken 791 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 791 times.
✗ Branch 3 not taken.
|
791 |
if (!utf8_to_utf16(&u16_str, path)) return false; |
47 |
1/2
✓ Branch 0 taken 791 times.
✗ Branch 1 not taken.
|
791 |
return !(GetFileAttributesW(u16_str.c_str()) & FILE_ATTRIBUTE_DIRECTORY); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
|
51 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1384 times.
|
1384 |
FileHandleType open_file(const char* path, FileOpenMode mode, FileOpenError* error) |
52 |
|
|
{ |
53 |
|
|
HANDLE out_handle = NULL; |
54 |
|
|
DWORD access_mode = 0; |
55 |
|
|
DWORD share_mode = 0; |
56 |
|
|
DWORD disposition = 0; |
57 |
|
|
|
58 |
|
|
switch (mode) |
59 |
|
|
{ |
60 |
|
|
case FileOpenMode_ReadWrite: |
61 |
|
|
access_mode = GENERIC_READ | GENERIC_WRITE; |
62 |
|
|
disposition = OPEN_EXISTING; |
63 |
|
|
break; |
64 |
|
|
case FileOpenMode_Read: |
65 |
|
|
access_mode = GENERIC_READ; |
66 |
|
|
// This should be fine, right? |
67 |
|
|
share_mode = FILE_SHARE_READ; |
68 |
|
|
disposition = OPEN_EXISTING; |
69 |
|
|
break; |
70 |
|
|
case FileOpenMode_Write: |
71 |
|
|
access_mode = GENERIC_WRITE; |
72 |
|
|
disposition = CREATE_ALWAYS; |
73 |
|
|
break; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
std::wstring u16_str; |
77 |
2/4
✓ Branch 0 taken 1384 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1384 times.
|
1384 |
if (!utf8_to_utf16(&u16_str, path)) |
78 |
|
|
{ |
79 |
|
|
// RPG Hacker: I treat encoding error as "file not found", which I guess is what |
80 |
|
|
// Windows would do, anyways. |
81 |
|
✗ |
*error = FileOpenError_NotFound; |
82 |
|
✗ |
return INVALID_HANDLE_VALUE; |
83 |
|
|
} |
84 |
|
|
|
85 |
1/2
✓ Branch 0 taken 1384 times.
✗ Branch 1 not taken.
|
1384 |
out_handle = CreateFileW(u16_str.c_str(), access_mode, share_mode, NULL, disposition, FILE_ATTRIBUTE_NORMAL, NULL); |
86 |
|
|
|
87 |
2/2
✓ Branch 0 taken 773 times.
✓ Branch 1 taken 611 times.
|
1384 |
if (error != NULL) |
88 |
|
|
{ |
89 |
1/2
✓ Branch 0 taken 773 times.
✗ Branch 1 not taken.
|
773 |
if (out_handle != INVALID_HANDLE_VALUE) |
90 |
|
|
{ |
91 |
|
773 |
*error = FileOpenError_None; |
92 |
|
|
} |
93 |
|
|
else |
94 |
|
|
{ |
95 |
|
✗ |
DWORD win_error = GetLastError(); |
96 |
|
|
|
97 |
|
✗ |
switch (win_error) |
98 |
|
|
{ |
99 |
|
✗ |
case ERROR_FILE_NOT_FOUND: |
100 |
|
✗ |
*error = FileOpenError_NotFound; |
101 |
|
✗ |
break; |
102 |
|
✗ |
case ERROR_ACCESS_DENIED: |
103 |
|
✗ |
*error = FileOpenError_AccessDenied; |
104 |
|
✗ |
break; |
105 |
|
✗ |
default: |
106 |
|
✗ |
*error = FileOpenError_Unknown; |
107 |
|
✗ |
break; |
108 |
|
|
} |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
return out_handle; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
1384 |
void close_file(FileHandleType handle) |
116 |
|
|
{ |
117 |
1/2
✓ Branch 0 taken 1384 times.
✗ Branch 1 not taken.
|
1384 |
if (handle == InvalidFileHandle) return; |
118 |
|
|
|
119 |
|
1384 |
CloseHandle(handle); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
1383 |
uint64_t get_file_size(FileHandleType handle) |
123 |
|
|
{ |
124 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1383 times.
|
1383 |
if (handle == InvalidFileHandle) return 0u; |
125 |
|
|
|
126 |
|
|
LARGE_INTEGER f_size; |
127 |
|
|
|
128 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1383 times.
|
1383 |
if (!GetFileSizeEx(handle, &f_size)) return 0u; |
129 |
|
|
|
130 |
|
1383 |
return (uint64_t)f_size.QuadPart; |
131 |
|
|
} |
132 |
|
|
|
133 |
|
1000 |
void set_file_pos(FileHandleType handle, uint64_t pos) |
134 |
|
|
{ |
135 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1000 times.
|
1000 |
if (handle == InvalidFileHandle) return; |
136 |
|
|
|
137 |
|
|
// TODO: Some error handling would be wise here. |
138 |
|
|
|
139 |
|
|
LARGE_INTEGER new_pos; |
140 |
|
1000 |
new_pos.QuadPart = (LONGLONG)pos; |
141 |
|
|
|
142 |
|
1000 |
SetFilePointerEx(handle, new_pos, NULL, FILE_BEGIN); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
1404 |
uint32_t read_file(FileHandleType handle, void* buffer, uint32_t num_bytes) |
146 |
|
|
{ |
147 |
1/2
✓ Branch 0 taken 1404 times.
✗ Branch 1 not taken.
|
1404 |
if (handle == InvalidFileHandle) return 0u; |
148 |
|
|
|
149 |
|
1404 |
DWORD bytes_read = 0u; |
150 |
|
|
|
151 |
|
|
// TODO: Some error handling would be wise here. |
152 |
|
|
|
153 |
|
1404 |
ReadFile(handle, buffer, (DWORD)num_bytes, &bytes_read, NULL); |
154 |
|
|
|
155 |
|
1404 |
return (uint32_t)bytes_read; |
156 |
|
|
} |
157 |
|
|
|
158 |
|
84 |
uint32_t write_file(FileHandleType handle, const void* buffer, uint32_t num_bytes) |
159 |
|
|
{ |
160 |
1/2
✓ Branch 0 taken 84 times.
✗ Branch 1 not taken.
|
84 |
if (handle == InvalidFileHandle) return 0u; |
161 |
|
|
|
162 |
|
84 |
DWORD bytes_written = 0u; |
163 |
|
|
|
164 |
|
|
// TODO: Some error handling would be wise here. |
165 |
|
|
|
166 |
|
84 |
WriteFile(handle, buffer, (DWORD)num_bytes, &bytes_written, NULL); |
167 |
|
|
|
168 |
|
84 |
return (uint32_t)bytes_written; |
169 |
|
|
} |
170 |
|
|
|