[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 | /** |
---|
| 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 | |
---|
[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()) |
---|
[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 | { |
---|
[7394] | 81 | // unregister all commands and Classes |
---|
| 82 | std::vector<ShellCommandClass*>::iterator classIT; |
---|
| 83 | for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) |
---|
| 84 | delete (*classIT); |
---|
[7374] | 85 | } |
---|
| 86 | |
---|
[7390] | 87 | |
---|
[7374] | 88 | /** |
---|
[7390] | 89 | * @brief collects the Commands registered to some class. |
---|
| 90 | * @param className the name of the Class to collect the Commands from. |
---|
| 91 | * @param stringList a List to paste the Commands into. |
---|
| 92 | * @returns true on success, false otherwise |
---|
| 93 | */ |
---|
| 94 | bool ShellCommandClass::getCommandListOfClass(const std::string& className, std::list<std::string>& stringList) |
---|
| 95 | { |
---|
[7394] | 96 | std::vector<ShellCommandClass*>::iterator elem; |
---|
| 97 | for(elem = ShellCommandClass::commandClassList.begin(); elem != ShellCommandClass::commandClassList.end(); elem++) |
---|
[7390] | 98 | { |
---|
| 99 | if (className == (*elem)->getName()) |
---|
| 100 | { |
---|
| 101 | std::vector<ShellCommand*>::iterator command; |
---|
| 102 | for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++) |
---|
| 103 | stringList.push_back((*command)->getName()); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | return true; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | |
---|
| 110 | /** |
---|
[7389] | 111 | * @brief checks if a Class is already registered to the Commands' class-stack |
---|
[7374] | 112 | * @param className the Name of the Class to check for |
---|
| 113 | * @returns the CommandClass if found, NULL otherwise |
---|
| 114 | */ |
---|
| 115 | const ShellCommandClass* ShellCommandClass::isRegistered(const std::string& className) |
---|
[5170] | 116 | { |
---|
[7394] | 117 | std::vector<ShellCommandClass*>::const_iterator classIT; |
---|
| 118 | for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) |
---|
[5170] | 119 | { |
---|
[7374] | 120 | if (className == (*classIT)->className) |
---|
| 121 | { |
---|
| 122 | if ((*classIT)->classID == CL_NULL) |
---|
| 123 | (*classIT)->classID = ClassList::StringToID(className); |
---|
[5171] | 124 | |
---|
[7374] | 125 | return (*classIT); |
---|
| 126 | } |
---|
[5170] | 127 | } |
---|
[7374] | 128 | return NULL; |
---|
[5170] | 129 | } |
---|
| 130 | |
---|
[7374] | 131 | /** |
---|
[7390] | 132 | * @brief searches for a CommandClass |
---|
[7374] | 133 | * @param className the name of the CommandClass |
---|
| 134 | * @returns the CommandClass if found, or a new CommandClass if not |
---|
| 135 | */ |
---|
| 136 | ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className) |
---|
| 137 | { |
---|
[7394] | 138 | std::vector<ShellCommandClass*>::iterator classIT; |
---|
| 139 | for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) |
---|
[7374] | 140 | if (className == (*classIT)->className) |
---|
| 141 | return (*classIT); |
---|
| 142 | return new ShellCommandClass(className); |
---|
[5170] | 143 | } |
---|
| 144 | |
---|
[7374] | 145 | /** |
---|
| 146 | * @brief displays help about ShellCommandClass |
---|
| 147 | * @param className: the Class of Commands to show help about |
---|
| 148 | */ |
---|
| 149 | void ShellCommandClass::help(const std::string& className) |
---|
[5204] | 150 | { |
---|
[7394] | 151 | std::vector<ShellCommandClass*>::iterator classIT; |
---|
| 152 | for (classIT = ShellCommandClass::commandClassList.begin(); classIT != ShellCommandClass::commandClassList.end(); classIT++) |
---|
[5204] | 153 | { |
---|
[7394] | 154 | if (className == (*classIT)->className) |
---|
[5204] | 155 | { |
---|
[7394] | 156 | PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size()); |
---|
| 157 | std::vector<ShellCommand*>::const_iterator cmdIT; |
---|
| 158 | for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++) |
---|
[5204] | 159 | { |
---|
[7394] | 160 | PRINT(0)(" command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount()); |
---|
| 161 | /// FIXME |
---|
| 162 | /* for (unsigned int i = 0; i< elem->paramCount; i++) |
---|
| 163 | PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/ |
---|
| 164 | if (!(*cmdIT)->description.empty()) |
---|
| 165 | PRINT(0)("- %s", (*cmdIT)->description.c_str()); |
---|
| 166 | PRINT(0)("\n"); |
---|
[5204] | 167 | } |
---|
[7394] | 168 | return; |
---|
[5204] | 169 | } |
---|
| 170 | } |
---|
[7394] | 171 | PRINTF(3)("Class %s not found in Command's classes\n", className.c_str()); |
---|
[5204] | 172 | } |
---|
[7374] | 173 | |
---|
[5204] | 174 | } |
---|
[7388] | 175 | |
---|
| 176 | |
---|
| 177 | |
---|
| 178 | |
---|