From fe6bd9f9d5c94085b133c4d05d04e9d5e24fa972 Mon Sep 17 00:00:00 2001 From: fxliang Date: Thu, 1 Feb 2024 22:57:24 +0800 Subject: [PATCH 1/2] fix: under Windows(CP_ACP != CP_UTF8), ResolvePath(resource_id) failed in some cases. --- src/rime/resource.cc | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/rime/resource.cc b/src/rime/resource.cc index b44d8d8df9..ef25f2dd9b 100644 --- a/src/rime/resource.cc +++ b/src/rime/resource.cc @@ -7,6 +7,10 @@ #include #include +#ifdef _MSC_VER +#include +#endif + namespace rime { string ResourceResolver::ToResourceId(const string& file_path) const { @@ -27,19 +31,48 @@ string ResourceResolver::ToFilePath(const string& resource_id) const { (missing_suffix ? type_.suffix : ""); } +#ifdef _MSC_VER +static std::string U16ToACP(const std::wstring& wstr) { + std::string ret; + int length = static_cast(wstr.length()); + int convcnt = + WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), length, NULL, 0, NULL, NULL); + if (convcnt > 0) { + ret.resize(convcnt); + WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), length, &ret[0], convcnt, NULL, + NULL); + } + return ret; +} +#endif + std::filesystem::path ResourceResolver::ResolvePath(const string& resource_id) { +#ifdef _MSC_VER + std::wstring platform_id_wstr = + opencc::UTF8Util::GetPlatformString(resource_id); + std::string resource_id_ = U16ToACP(platform_id_wstr); +#else + std::string resource_id_ = resource_id; +#endif return std::filesystem::absolute( root_path_ / - std::filesystem::path(type_.prefix + resource_id + type_.suffix)); + std::filesystem::path(type_.prefix + resource_id_ + type_.suffix)); } std::filesystem::path FallbackResourceResolver::ResolvePath( const string& resource_id) { - auto default_path = ResourceResolver::ResolvePath(resource_id); +#ifdef _MSC_VER + std::wstring platform_id_wstr = + opencc::UTF8Util::GetPlatformString(resource_id); + std::string resource_id_ = U16ToACP(platform_id_wstr); +#else + std::string resource_id_ = resource_id; +#endif + auto default_path = ResourceResolver::ResolvePath(resource_id_); if (!std::filesystem::exists(default_path)) { auto fallback_path = std::filesystem::absolute( fallback_root_path_ / - std::filesystem::path(type_.prefix + resource_id + type_.suffix)); + std::filesystem::path(type_.prefix + resource_id_ + type_.suffix)); if (std::filesystem::exists(fallback_path)) { return fallback_path; } From 001208c73ab53e942432c8a118cc2cee713f17b3 Mon Sep 17 00:00:00 2001 From: liangfx Date: Fri, 2 Feb 2024 12:38:51 +0800 Subject: [PATCH 2/2] use std::filesystem::u8path for c++17 standard --- src/rime/resource.cc | 49 +++++++++++++------------------------------- 1 file changed, 14 insertions(+), 35 deletions(-) diff --git a/src/rime/resource.cc b/src/rime/resource.cc index ef25f2dd9b..2fb7466455 100644 --- a/src/rime/resource.cc +++ b/src/rime/resource.cc @@ -7,10 +7,6 @@ #include #include -#ifdef _MSC_VER -#include -#endif - namespace rime { string ResourceResolver::ToResourceId(const string& file_path) const { @@ -31,48 +27,31 @@ string ResourceResolver::ToFilePath(const string& resource_id) const { (missing_suffix ? type_.suffix : ""); } -#ifdef _MSC_VER -static std::string U16ToACP(const std::wstring& wstr) { - std::string ret; - int length = static_cast(wstr.length()); - int convcnt = - WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), length, NULL, 0, NULL, NULL); - if (convcnt > 0) { - ret.resize(convcnt); - WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), length, &ret[0], convcnt, NULL, - NULL); - } - return ret; -} -#endif - std::filesystem::path ResourceResolver::ResolvePath(const string& resource_id) { -#ifdef _MSC_VER - std::wstring platform_id_wstr = - opencc::UTF8Util::GetPlatformString(resource_id); - std::string resource_id_ = U16ToACP(platform_id_wstr); +#ifdef _WIN32 + return std::filesystem::absolute( + root_path_ / + std::filesystem::u8path(type_.prefix + resource_id + type_.suffix)); #else - std::string resource_id_ = resource_id; -#endif return std::filesystem::absolute( root_path_ / - std::filesystem::path(type_.prefix + resource_id_ + type_.suffix)); + std::filesystem::path(type_.prefix + resource_id + type_.suffix)); +#endif } std::filesystem::path FallbackResourceResolver::ResolvePath( const string& resource_id) { -#ifdef _MSC_VER - std::wstring platform_id_wstr = - opencc::UTF8Util::GetPlatformString(resource_id); - std::string resource_id_ = U16ToACP(platform_id_wstr); -#else - std::string resource_id_ = resource_id; -#endif - auto default_path = ResourceResolver::ResolvePath(resource_id_); + auto default_path = ResourceResolver::ResolvePath(resource_id); if (!std::filesystem::exists(default_path)) { +#ifdef _WIN32 auto fallback_path = std::filesystem::absolute( fallback_root_path_ / - std::filesystem::path(type_.prefix + resource_id_ + type_.suffix)); + std::filesystem::u8path(type_.prefix + resource_id + type_.suffix)); +#else + auto fallback_path = std::filesystem::absolute( + fallback_root_path_ / + std::filesystem::path(type_.prefix + resource_id + type_.suffix)); +#endif if (std::filesystem::exists(fallback_path)) { return fallback_path; }