Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7374 in orxonox.OLD for trunk/src/lib/shell/shell_command_class.cc


Ignore:
Timestamp:
Apr 26, 2006, 3:28:55 AM (18 years ago)
Author:
bensch
Message:

orxonox/trunk: added new Files shell_completion_plugin for the new Plugin Structure.
Also created the first namespace: OrxShell

File:
1 edited

Legend:

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

    r7221 r7374  
    1414*/
    1515
    16 //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
     16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_SHELL
    1717
    1818#include "shell_command_class.h"
     
    2727#include <string.h>
    2828
    29 using namespace std;
    30 
    31 std::list<ShellCommandClass*>* ShellCommandClass::commandClassList = NULL;
    32 std::list<ShellCommandAlias*>* ShellCommandClass::aliasList = NULL;
    33 
    34 /**
    35  * creates a new ShellCommandClass
    36  * @param className the Name of the command-class to create
    37  */
    38 ShellCommandClass::ShellCommandClass(const std::string& className)
    39   : className(className)
     29namespace OrxShell
    4030{
    41   this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass");
    42   this->setName(className);
    43 
    44   this->classID = CL_NULL;
    45 
    46   ShellCommandClass::commandClassList->push_back(this);
    47 }
    48 
    49 /**
    50  * destructs the shellCommandClass again
    51  */
    52 ShellCommandClass::~ShellCommandClass()
    53 {
    54   while(this->commandList.size() > 0)
    55   {
    56     delete this->commandList.front();
    57     this->commandList.pop_front();
    58   }
    59 }
    60 
    61 /**
    62  * collects the Commands registered to some class.
    63  * @param className the name of the Class to collect the Commands from.
    64  * @param stringList a List to paste the Commands into.
    65  * @returns true on success, false otherwise
    66  */
    67 bool ShellCommandClass::getCommandListOfClass(const std::string& className, std::list<std::string>* stringList)
    68 {
    69   if (stringList == NULL)
    70     return false;
    71 
    72   list<ShellCommandClass*>::iterator elem;
    73   for(elem = ShellCommandClass::commandClassList->begin(); elem != ShellCommandClass::commandClassList->end(); elem++)
    74   {
    75     if (className == (*elem)->getName())
    76     {
    77       list<ShellCommand*>::iterator command;
    78       for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++)
    79         stringList->push_back((*command)->getName());
    80     }
    81   }
    82   return true;
    83 }
    84 
    85 /**
    86  * collects the Aliases registered to the ShellCommands
    87  * @param stringList a List to paste the Aliases into.
    88  * @returns true on success, false otherwise
    89  */
    90 bool ShellCommandClass::getCommandListOfAlias(std::list<std::string>* stringList)
    91 {
    92   if (stringList == NULL || ShellCommandClass::aliasList == NULL)
    93     return false;
    94 
    95   list<ShellCommandAlias*>::iterator alias;
    96   for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++)
    97     stringList->push_back((*alias)->getName());
    98   return true;
    99 }
    100 
    101 /**
    102  * unregisters all Commands that exist
    103  */
    104 void ShellCommandClass::unregisterAllCommands()
    105 {
    106    if (ShellCommandClass::commandClassList != NULL)
    107   {
    108     // unregister all commands and Classes
     31
     32  std::list<ShellCommandClass*>* ShellCommandClass::commandClassList = NULL;
     33  std::list<ShellCommandAlias*>* ShellCommandClass::aliasList = NULL;
     34
     35  /**
     36   * creates a new ShellCommandClass
     37   * @param className the Name of the command-class to create
     38   */
     39  ShellCommandClass::ShellCommandClass(const std::string& className)
     40      : className(className)
     41  {
     42    this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass");
     43    this->setName(className);
     44
     45    this->classID = CL_NULL;
     46
     47    ShellCommandClass::commandClassList->push_back(this);
     48  }
     49
     50  /**
     51   * destructs the shellCommandClass again
     52   */
     53  ShellCommandClass::~ShellCommandClass()
     54  {
     55    while(this->commandList.size() > 0)
     56    {
     57      delete this->commandList.front();
     58      this->commandList.pop_front();
     59    }
     60  }
     61
     62  /**
     63   * collects the Commands registered to some class.
     64   * @param className the name of the Class to collect the Commands from.
     65   * @param stringList a List to paste the Commands into.
     66   * @returns true on success, false otherwise
     67   */
     68  bool ShellCommandClass::getCommandListOfClass(const std::string& className, std::list<std::string>* stringList)
     69  {
     70    if (stringList == NULL)
     71      return false;
     72
     73    std::list<ShellCommandClass*>::iterator elem;
     74    for(elem = ShellCommandClass::commandClassList->begin(); elem != ShellCommandClass::commandClassList->end(); elem++)
     75    {
     76      if (className == (*elem)->getName())
     77      {
     78        std::list<ShellCommand*>::iterator command;
     79        for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++)
     80          stringList->push_back((*command)->getName());
     81      }
     82    }
     83    return true;
     84  }
     85
     86  /**
     87   * collects the Aliases registered to the ShellCommands
     88   * @param stringList a List to paste the Aliases into.
     89   * @returns true on success, false otherwise
     90   */
     91  bool ShellCommandClass::getCommandListOfAlias(std::list<std::string>* stringList)
     92  {
     93    if (stringList == NULL || ShellCommandClass::aliasList == NULL)
     94      return false;
     95
     96    std::list<ShellCommandAlias*>::iterator alias;
     97    for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++)
     98      stringList->push_back((*alias)->getName());
     99    return true;
     100  }
     101
     102  /**
     103   * unregisters all Commands that exist
     104   */
     105  void ShellCommandClass::unregisterAllCommands()
     106  {
     107    if (ShellCommandClass::commandClassList != NULL)
     108    {
     109      // unregister all commands and Classes
     110      std::list<ShellCommandClass*>::iterator classIT;
     111      for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
     112        delete (*classIT);
     113      delete ShellCommandClass::commandClassList;
     114      ShellCommandClass::commandClassList = NULL;
     115    }
     116
     117    // unregister all aliases (there should be nothing to do here :))
     118    if (ShellCommandClass::aliasList != NULL)
     119    {
     120      std::list<ShellCommandAlias*>::iterator alias;
     121      for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++)
     122        delete (*alias);
     123      delete ShellCommandClass::aliasList;
     124      ShellCommandClass::aliasList = NULL;
     125    }
     126  }
     127
     128  /**
     129   * checks if a Class is already registered to the Commands' class-stack
     130   * @param className the Name of the Class to check for
     131   * @returns the CommandClass if found, NULL otherwise
     132   */
     133  const ShellCommandClass* ShellCommandClass::isRegistered(const std::string& className)
     134  {
     135    if (ShellCommandClass::commandClassList == NULL)
     136      initCommandClassList();
     137
     138    std::list<ShellCommandClass*>::const_iterator classIT;
     139    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
     140    {
     141      if (className == (*classIT)->className)
     142      {
     143        if ((*classIT)->classID == CL_NULL)
     144          (*classIT)->classID = ClassList::StringToID(className);
     145
     146        return (*classIT);
     147      }
     148    }
     149    return NULL;
     150  }
     151
     152  /**
     153   * searches for a CommandClass
     154   * @param className the name of the CommandClass
     155   * @returns the CommandClass if found, or a new CommandClass if not
     156   */
     157  ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className)
     158  {
     159    if (ShellCommandClass::commandClassList == NULL)
     160      initCommandClassList();
     161
    109162    std::list<ShellCommandClass*>::iterator classIT;
    110163    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
    111       delete (*classIT);
    112     delete ShellCommandClass::commandClassList;
    113     ShellCommandClass::commandClassList = NULL;
    114   }
    115 
    116   // unregister all aliases (there should be nothing to do here :))
    117   if (ShellCommandClass::aliasList != NULL)
    118   {
    119     std::list<ShellCommandAlias*>::iterator alias;
    120     for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++)
    121       delete (*alias);
    122     delete ShellCommandClass::aliasList;
    123     ShellCommandClass::aliasList = NULL;
    124   }
     164    {
     165      if (className == (*classIT)->className)
     166      {
     167        return (*classIT);
     168      }
     169    }
     170    return new ShellCommandClass(className);
     171  }
     172
     173  /**
     174   * @brief initializes the CommandList (if it is NULL)
     175   */
     176  void ShellCommandClass::initCommandClassList()
     177  {
     178    if (ShellCommandClass::commandClassList == NULL)
     179    {
     180      ShellCommandClass::commandClassList = new std::list<ShellCommandClass*>;
     181      ShellCommand::registerCommand("debug", "ShellCommand", ExecutorStatic<ShellCommand>(ShellCommand::debug));
     182    }
     183  }
     184
     185  /**
     186   * @brief displays help about ShellCommandClass
     187   * @param className: the Class of Commands to show help about
     188   */
     189  void ShellCommandClass::help(const std::string& className)
     190  {
     191    if (likely(ShellCommandClass::commandClassList != NULL))
     192    {
     193      std::list<ShellCommandClass*>::iterator classIT;
     194      for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
     195      {
     196        if (className == (*classIT)->className)
     197        {
     198          PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
     199          std::list<ShellCommand*>::const_iterator cmdIT;
     200          for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
     201          {
     202            PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
     203            /// FIXME
     204            /*          for (unsigned int i = 0; i< elem->paramCount; i++)
     205              PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
     206            if (!(*cmdIT)->description.empty())
     207              PRINT(0)("- %s", (*cmdIT)->description.c_str());
     208            PRINT(0)("\n");
     209          }
     210          return;
     211        }
     212      }
     213      PRINTF(3)("Class %s not found in Command's classes\n", className.c_str());
     214    }
     215    else
     216    {
     217      PRINTF(1)("List of commandClasses does not exist");
     218    }
     219  }
     220
    125221}
    126 
    127 /**
    128  * checks if a Class is already registered to the Commands' class-stack
    129  * @param className the Name of the Class to check for
    130  * @returns the CommandClass if found, NULL otherwise
    131  */
    132 const ShellCommandClass* ShellCommandClass::isRegistered(const std::string& className)
    133 {
    134   if (ShellCommandClass::commandClassList == NULL)
    135     initCommandClassList();
    136 
    137   list<ShellCommandClass*>::const_iterator classIT;
    138   for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
    139   {
    140     if (className == (*classIT)->className)
    141     {
    142       if ((*classIT)->classID == CL_NULL)
    143         (*classIT)->classID = ClassList::StringToID(className);
    144 
    145       return (*classIT);
    146     }
    147   }
    148   return NULL;
    149 }
    150 
    151 /**
    152  * searches for a CommandClass
    153  * @param className the name of the CommandClass
    154  * @returns the CommandClass if found, or a new CommandClass if not
    155  */
    156 ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className)
    157 {
    158   if (ShellCommandClass::commandClassList == NULL)
    159     initCommandClassList();
    160 
    161   list<ShellCommandClass*>::iterator classIT;
    162   for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
    163   {
    164     if (className == (*classIT)->className)
    165     {
    166       return (*classIT);
    167     }
    168   }
    169   return new ShellCommandClass(className);
    170 }
    171 
    172 /**
    173  * initializes the CommandList (if it is NULL)
    174  */
    175 void ShellCommandClass::initCommandClassList()
    176 {
    177   if (ShellCommandClass::commandClassList == NULL)
    178   {
    179     ShellCommandClass::commandClassList = new std::list<ShellCommandClass*>;
    180     ShellCommand::registerCommand("debug", "ShellCommand", ExecutorStatic<ShellCommand>(ShellCommand::debug));
    181   }
    182 }
    183 
    184 /**
    185  * displays help about ShellCommandClass
    186  * @param className: the Class of Commands to show help about
    187  */
    188 void ShellCommandClass::help(const std::string& className)
    189 {
    190   if (likely(ShellCommandClass::commandClassList != NULL))
    191   {
    192     list<ShellCommandClass*>::iterator classIT;
    193     for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
    194     {
    195       if (className == (*classIT)->className)
    196       {
    197         PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
    198         list<ShellCommand*>::const_iterator cmdIT;
    199         for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
    200         {
    201           PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
    202           /// FIXME
    203           /*          for (unsigned int i = 0; i< elem->paramCount; i++)
    204             PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
    205           if (!(*cmdIT)->description.empty())
    206             PRINT(0)("- %s", (*cmdIT)->description.c_str());
    207           PRINT(0)("\n");
    208         }
    209         return;
    210       }
    211     }
    212     PRINTF(3)("Class %s not found in Command's classes\n", className.c_str());
    213   }
    214   else
    215   {
    216     PRINTF(1)("List of commandClasses does not exist");
    217   }
    218 }
    219 
Note: See TracChangeset for help on using the changeset viewer.