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