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 & cfm,
42 vector<string> const & dbs,
43 string const & d, vector<string> const & p):
44 name_(n), id_(i), engine_types_(cet), cite_framework_(cfm), default_biblios_(dbs),
45 description_(d), package_list_(p), checked_(false), available_(false)
47 filename_ = id_ + ".citeengine";
51 vector<string> LyXCiteEngine::prerequisites() const
55 return prerequisites_;
59 bool LyXCiteEngine::isAvailable() const
61 if (package_list_.empty())
67 //check whether all of the required packages are available
68 vector<string>::const_iterator it = package_list_.begin();
69 vector<string>::const_iterator end = package_list_.end();
70 for (; it != end; ++it) {
71 if (!LaTeXFeatures::isAvailable(*it)) {
73 prerequisites_.push_back(*it);
80 bool LyXCiteEngine::hasEngineType(CiteEngineType const & et) const
82 return std::find(engine_types_.begin(), engine_types_.end(),
83 theCiteEnginesList.getTypeAsString(et)) != engine_types_.end();
87 string LyXCiteEngine::getDefaultBiblio(CiteEngineType const & cet) const
90 string const etp = theCiteEnginesList.getTypeAsString(cet) + ":";
91 //check whether all of the required packages are available
92 for (string const &s: default_biblios_) {
95 else if (!contains(s, ':') && res.empty())
102 bool LyXCiteEngine::isDefaultBiblio(string const & bf) const
104 string const bfs = ":" + bf;
105 for (string const &s: default_biblios_)
106 if (suffixIs(s, bfs) || bf == s)
113 // used when sorting the cite engine list.
116 int operator()(LyXCiteEngine const & ce1, LyXCiteEngine const & ce2) const
118 return _(ce1.getName()) < _(ce2.getName());
126 typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
129 CiteEngineTypeTranslator const init_citeenginetypetranslator()
131 CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
132 translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
133 translator.addPair("default", ENGINE_TYPE_DEFAULT);
138 CiteEngineTypeTranslator const & citeenginetypetranslator()
140 static CiteEngineTypeTranslator const translator =
141 init_citeenginetypetranslator();
148 string CiteEnginesList::getTypeAsString(CiteEngineType const & et) const
150 return citeenginetypetranslator().find(et);
154 CiteEngineType CiteEnginesList::getType(string const & et) const
156 return citeenginetypetranslator().find(et);
160 // Much of this is borrowed from LayoutFileList::read()
161 bool CiteEnginesList::read()
163 FileName const real_file = libFileSearch("", "lyxciteengines.lst");
164 LYXERR(Debug::TCLASS, "Reading cite engines from `" << real_file << '\'');
166 if (real_file.empty()) {
167 LYXERR0("unable to find cite engines file `citeengines.lst'.\n"
168 << "No cite engines will be available.");
173 if (!lex.setFile(real_file)) {
174 LYXERR0("lyxlex was not able to set file: "
175 << real_file << ".\nNo cite engines will be available.");
180 LYXERR0("unable to open cite engines file `"
181 << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
182 << "'\nNo cite engines will be available.");
186 bool finished = false;
187 // Parse cite engines files
188 LYXERR(Debug::TCLASS, "Starting parsing of lyxciteengines.lst");
189 while (lex.isOK() && !finished) {
190 LYXERR(Debug::TCLASS, "\tline by line");
192 case Lexer::LEX_FEOF:
196 string const cename = lex.getString();
197 LYXERR(Debug::TCLASS, "Engine name: " << cename);
200 string const fname = lex.getString();
201 LYXERR(Debug::TCLASS, "Filename: " << fname);
204 string cet = lex.getString();
205 LYXERR(Debug::TCLASS, "Engine Type: " << cet);
207 while (!cet.empty()) {
209 cet = split(cet, p, '|');
214 string const citeframework = lex.getString();
215 LYXERR(Debug::TCLASS, "CiteFramework: " << citeframework);
218 string db = lex.getString();
219 LYXERR(Debug::TCLASS, "Default Biblio: " << db);
221 while (!db.empty()) {
223 db = split(db, p, '|');
228 string const desc = lex.getString();
229 LYXERR(Debug::TCLASS, "Description: " << desc);
233 string str = lex.getString();
234 LYXERR(Debug::TCLASS, "Packages: " << str);
236 while (!str.empty()) {
238 str = split(str, p, ',');
241 // This code is run when we have
242 // cename, fname, cets, citeframework, dbs, desc, pkgs
243 addCiteEngine(cename, fname, cets, citeframework, dbs, desc, pkgs);
247 LYXERR(Debug::TCLASS, "End of parsing of lyxciteengines.lst");
249 if (!theCiteEnginesList.empty())
250 sort(theCiteEnginesList.begin(), theCiteEnginesList.end(), EngineSorter());
255 void CiteEnginesList::addCiteEngine(string const & cename,
256 string const & filename, vector<string> const & cets,
257 string const & citeframework, vector<string> const & dbs,
258 string const & description, vector<string> const & pkgs)
260 LyXCiteEngine ce(cename, filename, cets, citeframework, dbs, description, pkgs);
261 englist_.push_back(ce);
265 LyXCiteEnginesList::const_iterator CiteEnginesList::begin() const
267 return englist_.begin();
271 LyXCiteEnginesList::iterator CiteEnginesList::begin()
273 return englist_.begin();
277 LyXCiteEnginesList::const_iterator CiteEnginesList::end() const
279 return englist_.end();
283 LyXCiteEnginesList::iterator CiteEnginesList::end()
285 return englist_.end();
289 LyXCiteEngine const * CiteEnginesList::operator[](string const & str) const
291 LyXCiteEnginesList::const_iterator it = englist_.begin();
292 for (; it != englist_.end(); ++it)
293 if (it->getID() == str) {
294 LyXCiteEngine const & eng = *it;
301 LyXCiteEngine * CiteEnginesList::operator[](string const & str)
303 LyXCiteEnginesList::iterator it = englist_.begin();
304 for (; it != englist_.end(); ++it)
305 if (it->getID() == str) {
306 LyXCiteEngine & eng = *it;