2012-10-30 16:59:47 +01:00
|
|
|
// Copyright (c) 2012, Thomas Goyne <plorkyeran@aegisub.org>
|
2006-12-25 22:56:56 +01:00
|
|
|
//
|
2012-10-30 16:59:47 +01:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2006-12-25 22:56:56 +01:00
|
|
|
//
|
2012-10-30 16:59:47 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2006-12-25 22:56:56 +01:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file thesaurus.cpp
|
2012-01-08 02:36:50 +01:00
|
|
|
/// @brief Thesaurus implementation
|
2009-07-29 07:43:02 +02:00
|
|
|
/// @ingroup thesaurus
|
|
|
|
///
|
2006-12-25 22:56:56 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-01-08 02:36:50 +01:00
|
|
|
#include "thesaurus.h"
|
2006-12-25 22:56:56 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include "options.h"
|
|
|
|
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
#include <boost/algorithm/string/case_conv.hpp>
|
2006-12-25 22:56:56 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs.h>
|
2012-01-08 02:36:50 +01:00
|
|
|
#include <libaegisub/log.h>
|
2013-01-30 04:35:37 +01:00
|
|
|
#include <libaegisub/path.h>
|
2012-01-08 02:36:50 +01:00
|
|
|
#include <libaegisub/thesaurus.h>
|
|
|
|
|
|
|
|
Thesaurus::Thesaurus()
|
|
|
|
: lang_listener(OPT_SUB("Tool/Thesaurus/Language", &Thesaurus::OnLanguageChanged, this))
|
|
|
|
, dict_path_listener(OPT_SUB("Path/Dictionary", &Thesaurus::OnPathChanged, this))
|
|
|
|
{
|
|
|
|
OnLanguageChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
Thesaurus::~Thesaurus() {
|
|
|
|
// Explicit empty destructor needed for scoped_ptr with incomplete types
|
|
|
|
}
|
|
|
|
|
2012-10-30 16:59:47 +01:00
|
|
|
void Thesaurus::Lookup(std::string word, std::vector<Entry> *result) {
|
2012-01-08 02:36:50 +01:00
|
|
|
if (!impl.get()) return;
|
2013-01-04 16:01:50 +01:00
|
|
|
boost::to_lower(word);
|
2012-10-30 16:59:47 +01:00
|
|
|
impl->Lookup(word, result);
|
2012-01-08 02:36:50 +01:00
|
|
|
}
|
|
|
|
|
2012-10-30 16:59:47 +01:00
|
|
|
std::vector<std::string> Thesaurus::GetLanguageList() const {
|
2012-01-08 02:36:50 +01:00
|
|
|
if (!languages.empty()) return languages;
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::vector<std::string> idx, dat;
|
2006-12-25 22:56:56 +01:00
|
|
|
|
2012-01-08 02:36:50 +01:00
|
|
|
// Get list of dictionaries
|
2013-01-30 04:35:37 +01:00
|
|
|
auto path = config::path->Decode("?data/dictionaries/");
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::DirectoryIterator(path, "th_*.idx").GetAll(idx);
|
|
|
|
agi::fs::DirectoryIterator(path, "th_*.dat").GetAll(dat);
|
|
|
|
|
2013-01-30 04:35:37 +01:00
|
|
|
path = config::path->Decode(OPT_GET("Path/Dictionary")->GetString());
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::DirectoryIterator(path, "th_*.idx").GetAll(idx);
|
|
|
|
agi::fs::DirectoryIterator(path, "th_*.dat").GetAll(dat);
|
|
|
|
|
2012-01-08 02:36:50 +01:00
|
|
|
if (idx.empty() || dat.empty()) return languages;
|
2006-12-25 22:56:56 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
sort(begin(idx), end(idx));
|
|
|
|
sort(begin(dat), end(dat));
|
2012-01-08 02:36:50 +01:00
|
|
|
|
|
|
|
// Drop extensions and the th_ prefix
|
2013-01-04 16:01:50 +01:00
|
|
|
for (auto& fn : idx) fn = fn.substr(3, fn.size() - 7);
|
|
|
|
for (auto& fn : dat) fn = fn.substr(3, fn.size() - 7);
|
2012-01-08 02:36:50 +01:00
|
|
|
|
|
|
|
// Verify that each idx has a dat
|
|
|
|
for (size_t i = 0, j = 0; i < idx.size() && j < dat.size(); ) {
|
2013-01-04 16:01:50 +01:00
|
|
|
int cmp = idx[i].compare(dat[j]);
|
2012-01-08 02:36:50 +01:00
|
|
|
if (cmp < 0) ++i;
|
|
|
|
else if (cmp > 0) ++j;
|
|
|
|
else {
|
|
|
|
// Don't insert a language twice if it's in both the user dir and
|
|
|
|
// the app's dir
|
2013-01-04 16:01:50 +01:00
|
|
|
if (languages.empty() || dat[j] != languages.back())
|
|
|
|
languages.push_back(dat[j]);
|
2012-01-08 02:36:50 +01:00
|
|
|
++i;
|
|
|
|
++j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return languages;
|
2006-12-25 22:56:56 +01:00
|
|
|
}
|
2009-07-29 07:43:02 +02:00
|
|
|
|
2012-01-08 02:36:50 +01:00
|
|
|
void Thesaurus::OnLanguageChanged() {
|
|
|
|
impl.reset();
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
auto language = OPT_GET("Tool/Thesaurus/Language")->GetString();
|
2012-01-08 02:36:50 +01:00
|
|
|
if (language.empty()) return;
|
|
|
|
|
2013-01-30 04:35:37 +01:00
|
|
|
auto path = config::path->Decode(OPT_GET("Path/Dictionary")->GetString() + "/");
|
2012-01-08 02:36:50 +01:00
|
|
|
|
|
|
|
// Get index and data paths
|
2013-01-04 16:01:50 +01:00
|
|
|
auto idxpath = path/str(boost::format("th_%s.idx") % language);
|
|
|
|
auto datpath = path/str(boost::format("th_%s.dat") % language);
|
2012-01-08 02:36:50 +01:00
|
|
|
|
|
|
|
// If they aren't in the user dictionary path, check the application directory
|
2013-01-04 16:01:50 +01:00
|
|
|
if (!agi::fs::FileExists(idxpath) || !agi::fs::FileExists(datpath)) {
|
2013-01-30 04:35:37 +01:00
|
|
|
path = config::path->Decode("?data/dictionaries/");
|
2013-01-04 16:01:50 +01:00
|
|
|
idxpath = path/str(boost::format("th_%s.idx") % language);
|
|
|
|
datpath = path/str(boost::format("th_%s.dat") % language);
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
if (!agi::fs::FileExists(idxpath) || !agi::fs::FileExists(datpath)) return;
|
2012-01-08 02:36:50 +01:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
LOG_I("thesaurus/file") << "Using thesaurus: " << datpath;
|
2012-01-08 02:36:50 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
impl.reset(new agi::Thesaurus(datpath, idxpath));
|
2012-01-08 02:36:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Thesaurus::OnPathChanged() {
|
|
|
|
languages.clear();
|
|
|
|
}
|