Changeset 5632 in orxonox.OLD for trunk/src/lib/util/executor/executor.cc
- Timestamp:
- Nov 18, 2005, 4:58:01 PM (19 years ago)
- Location:
- trunk/src/lib/util/executor
- Files:
-
- 1 added
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/executor/executor.cc
r5628 r5632 16 16 //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ 17 17 18 #include " shell_command.h"18 #include "executor.h" 19 19 20 20 #include "list.h" … … 29 29 using namespace std; 30 30 31 /**32 * creates a new ShellCommandClass33 * @param className the Name of the command-class to create34 */35 ShellCommandClass::ShellCommandClass(const char* className)36 {37 this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass");38 this->setName(className);39 40 this->className = className;41 this->classID = CL_NULL;42 this->commandList = new tList<ShellCommandBase>;43 44 ShellCommandClass::commandClassList->add(this);45 }46 47 /**48 * destructs the shellCommandClass again49 */50 ShellCommandClass::~ShellCommandClass()51 {52 tIterator<ShellCommandBase>* iterator = this->commandList->getIterator();53 ShellCommandBase* elem = iterator->firstElement();54 while(elem != NULL)55 {56 delete elem;57 elem = iterator->nextElement();58 }59 delete iterator;60 delete this->commandList;61 }62 63 /**64 * collects the Commands registered to some class.65 * @param className the name of the Class to collect the Commands from.66 * @param stringList a List to paste the Commands into.67 * @returns true on success, false otherwise68 */69 bool ShellCommandClass::getCommandListOfClass(const char* className, tList<const char>* stringList)70 {71 if (stringList == NULL || className == NULL)72 return false;73 74 tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();75 ShellCommandClass* elem = iterator->firstElement();76 while(elem != NULL)77 {78 if (!strcmp (elem->getName(), className))79 {80 tIterator<ShellCommandBase>* itFkt = elem->commandList->getIterator();81 ShellCommandBase* command = itFkt->firstElement();82 while (command != NULL)83 {84 stringList->add(command->getName());85 command = itFkt->nextElement();86 }87 delete itFkt;88 }89 90 elem = iterator->nextElement();91 }92 delete iterator;93 return true;94 }95 96 /**97 * collects the Aliases registered to the ShellCommands98 * @param stringList a List to paste the Aliases into.99 * @returns true on success, false otherwise100 */101 bool ShellCommandClass::getCommandListOfAlias(tList<const char>* stringList)102 {103 if (stringList == NULL || ShellCommandClass::aliasList == NULL)104 return false;105 106 tIterator<ShellCommandAlias>* iterator = ShellCommandClass::aliasList->getIterator();107 ShellCommandAlias* elem = iterator->firstElement();108 while(elem != NULL)109 {110 stringList->add(elem->getName());111 elem = iterator->nextElement();112 }113 delete iterator;114 return true;115 }116 117 /**118 * unregisters all Commands that exist119 */120 void ShellCommandClass::unregisterAllCommands()121 {122 // unregister all commands123 tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();124 ShellCommandClass* elem = iterator->firstElement();125 while(elem != NULL)126 {127 delete elem;128 129 elem = iterator->nextElement();130 }131 delete iterator;132 133 delete ShellCommandClass::commandClassList;134 ShellCommandClass::commandClassList = NULL;135 136 // unregister all aliases (there should be nothing to do here :))137 if (ShellCommandClass::aliasList != NULL)138 {139 tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator();140 ShellCommandAlias* elemAL = itAL->firstElement();141 while(elemAL != NULL)142 {143 delete elemAL;144 elemAL = itAL->nextElement();145 }146 delete itAL;147 delete ShellCommandClass::aliasList;148 ShellCommandClass::aliasList = NULL;149 }150 }151 152 /**153 * checks if a Class is already registered to the Commands' class-stack154 * @param className the Name of the Class to check for155 * @returns the CommandClass if found, NULL otherwise156 */157 const ShellCommandClass* ShellCommandClass::isRegistered(const char* className)158 {159 if (ShellCommandClass::commandClassList == NULL)160 initCommandClassList();161 162 tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();163 ShellCommandClass* elem = iterator->firstElement();164 while(elem != NULL)165 {166 if (!strcmp(className, elem->className))167 {168 if (elem->classID == CL_NULL)169 elem->classID = ClassList::StringToID(className);170 171 delete iterator;172 return elem;173 }174 elem = iterator->nextElement();175 }176 delete iterator;177 return NULL;178 }179 180 /**181 * searches for a CommandClass182 * @param className the name of the CommandClass183 * @returns the CommandClass if found, or a new CommandClass if not184 */185 ShellCommandClass* ShellCommandClass::getCommandClass(const char* className)186 {187 if (ShellCommandClass::commandClassList == NULL)188 initCommandClassList();189 190 tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();191 ShellCommandClass* elem = iterator->firstElement();192 while(elem != NULL)193 {194 if (!strcmp(className, elem->className))195 {196 delete iterator;197 return elem;198 }199 elem = iterator->nextElement();200 }201 delete iterator;202 return new ShellCommandClass(className);203 }204 205 /**206 * initializes the CommandList (if it is NULL)207 */208 void ShellCommandClass::initCommandClassList()209 {210 if (ShellCommandClass::commandClassList == NULL)211 {212 ShellCommandClass::commandClassList = new tList<ShellCommandClass>;213 ShellCommandStatic<ShellCommandBase>::registerCommand("debug", "ShellCommand", ShellCommandBase::debug);214 }215 }216 217 void ShellCommandClass::help(const char* className)218 {219 if (className == NULL)220 return;221 if (likely(ShellCommandClass::commandClassList != NULL))222 {223 tIterator<ShellCommandClass>* itCL = ShellCommandClass::commandClassList->getIterator();224 ShellCommandClass* elemCL = itCL->firstElement();225 while(elemCL != NULL)226 {227 if (elemCL->className && !strcasecmp(className, elemCL->className))228 {229 PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());230 tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator();231 const ShellCommandBase* elem = iterator->firstElement();232 while(elem != NULL)233 {234 PRINT(0)(" command:'%s' : params:%d: ", elem->getName(), elem->paramCount);235 for (unsigned int i = 0; i< elem->paramCount; i++)236 PRINT(0)("%s ", ShellCommandBase::paramToString(elem->parameters[i]));237 if (elem->description != NULL)238 PRINT(0)("- %s", elem->description);239 PRINT(0)("\n");240 elem = iterator->nextElement();241 }242 delete iterator;243 244 delete itCL;245 return;246 }247 elemCL = itCL->nextElement();248 }249 delete itCL;250 PRINTF(3)("Class %s not found in Command's classes\n", className);251 }252 else253 {254 PRINTF(1)("List of commandClasses does not exist");255 }256 }257 258 tList<ShellCommandClass>* ShellCommandClass::commandClassList = NULL;259 tList<ShellCommandAlias>* ShellCommandClass::aliasList = NULL;260 261 262 263 264 265 266 267 268 269 270 31 //////////////////////// 271 32 // SHELL COMMAND BASE // … … 277 38 * @param paramCount the count of parameters this command takes 278 39 */ 279 ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...)40 Executor::Executor(const char* commandName, const char* className, unsigned int paramCount, ...) 280 41 { 281 this->setClassID(CL_ SHELL_COMMAND, "ShellCommand");42 this->setClassID(CL_EXECUTOR, "Executor"); 282 43 this->setName(commandName); 283 this->description = NULL;284 this->alias = NULL;285 44 286 45 // this->classID = classID; 287 this->shellClass = ShellCommandClass::getCommandClass(className); //ClassList::IDToString(classID);288 if (this->shellClass != NULL)289 this->shellClass->commandList->add(this);290 46 // handling parameters, and storing them: 291 47 if (paramCount > FUNCTOR_MAX_ARGUMENTS) … … 304 60 305 61 /** 306 * deconstructs a ShellCommand62 * deconstructs a Executor 307 63 */ 308 ShellCommandBase::~ShellCommandBase()64 Executor::~Executor() 309 65 { 310 66 delete[] this->parameters; 311 67 delete[] this->defaultValue; 312 if (this->alias != NULL && ShellCommandClass::aliasList != NULL)313 {314 ShellCommandClass::aliasList->remove(this->alias);315 delete this->alias;316 }317 }318 319 /**320 * unregister an existing commandName321 * @param className the name of the Class the command belongs to.322 * @param commandName the name of the command itself323 */324 void ShellCommandBase::unregisterCommand(const char* commandName, const char* className)325 {326 if (ShellCommandClass::commandClassList == NULL)327 ShellCommandClass::initCommandClassList();328 329 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);330 331 if (checkClass != NULL)332 {333 tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();334 ShellCommandBase* elem = iterator->firstElement();335 while(elem != NULL)336 {337 if (!strcmp(commandName, elem->getName()))338 {339 checkClass->commandList->remove(elem);340 delete elem;341 break;342 }343 elem = iterator->nextElement();344 }345 delete iterator;346 347 if (checkClass->commandList->getSize() == 0)348 {349 ShellCommandClass::commandClassList->remove(checkClass);350 delete checkClass;351 }352 }353 }354 355 /**356 * checks if a command has already been registered.357 * @param commandName the name of the Command358 * @param className the name of the Class the command should apply to.359 * @param paramCount how many arguments the Command takes360 * @returns true, if the command is registered/false otherwise361 *362 * This is used internally, to see, if we have multiple command subscriptions.363 * This is checked in the registerCommand-function.364 */365 bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...)366 {367 if (ShellCommandClass::commandClassList == NULL)368 {369 ShellCommandClass::initCommandClassList();370 return false;371 }372 373 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);374 if (checkClass != NULL)375 {376 tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();377 ShellCommandBase* elem = iterator->firstElement();378 while(elem != NULL)379 {380 if (!strcmp(commandName, elem->getName()))381 {382 PRINTF(2)("Command already registered\n");383 delete iterator;384 return true;385 }386 elem = iterator->nextElement();387 }388 delete iterator;389 return false;390 }391 else392 return false;393 }394 395 396 /**397 * executes commands398 * @param executionString the string containing the following input399 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]400 * @return true on success, false otherwise.401 */402 bool ShellCommandBase::execute(const char* executionString)403 {404 if (ShellCommandClass::commandClassList == NULL)405 return false;406 407 long classID = CL_NULL; //< the classID retrieved from the Class.408 ShellCommandClass* commandClass = NULL; //< the command class this command applies to.409 tList<BaseObject>* objectList = NULL; //< the list of Objects stored in classID410 BaseObject* objectPointer = NULL; //< a pointer to th Object to Execute the command on411 bool emptyComplete = false; //< if the completion input is empty string. e.g ""412 unsigned int fktPos = 1; //< the position of the function (needed for finding it)413 // long completeType = SHELLC_NONE; //< the Type we'd like to complete.414 SubString inputSplits(executionString, true);415 416 if (inputSplits.getCount() == 0)417 return false;418 if (inputSplits.getCount() >= 1)419 {420 // CHECK FOR ALIAS421 if (ShellCommandClass::aliasList != NULL)422 {423 tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator();424 ShellCommandAlias* elemAL = itAL->firstElement();425 while(elemAL != NULL)426 {427 if (elemAL->getName() != NULL && !strcmp(elemAL->getName(), inputSplits.getString(0)) && elemAL->getCommand() != NULL &&428 elemAL->getCommand()->shellClass != NULL )429 {430 objectList = ClassList::getList(elemAL->getCommand()->shellClass->getName());431 if (objectList != NULL)432 {433 if (inputSplits.getCount() > 1)434 elemAL->getCommand()->executeCommand(objectList->firstElement(), executionString+inputSplits.getOffset(1));435 else436 elemAL->getCommand()->executeCommand(objectList->firstElement(), "");437 delete itAL;438 return true;439 }440 }441 elemAL = itAL->nextElement();442 }443 delete itAL;444 }445 // looking for a Matching Class446 if (likely(ShellCommandClass::commandClassList != NULL))447 {448 tIterator<ShellCommandClass>* itCL = ShellCommandClass::commandClassList->getIterator();449 ShellCommandClass* elemCL = itCL->firstElement();450 while(elemCL != NULL)451 {452 if (elemCL->getName() && !strcasecmp(inputSplits.getString(0), elemCL->getName()))453 {454 //elemCL->getName();455 classID = ClassList::StringToID(elemCL->getName());456 commandClass = elemCL;457 objectList = ClassList::getList(classID);458 break;459 }460 elemCL = itCL->nextElement();461 }462 delete itCL;463 }464 465 if (commandClass != NULL && inputSplits.getCount() >= 2)466 {467 if (objectList != NULL)468 {469 // Checking for a Match in the Objects of classID (else take the first)470 tIterator<BaseObject>* itBO = objectList->getIterator();471 BaseObject* enumBO = itBO->firstElement();472 while(enumBO)473 {474 if (enumBO->getName() != NULL && !strcasecmp(enumBO->getName(), inputSplits.getString(1)))475 {476 objectPointer = enumBO;477 fktPos = 2;478 break;479 }480 enumBO = itBO->nextElement();481 }482 delete itBO;483 484 //485 if (objectPointer == NULL)486 objectPointer = objectList->firstElement();487 }488 // match a function.489 if (commandClass != NULL && (fktPos == 1 || (fktPos == 2 && inputSplits.getCount() >= 3)))490 {491 tIterator<ShellCommandBase>* itCMD = commandClass->commandList->getIterator();492 ShellCommandBase* enumCMD = itCMD->firstElement();493 while (enumCMD != NULL)494 {495 if (!strcmp(enumCMD->getName(), inputSplits.getString(fktPos)))496 {497 if (objectPointer == NULL && enumCMD->functorType == ShellCommand_Objective)498 {499 delete itCMD;500 return false;501 }502 if (inputSplits.getCount() > fktPos+1)503 enumCMD->executeCommand(objectPointer, executionString+inputSplits.getOffset(fktPos +1));504 else505 enumCMD->executeCommand(objectPointer, "");506 delete itCMD;507 return true;508 }509 510 enumCMD = itCMD->nextElement();511 }512 delete itCMD;513 }514 }515 }516 68 } 517 69 … … 520 72 * @param description the description of the Given command 521 73 */ 522 ShellCommandBase* ShellCommandBase::describe(const char* description)74 Executor* Executor::describe(const char* description) 523 75 { 524 76 if (this == NULL) … … 532 84 533 85 /** 534 * adds an Alias to this Command535 * @param alias the name of the Alias to set536 * @returns itself537 */538 ShellCommandBase* ShellCommandBase::setAlias(const char* alias)539 {540 if (this == NULL)541 return NULL;542 543 if (this->alias != NULL)544 {545 PRINTF(2)("not more than one Alias allowed for functions (%s::%s)\n", this->getName(), this->shellClass->getName());546 }547 else548 {549 if (ShellCommandClass::aliasList == NULL)550 ShellCommandClass::aliasList = new tList<ShellCommandAlias>;551 552 ShellCommandAlias* aliasCMD = new ShellCommandAlias(alias, this);553 ShellCommandClass::aliasList->add(aliasCMD);554 this->alias = aliasCMD;555 }556 return this;557 }558 559 /**560 86 * sets default Values of the Commands 561 87 * @param count how many default Values to set. … … 566 92 * count, [EXACTLY THE SAME AS IF YOU WOULD CALL THE FUNCTION UP TO count ARGUMENTS] 567 93 */ 568 ShellCommandBase* ShellCommandBase::defaultValues(unsigned int count, ...)94 Executor* Executor::defaultValues(unsigned int count, ...) 569 95 { 570 96 if (this == NULL) … … 613 139 614 140 /** 615 * prints out nice information about the Shells Commands141 * prints out nice information about the Executor 616 142 */ 617 void ShellCommandBase::debug()143 void Executor::debug() 618 144 { 619 if (ShellCommandClass::commandClassList == NULL) 620 { 621 PRINT(0)("No Command registered.\n"); 622 return; 623 } 624 625 tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator(); 626 ShellCommandClass* elemCL = iteratorCL->firstElement(); 145 /* tIterator<ExecutorClass>* iteratorCL = ExecutorClass::commandClassList->getIterator(); 146 ExecutorClass* elemCL = iteratorCL->firstElement(); 627 147 while(elemCL != NULL) 628 148 { 629 149 PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize()); 630 tIterator< ShellCommandBase>* iterator = elemCL->commandList->getIterator();631 const ShellCommandBase* elem = iterator->firstElement();150 tIterator<Executor>* iterator = elemCL->commandList->getIterator(); 151 const Executor* elem = iterator->firstElement(); 632 152 while(elem != NULL) 633 153 { 634 154 PRINT(0)(" command:'%s' : params:%d: ", elem->getName(), elem->paramCount); 635 155 for (unsigned int i = 0; i< elem->paramCount; i++) 636 printf("%s ", ShellCommandBase::paramToString(elem->parameters[i]));156 printf("%s ", Executor::paramToString(elem->parameters[i])); 637 157 if (elem->description != NULL) 638 158 printf("- %s", elem->description); … … 644 164 elemCL = iteratorCL->nextElement(); 645 165 } 646 delete iteratorCL; 166 delete iteratorCL;*/ 647 167 } 648 649 /**650 * converts a Parameter to a String651 * @param parameter the Parameter we have.652 * @returns the Name of the Parameter at Hand653 */654 const char* ShellCommandBase::paramToString(long parameter)655 {656 switch (parameter)657 {658 case ParameterBool:659 return "BOOL";660 break;661 case ParameterChar:662 return "CHAR";663 break;664 case ParameterString:665 return "STRING";666 break;667 case ParameterInt:668 return "INT";669 break;670 case ParameterUInt:671 return "UINT";672 break;673 case ParameterFloat:674 return "FLOAT";675 break;676 case ParameterLong:677 return "LONG";678 break;679 default:680 return "NULL";681 break;682 }683 }
Note: See TracChangeset
for help on using the changeset viewer.