LyX 1.6.0 release candidate 4 (rc4)
[lyx.git] / src / ModuleList.cpp
blob547f1f922072a74a4c5c19ed4b84f222bf9de0a1
1 // -*- C++ -*-
2 /**
3 * \file ModuleList.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Richard Heck
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "ModuleList.h"
16 #include "LaTeXFeatures.h"
17 #include "Lexer.h"
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
24 #include <algorithm>
26 using namespace std;
27 using namespace lyx::support;
29 namespace lyx {
32 //global variable: module list
33 ModuleList moduleList;
36 LyXModule::LyXModule(string const & n, string const & i,
37 string const & d, vector<string> const & p,
38 vector<string> const & r, vector<string> const & e):
39 name(n), id(i), description(d),
40 packageList(p), requiredModules(r), excludedModules(e),
41 checked(false)
43 filename = id + ".module";
47 bool LyXModule::isAvailable() {
48 if (packageList.empty())
49 return true;
50 if (checked)
51 return available;
52 checked = true;
53 //check whether all of the required packages are available
54 vector<string>::const_iterator it = packageList.begin();
55 vector<string>::const_iterator end = packageList.end();
56 for (; it != end; ++it) {
57 if (!LaTeXFeatures::isAvailable(*it)) {
58 available = false;
59 return available;
62 available = true;
63 return available;
67 bool LyXModule::isCompatible(string const & modName) const
69 // do we exclude it?
70 if (find(excludedModules.begin(), excludedModules.end(), modName) !=
71 excludedModules.end())
72 return false;
74 LyXModule const * const lm = moduleList[modName];
75 if (!lm)
76 return true;
78 // does it exclude us?
79 vector<string> const excMods = lm->getExcludedModules();
80 if (find(excMods.begin(), excMods.end(), name) != excMods.end())
81 return false;
83 return true;
87 bool LyXModule::areCompatible(string const & mod1, string const & mod2)
89 LyXModule const * const lm1 = moduleList[mod1];
90 if (lm1)
91 return lm1->isCompatible(mod2);
92 LyXModule const * const lm2 = moduleList[mod2];
93 if (lm2)
94 return lm2->isCompatible(mod1);
95 // Can't check it either way.
96 return true;
99 // used when sorting the module list.
100 class ModuleSorter
102 public:
103 int operator()(LyXModule const & lm1, LyXModule const & lm2) const
105 return lm1.getName() < lm2.getName();
110 //Much of this is borrowed from LayoutFileList::read()
111 bool ModuleList::load()
113 FileName const real_file = libFileSearch("", "lyxmodules.lst");
114 LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
116 if (real_file.empty()) {
117 LYXERR0("unable to find modules file `"
118 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
119 << "'.\nNo modules will be available.");
120 return false;
123 Lexer lex;
124 if (!lex.setFile(real_file)) {
125 LYXERR0("lyxlex was not able to set file: "
126 << real_file << ".\nNo modules will be available.");
127 return false;
130 if (!lex.isOK()) {
131 LYXERR0("unable to open modules file `"
132 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
133 << "'\nNo modules will be available.");
134 return false;
137 bool finished = false;
138 // Parse modules files
139 LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
140 while (lex.isOK() && !finished) {
141 LYXERR(Debug::TCLASS, "\tline by line");
142 switch (lex.lex()) {
143 case Lexer::LEX_FEOF:
144 finished = true;
145 break;
146 default:
147 string const modName = lex.getString();
148 LYXERR(Debug::TCLASS, "Module name: " << modName);
149 if (!lex.next())
150 break;
151 string const fname = lex.getString();
152 LYXERR(Debug::TCLASS, "Filename: " << fname);
153 if (!lex.next())
154 break;
155 string const desc = lex.getString();
156 LYXERR(Debug::TCLASS, "Description: " << desc);
157 //FIXME Add packages
158 if (!lex.next())
159 break;
160 string str = lex.getString();
161 LYXERR(Debug::TCLASS, "Packages: " << str);
162 vector<string> pkgs;
163 while (!str.empty()) {
164 string p;
165 str = split(str, p, ',');
166 pkgs.push_back(p);
168 if (!lex.next())
169 break;
170 str = lex.getString();
171 LYXERR(Debug::TCLASS, "Required: " << str);
172 vector<string> req;
173 while (!str.empty()) {
174 string p;
175 str = split(str, p, '|');
176 req.push_back(p);
178 if (!lex.next())
179 break;
180 str = lex.getString();
181 LYXERR(Debug::TCLASS, "Excluded: " << str);
182 vector<string> exc;
183 while (!str.empty()) {
184 string p;
185 str = split(str, p, '|');
186 exc.push_back(p);
188 // This code is run when we have
189 // modName, fname, desc, pkgs, req, and exc
190 addLayoutModule(modName, fname, desc, pkgs, req, exc);
191 } // end switch
192 } //end while
194 LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
196 if (!moduleList.empty())
197 sort(moduleList.begin(), moduleList.end(), ModuleSorter());
198 return true;
202 void ModuleList::addLayoutModule(string const & moduleName,
203 string const & filename, string const & description,
204 vector<string> const & pkgs, vector<string> const & req,
205 vector<string> const & exc)
207 LyXModule lm(moduleName, filename, description, pkgs, req, exc);
208 modlist_.push_back(lm);
212 LyXModuleList::const_iterator ModuleList::begin() const
214 return modlist_.begin();
218 LyXModuleList::iterator ModuleList::begin()
220 return modlist_.begin();
224 LyXModuleList::const_iterator ModuleList::end() const
226 return modlist_.end();
230 LyXModuleList::iterator ModuleList::end()
232 return modlist_.end();
236 LyXModule * ModuleList::operator[](string const & str)
238 LyXModuleList::iterator it = modlist_.begin();
239 for (; it != modlist_.end(); ++it)
240 if (it->getID() == str) {
241 LyXModule & mod = *it;
242 return &mod;
244 return 0;
247 } // namespace lyx