This commit is contained in:
25
win32/File.c
25
win32/File.c
@@ -2,6 +2,10 @@
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#ifndef MAX_PATH
|
||||||
|
#define MAX_PATH 260
|
||||||
|
#endif
|
||||||
|
|
||||||
struct CgeFile {
|
struct CgeFile {
|
||||||
HANDLE handle;
|
HANDLE handle;
|
||||||
int flags;
|
int flags;
|
||||||
@@ -9,6 +13,21 @@ struct CgeFile {
|
|||||||
int lastError;
|
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) {
|
static int errorCodeFromError(void) {
|
||||||
DWORD error;
|
DWORD error;
|
||||||
|
|
||||||
@@ -52,6 +71,7 @@ static int errorCodeFromError(void) {
|
|||||||
|
|
||||||
static int fileInit(CgeFile *file, const char *path, int mode) {
|
static int fileInit(CgeFile *file, const char *path, int mode) {
|
||||||
DWORD access = 0, how = 0;
|
DWORD access = 0, how = 0;
|
||||||
|
wchar_t pathW[MAX_PATH];
|
||||||
|
|
||||||
assert(file != NULL);
|
assert(file != NULL);
|
||||||
assert(path != 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->flags = 0;
|
||||||
file->mode = mode;
|
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)
|
if (file->handle == INVALID_HANDLE_VALUE)
|
||||||
return errorCodeFromError();
|
return errorCodeFromError();
|
||||||
|
|||||||
Reference in New Issue
Block a user