PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lyx-2.0.4/src/Thesaurus.cpp

#
C++ | 267 lines | 194 code | 50 blank | 23 comment | 44 complexity | db8ff41e638286cea0d8840c3894a321 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0
  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. #include <config.h>
  12. #include "Thesaurus.h"
  13. #include "LyXRC.h"
  14. #include "support/FileNameList.h"
  15. #include "support/Package.h"
  16. #include "support/debug.h"
  17. #include "support/filetools.h"
  18. #include "support/gettext.h"
  19. #include "support/lstrings.h"
  20. #include "support/os.h"
  21. #ifdef USE_EXTERNAL_MYTHES
  22. #include MYTHES_H_LOCATION
  23. #else
  24. #include <cstdio>
  25. #include "support/mythes/mythes.hxx"
  26. #endif
  27. #include "frontends/alert.h"
  28. #include <algorithm>
  29. #include <cstring>
  30. #include <fstream>
  31. using namespace std;
  32. using namespace lyx::support;
  33. using namespace lyx::support::os;
  34. namespace lyx {
  35. namespace {
  36. typedef std::map<docstring, MyThes *> Thesauri;
  37. } // namespace anon
  38. struct Thesaurus::Private
  39. {
  40. ~Private()
  41. {
  42. for (Thesauri::iterator it = thes_.begin();
  43. it != thes_.end(); ++it) {
  44. delete it->second;
  45. }
  46. }
  47. ///
  48. bool thesaurusAvailable(docstring const & lang) const
  49. {
  50. for (Thesauri::const_iterator it = thes_.begin();
  51. it != thes_.end(); ++it) {
  52. if (it->first == lang)
  53. if (it->second)
  54. return true;
  55. }
  56. return false;
  57. }
  58. ///
  59. typedef std::pair<std::string, std::string> ThesFiles;
  60. ///
  61. ThesFiles getThesaurus(string const & path, docstring const & lang);
  62. ThesFiles getThesaurus(docstring const & lang);
  63. /// add a thesaurus to the list
  64. bool addThesaurus(docstring const & lang);
  65. /// the thesauri
  66. Thesauri thes_;
  67. /// the location below system/user directory
  68. /// there the data+idx files lookup will happen
  69. const string dataDirectory(void) { return "thes"; }
  70. };
  71. pair<string,string> Thesaurus::Private::getThesaurus(string const & path, docstring const & lang)
  72. {
  73. FileName base(path);
  74. if (!base.isDirectory()) {
  75. return make_pair(string(), string());
  76. }
  77. FileNameList const idx_files = base.dirList("idx");
  78. FileNameList const data_files = base.dirList("dat");
  79. string idx;
  80. string data;
  81. string basename;
  82. LYXERR(Debug::FILES, "thesaurus path: " << path);
  83. for (FileNameList::const_iterator it = idx_files.begin(); it != idx_files.end(); ++it) {
  84. basename = it->onlyFileNameWithoutExt();
  85. if (contains(basename, to_ascii(lang))) {
  86. ifstream ifs(it->absFileName().c_str());
  87. if (ifs) {
  88. // check for appropriate version of index file
  89. string encoding; // first line is encoding
  90. int items = 0; // second line is no. of items
  91. getline(ifs,encoding);
  92. ifs >> items;
  93. if (ifs.fail()) {
  94. LYXERR(Debug::FILES, "ignore irregular thesaurus idx file: " << it->absFileName());
  95. continue;
  96. }
  97. if (encoding.length() == 0 || encoding.find_first_of(',') != string::npos) {
  98. LYXERR(Debug::FILES, "ignore version1 thesaurus idx file: " << it->absFileName());
  99. continue;
  100. }
  101. }
  102. idx = it->absFileName();
  103. LYXERR(Debug::FILES, "selected thesaurus idx file: " << idx);
  104. break;
  105. }
  106. }
  107. if (idx.empty()) {
  108. return make_pair(string(), string());
  109. }
  110. for (support::FileNameList::const_iterator it = data_files.begin(); it != data_files.end(); ++it) {
  111. if (contains(it->onlyFileName(), basename)) {
  112. data = it->absFileName();
  113. LYXERR(Debug::FILES, "selected thesaurus data file: " << data);
  114. break;
  115. }
  116. }
  117. return make_pair(idx, data);
  118. }
  119. pair<string,string> Thesaurus::Private::getThesaurus(docstring const & lang)
  120. {
  121. string const thes_path = external_path(lyxrc.thesaurusdir_path);
  122. pair<string,string> result ;
  123. if (thesaurusAvailable(lang))
  124. return make_pair(string(), string());
  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. bool Thesaurus::Private::addThesaurus(docstring const & lang)
  139. {
  140. if (thesaurusAvailable(lang))
  141. return true;
  142. ThesFiles files = getThesaurus(lang);
  143. string const idx = files.first;
  144. string const data = files.second;
  145. if (idx.empty() || data.empty())
  146. return false;
  147. char const * af = idx.c_str();
  148. char const * df = data.c_str();
  149. thes_[lang] = new MyThes(af, df);
  150. return true;
  151. }
  152. bool Thesaurus::thesaurusAvailable(docstring const & lang) const
  153. {
  154. return d->thesaurusAvailable(lang);
  155. }
  156. bool Thesaurus::thesaurusInstalled(docstring const & lang) const
  157. {
  158. if (thesaurusAvailable(lang))
  159. return true;
  160. pair<string, string> files = d->getThesaurus(lang);
  161. return (!files.first.empty() && !files.second.empty());
  162. }
  163. Thesaurus::Meanings Thesaurus::lookup(docstring const & t, docstring const & lang)
  164. {
  165. Meanings meanings;
  166. MyThes * mythes = 0;
  167. if (!d->addThesaurus(lang))
  168. return meanings;
  169. for (Thesauri::const_iterator it = d->thes_.begin();
  170. it != d->thes_.end(); ++it) {
  171. if (it->first == lang) {
  172. mythes = it->second;
  173. break;
  174. }
  175. }
  176. if (!mythes)
  177. return meanings;
  178. string const encoding = mythes->get_th_encoding();
  179. mentry * pmean;
  180. string const text = to_iconv_encoding(support::lowercase(t), encoding);
  181. int len = strlen(text.c_str());
  182. int count = mythes->Lookup(text.c_str(), len, &pmean);
  183. if (!count)
  184. return meanings;
  185. // don't change value of pmean or count
  186. // they are needed for the CleanUpAfterLookup routine
  187. mentry * pm = pmean;
  188. docstring meaning;
  189. for (int i = 0; i < count; i++) {
  190. vector<docstring> ret;
  191. meaning = from_iconv_encoding(string(pm->defn), encoding);
  192. // remove silly item
  193. if (support::prefixIs(meaning, '-'))
  194. meaning = support::ltrim(meaning, "- ");
  195. for (int j = 0; j < pm->count; j++) {
  196. ret.push_back(from_iconv_encoding(string(pm->psyns[j]), encoding));
  197. }
  198. meanings[meaning] = ret;
  199. ++pm;
  200. }
  201. // now clean up all allocated memory
  202. mythes->CleanUpAfterLookup(&pmean, count);
  203. for (Meanings::iterator it = meanings.begin();
  204. it != meanings.end(); ++it)
  205. sort(it->second.begin(), it->second.end());
  206. return meanings;
  207. }
  208. Thesaurus::Thesaurus() : d(new Thesaurus::Private)
  209. {
  210. }
  211. Thesaurus::~Thesaurus()
  212. {
  213. delete d;
  214. }
  215. // Global instance
  216. Thesaurus thesaurus;
  217. } // namespace lyx