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