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