fix portability issue in PR #20 , plus simplify code

This commit is contained in:
Evgeniy A. Dushistov
2017-07-07 00:19:50 +03:00
parent f510300f59
commit 214fbbf91e

View File

@@ -811,7 +811,8 @@ namespace {
} }
} }
bool SynFile::load(const std::string& url, gulong wc) { bool SynFile::load(const std::string &url, gulong wc)
{
struct stat stat_buf; struct stat stat_buf;
if (!stat(url.c_str(), &stat_buf)) { if (!stat(url.c_str(), &stat_buf)) {
MapFile syn; MapFile syn;
@@ -822,12 +823,10 @@ bool SynFile::load(const std::string& url, gulong wc) {
// each entry in a syn-file is: // each entry in a syn-file is:
// - 0-terminated string // - 0-terminated string
// 4-byte index into .dict file in network byte order // 4-byte index into .dict file in network byte order
gchar *lower_string = g_utf8_casefold(current, -1); glib::CharStr lower_string{g_utf8_casefold(current, -1)};
std::string synonym(lower_string); std::string synonym{get_impl(lower_string)};
g_free(lower_string);
current += synonym.length() + 1; current += synonym.length() + 1;
unsigned int idx = * reinterpret_cast<const unsigned int*>(current); const guint32 idx = g_ntohl(get_uint32(current));
idx = g_ntohl(idx);
current += sizeof(idx); current += sizeof(idx);
synonyms[synonym] = idx; synonyms[synonym] = idx;
} }
@@ -837,23 +836,19 @@ bool SynFile::load(const std::string& url, gulong wc) {
} }
} }
bool SynFile::lookup(const char *str, glong &idx) { bool SynFile::lookup(const char *str, glong &idx)
gchar *lower_string = g_utf8_casefold(str, -1); {
auto it = synonyms.find(lower_string); glib::CharStr lower_string{g_utf8_casefold(str, -1)};
auto it = synonyms.find(get_impl(lower_string));
if (it != synonyms.end()) { if (it != synonyms.end()) {
g_free(lower_string);
idx = it->second; idx = it->second;
return true; return true;
} }
g_free(lower_string);
return false; return false;
} }
bool Dict::Lookup(const char *str, glong &idx) { bool Dict::Lookup(const char *str, glong &idx) {
if(syn_file->lookup(str, idx)) { return syn_file->lookup(str, idx) || idx_file->lookup(str, idx);
return true;
}
return idx_file->lookup(str, idx);
} }
bool Dict::load(const std::string& ifofilename) bool Dict::load(const std::string& ifofilename)