]> git.lyx.org Git - lyx.git/blob - src/CiteEnginesList.cpp
279063cafd37cf8d2c40f4db90fbfe605a17ce44
[lyx.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         for (string const &s: default_biblios_) {
127                 if (prefixIs(s, etp))
128                         res = split(s, ':');
129                 else if (!contains(s, ':') && res.empty())
130                         res = s;
131         }
132         return res;
133 }
134
135
136 bool LyXCiteEngine::isDefaultBiblio(string const & bf) const
137 {
138         string const bfs = ":" + bf;
139         for (string const &s: default_biblios_)
140                 if (suffixIs(s, bfs) || bf == s)
141                         return true;
142
143         return false;
144 }
145
146
147 // used when sorting the cite engine list.
148 class EngineSorter {
149 public:
150         int operator()(LyXCiteEngine const & ce1, LyXCiteEngine const & ce2) const
151         {
152                 return _(ce1.getName()) < _(ce2.getName());
153         }
154 };
155
156
157 // Local translators
158 namespace {
159
160 typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
161
162
163 CiteEngineTypeTranslator const init_citeenginetypetranslator()
164 {
165         CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
166         translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
167         translator.addPair("default", ENGINE_TYPE_DEFAULT);
168         return translator;
169 }
170
171
172 CiteEngineTypeTranslator const & citeenginetypetranslator()
173 {
174         static CiteEngineTypeTranslator const translator =
175                 init_citeenginetypetranslator();
176         return translator;
177 }
178
179 } // namespace anon
180
181
182 string CiteEnginesList::getTypeAsString(CiteEngineType const & et) const
183 {
184         return citeenginetypetranslator().find(et);
185 }
186
187
188 CiteEngineType CiteEnginesList::getType(string const & et) const
189 {
190         return citeenginetypetranslator().find(et);
191 }
192
193
194 // Much of this is borrowed from LayoutFileList::read()
195 bool CiteEnginesList::read()
196 {
197         FileName const real_file = libFileSearch("", "lyxciteengines.lst");
198         LYXERR(Debug::TCLASS, "Reading cite engines from `" << real_file << '\'');
199
200         if (real_file.empty()) {
201                 LYXERR0("unable to find cite engines file `citeengines.lst'.\n"
202                         << "No cite engines will be available.");
203                 return false;
204         }
205
206         Lexer lex;
207         if (!lex.setFile(real_file)) {
208                 LYXERR0("lyxlex was not able to set file: "
209                         << real_file << ".\nNo cite engines will be available.");
210                 return false;
211         }
212
213         if (!lex.isOK()) {
214                 LYXERR0("unable to open cite engines file  `"
215                         << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
216                         << "'\nNo cite engines will be available.");
217                 return false;
218         }
219
220         bool finished = false;
221         // Parse cite engines files
222         LYXERR(Debug::TCLASS, "Starting parsing of lyxciteengines.lst");
223         while (lex.isOK() && !finished) {
224                 LYXERR(Debug::TCLASS, "\tline by line");
225                 switch (lex.lex()) {
226                 case Lexer::LEX_FEOF:
227                         finished = true;
228                         break;
229                 default:
230                         string const cename = lex.getString();
231                         LYXERR(Debug::TCLASS, "Engine name: " << cename);
232                         if (!lex.next())
233                                 break;
234                         string const fname = lex.getString();
235                         LYXERR(Debug::TCLASS, "Filename: " << fname);
236                         if (!lex.next(true))
237                                 break;
238                         string cet = lex.getString();
239                         LYXERR(Debug::TCLASS, "Engine Type: " << cet);
240                         vector<string> cets;
241                         while (!cet.empty()) {
242                                 string p;
243                                 cet = split(cet, p, '|');
244                                 cets.push_back(p);
245                         }
246                         if (!lex.next(true))
247                                 break;
248                         string db = lex.getString();
249                         LYXERR(Debug::TCLASS, "Default Biblio: " << db);
250                         vector<string> dbs;
251                         while (!db.empty()) {
252                                 string p;
253                                 db = split(db, p, '|');
254                                 dbs.push_back(p);
255                         }
256                         if (!lex.next(true))
257                                 break;
258                         string const desc = lex.getString();
259                         LYXERR(Debug::TCLASS, "Description: " << desc);
260                         //FIXME Add packages
261                         if (!lex.next())
262                                 break;
263                         string str = lex.getString();
264                         LYXERR(Debug::TCLASS, "Packages: " << str);
265                         vector<string> pkgs;
266                         while (!str.empty()) {
267                                 string p;
268                                 str = split(str, p, ',');
269                                 pkgs.push_back(p);
270                         }
271                         if (!lex.next())
272                                 break;
273                         str = lex.getString();
274                         LYXERR(Debug::TCLASS, "Required: " << str);
275                         vector<string> req;
276                         while (!str.empty()) {
277                                 string p;
278                                 str = split(str, p, '|');
279                                 req.push_back(p);
280                         }
281                         if (!lex.next())
282                                 break;
283                         str = lex.getString();
284                         LYXERR(Debug::TCLASS, "Excluded: " << str);
285                         vector<string> exc;
286                         while (!str.empty()) {
287                                 string p;
288                                 str = split(str, p, '|');
289                                 exc.push_back(p);
290                         }
291                         // This code is run when we have
292                         // cename, fname, desc, pkgs, req and exc
293                         addCiteEngine(cename, fname, cets, dbs, desc, pkgs, req, exc);
294                 } // end switch
295         } //end while
296
297         LYXERR(Debug::TCLASS, "End of parsing of lyxciteengines.lst");
298
299         if (!theCiteEnginesList.empty())
300                 sort(theCiteEnginesList.begin(), theCiteEnginesList.end(), EngineSorter());
301         return true;
302 }
303
304
305 void CiteEnginesList::addCiteEngine(string const & cename,
306         string const & filename, vector<string> const & cets,
307         vector<string> const & dbs, string const & description,
308         vector<string> const & pkgs, vector<string> const & req,
309         vector<string> const & exc)
310 {
311         LyXCiteEngine ce(cename, filename, cets, dbs, description, pkgs, req, exc);
312         englist_.push_back(ce);
313 }
314
315
316 LyXCiteEnginesList::const_iterator CiteEnginesList::begin() const
317 {
318         return englist_.begin();
319 }
320
321
322 LyXCiteEnginesList::iterator CiteEnginesList::begin()
323 {
324         return englist_.begin();
325 }
326
327
328 LyXCiteEnginesList::const_iterator CiteEnginesList::end() const
329 {
330         return englist_.end();
331 }
332
333
334 LyXCiteEnginesList::iterator CiteEnginesList::end()
335 {
336         return englist_.end();
337 }
338
339
340 LyXCiteEngine const * CiteEnginesList::operator[](string const & str) const
341 {
342         LyXCiteEnginesList::const_iterator it = englist_.begin();
343         for (; it != englist_.end(); ++it)
344                 if (it->getID() == str) {
345                         LyXCiteEngine const & eng = *it;
346                         return &eng;
347                 }
348         return 0;
349 }
350
351
352 LyXCiteEngine * CiteEnginesList::operator[](string const & str)
353 {
354         LyXCiteEnginesList::iterator it = englist_.begin();
355         for (; it != englist_.end(); ++it)
356                 if (it->getID() == str) {
357                         LyXCiteEngine & eng = *it;
358                         return &eng;
359                 }
360         return 0;
361 }
362
363 } // namespace lyx