Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5639 in orxonox.OLD for trunk/src/lib/shell/shell_command.cc


Ignore:
Timestamp:
Nov 18, 2005, 7:21:32 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: splitted shell_command into shell_command and shell_command_class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/shell/shell_command.cc

    r5637 r5639  
    1717
    1818#include "shell_command.h"
     19#include "shell_command_class.h"
    1920
    2021#include "list.h"
     
    2930using namespace std;
    3031
    31 /**
    32  * creates a new ShellCommandClass
    33  * @param className the Name of the command-class to create
    34  */
    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<ShellCommand>;
    43 
    44   ShellCommandClass::commandClassList->add(this);
    45 }
    46 
    47 /**
    48  * destructs the shellCommandClass again
    49  */
    50 ShellCommandClass::~ShellCommandClass()
    51 {
    52   tIterator<ShellCommand>* iterator = this->commandList->getIterator();
    53   ShellCommand* 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 otherwise
    68  */
    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<ShellCommand>* itFkt = elem->commandList->getIterator();
    81       ShellCommand* 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 ShellCommands
    98  * @param stringList a List to paste the Aliases into.
    99  * @returns true on success, false otherwise
    100  */
    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 exist
    119  */
    120 void ShellCommandClass::unregisterAllCommands()
    121 {
    122   if (ShellCommandClass::commandClassList != NULL)
    123   {
    124     // unregister all commands
    125     tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
    126     ShellCommandClass* elem = iterator->firstElement();
    127     while(elem != NULL)
    128     {
    129       delete elem;
    130 
    131       elem = iterator->nextElement();
    132     }
    133     delete iterator;
    134 
    135     delete ShellCommandClass::commandClassList;
    136     ShellCommandClass::commandClassList = NULL;
    137   }
    138 
    139   // unregister all aliases (there should be nothing to do here :))
    140   if (ShellCommandClass::aliasList != NULL)
    141   {
    142     tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator();
    143     ShellCommandAlias* elemAL = itAL->firstElement();
    144     while(elemAL != NULL)
    145     {
    146       delete elemAL;
    147       elemAL = itAL->nextElement();
    148     }
    149     delete itAL;
    150     delete ShellCommandClass::aliasList;
    151     ShellCommandClass::aliasList = NULL;
    152   }
    153 }
    154 
    155 /**
    156  * checks if a Class is already registered to the Commands' class-stack
    157  * @param className the Name of the Class to check for
    158  * @returns the CommandClass if found, NULL otherwise
    159  */
    160 const ShellCommandClass* ShellCommandClass::isRegistered(const char* className)
    161 {
    162   if (ShellCommandClass::commandClassList == NULL)
    163     initCommandClassList();
    164 
    165   tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
    166   ShellCommandClass* elem = iterator->firstElement();
    167   while(elem != NULL)
    168   {
    169     if (!strcmp(className, elem->className))
    170     {
    171       if (elem->classID == CL_NULL)
    172         elem->classID = ClassList::StringToID(className);
    173 
    174       delete iterator;
    175       return elem;
    176     }
    177     elem = iterator->nextElement();
    178   }
    179   delete iterator;
    180   return NULL;
    181 }
    182 
    183 /**
    184  * searches for a CommandClass
    185  * @param className the name of the CommandClass
    186  * @returns the CommandClass if found, or a new CommandClass if not
    187  */
    188 ShellCommandClass* ShellCommandClass::getCommandClass(const char* className)
    189 {
    190   if (ShellCommandClass::commandClassList == NULL)
    191     initCommandClassList();
    192 
    193   tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
    194   ShellCommandClass* elem = iterator->firstElement();
    195   while(elem != NULL)
    196   {
    197     if (!strcmp(className, elem->className))
    198     {
    199       delete iterator;
    200       return elem;
    201     }
    202     elem = iterator->nextElement();
    203   }
    204   delete iterator;
    205   return new ShellCommandClass(className);
    206 }
    207 
    208 /**
    209  * initializes the CommandList (if it is NULL)
    210  */
    211 void ShellCommandClass::initCommandClassList()
    212 {
    213   if (ShellCommandClass::commandClassList == NULL)
    214   {
    215     ShellCommandClass::commandClassList = new tList<ShellCommandClass>;
    216     ShellCommand::registerCommand("debug", "ShellCommand", new ExecutorStatic<ShellCommand>(ShellCommand::debug));
    217   }
    218 }
    219 
    220 void ShellCommandClass::help(const char* className)
    221 {
    222   if (className == NULL)
    223     return;
    224   if (likely(ShellCommandClass::commandClassList != NULL))
    225   {
    226     tIterator<ShellCommandClass>* itCL = ShellCommandClass::commandClassList->getIterator();
    227     ShellCommandClass* elemCL = itCL->firstElement();
    228     while(elemCL != NULL)
    229     {
    230       if (elemCL->className && !strcasecmp(className, elemCL->className))
    231       {
    232         PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
    233         tIterator<ShellCommand>* iterator = elemCL->commandList->getIterator();
    234         const ShellCommand* elem = iterator->firstElement();
    235         while(elem != NULL)
    236         {
    237           PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->paramCount);
    238           for (unsigned int i = 0; i< elem->paramCount; i++)
    239             PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));
    240           if (elem->description != NULL)
    241             PRINT(0)("- %s", elem->description);
    242           PRINT(0)("\n");
    243           elem = iterator->nextElement();
    244         }
    245         delete iterator;
    246 
    247         delete itCL;
    248         return;
    249       }
    250       elemCL = itCL->nextElement();
    251     }
    252     delete itCL;
    253     PRINTF(3)("Class %s not found in Command's classes\n", className);
    254   }
    255   else
    256   {
    257     PRINTF(1)("List of commandClasses does not exist");
    258   }
    259 }
    260 
    261 tList<ShellCommandClass>* ShellCommandClass::commandClassList = NULL;
    262 tList<ShellCommandAlias>* ShellCommandClass::aliasList = NULL;
    263 
    264 
    265 
    266 
    267 
    268 
    269 
    270 
    271 
    272 
    273 ////////////////////////
    274 // SHELL COMMAND BASE //
    275 ////////////////////////
    27632/**
    27733 * constructs and registers a new Command
Note: See TracChangeset for help on using the changeset viewer.