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