[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" |
---|
[5780] | 23 | #include "compiler.h" |
---|
[5113] | 24 | |
---|
[7742] | 25 | |
---|
| 26 | |
---|
[7374] | 27 | namespace OrxShell |
---|
| 28 | { |
---|
[9692] | 29 | NewObjectListDefinition(ShellCommandClass); |
---|
| 30 | |
---|
[7743] | 31 | CmdClassList* ShellCommandClass::commandClassList = NULL; |
---|
[5639] | 32 | |
---|
[7374] | 33 | /** |
---|
[7407] | 34 | * @brief creates a new ShellCommandClass |
---|
[7374] | 35 | * @param className the Name of the command-class to create |
---|
| 36 | */ |
---|
| 37 | ShellCommandClass::ShellCommandClass(const std::string& className) |
---|
[9692] | 38 | : _className(className) |
---|
[7374] | 39 | { |
---|
[9692] | 40 | this->registerObject(this, ShellCommandClass::_objectList); |
---|
[7374] | 41 | this->setName(className); |
---|
[5188] | 42 | |
---|
[7374] | 43 | this->classID = CL_NULL; |
---|
[5170] | 44 | |
---|
[7743] | 45 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 46 | ShellCommandClass::commandClassList = new CmdClassList; |
---|
| 47 | ShellCommandClass::commandClassList->push_back(this); |
---|
[7374] | 48 | } |
---|
[5170] | 49 | |
---|
[7374] | 50 | /** |
---|
| 51 | * destructs the shellCommandClass again |
---|
| 52 | */ |
---|
| 53 | ShellCommandClass::~ShellCommandClass() |
---|
[5170] | 54 | { |
---|
[7388] | 55 | while(!this->commandList.empty()) |
---|
| 56 | delete this->commandList.back(); |
---|
[7743] | 57 | |
---|
| 58 | if (ShellCommandClass::commandClassList != NULL) |
---|
| 59 | { |
---|
| 60 | CmdClassList::iterator delClass = std::find(ShellCommandClass::commandClassList->begin(), ShellCommandClass::commandClassList->end(), this); |
---|
| 61 | if (delClass != ShellCommandClass::commandClassList->end()) |
---|
| 62 | ShellCommandClass::commandClassList->erase(delClass); |
---|
| 63 | } |
---|
[5170] | 64 | } |
---|
| 65 | |
---|
[7374] | 66 | /** |
---|
[7390] | 67 | * @param command the Command to register. |
---|
[7374] | 68 | */ |
---|
[7390] | 69 | void ShellCommandClass::registerCommand(ShellCommand* command) |
---|
[7374] | 70 | { |
---|
[7390] | 71 | this->commandList.push_back(command); |
---|
[5189] | 72 | } |
---|
| 73 | |
---|
[7374] | 74 | /** |
---|
[7742] | 75 | * @brief Unregisters a command. |
---|
[7390] | 76 | * @param command the Command to unregister. |
---|
| 77 | */ |
---|
| 78 | void ShellCommandClass::unregisterCommand(ShellCommand* command) |
---|
| 79 | { |
---|
[7742] | 80 | CmdList::iterator delC = std::find(this->commandList.begin(), this->commandList.end(), command); |
---|
[7390] | 81 | if (delC != this->commandList.end()) |
---|
| 82 | this->commandList.erase(delC); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /** |
---|
[7389] | 86 | * @brief unregisters all Commands that exist |
---|
[7374] | 87 | */ |
---|
| 88 | void ShellCommandClass::unregisterAllCommands() |
---|
| 89 | { |
---|
[7394] | 90 | // unregister all commands and Classes |
---|
[7742] | 91 | CmdClassList::iterator classIT; |
---|
[7743] | 92 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 93 | return; |
---|
[7742] | 94 | |
---|
[7743] | 95 | while (!ShellCommandClass::commandClassList->empty()) |
---|
| 96 | delete ShellCommandClass::commandClassList->back(); |
---|
| 97 | delete ShellCommandClass::commandClassList; |
---|
| 98 | ShellCommandClass::commandClassList = NULL; |
---|
[7374] | 99 | } |
---|
| 100 | |
---|
[7390] | 101 | |
---|
[7374] | 102 | /** |
---|
[7390] | 103 | * @brief collects the Commands registered to some class. |
---|
| 104 | * @param className the name of the Class to collect the Commands from. |
---|
| 105 | * @param stringList a List to paste the Commands into. |
---|
| 106 | * @returns true on success, false otherwise |
---|
| 107 | */ |
---|
| 108 | bool ShellCommandClass::getCommandListOfClass(const std::string& className, std::list<std::string>& stringList) |
---|
| 109 | { |
---|
[7743] | 110 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 111 | return false; |
---|
| 112 | |
---|
| 113 | |
---|
[7742] | 114 | CmdClassList::const_iterator elem; |
---|
[7743] | 115 | for(elem = ShellCommandClass::commandClassList->begin(); elem != ShellCommandClass::commandClassList->end(); elem++) |
---|
[7390] | 116 | { |
---|
| 117 | if (className == (*elem)->getName()) |
---|
| 118 | { |
---|
[7742] | 119 | CmdList::iterator command; |
---|
[7390] | 120 | for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++) |
---|
| 121 | stringList.push_back((*command)->getName()); |
---|
[7742] | 122 | return true; |
---|
[7390] | 123 | } |
---|
| 124 | } |
---|
[7742] | 125 | return false; |
---|
[7390] | 126 | } |
---|
| 127 | |
---|
| 128 | |
---|
| 129 | /** |
---|
[7389] | 130 | * @brief checks if a Class is already registered to the Commands' class-stack |
---|
[7374] | 131 | * @param className the Name of the Class to check for |
---|
| 132 | * @returns the CommandClass if found, NULL otherwise |
---|
| 133 | */ |
---|
[7742] | 134 | ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className) |
---|
[5170] | 135 | { |
---|
[7743] | 136 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 137 | return false; |
---|
| 138 | |
---|
| 139 | |
---|
[7742] | 140 | CmdClassList::const_iterator classIT; |
---|
[7743] | 141 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[7374] | 142 | if (className == (*classIT)->className) |
---|
| 143 | return (*classIT); |
---|
| 144 | return NULL; |
---|
[5170] | 145 | } |
---|
| 146 | |
---|
[7374] | 147 | /** |
---|
[7408] | 148 | * @brief checks if a Class is already registered to the Commands' class-stack |
---|
| 149 | * @param className the Name of the Class to check for |
---|
| 150 | * @returns the CommandClass if found, NULL otherwise |
---|
| 151 | */ |
---|
[7411] | 152 | bool ShellCommandClass::exists(const std::string& className) |
---|
[7408] | 153 | { |
---|
[7411] | 154 | return (ShellCommandClass::getCommandClass(className) != NULL); |
---|
| 155 | } |
---|
[7408] | 156 | |
---|
[7411] | 157 | ClassID ShellCommandClass::getClassID() |
---|
| 158 | { |
---|
| 159 | if (this->classID == CL_NULL) |
---|
| 160 | this->classID = ClassList::StringToID(this->className); |
---|
| 161 | return this->classID; |
---|
| 162 | } |
---|
[7408] | 163 | |
---|
[7411] | 164 | |
---|
[7408] | 165 | /** |
---|
[7390] | 166 | * @brief searches for a CommandClass |
---|
[7374] | 167 | * @param className the name of the CommandClass |
---|
| 168 | * @returns the CommandClass if found, or a new CommandClass if not |
---|
| 169 | */ |
---|
[7408] | 170 | ShellCommandClass* ShellCommandClass::acquireCommandClass(const std::string& className) |
---|
[7374] | 171 | { |
---|
[7742] | 172 | ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(className); |
---|
| 173 | if (cmdClass != NULL) |
---|
| 174 | return (cmdClass); |
---|
[7374] | 175 | return new ShellCommandClass(className); |
---|
[5170] | 176 | } |
---|
| 177 | |
---|
[7374] | 178 | /** |
---|
| 179 | * @brief displays help about ShellCommandClass |
---|
| 180 | * @param className: the Class of Commands to show help about |
---|
| 181 | */ |
---|
| 182 | void ShellCommandClass::help(const std::string& className) |
---|
[5204] | 183 | { |
---|
[7743] | 184 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 185 | { |
---|
| 186 | PRINT(0)("No Commands Registered\n"); |
---|
| 187 | return; |
---|
| 188 | } |
---|
[7742] | 189 | if (className.empty()) |
---|
[7743] | 190 | PRINT(0)("===== Displaying %d registered Classes:\n", ShellCommandClass::commandClassList->size()); |
---|
[7742] | 191 | |
---|
| 192 | |
---|
| 193 | CmdClassList::iterator classIT; |
---|
[7743] | 194 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[5204] | 195 | { |
---|
[7742] | 196 | if (className.empty() || className == (*classIT)->className) |
---|
[5204] | 197 | { |
---|
[7394] | 198 | PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size()); |
---|
[7742] | 199 | CmdList::const_iterator cmdIT; |
---|
[7394] | 200 | for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) |
---|
[5204] | 201 | { |
---|
[9406] | 202 | PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getCName(), (*cmdIT)->executor->getParamCount()); |
---|
[7394] | 203 | /// FIXME |
---|
| 204 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 205 | PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
| 206 | if (!(*cmdIT)->description.empty()) |
---|
| 207 | PRINT(0)("- %s", (*cmdIT)->description.c_str()); |
---|
| 208 | PRINT(0)("\n"); |
---|
[5204] | 209 | } |
---|
[7742] | 210 | if (likely(!className.empty())) |
---|
| 211 | return; |
---|
[5204] | 212 | } |
---|
| 213 | } |
---|
[7742] | 214 | PRINTF(3)("Class '%s' not found in Command's classes\n", className.c_str()); |
---|
[5204] | 215 | } |
---|
| 216 | } |
---|
[7388] | 217 | |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | |
---|