platform/windows/file-helpers-win32.cpp
| 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 | 5136 | if (!utf8_to_utf16(&u16_str, filename)) return false; | |
| 28 | 5136 | return (GetFileAttributesW(u16_str.c_str()) != INVALID_FILE_ATTRIBUTES); | |
| 29 | 5136 | } | |
| 30 | |||
| 31 | 11399 | bool path_is_absolute(const char* path) | |
| 32 | { | ||
| 33 | 22771 | return ((isalpha(path[0]) && | |
| 34 | 11372 | (':' == path[1]) && (('\\' == path[2]) || ('/' == path[2]))) /* drive letter + root, e.g. C:\dir or C:/dir */ | |
| 35 | 22771 | || ('\\' == 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 | 6 | if (!utf8_to_utf16(&u16_str, path)) return false; | |
| 47 | 6 | return !(GetFileAttributesW(u16_str.c_str()) & FILE_ATTRIBUTE_DIRECTORY); | |
| 48 | 6 | } | |
| 49 | |||
| 50 | |||
| 51 | 283 | FileHandleType open_file(const char* path, FileOpenMode mode, FileOpenError* error) | |
| 52 | { | ||
| 53 | 283 | HANDLE out_handle = NULL; | |
| 54 | 283 | DWORD access_mode = 0; | |
| 55 | 283 | DWORD share_mode = 0; | |
| 56 | 283 | DWORD disposition = 0; | |
| 57 | |||
| 58 | 283 | switch (mode) | |
| 59 | { | ||
| 60 | ✗ | case FileOpenMode_ReadWrite: | |
| 61 | ✗ | access_mode = GENERIC_READ | GENERIC_WRITE; | |
| 62 | ✗ | disposition = OPEN_EXISTING; | |
| 63 | ✗ | break; | |
| 64 | 283 | case FileOpenMode_Read: | |
| 65 | 283 | access_mode = GENERIC_READ; | |
| 66 | // This should be fine, right? | ||
| 67 | 283 | share_mode = FILE_SHARE_READ; | |
| 68 | 283 | disposition = OPEN_EXISTING; | |
| 69 | 283 | break; | |
| 70 | ✗ | case FileOpenMode_Write: | |
| 71 | ✗ | access_mode = GENERIC_WRITE; | |
| 72 | ✗ | disposition = CREATE_ALWAYS; | |
| 73 | ✗ | break; | |
| 74 | } | ||
| 75 | |||
| 76 | 566 | std::wstring u16_str; | |
| 77 | 283 | 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 | 283 | out_handle = CreateFileW(u16_str.c_str(), access_mode, share_mode, NULL, disposition, FILE_ATTRIBUTE_NORMAL, NULL); | |
| 86 | |||
| 87 | 283 | if (error != NULL) | |
| 88 | { | ||
| 89 | 283 | if (out_handle != INVALID_HANDLE_VALUE) | |
| 90 | { | ||
| 91 | 269 | *error = FileOpenError_None; | |
| 92 | } | ||
| 93 | else | ||
| 94 | { | ||
| 95 | 14 | DWORD win_error = GetLastError(); | |
| 96 | |||
| 97 | 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 | 283 | return out_handle; | |
| 113 | } | ||
| 114 | |||
| 115 | 269 | void close_file(FileHandleType handle) | |
| 116 | { | ||
| 117 | 269 | if (handle == InvalidFileHandle) return; | |
| 118 | |||
| 119 | 269 | CloseHandle(handle); | |
| 120 | } | ||
| 121 | |||
| 122 | 269 | uint64_t get_file_size(FileHandleType handle) | |
| 123 | { | ||
| 124 | 269 | if (handle == InvalidFileHandle) return 0u; | |
| 125 | |||
| 126 | LARGE_INTEGER f_size; | ||
| 127 | |||
| 128 | 269 | if (!GetFileSizeEx(handle, &f_size)) return 0u; | |
| 129 | |||
| 130 | 269 | return (uint64_t)f_size.QuadPart; | |
| 131 | } | ||
| 132 | |||
| 133 | 275 | void set_file_pos(FileHandleType handle, uint64_t pos) | |
| 134 | { | ||
| 135 | 275 | if (handle == InvalidFileHandle) return; | |
| 136 | |||
| 137 | // TODO: Some error handling would be wise here. | ||
| 138 | |||
| 139 | LARGE_INTEGER new_pos; | ||
| 140 | 275 | new_pos.QuadPart = (LONGLONG)pos; | |
| 141 | |||
| 142 | 275 | SetFilePointerEx(handle, new_pos, NULL, FILE_BEGIN); | |
| 143 | } | ||
| 144 | |||
| 145 | 275 | uint32_t read_file(FileHandleType handle, void* buffer, uint32_t num_bytes) | |
| 146 | { | ||
| 147 | 275 | if (handle == InvalidFileHandle) return 0u; | |
| 148 | |||
| 149 | 275 | DWORD bytes_read = 0u; | |
| 150 | |||
| 151 | // TODO: Some error handling would be wise here. | ||
| 152 | |||
| 153 | 275 | ReadFile(handle, buffer, (DWORD)num_bytes, &bytes_read, NULL); | |
| 154 | |||
| 155 | 275 | 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 |