aboutsummaryrefslogtreecommitdiff
path: root/src/CommandParser.cc
diff options
context:
space:
mode:
authorfluxgen <fluxgen>2003-06-30 14:44:43 (GMT)
committerfluxgen <fluxgen>2003-06-30 14:44:43 (GMT)
commitbbe279233a210fe8ef23b0d265421b8f18d2b358 (patch)
tree7a9458ebb3cc7582c008cf43bbdb2ed1b502b1ac /src/CommandParser.cc
parent0dc31b93a8538aac2a538737df52a92bf8e8c21a (diff)
downloadfluxbox_pavel-bbe279233a210fe8ef23b0d265421b8f18d2b358.zip
fluxbox_pavel-bbe279233a210fe8ef23b0d265421b8f18d2b358.tar.bz2
new command parser
Diffstat (limited to 'src/CommandParser.cc')
-rw-r--r--src/CommandParser.cc131
1 files changed, 131 insertions, 0 deletions
diff --git a/src/CommandParser.cc b/src/CommandParser.cc
new file mode 100644
index 0000000..dc2f297
--- /dev/null
+++ b/src/CommandParser.cc
@@ -0,0 +1,131 @@
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.1 2003/06/30 14:44:43 fluxgen Exp $
24
25#include "CommandParser.hh"
26
27#include "StringUtil.hh"
28
29#include <vector>
30#include <iostream>
31#include <iterator>
32using namespace std;
33namespace {
34
35string::size_type removeFirstWhitespace(std::string &str) {
36 string::size_type first_pos = str.find_first_not_of(" \t");
37 if (first_pos != string::npos)
38 str.erase(0, first_pos);
39 return first_pos;
40}
41
42}; // end anonymous namespace
43
44CommandFactory::CommandFactory() {
45
46}
47
48CommandFactory::~CommandFactory() {
49 // remove all associations with this factory
50 CommandParser::instance().removeAssociation(*this);
51}
52
53void CommandFactory::addCommand(const std::string &command_name) {
54 CommandParser::instance().associateCommand(command_name, *this);
55}
56
57CommandParser &CommandParser::instance() {
58 static CommandParser singleton;
59 return singleton;
60}
61
62FbTk::Command *CommandParser::parseLine(const std::string &line) {
63
64 // parse arguments and command
65 string command = line;
66 string arguments;
67 string::size_type first_pos = removeFirstWhitespace(command);
68 string::size_type second_pos = command.find_first_of(" \t", first_pos);
69 if (second_pos != string::npos) {
70 // ok we have arguments, parsing them here
71 arguments = command.substr(second_pos);
72 removeFirstWhitespace(arguments);
73 command.erase(second_pos); // remove argument from command
74 }
75
76 // now we have parsed command and arguments
77#ifdef DEBUG
78 cerr<<__FILE__<<"("<<__FUNCTION__<<"): command = ["<<
79 command<<"] arguments=["<<arguments<<"]"<<endl;
80#endif // DEBUG
81
82 FbTk::StringUtil::toLower(command);
83
84 // we didn't find any matching command in default commands,
85 // so we search in the command creators modules for a matching command string
86 return toCommand(command, arguments);
87
88}
89
90ostream &operator << (ostream &the_stream, const CommandParser::CommandFactoryMap::value_type &value) {
91 the_stream<<value.first;
92 return the_stream;
93}
94
95void CommandParser::showCommands(std::ostream &the_stream) const {
96 // copy command strings to stream
97 copy(m_commandfactorys.begin(),
98 m_commandfactorys.end(),
99 ostream_iterator<CommandFactoryMap::value_type>(the_stream, "\n"));
100}
101
102FbTk::Command *CommandParser::toCommand(const std::string &command_str, const std::string &arguments) {
103 if (m_commandfactorys[command_str] != 0)
104 return m_commandfactorys[command_str]->stringToCommand(command_str, arguments);
105
106 return 0;
107}
108
109void CommandParser::associateCommand(const std::string &command, CommandFactory &factory) {
110 // we shouldnt override other commands
111 if (m_commandfactorys[command] != 0)
112 return;
113
114 m_commandfactorys[command] = &factory;
115}
116
117void CommandParser::removeAssociation(CommandFactory &factory) {
118 std::vector<std::string> commands; // commands that are associated with the factory
119 // find associations
120 CommandFactoryMap::iterator factory_it = m_commandfactorys.begin();
121 const CommandFactoryMap::iterator factory_it_end = m_commandfactorys.end();
122 for (; factory_it != factory_it_end; ++factory_it) {
123 if ((*factory_it).second == &factory)
124 commands.push_back((*factory_it).first);
125 }
126 // remove all associations
127 while (!commands.empty()) {
128 m_commandfactorys.erase(commands.back());
129 commands.pop_back();
130 }
131}