]> git.lyx.org Git - lyx.git/blob - src/LaTeXPackages.cpp
Fix -Winconsistent-missing-override
[lyx.git] / src / LaTeXPackages.cpp
1 /**
2  * \file LaTeXPackages.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Jürgen Vigna
10  * \author André Pönitz
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "LaTeXPackages.h"
18
19 #include "Lexer.h"
20
21 #include "support/convert.h"
22 #include "support/debug.h"
23 #include "support/FileName.h"
24 #include "support/filetools.h"
25 #include "support/lstrings.h"
26 #include "support/Package.h"
27
28
29 using namespace std;
30 using namespace lyx::support;
31
32
33 namespace lyx {
34
35 LaTeXPackages::Packages LaTeXPackages::packages_;
36
37
38 void LaTeXPackages::getAvailable()
39 {
40         Lexer lex;
41         support::FileName const real_file = libFileSearch("", "packages.lst");
42
43         if (real_file.empty())
44                 return;
45
46         lex.setFile(real_file);
47
48         if (!lex.isOK())
49                 return;
50
51         // Make sure that we are clean
52         packages_.clear();
53
54         bool finished = false;
55         string lstformat = "1";
56         // Parse config-file
57         while (lex.isOK() && !finished) {
58                 switch (lex.lex()) {
59                 case Lexer::LEX_FEOF:
60                         finished = true;
61                         break;
62                 default: {
63                         string const p = lex.getString();
64                         // Parse optional version info
65                         lex.eatLine();
66                         string const v = trim(lex.getString());
67                         if (p == "!!fileformat") {
68                                 lstformat = v;
69                                 continue;
70                         }
71                         packages_.insert(make_pair(p, v));
72                 }
73                 }
74         }
75         // Check if the pkglist has current format.
76         // Reconfigure and re-parse if not.
77         if (lstformat != "2") {
78                 package().reconfigureUserLyXDir("");
79                 getAvailable();
80         }
81 }
82
83
84 bool LaTeXPackages::isAvailable(string const & name)
85 {
86         if (packages_.empty())
87                 getAvailable();
88         string n = name;
89         if (suffixIs(n, ".sty"))
90                 n.erase(name.length() - 4);
91         for (auto const & package : packages_) {
92                 if (package.first == n)
93                         return true;
94         }
95         return false;
96 }
97
98
99 bool LaTeXPackages::isAvailableAtLeastFrom(string const & name,
100                                            int const y, int const m, int const d)
101 {
102         if (packages_.empty())
103                 getAvailable();
104
105         // required date as int (yyyymmdd)
106         int const req_date = (y * 10000) + (m * 100) + d;
107         for (auto const & package : packages_) {
108                 if (package.first == name && !package.second.empty()) {
109                         if (!isStrInt(package.second)) {
110                                 LYXERR0("Warning: Invalid date of package "
111                                         << package.first << " (" << package.second << ")");
112                                 continue;
113                         }
114                         // required date not newer than available date
115                         return req_date <= convert<int>(package.second);
116                 }
117         }
118         return false;
119 }
120
121 } // namespace lyx