Skip to content

Commit

Permalink
use std::filesystem::u8path for c++17 standard
Browse files Browse the repository at this point in the history
  • Loading branch information
fxliang committed Feb 2, 2024
1 parent fe6bd9f commit 001208c
Showing 1 changed file with 14 additions and 35 deletions.
49 changes: 14 additions & 35 deletions src/rime/resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#include <filesystem>
#include <rime/resource.h>

#ifdef _MSC_VER
#include <opencc/UTF8Util.hpp>
#endif

namespace rime {

string ResourceResolver::ToResourceId(const string& file_path) const {
Expand All @@ -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<int>(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;
}
Expand Down

0 comments on commit 001208c

Please sign in to comment.