Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_command.cc @ 5166

Last change on this file since 5166 was 5166, checked in by bensch, 20 years ago

orxonox/trunk: doxygen-tags

File size: 9.8 KB
RevLine 
[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
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[5129]18#include "shell_command.h"
[1853]19
[5072]20#include "list.h"
[5129]21#include "debug.h"
[5113]22#include "class_list.h"
23
24#include "key_names.h"
[5075]25#include <stdarg.h>
26#include <stdio.h>
27
[1856]28using namespace std;
[1853]29
[5166]30/**
31 * constructs and registers a new Command
32 * @param commandName the name of the Command
33 * @param className the name of the class to apply this command to
34 * @param paramCount the count of parameters this command takes
35 * @return self
36 */
[5161]37ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...)
[3365]38{
[5141]39  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
40  this->setName(commandName);
[5164]41  this->description = NULL;
[5141]42
[5161]43//  this->classID = classID;
44  this->className = className; //ClassList::IDToString(classID);
[5141]45
[5130]46  // handling parameters, and storing them:
[5142]47  if (paramCount > FUNCTOR_MAX_ARGUMENTS)
48    paramCount = FUNCTOR_MAX_ARGUMENTS;
[5130]49  this->paramCount = paramCount;
[5148]50  this->parameters = new unsigned int[paramCount];
[5130]51
[5148]52  va_list parameterList;
53  va_start(parameterList, paramCount);
54
[5130]55  for (unsigned int i = 0; i < paramCount; i++)
[5142]56  {
[5148]57    this->parameters[i] = va_arg(parameterList, int);
[5130]58
[5146]59    switch (this->parameters[i])
[5142]60    {
61      case ParameterBool:
[5148]62        this->defaultBools[i] = va_arg(parameterList, int);
[5142]63        break;
64      case ParameterChar:
65        this->defaultStrings[i] = new char[2];
[5148]66        sprintf(this->defaultStrings[0], "%c",  va_arg(parameterList, int));
[5142]67        break;
68      case ParameterString:
[5148]69        this->defaultStrings[i] = va_arg(parameterList, char*);
[5142]70        break;
71      case ParameterInt:
[5148]72        this->defaultInts[i] = va_arg(parameterList, int);
[5142]73        break;
74      case ParameterUInt:
[5148]75        this->defaultInts[i] = va_arg(parameterList, unsigned int);
[5142]76        break;
77      case ParameterFloat:
[5148]78        this->defaultFloats[i] = va_arg(parameterList, double);
[5142]79        break;
80      case ParameterLong:
[5148]81        this->defaultInts[i] = va_arg(parameterList, long);
[5142]82        break;
83      default:
84        break;
85    }
86  }
87
[5130]88  // adding this ShellCommand to the list of known Commands
[5129]89  ShellCommandBase::commandList->add(this);
[5068]90}
[4320]91
[5166]92/**
93 * deconstructs a ShellCommand
94 * @return
95 */
[5130]96ShellCommandBase::~ShellCommandBase()
97{
98  delete[] this->parameters;
99}
[1853]100
[5166]101/**
102 * unregisters all Commands that exist
103 */
[5165]104void ShellCommandBase::unregisterAllCommands()
105{
106  tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
107  ShellCommandBase* elem = iterator->firstElement();
108  while(elem != NULL)
109  {
110    delete elem;
111
112    elem = iterator->nextElement();
113  }
114  delete iterator;
115
116  delete ShellCommandBase::commandList;
117  ShellCommandBase::commandList = NULL;
118}
119
[5166]120/**
121 * unregister an existing commandName
122 * @param className the name of the Class the command belongs to.
123 * @param commandName the name of the command itself
124 *
125 * @todo implement
126 */
127void ShellCommandBase::unregisterCommand(const char* commandName, const char* className)
[5165]128{
129  PRINTF(1)("IMPLEMENT THIS\n");
130}
131
[5129]132tList<ShellCommandBase>* ShellCommandBase::commandList = NULL;
[5079]133
[5161]134
[5166]135/**
136 * checks if a command has already been registered.
137 * @param commandName the name of the Command
138 * @param className the name of the Class the command should apply to.
139 * @param paramCount how many arguments the Command takes
140 * @returns true, if the command is registered/false otherwise
141 *
142 * This is used internally, to see, if we have multiple command subscriptions.
143 * This is checked in the registerCommand-function.
144 */
[5161]145bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...)
[5113]146{
[5148]147  va_list parameterList;
148  va_start(parameterList, paramCount);
[5135]149
[5129]150  if (ShellCommandBase::commandList == NULL)
[5072]151  {
[5129]152    ShellCommandBase::commandList = new tList<ShellCommandBase>;
[5161]153    ShellCommand<ShellCommandBase>::registerCommand("debug", "ShellCommand", &ShellCommandBase::debugDyn);
[5113]154    return false;
155  }
[5105]156
[5129]157  tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
158  ShellCommandBase* elem = iterator->firstElement();
159  while(elem != NULL)
[5113]160  {
[5161]161    if (!strcmp(className, elem->className) && !strcmp(commandName, elem->getName()))
[5113]162    {
[5140]163      PRINTF(2)("Command already registered\n");
[5129]164      delete iterator;
165      return true;
[5113]166    }
[5129]167    elem = iterator->nextElement();
[5113]168  }
[5129]169  delete iterator;
[5140]170  return false;
[5113]171}
172
[5140]173
[5145]174/**
175 * executes commands
176 * @param executionString the string containing the following input
[5148]177 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
[5145]178 * @return true on success, false otherwise.
179 */
[5135]180bool ShellCommandBase::execute(const char* executionString)
181{
182  if (ShellCommandBase::commandList == NULL)
183    return false;
184
185  tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
186  ShellCommandBase* elem = iterator->firstElement();
187  while(elem != NULL)
188  {
[5155]189    printf("%s::%s\n", elem->className, elem->getName());
[5142]190    if (!strncasecmp (executionString, elem->className, strlen(elem->className)) &&
191         (*(executionString+strlen(elem->className)) == ' ' ||
192          *(executionString+strlen(elem->className)) == ':' ))
[5135]193    {
[5142]194      const char* commandBegin = executionString + strlen(elem->className);
195
[5146]196      PRINTF(4)("Class %s matches\n", elem->className);
[5142]197      BaseObject* objectPointer = NULL;
[5163]198      if (ClassList::StringToID(elem->className) & CL_MASK_SINGLETON == CL_MASK_SINGLETON)
[5142]199      {
200        while(*commandBegin == ' ')
201          commandBegin++;
[5154]202        if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())) ||
203            *(commandBegin + strlen(elem->getName())) != ' ' &&
204            *(commandBegin + strlen(elem->getName())) != '\0')
[5144]205        {
206          elem = iterator->nextElement();
[5143]207          continue;
[5144]208        }
[5148]209        PRINTF(4)("Command %s matches\n", elem->getName());
[5144]210        // getting singleton-reference
[5161]211        tList<BaseObject>* list =  ClassList::getList(elem->className);
[5148]212        if (list != NULL)
[5145]213          objectPointer = list->firstElement();
[5142]214      }
215      else
216      {
[5144]217        // checking for the Object
[5143]218        while(*commandBegin == ' ')
219          commandBegin++;
[5161]220        tList<BaseObject>* list = ClassList::getList(elem->className);
[5145]221        if (list == NULL)
222          break;
223        tIterator<BaseObject>* iterBO = list->getIterator();
[5143]224        BaseObject* enumBO = iterBO->firstElement();
[5144]225        while(enumBO != NULL)
[5143]226        {
[5144]227          if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName())))
[5143]228          {
[5146]229            PRINTF(4)("Object %s matches\n", enumBO->getName());
[5143]230            objectPointer = enumBO;
231            break;
232          }
233          enumBO = iterBO->nextElement();
234        }
235        delete iterBO;
[5142]236
[5144]237        // break on no object Found. We cannot operate on Classes, but on Objects
[5143]238        if (objectPointer == NULL)
239          break;
240        commandBegin = commandBegin + strlen(objectPointer->getName());
241        while(*commandBegin == ' ')
242          commandBegin++;
[5144]243        // checking for the requested function.
244        if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
245        {
246          elem = iterator->nextElement();
247          continue;
248        }
[5148]249        PRINTF(4)("Function '%s' found\n", commandBegin);
[5142]250      }
[5144]251      const char* paramBegin = strchr(commandBegin, ' ');
252      if (paramBegin == NULL)
253        paramBegin = commandBegin + strlen(elem->getName());
[5147]254      while (*paramBegin == ' ')
255        paramBegin++;
[5142]256
[5148]257      PRINTF(3)("Parameters to Pass: %s\n", paramBegin);
[5146]258      if (objectPointer != NULL && paramBegin != NULL)
[5143]259      {
[5144]260        elem->executeCommand(objectPointer, paramBegin);
[5143]261        delete iterator;
262        return true;
263      }
[5135]264    }
265    elem = iterator->nextElement();
266  }
267  delete iterator;
268  return true;
269}
[5148]270
[5166]271/**
272 * lets a command be described
273 * @param description the description of the Given command
274 */
[5164]275ShellCommandBase* ShellCommandBase::describe(const char* description)
276{
277  if (this == NULL)
278    return NULL;
[5165]279 else
280 {
281   this->description = description;
282   return this;
283 }
[5164]284}
285
[5166]286/**
287 * see ShellCommandBase::debug()
288 */
[5161]289void ShellCommandBase::debugDyn()
290{
291  this->debug();
292}
[5148]293
[5166]294/**
295 * prints out nice information about the Shells Commands
296 */
[5148]297void ShellCommandBase::debug()
298{
299  if (ShellCommandBase::commandList == NULL)
300  {
301    PRINT(0)("No Command registered so far\n");
302    return;
303  }
304
305  tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
306  ShellCommandBase* elem = iterator->firstElement();
307  while(elem != NULL)
308  {
[5166]309    PRINT(0)("Class:'%s':command:'%s':params:%d: ", elem->className, elem->getName(), elem->paramCount);
[5148]310    for (unsigned int i = 0; i< elem->paramCount; i++)
[5164]311      printf("%s ", ShellCommandBase::paramToString(elem->parameters[i]));
312    if (elem->description != NULL)
313      printf("- %s", elem->description);
[5148]314    printf("\n");
315
316    elem = iterator->nextElement();
317  }
318  delete iterator;
319}
320
[5166]321/**
322 * converts a Parameter to a String
323 * @param parameter the Parameter we have.
324 * @returns the Name of the Parameter at Hand
325 */
[5148]326const char* ShellCommandBase::paramToString(long parameter)
327{
328  switch (parameter)
329  {
330    case ParameterBool:
331      return "BOOL";
332      break;
333    case ParameterChar:
334      return "CHAR";
335      break;
336    case ParameterString:
337      return "STRING";
338      break;
339    case ParameterInt:
340      return "INT";
341      break;
342    case ParameterUInt:
343      return "UINT";
344      break;
345    case ParameterFloat:
346      return "FLOAT";
347      break;
348    case ParameterLong:
349      return "LONG";
350      break;
351    default:
352      return "NULL";
353      break;
354  }
355}
Note: See TracBrowser for help on using the repository browser.