[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 | /** |
---|
| 58 | * collects the Commands registered to some class. |
---|
| 59 | * @param className the name of the Class to collect the Commands from. |
---|
| 60 | * @param stringList a List to paste the Commands into. |
---|
| 61 | * @returns true on success, false otherwise |
---|
| 62 | */ |
---|
[7386] | 63 | bool ShellCommandClass::getCommandListOfClass(const std::string& className, std::list<std::string>& stringList) |
---|
[7374] | 64 | { |
---|
| 65 | std::list<ShellCommandClass*>::iterator elem; |
---|
| 66 | for(elem = ShellCommandClass::commandClassList->begin(); elem != ShellCommandClass::commandClassList->end(); elem++) |
---|
[5189] | 67 | { |
---|
[7374] | 68 | if (className == (*elem)->getName()) |
---|
| 69 | { |
---|
[7388] | 70 | std::vector<ShellCommand*>::iterator command; |
---|
[7374] | 71 | for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++) |
---|
[7386] | 72 | stringList.push_back((*command)->getName()); |
---|
[7374] | 73 | } |
---|
[5189] | 74 | } |
---|
[7374] | 75 | return true; |
---|
[5189] | 76 | } |
---|
| 77 | |
---|
[7374] | 78 | /** |
---|
[7389] | 79 | * @brief unregisters all Commands that exist |
---|
[7374] | 80 | */ |
---|
| 81 | void ShellCommandClass::unregisterAllCommands() |
---|
| 82 | { |
---|
| 83 | if (ShellCommandClass::commandClassList != NULL) |
---|
| 84 | { |
---|
| 85 | // unregister all commands and Classes |
---|
| 86 | std::list<ShellCommandClass*>::iterator classIT; |
---|
| 87 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
| 88 | delete (*classIT); |
---|
| 89 | delete ShellCommandClass::commandClassList; |
---|
| 90 | ShellCommandClass::commandClassList = NULL; |
---|
| 91 | } |
---|
[5170] | 92 | |
---|
[7374] | 93 | } |
---|
| 94 | |
---|
| 95 | /** |
---|
[7389] | 96 | * @brief checks if a Class is already registered to the Commands' class-stack |
---|
[7374] | 97 | * @param className the Name of the Class to check for |
---|
| 98 | * @returns the CommandClass if found, NULL otherwise |
---|
| 99 | */ |
---|
| 100 | const ShellCommandClass* ShellCommandClass::isRegistered(const std::string& className) |
---|
[5170] | 101 | { |
---|
[7374] | 102 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 103 | initCommandClassList(); |
---|
| 104 | |
---|
| 105 | std::list<ShellCommandClass*>::const_iterator classIT; |
---|
| 106 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[5170] | 107 | { |
---|
[7374] | 108 | if (className == (*classIT)->className) |
---|
| 109 | { |
---|
| 110 | if ((*classIT)->classID == CL_NULL) |
---|
| 111 | (*classIT)->classID = ClassList::StringToID(className); |
---|
[5171] | 112 | |
---|
[7374] | 113 | return (*classIT); |
---|
| 114 | } |
---|
[5170] | 115 | } |
---|
[7374] | 116 | return NULL; |
---|
[5170] | 117 | } |
---|
| 118 | |
---|
[7374] | 119 | /** |
---|
| 120 | * searches for a CommandClass |
---|
| 121 | * @param className the name of the CommandClass |
---|
| 122 | * @returns the CommandClass if found, or a new CommandClass if not |
---|
| 123 | */ |
---|
| 124 | ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className) |
---|
| 125 | { |
---|
| 126 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 127 | initCommandClassList(); |
---|
[5170] | 128 | |
---|
[7374] | 129 | std::list<ShellCommandClass*>::iterator classIT; |
---|
| 130 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[5170] | 131 | { |
---|
[7374] | 132 | if (className == (*classIT)->className) |
---|
| 133 | { |
---|
| 134 | return (*classIT); |
---|
| 135 | } |
---|
[5170] | 136 | } |
---|
[7374] | 137 | return new ShellCommandClass(className); |
---|
[5170] | 138 | } |
---|
| 139 | |
---|
[7374] | 140 | /** |
---|
| 141 | * @brief initializes the CommandList (if it is NULL) |
---|
| 142 | */ |
---|
| 143 | void ShellCommandClass::initCommandClassList() |
---|
[5170] | 144 | { |
---|
[7374] | 145 | if (ShellCommandClass::commandClassList == NULL) |
---|
| 146 | { |
---|
| 147 | ShellCommandClass::commandClassList = new std::list<ShellCommandClass*>; |
---|
| 148 | ShellCommand::registerCommand("debug", "ShellCommand", ExecutorStatic<ShellCommand>(ShellCommand::debug)); |
---|
| 149 | } |
---|
[5170] | 150 | } |
---|
| 151 | |
---|
[7374] | 152 | /** |
---|
| 153 | * @brief displays help about ShellCommandClass |
---|
| 154 | * @param className: the Class of Commands to show help about |
---|
| 155 | */ |
---|
| 156 | void ShellCommandClass::help(const std::string& className) |
---|
[5204] | 157 | { |
---|
[7374] | 158 | if (likely(ShellCommandClass::commandClassList != NULL)) |
---|
[5204] | 159 | { |
---|
[7374] | 160 | std::list<ShellCommandClass*>::iterator classIT; |
---|
| 161 | for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++) |
---|
[5204] | 162 | { |
---|
[7374] | 163 | if (className == (*classIT)->className) |
---|
[5204] | 164 | { |
---|
[7374] | 165 | PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size()); |
---|
[7388] | 166 | std::vector<ShellCommand*>::const_iterator cmdIT; |
---|
[7374] | 167 | for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) |
---|
| 168 | { |
---|
| 169 | PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount()); |
---|
| 170 | /// FIXME |
---|
| 171 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 172 | PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
| 173 | if (!(*cmdIT)->description.empty()) |
---|
| 174 | PRINT(0)("- %s", (*cmdIT)->description.c_str()); |
---|
| 175 | PRINT(0)("\n"); |
---|
| 176 | } |
---|
| 177 | return; |
---|
[5204] | 178 | } |
---|
| 179 | } |
---|
[7374] | 180 | PRINTF(3)("Class %s not found in Command's classes\n", className.c_str()); |
---|
[5204] | 181 | } |
---|
[7374] | 182 | else |
---|
| 183 | { |
---|
| 184 | PRINTF(1)("List of commandClasses does not exist"); |
---|
| 185 | } |
---|
[5204] | 186 | } |
---|
[7374] | 187 | |
---|
[7388] | 188 | void ShellCommandClass::registerCommand(ShellCommand* command) |
---|
| 189 | { |
---|
| 190 | this->commandList.push_back(command); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | void ShellCommandClass::unregisterCommand(ShellCommand* command) |
---|
| 195 | { |
---|
| 196 | std::vector<ShellCommand*>::iterator delC = std::find(this->commandList.begin(), this->commandList.end(), command); |
---|
[7389] | 197 | if (delC != this->commandList.end()) |
---|
| 198 | this->commandList.erase(delC); |
---|
[7388] | 199 | } |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | |
---|
[5204] | 203 | } |
---|
[7388] | 204 | |
---|
| 205 | |
---|
| 206 | |
---|
| 207 | |
---|