Skip to content

Commit 091cb9d

Browse files
committed
fix(simplifier): fix crash if no opencc file
1 parent 510f76c commit 091cb9d

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/rime/gear/simplifier.cc

+12-4
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@ class Opencc {
3434
Opencc(const string& config_path) {
3535
LOG(INFO) << "initilizing opencc: " << config_path;
3636
opencc::Config config;
37-
converter_ = config.NewFromFile(config_path);
38-
const list<opencc::ConversionPtr> conversions =
39-
converter_->GetConversionChain()->GetConversions();
40-
dict_ = conversions.front()->GetDict();
37+
try {
38+
converter_ = config.NewFromFile(config_path);
39+
const list<opencc::ConversionPtr> conversions =
40+
converter_->GetConversionChain()->GetConversions();
41+
dict_ = conversions.front()->GetDict();
42+
}
43+
catch (...) {
44+
LOG(ERROR) << "opencc config not found: " << config_path;
45+
}
4146
}
4247

4348
bool ConvertWord(const string& text,
4449
vector<string>* forms) {
50+
if (dict_ == nullptr) return false;
4551
opencc::Optional<const opencc::DictEntry*> item = dict_->Match(text);
4652
if (item.IsNull()) {
4753
// Match not found
@@ -57,6 +63,7 @@ class Opencc {
5763

5864
bool RandomConvertText(const string& text,
5965
string* simplified) {
66+
if (dict_ == nullptr) return false;
6067
const char *phrase = text.c_str();
6168
std::ostringstream buffer;
6269
for (const char* pstr = phrase; *pstr != '\0';) {
@@ -78,6 +85,7 @@ class Opencc {
7885

7986
bool ConvertText(const string& text,
8087
string* simplified) {
88+
if (converter_ == nullptr) return false;
8189
*simplified = converter_->Convert(text);
8290
return *simplified != text;
8391
}

0 commit comments

Comments
 (0)