~ubuntu-branches/ubuntu/trusty/fluxbox/trusty-proposed

« back to all changes in this revision

Viewing changes to src/CommandParser.cc

  • Committer: Bazaar Package Importer
  • Author(s): Dmitry E. Oboukhov
  • Date: 2008-07-01 10:38:14 UTC
  • mfrom: (2.1.12 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080701103814-khx2b6il152x9p93
Tags: 1.0.0+deb1-8
* x-dev has been removed from build-depends (out-of-date package).
* Standards-Version bumped to 3.8.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// CommandParser.cc for Fluxbox - an X11 Window manager
2
 
// Copyright (c) 2003 - 2005 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: CommandParser.cc 3910 2005-03-10 16:51:24Z mathias $
24
 
 
25
 
#include "CommandParser.hh"
26
 
#include "FbTk/StringUtil.hh"
27
 
 
28
 
#include <vector>
29
 
 
30
 
using std::string;
31
 
using std::vector;
32
 
using FbTk::StringUtil::removeFirstWhitespace;
33
 
using FbTk::StringUtil::toLower;
34
 
 
35
 
CommandFactory::CommandFactory() {
36
 
 
37
 
}
38
 
 
39
 
CommandFactory::~CommandFactory() {
40
 
    // remove all associations with this factory
41
 
    CommandParser::instance().removeAssociation(*this);
42
 
}
43
 
 
44
 
void CommandFactory::addCommand(const string &command_name) {
45
 
    CommandParser::instance().associateCommand(command_name, *this);
46
 
}
47
 
 
48
 
CommandParser &CommandParser::instance() {
49
 
    static CommandParser singleton;
50
 
    return singleton;
51
 
}
52
 
 
53
 
FbTk::Command *CommandParser::parseLine(const string &line) {
54
 
 
55
 
    // parse arguments and command
56
 
    string command = line;
57
 
    string arguments;
58
 
    string::size_type first_pos = removeFirstWhitespace(command);
59
 
    FbTk::StringUtil::removeTrailingWhitespace(command);
60
 
    string::size_type second_pos = command.find_first_of(" \t", first_pos);
61
 
    if (second_pos != string::npos) {
62
 
        // ok we have arguments, parsing them here
63
 
        arguments = command.substr(second_pos);
64
 
        removeFirstWhitespace(arguments);
65
 
        command.erase(second_pos); // remove argument from command
66
 
    }
67
 
 
68
 
    // now we have parsed command and arguments
69
 
    command = toLower(command);
70
 
 
71
 
    // we didn't find any matching command in default commands,
72
 
    // so we search in the command creators modules for a 
73
 
    // matching command string
74
 
    return toCommand(command, arguments);
75
 
 
76
 
}
77
 
 
78
 
FbTk::Command *CommandParser::toCommand(const std::string &command_str, const std::string &arguments) {
79
 
    if (m_commandfactorys[command_str] != 0)
80
 
        return m_commandfactorys[command_str]->stringToCommand(command_str, arguments);
81
 
 
82
 
    return 0;
83
 
}
84
 
 
85
 
void CommandParser::associateCommand(const std::string &command, CommandFactory &factory) {
86
 
    // we shouldnt override other commands
87
 
    if (m_commandfactorys[command] != 0)
88
 
        return;
89
 
 
90
 
    m_commandfactorys[command] = &factory;
91
 
}
92
 
 
93
 
void CommandParser::removeAssociation(CommandFactory &factory) {
94
 
    // commands that are associated with the factory
95
 
    vector<string> commands; 
96
 
    // find associations
97
 
    CommandFactoryMap::iterator factory_it = m_commandfactorys.begin();
98
 
    const CommandFactoryMap::iterator factory_it_end = m_commandfactorys.end();
99
 
    for (; factory_it != factory_it_end; ++factory_it) {
100
 
        if ((*factory_it).second == &factory)
101
 
            commands.push_back((*factory_it).first);
102
 
    }
103
 
    // remove all associations
104
 
    while (!commands.empty()) {
105
 
        m_commandfactorys.erase(commands.back());
106
 
        commands.pop_back();
107
 
    }
108
 
}