From 66ea991028e62daf8bae5b6f1ad348c3eabe1da8 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 26 Nov 2013 03:58:54 +0000 Subject: [PATCH] Try to fix non-trivial filename character encoding on Windows. --- asdcplib/src/KM_fileio.cpp | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/asdcplib/src/KM_fileio.cpp b/asdcplib/src/KM_fileio.cpp index d48e7554..df2eb974 100644 --- a/asdcplib/src/KM_fileio.cpp +++ b/asdcplib/src/KM_fileio.cpp @@ -679,6 +679,7 @@ Kumu::FileWriter::StopHashing() //------------------------------------------------------------------------------------------ // +/** @param filename File name (UTF-8 encoded) */ Kumu::Result_t Kumu::FileReader::OpenRead(const char* filename) const { @@ -688,7 +689,12 @@ Kumu::FileReader::OpenRead(const char* filename) const // suppress popup window on error UINT prev = ::SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX); - const_cast(this)->m_Handle = ::CreateFileA(filename, + wchar_t buffer[1024]; + if (MultiByteToWideChar (CP_UTF8, MB_PRECOMPOSED, filename, -1, buffer, 1024)) { + return Kumu::RESULT_FAIL; + } + + const_cast(this)->m_Handle = ::CreateFileW(buffer, (GENERIC_READ), // open for reading FILE_SHARE_READ, // share for reading NULL, // no security @@ -803,7 +809,7 @@ Kumu::FileReader::Read(byte_t* buf, ui32_t buf_len, ui32_t* read_count) const //------------------------------------------------------------------------------------------ // -// +/** @param filename File name (UTF-8 encoded) */ Kumu::Result_t Kumu::FileWriter::OpenWrite(const char* filename) { @@ -813,7 +819,12 @@ Kumu::FileWriter::OpenWrite(const char* filename) // suppress popup window on error UINT prev = ::SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX); - m_Handle = ::CreateFileA(filename, + wchar_t buffer[1024]; + if (MultiByteToWideChar (CP_UTF8, MB_PRECOMPOSED, filename, -1, buffer, 1024)) { + return Kumu::RESULT_FAIL; + } + + m_Handle = ::CreateFileW(buffer, (GENERIC_WRITE|GENERIC_READ), // open for reading FILE_SHARE_READ, // share for reading NULL, // no security @@ -831,7 +842,7 @@ Kumu::FileWriter::OpenWrite(const char* filename) return Kumu::RESULT_OK; } -// +/** @param filename File name (UTF-8 encoded) */ Kumu::Result_t Kumu::FileWriter::OpenModify(const char* filename) { @@ -841,7 +852,12 @@ Kumu::FileWriter::OpenModify(const char* filename) // suppress popup window on error UINT prev = ::SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOOPENFILEERRORBOX); - m_Handle = ::CreateFileA(filename, + wchar_t buffer[1024]; + if (MultiByteToWideChar (CP_UTF8, MB_PRECOMPOSED, filename, -1, buffer, 1024)) { + return Kumu::RESULT_FAIL; + } + + m_Handle = ::CreateFileW(buffer, (GENERIC_WRITE|GENERIC_READ), // open for reading FILE_SHARE_READ, // share for reading NULL, // no security -- 2.30.2