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 #ifdef USE_EXTERNAL_MYTHES
27 #include MYTHES_H_LOCATION
30 #include "support/mythes/mythes.hxx"
33 #include "frontends/alert.h"
40 using namespace lyx::support;
41 using namespace lyx::support::os;
47 typedef std::map<docstring, MyThes *> Thesauri;
51 struct Thesaurus::Private
55 for (Thesauri::iterator it = thes_.begin();
56 it != thes_.end(); ++it) {
61 bool thesaurusAvailable(docstring const & lang) const
63 for (Thesauri::const_iterator it = thes_.begin();
64 it != thes_.end(); ++it) {
65 if (it->first == lang)
73 typedef std::pair<std::string, std::string> ThesFiles;
75 ThesFiles getThesaurus(string const & path, docstring const & lang);
76 ThesFiles getThesaurus(docstring const & lang);
77 /// add a thesaurus to the list
78 bool addThesaurus(docstring const & lang);
83 /// the location below system/user directory
84 /// there the data+idx files lookup will happen
85 const string dataDirectory(void) { return "thes"; }
90 pair<string,string> Thesaurus::Private::getThesaurus(string const & path, docstring const & lang)
93 if (!base.isDirectory()) {
94 return make_pair(string(), string());
96 FileNameList const idx_files = base.dirList("idx");
97 FileNameList const data_files = base.dirList("dat");
102 LYXERR(Debug::FILES, "thesaurus path: " << path);
103 for (FileNameList::const_iterator it = idx_files.begin(); it != idx_files.end(); ++it) {
104 basename = it->onlyFileNameWithoutExt();
105 if (contains(basename, to_ascii(lang))) {
106 ifstream ifs(it->absFileName().c_str());
108 // check for appropriate version of index file
109 string encoding; // first line is encoding
110 int items = 0; // second line is no. of items
111 getline(ifs,encoding);
114 LYXERR(Debug::FILES, "ignore irregular thesaurus idx file: " << it->absFileName());
117 if (encoding.length() == 0 || encoding.find_first_of(',') != string::npos) {
118 LYXERR(Debug::FILES, "ignore version1 thesaurus idx file: " << it->absFileName());
122 idx = it->absFileName();
123 LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
128 return make_pair(string(), string());
130 for (support::FileNameList::const_iterator it = data_files.begin(); it != data_files.end(); ++it) {
131 if (contains(it->onlyFileName(), basename)) {
132 data = it->absFileName();
133 LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
137 return make_pair(idx, data);
141 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
143 string const thes_path = external_path(lyxrc.thesaurusdir_path);
144 pair<string,string> result ;
146 if (thesaurusAvailable(lang))
147 return make_pair(string(), string());
149 if (!thes_path.empty()) {
150 result = getThesaurus(thes_path, lang);
152 if (result.first.empty() || result.second.empty()) {
153 string const sys_path = external_path(addName(lyx::support::package().system_support().absFileName(),dataDirectory())) ;
154 result = getThesaurus(sys_path, lang);
156 if (result.first.empty() || result.second.empty()) {
157 string const user_path = external_path(addName(lyx::support::package().user_support().absFileName(),dataDirectory())) ;
158 result = getThesaurus(user_path, lang);
164 bool Thesaurus::Private::addThesaurus(docstring const & lang)
166 if (thesaurusAvailable(lang))
169 ThesFiles files = getThesaurus(lang);
170 string const idx = files.first;
171 string const data = files.second;
173 if (idx.empty() || data.empty())
176 char const * af = idx.c_str();
177 char const * df = data.c_str();
178 thes_[lang] = new MyThes(af, df);
183 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
185 return d->thesaurusAvailable(lang);
189 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
191 if (thesaurusAvailable(lang))
193 pair<string, string> files = d->getThesaurus(lang);
194 return (!files.first.empty() && !files.second.empty());
198 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
203 if (!d->addThesaurus(lang))
206 for (Thesauri::const_iterator it = d->thes_.begin();
207 it != d->thes_.end(); ++it) {
208 if (it->first == lang) {
217 string const encoding = mythes->get_th_encoding();
220 string const text = to_iconv_encoding(support::lowercase(t), encoding);
221 int len = strlen(text.c_str());
222 int count = mythes->Lookup(text.c_str(), len, &pmean);
226 // don't change value of pmean or count
227 // they are needed for the CleanUpAfterLookup routine
230 for (int i = 0; i < count; i++) {
231 vector<docstring> ret;
232 meaning = from_iconv_encoding(string(pm->defn), encoding);
234 if (support::prefixIs(meaning, '-'))
235 meaning = support::ltrim(meaning, "- ");
236 for (int j = 0; j < pm->count; j++) {
237 ret.push_back(from_iconv_encoding(string(pm->psyns[j]), encoding));
239 meanings[meaning] = ret;
242 // now clean up all allocated memory
243 mythes->CleanUpAfterLookup(&pmean, count);
245 for (Meanings::iterator it = meanings.begin();
246 it != meanings.end(); ++it)
247 sort(it->second.begin(), it->second.end());
253 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
258 Thesaurus::~Thesaurus()