]> git.lyx.org Git - lyx.git/blob - src/Thesaurus.cpp
This commit does two things.
[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/debug.h"
20 #include "support/filetools.h"
21 #include "support/gettext.h"
22 #include "support/lstrings.h"
23 #include "support/os.h"
24 #include "support/unicode.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 string const to_iconv_encoding(docstring const & s, string const & encoding)
42 {
43         std::vector<char> const encoded =
44                 ucs4_to_eightbit(s.data(), s.length(), encoding);
45         return string(encoded.begin(), encoded.end());
46 }
47
48
49 docstring const from_iconv_encoding(string const & s, string const & encoding)
50 {
51         std::vector<char_type> const ucs4 =
52                 eightbit_to_ucs4(s.data(), s.length(), encoding);
53         return docstring(ucs4.begin(), ucs4.end());
54 }
55
56 typedef std::map<docstring, MyThes *> Thesauri;
57
58 } // namespace anon
59
60
61 struct Thesaurus::Private
62 {
63         ~Private()
64         {
65                 for (Thesauri::iterator it = thes_.begin();
66                      it != thes_.end(); ++it) {
67                         delete it->second;
68                 }
69         }
70         ///
71         bool thesaurusAvailable(docstring const & lang) const
72         {
73                 for (Thesauri::const_iterator it = thes_.begin();
74                         it != thes_.end(); ++it) {
75                                 if (it->first == lang)
76                                         if (it->second)
77                                                 return true;
78                 }
79                 return false;
80         }
81
82         /// add a thesaurus to the list
83         bool addThesaurus(docstring const & lang);
84
85         /// the thesauri
86         Thesauri thes_;
87 };
88
89 bool Thesaurus::Private::addThesaurus(docstring const & lang)
90 {
91         string const thes_path = external_path(lyxrc.thesaurusdir_path);
92         LYXERR(Debug::FILES, "thesaurus path: " << thes_path);
93         if (thes_path.empty())
94                 return false;
95
96         if (thesaurusAvailable(lang))
97                 return true;
98
99         FileNameList const idx_files = FileName(thes_path).dirList("idx");
100         FileNameList const data_files = FileName(thes_path).dirList("dat");
101         string idx;
102         string data;
103
104         for (FileNameList::const_iterator it = idx_files.begin();
105              it != idx_files.end(); ++it) {
106                 LYXERR(Debug::FILES, "found thesaurus idx file: " << it->onlyFileName());
107                 if (contains(it->onlyFileName(), to_ascii(lang))) {
108                         idx = it->absFilename();
109                         LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
110                         break;
111                         }
112                 }
113
114         for (support::FileNameList::const_iterator it = data_files.begin();
115              it != data_files.end(); ++it) {
116                 LYXERR(Debug::FILES, "found thesaurus data file: " << it->onlyFileName());
117                 if (contains(it->onlyFileName(), to_ascii(lang))) {
118                         data = it->absFilename();
119                         LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
120                         break;
121                         }
122                 }
123
124         if (idx.empty() || data.empty())
125                 return false;
126
127         char const * af = idx.c_str();
128         char const * df = data.c_str();
129         thes_[lang] = new MyThes(af, df);
130         return true;
131 }
132
133
134 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
135 {
136         return d->thesaurusAvailable(lang);
137 }
138
139
140 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
141 {
142         Meanings meanings;
143         MyThes * mythes = 0;
144
145         if (!d->addThesaurus(lang))
146                 return meanings;
147
148         for (Thesauri::const_iterator it = d->thes_.begin();
149              it != d->thes_.end(); ++it) {
150                 if (it->first == lang) {
151                         mythes = it->second;
152                         break;
153                 }
154         }
155
156         if (!mythes)
157                 return meanings;
158
159         string const encoding = mythes->get_th_encoding();
160         
161         mentry * pmean;
162         string const text = to_iconv_encoding(support::lowercase(t), encoding);
163         int len = strlen(text.c_str());
164         int count = mythes->Lookup(text.c_str(), len, &pmean);
165         if (!count)
166                 return meanings;
167
168         // don't change value of pmean or count
169         // they are needed for the CleanUpAfterLookup routine
170         mentry * pm = pmean;
171         docstring meaning;
172         docstring ret;
173         for (int i = 0; i < count; i++) {
174                 meaning = from_iconv_encoding(string(pm->defn), encoding);
175                 // remove silly item
176                 if (support::prefixIs(meaning, '-'))
177                         meaning = support::ltrim(meaning, "- ");
178                 for (int j = 0; j < pm->count; j++) {
179                         ret = from_iconv_encoding(string(pm->psyns[j]), encoding);
180                 }
181         meanings[meaning].push_back(ret);
182         pm++;
183         }
184         // now clean up all allocated memory
185         mythes->CleanUpAfterLookup(&pmean, count);
186
187         for (Meanings::iterator it = meanings.begin();
188              it != meanings.end(); ++it)
189                 sort(it->second.begin(), it->second.end());
190
191         return meanings;
192 }
193
194
195 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
196 {
197 }
198
199
200 Thesaurus::~Thesaurus()
201 {
202         delete d;
203 }
204
205 // Global instance
206 Thesaurus thesaurus;
207
208
209 } // namespace lyx