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