diff --git a/src/rime/dict/db.cc b/src/rime/dict/db.cc index f7a4eaf52..314a03fad 100644 --- a/src/rime/dict/db.cc +++ b/src/rime/dict/db.cc @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -20,15 +21,17 @@ bool DbAccessor::MatchesPrefix(const string& key) { // Db members +static const ResourceType kDbResourceType = { + "db", "", "" +}; + Db::Db(const string& name) : name_(name) { - boost::filesystem::path path(name); - if (path.has_parent_path()) { - file_name_ = name; - } - else { - boost::filesystem::path dir(Service::instance().deployer().user_data_dir); - file_name_ = (dir / path).string(); + static ResourceResolver db_resource_resolver(kDbResourceType); + if (db_resource_resolver.root_path().empty()) { + db_resource_resolver.set_root_path( + Service::instance().deployer().user_data_dir); } + file_name_ = db_resource_resolver.ResolvePath(name).string(); } bool Db::Exists() const { diff --git a/src/rime/dict/dictionary.cc b/src/rime/dict/dictionary.cc index e6f177044..ee29a057c 100644 --- a/src/rime/dict/dictionary.cc +++ b/src/rime/dict/dictionary.cc @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -279,7 +280,22 @@ bool Dictionary::loaded() const { // DictionaryComponent members -DictionaryComponent::DictionaryComponent() { +static const ResourceType kPrismResourceType = { + "prism", "", ".prism.bin" +}; + +static const ResourceType kTableResourceType = { + "table", "", ".table.bin" +}; + +DictionaryComponent::DictionaryComponent() + : prism_resource_resolver_( + Service::instance().CreateResourceResolver(kPrismResourceType)), + table_resource_resolver_( + Service::instance().CreateResourceResolver(kTableResourceType)) { +} + +DictionaryComponent::~DictionaryComponent() { } Dictionary* DictionaryComponent::Create(const Ticket& ticket) { @@ -308,13 +324,13 @@ DictionaryComponent::CreateDictionaryWithName(const string& dict_name, boost::filesystem::path path(Service::instance().deployer().user_data_dir); auto table = table_map_[dict_name].lock(); if (!table) { - table = New((path / dict_name).string() + ".table.bin"); - table_map_[dict_name] = table; + auto file_path = table_resource_resolver_->ResolvePath(dict_name).string(); + table_map_[dict_name] = table = New
(file_path); } auto prism = prism_map_[prism_name].lock(); if (!prism) { - prism = New((path / prism_name).string() + ".prism.bin"); - prism_map_[prism_name] = prism; + auto file_path = prism_resource_resolver_->ResolvePath(prism_name).string(); + prism_map_[prism_name] = prism = New(file_path); } return new Dictionary(dict_name, table, prism); } diff --git a/src/rime/dict/dictionary.h b/src/rime/dict/dictionary.h index 3c52f7958..2851cf7b4 100644 --- a/src/rime/dict/dictionary.h +++ b/src/rime/dict/dictionary.h @@ -108,9 +108,12 @@ class Dictionary : public Class { an prism_; }; +class ResourceResolver; + class DictionaryComponent : public Dictionary::Component { public: DictionaryComponent(); + ~DictionaryComponent(); Dictionary* Create(const Ticket& ticket); Dictionary* CreateDictionaryWithName(const string& dict_name, const string& prism_name); @@ -118,6 +121,8 @@ class DictionaryComponent : public Dictionary::Component { private: map> prism_map_; map> table_map_; + the prism_resource_resolver_; + the table_resource_resolver_; }; } // namespace rime diff --git a/src/rime/dict/preset_vocabulary.cc b/src/rime/dict/preset_vocabulary.cc index 3d2d960b8..f1d6c759b 100644 --- a/src/rime/dict/preset_vocabulary.cc +++ b/src/rime/dict/preset_vocabulary.cc @@ -7,12 +7,19 @@ #include #include #include +#include #include #include #include namespace rime { +static const ResourceType kVocabularyResourceType = { + "vocabulary", "", ".txt" +}; + +static const string kDefaultVocabulary = "essay"; + struct VocabularyDb : public TextDb { explicit VocabularyDb(const string& path); an cursor; @@ -20,7 +27,7 @@ struct VocabularyDb : public TextDb { }; VocabularyDb::VocabularyDb(const string& path) - : TextDb(path, "vocabulary", VocabularyDb::format) { + : TextDb(path, kVocabularyResourceType.name, VocabularyDb::format) { } static bool rime_vocabulary_entry_parser(const Tsv& row, @@ -50,14 +57,9 @@ const TextFormat VocabularyDb::format = { }; string PresetVocabulary::DictFilePath() { - auto& deployer(Service::instance().deployer()); - boost::filesystem::path path(deployer.user_data_dir); - path /= "essay.txt"; - if (!boost::filesystem::exists(path)) { - path = deployer.shared_data_dir; - path /= "essay.txt"; - } - return path.string(); + the resource_resolver( + Service::instance().CreateResourceResolver(kVocabularyResourceType)); + return resource_resolver->ResolvePath(kDefaultVocabulary).string(); } PresetVocabulary::PresetVocabulary() { diff --git a/src/rime/dict/reverse_lookup_dictionary.cc b/src/rime/dict/reverse_lookup_dictionary.cc index 64f5926b1..c6404e090 100644 --- a/src/rime/dict/reverse_lookup_dictionary.cc +++ b/src/rime/dict/reverse_lookup_dictionary.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -24,13 +25,8 @@ const size_t kReverseFormatPrefixLen = sizeof(kReverseFormatPrefix) - 1; static const char* kStemKeySuffix = "\x1fstem"; -static string reverse_db_file_name(const string& dict_name) { - boost::filesystem::path dir(Service::instance().deployer().user_data_dir); - return (dir / dict_name).string() + ".reverse.bin"; -} - -ReverseDb::ReverseDb(const string& dict_name) - : MappedFile(reverse_db_file_name(dict_name)) { +ReverseDb::ReverseDb(const string& file_name) + : MappedFile(file_name) { } bool ReverseDb::Load() { @@ -200,10 +196,6 @@ ReverseLookupDictionary::ReverseLookupDictionary(an db) : db_(db) { } -ReverseLookupDictionary::ReverseLookupDictionary(const string& dict_name) - : db_(new ReverseDb(dict_name)) { -} - bool ReverseLookupDictionary::Load() { return db_ && (db_->IsOpen() || db_->Load()); } @@ -233,7 +225,13 @@ an ReverseLookupDictionary::GetDictSettings() { return settings; } -ReverseLookupDictionaryComponent::ReverseLookupDictionaryComponent() { +static const ResourceType kReverseDbResourceType = { + "reverse_db", "", ".reverse.bin" +}; + +ReverseLookupDictionaryComponent::ReverseLookupDictionaryComponent() + : resource_resolver_( + Service::instance().CreateResourceResolver(kReverseDbResourceType)) { } ReverseLookupDictionary* @@ -248,7 +246,8 @@ ReverseLookupDictionaryComponent::Create(const Ticket& ticket) { } auto db = db_pool_[dict_name].lock(); if (!db) { - db = New(dict_name); + auto file_path = resource_resolver_->ResolvePath(dict_name).string(); + db = New(file_path); db_pool_[dict_name] = db; } return new ReverseLookupDictionary(db); diff --git a/src/rime/dict/reverse_lookup_dictionary.h b/src/rime/dict/reverse_lookup_dictionary.h index 9efed2a01..8c328a972 100644 --- a/src/rime/dict/reverse_lookup_dictionary.h +++ b/src/rime/dict/reverse_lookup_dictionary.h @@ -38,7 +38,7 @@ class DictSettings; class ReverseDb : public MappedFile { public: - explicit ReverseDb(const string& dict_name); + explicit ReverseDb(const string& file_name); bool Load(); bool Lookup(const string& text, string* result); @@ -62,7 +62,6 @@ class ReverseLookupDictionary : public Class { public: explicit ReverseLookupDictionary(an db); - explicit ReverseLookupDictionary(const string& dict_name); bool Load(); bool ReverseLookup(const string& text, string* result); bool LookupStems(const string& text, string* result); @@ -72,6 +71,8 @@ class ReverseLookupDictionary an db_; }; +class ResourceResolver; + class ReverseLookupDictionaryComponent : public ReverseLookupDictionary::Component { public: @@ -79,6 +80,7 @@ class ReverseLookupDictionaryComponent ReverseLookupDictionary* Create(const Ticket& ticket); private: map> db_pool_; + the resource_resolver_; }; } // namespace rime