]> git.lyx.org Git - features.git/blob - src/CmdDef.cpp
'using namespace std' instead of 'using std::xxx'
[features.git] / src / CmdDef.cpp
1 /**
2  * \file CmdDef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Bernhard Roider
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "CmdDef.h"
14
15 #include "LyXAction.h"
16 #include "Lexer.h"
17
18 #include "support/debug.h"
19 #include "support/filetools.h"
20 #include "support/lstrings.h"
21
22 #include <ostream>
23 #include <string>
24
25 using namespace std;
26
27 namespace lyx {
28
29 using support::FileName;
30 using support::i18nLibFileSearch;
31 using support::trim;
32
33
34 namespace {
35
36 enum CmdDefTags {
37         BN_DEFFILE,
38         BN_DEFINE
39 };
40
41 keyword_item cmdDefTags[] = {
42         { "\\def_file", BN_DEFFILE },
43         { "\\define", BN_DEFINE }
44 };
45
46 }
47
48
49 bool CmdDef::read(string const & def_file)
50 {
51         const int cmdDefCount = sizeof(cmdDefTags) / sizeof(keyword_item);
52
53         Lexer lexrc(cmdDefTags, cmdDefCount);
54         if (lyxerr.debugging(Debug::PARSER))
55                 lexrc.printTable(lyxerr);
56
57         FileName const tmp(i18nLibFileSearch("commands", def_file, "def"));
58         lexrc.setFile(tmp);
59         if (!lexrc.isOK()) {
60                 lyxerr << "CmdDef::read: cannot open def file:"
61                        << tmp << endl;
62                 return false;
63         }
64
65         bool error = false;
66         while (lexrc.isOK()) {
67                 switch (lexrc.lex()) {
68                 case Lexer::LEX_UNDEF:
69                         lexrc.printError("Unknown tag `$$Token'");
70                         error = true;
71                         continue;
72                 case Lexer::LEX_FEOF:
73                         continue;
74                 case BN_DEFINE:
75                 {
76                         string name, def;
77
78                         if (lexrc.next()) {
79                                 name = lexrc.getString();
80                         } else {
81                                 lexrc.printError("BN_DEFINE: Missing command name");
82                                 error = true;
83                                 break;
84                         }
85
86                         if (lexrc.next(true)) {
87                                 def = lexrc.getString();
88                         } else {
89                                 lexrc.printError("BN_DEFINE: missing command definition");
90                                 error = true;
91                                 break;
92                         }
93
94                         newCmdDefResult e = newCmdDef(name, def);
95                         switch (e) {
96                                 case CmdDefNameEmpty:
97                                         lexrc.printError("BN_DEFINE: Command name is empty");
98                                         error = true;
99                                         break;
100                                 case CmdDefExists:
101                                         lexrc.printError("BN_DEFINE: Command `" + name + "' already defined");
102                                         error = true;
103                                         break;
104                                 case CmdDefInvalid:
105                                         lexrc.printError("BN_DEFINE: Command definition for `" + name + "' is not valid");
106                                         error = true;
107                         }
108
109                         break;
110                 }
111                 case BN_DEFFILE:
112                         if (lexrc.next()) {
113                                 string const tmp(lexrc.getString());
114                                 error |= !read(tmp);
115                         } else {
116                                 lexrc.printError("BN_DEFFILE: Missing file name");
117                                 error = true;
118                                 break;
119
120                         }
121                         break;
122                 }
123         }
124
125         if (error)
126                 lyxerr << "CmdDef::read: error while reading def file:"
127                        << tmp << endl;
128         return !error;
129 }
130
131
132 bool CmdDef::lock(string const & name, FuncRequest & func)
133 {
134         if (cmdDefMap.empty()) {
135                 func = FuncRequest::unknown;
136                 return false;
137         }
138
139         string const name2 = trim(name);
140
141         if (lockSet.find(name2) != lockSet.end()) {
142                 func = FuncRequest::noaction;
143                 return false;
144         }
145
146         CmdDefMap::const_iterator pos = cmdDefMap.find(name2);
147
148         if (pos == cmdDefMap.end()) {
149                 func = FuncRequest::unknown;
150                 return false;
151         }
152
153         lockSet.insert(name2);
154         func = pos->second;
155         return true;
156 }
157
158
159 void CmdDef::release(string const & name)
160 {
161         string const name2 = trim(name);
162
163         lockSet.erase(name2);
164 }
165
166 CmdDef::newCmdDefResult CmdDef::newCmdDef(string const & name, 
167                                                                                   string const & def)
168 {
169         string const name2 = trim(name);
170
171         if (name2.empty()) 
172                 return CmdDefNameEmpty;
173
174         if (cmdDefMap.find(name) != cmdDefMap.end())
175                 return CmdDefExists;
176
177         FuncRequest     func = lyxaction.lookupFunc(def);
178         if (func.action == LFUN_NOACTION ||
179                 func.action == LFUN_UNKNOWN_ACTION) {
180                         return CmdDefInvalid;
181         }
182
183         cmdDefMap[name2] = func;
184
185         return CmdDefOk;
186 }
187
188
189 } // namespace lyx