]> git.lyx.org Git - lyx.git/blob - src/CiteEnginesList.cpp
Merge branch 'master' into biblatex2
[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, 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)
46 {
47         filename_ = id_ + ".citeengine";
48 }
49
50
51 vector<string> LyXCiteEngine::prerequisites() const
52 {
53         if (!checked_)
54                 isAvailable();
55         return prerequisites_;
56 }
57
58
59 bool LyXCiteEngine::isAvailable() const
60 {
61         if (package_list_.empty())
62                 return true;
63         if (checked_)
64                 return available_;
65         checked_ = true;
66         available_ = true;
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)) {
72                         available_ = false;
73                         prerequisites_.push_back(*it);
74                 }
75         }
76         return available_;
77 }
78
79
80 bool LyXCiteEngine::hasEngineType(CiteEngineType const & et) const
81 {
82         return std::find(engine_types_.begin(), engine_types_.end(),
83                          theCiteEnginesList.getTypeAsString(et)) != engine_types_.end();
84 }
85
86
87 string LyXCiteEngine::getDefaultBiblio(CiteEngineType const & cet) const
88 {
89         string res;
90         string const etp = theCiteEnginesList.getTypeAsString(cet) + ":";
91         //check whether all of the required packages are available
92         for (string const &s: default_biblios_) {
93                 if (prefixIs(s, etp))
94                         res = split(s, ':');
95                 else if (!contains(s, ':') && res.empty())
96                         res = s;
97         }
98         return res;
99 }
100
101
102 bool LyXCiteEngine::isDefaultBiblio(string const & bf) const
103 {
104         string const bfs = ":" + bf;
105         for (string const &s: default_biblios_)
106                 if (suffixIs(s, bfs) || bf == s)
107                         return true;
108
109         return false;
110 }
111
112
113 // used when sorting the cite engine list.
114 class EngineSorter {
115 public:
116         int operator()(LyXCiteEngine const & ce1, LyXCiteEngine const & ce2) const
117         {
118                 return _(ce1.getName()) < _(ce2.getName());
119         }
120 };
121
122
123 // Local translators
124 namespace {
125
126 typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
127
128
129 CiteEngineTypeTranslator const init_citeenginetypetranslator()
130 {
131         CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
132         translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
133         translator.addPair("default", ENGINE_TYPE_DEFAULT);
134         return translator;
135 }
136
137
138 CiteEngineTypeTranslator const & citeenginetypetranslator()
139 {
140         static CiteEngineTypeTranslator const translator =
141                 init_citeenginetypetranslator();
142         return translator;
143 }
144
145 } // namespace anon
146
147
148 string CiteEnginesList::getTypeAsString(CiteEngineType const & et) const
149 {
150         return citeenginetypetranslator().find(et);
151 }
152
153
154 CiteEngineType CiteEnginesList::getType(string const & et) const
155 {
156         return citeenginetypetranslator().find(et);
157 }
158
159
160 // Much of this is borrowed from LayoutFileList::read()
161 bool CiteEnginesList::read()
162 {
163         FileName const real_file = libFileSearch("", "lyxciteengines.lst");
164         LYXERR(Debug::TCLASS, "Reading cite engines from `" << real_file << '\'');
165
166         if (real_file.empty()) {
167                 LYXERR0("unable to find cite engines file `citeengines.lst'.\n"
168                         << "No cite engines will be available.");
169                 return false;
170         }
171
172         Lexer lex;
173         if (!lex.setFile(real_file)) {
174                 LYXERR0("lyxlex was not able to set file: "
175                         << real_file << ".\nNo cite engines will be available.");
176                 return false;
177         }
178
179         if (!lex.isOK()) {
180                 LYXERR0("unable to open cite engines file  `"
181                         << to_utf8(makeDisplayPath(real_file.absFileName(), 1000))
182                         << "'\nNo cite engines will be available.");
183                 return false;
184         }
185
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");
191                 switch (lex.lex()) {
192                 case Lexer::LEX_FEOF:
193                         finished = true;
194                         break;
195                 default:
196                         string const cename = lex.getString();
197                         LYXERR(Debug::TCLASS, "Engine name: " << cename);
198                         if (!lex.next())
199                                 break;
200                         string const fname = lex.getString();
201                         LYXERR(Debug::TCLASS, "Filename: " << fname);
202                         if (!lex.next(true))
203                                 break;
204                         string cet = lex.getString();
205                         LYXERR(Debug::TCLASS, "Engine Type: " << cet);
206                         vector<string> cets;
207                         while (!cet.empty()) {
208                                 string p;
209                                 cet = split(cet, p, '|');
210                                 cets.push_back(p);
211                         }
212                         if (!lex.next(true))
213                                 break;
214                         string const citeframework = lex.getString();
215                         LYXERR(Debug::TCLASS, "CiteFramework: " << citeframework);
216                         if (!lex.next(true))
217                                 break;
218                         string db = lex.getString();
219                         LYXERR(Debug::TCLASS, "Default Biblio: " << db);
220                         vector<string> dbs;
221                         while (!db.empty()) {
222                                 string p;
223                                 db = split(db, p, '|');
224                                 dbs.push_back(p);
225                         }
226                         if (!lex.next(true))
227                                 break;
228                         string const desc = lex.getString();
229                         LYXERR(Debug::TCLASS, "Description: " << desc);
230                         //FIXME Add packages
231                         if (!lex.next())
232                                 break;
233                         string str = lex.getString();
234                         LYXERR(Debug::TCLASS, "Packages: " << str);
235                         vector<string> pkgs;
236                         while (!str.empty()) {
237                                 string p;
238                                 str = split(str, p, ',');
239                                 pkgs.push_back(p);
240                         }
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);
244                 } // end switch
245         } //end while
246
247         LYXERR(Debug::TCLASS, "End of parsing of lyxciteengines.lst");
248
249         if (!theCiteEnginesList.empty())
250                 sort(theCiteEnginesList.begin(), theCiteEnginesList.end(), EngineSorter());
251         return true;
252 }
253
254
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)
259 {
260         LyXCiteEngine ce(cename, filename, cets, citeframework, dbs, description, pkgs);
261         englist_.push_back(ce);
262 }
263
264
265 LyXCiteEnginesList::const_iterator CiteEnginesList::begin() const
266 {
267         return englist_.begin();
268 }
269
270
271 LyXCiteEnginesList::iterator CiteEnginesList::begin()
272 {
273         return englist_.begin();
274 }
275
276
277 LyXCiteEnginesList::const_iterator CiteEnginesList::end() const
278 {
279         return englist_.end();
280 }
281
282
283 LyXCiteEnginesList::iterator CiteEnginesList::end()
284 {
285         return englist_.end();
286 }
287
288
289 LyXCiteEngine const * CiteEnginesList::operator[](string const & str) const
290 {
291         LyXCiteEnginesList::const_iterator it = englist_.begin();
292         for (; it != englist_.end(); ++it)
293                 if (it->getID() == str) {
294                         LyXCiteEngine const & eng = *it;
295                         return &eng;
296                 }
297         return 0;
298 }
299
300
301 LyXCiteEngine * CiteEnginesList::operator[](string const & str)
302 {
303         LyXCiteEnginesList::iterator it = englist_.begin();
304         for (; it != englist_.end(); ++it)
305                 if (it->getID() == str) {
306                         LyXCiteEngine & eng = *it;
307                         return &eng;
308                 }
309         return 0;
310 }
311
312 } // namespace lyx