diff options
author | Mark Tiefenbruck <mark@fluxbox.org> | 2008-01-11 10:46:06 (GMT) |
---|---|---|
committer | Mark Tiefenbruck <mark@fluxbox.org> | 2008-01-11 10:46:06 (GMT) |
commit | 5b0806f1cb3d6fc84df731551db77025a5bacc32 (patch) | |
tree | 0b3d640c391ef620007c3a1fc551a4ea345238fd /src | |
parent | 9f2f65a698c4cc71373a7fe9d73a0889e0d3487b (diff) | |
download | fluxbox_paul-5b0806f1cb3d6fc84df731551db77025a5bacc32.zip fluxbox_paul-5b0806f1cb3d6fc84df731551db77025a5bacc32.tar.bz2 |
forgot to add this
Diffstat (limited to 'src')
-rw-r--r-- | src/FbTk/CommandParser.hh | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/src/FbTk/CommandParser.hh b/src/FbTk/CommandParser.hh new file mode 100644 index 0000000..89cac7d --- /dev/null +++ b/src/FbTk/CommandParser.hh | |||
@@ -0,0 +1,128 @@ | |||
1 | // CommandParser.hh 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 | #ifndef CommandParser_HH | ||
23 | #define CommandParser_HH | ||
24 | |||
25 | #include "ObjectRegistry.hh" | ||
26 | #include "StringUtil.hh" | ||
27 | |||
28 | using std::string; | ||
29 | |||
30 | namespace FbTk { | ||
31 | |||
32 | // helper for registering a function to parse arguments | ||
33 | #define REGISTER_COMMAND_PARSER(name, parser, type) \ | ||
34 | namespace { \ | ||
35 | static const bool p_register_command_##type_##name = FbTk::CommandParser<type>::instance().registerCommand(#name, parser); \ | ||
36 | } | ||
37 | |||
38 | // include some basic Command<void> creators | ||
39 | template <typename ClassName, typename Type> | ||
40 | Command<Type> *CommandCreator(const string &name, const string &args, | ||
41 | bool trusted) { | ||
42 | return new ClassName(); | ||
43 | } | ||
44 | |||
45 | #define REGISTER_COMMAND(name, classname, type) \ | ||
46 | namespace { \ | ||
47 | static const bool p_register_##type_##name = FbTk::CommandParser<type>::instance().registerCommand(#name, FbTk::CommandCreator<classname, type>); \ | ||
48 | } | ||
49 | |||
50 | template <typename ClassName, typename Type> | ||
51 | Command<Type> *CommandCreatorWithArgs(const string &name, const string &args, | ||
52 | bool trusted) { | ||
53 | return new ClassName(args); | ||
54 | } | ||
55 | |||
56 | #define REGISTER_COMMAND_WITH_ARGS(name, classname, type) \ | ||
57 | namespace { \ | ||
58 | static const bool p_register_##type_##name = FbTk::CommandParser<type>::instance().registerCommand(#name, FbTk::CommandCreatorWithArgs<classname, type>); \ | ||
59 | } | ||
60 | |||
61 | template <typename ClassName, typename Type> | ||
62 | Command<Type> *UntrustedCommandCreator(const string &name, const string &args, | ||
63 | bool trusted) { | ||
64 | if (!trusted) return 0; | ||
65 | return new ClassName(); | ||
66 | } | ||
67 | |||
68 | #define REGISTER_UNTRUSTED_COMMAND(name, classname, type) \ | ||
69 | namespace { \ | ||
70 | static const bool p_register_##type_##name = FbTk::CommandParser<type>::instance().registerCommand(#name, FbTk::UntrustedCommandCreator<classname, type>); \ | ||
71 | } | ||
72 | |||
73 | template <typename ClassName, typename Type> | ||
74 | Command<Type> *UntrustedCommandCreatorWithArgs(const string &name, | ||
75 | const string &args, bool trusted) { | ||
76 | if (!trusted) return 0; | ||
77 | return new ClassName(args); | ||
78 | } | ||
79 | |||
80 | #define REGISTER_UNTRUSTED_COMMAND_WITH_ARGS(name, classname, type) \ | ||
81 | namespace { \ | ||
82 | static const bool p_register_##type_##name = FbTk::CommandParser<type>::instance().registerCommand(#name, FbTk::UntrustedCommandCreatorWithArgs<classname, type>); \ | ||
83 | } | ||
84 | |||
85 | template <typename Type> | ||
86 | class CommandParser { | ||
87 | public: | ||
88 | typedef Command<Type> *Creator(const string &, const string &, bool); | ||
89 | |||
90 | static CommandParser<Type> &instance() { | ||
91 | static CommandParser<Type> s_instance; | ||
92 | return s_instance; | ||
93 | } | ||
94 | |||
95 | Command<Type> *parse(const string &name, const string &args, | ||
96 | bool trusted = true) const { | ||
97 | string lc = StringUtil::toLower(name); | ||
98 | Creator *creator = ObjectRegistry<Creator *>::instance().lookup(lc); | ||
99 | if (creator) | ||
100 | return creator(lc, args, trusted); | ||
101 | return 0; | ||
102 | } | ||
103 | |||
104 | Command<Type> *parse(const string &line, bool trusted = true) const { | ||
105 | // separate args and command | ||
106 | string command, args; | ||
107 | StringUtil::getFirstWord(line, command, args); | ||
108 | StringUtil::removeFirstWhitespace(args); | ||
109 | StringUtil::removeTrailingWhitespace(args); | ||
110 | |||
111 | // now we parse | ||
112 | return parse(command, args, trusted); | ||
113 | } | ||
114 | |||
115 | bool registerCommand(string name, Creator creator) { | ||
116 | name = StringUtil::toLower(name); | ||
117 | return ObjectRegistry<Creator *>::instance().registerObject(name, | ||
118 | creator); | ||
119 | } | ||
120 | |||
121 | private: | ||
122 | CommandParser() {} | ||
123 | ~CommandParser() {} | ||
124 | }; | ||
125 | |||
126 | } // end namespace FbTk | ||
127 | |||
128 | #endif // COMMANDPARSER_HH | ||