Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/shell_command.cc @ 5150

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

orxonox/trunk: one Parameter safe now

File size: 7.6 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
[5135]30ShellCommandBase::ShellCommandBase(const char* commandName, ClassID classID, unsigned int paramCount, ...)
[3365]31{
[5141]32  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
33  this->setName(commandName);
34
[5129]35  this->classID = classID;
[5141]36  this->className = ClassList::IDToString(classID);
37
[5140]38  if (classID & CL_MASK_SINGLETON == CL_MASK_SINGLETON)
39    this->isSingleton = true;
40  else
41    this->isSingleton = false;
42
[5130]43  // handling parameters, and storing them:
[5142]44  if (paramCount > FUNCTOR_MAX_ARGUMENTS)
45    paramCount = FUNCTOR_MAX_ARGUMENTS;
[5130]46  this->paramCount = paramCount;
[5148]47  this->parameters = new unsigned int[paramCount];
[5130]48
[5148]49  va_list parameterList;
50  va_start(parameterList, paramCount);
51
[5130]52  for (unsigned int i = 0; i < paramCount; i++)
[5142]53  {
[5148]54    this->parameters[i] = va_arg(parameterList, int);
[5130]55
[5146]56    switch (this->parameters[i])
[5142]57    {
58      case ParameterBool:
[5148]59        this->defaultBools[i] = va_arg(parameterList, int);
[5142]60        break;
61      case ParameterChar:
62        this->defaultStrings[i] = new char[2];
[5148]63        sprintf(this->defaultStrings[0], "%c",  va_arg(parameterList, int));
[5142]64        break;
65      case ParameterString:
[5148]66        this->defaultStrings[i] = va_arg(parameterList, char*);
[5142]67        break;
68      case ParameterInt:
[5148]69        this->defaultInts[i] = va_arg(parameterList, int);
[5142]70        break;
71      case ParameterUInt:
[5148]72        this->defaultInts[i] = va_arg(parameterList, unsigned int);
[5142]73        break;
74      case ParameterFloat:
[5148]75        this->defaultFloats[i] = va_arg(parameterList, double);
[5142]76        break;
77      case ParameterLong:
[5148]78        this->defaultInts[i] = va_arg(parameterList, long);
[5142]79        break;
80      default:
81        break;
82    }
83  }
84
[5130]85  // adding this ShellCommand to the list of known Commands
[5129]86  ShellCommandBase::commandList->add(this);
[5068]87}
[4320]88
[5130]89ShellCommandBase::~ShellCommandBase()
90{
91  delete[] this->parameters;
92}
[1853]93
[5093]94
[5129]95tList<ShellCommandBase>* ShellCommandBase::commandList = NULL;
[5079]96
[5135]97bool ShellCommandBase::isRegistered(const char* commandName, ClassID classID, unsigned int paramCount, ...)
[5113]98{
[5148]99  va_list parameterList;
100  va_start(parameterList, paramCount);
[5135]101
[5129]102  if (ShellCommandBase::commandList == NULL)
[5072]103  {
[5129]104    ShellCommandBase::commandList = new tList<ShellCommandBase>;
[5148]105    ShellCommand<ShellCommandBase>::registerCommand("debug", CL_SHELL_COMMAND, &ShellCommandBase::debug);
[5113]106    return false;
107  }
[5105]108
[5129]109  tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
110  ShellCommandBase* elem = iterator->firstElement();
111  while(elem != NULL)
[5113]112  {
[5141]113    if (classID == elem->classID && !strcmp(commandName, elem->getName()))
[5113]114    {
[5140]115      PRINTF(2)("Command already registered\n");
[5129]116      delete iterator;
117      return true;
[5113]118    }
[5129]119    elem = iterator->nextElement();
[5113]120  }
[5129]121  delete iterator;
[5140]122  return false;
[5113]123}
124
[5140]125
[5145]126/**
127 * executes commands
128 * @param executionString the string containing the following input
[5148]129 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
[5145]130 * @return true on success, false otherwise.
131 */
[5135]132bool ShellCommandBase::execute(const char* executionString)
133{
134  if (ShellCommandBase::commandList == NULL)
135    return false;
136
137  tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
138  ShellCommandBase* elem = iterator->firstElement();
139  while(elem != NULL)
140  {
[5141]141    printf("%s\n", elem->getName());
[5142]142    if (!strncasecmp (executionString, elem->className, strlen(elem->className)) &&
143         (*(executionString+strlen(elem->className)) == ' ' ||
144          *(executionString+strlen(elem->className)) == ':' ))
[5135]145    {
[5142]146      const char* commandBegin = executionString + strlen(elem->className);
147
[5146]148      PRINTF(4)("Class %s matches\n", elem->className);
[5142]149      BaseObject* objectPointer = NULL;
150      if (elem->isSingleton)
151      {
152        while(*commandBegin == ' ')
153          commandBegin++;
154        if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
[5144]155        {
156          elem = iterator->nextElement();
[5143]157          continue;
[5144]158        }
[5148]159        PRINTF(4)("Command %s matches\n", elem->getName());
[5144]160        // getting singleton-reference
[5145]161        tList<BaseObject>* list =  ClassList::getList(elem->classID);
[5148]162        if (list != NULL)
[5145]163          objectPointer = list->firstElement();
[5142]164      }
165      else
166      {
[5144]167        // checking for the Object
[5143]168        while(*commandBegin == ' ')
169          commandBegin++;
[5145]170        tList<BaseObject>* list =  ClassList::getList(elem->classID);
171        if (list == NULL)
172          break;
173        tIterator<BaseObject>* iterBO = list->getIterator();
[5143]174        BaseObject* enumBO = iterBO->firstElement();
[5144]175        while(enumBO != NULL)
[5143]176        {
[5144]177          if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName())))
[5143]178          {
[5146]179            PRINTF(4)("Object %s matches\n", enumBO->getName());
[5143]180            objectPointer = enumBO;
181            break;
182          }
183          enumBO = iterBO->nextElement();
184        }
185        delete iterBO;
[5142]186
[5144]187        // break on no object Found. We cannot operate on Classes, but on Objects
[5143]188        if (objectPointer == NULL)
189          break;
190        commandBegin = commandBegin + strlen(objectPointer->getName());
191        while(*commandBegin == ' ')
192          commandBegin++;
[5144]193        // checking for the requested function.
194        if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
195        {
196          elem = iterator->nextElement();
197          continue;
198        }
[5148]199        PRINTF(4)("Function '%s' found\n", commandBegin);
[5142]200      }
[5144]201      const char* paramBegin = strchr(commandBegin, ' ');
202      if (paramBegin == NULL)
203        paramBegin = commandBegin + strlen(elem->getName());
[5147]204      while (*paramBegin == ' ')
205        paramBegin++;
[5142]206
[5148]207      PRINTF(3)("Parameters to Pass: %s\n", paramBegin);
[5146]208      if (objectPointer != NULL && paramBegin != NULL)
[5143]209      {
[5144]210        elem->executeCommand(objectPointer, paramBegin);
[5143]211        delete iterator;
212        return true;
213      }
[5135]214    }
215    elem = iterator->nextElement();
216  }
217  delete iterator;
218  return true;
219}
[5148]220
221
222void ShellCommandBase::debug()
223{
224  if (ShellCommandBase::commandList == NULL)
225  {
226    PRINT(0)("No Command registered so far\n");
227    return;
228  }
229
230  tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
231  ShellCommandBase* elem = iterator->firstElement();
232  while(elem != NULL)
233  {
234    PRINT(0)("Class %s registered command %s with %d parameters: ", elem->className, elem->getName(), elem->paramCount);
235    for (unsigned int i = 0; i< elem->paramCount; i++)
236      printf(ShellCommandBase::paramToString(elem->parameters[i]));
237    printf("\n");
238
239    elem = iterator->nextElement();
240  }
241  delete iterator;
242}
243
244
245
246const char* ShellCommandBase::paramToString(long parameter)
247{
248  switch (parameter)
249  {
250    case ParameterBool:
251      return "BOOL";
252      break;
253    case ParameterChar:
254      return "CHAR";
255      break;
256    case ParameterString:
257      return "STRING";
258      break;
259    case ParameterInt:
260      return "INT";
261      break;
262    case ParameterUInt:
263      return "UINT";
264      break;
265    case ParameterFloat:
266      return "FLOAT";
267      break;
268    case ParameterLong:
269      return "LONG";
270      break;
271    default:
272      return "NULL";
273      break;
274  }
275}
Note: See TracBrowser for help on using the repository browser.