Migrate to CreateFileW
All checks were successful
CI / build-and-analyze (push) Successful in 1m16s

This commit is contained in:
2026-08-18 10:53:36 +03:00
parent 39076d3ded
commit 8b551a7218

View File

@@ -2,6 +2,10 @@
#include <windows.h>
#include <assert.h>
#ifndef MAX_PATH
#define MAX_PATH 260
#endif
struct CgeFile {
HANDLE handle;
int flags;
@@ -9,6 +13,21 @@ struct CgeFile {
int lastError;
};
static int utf8ToWchar(const char *utf8, wchar_t *out, int size) {
int len;
if (size <= 0)
return 0;
len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, out, size);
if (len <= 0 || len > size) {
out[0] = L'\0';
return 0;
}
return 1;
}
static int errorCodeFromError(void) {
DWORD error;
@@ -52,6 +71,7 @@ static int errorCodeFromError(void) {
static int fileInit(CgeFile *file, const char *path, int mode) {
DWORD access = 0, how = 0;
wchar_t pathW[MAX_PATH];
assert(file != NULL);
assert(path != NULL);
@@ -83,9 +103,12 @@ static int fileInit(CgeFile *file, const char *path, int mode) {
}
}
if (!utf8ToWchar(path, pathW, MAX_PATH))
return CGE_FILE_ENAMETOOLONG;
file->flags = 0;
file->mode = mode;
file->handle = CreateFileA(path, access, FILE_SHARE_READ, NULL, how, FILE_ATTRIBUTE_NORMAL, NULL);
file->handle = CreateFileW(pathW, access, FILE_SHARE_READ, NULL, how, FILE_ATTRIBUTE_NORMAL, NULL);
if (file->handle == INVALID_HANDLE_VALUE)
return errorCodeFromError();