]> git.lyx.org Git - lyx.git/blob - src/ModuleList.cpp
Embedding: saving inzip name to .lyx file so that embedded files can always be found...
[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 "support/debug.h"
17 #include "LaTeXFeatures.h"
18 #include "Lexer.h"
19
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 & f, 
38                            string const & d, vector<string> const & p,
39                            vector<string> const & r, vector<string> const & e):
40         name(n), filename(f), description(d), 
41         packageList(p), requiredModules(r), excludedModules(e),
42         checked(false)
43 {}
44
45
46 bool LyXModule::isAvailable() {
47         if (packageList.empty())
48                 return true;
49         if (checked)
50                 return available;
51         checked = true;
52         //check whether all of the required packages are available
53         vector<string>::const_iterator it  = packageList.begin();
54         vector<string>::const_iterator end = packageList.end(); 
55         for (; it != end; ++it) {
56                 if (!LaTeXFeatures::isAvailable(*it)) {
57                         available = false;
58                         return available;
59                 }
60         }
61         available = true;
62         return available;
63 }
64
65
66 // used when sorting the module list.
67 class ModuleSorter
68 {
69 public:
70         int operator()(LyXModule const & lm1, LyXModule const & lm2) const
71         {
72                 return lm1.getName() < lm2.getName();
73         }
74 };
75
76
77 //Much of this is borrowed from TextClassList::read()
78 bool ModuleList::load()
79 {
80         FileName const real_file = libFileSearch("", "lyxmodules.lst");
81         LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
82
83         if (real_file.empty()) {
84                 lyxerr << "ModuleList::load(): unable to find "
85                                 "modules file  `"
86                                 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
87                                 << "'.\nNo modules will be available." << endl;
88                 return false;
89         }
90
91         Lexer lex(0, 0);
92         if (!lex.setFile(real_file)) {
93                 lyxerr << "ModuleList::load():"
94                                 "lyxlex was not able to set file: "
95                                 << real_file << ".\nNo modules will be available." << endl;
96                 return false;
97         }
98
99         if (!lex.isOK()) {
100                 lyxerr << "ModuleList::load():" <<
101                                 "unable to open modules file  `"
102                                 << to_utf8(makeDisplayPath(real_file.absFilename(), 1000))
103                                 << "'\nNo modules will be available."
104                                 << endl;
105                 return false;
106         }
107
108         bool finished = false;
109         // Parse modules files
110         LYXERR(Debug::TCLASS, "Starting parsing of lyxmodules.lst");
111         while (lex.isOK() && !finished) {
112                 LYXERR(Debug::TCLASS, "\tline by line");
113                 switch (lex.lex()) {
114                 case Lexer::LEX_FEOF:
115                         finished = true;
116                         break;
117                 default:
118                         string const modName = lex.getString();
119                         LYXERR(Debug::TCLASS, "Module name: " << modName);
120                         if (!lex.next())
121                                 break;
122                         string const fname = lex.getString();
123                         LYXERR(Debug::TCLASS, "Filename: " << fname);
124                         if (!lex.next())
125                                 break;
126                         string const desc = lex.getString();
127                         LYXERR(Debug::TCLASS, "Description: " << desc);
128                         //FIXME Add packages
129                         if (!lex.next())
130                                 break;
131                         string str = lex.getString();
132                         LYXERR(Debug::TCLASS, "Packages: " << str);
133                         vector<string> pkgs;
134                         while (!str.empty()) {
135                                 string p;
136                                 str = split(str, p, ',');
137                                 pkgs.push_back(p);
138                         }
139                         if (!lex.next())
140                                 break;
141                         str = lex.getString();
142                         LYXERR(Debug::TCLASS, "Required: " << str);
143                         vector<string> req;
144                         while (!str.empty()) {
145                                 string p;
146                                 str = split(str, p, '|');
147                                 req.push_back(p);
148                         }
149                         if (!lex.next())
150                                 break;
151                         str = lex.getString();
152                         LYXERR(Debug::TCLASS, "Excluded: " << str);
153                         vector<string> exc;
154                         while (!str.empty()) {
155                                 string p;
156                                 str = split(str, p, '|');
157                                 exc.push_back(p);
158                         }
159                         // This code is run when we have
160                         // modName, fname, desc, pkgs, req, and exc
161                         addLayoutModule(modName, fname, desc, pkgs, req, exc);
162                 } // end switch
163         } //end while
164         
165         LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
166
167         if (!moduleList.empty())
168                 sort(moduleList.begin(), moduleList.end(), ModuleSorter());
169         return true;
170 }
171
172
173 void ModuleList::addLayoutModule(string const & moduleName, 
174         string const & filename, string const & description,
175         vector<string> const & pkgs, vector<string> const & req,
176         vector<string> const & exc)
177 {
178         LyXModule lm(moduleName, filename, description, pkgs, req, exc);
179         modlist_.push_back(lm);
180 }
181
182
183 LyXModuleList::const_iterator ModuleList::begin() const
184 {
185         return modlist_.begin();
186 }
187
188
189 LyXModuleList::iterator ModuleList::begin()
190 {
191         return modlist_.begin();
192 }
193
194
195 LyXModuleList::const_iterator ModuleList::end() const
196 {
197         return modlist_.end();
198 }
199
200
201 LyXModuleList::iterator ModuleList::end()
202 {
203         return modlist_.end();
204 }
205
206
207 LyXModule * ModuleList::operator[](string const & str)
208 {
209         LyXModuleList::iterator it = modlist_.begin();
210         for (; it != modlist_.end(); ++it)
211                 if (it->getName() == str) {
212                         LyXModule & mod = *it;
213                         return &mod;
214                 }
215         return 0;
216 }
217
218 } // namespace lyx