Skip to content

Commit 65239cd

Browse files
committed
follow up librime, avoid path-string conversion
Avoid using `RimeGet*Dir()` from `rime_api`. They return native path on Windows and coding conversion is incurred. When passing path from lua to librime class, use either rime::path object or UTF-8 encoded string.
1 parent a29897d commit 65239cd

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

src/modules.cc

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <cstdio>
22
#include <rime/common.h>
33
#include <rime/registry.h>
4-
#include <rime_api.h>
4+
#include <rime/service.h>
55
#include "lib/lua_templates.h"
66
#include "lua_gears.h"
77

@@ -17,8 +17,11 @@ static bool file_exists(const char *fname) noexcept {
1717
}
1818

1919
static void lua_init(lua_State *L) {
20-
const auto user_dir = std::string(RimeGetUserDataDir());
21-
const auto shared_dir = std::string(RimeGetSharedDataDir());
20+
// path::string() returns native encoding on Windows
21+
const auto user_dir =
22+
rime::Service::instance().deployer().user_data_dir.string();
23+
const auto shared_dir =
24+
rime::Service::instance().deployer().shared_data_dir.string();
2225

2326
types_init(L);
2427
lua_getglobal(L, "package");

src/opencc.cc

+10-9
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#include <opencc/Dict.hpp>
1414
#include <opencc/DictEntry.hpp>
1515
#include <opencc/Common.hpp>
16-
#include <rime_api.h>
1716
#include <rime/common.h>
17+
#include <rime/service.h>
1818

1919
#include "lib/lua_export_type.h"
2020
#include "optional.h"
@@ -28,8 +28,8 @@ namespace {
2828

2929
class Opencc {
3030
public:
31-
//static shared_ptr<Opencc> create(const string &config_path);
32-
Opencc(const string& config_path);
31+
//static shared_ptr<Opencc> create(const path &config_path);
32+
Opencc(const path& config_path);
3333
bool ConvertWord(const string& text, vector<string>* forms);
3434
bool RandomConvertText(const string& text, string* simplified);
3535
bool ConvertText(const string& text, string* simplified);
@@ -43,9 +43,10 @@ class Opencc {
4343
opencc::DictPtr dict_;
4444
};
4545

46-
Opencc::Opencc(const string& config_path) {
46+
Opencc::Opencc(const path& config_path) {
4747
opencc::Config config;
48-
converter_ = config.NewFromFile(config_path);
48+
// OpenCC accepts UTF-8 encoded path.
49+
converter_ = config.NewFromFile(config_path.u8string());
4950
const list<opencc::ConversionPtr> conversions =
5051
converter_->GetConversionChain()->GetConversions();
5152
dict_ = conversions.front()->GetDict();
@@ -116,14 +117,14 @@ namespace OpenccReg {
116117
using T = Opencc;
117118

118119
optional<T> make(const string &filename) {
119-
string user_path( RimeGetUserDataDir());
120-
string shared_path(RimeGetSharedDataDir());
120+
path user_path = Service::instance().deployer().user_data_dir;
121+
path shared_path = Service::instance().deployer().shared_data_dir;
121122
try{
122-
return T(user_path + "/opencc/" + filename);
123+
return T(user_path / "opencc" / filename);
123124
}
124125
catch(...) {
125126
try{
126-
return T(shared_path + "/opencc/" + filename);
127+
return T(shared_path / "opencc" / filename);
127128
}
128129
catch(...) {
129130
LOG(ERROR) << " [" << user_path << "|" << shared_path << "]/opencc/"

src/types.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,8 @@ namespace ReverseDbReg {
308308
using T = ReverseDb;
309309

310310
an<T> make(const string &file) {
311-
an<T> db = New<ReverseDb>(string(RimeGetUserDataDir()) + "/" + file);
311+
an<T> db = New<ReverseDb>(
312+
Service::instance().deployer().user_data_dir / file);
312313
db->Load();
313314
return db;
314315
}

src/types_ext.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ namespace UserDbReg{
263263
return {};
264264
}
265265

266+
string file_name(const T& t) {
267+
return t.file_path().u8string();
268+
}
269+
266270
bool Open(T &t) { return t.Open(); }
267271
bool Close(T &t) { return t.Close(); }
268272
bool OpenReadOnly(T &t) { return t.OpenReadOnly(); }
@@ -300,7 +304,7 @@ namespace UserDbReg{
300304
{"read_only",WRAPMEM(T, readonly)},
301305
{"disabled",WRAPMEM(T, disabled)},
302306
{"name", WRAPMEM(T, name)},
303-
{"file_name", WRAPMEM(T, file_name)},
307+
{"file_name", WRAP(file_name)},
304308
{ NULL, NULL },
305309
};
306310

0 commit comments

Comments
 (0)