diff --git a/src/modules.cc b/src/modules.cc index 2e84459..a4ed5e0 100644 --- a/src/modules.cc +++ b/src/modules.cc @@ -2,6 +2,7 @@ #include #include #include +#include #include "lib/lua_templates.h" #include "lua_gears.h" @@ -16,9 +17,37 @@ static bool file_exists(const char *fname) noexcept { return false; } +namespace { +template using void_t = void; + +template +struct COMPAT { + static std::string get_shared_data_dir() { + return std::string(rime_get_api()->get_shared_data_dir()); + } + + static std::string get_user_data_dir() { + return std::string(rime_get_api()->get_user_data_dir()); + } +}; + +template +struct COMPAT().user_data_dir.string())>> { + static std::string get_shared_data_dir() { + // path::string() returns native encoding on Windows + return rime::Service::instance().deployer().shared_data_dir.string(); + } + + static std::string get_user_data_dir() { + return rime::Service::instance().deployer().user_data_dir.string(); + } +}; +} + static void lua_init(lua_State *L) { - const auto user_dir = std::string(RimeGetUserDataDir()); - const auto shared_dir = std::string(RimeGetSharedDataDir()); + + const auto user_dir = COMPAT::get_user_data_dir(); + const auto shared_dir = COMPAT::get_shared_data_dir(); types_init(L); lua_getglobal(L, "package"); diff --git a/src/opencc.cc b/src/opencc.cc index 17c9b90..16a5647 100644 --- a/src/opencc.cc +++ b/src/opencc.cc @@ -13,8 +13,8 @@ #include #include #include -#include #include +#include #include "lib/lua_export_type.h" #include "optional.h" @@ -28,8 +28,8 @@ namespace { class Opencc { public: - //static shared_ptr create(const string &config_path); - Opencc(const string& config_path); + //static shared_ptr create(const path &config_path); + Opencc(const string& utf8_config_path); bool ConvertWord(const string& text, vector* forms); bool RandomConvertText(const string& text, string* simplified); bool ConvertText(const string& text, string* simplified); @@ -43,9 +43,10 @@ class Opencc { opencc::DictPtr dict_; }; -Opencc::Opencc(const string& config_path) { +Opencc::Opencc(const string& utf8_config_path) { opencc::Config config; - converter_ = config.NewFromFile(config_path); + // OpenCC accepts UTF-8 encoded path. + converter_ = config.NewFromFile(utf8_config_path); const list conversions = converter_->GetConversionChain()->GetConversions(); dict_ = conversions.front()->GetDict(); @@ -115,23 +116,49 @@ vector Opencc::convert_word(const string& text){ namespace OpenccReg { using T = Opencc; - optional make(const string &filename) { - string user_path( RimeGetUserDataDir()); - string shared_path(RimeGetSharedDataDir()); - try{ - return T(user_path + "/opencc/" + filename); + template using void_t = void; + + template + struct COMPAT { + static optional make(const string &filename) { + auto user_path = string(rime_get_api()->get_user_data_dir()); + auto shared_path = string(rime_get_api()->get_shared_data_dir()); + try{ + return T(user_path + "/opencc/" + filename); + } + catch(...) { + try{ + return T(shared_path + "/opencc/" + filename); + } + catch(...) { + LOG(ERROR) << " [" << user_path << "|" << shared_path << "]/opencc/" + << filename << ": File not found or InvalidFormat"; + return {}; + } + } } - catch(...) { + }; + + template + struct COMPAT().user_data_dir.string())>> { + static optional make(const string &filename) { + path user_path = Service::instance().deployer().user_data_dir; + path shared_path = Service::instance().deployer().shared_data_dir; try{ - return T(shared_path + "/opencc/" + filename); + return T((user_path / "opencc" / filename).u8string()); } catch(...) { - LOG(ERROR) << " [" << user_path << "|" << shared_path << "]/opencc/" - << filename << ": File not found or InvalidFormat"; - return {}; + try{ + return T((shared_path / "opencc" / filename).u8string()); + } + catch(...) { + LOG(ERROR) << " [" << user_path << "|" << shared_path << "]/opencc/" + << filename << ": File not found or InvalidFormat"; + return {}; + } } } - } + }; optional> convert_word(T &t,const string &s) { vector res; @@ -141,7 +168,7 @@ namespace OpenccReg { } static const luaL_Reg funcs[] = { - {"Opencc",WRAP(make)}, + {"Opencc",WRAP(COMPAT::make)}, { NULL, NULL }, }; diff --git a/src/types.cc b/src/types.cc index 21fdd9f..1f4fe74 100644 --- a/src/types.cc +++ b/src/types.cc @@ -1,3 +1,4 @@ +#include #include #include #include @@ -15,9 +16,9 @@ #include #include #include +#include #include #include "lua_gears.h" -#include #include #include "lib/lua_export_type.h" @@ -29,6 +30,47 @@ using namespace rime; namespace { +template using void_t = void; + +template +struct COMPAT { + // fallback version if librime is old + static an new_ReverseDb(const std::string &file) { + return New(std::string(RimeGetUserDataDir()) + "/" + file); + } + + static string get_shared_data_dir() { + return string(rime_get_api()->get_shared_data_dir()); + } + + static string get_user_data_dir() { + return string(rime_get_api()->get_user_data_dir()); + } + + static string get_sync_dir() { + return string(rime_get_api()->get_sync_dir()); + } +}; + +template +struct COMPAT().user_data_dir.string())>> { + static an new_ReverseDb(const std::string &file) { + return New(Service::instance().deployer().user_data_dir / file); + } + + static string get_shared_data_dir() { + return Service::instance().deployer().shared_data_dir.string(); + } + + static string get_user_data_dir() { + return Service::instance().deployer().user_data_dir.string(); + } + + static string get_sync_dir() { + return Service::instance().deployer().sync_dir.string(); + } +}; + //--- wrappers for Segment namespace SegmentReg { using T = Segment; @@ -308,7 +350,7 @@ namespace ReverseDbReg { using T = ReverseDb; an make(const string &file) { - an db = New(string(RimeGetUserDataDir()) + "/" + file); + an db = COMPAT::new_ReverseDb(file); db->Load(); return db; } @@ -1844,43 +1886,23 @@ namespace KeySequenceReg { namespace RimeApiReg { string get_rime_version() { - RimeApi* rime = rime_get_api(); - return string(rime->get_version()); - } - - string get_shared_data_dir() { - RimeApi* rime = rime_get_api(); - return string(rime->get_shared_data_dir()); - } - - string get_user_data_dir() { - RimeApi* rime = rime_get_api(); - return string(rime->get_user_data_dir()); - } - - string get_sync_dir() { - RimeApi* rime = rime_get_api(); - return string(rime->get_sync_dir()); + return string(rime_get_api()->get_version()); } string get_distribution_name(){ - Deployer &deployer(Service::instance().deployer()); - return deployer.distribution_name; + return Service::instance().deployer().distribution_name; } string get_distribution_code_name(){ - Deployer &deployer(Service::instance().deployer()); - return deployer.distribution_code_name; + return Service::instance().deployer().distribution_code_name; } string get_distribution_version(){ - Deployer &deployer(Service::instance().deployer()); - return deployer.distribution_version; + return Service::instance().deployer().distribution_version; } string get_user_id(){ - Deployer &deployer(Service::instance().deployer()); - return deployer.user_id; + return Service::instance().deployer().user_id; } // boost::regex api @@ -1912,9 +1934,9 @@ namespace RimeApiReg { static const luaL_Reg funcs[]= { { "get_rime_version", WRAP(get_rime_version) }, - { "get_shared_data_dir", WRAP(get_shared_data_dir) }, - { "get_user_data_dir", WRAP(get_user_data_dir) }, - { "get_sync_dir", WRAP(get_sync_dir) }, + { "get_shared_data_dir", WRAP(COMPAT::get_shared_data_dir) }, + { "get_user_data_dir", WRAP(COMPAT::get_user_data_dir) }, + { "get_sync_dir", WRAP(COMPAT::get_sync_dir) }, { "get_distribution_name", WRAP(get_distribution_name) }, { "get_distribution_code_name", WRAP(get_distribution_code_name) }, { "get_distribution_version", WRAP(get_distribution_version) }, diff --git a/src/types_ext.cc b/src/types_ext.cc index a69d033..99e351d 100644 --- a/src/types_ext.cc +++ b/src/types_ext.cc @@ -38,6 +38,18 @@ struct COMPAT().name_space())>> { } }; +// fallback version of file_path() if librime is old +template struct void_t1 { using t = int; }; +template().file_name())>::t = 0> +std::string get_UserDb_file_path_string(const T &t) { + return t.file_name(); +} + +template().file_path())>::t = 0> +std::string get_UserDb_file_path_string(const T &t) { + return t.file_path().string(); +} + namespace ProcessorReg{ using T = Processor; @@ -263,6 +275,11 @@ namespace UserDbReg{ return {}; } + string file_name(const T& t) { + // returns ANSI encoded string on Windows + return t.file_path().string(); + } + bool Open(T &t) { return t.Open(); } bool Close(T &t) { return t.Close(); } bool OpenReadOnly(T &t) { return t.OpenReadOnly(); } @@ -300,7 +317,7 @@ namespace UserDbReg{ {"read_only",WRAPMEM(T, readonly)}, {"disabled",WRAPMEM(T, disabled)}, {"name", WRAPMEM(T, name)}, - {"file_name", WRAPMEM(T, file_name)}, + {"file_name", WRAP(get_UserDb_file_path_string)}, { NULL, NULL }, };