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