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"
35 using namespace lyx::support;
36 using namespace lyx::support::os;
42 typedef std::map<docstring, MyThes *> Thesauri;
46 struct Thesaurus::Private
50 for (Thesauri::iterator it = thes_.begin();
51 it != thes_.end(); ++it) {
56 bool thesaurusAvailable(docstring const & lang) const
58 for (Thesauri::const_iterator it = thes_.begin();
59 it != thes_.end(); ++it) {
60 if (it->first == lang)
68 typedef std::pair<std::string, std::string> ThesFiles;
70 ThesFiles getThesaurus(string const & path, docstring const & lang);
71 ThesFiles getThesaurus(docstring const & lang);
72 /// add a thesaurus to the list
73 bool addThesaurus(docstring const & lang);
78 /// the location below system/user directory
79 /// there the data+idx files lookup will happen
80 const string dataDirectory(void) { return "thes"; }
85 pair<string,string> Thesaurus::Private::getThesaurus(string const & path, docstring const & lang)
88 if (!base.isDirectory()) {
89 return make_pair(string(), string());
91 FileNameList const idx_files = base.dirList("idx");
92 FileNameList const data_files = base.dirList("dat");
97 LYXERR(Debug::FILES, "thesaurus path: " << path);
98 for (FileNameList::const_iterator it = idx_files.begin(); it != idx_files.end(); ++it) {
99 basename = it->onlyFileNameWithoutExt();
100 if (contains(basename, to_ascii(lang))) {
101 ifstream ifs(it->absFileName().c_str());
105 if (s.find_first_of(',') != string::npos) {
106 LYXERR(Debug::FILES, "ignore version1 thesaurus idx file: " << it->absFileName());
110 idx = it->absFileName();
111 LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
116 return make_pair(string(), string());
118 for (support::FileNameList::const_iterator it = data_files.begin(); it != data_files.end(); ++it) {
119 if (contains(it->onlyFileName(), basename)) {
120 data = it->absFileName();
121 LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
125 return make_pair(idx, data);
129 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
131 string const thes_path = external_path(lyxrc.thesaurusdir_path);
132 pair<string,string> result ;
134 if (thesaurusAvailable(lang))
135 return make_pair(string(), string());
137 if (!thes_path.empty()) {
138 result = getThesaurus(thes_path, lang);
140 if (result.first.empty() || result.second.empty()) {
141 string const sys_path = external_path(addName(lyx::support::package().system_support().absFileName(),dataDirectory())) ;
142 result = getThesaurus(sys_path, lang);
144 if (result.first.empty() || result.second.empty()) {
145 string const user_path = external_path(addName(lyx::support::package().user_support().absFileName(),dataDirectory())) ;
146 result = getThesaurus(user_path, lang);
152 bool Thesaurus::Private::addThesaurus(docstring const & lang)
154 if (thesaurusAvailable(lang))
157 ThesFiles files = getThesaurus(lang);
158 string const idx = files.first;
159 string const data = files.second;
161 if (idx.empty() || data.empty())
164 char const * af = idx.c_str();
165 char const * df = data.c_str();
166 thes_[lang] = new MyThes(af, df);
171 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
173 return d->thesaurusAvailable(lang);
177 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
179 if (thesaurusAvailable(lang))
181 pair<string, string> files = d->getThesaurus(lang);
182 return (!files.first.empty() && !files.second.empty());
186 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
191 if (!d->addThesaurus(lang))
194 for (Thesauri::const_iterator it = d->thes_.begin();
195 it != d->thes_.end(); ++it) {
196 if (it->first == lang) {
205 string const encoding = mythes->get_th_encoding();
208 string const text = to_iconv_encoding(support::lowercase(t), encoding);
209 int len = strlen(text.c_str());
210 int count = mythes->Lookup(text.c_str(), len, &pmean);
214 // don't change value of pmean or count
215 // they are needed for the CleanUpAfterLookup routine
219 for (int i = 0; i < count; i++) {
220 meaning = from_iconv_encoding(string(pm->defn), encoding);
222 if (support::prefixIs(meaning, '-'))
223 meaning = support::ltrim(meaning, "- ");
224 for (int j = 0; j < pm->count; j++) {
225 ret = from_iconv_encoding(string(pm->psyns[j]), encoding);
227 meanings[meaning].push_back(ret);
230 // now clean up all allocated memory
231 mythes->CleanUpAfterLookup(&pmean, count);
233 for (Meanings::iterator it = meanings.begin();
234 it != meanings.end(); ++it)
235 sort(it->second.begin(), it->second.end());
241 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
246 Thesaurus::~Thesaurus()