]> git.lyx.org Git - lyx.git/blob - src/LaTeXPackages.cpp
tex2lyx: exit earlier if input file could not be found
[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
27
28 using namespace std;
29 using namespace lyx::support;
30
31
32 namespace lyx {
33
34 LaTeXPackages::Packages LaTeXPackages::packages_;
35
36
37 void LaTeXPackages::getAvailable()
38 {
39         Lexer lex;
40         support::FileName const real_file = libFileSearch("", "packages.lst");
41
42         if (real_file.empty())
43                 return;
44
45         lex.setFile(real_file);
46
47         if (!lex.isOK())
48                 return;
49
50         // Make sure that we are clean
51         packages_.clear();
52
53         bool finished = false;
54         // Parse config-file
55         while (lex.isOK() && !finished) {
56                 switch (lex.lex()) {
57                 case Lexer::LEX_FEOF:
58                         finished = true;
59                         break;
60                 default: {
61                         string const p = lex.getString();
62                         // Parse optional version info
63                         lex.eatLine();
64                         string const v = trim(lex.getString());
65                         packages_.insert(make_pair(p, v));
66                 }
67                 }
68         }
69 }
70
71
72 bool LaTeXPackages::isAvailable(string const & name)
73 {
74         if (packages_.empty())
75                 getAvailable();
76         string n = name;
77         if (suffixIs(n, ".sty"))
78                 n.erase(name.length() - 4);
79         for (auto const & package : packages_) {
80                 if (package.first == n)
81                         return true;
82         }
83         return false;
84 }
85
86
87 bool LaTeXPackages::isAvailableAtLeastFrom(string const & name,
88                                            int const y, int const m, int const d)
89 {
90         if (packages_.empty())
91                 getAvailable();
92
93         // required date as int (yyyymmdd)
94         int const req_date = (y * 10000) + (m * 100) + d;
95         for (auto const & package : packages_) {
96                 if (package.first == name && !package.second.empty()) {
97                         if (!isStrInt(package.second)) {
98                                 LYXERR0("Warning: Invalid date of package "
99                                         << package.first << " (" << package.second << ")");
100                                 continue;
101                         }
102                         // required date not newer than available date
103                         return req_date <= convert<int>(package.second);
104                 }
105         }
106         return false;
107 }
108
109 } // namespace lyx