[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 | { |
---|
[7394] | 28 | std::vector<ShellCommandClass*> ShellCommandClass::commandClassList; |
---|
[5639] | 29 | |
---|
[7374] | 30 | /** |
---|
[7407] | 31 | * @brief creates a new ShellCommandClass |
---|
[7374] | 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 | |
---|
[7394] | 42 | ShellCommandClass::commandClassList.push_back(this); |
---|
[7374] | 43 | } |
---|
[5170] | 44 | |
---|
[7374] | 45 | /** |
---|
| 46 | * destructs the shellCommandClass again |
---|
| 47 | */ |
---|
| 48 | ShellCommandClass::~ShellCommandClass() |
---|
[5170] | 49 | { |
---|
[7388] | 50 | while(!this->commandList.empty()) |
---|
| 51 | delete this->commandList.back(); |
---|
[5170] | 52 | } |
---|
| 53 | |
---|
[7374] | 54 | /** |
---|
[7390] | 55 | * @param command the Command to register. |
---|
[7374] | 56 | */ |
---|
[7390] | 57 | void ShellCommandClass::registerCommand(ShellCommand* command) |
---|
[7374] | 58 | { |
---|
[7390] | 59 | this->commandList.push_back(command); |
---|
[5189] | 60 | } |
---|
| 61 | |
---|
[7374] | 62 | /** |
---|
[7390] | 63 | * @brief unregister command. |
---|
| 64 | * @param command the Command to unregister. |
---|
| 65 | */ |
---|
| 66 | void ShellCommandClass::unregisterCommand(ShellCommand* command) |
---|
| 67 | { |
---|
| 68 | std::vector<ShellCommand*>::iterator delC = std::find(this->commandList.begin(), this->commandList.end(), command); |
---|
| 69 | if (delC != this->commandList.end()) |
---|
| 70 | this->commandList.erase(delC); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
[7389] | 74 | * @brief unregisters all Commands that exist |
---|
[7374] | 75 | */ |
---|
| 76 | void ShellCommandClass::unregisterAllCommands() |
---|
| 77 | { |
---|
[7394] | 78 | // unregister all commands and Classes |
---|
| 79 | std::vector<ShellCommandClass*>::iterator classIT; |
---|
| 80 | for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) |
---|
| 81 | delete (*classIT); |
---|
[7374] | 82 | } |
---|
| 83 | |
---|
[7390] | 84 | |
---|
[7374] | 85 | /** |
---|
[7390] | 86 | * @brief collects the Commands registered to some class. |
---|
| 87 | * @param className the name of the Class to collect the Commands from. |
---|
| 88 | * @param stringList a List to paste the Commands into. |
---|
| 89 | * @returns true on success, false otherwise |
---|
| 90 | */ |
---|
| 91 | bool ShellCommandClass::getCommandListOfClass(const std::string& className, std::list<std::string>& stringList) |
---|
| 92 | { |
---|
[7394] | 93 | std::vector<ShellCommandClass*>::iterator elem; |
---|
| 94 | for(elem = ShellCommandClass::commandClassList.begin(); elem != ShellCommandClass::commandClassList.end(); elem++) |
---|
[7390] | 95 | { |
---|
| 96 | if (className == (*elem)->getName()) |
---|
| 97 | { |
---|
| 98 | std::vector<ShellCommand*>::iterator command; |
---|
| 99 | for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++) |
---|
| 100 | stringList.push_back((*command)->getName()); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | return true; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | /** |
---|
[7389] | 108 | * @brief checks if a Class is already registered to the Commands' class-stack |
---|
[7374] | 109 | * @param className the Name of the Class to check for |
---|
| 110 | * @returns the CommandClass if found, NULL otherwise |
---|
| 111 | */ |
---|
[7408] | 112 | const ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className) |
---|
[5170] | 113 | { |
---|
[7394] | 114 | std::vector<ShellCommandClass*>::const_iterator classIT; |
---|
| 115 | for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) |
---|
[7374] | 116 | if (className == (*classIT)->className) |
---|
| 117 | return (*classIT); |
---|
| 118 | return NULL; |
---|
[5170] | 119 | } |
---|
| 120 | |
---|
[7374] | 121 | /** |
---|
[7408] | 122 | * @brief checks if a Class is already registered to the Commands' class-stack |
---|
| 123 | * @param className the Name of the Class to check for |
---|
| 124 | * @returns the CommandClass if found, NULL otherwise |
---|
| 125 | */ |
---|
[7411] | 126 | bool ShellCommandClass::exists(const std::string& className) |
---|
[7408] | 127 | { |
---|
[7411] | 128 | return (ShellCommandClass::getCommandClass(className) != NULL); |
---|
| 129 | } |
---|
[7408] | 130 | |
---|
[7411] | 131 | ClassID ShellCommandClass::getClassID() |
---|
| 132 | { |
---|
| 133 | if (this->classID == CL_NULL) |
---|
| 134 | this->classID = ClassList::StringToID(this->className); |
---|
| 135 | return this->classID; |
---|
| 136 | } |
---|
[7408] | 137 | |
---|
[7411] | 138 | |
---|
[7408] | 139 | /** |
---|
[7390] | 140 | * @brief searches for a CommandClass |
---|
[7374] | 141 | * @param className the name of the CommandClass |
---|
| 142 | * @returns the CommandClass if found, or a new CommandClass if not |
---|
| 143 | */ |
---|
[7408] | 144 | ShellCommandClass* ShellCommandClass::acquireCommandClass(const std::string& className) |
---|
[7374] | 145 | { |
---|
[7394] | 146 | std::vector<ShellCommandClass*>::iterator classIT; |
---|
| 147 | for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) |
---|
[7374] | 148 | if (className == (*classIT)->className) |
---|
| 149 | return (*classIT); |
---|
| 150 | return new ShellCommandClass(className); |
---|
[5170] | 151 | } |
---|
| 152 | |
---|
[7374] | 153 | /** |
---|
| 154 | * @brief displays help about ShellCommandClass |
---|
| 155 | * @param className: the Class of Commands to show help about |
---|
| 156 | */ |
---|
| 157 | void ShellCommandClass::help(const std::string& className) |
---|
[5204] | 158 | { |
---|
[7394] | 159 | std::vector<ShellCommandClass*>::iterator classIT; |
---|
| 160 | for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) |
---|
[5204] | 161 | { |
---|
[7394] | 162 | if (className == (*classIT)->className) |
---|
[5204] | 163 | { |
---|
[7394] | 164 | PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size()); |
---|
| 165 | std::vector<ShellCommand*>::const_iterator cmdIT; |
---|
| 166 | for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) |
---|
[5204] | 167 | { |
---|
[7394] | 168 | PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount()); |
---|
| 169 | /// FIXME |
---|
| 170 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 171 | PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
| 172 | if (!(*cmdIT)->description.empty()) |
---|
| 173 | PRINT(0)("- %s", (*cmdIT)->description.c_str()); |
---|
| 174 | PRINT(0)("\n"); |
---|
[5204] | 175 | } |
---|
[7394] | 176 | return; |
---|
[5204] | 177 | } |
---|
| 178 | } |
---|
[7394] | 179 | PRINTF(3)("Class %s not found in Command's classes\n", className.c_str()); |
---|
[5204] | 180 | } |
---|
[7374] | 181 | |
---|
[5204] | 182 | } |
---|
[7388] | 183 | |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | |
---|