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