aboutsummaryrefslogtreecommitdiff
path: root/src/CommandParser.cc
diff options
context:
space:
mode:
authormarkt <markt>2007-12-13 05:48:00 (GMT)
committermarkt <markt>2007-12-13 05:48:00 (GMT)
commit8b7464046cea5e521ac46811591b0fce0c45aca1 (patch)
tree09df752f426a249ae15375a626a98436c8727593 /src/CommandParser.cc
parentdaca07edafc2e75eb9ee04d35fe80759308a8583 (diff)
downloadfluxbox-8b7464046cea5e521ac46811591b0fce0c45aca1.zip
fluxbox-8b7464046cea5e521ac46811591b0fce0c45aca1.tar.bz2
added FbTk::CommandRegistry, decentralized command parsing, and made them auto-register
Diffstat (limited to 'src/CommandParser.cc')
-rw-r--r--src/CommandParser.cc124
1 files changed, 0 insertions, 124 deletions
diff --git a/src/CommandParser.cc b/src/CommandParser.cc
deleted file mode 100644
index a1e52e5..0000000
--- a/src/CommandParser.cc
+++ /dev/null
@@ -1,124 +0,0 @@
1// CommandParser.cc for Fluxbox - an X11 Window manager
2// Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
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$
24
25#include "CommandParser.hh"
26#include "FbTk/StringUtil.hh"
27
28#include <vector>
29
30using std::string;
31using std::vector;
32using FbTk::StringUtil::removeFirstWhitespace;
33using FbTk::StringUtil::toLower;
34
35
36CommandParser *CommandParser::s_singleton = 0;
37
38CommandFactory::CommandFactory() {
39
40}
41
42CommandFactory::~CommandFactory() {
43 // remove all associations with this factory
44 CommandParser::instance().removeAssociation(*this);
45
46}
47
48void CommandFactory::addCommand(const std::string &command_name) {
49 CommandParser::instance().associateCommand(command_name, *this);
50}
51
52// ensure it is singleton
53CommandParser::CommandParser() {
54 if (s_singleton != 0)
55 throw std::string("CommandParser currently meant ot be singleton");
56}
57
58CommandParser &CommandParser::instance() {
59 if (s_singleton == 0)
60 s_singleton = new CommandParser();
61
62 return *s_singleton;
63}
64
65FbTk::Command *CommandParser::parseLine(const std::string &line, bool trusted) {
66
67 // parse arguments and command
68 string command = line;
69 string arguments;
70 string::size_type first_pos = removeFirstWhitespace(command);
71 FbTk::StringUtil::removeTrailingWhitespace(command);
72 string::size_type second_pos = command.find_first_of(" \t", first_pos);
73 if (second_pos != string::npos) {
74 // ok we have arguments, parsing them here
75 arguments = command.substr(second_pos);
76 removeFirstWhitespace(arguments);
77 command.erase(second_pos); // remove argument from command
78 }
79
80 // now we have parsed command and arguments
81 command = toLower(command);
82
83 // we didn't find any matching command in default commands,
84 // so we search in the command creators modules for a
85 // matching command string
86 return toCommand(command, arguments, trusted);
87
88}
89
90FbTk::Command *CommandParser::toCommand(const std::string &command_str,
91 const std::string &arguments, bool trusted) {
92 if (m_commandfactorys[command_str] != 0)
93 return m_commandfactorys[command_str]->stringToCommand(command_str, arguments, trusted);
94
95 return 0;
96}
97
98void CommandParser::associateCommand(const std::string &command, CommandFactory &factory) {
99 // we shouldnt override other commands
100 if (m_commandfactorys[command] != 0)
101 return;
102
103 m_commandfactorys[command] = &factory;
104}
105
106void CommandParser::removeAssociation(CommandFactory &factory) {
107 // commands that are associated with the factory
108 vector<string> commands;
109 // find associations
110 CommandFactoryMap::iterator factory_it = m_commandfactorys.begin();
111 const CommandFactoryMap::iterator factory_it_end = m_commandfactorys.end();
112 for (; factory_it != factory_it_end; ++factory_it) {
113 if ((*factory_it).second == &factory)
114 commands.push_back((*factory_it).first);
115 }
116 // remove all associations
117 while (!commands.empty()) {
118 m_commandfactorys.erase(commands.back());
119 commands.pop_back();
120 }
121
122 if (m_commandfactorys.empty())
123 delete s_singleton;
124}