]> git.lyx.org Git - lyx.git/blob - src/Thesaurus.cpp
Provide proper fallback if a bibliography processor is not found
[lyx.git] / src / Thesaurus.cpp
1 /**
2  * \file Thesaurus.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Thesaurus.h"
15
16 #include "LyXRC.h"
17
18 #include "SpellChecker.h"
19 #include "WordLangTuple.h"
20
21 #include "support/FileNameList.h"
22 #include "support/Package.h"
23 #include "support/debug.h"
24 #include "support/docstring_list.h"
25 #include "support/filetools.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28 #include "support/os.h"
29
30 #include <cstdio>
31 #include MYTHES_H_LOCATION
32
33 #include "frontends/alert.h"
34
35 #include <algorithm>
36 #include <cstring>
37 #include <fstream>
38
39 using namespace std;
40 using namespace lyx::support;
41 using namespace lyx::support::os;
42
43 namespace lyx {
44
45 namespace {
46
47 typedef std::map<docstring, MyThes *> Thesauri;
48
49 } // namespace
50
51 struct Thesaurus::Private
52 {
53         ~Private()
54         {
55                 for (Thesauri::iterator it = thes_.begin();
56                      it != thes_.end(); ++it) {
57                         delete it->second;
58                 }
59         }
60         ///
61         bool thesaurusAvailable(docstring const & lang) const
62         {
63                 for (Thesauri::const_iterator it = thes_.begin();
64                         it != thes_.end(); ++it) {
65                                 if (it->first == lang)
66                                         if (it->second)
67                                                 return true;
68                 }
69                 return false;
70         }
71
72         ///
73         typedef std::pair<std::string, std::string> ThesFiles;
74         ///
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);
79
80         /// the thesauri
81         Thesauri thes_;
82
83         /// the location below system/user directory
84         /// there the data+idx files lookup will happen
85         const string dataDirectory(void) { return "thes"; }
86
87 };
88
89
90 pair<string,string> Thesaurus::Private::getThesaurus(string const & path, docstring const & lang)
91 {
92         FileName base(path);
93         if (!base.isDirectory()) {
94                 return make_pair(string(), string());
95         }
96         FileNameList const idx_files = base.dirList("idx");
97         FileNameList const data_files = base.dirList("dat");
98         string idx;
99         string data;
100         string basename;
101
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                         // do not use more specific dicts.
107                         if (contains(basename, '_') && !contains(lang, '_'))
108                                 continue;
109                         if (contains(basename, '-') && !contains(lang, '-'))
110                                 continue;
111                         ifstream ifs(it->absFileName().c_str());
112                         if (ifs) {
113                                 // check for appropriate version of index file
114                                 string encoding; // first line is encoding
115                                 int items = 0;   // second line is no. of items
116                                 getline(ifs,encoding);
117                                 ifs >> items;
118                                 if (ifs.fail()) {
119                                         LYXERR(Debug::FILES, "ignore irregular thesaurus idx file: " << it->absFileName());
120                                         continue;
121                                 }
122                                 if (encoding.length() == 0 || encoding.find_first_of(',') != string::npos) {
123                                         LYXERR(Debug::FILES, "ignore version1 thesaurus idx file: " << it->absFileName());
124                                         continue;
125                                 }
126                         }
127                         idx = it->absFileName();
128                         LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
129                         break;
130                 }
131         }
132         if (idx.empty()) {
133                 // try with a more general dictionary
134                 docstring shortcode;
135                 if (contains(lang, '_')) {
136                         split(lang, shortcode, '_');
137                         LYXERR(Debug::FILES, "Did not find thesaurus for LANG code "
138                                << lang << ". Trying with " << shortcode);
139                         return getThesaurus(path, shortcode);
140                 }
141                 else if (contains(lang, '-')) {
142                         split(lang, shortcode, '-');
143                         LYXERR(Debug::FILES, "Did not find thesaurus for LANG code "
144                                << lang << ". Trying with " << shortcode);
145                         return getThesaurus(path, shortcode);
146                 }
147                 return make_pair(string(), string());
148         }
149         for (support::FileNameList::const_iterator it = data_files.begin(); it != data_files.end(); ++it) {
150                 if (contains(it->onlyFileName(), basename)) {
151                         data = it->absFileName();
152                         LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
153                         break;
154                 }
155         }
156         return make_pair(idx, data);
157 }
158
159
160 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
161 {
162         string const thes_path = external_path(lyxrc.thesaurusdir_path);
163         pair<string,string> result ;
164
165         if (thesaurusAvailable(lang))
166                 return make_pair(string(), string());
167
168         if (!thes_path.empty())
169                 result = getThesaurus(thes_path, lang);
170         if (result.first.empty() || result.second.empty()) {
171                 string const sys_path = external_path(addName(lyx::support::package().system_support().absFileName(),dataDirectory())) ;
172                 result = getThesaurus(sys_path, lang);
173         }
174         if (result.first.empty() || result.second.empty()) {
175                 string const user_path = external_path(addName(lyx::support::package().user_support().absFileName(),dataDirectory())) ;
176                 result = getThesaurus(user_path, lang);
177         }
178         return result;
179 }
180
181
182 bool Thesaurus::Private::addThesaurus(docstring const & lang)
183 {
184         if (thesaurusAvailable(lang))
185                 return true;
186
187         ThesFiles files = getThesaurus(lang);
188         string const idx = files.first;
189         string const data = files.second;
190
191         if (idx.empty() || data.empty())
192                 return false;
193
194         char const * af = idx.c_str();
195         char const * df = data.c_str();
196         thes_[lang] = new MyThes(af, df);
197         return true;
198 }
199
200
201 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
202 {
203         return d->thesaurusAvailable(lang);
204 }
205
206
207 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
208 {
209         if (thesaurusAvailable(lang))
210                 return true;
211         pair<string, string> files = d->getThesaurus(lang);
212         return (!files.first.empty() && !files.second.empty());
213 }
214
215
216 Thesaurus::Meanings Thesaurus::lookup(WordLangTuple const & wl)
217 {
218         Meanings meanings;
219         MyThes * mythes = 0;
220
221         docstring const lang_code = from_ascii(wl.lang()->code());
222         docstring const t = wl.word();
223
224         if (!d->addThesaurus(lang_code))
225                 return meanings;
226
227         for (Thesauri::const_iterator it = d->thes_.begin();
228              it != d->thes_.end(); ++it) {
229                 if (it->first == lang_code) {
230                         mythes = it->second;
231                         break;
232                 }
233         }
234
235         if (!mythes)
236                 return meanings;
237
238         string const encoding = mythes->get_th_encoding();
239
240         mentry * pmean;
241         string const text = to_iconv_encoding(support::lowercase(t), encoding);
242         int len = strlen(text.c_str());
243         int count = mythes->Lookup(text.c_str(), len, &pmean);
244         if (!count) {
245                 SpellChecker * speller = theSpellChecker();
246                 if (!speller)
247                         return meanings;
248                 docstring_list suggestions;
249                 speller->stem(wl, suggestions);
250                 for (size_t i = 0; i != suggestions.size(); ++i) {
251                         string const wordform = to_iconv_encoding(support::lowercase(suggestions[i]), encoding);
252                         len = strlen(wordform.c_str());
253                         count = mythes->Lookup(wordform.c_str(), len, &pmean);
254                         if (count)
255                                 break;
256                 }
257                 if (!count)
258                         return meanings;
259         }
260
261         // don't change value of pmean or count
262         // they are needed for the CleanUpAfterLookup routine
263         mentry * pm = pmean;
264         docstring meaning;
265         for (int i = 0; i < count; i++) {
266                 vector<docstring> ret;
267                 meaning = from_iconv_encoding(string(pm->defn), encoding);
268                 // remove silly item
269                 if (support::prefixIs(meaning, '-'))
270                         meaning = support::ltrim(meaning, "- ");
271                 for (int j = 0; j < pm->count; j++) {
272                         ret.push_back(from_iconv_encoding(string(pm->psyns[j]), encoding));
273                 }
274                 meanings[meaning] = ret;
275                 ++pm;
276         }
277         // now clean up all allocated memory
278         mythes->CleanUpAfterLookup(&pmean, count);
279
280         for (Meanings::iterator it = meanings.begin();
281              it != meanings.end(); ++it)
282                 sort(it->second.begin(), it->second.end());
283
284         return meanings;
285 }
286
287
288 Thesaurus::Thesaurus()
289         : d(new Thesaurus::Private)
290 {
291 }
292
293
294 Thesaurus::~Thesaurus()
295 {
296         delete d;
297 }
298
299 // Global instance
300 Thesaurus thesaurus;
301
302
303 } // namespace lyx