diff options
Diffstat (limited to 'src/CommandParser.cc')
-rw-r--r-- | src/CommandParser.cc | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/CommandParser.cc b/src/CommandParser.cc new file mode 100644 index 0000000..baa5e7a --- /dev/null +++ b/src/CommandParser.cc | |||
@@ -0,0 +1,116 @@ | |||
1 | // CommandParser.cc for Fluxbox - an X11 Window manager | ||
2 | // Copyright (c) 2003 Henrik Kinnunen (fluxgen{<a*t>}users.sourceforge.net) | ||
3 | // and Simon Bowden (rathnor at users.sourceforge.net) | ||
4 | // | ||
5 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
6 | // copy of this software and associated documentation files (the "Software"), | ||
7 | // to deal in the Software without restriction, including without limitation | ||
8 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
9 | // and/or sell copies of the Software, and to permit persons to whom the | ||
10 | // Software is furnished to do so, subject to the following conditions: | ||
11 | // | ||
12 | // The above copyright notice and this permission notice shall be included in | ||
13 | // all copies or substantial portions of the Software. | ||
14 | // | ||
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
21 | // DEALINGS IN THE SOFTWARE. | ||
22 | |||
23 | // $Id: CommandParser.cc,v 1.4 2003/08/11 13:47:51 fluxgen Exp $ | ||
24 | |||
25 | #include "CommandParser.hh" | ||
26 | |||
27 | #include "StringUtil.hh" | ||
28 | |||
29 | #include <vector> | ||
30 | using namespace std; | ||
31 | |||
32 | namespace { | ||
33 | |||
34 | string::size_type removeFirstWhitespace(std::string &str) { | ||
35 | string::size_type first_pos = str.find_first_not_of(" \t"); | ||
36 | if (first_pos != string::npos) | ||
37 | str.erase(0, first_pos); | ||
38 | return first_pos; | ||
39 | } | ||
40 | |||
41 | }; // end anonymous namespace | ||
42 | |||
43 | CommandFactory::CommandFactory() { | ||
44 | |||
45 | } | ||
46 | |||
47 | CommandFactory::~CommandFactory() { | ||
48 | // remove all associations with this factory | ||
49 | CommandParser::instance().removeAssociation(*this); | ||
50 | } | ||
51 | |||
52 | void CommandFactory::addCommand(const std::string &command_name) { | ||
53 | CommandParser::instance().associateCommand(command_name, *this); | ||
54 | } | ||
55 | |||
56 | CommandParser &CommandParser::instance() { | ||
57 | static CommandParser singleton; | ||
58 | return singleton; | ||
59 | } | ||
60 | |||
61 | FbTk::Command *CommandParser::parseLine(const std::string &line) { | ||
62 | |||
63 | // parse arguments and command | ||
64 | string command = line; | ||
65 | string arguments; | ||
66 | string::size_type first_pos = removeFirstWhitespace(command); | ||
67 | string::size_type second_pos = command.find_first_of(" \t", first_pos); | ||
68 | if (second_pos != string::npos) { | ||
69 | // ok we have arguments, parsing them here | ||
70 | arguments = command.substr(second_pos); | ||
71 | removeFirstWhitespace(arguments); | ||
72 | command.erase(second_pos); // remove argument from command | ||
73 | } | ||
74 | |||
75 | // now we have parsed command and arguments | ||
76 | |||
77 | command = FbTk::StringUtil::toLower(command); | ||
78 | |||
79 | // we didn't find any matching command in default commands, | ||
80 | // so we search in the command creators modules for a | ||
81 | // matching command string | ||
82 | return toCommand(command, arguments); | ||
83 | |||
84 | } | ||
85 | |||
86 | FbTk::Command *CommandParser::toCommand(const std::string &command_str, const std::string &arguments) { | ||
87 | if (m_commandfactorys[command_str] != 0) | ||
88 | return m_commandfactorys[command_str]->stringToCommand(command_str, arguments); | ||
89 | |||
90 | return 0; | ||
91 | } | ||
92 | |||
93 | void CommandParser::associateCommand(const std::string &command, CommandFactory &factory) { | ||
94 | // we shouldnt override other commands | ||
95 | if (m_commandfactorys[command] != 0) | ||
96 | return; | ||
97 | |||
98 | m_commandfactorys[command] = &factory; | ||
99 | } | ||
100 | |||
101 | void CommandParser::removeAssociation(CommandFactory &factory) { | ||
102 | // commands that are associated with the factory | ||
103 | std::vector<std::string> commands; | ||
104 | // find associations | ||
105 | CommandFactoryMap::iterator factory_it = m_commandfactorys.begin(); | ||
106 | const CommandFactoryMap::iterator factory_it_end = m_commandfactorys.end(); | ||
107 | for (; factory_it != factory_it_end; ++factory_it) { | ||
108 | if ((*factory_it).second == &factory) | ||
109 | commands.push_back((*factory_it).first); | ||
110 | } | ||
111 | // remove all associations | ||
112 | while (!commands.empty()) { | ||
113 | m_commandfactorys.erase(commands.back()); | ||
114 | commands.pop_back(); | ||
115 | } | ||
116 | } | ||