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