]> git.lyx.org Git - lyx.git/blob - src/ModuleList.cpp
infrastructure for 'graceful asserts'
[lyx.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 #include <ostream>
26         
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32
33 //global variable: module list
34 ModuleList moduleList;
35
36
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)
43 {
44         filename = id + ".module";
45 }
46
47
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;
61                 }
62         }
63         available = true;
64         return available;
65 }
66
67
68 // used when sorting the module list.
69 class ModuleSorter
70 {
71 public:
72         int operator()(LyXModule const & lm1, LyXModule const & lm2) const
73         {
74                 return lm1.getName() < lm2.getName();
75         }
76 };
77
78
79 //Much of this is borrowed from LayoutFileList::read()
80 bool ModuleList::load()
81 {
82         FileName const real_file = libFileSearch("", "lyxmodules.lst");
83         LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
84
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;
91         }
92
93         Lexer lex;
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;
99         }
100
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;
108         }
109
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);
140                         }
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);
150                         }
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);
160                         }
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
166         
167         LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
168
169         if (!moduleList.empty())
170                 sort(moduleList.begin(), moduleList.end(), ModuleSorter());
171         return true;
172 }
173
174
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)
179 {
180         LyXModule lm(moduleName, filename, description, pkgs, req, exc);
181         modlist_.push_back(lm);
182 }
183
184
185 LyXModuleList::const_iterator ModuleList::begin() const
186 {
187         return modlist_.begin();
188 }
189
190
191 LyXModuleList::iterator ModuleList::begin()
192 {
193         return modlist_.begin();
194 }
195
196
197 LyXModuleList::const_iterator ModuleList::end() const
198 {
199         return modlist_.end();
200 }
201
202
203 LyXModuleList::iterator ModuleList::end()
204 {
205         return modlist_.end();
206 }
207
208
209 LyXModule * ModuleList::getModuleByName(string const & str)
210 {
211         LyXModuleList::iterator it = modlist_.begin();
212         for (; it != modlist_.end(); ++it)
213                 if (it->getName() == str) {
214                         LyXModule & mod = *it;
215                         return &mod;
216                 }
217         return 0;
218 }
219
220 LyXModule * ModuleList::operator[](string const & str)
221 {
222         LyXModuleList::iterator it = modlist_.begin();
223         for (; it != modlist_.end(); ++it)
224                 if (it->getID() == str) {
225                         LyXModule & mod = *it;
226                         return &mod;
227                 }
228         return 0;
229 }
230
231 } // namespace lyx