]> git.lyx.org Git - lyx.git/blob - src/ModuleList.cpp
258b71c585f9cff1159019ca8d5e17881363ca90
[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 std::endl;
26 using std::map;
27 using std::string;
28 using std::vector;
29
30 namespace lyx {
31
32 using support::FileName;
33 using support::libFileSearch;
34 using support::makeDisplayPath;
35
36 //global variable: module list
37 ModuleList moduleList;
38
39
40 // used when sorting the module list.
41 class ModuleSorter
42 {
43 public:
44         int operator()(LyXModule const & lm1, LyXModule const & lm2) const
45         {
46                 return lm1.name < lm2.name;
47         }
48 };
49
50
51 //Much of this is borrowed from TextClassList::read()
52 bool ModuleList::load()
53 {
54         support::FileName const real_file = libFileSearch("", "lyxmodules.lst");
55         LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
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");
85         while (lex.isOK() && !finished) {
86                 LYXERR(Debug::TCLASS, "\tline by line");
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);
94                         if (!lex.next())
95                                 break;
96                         string const fname = lex.getString();
97                         LYXERR(Debug::TCLASS, "Filename: " << fname);
98                         if (!lex.next())
99                                 break;
100                         string const desc = lex.getString();
101                         LYXERR(Debug::TCLASS, "Description: " << desc);
102                         //FIXME Add packages
103                         if (!lex.next())
104                                 break;
105                         string packages = lex.getString();
106                         LYXERR(Debug::TCLASS, "Packages: " << packages);
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");
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