news download themes documentation links










CommandParser.cc

00001 // CommandParser.cc for Fluxbox - an X11 Window manager
00002 // Copyright (c) 2003 Henrik Kinnunen (fluxgen{<a*t>}users.sourceforge.net)
00003 //                and Simon Bowden (rathnor at users.sourceforge.net)
00004 //
00005 // Permission is hereby granted, free of charge, to any person obtaining a
00006 // copy of this software and associated documentation files (the "Software"),
00007 // to deal in the Software without restriction, including without limitation
00008 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
00009 // and/or sell copies of the Software, and to permit persons to whom the
00010 // Software is furnished to do so, subject to the following conditions:
00011 //
00012 // The above copyright notice and this permission notice shall be included in
00013 // all copies or substantial portions of the Software.
00014 //
00015 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00018 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00020 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00021 // DEALINGS IN THE SOFTWARE.
00022 
00023 // $Id: CommandParser.cc,v 1.4 2003/08/11 13:47:51 fluxgen Exp $
00024 
00025 #include "CommandParser.hh"
00026 
00027 #include "StringUtil.hh"
00028 
00029 #include <vector>
00030 using namespace std;
00031 
00032 namespace {
00033 
00034 string::size_type removeFirstWhitespace(std::string &str) {
00035     string::size_type first_pos = str.find_first_not_of(" \t");
00036     if (first_pos != string::npos)
00037         str.erase(0, first_pos);
00038     return first_pos;
00039 }
00040 
00041 }; // end anonymous namespace
00042 
00043 CommandFactory::CommandFactory() {
00044 
00045 }
00046 
00047 CommandFactory::~CommandFactory() {
00048     // remove all associations with this factory
00049     CommandParser::instance().removeAssociation(*this);
00050 }
00051 
00052 void CommandFactory::addCommand(const std::string &command_name) {
00053     CommandParser::instance().associateCommand(command_name, *this);
00054 }
00055 
00056 CommandParser &CommandParser::instance() {
00057     static CommandParser singleton;
00058     return singleton;
00059 }
00060 
00061 FbTk::Command *CommandParser::parseLine(const std::string &line) {
00062 
00063     // parse arguments and command
00064     string command = line;
00065     string arguments;
00066     string::size_type first_pos = removeFirstWhitespace(command);
00067     string::size_type second_pos = command.find_first_of(" \t", first_pos);
00068     if (second_pos != string::npos) {
00069         // ok we have arguments, parsing them here
00070         arguments = command.substr(second_pos);
00071         removeFirstWhitespace(arguments);
00072         command.erase(second_pos); // remove argument from command
00073     }
00074 
00075     // now we have parsed command and arguments
00076 
00077     command = FbTk::StringUtil::toLower(command);
00078 
00079     // we didn't find any matching command in default commands,
00080     // so we search in the command creators modules for a 
00081     // matching command string
00082     return toCommand(command, arguments);
00083 
00084 }
00085 
00086 FbTk::Command *CommandParser::toCommand(const std::string &command_str, const std::string &arguments) {
00087     if (m_commandfactorys[command_str] != 0)
00088         return m_commandfactorys[command_str]->stringToCommand(command_str, arguments);
00089 
00090     return 0;
00091 }
00092 
00093 void CommandParser::associateCommand(const std::string &command, CommandFactory &factory) {
00094     // we shouldnt override other commands
00095     if (m_commandfactorys[command] != 0)
00096         return;
00097 
00098     m_commandfactorys[command] = &factory;
00099 }
00100 
00101 void CommandParser::removeAssociation(CommandFactory &factory) {
00102     // commands that are associated with the factory
00103     std::vector<std::string> commands; 
00104     // find associations
00105     CommandFactoryMap::iterator factory_it = m_commandfactorys.begin();
00106     const CommandFactoryMap::iterator factory_it_end = m_commandfactorys.end();
00107     for (; factory_it != factory_it_end; ++factory_it) {
00108         if ((*factory_it).second == &factory)
00109             commands.push_back((*factory_it).first);
00110     }
00111     // remove all associations
00112     while (!commands.empty()) {
00113         m_commandfactorys.erase(commands.back());
00114         commands.pop_back();
00115     }
00116 }

Fluxbox CVS-Jan-2003




      



Got comments about the page? Send them to webmaster.
If you have general Fluxbox related questions ask them on our irc channel or mailing lists.

Show Source








Designed by aLEczapKA