[4744] | 1 | /* |
---|
[1853] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[1855] | 10 | |
---|
| 11 | ### File Specific: |
---|
[5068] | 12 | main-programmer: Benjamin Grauer |
---|
[1855] | 13 | co-programmer: ... |
---|
[1853] | 14 | */ |
---|
| 15 | |
---|
[7374] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL |
---|
[1853] | 17 | |
---|
[5639] | 18 | #include "shell_command_class.h" |
---|
| 19 | |
---|
[5129] | 20 | #include "shell_command.h" |
---|
[1853] | 21 | |
---|
[5129] | 22 | #include "debug.h" |
---|
[5113] | 23 | #include "class_list.h" |
---|
[5780] | 24 | #include "compiler.h" |
---|
[5113] | 25 | |
---|
[7374] | 26 | namespace OrxShell |
---|
| 27 | { |
---|
| 28 | std::list<ShellCommandClass*>* ShellCommandClass::commandClassList = NULL; |
---|
[5639] | 29 | |
---|
[7374] | 30 | /** |
---|
| 31 | * creates a new ShellCommandClass |
---|
| 32 | * @param className the Name of the command-class to create |
---|
| 33 | */ |
---|
| 34 | ShellCommandClass::ShellCommandClass(const std::string& className) |
---|
| 35 | : className(className) |
---|
| 36 | { |
---|
| 37 | this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass"); |
---|
| 38 | this->setName(className); |
---|
[5188] | 39 | |
---|
[7374] | 40 | this->classID = CL_NULL; |
---|
[5170] | 41 | |
---|
[7374] | 42 | ShellCommandClass::commandClassList->push_back(this); |
---|
| 43 | } |
---|
[5170] | 44 | |
---|
[7374] | 45 | /** |
---|
| 46 | * destructs the shellCommandClass again |
---|
| 47 | */ |
---|
| 48 | ShellCommandClass::~ShellCommandClass() |
---|
[5170] | 49 | { |
---|
[7388] | 50 | while(!this->commandList.empty()) |
---|
[7374] | 51 | { |
---|
[7388] | 52 | delete this->commandList.back(); |
---|
| 53 | this->commandList.pop_back(); |
---|
[7374] | 54 | } |
---|
[5170] | 55 | } |
---|
| 56 | |
---|
[7374] | 57 | /** |
---|
[7390] | 58 | * @param command the Command to register. |
---|
[7374] | 59 | */ |
---|
[7390] | 60 | void ShellCommandClass::registerCommand(ShellCommand* command) |
---|
[7374] | 61 | { |
---|
[7390] | 62 | this->commandList.push_back(command); |
---|
[5189] | 63 | } |
---|
| 64 | |
---|
[7374] | 65 | /** |
---|
[7390] | 66 | * @brief unregister command. |
---|
| 67 | * @param command the Command to unregister. |
---|
| 68 | */ |
---|
| 69 | void ShellCommandClass::unregisterCommand(ShellCommand* command) |
---|
| 70 | { |
---|
| 71 | std::vector<ShellCommand*>::iterator delC = std::find(this->commandList.begin(), this->commandList.end(), command); |
---|
| 72 | if (delC != this->commandList.end()) |
---|
| 73 | this->commandList.erase(delC); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
[7389] | 77 | * @brief unregisters all Commands that exist |
---|
[7374] | 78 | */ |
---|
| 79 | void ShellCommandClass::unregisterAllCommands() |
---|
| 80 | { |
---|
| 81 | if (ShellCommandClass::commandClassList != NULL) |
---|
| 82 | { |
---|
| 83 | // unregister all commands and Classes |
---|
| 84 | std::list<ShellCommandClass*>::iterator classIT; |
---|
| 85 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
| 86 | delete (*classIT); |
---|
| 87 | delete ShellCommandClass::commandClassList; |
---|
| 88 | ShellCommandClass::commandClassList = NULL; |
---|
| 89 | } |
---|
[5170] | 90 | |
---|
[7374] | 91 | } |
---|
| 92 | |
---|
[7390] | 93 | |
---|
[7374] | 94 | /** |
---|
[7390] | 95 | * @brief collects the Commands registered to some class. |
---|
| 96 | * @param className the name of the Class to collect the Commands from. |
---|
| 97 | * @param stringList a List to paste the Commands into. |
---|
| 98 | * @returns true on success, false otherwise |
---|
| 99 | */ |
---|
| 100 | bool ShellCommandClass::getCommandListOfClass(const std::string& className, std::list<std::string>& stringList) |
---|
| 101 | { |
---|
| 102 | std::list<ShellCommandClass*>::iterator elem; |
---|
| 103 | for(elem = ShellCommandClass::commandClassList->begin(); elem != ShellCommandClass::commandClassList->end(); elem++) |
---|
| 104 | { |
---|
| 105 | if (className == (*elem)->getName()) |
---|
| 106 | { |
---|
| 107 | std::vector<ShellCommand*>::iterator command; |
---|
| 108 | for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++) |
---|
| 109 | stringList.push_back((*command)->getName()); |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | return true; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | /** |
---|
[7389] | 117 | * @brief checks if a Class is already registered to the Commands' class-stack |
---|
[7374] | 118 | * @param className the Name of the Class to check for |
---|
| 119 | * @returns the CommandClass if found, NULL otherwise |
---|
| 120 | */ |
---|
| 121 | const ShellCommandClass* ShellCommandClass::isRegistered(const std::string& className) |
---|
[5170] | 122 | { |
---|
[7374] | 123 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 124 | initCommandClassList(); |
---|
| 125 | |
---|
| 126 | std::list<ShellCommandClass*>::const_iterator classIT; |
---|
| 127 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[5170] | 128 | { |
---|
[7374] | 129 | if (className == (*classIT)->className) |
---|
| 130 | { |
---|
| 131 | if ((*classIT)->classID == CL_NULL) |
---|
| 132 | (*classIT)->classID = ClassList::StringToID(className); |
---|
[5171] | 133 | |
---|
[7374] | 134 | return (*classIT); |
---|
| 135 | } |
---|
[5170] | 136 | } |
---|
[7374] | 137 | return NULL; |
---|
[5170] | 138 | } |
---|
| 139 | |
---|
[7374] | 140 | /** |
---|
[7390] | 141 | * @brief searches for a CommandClass |
---|
[7374] | 142 | * @param className the name of the CommandClass |
---|
| 143 | * @returns the CommandClass if found, or a new CommandClass if not |
---|
| 144 | */ |
---|
| 145 | ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className) |
---|
| 146 | { |
---|
| 147 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 148 | initCommandClassList(); |
---|
[5170] | 149 | |
---|
[7374] | 150 | std::list<ShellCommandClass*>::iterator classIT; |
---|
| 151 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[5170] | 152 | { |
---|
[7374] | 153 | if (className == (*classIT)->className) |
---|
| 154 | { |
---|
| 155 | return (*classIT); |
---|
| 156 | } |
---|
[5170] | 157 | } |
---|
[7374] | 158 | return new ShellCommandClass(className); |
---|
[5170] | 159 | } |
---|
| 160 | |
---|
[7374] | 161 | /** |
---|
| 162 | * @brief initializes the CommandList (if it is NULL) |
---|
| 163 | */ |
---|
| 164 | void ShellCommandClass::initCommandClassList() |
---|
[5170] | 165 | { |
---|
[7374] | 166 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 167 | { |
---|
| 168 | ShellCommandClass::commandClassList = new std::list<ShellCommandClass*>; |
---|
| 169 | ShellCommand::registerCommand("debug", "ShellCommand", ExecutorStatic<ShellCommand>(ShellCommand::debug)); |
---|
| 170 | } |
---|
[5170] | 171 | } |
---|
| 172 | |
---|
[7374] | 173 | /** |
---|
| 174 | * @brief displays help about ShellCommandClass |
---|
| 175 | * @param className: the Class of Commands to show help about |
---|
| 176 | */ |
---|
| 177 | void ShellCommandClass::help(const std::string& className) |
---|
[5204] | 178 | { |
---|
[7374] | 179 | if (likely(ShellCommandClass::commandClassList != NULL)) |
---|
[5204] | 180 | { |
---|
[7374] | 181 | std::list<ShellCommandClass*>::iterator classIT; |
---|
| 182 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[5204] | 183 | { |
---|
[7374] | 184 | if (className == (*classIT)->className) |
---|
[5204] | 185 | { |
---|
[7374] | 186 | PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size()); |
---|
[7388] | 187 | std::vector<ShellCommand*>::const_iterator cmdIT; |
---|
[7374] | 188 | for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) |
---|
| 189 | { |
---|
| 190 | PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount()); |
---|
| 191 | /// FIXME |
---|
| 192 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 193 | PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
| 194 | if (!(*cmdIT)->description.empty()) |
---|
| 195 | PRINT(0)("- %s", (*cmdIT)->description.c_str()); |
---|
| 196 | PRINT(0)("\n"); |
---|
| 197 | } |
---|
| 198 | return; |
---|
[5204] | 199 | } |
---|
| 200 | } |
---|
[7374] | 201 | PRINTF(3)("Class %s not found in Command's classes\n", className.c_str()); |
---|
[5204] | 202 | } |
---|
[7374] | 203 | else |
---|
| 204 | { |
---|
| 205 | PRINTF(1)("List of commandClasses does not exist"); |
---|
| 206 | } |
---|
[5204] | 207 | } |
---|
[7374] | 208 | |
---|
[5204] | 209 | } |
---|
[7388] | 210 | |
---|
| 211 | |
---|
| 212 | |
---|
| 213 | |
---|