aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/CommandRegistry.cc
blob: fc40920beeaa5da97e4a2d2efc49182b8608e203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// CommandRegistry.cc for FbTk
// Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id: $

#include "CommandRegistry.hh"
#include "Command.hh"
#include "StringUtil.hh"

#include <iostream>

namespace FbTk {

CommandRegistry &CommandRegistry::instance() {
    static CommandRegistry s_instance;
    return s_instance;
}


bool CommandRegistry::registerCommand(string name,
                                      CreateFunction createFunction) {
    name = StringUtil::toLower(name);
    m_commandCreators[name] = createFunction;
    return true;
}

bool CommandRegistry::registerBoolCommand(string name,
                                          BoolCreateFunction createFunction) {
    name = StringUtil::toLower(name);
    m_boolcommandCreators[name] = createFunction;
    return true;
}

Command *CommandRegistry::parseLine(const string &line, bool trusted) const {
    // parse args and command
    string command, args;
    StringUtil::getFirstWord(line, command, args);
    StringUtil::removeFirstWhitespace(args);
    StringUtil::removeTrailingWhitespace(args);

    // now we have parsed command and args
    command = StringUtil::toLower(command);
    return getCommand(command, args, trusted);
}

BoolCommand *CommandRegistry::parseBoolLine(const string &line, bool trusted) const {
    // parse args and command
    string command, args;
    StringUtil::getFirstWord(line, command, args);
    StringUtil::removeFirstWhitespace(args);
    StringUtil::removeTrailingWhitespace(args);

    // now we have parsed command and args
    command = StringUtil::toLower(command);
    return getBoolCommand(command, args, trusted);
}

Command *CommandRegistry::getCommand(const string &name, const string &args,
                                     bool trusted) const {
    string lc = StringUtil::toLower(name);
    CreatorMap::const_iterator it = m_commandCreators.find(lc.c_str());
    if (it == m_commandCreators.end())
        return 0;
    else
        return it->second(name, args, trusted);
}

BoolCommand *CommandRegistry::getBoolCommand(const string &name,
        const string &args, bool trusted) const {
    string lc = StringUtil::toLower(name);
    BoolCreatorMap::const_iterator it = m_boolcommandCreators.find(lc.c_str());
    if (it == m_boolcommandCreators.end())
        return 0;
    else
        return it->second(name, args, trusted);
}

}; // end namespace FbTk