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/Package.h"
20 #include "support/debug.h"
21 #include "support/filetools.h"
22 #include "support/gettext.h"
23 #include "support/lstrings.h"
24 #include "support/os.h"
26 #include "support/mythes/mythes.hxx"
28 #include "frontends/alert.h"
34 using namespace lyx::support;
35 using namespace lyx::support::os;
41 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(string const & path, docstring const & lang);
70 ThesFiles getThesaurus(docstring const & lang);
71 /// add a thesaurus to the list
72 bool addThesaurus(docstring const & lang);
77 /// the location below system/user directory
78 /// there the data+idx files lookup will happen
79 const string dataDirectory(void) { return "thes"; }
84 pair<string,string> Thesaurus::Private::getThesaurus(string const & path, docstring const & lang)
87 if (!base.isDirectory()) {
88 return make_pair(string(), string());
90 FileNameList const idx_files = base.dirList("idx");
91 FileNameList const data_files = base.dirList("dat");
95 LYXERR(Debug::FILES, "thesaurus path: " << path);
96 for (FileNameList::const_iterator it = idx_files.begin(); it != idx_files.end(); ++it) {
97 if (contains(it->onlyFileName(), to_ascii(lang))) {
98 idx = it->absFileName();
99 LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
104 return make_pair(string(), string());
106 for (support::FileNameList::const_iterator it = data_files.begin(); it != data_files.end(); ++it) {
107 if (contains(it->onlyFileName(), to_ascii(lang))) {
108 data = it->absFileName();
109 LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
113 return make_pair(idx, data);
117 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
119 string const thes_path = external_path(lyxrc.thesaurusdir_path);
120 pair<string,string> result ;
122 if (thesaurusAvailable(lang))
123 return make_pair(string(), string());
125 if (!thes_path.empty()) {
126 result = getThesaurus(thes_path, lang);
128 if (result.first.empty() || result.second.empty()) {
129 string const sys_path = external_path(addName(lyx::support::package().system_support().absFileName(),dataDirectory())) ;
130 result = getThesaurus(sys_path, lang);
132 if (result.first.empty() || result.second.empty()) {
133 string const user_path = external_path(addName(lyx::support::package().user_support().absFileName(),dataDirectory())) ;
134 result = getThesaurus(user_path, lang);
140 bool Thesaurus::Private::addThesaurus(docstring const & lang)
142 if (thesaurusAvailable(lang))
145 ThesFiles files = getThesaurus(lang);
146 string const idx = files.first;
147 string const data = files.second;
149 if (idx.empty() || data.empty())
152 char const * af = idx.c_str();
153 char const * df = data.c_str();
154 thes_[lang] = new MyThes(af, df);
159 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
161 return d->thesaurusAvailable(lang);
165 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
167 pair<string, string> files = d->getThesaurus(lang);
168 return (!files.first.empty() && !files.second.empty());
172 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
177 if (!d->addThesaurus(lang))
180 for (Thesauri::const_iterator it = d->thes_.begin();
181 it != d->thes_.end(); ++it) {
182 if (it->first == lang) {
191 string const encoding = mythes->get_th_encoding();
194 string const text = to_iconv_encoding(support::lowercase(t), encoding);
195 int len = strlen(text.c_str());
196 int count = mythes->Lookup(text.c_str(), len, &pmean);
200 // don't change value of pmean or count
201 // they are needed for the CleanUpAfterLookup routine
205 for (int i = 0; i < count; i++) {
206 meaning = from_iconv_encoding(string(pm->defn), encoding);
208 if (support::prefixIs(meaning, '-'))
209 meaning = support::ltrim(meaning, "- ");
210 for (int j = 0; j < pm->count; j++) {
211 ret = from_iconv_encoding(string(pm->psyns[j]), encoding);
213 meanings[meaning].push_back(ret);
216 // now clean up all allocated memory
217 mythes->CleanUpAfterLookup(&pmean, count);
219 for (Meanings::iterator it = meanings.begin();
220 it != meanings.end(); ++it)
221 sort(it->second.begin(), it->second.end());
227 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
232 Thesaurus::~Thesaurus()