]> git.lyx.org Git - features.git/blob - src/Thesaurus.cpp
Better compil fix than previous commit. Sorry...
[features.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 "support/debug.h"
17 #include "support/gettext.h"
18 #include "LyXRC.h"
19
20 #include "support/FileNameList.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23 #include "support/os.h"
24 #include "support/unicode.h"
25
26 #include "frontends/alert.h"
27
28 #ifdef HAVE_LIBMYTHES
29 #include MYTHES_H_LOCATION
30 #else
31 #ifdef HAVE_LIBAIKSAURUS
32 #include AIKSAURUS_H_LOCATION
33 #endif // HAVE_LIBAIKSAURUS
34 #endif // !HAVE_LIBMYTHES
35
36 #include <algorithm>
37 #include <cstring>
38
39 using namespace std;
40 using namespace lyx::support;
41 using namespace lyx::support::os;
42
43 namespace lyx {
44
45 #ifdef HAVE_LIBAIKSAURUS
46
47 struct Thesaurus::Private
48 {
49         Private(): thes_(new Aiksaurus) {}
50         Aiksaurus * thes_;
51 };
52
53 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const &)
54 {
55         Meanings meanings;
56
57         // aiksaurus is for english text only, therefore it does not work
58         // with non-ascii strings.
59         // The interface of the Thesaurus class uses docstring because a
60         // non-english thesaurus is possible in theory.
61         if (!support::isAscii(t))
62                 // to_ascii() would assert
63                 return meanings;
64
65         string const text = to_ascii(t);
66
67         docstring error = from_ascii(d->thes_->error());
68         if (!error.empty()) {
69                 static bool sent_error = false;
70                 if (!sent_error) {
71                         frontend::Alert::error(_("Thesaurus failure"),
72                                      bformat(_("Aiksaurus returned the following error:\n\n%1$s."),
73                                              error));
74                         sent_error = true;
75                 }
76                 return meanings;
77         }
78         if (!d->thes_->find(text.c_str()))
79                 return meanings;
80
81         // weird api, but ...
82
83         int prev_meaning = -1;
84         int cur_meaning;
85         docstring meaning;
86
87         // correct, returns "" at the end
88         string ret = d->thes_->next(cur_meaning);
89
90         while (!ret.empty()) {
91                 if (cur_meaning != prev_meaning) {
92                         meaning = from_ascii(ret);
93                         ret = d->thes_->next(cur_meaning);
94                         prev_meaning = cur_meaning;
95                 } else {
96                         if (ret != text)
97                                 meanings[meaning].push_back(from_ascii(ret));
98                 }
99
100                 ret = d->thes_->next(cur_meaning);
101         }
102
103         for (Meanings::iterator it = meanings.begin();
104              it != meanings.end(); ++it)
105                 sort(it->second.begin(), it->second.end());
106
107         return meanings;
108 }
109
110
111 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
112 {
113         // we support English only
114         return prefixIs(lang, from_ascii("en_"));
115 }
116
117 #else // HAVE_LIBAIKSAURUS
118 #ifdef HAVE_LIBMYTHES
119
120 namespace {
121
122 string const to_iconv_encoding(docstring const & s, string const & encoding)
123 {
124         std::vector<char> const encoded =
125                 ucs4_to_eightbit(s.data(), s.length(), encoding);
126         return string(encoded.begin(), encoded.end());
127 }
128
129
130 docstring const from_iconv_encoding(string const & s, string const & encoding)
131 {
132         std::vector<char_type> const ucs4 =
133                 eightbit_to_ucs4(s.data(), s.length(), encoding);
134         return docstring(ucs4.begin(), ucs4.end());
135 }
136
137 typedef std::map<docstring, MyThes *> Thesauri;
138
139 } // namespace anon
140
141
142 struct Thesaurus::Private
143 {
144         ~Private()
145         {
146                 for (Thesauri::iterator it = thes_.begin();
147                      it != thes_.end(); ++it) {
148                         delete it->second;
149                 }
150         }
151         ///
152         bool thesaurusAvailable(docstring const & lang) const
153         {
154                 for (Thesauri::const_iterator it = thes_.begin();
155                         it != thes_.end(); ++it) {
156                                 if (it->first == lang)
157                                         if (it->second)
158                                                 return true;
159                 }
160                 return false;
161         }
162
163         /// add a thesaurus to the list
164         bool addThesaurus(docstring const & lang);
165
166         /// the thesauri
167         Thesauri thes_;
168 };
169
170 bool Thesaurus::Private::addThesaurus(docstring const & lang)
171 {
172         string const thes_path = external_path(lyxrc.thesaurusdir_path);
173         LYXERR(Debug::FILES, "thesaurus path: " << thes_path);
174         if (thes_path.empty())
175                 return false;
176
177         if (thesaurusAvailable(lang))
178                 return true;
179
180         FileNameList const idx_files = FileName(thes_path).dirList("idx");
181         FileNameList const data_files = FileName(thes_path).dirList("dat");
182         string idx;
183         string data;
184
185         for (FileNameList::const_iterator it = idx_files.begin();
186              it != idx_files.end(); ++it) {
187                 LYXERR(Debug::FILES, "found thesaurus idx file: " << it->onlyFileName());
188                 if (contains(it->onlyFileName(), to_ascii(lang))) {
189                         idx = it->absFilename();
190                         LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
191                         break;
192                         }
193                 }
194
195         for (support::FileNameList::const_iterator it = data_files.begin();
196              it != data_files.end(); ++it) {
197                 LYXERR(Debug::FILES, "found thesaurus data file: " << it->onlyFileName());
198                 if (contains(it->onlyFileName(), to_ascii(lang))) {
199                         data = it->absFilename();
200                         LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
201                         break;
202                         }
203                 }
204
205         if (idx.empty() || data.empty())
206                 return false;
207
208         char const * af = idx.c_str();
209         char const * df = data.c_str();
210         thes_[lang] = new MyThes(af, df);
211         return true;
212 }
213
214
215 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
216 {
217         return d->thesaurusAvailable(lang);
218 }
219
220
221 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
222 {
223         Meanings meanings;
224         MyThes * mythes = 0;
225
226         if (!d->addThesaurus(lang))
227                 return meanings;
228
229         for (Thesauri::const_iterator it = d->thes_.begin();
230              it != d->thes_.end(); ++it) {
231                 if (it->first == lang) {
232                         mythes = it->second;
233                         break;
234                 }
235         }
236
237         if (!mythes)
238                 return meanings;
239
240         string const encoding = mythes->get_th_encoding();
241         
242         mentry * pmean;
243         string const text = to_iconv_encoding(support::lowercase(t), encoding);
244         int len = strlen(text.c_str());
245         int count = mythes->Lookup(text.c_str(), len, &pmean);
246         if (!count)
247                 return meanings;
248
249         // don't change value of pmean or count
250         // they are needed for the CleanUpAfterLookup routine
251         mentry * pm = pmean;
252         docstring meaning;
253         docstring ret;
254         for (int i = 0; i < count; i++) {
255                 meaning = from_iconv_encoding(string(pm->defn), encoding);
256                 // remove silly item
257                 if (support::prefixIs(meaning, '-'))
258                         meaning = support::ltrim(meaning, "- ");
259                 for (int j = 0; j < pm->count; j++) {
260                         ret = from_iconv_encoding(string(pm->psyns[j]), encoding);
261                 }
262         meanings[meaning].push_back(ret);
263         pm++;
264         }
265         // now clean up all allocated memory
266         mythes->CleanUpAfterLookup(&pmean, count);
267
268         for (Meanings::iterator it = meanings.begin();
269              it != meanings.end(); ++it)
270                 sort(it->second.begin(), it->second.end());
271
272         return meanings;
273 }
274
275 #else
276
277 struct Thesaurus::Private
278 {
279 };
280
281
282 Thesaurus::Meanings Thesaurus::lookup(docstring const &, docstring const &)
283 {
284         return Meanings();
285 }
286
287
288 bool Thesaurus::thesaurusAvailable(docstring const &) const
289 {
290         return false;
291 }
292
293 #endif // HAVE_LIBMYTHES
294 #endif // HAVE_LIBAIKSAURUS
295
296 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
297 {
298 }
299
300
301 Thesaurus::~Thesaurus()
302 {
303         delete d;
304 }
305
306 // Global instance
307 Thesaurus thesaurus;
308
309
310 } // namespace lyx