From 8b0c3f0dc89c6b9e1a55f4df3fb25a686e510b26 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Fri, 31 Jul 2020 16:03:18 +1000 Subject: [PATCH] FileSystem: Use wide strings for fopen on Windows --- src/common/file_system.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/common/file_system.cpp b/src/common/file_system.cpp index 50f85a123..dbe854dc0 100644 --- a/src/common/file_system.cpp +++ b/src/common/file_system.cpp @@ -401,6 +401,28 @@ FileSystem::ManagedCFilePtr OpenManagedCFile(const char* filename, const char* m std::FILE* OpenCFile(const char* filename, const char* mode) { #ifdef WIN32 + int filename_len = static_cast(std::strlen(filename)); + int mode_len = static_cast(std::strlen(mode)); + int wlen = MultiByteToWideChar(CP_UTF8, 0, filename, filename_len, nullptr, 0); + int wmodelen = MultiByteToWideChar(CP_UTF8, 0, mode, mode_len, nullptr, 0); + if (wlen > 0 && wmodelen > 0) + { + wchar_t* wfilename = static_cast(alloca(sizeof(wchar_t) * (wlen + 1))); + wchar_t* wmode = static_cast(alloca(sizeof(wchar_t) * (wmodelen + 1))); + wlen = MultiByteToWideChar(CP_UTF8, 0, filename, filename_len, wfilename, wlen); + wmodelen = MultiByteToWideChar(CP_UTF8, 0, mode, mode_len, wmode, wmodelen); + if (wlen > 0 && wmodelen > 0) + { + wfilename[wlen] = 0; + wmode[wmodelen] = 0; + std::FILE* fp; + if (_wfopen_s(&fp, wfilename, wmode) != 0) + return nullptr; + + return fp; + } + } + std::FILE* fp; if (fopen_s(&fp, filename, mode) != 0) return nullptr;