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