From ce1bf19d40b8a0e6d52176229e4f178dac1f1c5f Mon Sep 17 00:00:00 2001 From: Shivendra Agarwal Date: Fri, 6 Nov 2020 10:56:36 +0530 Subject: [PATCH] Adding support of UTF8 file name processing in mio on windows Contributing back changes from app. --- single_include/mio/mio.hpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/single_include/mio/mio.hpp b/single_include/mio/mio.hpp index 95c696c..bc759e9 100644 --- a/single_include/mio/mio.hpp +++ b/single_include/mio/mio.hpp @@ -793,6 +793,16 @@ inline DWORD int64_low(int64_t n) noexcept { return n & 0xffffffff; } + +std::wstring s2ws(const std::string& s) +{ + if (s.empty()) + return{}; + const auto sLength = static_cast(s.length()); + auto buf = std::vector(sLength); + const auto wideCharCount = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), sLength, buf.data(), sLength); + return std::wstring(buf.data(), wideCharCount); +} template< typename String, @@ -801,7 +811,7 @@ template< >::type > file_handle_type open_file_helper(const String& path, const access_mode mode) { - return ::CreateFileA(c_str(path), + return ::CreateFileW(s2ws(path).c_str(), mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,