| 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 |
|
5136 |
bool file_exists(const char * filename) |
| 25 |
|
|
{ |
| 26 |
|
5136 |
std::wstring u16_str; |
| 27 |
2/4
✓ Branch 0 taken 5136 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5136 times.
|
5136 |
if (!utf8_to_utf16(&u16_str, filename)) return false; |
| 28 |
1/2
✓ Branch 0 taken 5136 times.
✗ Branch 1 not taken.
|
5136 |
return (GetFileAttributesW(u16_str.c_str()) != INVALID_FILE_ATTRIBUTES); |
| 29 |
|
5136 |
} |
| 30 |
|
|
|
| 31 |
|
11382 |
bool path_is_absolute(const char* path) |
| 32 |
|
|
{ |
| 33 |
|
22737 |
return ((isalpha(path[0]) && |
| 34 |
4/6
✓ Branch 0 taken 1107 times.
✓ Branch 1 taken 10248 times.
✓ Branch 2 taken 1107 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1107 times.
|
11355 |
(':' == path[1]) && (('\\' == path[2]) || ('/' == path[2]))) /* drive letter + root, e.g. C:\dir or C:/dir */ |
| 35 |
4/6
✓ Branch 0 taken 11355 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 10275 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 10275 times.
|
22737 |
|| ('\\' == path[0]) || ('/' == path[0])); /* just root, e.g. \dir or /dir */ |
| 36 |
|
|
} |
| 37 |
|
|
|
| 38 |
|
✗ |
char get_native_path_separator() |
| 39 |
|
|
{ |
| 40 |
|
✗ |
return '\\'; |
| 41 |
|
|
} |
| 42 |
|
|
|
| 43 |
|
6 |
bool check_is_regular_file(const char* path) |
| 44 |
|
|
{ |
| 45 |
|
6 |
std::wstring u16_str; |
| 46 |
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 |
if (!utf8_to_utf16(&u16_str, path)) return false; |
| 47 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
return !(GetFileAttributesW(u16_str.c_str()) & FILE_ATTRIBUTE_DIRECTORY); |
| 48 |
|
6 |
} |
| 49 |
|
|
|
| 50 |
|
|
|
| 51 |
|
280 |
FileHandleType open_file(const char* path, FileOpenMode mode, FileOpenError* error) |
| 52 |
|
|
{ |
| 53 |
|
280 |
HANDLE out_handle = NULL; |
| 54 |
|
280 |
DWORD access_mode = 0; |
| 55 |
|
280 |
DWORD share_mode = 0; |
| 56 |
|
280 |
DWORD disposition = 0; |
| 57 |
|
|
|
| 58 |
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
280 |
switch (mode) |
| 59 |
|
|
{ |
| 60 |
|
✗ |
case FileOpenMode_ReadWrite: |
| 61 |
|
✗ |
access_mode = GENERIC_READ | GENERIC_WRITE; |
| 62 |
|
✗ |
disposition = OPEN_EXISTING; |
| 63 |
|
✗ |
break; |
| 64 |
|
280 |
case FileOpenMode_Read: |
| 65 |
|
280 |
access_mode = GENERIC_READ; |
| 66 |
|
|
// This should be fine, right? |
| 67 |
|
280 |
share_mode = FILE_SHARE_READ; |
| 68 |
|
280 |
disposition = OPEN_EXISTING; |
| 69 |
|
280 |
break; |
| 70 |
|
✗ |
case FileOpenMode_Write: |
| 71 |
|
✗ |
access_mode = GENERIC_WRITE; |
| 72 |
|
✗ |
disposition = CREATE_ALWAYS; |
| 73 |
|
✗ |
break; |
| 74 |
|
|
} |
| 75 |
|
|
|
| 76 |
|
560 |
std::wstring u16_str; |
| 77 |
2/4
✓ Branch 0 taken 280 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 280 times.
|
280 |
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 280 times.
✗ Branch 1 not taken.
|
280 |
out_handle = CreateFileW(u16_str.c_str(), access_mode, share_mode, NULL, disposition, FILE_ATTRIBUTE_NORMAL, NULL); |
| 86 |
|
|
|
| 87 |
1/2
✓ Branch 0 taken 280 times.
✗ Branch 1 not taken.
|
280 |
if (error != NULL) |
| 88 |
|
|
{ |
| 89 |
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 14 times.
|
280 |
if (out_handle != INVALID_HANDLE_VALUE) |
| 90 |
|
|
{ |
| 91 |
|
266 |
*error = FileOpenError_None; |
| 92 |
|
|
} |
| 93 |
|
|
else |
| 94 |
|
|
{ |
| 95 |
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 |
DWORD win_error = GetLastError(); |
| 96 |
|
|
|
| 97 |
2/3
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
14 |
switch (win_error) |
| 98 |
|
|
{ |
| 99 |
|
8 |
case ERROR_FILE_NOT_FOUND: |
| 100 |
|
8 |
*error = FileOpenError_NotFound; |
| 101 |
|
8 |
break; |
| 102 |
|
6 |
case ERROR_ACCESS_DENIED: |
| 103 |
|
6 |
*error = FileOpenError_AccessDenied; |
| 104 |
|
6 |
break; |
| 105 |
|
✗ |
default: |
| 106 |
|
✗ |
*error = FileOpenError_Unknown; |
| 107 |
|
✗ |
break; |
| 108 |
|
|
} |
| 109 |
|
|
} |
| 110 |
|
|
} |
| 111 |
|
|
|
| 112 |
|
280 |
return out_handle; |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
|
266 |
void close_file(FileHandleType handle) |
| 116 |
|
|
{ |
| 117 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266 times.
|
266 |
if (handle == InvalidFileHandle) return; |
| 118 |
|
|
|
| 119 |
|
266 |
CloseHandle(handle); |
| 120 |
|
|
} |
| 121 |
|
|
|
| 122 |
|
266 |
uint64_t get_file_size(FileHandleType handle) |
| 123 |
|
|
{ |
| 124 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 266 times.
|
266 |
if (handle == InvalidFileHandle) return 0u; |
| 125 |
|
|
|
| 126 |
|
|
LARGE_INTEGER f_size; |
| 127 |
|
|
|
| 128 |
2/4
✓ Branch 0 taken 266 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 266 times.
|
266 |
if (!GetFileSizeEx(handle, &f_size)) return 0u; |
| 129 |
|
|
|
| 130 |
|
266 |
return (uint64_t)f_size.QuadPart; |
| 131 |
|
|
} |
| 132 |
|
|
|
| 133 |
|
272 |
void set_file_pos(FileHandleType handle, uint64_t pos) |
| 134 |
|
|
{ |
| 135 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 272 times.
|
272 |
if (handle == InvalidFileHandle) return; |
| 136 |
|
|
|
| 137 |
|
|
// TODO: Some error handling would be wise here. |
| 138 |
|
|
|
| 139 |
|
|
LARGE_INTEGER new_pos; |
| 140 |
|
272 |
new_pos.QuadPart = (LONGLONG)pos; |
| 141 |
|
|
|
| 142 |
1/2
✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
|
272 |
SetFilePointerEx(handle, new_pos, NULL, FILE_BEGIN); |
| 143 |
|
|
} |
| 144 |
|
|
|
| 145 |
|
272 |
uint32_t read_file(FileHandleType handle, void* buffer, uint32_t num_bytes) |
| 146 |
|
|
{ |
| 147 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 272 times.
|
272 |
if (handle == InvalidFileHandle) return 0u; |
| 148 |
|
|
|
| 149 |
|
272 |
DWORD bytes_read = 0u; |
| 150 |
|
|
|
| 151 |
|
|
// TODO: Some error handling would be wise here. |
| 152 |
|
|
|
| 153 |
1/2
✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
|
272 |
ReadFile(handle, buffer, (DWORD)num_bytes, &bytes_read, NULL); |
| 154 |
|
|
|
| 155 |
|
272 |
return (uint32_t)bytes_read; |
| 156 |
|
|
} |
| 157 |
|
|
|
| 158 |
|
✗ |
uint32_t write_file(FileHandleType handle, const void* buffer, uint32_t num_bytes) |
| 159 |
|
|
{ |
| 160 |
|
✗ |
if (handle == InvalidFileHandle) return 0u; |
| 161 |
|
|
|
| 162 |
|
✗ |
DWORD bytes_written = 0u; |
| 163 |
|
|
|
| 164 |
|
|
// TODO: Some error handling would be wise here. |
| 165 |
|
|
|
| 166 |
|
✗ |
WriteFile(handle, buffer, (DWORD)num_bytes, &bytes_written, NULL); |
| 167 |
|
|
|
| 168 |
|
✗ |
return (uint32_t)bytes_written; |
| 169 |
|
|
} |
| 170 |
|
|
|