]> git.lyx.org Git - features.git/blob - src/CiteEnginesList.cpp
Merge branch 'master' into biblatex2
[features.git] / src / CiteEnginesList.cpp
1 // -*- C++ -*-
2 /**
3  * \file CiteEnginesList.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Richard Heck
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "CiteEnginesList.h"
16
17 #include "Citation.h"
18 #include "LaTeXFeatures.h"
19 #include "Lexer.h"
20
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"
27
28 #include <algorithm>
29
30 using namespace std;
31 using namespace lyx::support;
32
33 namespace lyx {
34
35
36 //global variable: cite engine list
37 CiteEnginesList theCiteEnginesList;
38
39
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                              vector<string> const & r, vector<string> const & e):
45         name_(n), id_(i), engine_types_(cet), cite_framework_(cfm), default_biblios_(dbs),
46         description_(d), package_list_(p), required_engines_(r), excluded_engines_(e),
47         checked_(false), available_(false)
48 {
49         filename_ = id_ + ".citeengine";
50 }
51
52
53 vector<string> LyXCiteEngine::prerequisites() const
54 {
55         if (!checked_)
56                 isAvailable();
57         return prerequisites_;
58 }
59
60
61 bool LyXCiteEngine::isAvailable() const
62 {
63         if (package_list_.empty())
64                 return true;
65         if (checked_)
66                 return available_;
67         checked_ = true;
68         available_ = true;
69         //check whether all of the required packages are available
70         vector<string>::const_iterator it  = package_list_.begin();
71         vector<string>::const_iterator end = package_list_.end();
72         for (; it != end; ++it) {
73                 if (!LaTeXFeatures::isAvailable(*it)) {
74                         available_ = false;
75                         prerequisites_.push_back(*it);
76                 }
77         }
78         return available_;
79 }
80
81
82 bool LyXCiteEngine::hasEngineType(CiteEngineType const & et) const
83 {
84         return std::find(engine_types_.begin(), engine_types_.end(),
85                          theCiteEnginesList.getTypeAsString(et)) != engine_types_.end();
86 }
87
88
89 bool LyXCiteEngine::isCompatible(string const & cename) const
90 {
91         // do we exclude it?
92         if (find(excluded_engines_.begin(), excluded_engines_.end(), cename) !=
93                         excluded_engines_.end())
94                 return false;
95
96         LyXCiteEngine const * const lm = theCiteEnginesList[cename];
97         if (!lm)
98                 return true;
99
100         // does it exclude us?
101         vector<string> const excengs = lm->getExcludedEngines();
102         if (find(excengs.begin(), excengs.end(), id_) != excengs.end())
103                 return false;
104
105         return true;
106 }
107
108
109 bool LyXCiteEngine::areCompatible(string const & eng1, string const & eng2)
110 {
111         LyXCiteEngine const * const lm1 = theCiteEnginesList[eng1];
112         if (lm1)
113                 return lm1->isCompatible(eng2);
114         LyXCiteEngine const * const lm2 = theCiteEnginesList[eng2];
115         if (lm2)
116                 return lm2->isCompatible(eng1);
117         // Can't check it either way.
118         return true;
119 }
120
121
122 string LyXCiteEngine::getDefaultBiblio(CiteEngineType const & cet) const
123 {
124         string res;
125         string const etp = theCiteEnginesList.getTypeAsString(cet) + ":";
126         //check whether all of the required packages are available
127         for (string const &s: default_biblios_) {
128                 if (prefixIs(s, etp))
129                         res = split(s, ':');
130                 else if (!contains(s, ':') && res.empty())
131                         res = s;
132         }
133         return res;
134 }
135
136
137 bool LyXCiteEngine::isDefaultBiblio(string const & bf) const
138 {
139         string const bfs = ":" + bf;
140         for (string const &s: default_biblios_)
141                 if (suffixIs(s, bfs) || bf == s)
142                         return true;
143
144         return false;
145 }
146
147
148 // used when sorting the cite engine list.
149 class EngineSorter {
150 public:
151         int operator()(LyXCiteEngine const & ce1, LyXCiteEngine const & ce2) const
152         {
153                 return _(ce1.getName()) < _(ce2.getName());
154         }
155 };
156
157
158 // Local translators
159 namespace {
160
161 typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
162
163
164 CiteEngineTypeTranslator const init_citeenginetypetranslator()
165 {
166         CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
167         translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
168         translator.addPair("default", ENGINE_TYPE_DEFAULT);
169         return translator;
170 }
171
172
173 CiteEngineTypeTranslator const & citeenginetypetranslator()
174 {
175         static CiteEngineTypeTranslator const translator =
176                 init_citeenginetypetranslator();
177         return translator;
178 }
179
180 } // namespace anon
181
182
183 string CiteEnginesList::getTypeAsString(CiteEngineType const & et) const
184 {
185         return citeenginetypetranslator().find(et);
186 }
187
188
189 CiteEngineType CiteEnginesList::getType(string const & et) const
190 {
191         return citeenginetypetranslator().find(et);
192 }
193
194
195 // Much of this is borrowed from LayoutFileList::read()
196 bool CiteEnginesList::read()
197 {
198         FileName const real_file = libFileSearch("", "lyxciteengines.lst");
199         LYXERR(Debug::TCLASS, "Reading cite engines from `" << real_file << '\'');
200
201         if (real_file.empty()) {
202                 LYXERR0("unable to find cite engines file `citeengines.lst'.\n"
203                         << "No cite engines will be available.");
204                 return false;
205         }
206
207         Lexer lex;
208         if (!lex.setFile(real_file)) {
209                 LYXERR0("lyxlex was not able to set file: "
210                         << real_file << ".\nNo cite engines will be available.");
211                 return false;
212         }
213
214         if (!lex.isOK()) {
215                 LYXERR0("unable to open cite engines file  `"
216                         << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
217                         << "'\nNo cite engines will be available.");
218                 return false;
219         }
220
221         bool finished = false;
222         // Parse cite engines files
223         LYXERR(Debug::TCLASS, "Starting parsing of lyxciteengines.lst");
224         while (lex.isOK() && !finished) {
225                 LYXERR(Debug::TCLASS, "\tline by line");
226                 switch (lex.lex()) {
227                 case Lexer::LEX_FEOF:
228                         finished = true;
229                         break;
230                 default:
231                         string const cename = lex.getString();
232                         LYXERR(Debug::TCLASS, "Engine name: " << cename);
233                         if (!lex.next())
234                                 break;
235                         string const fname = lex.getString();
236                         LYXERR(Debug::TCLASS, "Filename: " << fname);
237                         if (!lex.next(true))
238                                 break;
239                         string cet = lex.getString();
240                         LYXERR(Debug::TCLASS, "Engine Type: " << cet);
241                         vector<string> cets;
242                         while (!cet.empty()) {
243                                 string p;
244                                 cet = split(cet, p, '|');
245                                 cets.push_back(p);
246                         }
247                         if (!lex.next(true))
248                                 break;
249                         string const citeframework = lex.getString();
250                         LYXERR(Debug::TCLASS, "CiteFramework: " << citeframework);
251                         if (!lex.next(true))
252                                 break;
253                         string db = lex.getString();
254                         LYXERR(Debug::TCLASS, "Default Biblio: " << db);
255                         vector<string> dbs;
256                         while (!db.empty()) {
257                                 string p;
258                                 db = split(db, p, '|');
259                                 dbs.push_back(p);
260                         }
261                         if (!lex.next(true))
262                                 break;
263                         string const desc = lex.getString();
264                         LYXERR(Debug::TCLASS, "Description: " << desc);
265                         //FIXME Add packages
266                         if (!lex.next())
267                                 break;
268                         string str = lex.getString();
269                         LYXERR(Debug::TCLASS, "Packages: " << str);
270                         vector<string> pkgs;
271                         while (!str.empty()) {
272                                 string p;
273                                 str = split(str, p, ',');
274                                 pkgs.push_back(p);
275                         }
276                         if (!lex.next())
277                                 break;
278                         str = lex.getString();
279                         LYXERR(Debug::TCLASS, "Required: " << str);
280                         vector<string> req;
281                         while (!str.empty()) {
282                                 string p;
283                                 str = split(str, p, '|');
284                                 req.push_back(p);
285                         }
286                         if (!lex.next())
287                                 break;
288                         str = lex.getString();
289                         LYXERR(Debug::TCLASS, "Excluded: " << str);
290                         vector<string> exc;
291                         while (!str.empty()) {
292                                 string p;
293                                 str = split(str, p, '|');
294                                 exc.push_back(p);
295                         }
296                         // This code is run when we have
297                         // cename, fname, desc, pkgs, req and exc
298                         addCiteEngine(cename, fname, cets, citeframework, dbs, desc, pkgs, req, exc);
299                 } // end switch
300         } //end while
301
302         LYXERR(Debug::TCLASS, "End of parsing of lyxciteengines.lst");
303
304         if (!theCiteEnginesList.empty())
305                 sort(theCiteEnginesList.begin(), theCiteEnginesList.end(), EngineSorter());
306         return true;
307 }
308
309
310 void CiteEnginesList::addCiteEngine(string const & cename,
311         string const & filename, vector<string> const & cets,
312         string const & citeframework, vector<string> const & dbs,
313         string const & description, vector<string> const & pkgs,
314         vector<string> const & req, vector<string> const & exc)
315 {
316         LyXCiteEngine ce(cename, filename, cets, citeframework, dbs, description, pkgs, req, exc);
317         englist_.push_back(ce);
318 }
319
320
321 LyXCiteEnginesList::const_iterator CiteEnginesList::begin() const
322 {
323         return englist_.begin();
324 }
325
326
327 LyXCiteEnginesList::iterator CiteEnginesList::begin()
328 {
329         return englist_.begin();
330 }
331
332
333 LyXCiteEnginesList::const_iterator CiteEnginesList::end() const
334 {
335         return englist_.end();
336 }
337
338
339 LyXCiteEnginesList::iterator CiteEnginesList::end()
340 {
341         return englist_.end();
342 }
343
344
345 LyXCiteEngine const * CiteEnginesList::operator[](string const & str) const
346 {
347         LyXCiteEnginesList::const_iterator it = englist_.begin();
348         for (; it != englist_.end(); ++it)
349                 if (it->getID() == str) {
350                         LyXCiteEngine const & eng = *it;
351                         return &eng;
352                 }
353         return 0;
354 }
355
356
357 LyXCiteEngine * CiteEnginesList::operator[](string const & str)
358 {
359         LyXCiteEnginesList::iterator it = englist_.begin();
360         for (; it != englist_.end(); ++it)
361                 if (it->getID() == str) {
362                         LyXCiteEngine & eng = *it;
363                         return &eng;
364                 }
365         return 0;
366 }
367
368 } // namespace lyx