Merge pull request #68 from shivendra14/master

Adding support of UTF8 file name processing in mio on windows
This commit is contained in:
mandreyel 2020-12-20 21:04:38 +01:00 committed by GitHub
commit 3f86a95c07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -52,6 +52,16 @@ inline DWORD int64_low(int64_t n) noexcept
return n & 0xffffffff; return n & 0xffffffff;
} }
std::wstring s_2_ws(const std::string& s)
{
if (s.empty())
return{};
const auto s_length = static_cast<int>(s.length());
auto buf = std::vector<wchar_t>(s_length);
const auto wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s_length, buf.data(), s_length);
return std::wstring(buf.data(), wide_char_count);
}
template< template<
typename String, typename String,
typename = typename std::enable_if< typename = typename std::enable_if<
@ -59,7 +69,7 @@ template<
>::type >::type
> file_handle_type open_file_helper(const String& path, const access_mode mode) > file_handle_type open_file_helper(const String& path, const access_mode mode)
{ {
return ::CreateFileA(c_str(path), return ::CreateFileW(s_2_ws(path).c_str(),
mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE, mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
0, 0,

View File

@ -794,6 +794,16 @@ inline DWORD int64_low(int64_t n) noexcept
return n & 0xffffffff; return n & 0xffffffff;
} }
std::wstring s_2_ws(const std::string& s)
{
if (s.empty())
return{};
const auto s_length = static_cast<int>(s.length());
auto buf = std::vector<wchar_t>(s_length);
const auto wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s_length, buf.data(), s_length);
return std::wstring(buf.data(), wide_char_count);
}
template< template<
typename String, typename String,
typename = typename std::enable_if< typename = typename std::enable_if<
@ -801,7 +811,7 @@ template<
>::type >::type
> file_handle_type open_file_helper(const String& path, const access_mode mode) > file_handle_type open_file_helper(const String& path, const access_mode mode)
{ {
return ::CreateFileA(c_str(path), return ::CreateFileW(s_2_ws(path).c_str(),
mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE, mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
0, 0,