]> git.lyx.org Git - lyx.git/blob - src/Thesaurus.cpp
listerrors.lyx : Update a link.
[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 "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"
25
26 #ifdef USE_EXTERNAL_MYTHES
27 #include MYTHES_H_LOCATION
28 #else
29 #include <cstdio>
30 #include "support/mythes/mythes.hxx"
31 #endif
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 anon
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                         ifstream ifs(it->absFileName().c_str());
107                         if (ifs) {
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);
112                                 ifs >> items;
113                                 if (ifs.fail()) {
114                                         LYXERR(Debug::FILES, "ignore irregular thesaurus idx file: " << it->absFileName());
115                                         continue;
116                                 }
117                                 if (encoding.length() == 0 || encoding.find_first_of(',') != string::npos) {
118                                         LYXERR(Debug::FILES, "ignore version1 thesaurus idx file: " << it->absFileName());
119                                         continue;
120                                 }
121                         }
122                         idx = it->absFileName();
123                         LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
124                         break;
125                 }
126         }
127         if (idx.empty()) {
128                 return make_pair(string(), string());
129         }
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);
134                         break;
135                 }
136         }
137         return make_pair(idx, data);
138 }
139
140
141 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
142 {
143         string const thes_path = external_path(lyxrc.thesaurusdir_path);
144         pair<string,string> result ;
145
146         if (thesaurusAvailable(lang))
147                 return make_pair(string(), string());
148
149         if (!thes_path.empty()) {
150                 result = getThesaurus(thes_path, lang);
151         }
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);
155         }
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);
159         }
160         return result;
161 }
162
163
164 bool Thesaurus::Private::addThesaurus(docstring const & lang)
165 {
166         if (thesaurusAvailable(lang))
167                 return true;
168
169         ThesFiles files = getThesaurus(lang);
170         string const idx = files.first;
171         string const data = files.second;
172
173         if (idx.empty() || data.empty())
174                 return false;
175
176         char const * af = idx.c_str();
177         char const * df = data.c_str();
178         thes_[lang] = new MyThes(af, df);
179         return true;
180 }
181
182
183 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
184 {
185         return d->thesaurusAvailable(lang);
186 }
187
188
189 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
190 {
191         if (thesaurusAvailable(lang))
192                 return true;
193         pair<string, string> files = d->getThesaurus(lang);
194         return (!files.first.empty() && !files.second.empty());
195 }
196
197
198 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
199 {
200         Meanings meanings;
201         MyThes * mythes = 0;
202
203         if (!d->addThesaurus(lang))
204                 return meanings;
205
206         for (Thesauri::const_iterator it = d->thes_.begin();
207              it != d->thes_.end(); ++it) {
208                 if (it->first == lang) {
209                         mythes = it->second;
210                         break;
211                 }
212         }
213
214         if (!mythes)
215                 return meanings;
216
217         string const encoding = mythes->get_th_encoding();
218         
219         mentry * pmean;
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);
223         if (!count)
224                 return meanings;
225
226         // don't change value of pmean or count
227         // they are needed for the CleanUpAfterLookup routine
228         mentry * pm = pmean;
229         docstring meaning;
230         for (int i = 0; i < count; i++) {
231                 vector<docstring> ret;
232                 meaning = from_iconv_encoding(string(pm->defn), encoding);
233                 // remove silly item
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));
238                 }
239                 meanings[meaning] = ret;
240                 ++pm;
241         }
242         // now clean up all allocated memory
243         mythes->CleanUpAfterLookup(&pmean, count);
244
245         for (Meanings::iterator it = meanings.begin();
246              it != meanings.end(); ++it)
247                 sort(it->second.begin(), it->second.end());
248
249         return meanings;
250 }
251
252
253 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
254 {
255 }
256
257
258 Thesaurus::~Thesaurus()
259 {
260         delete d;
261 }
262
263 // Global instance
264 Thesaurus thesaurus;
265
266
267 } // namespace lyx