Skip to content

Commit 8ea08b3

Browse files
committed
feat(dict): use resource resolver to find dictionary files
1 parent fdf8172 commit 8ea08b3

6 files changed

+63
-36
lines changed

src/rime/dict/db.cc

+10-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <boost/algorithm/string.hpp>
88
#include <boost/filesystem.hpp>
99
#include <rime/common.h>
10+
#include <rime/resource.h>
1011
#include <rime/service.h>
1112
#include <rime/dict/db.h>
1213

@@ -20,15 +21,17 @@ bool DbAccessor::MatchesPrefix(const string& key) {
2021

2122
// Db members
2223

24+
static const ResourceType kDbResourceType = {
25+
"db", "", ""
26+
};
27+
2328
Db::Db(const string& name) : name_(name) {
24-
boost::filesystem::path path(name);
25-
if (path.has_parent_path()) {
26-
file_name_ = name;
27-
}
28-
else {
29-
boost::filesystem::path dir(Service::instance().deployer().user_data_dir);
30-
file_name_ = (dir / path).string();
29+
static ResourceResolver db_resource_resolver(kDbResourceType);
30+
if (db_resource_resolver.root_path().empty()) {
31+
db_resource_resolver.set_root_path(
32+
Service::instance().deployer().user_data_dir);
3133
}
34+
file_name_ = db_resource_resolver.ResolvePath(name).string();
3235
}
3336

3437
bool Db::Exists() const {

src/rime/dict/dictionary.cc

+21-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <utility>
88
#include <boost/filesystem.hpp>
99
#include <rime/common.h>
10+
#include <rime/resource.h>
1011
#include <rime/schema.h>
1112
#include <rime/service.h>
1213
#include <rime/ticket.h>
@@ -279,7 +280,22 @@ bool Dictionary::loaded() const {
279280

280281
// DictionaryComponent members
281282

282-
DictionaryComponent::DictionaryComponent() {
283+
static const ResourceType kPrismResourceType = {
284+
"prism", "", ".prism.bin"
285+
};
286+
287+
static const ResourceType kTableResourceType = {
288+
"table", "", ".table.bin"
289+
};
290+
291+
DictionaryComponent::DictionaryComponent()
292+
: prism_resource_resolver_(
293+
Service::instance().CreateResourceResolver(kPrismResourceType)),
294+
table_resource_resolver_(
295+
Service::instance().CreateResourceResolver(kTableResourceType)) {
296+
}
297+
298+
DictionaryComponent::~DictionaryComponent() {
283299
}
284300

285301
Dictionary* DictionaryComponent::Create(const Ticket& ticket) {
@@ -308,13 +324,13 @@ DictionaryComponent::CreateDictionaryWithName(const string& dict_name,
308324
boost::filesystem::path path(Service::instance().deployer().user_data_dir);
309325
auto table = table_map_[dict_name].lock();
310326
if (!table) {
311-
table = New<Table>((path / dict_name).string() + ".table.bin");
312-
table_map_[dict_name] = table;
327+
auto file_path = table_resource_resolver_->ResolvePath(dict_name).string();
328+
table_map_[dict_name] = table = New<Table>(file_path);
313329
}
314330
auto prism = prism_map_[prism_name].lock();
315331
if (!prism) {
316-
prism = New<Prism>((path / prism_name).string() + ".prism.bin");
317-
prism_map_[prism_name] = prism;
332+
auto file_path = prism_resource_resolver_->ResolvePath(prism_name).string();
333+
prism_map_[prism_name] = prism = New<Prism>(file_path);
318334
}
319335
return new Dictionary(dict_name, table, prism);
320336
}

src/rime/dict/dictionary.h

+5
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,21 @@ class Dictionary : public Class<Dictionary, const Ticket&> {
108108
an<Prism> prism_;
109109
};
110110

111+
class ResourceResolver;
112+
111113
class DictionaryComponent : public Dictionary::Component {
112114
public:
113115
DictionaryComponent();
116+
~DictionaryComponent();
114117
Dictionary* Create(const Ticket& ticket);
115118
Dictionary* CreateDictionaryWithName(const string& dict_name,
116119
const string& prism_name);
117120

118121
private:
119122
map<string, weak<Prism>> prism_map_;
120123
map<string, weak<Table>> table_map_;
124+
the<ResourceResolver> prism_resource_resolver_;
125+
the<ResourceResolver> table_resource_resolver_;
121126
};
122127

123128
} // namespace rime

src/rime/dict/preset_vocabulary.cc

+11-9
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,27 @@
77
#include <boost/filesystem.hpp>
88
#include <boost/lexical_cast.hpp>
99
#include <utf8.h>
10+
#include <rime/resource.h>
1011
#include <rime/service.h>
1112
#include <rime/dict/preset_vocabulary.h>
1213
#include <rime/dict/text_db.h>
1314

1415
namespace rime {
1516

17+
static const ResourceType kVocabularyResourceType = {
18+
"vocabulary", "", ".txt"
19+
};
20+
21+
static const string kDefaultVocabulary = "essay";
22+
1623
struct VocabularyDb : public TextDb {
1724
explicit VocabularyDb(const string& path);
1825
an<DbAccessor> cursor;
1926
static const TextFormat format;
2027
};
2128

2229
VocabularyDb::VocabularyDb(const string& path)
23-
: TextDb(path, "vocabulary", VocabularyDb::format) {
30+
: TextDb(path, kVocabularyResourceType.name, VocabularyDb::format) {
2431
}
2532

2633
static bool rime_vocabulary_entry_parser(const Tsv& row,
@@ -50,14 +57,9 @@ const TextFormat VocabularyDb::format = {
5057
};
5158

5259
string PresetVocabulary::DictFilePath() {
53-
auto& deployer(Service::instance().deployer());
54-
boost::filesystem::path path(deployer.user_data_dir);
55-
path /= "essay.txt";
56-
if (!boost::filesystem::exists(path)) {
57-
path = deployer.shared_data_dir;
58-
path /= "essay.txt";
59-
}
60-
return path.string();
60+
the<ResourceResolver> resource_resolver(
61+
Service::instance().CreateResourceResolver(kVocabularyResourceType));
62+
return resource_resolver->ResolvePath(kDefaultVocabulary).string();
6163
}
6264

6365
PresetVocabulary::PresetVocabulary() {

src/rime/dict/reverse_lookup_dictionary.cc

+12-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <boost/algorithm/string.hpp>
1010
#include <boost/filesystem.hpp>
1111
#include <boost/lexical_cast.hpp>
12+
#include <rime/resource.h>
1213
#include <rime/schema.h>
1314
#include <rime/service.h>
1415
#include <rime/ticket.h>
@@ -24,13 +25,8 @@ const size_t kReverseFormatPrefixLen = sizeof(kReverseFormatPrefix) - 1;
2425

2526
static const char* kStemKeySuffix = "\x1fstem";
2627

27-
static string reverse_db_file_name(const string& dict_name) {
28-
boost::filesystem::path dir(Service::instance().deployer().user_data_dir);
29-
return (dir / dict_name).string() + ".reverse.bin";
30-
}
31-
32-
ReverseDb::ReverseDb(const string& dict_name)
33-
: MappedFile(reverse_db_file_name(dict_name)) {
28+
ReverseDb::ReverseDb(const string& file_name)
29+
: MappedFile(file_name) {
3430
}
3531

3632
bool ReverseDb::Load() {
@@ -200,10 +196,6 @@ ReverseLookupDictionary::ReverseLookupDictionary(an<ReverseDb> db)
200196
: db_(db) {
201197
}
202198

203-
ReverseLookupDictionary::ReverseLookupDictionary(const string& dict_name)
204-
: db_(new ReverseDb(dict_name)) {
205-
}
206-
207199
bool ReverseLookupDictionary::Load() {
208200
return db_ && (db_->IsOpen() || db_->Load());
209201
}
@@ -233,7 +225,13 @@ an<DictSettings> ReverseLookupDictionary::GetDictSettings() {
233225
return settings;
234226
}
235227

236-
ReverseLookupDictionaryComponent::ReverseLookupDictionaryComponent() {
228+
static const ResourceType kReverseDbResourceType = {
229+
"reverse_db", "", ".reverse.bin"
230+
};
231+
232+
ReverseLookupDictionaryComponent::ReverseLookupDictionaryComponent()
233+
: resource_resolver_(
234+
Service::instance().CreateResourceResolver(kReverseDbResourceType)) {
237235
}
238236

239237
ReverseLookupDictionary*
@@ -248,7 +246,8 @@ ReverseLookupDictionaryComponent::Create(const Ticket& ticket) {
248246
}
249247
auto db = db_pool_[dict_name].lock();
250248
if (!db) {
251-
db = New<ReverseDb>(dict_name);
249+
auto file_path = resource_resolver_->ResolvePath(dict_name).string();
250+
db = New<ReverseDb>(file_path);
252251
db_pool_[dict_name] = db;
253252
}
254253
return new ReverseLookupDictionary(db);

src/rime/dict/reverse_lookup_dictionary.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class DictSettings;
3838

3939
class ReverseDb : public MappedFile {
4040
public:
41-
explicit ReverseDb(const string& dict_name);
41+
explicit ReverseDb(const string& file_name);
4242

4343
bool Load();
4444
bool Lookup(const string& text, string* result);
@@ -62,7 +62,6 @@ class ReverseLookupDictionary
6262
: public Class<ReverseLookupDictionary, const Ticket&> {
6363
public:
6464
explicit ReverseLookupDictionary(an<ReverseDb> db);
65-
explicit ReverseLookupDictionary(const string& dict_name);
6665
bool Load();
6766
bool ReverseLookup(const string& text, string* result);
6867
bool LookupStems(const string& text, string* result);
@@ -72,13 +71,16 @@ class ReverseLookupDictionary
7271
an<ReverseDb> db_;
7372
};
7473

74+
class ResourceResolver;
75+
7576
class ReverseLookupDictionaryComponent
7677
: public ReverseLookupDictionary::Component {
7778
public:
7879
ReverseLookupDictionaryComponent();
7980
ReverseLookupDictionary* Create(const Ticket& ticket);
8081
private:
8182
map<string, weak<ReverseDb>> db_pool_;
83+
the<ResourceResolver> resource_resolver_;
8284
};
8385

8486
} // namespace rime

0 commit comments

Comments
 (0)