00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <cassert>
00021
00022 #include "CommandManager.h"
00023 #include "CommandHandler.h"
00024 #include "Stream.h"
00025
00026 namespace mdw
00027 {
00028
00029 CommandManager *CommandManager::_instance = 0;
00030
00031 CommandManager::CommandManager()
00032 {
00033 }
00034
00035 CommandManager::~CommandManager()
00036 {
00037 }
00038
00039 void CommandManager::initialise()
00040 {
00041 _instance = new CommandManager();
00042 _instance->init();
00043 }
00044
00045 CommandManager &CommandManager::getInstance()
00046 {
00047 assert(_instance != 0);
00048 return *_instance;
00049 }
00050
00051 CommandHandler *CommandManager::getHandler (const std::vector<std::string> &tokens, int &position)
00052 {
00053 if (tokens.empty())
00054 {
00055 return 0;
00056 }
00057 CommandHandler *handler = getHandler(tokens[0]);
00058 if (handler == 0)
00059 {
00060 return 0;
00061 }
00062 for (int i = 1; i < tokens.size(); ++i)
00063 {
00064 CommandHandler *nextHandler = handler->getHandler(tokens[i]);
00065 if (nextHandler == 0)
00066 {
00067 position = i;
00068 return handler;
00069 }
00070 else
00071 {
00072 handler = nextHandler;
00073 }
00074 }
00075 }
00076
00077 CommandHandler *CommandManager::getHandler (const std::string &firstLevel)
00078 {
00079 std::map<std::string, CommandHandler*>::iterator aIter = _commandTree.find(firstLevel);
00080 if (aIter == _commandTree.end())
00081 {
00082 return 0;
00083 }
00084 return aIter->second;
00085 }
00086
00087 void CommandManager::execute (const std::vector<std::string> &tokens, Stream &output)
00088 {
00089 if (tokens.empty())
00090 {
00091 return;
00092 }
00093 int position;
00094 CommandHandler *aHandler = getHandler(tokens, position);
00095 if (aHandler == 0)
00096 {
00097 output << "Command not found : " << tokens[0] << endl;
00098 output << "Possible commands are: " << endl;
00099 for (std::map<std::string, CommandHandler*>::iterator aIter = _commandTree.begin();
00100 aIter != _commandTree.end();
00101 ++aIter)
00102 {
00103 output << aIter->first << endl;
00104 }
00105 }
00106 aHandler->execute(tokens, position, output);
00107 }
00108
00109 void CommandManager::init()
00110 {
00111 }
00112
00113 CommandHandler &CommandManager::createNode (const std::string &subHandler, CommandHandler *handler)
00114 {
00115 std::map<std::string, CommandHandler*>::iterator aIter = _commandTree.find(subHandler);
00116 if (aIter == _commandTree.end())
00117 {
00118 if (handler == 0)
00119 {
00120 handler = new CommandHandler();
00121 }
00122
00123 _commandTree.insert(make_pair(subHandler, handler));
00124 }
00125 else
00126 {
00127 if (handler != 0)
00128 {
00129 for (std::map<std::string, CommandHandler*>::iterator aIter = aIter->second->_subCommands.begin();
00130 aIter != aIter->second->_subCommands.end();
00131 ++aIter)
00132 {
00133 handler->_subCommands.insert(*aIter);
00134 }
00135 _commandTree.insert(make_pair(subHandler, handler));
00136 }
00137 }
00138 return *handler;
00139 }
00140
00141 }