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