]> git.lyx.org Git - features.git/blob - src/CiteEnginesList.cpp
0e3f45543d3bd970d99ff904d3cf3d271eafedd2
[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, 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)
47 {
48         filename_ = id_ + ".citeengine";
49 }
50
51
52 vector<string> LyXCiteEngine::prerequisites() const
53 {
54         if (!checked_)
55                 isAvailable();
56         return prerequisites_;
57 }
58
59
60 bool LyXCiteEngine::isAvailable() const
61 {
62         if (package_list_.empty())
63                 return true;
64         if (checked_)
65                 return available_;
66         checked_ = true;
67         available_ = true;
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)) {
73                         available_ = false;
74                         prerequisites_.push_back(*it);
75                 }
76         }
77         return available_;
78 }
79
80
81 bool LyXCiteEngine::hasEngineType(CiteEngineType const & et) const
82 {
83         return std::find(engine_types_.begin(), engine_types_.end(),
84                          theCiteEnginesList.getTypeAsString(et)) != engine_types_.end();
85 }
86
87
88 bool LyXCiteEngine::isCompatible(string const & cename) const
89 {
90         // do we exclude it?
91         if (find(excluded_engines_.begin(), excluded_engines_.end(), cename) !=
92                         excluded_engines_.end())
93                 return false;
94
95         LyXCiteEngine const * const lm = theCiteEnginesList[cename];
96         if (!lm)
97                 return true;
98
99         // does it exclude us?
100         vector<string> const excengs = lm->getExcludedEngines();
101         if (find(excengs.begin(), excengs.end(), id_) != excengs.end())
102                 return false;
103
104         return true;
105 }
106
107
108 bool LyXCiteEngine::areCompatible(string const & eng1, string const & eng2)
109 {
110         LyXCiteEngine const * const lm1 = theCiteEnginesList[eng1];
111         if (lm1)
112                 return lm1->isCompatible(eng2);
113         LyXCiteEngine const * const lm2 = theCiteEnginesList[eng2];
114         if (lm2)
115                 return lm2->isCompatible(eng1);
116         // Can't check it either way.
117         return true;
118 }
119
120
121 string LyXCiteEngine::getDefaultBiblio(CiteEngineType const & cet) const
122 {
123         string res;
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))
131                         res = split(s, ':');
132                 else if (!contains(s, ':') && res.empty())
133                         res = s;
134         }
135         return res;
136 }
137
138
139 bool LyXCiteEngine::isDefaultBiblio(string const & bf) const
140 {
141         if (find(default_biblios_.begin(), default_biblios_.end(), bf) != default_biblios_.end())
142                 return true;
143         string const bfp = ":" + bf;
144         return find(default_biblios_.begin(), default_biblios_.end(), bfp) != default_biblios_.end();
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 db = lex.getString();
250                         LYXERR(Debug::TCLASS, "Default Biblio: " << db);
251                         vector<string> dbs;
252                         while (!db.empty()) {
253                                 string p;
254                                 db = split(db, p, '|');
255                                 dbs.push_back(p);
256                         }
257                         if (!lex.next(true))
258                                 break;
259                         string const desc = lex.getString();
260                         LYXERR(Debug::TCLASS, "Description: " << desc);
261                         //FIXME Add packages
262                         if (!lex.next())
263                                 break;
264                         string str = lex.getString();
265                         LYXERR(Debug::TCLASS, "Packages: " << str);
266                         vector<string> pkgs;
267                         while (!str.empty()) {
268                                 string p;
269                                 str = split(str, p, ',');
270                                 pkgs.push_back(p);
271                         }
272                         if (!lex.next())
273                                 break;
274                         str = lex.getString();
275                         LYXERR(Debug::TCLASS, "Required: " << str);
276                         vector<string> req;
277                         while (!str.empty()) {
278                                 string p;
279                                 str = split(str, p, '|');
280                                 req.push_back(p);
281                         }
282                         if (!lex.next())
283                                 break;
284                         str = lex.getString();
285                         LYXERR(Debug::TCLASS, "Excluded: " << str);
286                         vector<string> exc;
287                         while (!str.empty()) {
288                                 string p;
289                                 str = split(str, p, '|');
290                                 exc.push_back(p);
291                         }
292                         // This code is run when we have
293                         // cename, fname, desc, pkgs, req and exc
294                         addCiteEngine(cename, fname, cets, dbs, desc, pkgs, req, exc);
295                 } // end switch
296         } //end while
297
298         LYXERR(Debug::TCLASS, "End of parsing of lyxciteengines.lst");
299
300         if (!theCiteEnginesList.empty())
301                 sort(theCiteEnginesList.begin(), theCiteEnginesList.end(), EngineSorter());
302         return true;
303 }
304
305
306 void CiteEnginesList::addCiteEngine(string const & cename,
307         string const & filename, vector<string> const & cets,
308         vector<string> const & dbs, string const & description,
309         vector<string> const & pkgs, vector<string> const & req,
310         vector<string> const & exc)
311 {
312         LyXCiteEngine ce(cename, filename, cets, dbs, description, pkgs, req, exc);
313         englist_.push_back(ce);
314 }
315
316
317 LyXCiteEnginesList::const_iterator CiteEnginesList::begin() const
318 {
319         return englist_.begin();
320 }
321
322
323 LyXCiteEnginesList::iterator CiteEnginesList::begin()
324 {
325         return englist_.begin();
326 }
327
328
329 LyXCiteEnginesList::const_iterator CiteEnginesList::end() const
330 {
331         return englist_.end();
332 }
333
334
335 LyXCiteEnginesList::iterator CiteEnginesList::end()
336 {
337         return englist_.end();
338 }
339
340
341 LyXCiteEngine const * CiteEnginesList::operator[](string const & str) const
342 {
343         LyXCiteEnginesList::const_iterator it = englist_.begin();
344         for (; it != englist_.end(); ++it)
345                 if (it->getID() == str) {
346                         LyXCiteEngine const & eng = *it;
347                         return &eng;
348                 }
349         return 0;
350 }
351
352
353 LyXCiteEngine * CiteEnginesList::operator[](string const & str)
354 {
355         LyXCiteEnginesList::iterator it = englist_.begin();
356         for (; it != englist_.end(); ++it)
357                 if (it->getID() == str) {
358                         LyXCiteEngine & eng = *it;
359                         return &eng;
360                 }
361         return 0;
362 }
363
364 } // namespace lyx