3 * \file CiteEnginesList.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "CiteEnginesList.h"
18 #include "LaTeXFeatures.h"
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/gettext.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26 #include "support/Translator.h"
31 using namespace lyx::support;
36 //global variable: cite engine list
37 CiteEnginesList theCiteEnginesList;
40 LyXCiteEngine::LyXCiteEngine(string const & n, string const & i,
41 vector<string> const & cet, string const & d,
42 vector<string> const & p,
43 vector<string> const & r, vector<string> const & e):
44 name_(n), id_(i), engine_types_(cet), description_(d), package_list_(p),
45 required_engines_(r), excluded_engines_(e),
46 checked_(false), available_(false)
48 filename_ = id_ + ".citeengine";
52 vector<string> LyXCiteEngine::prerequisites() const
56 return prerequisites_;
60 bool LyXCiteEngine::isAvailable() const
62 if (package_list_.empty())
68 //check whether all of the required packages are available
69 vector<string>::const_iterator it = package_list_.begin();
70 vector<string>::const_iterator end = package_list_.end();
71 for (; it != end; ++it) {
72 if (!LaTeXFeatures::isAvailable(*it)) {
74 prerequisites_.push_back(*it);
81 bool LyXCiteEngine::hasEngineType(CiteEngineType const & et) const
83 return std::find(engine_types_.begin(), engine_types_.end(),
84 theCiteEnginesList.getTypeAsString(et)) != engine_types_.end();
88 bool LyXCiteEngine::isCompatible(string const & cename) const
91 if (find(excluded_engines_.begin(), excluded_engines_.end(), cename) !=
92 excluded_engines_.end())
95 LyXCiteEngine const * const lm = theCiteEnginesList[cename];
99 // does it exclude us?
100 vector<string> const excengs = lm->getExcludedEngines();
101 if (find(excengs.begin(), excengs.end(), id_) != excengs.end())
108 bool LyXCiteEngine::areCompatible(string const & eng1, string const & eng2)
110 LyXCiteEngine const * const lm1 = theCiteEnginesList[eng1];
112 return lm1->isCompatible(eng2);
113 LyXCiteEngine const * const lm2 = theCiteEnginesList[eng2];
115 return lm2->isCompatible(eng1);
116 // Can't check it either way.
121 // used when sorting the cite engine list.
124 int operator()(LyXCiteEngine const & ce1, LyXCiteEngine const & ce2) const
126 return _(ce1.getName()) < _(ce2.getName());
134 typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
137 CiteEngineTypeTranslator const init_citeenginetypetranslator()
139 CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
140 translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
141 translator.addPair("default", ENGINE_TYPE_DEFAULT);
146 CiteEngineTypeTranslator const & citeenginetypetranslator()
148 static CiteEngineTypeTranslator const translator =
149 init_citeenginetypetranslator();
156 string CiteEnginesList::getTypeAsString(CiteEngineType const & et) const
158 return citeenginetypetranslator().find(et);
162 CiteEngineType CiteEnginesList::getType(string const & et) const
164 return citeenginetypetranslator().find(et);
168 // Much of this is borrowed from LayoutFileList::read()
169 bool CiteEnginesList::read()
171 FileName const real_file = libFileSearch("", "lyxciteengines.lst");
172 LYXERR(Debug::TCLASS, "Reading cite engines from `" << real_file << '\'');
174 if (real_file.empty()) {
175 LYXERR0("unable to find cite engines file `citeengines.lst'.\n"
176 << "No cite engines will be available.");
181 if (!lex.setFile(real_file)) {
182 LYXERR0("lyxlex was not able to set file: "
183 << real_file << ".\nNo cite engines will be available.");
188 LYXERR0("unable to open cite engines file `"
189 << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
190 << "'\nNo cite engines will be available.");
194 bool finished = false;
195 // Parse cite engines files
196 LYXERR(Debug::TCLASS, "Starting parsing of lyxciteengines.lst");
197 while (lex.isOK() && !finished) {
198 LYXERR(Debug::TCLASS, "\tline by line");
200 case Lexer::LEX_FEOF:
204 string const cename = lex.getString();
205 LYXERR(Debug::TCLASS, "Engine name: " << cename);
208 string const fname = lex.getString();
209 LYXERR(Debug::TCLASS, "Filename: " << fname);
212 string cet = lex.getString();
213 LYXERR(Debug::TCLASS, "Engine Type: " << cet);
215 while (!cet.empty()) {
217 cet = split(cet, p, '|');
222 string const desc = lex.getString();
223 LYXERR(Debug::TCLASS, "Description: " << desc);
227 string str = lex.getString();
228 LYXERR(Debug::TCLASS, "Packages: " << str);
230 while (!str.empty()) {
232 str = split(str, p, ',');
237 str = lex.getString();
238 LYXERR(Debug::TCLASS, "Required: " << str);
240 while (!str.empty()) {
242 str = split(str, p, '|');
247 str = lex.getString();
248 LYXERR(Debug::TCLASS, "Excluded: " << str);
250 while (!str.empty()) {
252 str = split(str, p, '|');
255 // This code is run when we have
256 // cename, fname, desc, pkgs, req and exc
257 addCiteEngine(cename, fname, cets, desc, pkgs, req, exc);
261 LYXERR(Debug::TCLASS, "End of parsing of lyxciteengines.lst");
263 if (!theCiteEnginesList.empty())
264 sort(theCiteEnginesList.begin(), theCiteEnginesList.end(), EngineSorter());
269 void CiteEnginesList::addCiteEngine(string const & cename,
270 string const & filename, vector<string> const & cets, string const & description,
271 vector<string> const & pkgs, vector<string> const & req,
272 vector<string> const & exc)
274 LyXCiteEngine ce(cename, filename, cets, description, pkgs, req, exc);
275 englist_.push_back(ce);
279 LyXCiteEnginesList::const_iterator CiteEnginesList::begin() const
281 return englist_.begin();
285 LyXCiteEnginesList::iterator CiteEnginesList::begin()
287 return englist_.begin();
291 LyXCiteEnginesList::const_iterator CiteEnginesList::end() const
293 return englist_.end();
297 LyXCiteEnginesList::iterator CiteEnginesList::end()
299 return englist_.end();
303 LyXCiteEngine const * CiteEnginesList::operator[](string const & str) const
305 LyXCiteEnginesList::const_iterator it = englist_.begin();
306 for (; it != englist_.end(); ++it)
307 if (it->getID() == str) {
308 LyXCiteEngine const & eng = *it;
315 LyXCiteEngine * CiteEnginesList::operator[](string const & str)
317 LyXCiteEnginesList::iterator it = englist_.begin();
318 for (; it != englist_.end(); ++it)
319 if (it->getID() == str) {
320 LyXCiteEngine & eng = *it;