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, vector<string> const & dbs,
42 string const & d, vector<string> const & p,
43 vector<string> const & r, vector<string> const & e):
44 name_(n), id_(i), engine_types_(cet), default_biblios_(dbs), description_(d),
45 package_list_(p), 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 string LyXCiteEngine::getDefaultBiblio(CiteEngineType const & cet) const
124 string const etp = theCiteEnginesList.getTypeAsString(cet) + ":";
125 //check whether all of the required packages are available
126 vector<string>::const_iterator it = default_biblios_.begin();
127 vector<string>::const_iterator end = default_biblios_.end();
128 for (; it != end; ++it) {
129 string const s = *it;
130 if (prefixIs(s, etp))
132 else if (!contains(s, ':') && res.empty())
139 bool LyXCiteEngine::isDefaultBiblio(string const & bf) const
141 string const bfs = ":" + bf;
142 vector<string>::const_iterator it = default_biblios_.begin();
143 vector<string>::const_iterator end = default_biblios_.end();
144 for (; it != end; ++it) {
145 string const s = *it;
146 if (suffixIs(s, bfs) || bf == s)
153 // used when sorting the cite engine list.
156 int operator()(LyXCiteEngine const & ce1, LyXCiteEngine const & ce2) const
158 return _(ce1.getName()) < _(ce2.getName());
166 typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
169 CiteEngineTypeTranslator const init_citeenginetypetranslator()
171 CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
172 translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
173 translator.addPair("default", ENGINE_TYPE_DEFAULT);
178 CiteEngineTypeTranslator const & citeenginetypetranslator()
180 static CiteEngineTypeTranslator const translator =
181 init_citeenginetypetranslator();
188 string CiteEnginesList::getTypeAsString(CiteEngineType const & et) const
190 return citeenginetypetranslator().find(et);
194 CiteEngineType CiteEnginesList::getType(string const & et) const
196 return citeenginetypetranslator().find(et);
200 // Much of this is borrowed from LayoutFileList::read()
201 bool CiteEnginesList::read()
203 FileName const real_file = libFileSearch("", "lyxciteengines.lst");
204 LYXERR(Debug::TCLASS, "Reading cite engines from `" << real_file << '\'');
206 if (real_file.empty()) {
207 LYXERR0("unable to find cite engines file `citeengines.lst'.\n"
208 << "No cite engines will be available.");
213 if (!lex.setFile(real_file)) {
214 LYXERR0("lyxlex was not able to set file: "
215 << real_file << ".\nNo cite engines will be available.");
220 LYXERR0("unable to open cite engines file `"
221 << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
222 << "'\nNo cite engines will be available.");
226 bool finished = false;
227 // Parse cite engines files
228 LYXERR(Debug::TCLASS, "Starting parsing of lyxciteengines.lst");
229 while (lex.isOK() && !finished) {
230 LYXERR(Debug::TCLASS, "\tline by line");
232 case Lexer::LEX_FEOF:
236 string const cename = lex.getString();
237 LYXERR(Debug::TCLASS, "Engine name: " << cename);
240 string const fname = lex.getString();
241 LYXERR(Debug::TCLASS, "Filename: " << fname);
244 string cet = lex.getString();
245 LYXERR(Debug::TCLASS, "Engine Type: " << cet);
247 while (!cet.empty()) {
249 cet = split(cet, p, '|');
254 string db = lex.getString();
255 LYXERR(Debug::TCLASS, "Default Biblio: " << db);
257 while (!db.empty()) {
259 db = split(db, p, '|');
264 string const desc = lex.getString();
265 LYXERR(Debug::TCLASS, "Description: " << desc);
269 string str = lex.getString();
270 LYXERR(Debug::TCLASS, "Packages: " << str);
272 while (!str.empty()) {
274 str = split(str, p, ',');
279 str = lex.getString();
280 LYXERR(Debug::TCLASS, "Required: " << str);
282 while (!str.empty()) {
284 str = split(str, p, '|');
289 str = lex.getString();
290 LYXERR(Debug::TCLASS, "Excluded: " << str);
292 while (!str.empty()) {
294 str = split(str, p, '|');
297 // This code is run when we have
298 // cename, fname, desc, pkgs, req and exc
299 addCiteEngine(cename, fname, cets, dbs, desc, pkgs, req, exc);
303 LYXERR(Debug::TCLASS, "End of parsing of lyxciteengines.lst");
305 if (!theCiteEnginesList.empty())
306 sort(theCiteEnginesList.begin(), theCiteEnginesList.end(), EngineSorter());
311 void CiteEnginesList::addCiteEngine(string const & cename,
312 string const & filename, vector<string> const & cets,
313 vector<string> const & dbs, string const & description,
314 vector<string> const & pkgs, vector<string> const & req,
315 vector<string> const & exc)
317 LyXCiteEngine ce(cename, filename, cets, dbs, description, pkgs, req, exc);
318 englist_.push_back(ce);
322 LyXCiteEnginesList::const_iterator CiteEnginesList::begin() const
324 return englist_.begin();
328 LyXCiteEnginesList::iterator CiteEnginesList::begin()
330 return englist_.begin();
334 LyXCiteEnginesList::const_iterator CiteEnginesList::end() const
336 return englist_.end();
340 LyXCiteEnginesList::iterator CiteEnginesList::end()
342 return englist_.end();
346 LyXCiteEngine const * CiteEnginesList::operator[](string const & str) const
348 LyXCiteEnginesList::const_iterator it = englist_.begin();
349 for (; it != englist_.end(); ++it)
350 if (it->getID() == str) {
351 LyXCiteEngine const & eng = *it;
358 LyXCiteEngine * CiteEnginesList::operator[](string const & str)
360 LyXCiteEnginesList::iterator it = englist_.begin();
361 for (; it != englist_.end(); ++it)
362 if (it->getID() == str) {
363 LyXCiteEngine & eng = *it;