diff options
author | markt <markt> | 2007-12-13 05:48:00 (GMT) |
---|---|---|
committer | markt <markt> | 2007-12-13 05:48:00 (GMT) |
commit | 8b7464046cea5e521ac46811591b0fce0c45aca1 (patch) | |
tree | 09df752f426a249ae15375a626a98436c8727593 /src/FbTk/CommandRegistry.cc | |
parent | daca07edafc2e75eb9ee04d35fe80759308a8583 (diff) | |
download | fluxbox-8b7464046cea5e521ac46811591b0fce0c45aca1.zip fluxbox-8b7464046cea5e521ac46811591b0fce0c45aca1.tar.bz2 |
added FbTk::CommandRegistry, decentralized command parsing, and made them auto-register
Diffstat (limited to 'src/FbTk/CommandRegistry.cc')
-rw-r--r-- | src/FbTk/CommandRegistry.cc | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/FbTk/CommandRegistry.cc b/src/FbTk/CommandRegistry.cc new file mode 100644 index 0000000..1d16c75 --- /dev/null +++ b/src/FbTk/CommandRegistry.cc | |||
@@ -0,0 +1,92 @@ | |||
1 | // CommandRegistry.cc for FbTk | ||
2 | // Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | // $Id: $ | ||
23 | |||
24 | #include "CommandRegistry.hh" | ||
25 | #include "Command.hh" | ||
26 | #include "StringUtil.hh" | ||
27 | |||
28 | #include <iostream> | ||
29 | |||
30 | namespace FbTk { | ||
31 | |||
32 | CommandRegistry &CommandRegistry::instance() { | ||
33 | static CommandRegistry s_instance; | ||
34 | return s_instance; | ||
35 | } | ||
36 | |||
37 | |||
38 | bool CommandRegistry::registerCommand(string name, | ||
39 | CreateFunction createFunction) { | ||
40 | name = StringUtil::toLower(name); | ||
41 | m_commandCreators[name] = createFunction; | ||
42 | return true; | ||
43 | } | ||
44 | |||
45 | bool CommandRegistry::registerBoolCommand(string name, | ||
46 | BoolCreateFunction createFunction) { | ||
47 | name = StringUtil::toLower(name); | ||
48 | m_boolcommandCreators[name] = createFunction; | ||
49 | return true; | ||
50 | } | ||
51 | |||
52 | Command *CommandRegistry::parseLine(const string &line, bool trusted) const { | ||
53 | // parse args and command | ||
54 | string command, args; | ||
55 | StringUtil::getFirstWord(line, command, args); | ||
56 | |||
57 | // now we have parsed command and args | ||
58 | command = StringUtil::toLower(command); | ||
59 | return getCommand(command, args, trusted); | ||
60 | } | ||
61 | |||
62 | BoolCommand *CommandRegistry::parseBoolLine(const string &line, bool trusted) const { | ||
63 | // parse args and command | ||
64 | string command, args; | ||
65 | StringUtil::getFirstWord(line, command, args); | ||
66 | |||
67 | // now we have parsed command and args | ||
68 | command = StringUtil::toLower(command); | ||
69 | return getBoolCommand(command, args, trusted); | ||
70 | } | ||
71 | |||
72 | Command *CommandRegistry::getCommand(const string &name, const string &args, | ||
73 | bool trusted) const { | ||
74 | string lc = StringUtil::toLower(name); | ||
75 | CreatorMap::const_iterator it = m_commandCreators.find(lc.c_str()); | ||
76 | if (it == m_commandCreators.end()) | ||
77 | return 0; | ||
78 | else | ||
79 | return it->second(name, args, trusted); | ||
80 | } | ||
81 | |||
82 | BoolCommand *CommandRegistry::getBoolCommand(const string &name, | ||
83 | const string &args, bool trusted) const { | ||
84 | string lc = StringUtil::toLower(name); | ||
85 | BoolCreatorMap::const_iterator it = m_boolcommandCreators.find(lc.c_str()); | ||
86 | if (it == m_boolcommandCreators.end()) | ||
87 | return 0; | ||
88 | else | ||
89 | return it->second(name, args, trusted); | ||
90 | } | ||
91 | |||
92 | }; // end namespace FbTk | ||