Skip to content

Commit d1e644d

Browse files
committed
compat old librime
1 parent df714a5 commit d1e644d

File tree

4 files changed

+130
-38
lines changed

4 files changed

+130
-38
lines changed

src/modules.cc

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <cstdio>
22
#include <rime/common.h>
33
#include <rime/registry.h>
4+
#include <rime_api.h>
45
#include <rime/service.h>
56
#include "lib/lua_templates.h"
67
#include "lua_gears.h"
@@ -16,12 +17,37 @@ static bool file_exists(const char *fname) noexcept {
1617
return false;
1718
}
1819

20+
namespace {
21+
template<typename> using void_t = void;
22+
23+
template<typename T, typename = void>
24+
struct COMPAT {
25+
static std::string get_shared_data_dir() {
26+
return std::string(rime_get_api()->get_shared_data_dir());
27+
}
28+
29+
static std::string get_user_data_dir() {
30+
return std::string(rime_get_api()->get_user_data_dir());
31+
}
32+
};
33+
34+
template<typename T>
35+
struct COMPAT<T, void_t<decltype(std::declval<T>().user_data_dir.string())>> {
36+
static std::string get_shared_data_dir() {
37+
// path::string() returns native encoding on Windows
38+
return rime::Service::instance().deployer().shared_data_dir.string();
39+
}
40+
41+
static std::string get_user_data_dir() {
42+
return rime::Service::instance().deployer().user_data_dir.string();
43+
}
44+
};
45+
}
46+
1947
static void lua_init(lua_State *L) {
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();
48+
49+
const auto user_dir = COMPAT<rime::Deployer>::get_user_data_dir();
50+
const auto shared_dir = COMPAT<rime::Deployer>::get_shared_data_dir();
2551

2652
types_init(L);
2753
lua_getglobal(L, "package");

src/opencc.cc

+41-15
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace {
2929
class Opencc {
3030
public:
3131
//static shared_ptr<Opencc> create(const path &config_path);
32-
Opencc(const path& config_path);
32+
Opencc(const string& utf8_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,10 +43,10 @@ class Opencc {
4343
opencc::DictPtr dict_;
4444
};
4545

46-
Opencc::Opencc(const path& config_path) {
46+
Opencc::Opencc(const string& utf8_config_path) {
4747
opencc::Config config;
4848
// OpenCC accepts UTF-8 encoded path.
49-
converter_ = config.NewFromFile(config_path.u8string());
49+
converter_ = config.NewFromFile(utf8_config_path);
5050
const list<opencc::ConversionPtr> conversions =
5151
converter_->GetConversionChain()->GetConversions();
5252
dict_ = conversions.front()->GetDict();
@@ -116,23 +116,49 @@ vector<string> Opencc::convert_word(const string& text){
116116
namespace OpenccReg {
117117
using T = Opencc;
118118

119-
optional<T> make(const string &filename) {
120-
path user_path = Service::instance().deployer().user_data_dir;
121-
path shared_path = Service::instance().deployer().shared_data_dir;
122-
try{
123-
return T(user_path / "opencc" / filename);
119+
template<typename> using void_t = void;
120+
121+
template<typename U, typename = void>
122+
struct COMPAT {
123+
static optional<T> make(const string &filename) {
124+
auto user_path = string(rime_get_api()->get_user_data_dir());
125+
auto shared_path = string(rime_get_api()->get_shared_data_dir());
126+
try{
127+
return T(user_path + "/opencc/" + filename);
128+
}
129+
catch(...) {
130+
try{
131+
return T(shared_path + "/opencc/" + filename);
132+
}
133+
catch(...) {
134+
LOG(ERROR) << " [" << user_path << "|" << shared_path << "]/opencc/"
135+
<< filename << ": File not found or InvalidFormat";
136+
return {};
137+
}
138+
}
124139
}
125-
catch(...) {
140+
};
141+
142+
template<typename U>
143+
struct COMPAT<U, void_t<decltype(std::declval<U>().user_data_dir.string())>> {
144+
static optional<T> make(const string &filename) {
145+
path user_path = Service::instance().deployer().user_data_dir;
146+
path shared_path = Service::instance().deployer().shared_data_dir;
126147
try{
127-
return T(shared_path / "opencc" / filename);
148+
return T((user_path / "opencc" / filename).u8string());
128149
}
129150
catch(...) {
130-
LOG(ERROR) << " [" << user_path << "|" << shared_path << "]/opencc/"
131-
<< filename << ": File not found or InvalidFormat";
132-
return {};
151+
try{
152+
return T((shared_path / "opencc" / filename).u8string());
153+
}
154+
catch(...) {
155+
LOG(ERROR) << " [" << user_path << "|" << shared_path << "]/opencc/"
156+
<< filename << ": File not found or InvalidFormat";
157+
return {};
158+
}
133159
}
134160
}
135-
}
161+
};
136162

137163
optional<vector<string>> convert_word(T &t,const string &s) {
138164
vector<string> res;
@@ -142,7 +168,7 @@ namespace OpenccReg {
142168
}
143169

144170
static const luaL_Reg funcs[] = {
145-
{"Opencc",WRAP(make)},
171+
{"Opencc",WRAP(COMPAT<Deployer>::make)},
146172
{ NULL, NULL },
147173
};
148174

src/types.cc

+45-17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,47 @@ using namespace rime;
3030

3131
namespace {
3232

33+
template<typename> using void_t = void;
34+
35+
template<typename T, typename = void>
36+
struct COMPAT {
37+
// fallback version if librime is old
38+
static an<ReverseDb> new_ReverseDb(const std::string &file) {
39+
return New<ReverseDb>(std::string(RimeGetUserDataDir()) + "/" + file);
40+
}
41+
42+
static string get_shared_data_dir() {
43+
return string(rime_get_api()->get_shared_data_dir());
44+
}
45+
46+
static string get_user_data_dir() {
47+
return string(rime_get_api()->get_user_data_dir());
48+
}
49+
50+
static string get_sync_dir() {
51+
return string(rime_get_api()->get_sync_dir());
52+
}
53+
};
54+
55+
template<typename T>
56+
struct COMPAT<T, void_t<decltype(std::declval<T>().user_data_dir.string())>> {
57+
static an<ReverseDb> new_ReverseDb(const std::string &file) {
58+
return New<ReverseDb>(Service::instance().deployer().user_data_dir / file);
59+
}
60+
61+
static string get_shared_data_dir() {
62+
return Service::instance().deployer().shared_data_dir.string();
63+
}
64+
65+
static string get_user_data_dir() {
66+
return Service::instance().deployer().user_data_dir.string();
67+
}
68+
69+
static string get_sync_dir() {
70+
return Service::instance().deployer().sync_dir.string();
71+
}
72+
};
73+
3374
//--- wrappers for Segment
3475
namespace SegmentReg {
3576
using T = Segment;
@@ -309,8 +350,7 @@ namespace ReverseDbReg {
309350
using T = ReverseDb;
310351

311352
an<T> make(const string &file) {
312-
an<T> db = New<ReverseDb>(
313-
Service::instance().deployer().user_data_dir / file);
353+
an<T> db = COMPAT<Deployer>::new_ReverseDb(file);
314354
db->Load();
315355
return db;
316356
}
@@ -1849,18 +1889,6 @@ namespace RimeApiReg {
18491889
return string(rime_get_api()->get_version());
18501890
}
18511891

1852-
string get_shared_data_dir() {
1853-
return Service::instance().deployer().shared_data_dir.string();
1854-
}
1855-
1856-
string get_user_data_dir() {
1857-
return Service::instance().deployer().user_data_dir.string();
1858-
}
1859-
1860-
string get_sync_dir() {
1861-
return Service::instance().deployer().sync_dir.string();
1862-
}
1863-
18641892
string get_distribution_name(){
18651893
return Service::instance().deployer().distribution_name;
18661894
}
@@ -1906,9 +1934,9 @@ namespace RimeApiReg {
19061934

19071935
static const luaL_Reg funcs[]= {
19081936
{ "get_rime_version", WRAP(get_rime_version) },
1909-
{ "get_shared_data_dir", WRAP(get_shared_data_dir) },
1910-
{ "get_user_data_dir", WRAP(get_user_data_dir) },
1911-
{ "get_sync_dir", WRAP(get_sync_dir) },
1937+
{ "get_shared_data_dir", WRAP(COMPAT<Deployer>::get_shared_data_dir) },
1938+
{ "get_user_data_dir", WRAP(COMPAT<Deployer>::get_user_data_dir) },
1939+
{ "get_sync_dir", WRAP(COMPAT<Deployer>::get_sync_dir) },
19121940
{ "get_distribution_name", WRAP(get_distribution_name) },
19131941
{ "get_distribution_code_name", WRAP(get_distribution_code_name) },
19141942
{ "get_distribution_version", WRAP(get_distribution_version) },

src/types_ext.cc

+13-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ struct COMPAT<T, void_t<decltype(std::declval<T>().name_space())>> {
3838
}
3939
};
4040

41+
// fallback version of file_path() if librime is old
42+
template<typename> struct void_t1 { using t = int; };
43+
template<typename T, typename void_t1<decltype(std::declval<T>().file_name())>::t = 0>
44+
std::string get_UserDb_file_path_string(const T &t) {
45+
return t.file_name();
46+
}
47+
48+
template<typename T, typename void_t1<decltype(std::declval<T>().file_path())>::t = 0>
49+
std::string get_UserDb_file_path_string(const T &t) {
50+
return t.file_path().string();
51+
}
52+
4153
namespace ProcessorReg{
4254
using T = Processor;
4355

@@ -305,7 +317,7 @@ namespace UserDbReg{
305317
{"read_only",WRAPMEM(T, readonly)},
306318
{"disabled",WRAPMEM(T, disabled)},
307319
{"name", WRAPMEM(T, name)},
308-
{"file_name", WRAP(file_name)},
320+
{"file_name", WRAP(get_UserDb_file_path_string<T>)},
309321
{ NULL, NULL },
310322
};
311323

0 commit comments

Comments
 (0)