]> git.lyx.org Git - lyx.git/blob - src/ModuleList.cpp
'using namespace std' instead of 'using std::xxx'
[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
27 namespace lyx {
28
29 using support::FileName;
30 using support::libFileSearch;
31 using support::makeDisplayPath;
32
33 //global variable: module list
34 ModuleList moduleList;
35
36
37 // used when sorting the module list.
38 class ModuleSorter
39 {
40 public:
41         int operator()(LyXModule const & lm1, 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 {
51         support::FileName const real_file = libFileSearch("", "lyxmodules.lst");
52         LYXERR(Debug::TCLASS, "Reading modules from `" << real_file << '\'');
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");
82         while (lex.isOK() && !finished) {
83                 LYXERR(Debug::TCLASS, "\tline by line");
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);
91                         if (!lex.next())
92                                 break;
93                         string const fname = lex.getString();
94                         LYXERR(Debug::TCLASS, "Filename: " << fname);
95                         if (!lex.next())
96                                 break;
97                         string const desc = lex.getString();
98                         LYXERR(Debug::TCLASS, "Description: " << desc);
99                         //FIXME Add packages
100                         if (!lex.next())
101                                 break;
102                         string packages = lex.getString();
103                         LYXERR(Debug::TCLASS, "Packages: " << packages);
104                         vector<string> pkgs;
105                         while (!packages.empty()) {
106                                 string p;
107                                 packages = support::split(packages, p, ',');
108                                 pkgs.push_back(p);
109                         }
110                         // This code is run when we have
111                         // modName, fname, desc, and pkgs
112                         addLayoutModule(modName, fname, desc, pkgs);
113                 } // end switch
114         } //end while
115         
116         LYXERR(Debug::TCLASS, "End of parsing of lyxmodules.lst");
117
118         if (!moduleList.empty())
119                 std::sort(moduleList.begin(), moduleList.end(), ModuleSorter());
120         return true;
121 }
122
123
124 void ModuleList::addLayoutModule(string const & moduleName, 
125         string const & filename, string const & description,
126         vector<string> const & pkgs)
127 {
128         LyXModule lm;
129         lm.name = moduleName;
130         lm.filename = filename;
131         lm.description = description;
132         lm.packageList = pkgs;
133         modlist_.push_back(lm);
134 }
135
136
137 LyXModuleList::const_iterator ModuleList::begin() const
138 {
139         return modlist_.begin();
140 }
141
142
143 LyXModuleList::iterator ModuleList::begin()
144 {
145         return modlist_.begin();
146 }
147
148
149 LyXModuleList::const_iterator ModuleList::end() const
150 {
151         return modlist_.end();
152 }
153
154
155 LyXModuleList::iterator ModuleList::end()
156 {
157         return modlist_.end();
158 }
159
160
161 LyXModule * ModuleList::operator[](string const & str)
162 {
163         LyXModuleList::iterator it = modlist_.begin();
164         for (; it != modlist_.end(); ++it)
165                 if (it->name == str) {
166                         LyXModule & mod = *it;
167                         return &mod;
168                 }
169         return 0;
170 }
171
172 } // namespace lyx