Skip to content

Commit 17b33b6

Browse files
shewerhchunhui
andauthored
fixed config:load_from_file() & config:save_tofile , add Config() (#365)
* fixed load_from_file(string) & save_to_file(string) 解決 librime v1.10 使用 class path 替代 string * add Config([string]) * build with old librime --------- Co-authored-by: Chunhui He <hchunhui@mail.ustc.edu.cn>
1 parent 0960d99 commit 17b33b6

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/types.cc

+27-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ struct COMPAT {
5050
static string get_sync_dir() {
5151
return string(rime_get_api()->get_sync_dir());
5252
}
53+
54+
static string to_path(const std::string &file) {
55+
return file;
56+
}
5357
};
5458

5559
template<typename T>
@@ -73,6 +77,10 @@ struct COMPAT<T, void_t<decltype(std::declval<T>().user_data_dir.string())>> {
7377
T &deployer = Service::instance().deployer();
7478
return deployer.sync_dir.string();
7579
}
80+
81+
static path to_path(const std::string &file) {
82+
return path(file);
83+
}
7684
};
7785

7886
//--- wrappers for Segment
@@ -1214,6 +1222,15 @@ namespace ProjectionReg{
12141222
namespace ConfigReg {
12151223
using T = Config;
12161224

1225+
int raw_make(lua_State *L) {
1226+
an<T> config = New<T>();
1227+
if (auto cstr = lua_tostring(L, 1)) {
1228+
config->LoadFromFile(COMPAT<Deployer>::to_path(cstr));
1229+
}
1230+
LuaType<an<T>>::pushdata(L, config);
1231+
return 1;
1232+
}
1233+
12171234
optional<bool> get_bool(T &t, const string &path) {
12181235
bool v;
12191236
if (t.GetBool(path, &v))
@@ -1272,15 +1289,23 @@ namespace ConfigReg {
12721289
return t.SetItem(path, value);
12731290
}
12741291

1292+
bool load_from_file(T &t, const string &f) {
1293+
return t.LoadFromFile(COMPAT<Deployer>::to_path(f));
1294+
}
1295+
bool save_to_file(T &t, const string &f) {
1296+
return t.SaveToFile(COMPAT<Deployer>::to_path(f));
1297+
}
1298+
12751299
static const luaL_Reg funcs[] = {
1300+
{ "Config", (raw_make)},
12761301
{ NULL, NULL },
12771302
};
12781303

12791304
static const luaL_Reg methods[] = {
12801305
//bool LoadFromStream(std::istream& stream);
12811306
//bool SaveToStream(std::ostream& stream);
1282-
{ "load_from_file", WRAPMEM(T::LoadFromFile) },
1283-
{ "save_to_file", WRAPMEM(T::SaveToFile) },
1307+
{ "load_from_file", WRAP(load_from_file) },
1308+
{ "save_to_file", WRAP(save_to_file) },
12841309

12851310
{ "is_null", WRAPMEM(T::IsNull) },
12861311
{ "is_value", WRAPMEM(T::IsValue) },

0 commit comments

Comments
 (0)