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