From 72aa534b813f957053849be7668f6da35faad9f2 Mon Sep 17 00:00:00 2001 From: shiagarw Date: Sun, 20 Dec 2020 23:49:18 +0530 Subject: [PATCH 1/3] Adding support of UTF8 file name processing in mio on windows Contributing back changes from app. --- include/mio/detail/mmap.ipp | 12 +++++++++++- single_include/mio/mio.hpp | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/mio/detail/mmap.ipp b/include/mio/detail/mmap.ipp index aaf6494..2dbfe1f 100644 --- a/include/mio/detail/mmap.ipp +++ b/include/mio/detail/mmap.ipp @@ -68,13 +68,23 @@ template< 0); } +std::wstring s_2_ws(const std::string& s) +{ + if (s.empty()) + return{}; + const auto s_length = static_cast(s.length()); + auto buf = std::vector(s_length); + const auto wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), sLength, buf.data(), s_length); + return std::wstring(buf.data(), wide_char_count); +} + template typename std::enable_if< std::is_same::type, wchar_t>::value, file_handle_type >::type open_file_helper(const String& path, const access_mode mode) { - return ::CreateFileW(c_str(path), + return ::CreateFileW(s_2_ws(path).c_str(), mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, diff --git a/single_include/mio/mio.hpp b/single_include/mio/mio.hpp index 95c696c..f1b53c7 100644 --- a/single_include/mio/mio.hpp +++ b/single_include/mio/mio.hpp @@ -810,13 +810,23 @@ template< 0); } +std::wstring s_2_ws(const std::string& s) +{ + if (s.empty()) + return{}; + const auto s_length = static_cast(s.length()); + auto buf = std::vector(s_length); + const auto wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), sLength, buf.data(), s_length); + return std::wstring(buf.data(), wide_char_count); +} + template typename std::enable_if< std::is_same::type, wchar_t>::value, file_handle_type >::type open_file_helper(const String& path, const access_mode mode) { - return ::CreateFileW(c_str(path), + return ::CreateFileW(s_2_ws(path).c_str(), mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, From 5bd9c3981f5fc63995ed6b3b7c8ff251f25c243c Mon Sep 17 00:00:00 2001 From: shiagarw Date: Sun, 20 Dec 2020 23:53:16 +0530 Subject: [PATCH 2/3] fixing typo --- include/mio/detail/mmap.ipp | 2 +- single_include/mio/mio.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/mio/detail/mmap.ipp b/include/mio/detail/mmap.ipp index 2dbfe1f..eef5db5 100644 --- a/include/mio/detail/mmap.ipp +++ b/include/mio/detail/mmap.ipp @@ -74,7 +74,7 @@ std::wstring s_2_ws(const std::string& s) return{}; const auto s_length = static_cast(s.length()); auto buf = std::vector(s_length); - const auto wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), sLength, buf.data(), 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); } diff --git a/single_include/mio/mio.hpp b/single_include/mio/mio.hpp index f1b53c7..124f1fd 100644 --- a/single_include/mio/mio.hpp +++ b/single_include/mio/mio.hpp @@ -816,7 +816,7 @@ std::wstring s_2_ws(const std::string& s) return{}; const auto s_length = static_cast(s.length()); auto buf = std::vector(s_length); - const auto wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), sLength, buf.data(), 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); } From 1dada12aba4c21eb0166db7d637f64c099c42f80 Mon Sep 17 00:00:00 2001 From: shiagarw Date: Mon, 21 Dec 2020 00:05:44 +0530 Subject: [PATCH 3/3] fixing overload made changes in wrong overload by mistake --- include/mio/detail/mmap.ipp | 34 +++++++++++++++++----------------- single_include/mio/mio.hpp | 34 +++++++++++++++++----------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/include/mio/detail/mmap.ipp b/include/mio/detail/mmap.ipp index eef5db5..1b6dc4d 100644 --- a/include/mio/detail/mmap.ipp +++ b/include/mio/detail/mmap.ipp @@ -52,22 +52,6 @@ inline DWORD int64_low(int64_t n) noexcept return n & 0xffffffff; } -template< - typename String, - typename = typename std::enable_if< - std::is_same::type, char>::value - >::type -> file_handle_type open_file_helper(const String& path, const access_mode mode) -{ - return ::CreateFileA(c_str(path), - mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, - 0, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - 0); -} - std::wstring s_2_ws(const std::string& s) { if (s.empty()) @@ -78,13 +62,29 @@ std::wstring s_2_ws(const std::string& s) return std::wstring(buf.data(), wide_char_count); } +template< + typename String, + typename = typename std::enable_if< + std::is_same::type, char>::value + >::type +> file_handle_type open_file_helper(const String& path, const access_mode mode) +{ + return ::CreateFileW(s_2_ws(path).c_str(), + mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + 0, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + 0); +} + template typename std::enable_if< std::is_same::type, wchar_t>::value, file_handle_type >::type open_file_helper(const String& path, const access_mode mode) { - return ::CreateFileW(s_2_ws(path).c_str(), + return ::CreateFileW(c_str(path), mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, diff --git a/single_include/mio/mio.hpp b/single_include/mio/mio.hpp index 124f1fd..c568a46 100644 --- a/single_include/mio/mio.hpp +++ b/single_include/mio/mio.hpp @@ -794,22 +794,6 @@ inline DWORD int64_low(int64_t n) noexcept return n & 0xffffffff; } -template< - typename String, - typename = typename std::enable_if< - std::is_same::type, char>::value - >::type -> file_handle_type open_file_helper(const String& path, const access_mode mode) -{ - return ::CreateFileA(c_str(path), - mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE, - FILE_SHARE_READ | FILE_SHARE_WRITE, - 0, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - 0); -} - std::wstring s_2_ws(const std::string& s) { if (s.empty()) @@ -820,13 +804,29 @@ std::wstring s_2_ws(const std::string& s) return std::wstring(buf.data(), wide_char_count); } +template< + typename String, + typename = typename std::enable_if< + std::is_same::type, char>::value + >::type +> file_handle_type open_file_helper(const String& path, const access_mode mode) +{ + return ::CreateFileW(s_2_ws(path).c_str(), + mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + 0, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + 0); +} + template typename std::enable_if< std::is_same::type, wchar_t>::value, file_handle_type >::type open_file_helper(const String& path, const access_mode mode) { - return ::CreateFileW(s_2_ws(path).c_str(), + return ::CreateFileW(c_str(path), mode == access_mode::read ? GENERIC_READ : GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0,