3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "Thesaurus.h"
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"
26 #include "support/mythes/mythes.hxx"
28 #include "frontends/alert.h"
35 using namespace lyx::support;
36 using namespace lyx::support::os;
42 typedef std::map<docstring, MyThes *> Thesauri;
46 struct Thesaurus::Private
50 for (Thesauri::iterator it = thes_.begin();
51 it != thes_.end(); ++it) {
56 bool thesaurusAvailable(docstring const & lang) const
58 for (Thesauri::const_iterator it = thes_.begin();
59 it != thes_.end(); ++it) {
60 if (it->first == lang)
68 typedef std::pair<std::string, std::string> ThesFiles;
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);
78 /// the location below system/user directory
79 /// there the data+idx files lookup will happen
80 const string dataDirectory(void) { return "thes"; }
85 pair<string,string> Thesaurus::Private::getThesaurus(string const & path, docstring const & lang)
88 if (!base.isDirectory()) {
89 return make_pair(string(), string());
91 FileNameList const idx_files = base.dirList("idx");
92 FileNameList const data_files = base.dirList("dat");
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());
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);
109 LYXERR(Debug::FILES, "ignore irregular thesaurus idx file: " << it->absFileName());
112 if (encoding.length() == 0 || encoding.find_first_of(',') != string::npos) {
113 LYXERR(Debug::FILES, "ignore version1 thesaurus idx file: " << it->absFileName());
117 idx = it->absFileName();
118 LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
123 return make_pair(string(), string());
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);
132 return make_pair(idx, data);
136 pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
138 string const thes_path = external_path(lyxrc.thesaurusdir_path);
139 pair<string,string> result ;
141 if (thesaurusAvailable(lang))
142 return make_pair(string(), string());
144 if (!thes_path.empty()) {
145 result = getThesaurus(thes_path, lang);
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);
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);
159 bool Thesaurus::Private::addThesaurus(docstring const & lang)
161 if (thesaurusAvailable(lang))
164 ThesFiles files = getThesaurus(lang);
165 string const idx = files.first;
166 string const data = files.second;
168 if (idx.empty() || data.empty())
171 char const * af = idx.c_str();
172 char const * df = data.c_str();
173 thes_[lang] = new MyThes(af, df);
178 bool Thesaurus::thesaurusAvailable(docstring const & lang) const
180 return d->thesaurusAvailable(lang);
184 bool Thesaurus::thesaurusInstalled(docstring const & lang) const
186 if (thesaurusAvailable(lang))
188 pair<string, string> files = d->getThesaurus(lang);
189 return (!files.first.empty() && !files.second.empty());
193 Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
198 if (!d->addThesaurus(lang))
201 for (Thesauri::const_iterator it = d->thes_.begin();
202 it != d->thes_.end(); ++it) {
203 if (it->first == lang) {
212 string const encoding = mythes->get_th_encoding();
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);
221 // don't change value of pmean or count
222 // they are needed for the CleanUpAfterLookup routine
225 for (int i = 0; i < count; i++) {
226 vector<docstring> ret;
227 meaning = from_iconv_encoding(string(pm->defn), encoding);
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));
234 meanings[meaning] = ret;
237 // now clean up all allocated memory
238 mythes->CleanUpAfterLookup(&pmean, count);
240 for (Meanings::iterator it = meanings.begin();
241 it != meanings.end(); ++it)
242 sort(it->second.begin(), it->second.end());
248 Thesaurus::Thesaurus() : d(new Thesaurus::Private)
253 Thesaurus::~Thesaurus()