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