3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "Thesaurus.h"
18 #include "support/FileNameList.h"
19 #include "support/debug.h"
20 #include "support/filetools.h"
21 #include "support/gettext.h"
22 #include "support/lstrings.h"
23 #include "support/os.h"
25 #include "support/mythes/mythes.hxx"
27 #include "frontends/alert.h"
33 using namespace lyx::support;
34 using namespace lyx::support::os;
40 typedef std::map<docstring, MyThes *> Thesauri;
45 struct Thesaurus::Private
49 for (Thesauri::iterator it = thes_.begin();
50 it != thes_.end(); ++it) {
55 bool thesaurusAvailable(docstring const & lang) const
57 for (Thesauri::const_iterator it = thes_.begin();
58 it != thes_.end(); ++it) {
59 if (it->first == lang)
67 typedef std::pair<std::string, std::string> ThesFiles;
69 ThesFiles getThesaurus(docstring const & lang);
70 /// add a thesaurus to the list
71 bool addThesaurus(docstring const & lang);
78 pair<string, string> Thesaurus::Private::getThesaurus(docstring const & lang)
80 string const thes_path = external_path(lyxrc.thesaurusdir_path);
81 LYXERR(Debug::FILES, "thesaurus path: " << thes_path);
82 if (thes_path.empty())
83 return make_pair(string(), string());
85 if (thesaurusAvailable(lang))
86 return make_pair(string(), string());
88 FileNameList const idx_files = FileName(thes_path).dirList("idx");
89 FileNameList const data_files = FileName(thes_path).dirList("dat");
93 for (FileNameList::const_iterator it = idx_files.begin();
94 it != idx_files.end(); ++it) {
95 LYXERR(Debug::FILES, "found thesaurus idx file: " << it->onlyFileName());
96 if (contains(it->onlyFileName(), to_ascii(lang))) {
97 idx = it->absFilename();
98 LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
103 for (support::FileNameList::const_iterator it = data_files.begin();
104 it != data_files.end(); ++it) {
105 LYXERR(Debug::FILES, "found thesaurus data file: " << it->onlyFileName());
106 if (contains(it->onlyFileName(), to_ascii(lang))) {
107 data = it->absFilename();
108 LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
113 return make_pair(idx, data);
117 bool Thesaurus::Private::addThesaurus(docstring const & lang)
119 ThesFiles files = getThesaurus(lang);
120 string const idx = files.first;
121 string const data = files.second;
123 if (idx.empty() || data.empty())
126 char const * af = idx.c_str();
127 char const * df = data.c_str();
128 thes_[lang] = new MyThes(af, df);
133 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
135 return d->thesaurusAvailable(lang);
139 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
141 pair<string, string> files = d->getThesaurus(lang);
142 return (!files.first.empty() && !files.second.empty());
146 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
151 if (!d->addThesaurus(lang))
154 for (Thesauri::const_iterator it = d->thes_.begin();
155 it != d->thes_.end(); ++it) {
156 if (it->first == lang) {
165 string const encoding = mythes->get_th_encoding();
168 string const text = to_iconv_encoding(support::lowercase(t), encoding);
169 int len = strlen(text.c_str());
170 int count = mythes->Lookup(text.c_str(), len, &pmean);
174 // don't change value of pmean or count
175 // they are needed for the CleanUpAfterLookup routine
179 for (int i = 0; i < count; i++) {
180 meaning = from_iconv_encoding(string(pm->defn), encoding);
182 if (support::prefixIs(meaning, '-'))
183 meaning = support::ltrim(meaning, "- ");
184 for (int j = 0; j < pm->count; j++) {
185 ret = from_iconv_encoding(string(pm->psyns[j]), encoding);
187 meanings[meaning].push_back(ret);
190 // now clean up all allocated memory
191 mythes->CleanUpAfterLookup(&pmean, count);
193 for (Meanings::iterator it = meanings.begin();
194 it != meanings.end(); ++it)
195 sort(it->second.begin(), it->second.end());
201 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
206 Thesaurus::~Thesaurus()