Refactor thesaurus language listing a bit

This commit is contained in:
Thomas Goyne 2013-12-22 16:10:34 -08:00
parent 6a193f280e
commit 6957cd76a6
1 changed files with 32 additions and 46 deletions

View File

@ -27,11 +27,13 @@
#include <boost/format.hpp> #include <boost/format.hpp>
#include <boost/algorithm/string/case_conv.hpp> #include <boost/algorithm/string/case_conv.hpp>
#include <boost/range/algorithm.hpp>
#include <libaegisub/fs.h> #include <libaegisub/fs.h>
#include <libaegisub/log.h> #include <libaegisub/log.h>
#include <libaegisub/path.h> #include <libaegisub/path.h>
#include <libaegisub/thesaurus.h> #include <libaegisub/thesaurus.h>
#include <libaegisub/util.h>
Thesaurus::Thesaurus() Thesaurus::Thesaurus()
: lang_listener(OPT_SUB("Tool/Thesaurus/Language", &Thesaurus::OnLanguageChanged, this)) : lang_listener(OPT_SUB("Tool/Thesaurus/Language", &Thesaurus::OnLanguageChanged, this))
@ -50,70 +52,54 @@ std::vector<Thesaurus::Entry> Thesaurus::Lookup(std::string word) {
return impl->Lookup(word); return impl->Lookup(word);
} }
std::vector<std::string> Thesaurus::GetLanguageList() const { static std::vector<std::string> langs(const char *ext) {
if (!languages.empty()) return languages; std::vector<std::string> paths;
auto data_path = config::path->Decode("?data/dictionaries/");
auto user_path = config::path->Decode(OPT_GET("Path/Dictionary")->GetString());
std::vector<std::string> idx, dat; auto filter = std::string("th_*.") + ext;
agi::fs::DirectoryIterator(data_path, filter).GetAll(paths);
// Get list of dictionaries agi::fs::DirectoryIterator(user_path, filter).GetAll(paths);
auto path = config::path->Decode("?data/dictionaries/");
agi::fs::DirectoryIterator(path, "th_*.idx").GetAll(idx);
agi::fs::DirectoryIterator(path, "th_*.dat").GetAll(dat);
path = config::path->Decode(OPT_GET("Path/Dictionary")->GetString());
agi::fs::DirectoryIterator(path, "th_*.idx").GetAll(idx);
agi::fs::DirectoryIterator(path, "th_*.dat").GetAll(dat);
if (idx.empty() || dat.empty()) return languages;
sort(begin(idx), end(idx));
sort(begin(dat), end(dat));
// Drop extensions and the th_ prefix // Drop extensions and the th_ prefix
for (auto& fn : idx) fn = fn.substr(3, fn.size() - 7); for (auto& fn : paths) fn = fn.substr(3, fn.size() - filter.size() + 1);
for (auto& fn : dat) fn = fn.substr(3, fn.size() - 7);
// Verify that each idx has a dat boost::sort(paths);
for (size_t i = 0, j = 0; i < idx.size() && j < dat.size(); ) { paths.erase(unique(begin(paths), end(paths)), end(paths));
int cmp = idx[i].compare(dat[j]);
if (cmp < 0) ++i; return paths;
else if (cmp > 0) ++j; }
else {
// Don't insert a language twice if it's in both the user dir and std::vector<std::string> Thesaurus::GetLanguageList() const {
// the app's dir if (languages.empty())
if (languages.empty() || dat[j] != languages.back()) boost::set_intersection(langs("idx"), langs("dat"), back_inserter(languages));
languages.push_back(dat[j]);
++i;
++j;
}
}
return languages; return languages;
} }
static bool check_path(agi::fs::path const& path, std::string const& language, agi::fs::path& idx, agi::fs::path& dat) {
idx = path/str(boost::format("th_%s.idx") % language);
dat = path/str(boost::format("th_%s.dat") % language);
return agi::fs::FileExists(idx) && agi::fs::FileExists(dat);
}
void Thesaurus::OnLanguageChanged() { void Thesaurus::OnLanguageChanged() {
impl.reset(); impl.reset();
auto language = OPT_GET("Tool/Thesaurus/Language")->GetString(); auto language = OPT_GET("Tool/Thesaurus/Language")->GetString();
if (language.empty()) return; if (language.empty()) return;
agi::fs::path idx, dat;
auto path = config::path->Decode(OPT_GET("Path/Dictionary")->GetString() + "/"); auto path = config::path->Decode(OPT_GET("Path/Dictionary")->GetString() + "/");
if (!check_path(path, language, idx, dat)) {
// Get index and data paths
auto idxpath = path/str(boost::format("th_%s.idx") % language);
auto datpath = path/str(boost::format("th_%s.dat") % language);
// If they aren't in the user dictionary path, check the application directory
if (!agi::fs::FileExists(idxpath) || !agi::fs::FileExists(datpath)) {
path = config::path->Decode("?data/dictionaries/"); path = config::path->Decode("?data/dictionaries/");
idxpath = path/str(boost::format("th_%s.idx") % language); if (!check_path(path, language, idx, dat))
datpath = path/str(boost::format("th_%s.dat") % language); return;
if (!agi::fs::FileExists(idxpath) || !agi::fs::FileExists(datpath)) return;
} }
LOG_I("thesaurus/file") << "Using thesaurus: " << datpath; LOG_I("thesaurus/file") << "Using thesaurus: " << dat;
impl.reset(new agi::Thesaurus(datpath, idxpath)); impl = agi::util::make_unique<agi::Thesaurus>(dat, idx);
} }
void Thesaurus::OnPathChanged() { void Thesaurus::OnPathChanged() {