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"
24 #include "support/unicode.h"
26 #include "support/mythes/mythes.hxx"
28 #include "frontends/alert.h"
34 using namespace lyx::support;
35 using namespace lyx::support::os;
41 string const to_iconv_encoding(docstring const & s, string const & encoding)
43 std::vector<char> const encoded =
44 ucs4_to_eightbit(s.data(), s.length(), encoding);
45 return string(encoded.begin(), encoded.end());
49 docstring const from_iconv_encoding(string const & s, string const & encoding)
51 std::vector<char_type> const ucs4 =
52 eightbit_to_ucs4(s.data(), s.length(), encoding);
53 return docstring(ucs4.begin(), ucs4.end());
56 typedef std::map<docstring, MyThes *> Thesauri;
61 struct Thesaurus::Private
65 for (Thesauri::iterator it = thes_.begin();
66 it != thes_.end(); ++it) {
71 bool thesaurusAvailable(docstring const & lang) const
73 for (Thesauri::const_iterator it = thes_.begin();
74 it != thes_.end(); ++it) {
75 if (it->first == lang)
83 typedef std::pair<std::string, std::string> ThesFiles;
85 ThesFiles getThesaurus(docstring const & lang);
86 /// add a thesaurus to the list
87 bool addThesaurus(docstring const & lang);
94 pair<string, string> Thesaurus::Private::getThesaurus(docstring const & lang)
96 string const thes_path = external_path(lyxrc.thesaurusdir_path);
97 LYXERR(Debug::FILES, "thesaurus path: " << thes_path);
98 if (thes_path.empty())
99 return make_pair(string(), string());
101 if (thesaurusAvailable(lang))
102 return make_pair(string(), string());
104 FileNameList const idx_files = FileName(thes_path).dirList("idx");
105 FileNameList const data_files = FileName(thes_path).dirList("dat");
109 for (FileNameList::const_iterator it = idx_files.begin();
110 it != idx_files.end(); ++it) {
111 LYXERR(Debug::FILES, "found thesaurus idx file: " << it->onlyFileName());
112 if (contains(it->onlyFileName(), to_ascii(lang))) {
113 idx = it->absFilename();
114 LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
119 for (support::FileNameList::const_iterator it = data_files.begin();
120 it != data_files.end(); ++it) {
121 LYXERR(Debug::FILES, "found thesaurus data file: " << it->onlyFileName());
122 if (contains(it->onlyFileName(), to_ascii(lang))) {
123 data = it->absFilename();
124 LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
129 return make_pair(idx, data);
133 bool Thesaurus::Private::addThesaurus(docstring const & lang)
135 ThesFiles files = getThesaurus(lang);
136 string const idx = files.first;
137 string const data = files.second;
139 if (idx.empty() || data.empty())
142 char const * af = idx.c_str();
143 char const * df = data.c_str();
144 thes_[lang] = new MyThes(af, df);
149 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
151 return d->thesaurusAvailable(lang);
155 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
157 pair<string, string> files = d->getThesaurus(lang);
158 return (!files.first.empty() && !files.second.empty());
162 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
167 if (!d->addThesaurus(lang))
170 for (Thesauri::const_iterator it = d->thes_.begin();
171 it != d->thes_.end(); ++it) {
172 if (it->first == lang) {
181 string const encoding = mythes->get_th_encoding();
184 string const text = to_iconv_encoding(support::lowercase(t), encoding);
185 int len = strlen(text.c_str());
186 int count = mythes->Lookup(text.c_str(), len, &pmean);
190 // don't change value of pmean or count
191 // they are needed for the CleanUpAfterLookup routine
195 for (int i = 0; i < count; i++) {
196 meaning = from_iconv_encoding(string(pm->defn), encoding);
198 if (support::prefixIs(meaning, '-'))
199 meaning = support::ltrim(meaning, "- ");
200 for (int j = 0; j < pm->count; j++) {
201 ret = from_iconv_encoding(string(pm->psyns[j]), encoding);
203 meanings[meaning].push_back(ret);
206 // now clean up all allocated memory
207 mythes->CleanUpAfterLookup(&pmean, count);
209 for (Meanings::iterator it = meanings.begin();
210 it != meanings.end(); ++it)
211 sort(it->second.begin(), it->second.end());
217 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
222 Thesaurus::~Thesaurus()