Update NEWS preparing for alpha1
[lyx.git] / src / ModuleList.cpp
blob14e2917159bddeeea7fb184ad714221ed823c6a5
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>
25 #include <ostream>
27 using namespace std;
28 using namespace lyx::support;
30 namespace lyx {
33 //global variable: module list
34 ModuleList moduleList;
37 LyXModule::LyXModule(string const & n, string const & i,
38 string const & d, vector<string> const & p,
39 vector<string> const & r, vector<string> const & e):
40 name(n), id(i), description(d),
41 packageList(p), requiredModules(r), excludedModules(e),
42 checked(false)
44 filename = id + ".module";
48 bool LyXModule::isAvailable() {
49 if (packageList.empty())
50 return true;
51 if (checked)
52 return available;
53 checked = true;
54 //check whether all of the required packages are available
55 vector<string>::const_iterator it = packageList.begin();
56 vector<string>::const_iterator end = packageList.end();
57 for (; it != end; ++it) {
58 if (!LaTeXFeatures::isAvailable(*it)) {
59 available = false;
60 return available;
63 available = true;
64 return available;
68 // used when sorting the module list.
69 class ModuleSorter
71 public:
72 int operator()(LyXModule const & lm1, LyXModule const & lm2) const
74 return lm1.getName() < lm2.getName();
79 //Much of this is borrowed from LayoutFileList::read()
80 bool ModuleList::load()
82 FileName const real_file = libFileSearch("", "lyxmodules.lst");
83 LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
85 if (real_file.empty()) {
86 lyxerr << "ModuleList::load(): unable to find "
87 "modules file `"
88 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
89 << "'.\nNo modules will be available." << endl;
90 return false;
93 Lexer lex(0, 0);
94 if (!lex.setFile(real_file)) {
95 lyxerr << "ModuleList::load():"
96 "lyxlex was not able to set file: "
97 << real_file << ".\nNo modules will be available." << endl;
98 return false;
101 if (!lex.isOK()) {
102 lyxerr << "ModuleList::load():" <<
103 "unable to open modules file `"
104 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
105 << "'\nNo modules will be available."
106 << endl;
107 return false;
110 bool finished = false;
111 // Parse modules files
112 LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
113 while (lex.isOK() && !finished) {
114 LYXERR(Debug::TCLASS, "\tline by line");
115 switch (lex.lex()) {
116 case Lexer::LEX_FEOF:
117 finished = true;
118 break;
119 default:
120 string const modName = lex.getString();
121 LYXERR(Debug::TCLASS, "Module name: " << modName);
122 if (!lex.next())
123 break;
124 string const fname = lex.getString();
125 LYXERR(Debug::TCLASS, "Filename: " << fname);
126 if (!lex.next())
127 break;
128 string const desc = lex.getString();
129 LYXERR(Debug::TCLASS, "Description: " << desc);
130 //FIXME Add packages
131 if (!lex.next())
132 break;
133 string str = lex.getString();
134 LYXERR(Debug::TCLASS, "Packages: " << str);
135 vector<string> pkgs;
136 while (!str.empty()) {
137 string p;
138 str = split(str, p, ',');
139 pkgs.push_back(p);
141 if (!lex.next())
142 break;
143 str = lex.getString();
144 LYXERR(Debug::TCLASS, "Required: " << str);
145 vector<string> req;
146 while (!str.empty()) {
147 string p;
148 str = split(str, p, '|');
149 req.push_back(p);
151 if (!lex.next())
152 break;
153 str = lex.getString();
154 LYXERR(Debug::TCLASS, "Excluded: " << str);
155 vector<string> exc;
156 while (!str.empty()) {
157 string p;
158 str = split(str, p, '|');
159 exc.push_back(p);
161 // This code is run when we have
162 // modName, fname, desc, pkgs, req, and exc
163 addLayoutModule(modName, fname, desc, pkgs, req, exc);
164 } // end switch
165 } //end while
167 LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
169 if (!moduleList.empty())
170 sort(moduleList.begin(), moduleList.end(), ModuleSorter());
171 return true;
175 void ModuleList::addLayoutModule(string const & moduleName,
176 string const & filename, string const & description,
177 vector<string> const & pkgs, vector<string> const & req,
178 vector<string> const & exc)
180 LyXModule lm(moduleName, filename, description, pkgs, req, exc);
181 modlist_.push_back(lm);
185 LyXModuleList::const_iterator ModuleList::begin() const
187 return modlist_.begin();
191 LyXModuleList::iterator ModuleList::begin()
193 return modlist_.begin();
197 LyXModuleList::const_iterator ModuleList::end() const
199 return modlist_.end();
203 LyXModuleList::iterator ModuleList::end()
205 return modlist_.end();
209 LyXModule * ModuleList::getModuleByName(string const & str)
211 LyXModuleList::iterator it = modlist_.begin();
212 for (; it != modlist_.end(); ++it)
213 if (it->getName() == str) {
214 LyXModule & mod = *it;
215 return &mod;
217 return 0;
220 LyXModule * ModuleList::operator[](string const & str)
222 LyXModuleList::iterator it = modlist_.begin();
223 for (; it != modlist_.end(); ++it)
224 if (it->getID() == str) {
225 LyXModule & mod = *it;
226 return &mod;
228 return 0;
231 } // namespace lyx