From 091cb9dae305158725e0d33432a4f0cd72afbb37 Mon Sep 17 00:00:00 2001 From: osfans Date: Fri, 25 Aug 2017 10:07:47 +0800 Subject: [PATCH] fix(simplifier): fix crash if no opencc file --- src/rime/gear/simplifier.cc | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/rime/gear/simplifier.cc b/src/rime/gear/simplifier.cc index de9126cf8..eff827fe7 100644 --- a/src/rime/gear/simplifier.cc +++ b/src/rime/gear/simplifier.cc @@ -34,14 +34,20 @@ class Opencc { Opencc(const string& config_path) { LOG(INFO) << "initilizing opencc: " << config_path; opencc::Config config; - converter_ = config.NewFromFile(config_path); - const list conversions = - converter_->GetConversionChain()->GetConversions(); - dict_ = conversions.front()->GetDict(); + try { + converter_ = config.NewFromFile(config_path); + const list conversions = + converter_->GetConversionChain()->GetConversions(); + dict_ = conversions.front()->GetDict(); + } + catch (...) { + LOG(ERROR) << "opencc config not found: " << config_path; + } } bool ConvertWord(const string& text, vector* forms) { + if (dict_ == nullptr) return false; opencc::Optional item = dict_->Match(text); if (item.IsNull()) { // Match not found @@ -57,6 +63,7 @@ class Opencc { bool RandomConvertText(const string& text, string* simplified) { + if (dict_ == nullptr) return false; const char *phrase = text.c_str(); std::ostringstream buffer; for (const char* pstr = phrase; *pstr != '\0';) { @@ -78,6 +85,7 @@ class Opencc { bool ConvertText(const string& text, string* simplified) { + if (converter_ == nullptr) return false; *simplified = converter_->Convert(text); return *simplified != text; }