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