]> git.lyx.org Git - lyx.git/blob - src/Thesaurus.cpp
Add known citation packages to LaTeXFeatures:
[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 #ifdef USE_EXTERNAL_MYTHES
31 #include MYTHES_H_LOCATION
32 #else
33 #include <cstdio>
34 #include "support/mythes/mythes.hxx"
35 #endif
36
37 #include "frontends/alert.h"
38
39 #include <algorithm>
40 #include <cstring>
41 #include <fstream>
42
43 using namespace std;
44 using namespace lyx::support;
45 using namespace lyx::support::os;
46
47 namespace lyx {
48
49 namespace {
50
51 typedef std::map<docstring, MyThes *> Thesauri;
52
53 } // namespace anon
54
55 struct Thesaurus::Private
56 {
57         ~Private()
58         {
59                 for (Thesauri::iterator it = thes_.begin();
60                      it != thes_.end(); ++it) {
61                         delete it->second;
62                 }
63         }
64         ///
65         bool thesaurusAvailable(docstring const & lang) const
66         {
67                 for (Thesauri::const_iterator it = thes_.begin();
68                         it != thes_.end(); ++it) {
69                                 if (it->first == lang)
70                                         if (it->second)
71                                                 return true;
72                 }
73                 return false;
74         }
75
76         ///
77         typedef std::pair<std::string, std::string> ThesFiles;
78         ///
79         ThesFiles getThesaurus(string const & path, docstring const & lang);
80         ThesFiles getThesaurus(docstring const & lang);
81         /// add a thesaurus to the list
82         bool addThesaurus(docstring const & lang);
83
84         /// the thesauri
85         Thesauri thes_;
86
87         /// the location below system/user directory
88         /// there the data+idx files lookup will happen
89         const string dataDirectory(void) { return "thes"; }
90
91 };
92
93
94 pair<string,string> Thesaurus::Private::getThesaurus(string const & path, docstring const & lang)
95 {
96         FileName base(path);
97         if (!base.isDirectory()) {
98                 return make_pair(string(), string());
99         }
100         FileNameList const idx_files = base.dirList("idx");
101         FileNameList const data_files = base.dirList("dat");
102         string idx;
103         string data;
104         string basename;
105
106         LYXERR(Debug::FILES, "thesaurus path: " << path);
107         for (FileNameList::const_iterator it = idx_files.begin(); it != idx_files.end(); ++it) {
108                 basename = it->onlyFileNameWithoutExt();
109                 if (contains(basename, to_ascii(lang))) {
110                         ifstream ifs(it->absFileName().c_str());
111                         if (ifs) {
112                                 // check for appropriate version of index file
113                                 string encoding; // first line is encoding
114                                 int items = 0;   // second line is no. of items
115                                 getline(ifs,encoding);
116                                 ifs >> items;
117                                 if (ifs.fail()) {
118                                         LYXERR(Debug::FILES, "ignore irregular thesaurus idx file: " << it->absFileName());
119                                         continue;
120                                 }
121                                 if (encoding.length() == 0 || encoding.find_first_of(',') != string::npos) {
122                                         LYXERR(Debug::FILES, "ignore version1 thesaurus idx file: " << it->absFileName());
123                                         continue;
124                                 }
125                         }
126                         idx = it->absFileName();
127                         LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
128                         break;
129                 }
130         }
131         if (idx.empty())
132                 return make_pair(string(), string());
133         for (support::FileNameList::const_iterator it = data_files.begin(); it != data_files.end(); ++it) {
134                 if (contains(it->onlyFileName(), basename)) {
135                         data = it->absFileName();
136                         LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
137                         break;
138                 }
139         }
140         return make_pair(idx, data);
141 }
142
143
144 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
145 {
146         string const thes_path = external_path(lyxrc.thesaurusdir_path);
147         pair<string,string> result ;
148
149         if (thesaurusAvailable(lang))
150                 return make_pair(string(), string());
151
152         if (!thes_path.empty())
153                 result = getThesaurus(thes_path, lang);
154         if (result.first.empty() || result.second.empty()) {
155                 string const sys_path = external_path(addName(lyx::support::package().system_support().absFileName(),dataDirectory())) ;
156                 result = getThesaurus(sys_path, lang);
157         }
158         if (result.first.empty() || result.second.empty()) {
159                 string const user_path = external_path(addName(lyx::support::package().user_support().absFileName(),dataDirectory())) ;
160                 result = getThesaurus(user_path, lang);
161         }
162         return result;
163 }
164
165
166 bool Thesaurus::Private::addThesaurus(docstring const & lang)
167 {
168         if (thesaurusAvailable(lang))
169                 return true;
170
171         ThesFiles files = getThesaurus(lang);
172         string const idx = files.first;
173         string const data = files.second;
174
175         if (idx.empty() || data.empty())
176                 return false;
177
178         char const * af = idx.c_str();
179         char const * df = data.c_str();
180         thes_[lang] = new MyThes(af, df);
181         return true;
182 }
183
184
185 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
186 {
187         return d->thesaurusAvailable(lang);
188 }
189
190
191 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
192 {
193         if (thesaurusAvailable(lang))
194                 return true;
195         pair<string, string> files = d->getThesaurus(lang);
196         return (!files.first.empty() && !files.second.empty());
197 }
198
199
200 Thesaurus::Meanings Thesaurus::lookup(WordLangTuple const & wl)
201 {
202         Meanings meanings;
203         MyThes * mythes = 0;
204
205         docstring const lang_code = from_ascii(wl.lang()->code());
206         docstring const t = wl.word();
207
208         if (!d->addThesaurus(lang_code))
209                 return meanings;
210
211         for (Thesauri::const_iterator it = d->thes_.begin();
212              it != d->thes_.end(); ++it) {
213                 if (it->first == lang_code) {
214                         mythes = it->second;
215                         break;
216                 }
217         }
218
219         if (!mythes)
220                 return meanings;
221
222         string const encoding = mythes->get_th_encoding();
223         
224         mentry * pmean;
225         string const text = to_iconv_encoding(support::lowercase(t), encoding);
226         int len = strlen(text.c_str());
227         int count = mythes->Lookup(text.c_str(), len, &pmean);
228         if (!count) {
229                 SpellChecker * speller = theSpellChecker();
230                 if (!speller)
231                         return meanings;
232                 docstring_list suggestions;
233                 speller->stem(wl, suggestions);
234                 for (size_t i = 0; i != suggestions.size(); ++i) {
235                         string const wordform = to_iconv_encoding(support::lowercase(suggestions[i]), encoding);
236                         len = strlen(wordform.c_str());
237                         count = mythes->Lookup(wordform.c_str(), len, &pmean);
238                         if (count)
239                                 break;
240                 }
241                 if (!count)
242                         return meanings;
243         }
244
245         // don't change value of pmean or count
246         // they are needed for the CleanUpAfterLookup routine
247         mentry * pm = pmean;
248         docstring meaning;
249         for (int i = 0; i < count; i++) {
250                 vector<docstring> ret;
251                 meaning = from_iconv_encoding(string(pm->defn), encoding);
252                 // remove silly item
253                 if (support::prefixIs(meaning, '-'))
254                         meaning = support::ltrim(meaning, "- ");
255                 for (int j = 0; j < pm->count; j++) {
256                         ret.push_back(from_iconv_encoding(string(pm->psyns[j]), encoding));
257                 }
258                 meanings[meaning] = ret;
259                 ++pm;
260         }
261         // now clean up all allocated memory
262         mythes->CleanUpAfterLookup(&pmean, count);
263
264         for (Meanings::iterator it = meanings.begin();
265              it != meanings.end(); ++it)
266                 sort(it->second.begin(), it->second.end());
267
268         return meanings;
269 }
270
271
272 Thesaurus::Thesaurus()
273         : d(new Thesaurus::Private)
274 {
275 }
276
277
278 Thesaurus::~Thesaurus()
279 {
280         delete d;
281 }
282
283 // Global instance
284 Thesaurus thesaurus;
285
286
287 } // namespace lyx