]> git.lyx.org Git - features.git/blob - src/ModuleList.cpp
remove unneeded includes.
[features.git] / src / ModuleList.cpp
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.
6  *
7  * \author Richard Heck
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ModuleList.h"
15
16 #include "LaTeXFeatures.h"
17 #include "Lexer.h"
18
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h"
22 #include "support/lstrings.h"
23
24 #include <algorithm>
25         
26 using namespace std;
27 using namespace lyx::support;
28
29 namespace lyx {
30
31
32 //global variable: module list
33 ModuleList moduleList;
34
35
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)
42 {
43         filename = id + ".module";
44 }
45
46
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;
60                 }
61         }
62         available = true;
63         return available;
64 }
65
66
67 // used when sorting the module list.
68 class ModuleSorter
69 {
70 public:
71         int operator()(LyXModule const & lm1, LyXModule const & lm2) const
72         {
73                 return lm1.getName() < lm2.getName();
74         }
75 };
76
77
78 //Much of this is borrowed from LayoutFileList::read()
79 bool ModuleList::load()
80 {
81         FileName const real_file = libFileSearch("", "lyxmodules.lst");
82         LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
83
84         if (real_file.empty()) {
85                 lyxerr << "ModuleList::load(): unable to find "
86                                 "modules file  `"
87                                 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
88                                 << "'.\nNo modules will be available." << endl;
89                 return false;
90         }
91
92         Lexer lex;
93         if (!lex.setFile(real_file)) {
94                 lyxerr << "ModuleList::load():"
95                                 "lyxlex was not able to set file: "
96                                 << real_file << ".\nNo modules will be available." << endl;
97                 return false;
98         }
99
100         if (!lex.isOK()) {
101                 lyxerr << "ModuleList::load():" <<
102                                 "unable to open modules file  `"
103                                 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
104                                 << "'\nNo modules will be available."
105                                 << endl;
106                 return false;
107         }
108
109         bool finished = false;
110         // Parse modules files
111         LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
112         while (lex.isOK() && !finished) {
113                 LYXERR(Debug::TCLASS, "\tline by line");
114                 switch (lex.lex()) {
115                 case Lexer::LEX_FEOF:
116                         finished = true;
117                         break;
118                 default:
119                         string const modName = lex.getString();
120                         LYXERR(Debug::TCLASS, "Module name: " << modName);
121                         if (!lex.next())
122                                 break;
123                         string const fname = lex.getString();
124                         LYXERR(Debug::TCLASS, "Filename: " << fname);
125                         if (!lex.next())
126                                 break;
127                         string const desc = lex.getString();
128                         LYXERR(Debug::TCLASS, "Description: " << desc);
129                         //FIXME Add packages
130                         if (!lex.next())
131                                 break;
132                         string str = lex.getString();
133                         LYXERR(Debug::TCLASS, "Packages: " << str);
134                         vector<string> pkgs;
135                         while (!str.empty()) {
136                                 string p;
137                                 str = split(str, p, ',');
138                                 pkgs.push_back(p);
139                         }
140                         if (!lex.next())
141                                 break;
142                         str = lex.getString();
143                         LYXERR(Debug::TCLASS, "Required: " << str);
144                         vector<string> req;
145                         while (!str.empty()) {
146                                 string p;
147                                 str = split(str, p, '|');
148                                 req.push_back(p);
149                         }
150                         if (!lex.next())
151                                 break;
152                         str = lex.getString();
153                         LYXERR(Debug::TCLASS, "Excluded: " << str);
154                         vector<string> exc;
155                         while (!str.empty()) {
156                                 string p;
157                                 str = split(str, p, '|');
158                                 exc.push_back(p);
159                         }
160                         // This code is run when we have
161                         // modName, fname, desc, pkgs, req, and exc
162                         addLayoutModule(modName, fname, desc, pkgs, req, exc);
163                 } // end switch
164         } //end while
165         
166         LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
167
168         if (!moduleList.empty())
169                 sort(moduleList.begin(), moduleList.end(), ModuleSorter());
170         return true;
171 }
172
173
174 void ModuleList::addLayoutModule(string const & moduleName, 
175         string const & filename, string const & description,
176         vector<string> const & pkgs, vector<string> const & req,
177         vector<string> const & exc)
178 {
179         LyXModule lm(moduleName, filename, description, pkgs, req, exc);
180         modlist_.push_back(lm);
181 }
182
183
184 LyXModuleList::const_iterator ModuleList::begin() const
185 {
186         return modlist_.begin();
187 }
188
189
190 LyXModuleList::iterator ModuleList::begin()
191 {
192         return modlist_.begin();
193 }
194
195
196 LyXModuleList::const_iterator ModuleList::end() const
197 {
198         return modlist_.end();
199 }
200
201
202 LyXModuleList::iterator ModuleList::end()
203 {
204         return modlist_.end();
205 }
206
207
208 LyXModule * ModuleList::getModuleByName(string const & str)
209 {
210         LyXModuleList::iterator it = modlist_.begin();
211         for (; it != modlist_.end(); ++it)
212                 if (it->getName() == str) {
213                         LyXModule & mod = *it;
214                         return &mod;
215                 }
216         return 0;
217 }
218
219 LyXModule * ModuleList::operator[](string const & str)
220 {
221         LyXModuleList::iterator it = modlist_.begin();
222         for (; it != modlist_.end(); ++it)
223                 if (it->getID() == str) {
224                         LyXModule & mod = *it;
225                         return &mod;
226                 }
227         return 0;
228 }
229
230 } // namespace lyx