Skip to content

Commit 9c7d59e

Browse files
committed
fix(path): convert to native encoding on Windows
Follow @fxliang 's PR, use `u8path` on Windows to convert UTF-8 string to Windows native path. Closes rime#804 Fixes rime/weasel#576 Fixes rime/weasel#1080 BREAKING CHANGE: `installation.yaml` should be UTF-8 encoded. Previouly on Windows, the file can be written in local encoding to enable paths with non-ASCII characters. It should be updated to UTF-8 after this change.
1 parent d402755 commit 9c7d59e

23 files changed

+123
-137
lines changed

plugins/plugins_module.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ namespace rime {
2020

2121
class PluginManager {
2222
public:
23-
void LoadPlugins(fs::path plugins_dir);
23+
void LoadPlugins(path plugins_dir);
2424

25-
static string plugin_name_of(fs::path plugin_file);
25+
static string plugin_name_of(path plugin_file);
2626

2727
static PluginManager& instance();
2828

@@ -32,14 +32,14 @@ class PluginManager {
3232
map<string, boost::dll::shared_library> plugin_libs_;
3333
};
3434

35-
void PluginManager::LoadPlugins(fs::path plugins_dir) {
35+
void PluginManager::LoadPlugins(path plugins_dir) {
3636
ModuleManager& mm = ModuleManager::instance();
3737
if (!fs::is_directory(plugins_dir)) {
3838
return;
3939
}
4040
LOG(INFO) << "loading plugins from " << plugins_dir;
4141
for (fs::directory_iterator iter(plugins_dir), end; iter != end; ++iter) {
42-
fs::path plugin_file = iter->path();
42+
path plugin_file = iter->path();
4343
if (plugin_file.extension() == boost::dll::shared_library::suffix()) {
4444
fs::file_status plugin_file_status = fs::status(plugin_file);
4545
if (fs::is_regular_file(plugin_file_status)) {
@@ -69,7 +69,7 @@ void PluginManager::LoadPlugins(fs::path plugins_dir) {
6969
}
7070
}
7171

72-
string PluginManager::plugin_name_of(fs::path plugin_file) {
72+
string PluginManager::plugin_name_of(path plugin_file) {
7373
string name = plugin_file.stem().string();
7474
// remove prefix "(lib)rime-"
7575
if (boost::starts_with(name, "librime-")) {

src/rime/common.h

+22
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <rime/build_config.h>
1111

12+
#include <filesystem>
1213
#include <functional>
1314
#include <list>
1415
#include <map>
@@ -80,6 +81,27 @@ inline an<T> New(Args&&... args) {
8081
using boost::signals2::connection;
8182
using boost::signals2::signal;
8283

84+
#ifdef _WIN32
85+
class path : public std::filesystem::path {
86+
using fs_path = std::filesystem::path;
87+
88+
public:
89+
path() : fs_path() {}
90+
path(const fs_path& p) : fs_path(p) {}
91+
path(fs_path&& p) : fs_path(std::move(p)) {}
92+
path(const std::string& utf8_path)
93+
: fs_path(std::filesystem::u8path(utf8_path))
94+
{
95+
}
96+
path(const char* utf8_path)
97+
: fs_path(std::filesystem::u8path(utf8_path))
98+
{
99+
}
100+
};
101+
#else
102+
using std::filesystem::path;
103+
#endif
104+
83105
} // namespace rime
84106

85107
#endif // RIME_COMMON_H_

src/rime/config/save_output_plugin.cc

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Copyright RIME Developers
33
// Distributed under the BSD License
44
//
5-
#include <filesystem>
65
#include <rime/resource.h>
76
#include <rime/service.h>
87
#include <rime/config/config_compiler.h>

src/rime/deployer.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <chrono>
88
#include <exception>
99
#include <utility>
10-
#include <filesystem>
10+
#include <rime/common.h>
1111
#include <rime/deployer.h>
1212

1313
namespace rime {
@@ -147,7 +147,7 @@ void Deployer::JoinMaintenanceThread() {
147147
}
148148

149149
string Deployer::user_data_sync_dir() const {
150-
return (std::filesystem::path(sync_dir) / user_id).string();
150+
return (path(sync_dir) / user_id).string();
151151
}
152152

153153
} // namespace rime

src/rime/dict/dict_compiler.cc

+10-12
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
#include <rime/resource.h>
2323
#include <rime/service.h>
2424

25-
namespace fs = std::filesystem;
26-
2725
namespace rime {
2826

2927
DictCompiler::DictCompiler(Dictionary* dictionary)
@@ -39,7 +37,7 @@ DictCompiler::DictCompiler(Dictionary* dictionary)
3937
DictCompiler::~DictCompiler() {}
4038

4139
static bool load_dict_settings_from_file(DictSettings* settings,
42-
const fs::path& dict_file) {
40+
const path& dict_file) {
4341
std::ifstream fin(dict_file.string().c_str());
4442
bool success = settings->LoadDictHeader(fin);
4543
fin.close();
@@ -53,7 +51,7 @@ static bool get_dict_files_from_settings(vector<string>* dict_files,
5351
for (auto it = tables->begin(); it != tables->end(); ++it) {
5452
string dict_name = As<ConfigValue>(*it)->str();
5553
auto dict_file = source_resolver->ResolvePath(dict_name + ".dict.yaml");
56-
if (!fs::exists(dict_file)) {
54+
if (!std::filesystem::exists(dict_file)) {
5755
LOG(ERROR) << "source file '" << dict_file << "' does not exist.";
5856
return false;
5957
}
@@ -162,7 +160,7 @@ bool DictCompiler::Compile(const string& schema_file) {
162160
EntryCollector collector(std::move(syllabary));
163161
DictSettings settings;
164162
auto dict_file = source_resolver_->ResolvePath(pack_name + ".dict.yaml");
165-
if (!fs::exists(dict_file)) {
163+
if (!std::filesystem::exists(dict_file)) {
166164
LOG(ERROR) << "source file '" << dict_file << "' does not exist.";
167165
continue;
168166
}
@@ -188,8 +186,8 @@ bool DictCompiler::Compile(const string& schema_file) {
188186
return true;
189187
}
190188

191-
static fs::path relocate_target(const fs::path& source_path,
192-
ResourceResolver* target_resolver) {
189+
static path relocate_target(const path& source_path,
190+
ResourceResolver* target_resolver) {
193191
auto resource_id = source_path.filename().string();
194192
return target_resolver->ResolvePath(resource_id);
195193
}
@@ -208,7 +206,7 @@ bool DictCompiler::BuildTable(int table_index,
208206
collector.Configure(settings);
209207
collector.Collect(dict_files);
210208
if (options_ & kDump) {
211-
fs::path dump_path(table->file_name());
209+
path dump_path(table->file_name());
212210
dump_path.replace_extension(".txt");
213211
collector.Dump(dump_path.string());
214212
}
@@ -313,7 +311,7 @@ bool DictCompiler::BuildPrism(const string& schema_file,
313311
bool enable_correction = false; // Avoid if initializer to comfort compilers
314312
if (config.GetBool("translator/enable_correction", &enable_correction) &&
315313
enable_correction) {
316-
fs::path corrector_path(prism_->file_name());
314+
path corrector_path(prism_->file_name());
317315
corrector_path.replace_extension("");
318316
corrector_path.replace_extension(".correction.bin");
319317
auto target_path = relocate_target(corrector_path,
@@ -331,9 +329,9 @@ bool DictCompiler::BuildPrism(const string& schema_file,
331329
#endif
332330
}
333331
if ((options_ & kDump) && !script.empty()) {
334-
fs::path path(prism_->file_name());
335-
path.replace_extension(".txt");
336-
script.Dump(path.string());
332+
path dump_path(prism_->file_name());
333+
dump_path.replace_extension(".txt");
334+
script.Dump(dump_path.string());
337335
}
338336
// build .prism.bin
339337
{

src/rime/dict/level_db.cc

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// 2014-12-04 Chen Gong <[email protected]>
66
//
77

8-
#include <filesystem>
98
#include <leveldb/db.h>
109
#include <leveldb/write_batch.h>
1110
#include <rime/common.h>

src/rime/dict/preset_vocabulary.cc

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//
55
// 2011-11-27 GONG Chen <[email protected]>
66
//
7-
#include <filesystem>
87
#include <utf8.h>
98
#include <rime/resource.h>
109
#include <rime/service.h>

src/rime/dict/reverse_lookup_dictionary.cc

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <cstdlib>
1010
#include <sstream>
1111
#include <boost/algorithm/string.hpp>
12-
#include <filesystem>
1312
#include <rime/resource.h>
1413
#include <rime/schema.h>
1514
#include <rime/service.h>

src/rime/dict/user_db_recovery_task.cc

+4-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include <rime/dict/user_db.h>
1313
#include <rime/dict/user_db_recovery_task.h>
1414

15-
namespace fs = std::filesystem;
16-
1715
namespace rime {
1816

1917
UserDbRecoveryTask::UserDbRecoveryTask(an<Db> db) : db_(db) {
@@ -65,15 +63,15 @@ void UserDbRecoveryTask::RestoreUserDataFromSnapshot(Deployer* deployer) {
6563
string dict_name(db_->name());
6664
boost::erase_last(dict_name, component->extension());
6765
// locate snapshot file
68-
std::filesystem::path dir(deployer->user_data_sync_dir());
66+
path dir(deployer->user_data_sync_dir());
6967
// try *.userdb.txt
70-
fs::path snapshot_path = dir / (dict_name + UserDb::snapshot_extension());
71-
if (!fs::exists(snapshot_path)) {
68+
path snapshot_path = dir / (dict_name + UserDb::snapshot_extension());
69+
if (!std::filesystem::exists(snapshot_path)) {
7270
// try *.userdb.*.snapshot
7371
string legacy_snapshot_file =
7472
dict_name + component->extension() + ".snapshot";
7573
snapshot_path = dir / legacy_snapshot_file;
76-
if (!fs::exists(snapshot_path)) {
74+
if (!std::filesystem::exists(snapshot_path)) {
7775
return; // not found
7876
}
7977
}

src/rime/gear/simplifier.cc

+2-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// 2011-12-12 GONG Chen <[email protected]>
66
//
77
#include <boost/algorithm/string.hpp>
8-
#include <filesystem>
98
#include <stdint.h>
109
#include <utf8.h>
1110
#include <utility>
@@ -25,11 +24,6 @@
2524
#include <opencc/Dict.hpp>
2625
#include <opencc/DictEntry.hpp>
2726

28-
#ifdef _MSC_VER
29-
#include <opencc/UTF8Util.hpp>
30-
namespace fs = std::filesystem;
31-
#endif
32-
3327
static const char* quote_left = "\xe3\x80\x94"; //"\xef\xbc\x88";
3428
static const char* quote_right = "\xe3\x80\x95"; //"\xef\xbc\x89";
3529

@@ -42,13 +36,9 @@ class Opencc {
4236
opencc::Config config;
4337
try {
4438
// windows config_path in CP_ACP, convert it to UTF-8
45-
#ifdef _MSC_VER
46-
fs::path path{config_path};
47-
converter_ =
48-
config.NewFromFile(opencc::UTF8Util::U16ToU8(path.wstring()));
49-
#else
39+
path path{config_path};
5040
converter_ = config.NewFromFile(config_path);
51-
#endif /* _MSC_VER */
41+
5242
const list<opencc::ConversionPtr> conversions =
5343
converter_->GetConversionChain()->GetConversions();
5444
dict_ = conversions.front()->GetDict();
@@ -178,7 +168,6 @@ Simplifier::Simplifier(const Ticket& ticket)
178168
}
179169

180170
void Simplifier::Initialize() {
181-
using namespace std::filesystem;
182171
initialized_ = true; // no retry
183172
path opencc_config_path = opencc_config_;
184173
if (opencc_config_path.extension().string() == ".ini") {

src/rime/lever/custom_settings.cc

+6-11
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
// 2012-02-26 GONG Chen <[email protected]>
66
//
77
#include <boost/algorithm/string.hpp>
8-
#include <filesystem>
98
#include <rime/config.h>
109
#include <rime/deployer.h>
1110
#include <rime/signature.h>
1211
#include <rime/lever/custom_settings.h>
1312

14-
namespace fs = std::filesystem;
15-
1613
namespace rime {
1714

1815
static string remove_suffix(const string& input, const string& suffix) {
@@ -31,17 +28,15 @@ CustomSettings::CustomSettings(Deployer* deployer,
3128
: deployer_(deployer), config_id_(config_id), generator_id_(generator_id) {}
3229

3330
bool CustomSettings::Load() {
34-
fs::path config_path =
35-
fs::path(deployer_->staging_dir) / (config_id_ + ".yaml");
31+
path config_path = path(deployer_->staging_dir) / (config_id_ + ".yaml");
3632
if (!config_.LoadFromFile(config_path.string())) {
37-
config_path =
38-
fs::path(deployer_->prebuilt_data_dir) / (config_id_ + ".yaml");
33+
config_path = path(deployer_->prebuilt_data_dir) / (config_id_ + ".yaml");
3934
if (!config_.LoadFromFile(config_path.string())) {
4035
LOG(WARNING) << "cannot find '" << config_id_ << ".yaml'.";
4136
}
4237
}
43-
fs::path custom_config_path =
44-
fs::path(deployer_->user_data_dir) / custom_config_file(config_id_);
38+
path custom_config_path =
39+
path(deployer_->user_data_dir) / custom_config_file(config_id_);
4540
if (!custom_config_.LoadFromFile(custom_config_path.string())) {
4641
return false;
4742
}
@@ -54,7 +49,7 @@ bool CustomSettings::Save() {
5449
return false;
5550
Signature signature(generator_id_, "customization");
5651
signature.Sign(&custom_config_, deployer_);
57-
fs::path custom_config_path(deployer_->user_data_dir);
52+
path custom_config_path(deployer_->user_data_dir);
5853
custom_config_path /= custom_config_file(config_id_);
5954
custom_config_.SaveToFile(custom_config_path.string());
6055
modified_ = false;
@@ -87,7 +82,7 @@ bool CustomSettings::Customize(const string& key, const an<ConfigItem>& item) {
8782
}
8883

8984
bool CustomSettings::IsFirstRun() {
90-
fs::path custom_config_path(deployer_->user_data_dir);
85+
path custom_config_path(deployer_->user_data_dir);
9186
custom_config_path /= custom_config_file(config_id_);
9287
Config config;
9388
if (!config.LoadFromFile(custom_config_path.string()))

src/rime/lever/customizer.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55
// 2011-12-12 GONG Chen <[email protected]>
66
//
7+
#include <filesystem>
78
#include <stdint.h>
89
#include <rime/common.h>
910
#include <rime/config.h>
@@ -60,7 +61,7 @@ bool Customizer::UpdateConfigFile() {
6061
}
6162
}
6263

63-
fs::path custom_path(dest_path_);
64+
path custom_path(dest_path_);
6465
if (custom_path.extension() != ".yaml") {
6566
custom_path.clear();
6667
} else {

src/rime/lever/customizer.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
#ifndef RIME_CUSTOMIZER_H_
66
#define RIME_CUSTOMIZER_H_
77

8-
#include <filesystem>
8+
#include <rime/common.h>
99

1010
namespace rime {
1111

1212
class Customizer {
1313
public:
14-
Customizer(const std::filesystem::path& source_path,
15-
const std::filesystem::path& dest_path,
14+
Customizer(const path& source_path,
15+
const path& dest_path,
1616
const string& version_key)
1717
: source_path_(source_path),
1818
dest_path_(dest_path),
@@ -22,8 +22,8 @@ class Customizer {
2222
bool UpdateConfigFile();
2323

2424
protected:
25-
std::filesystem::path source_path_;
26-
std::filesystem::path dest_path_;
25+
path source_path_;
26+
path dest_path_;
2727
string version_key_;
2828
};
2929

0 commit comments

Comments
 (0)