Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: one Parameter safe now

File size: 7.6 KB
Line 
1/*
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.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "shell_command.h"
19
20#include "list.h"
21#include "debug.h"
22#include "class_list.h"
23
24#include "key_names.h"
25#include <stdarg.h>
26#include <stdio.h>
27
28using namespace std;
29
30ShellCommandBase::ShellCommandBase(const char* commandName, ClassID classID, unsigned int paramCount, ...)
31{
32  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
33  this->setName(commandName);
34
35  this->classID = classID;
36  this->className = ClassList::IDToString(classID);
37
38  if (classID & CL_MASK_SINGLETON == CL_MASK_SINGLETON)
39    this->isSingleton = true;
40  else
41    this->isSingleton = false;
42
43  // handling parameters, and storing them:
44  if (paramCount > FUNCTOR_MAX_ARGUMENTS)
45    paramCount = FUNCTOR_MAX_ARGUMENTS;
46  this->paramCount = paramCount;
47  this->parameters = new unsigned int[paramCount];
48
49  va_list parameterList;
50  va_start(parameterList, paramCount);
51
52  for (unsigned int i = 0; i < paramCount; i++)
53  {
54    this->parameters[i] = va_arg(parameterList, int);
55
56    switch (this->parameters[i])
57    {
58      case ParameterBool:
59        this->defaultBools[i] = va_arg(parameterList, int);
60        break;
61      case ParameterChar:
62        this->defaultStrings[i] = new char[2];
63        sprintf(this->defaultStrings[0], "%c",  va_arg(parameterList, int));
64        break;
65      case ParameterString:
66        this->defaultStrings[i] = va_arg(parameterList, char*);
67        break;
68      case ParameterInt:
69        this->defaultInts[i] = va_arg(parameterList, int);
70        break;
71      case ParameterUInt:
72        this->defaultInts[i] = va_arg(parameterList, unsigned int);
73        break;
74      case ParameterFloat:
75        this->defaultFloats[i] = va_arg(parameterList, double);
76        break;
77      case ParameterLong:
78        this->defaultInts[i] = va_arg(parameterList, long);
79        break;
80      default:
81        break;
82    }
83  }
84
85  // adding this ShellCommand to the list of known Commands
86  ShellCommandBase::commandList->add(this);
87}
88
89ShellCommandBase::~ShellCommandBase()
90{
91  delete[] this->parameters;
92}
93
94
95tList<ShellCommandBase>* ShellCommandBase::commandList = NULL;
96
97bool ShellCommandBase::isRegistered(const char* commandName, ClassID classID, unsigned int paramCount, ...)
98{
99  va_list parameterList;
100  va_start(parameterList, paramCount);
101
102  if (ShellCommandBase::commandList == NULL)
103  {
104    ShellCommandBase::commandList = new tList<ShellCommandBase>;
105    ShellCommand<ShellCommandBase>::registerCommand("debug", CL_SHELL_COMMAND, &ShellCommandBase::debug);
106    return false;
107  }
108
109  tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
110  ShellCommandBase* elem = iterator->firstElement();
111  while(elem != NULL)
112  {
113    if (classID == elem->classID && !strcmp(commandName, elem->getName()))
114    {
115      PRINTF(2)("Command already registered\n");
116      delete iterator;
117      return true;
118    }
119    elem = iterator->nextElement();
120  }
121  delete iterator;
122  return false;
123}
124
125
126/**
127 * executes commands
128 * @param executionString the string containing the following input
129 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
130 * @return true on success, false otherwise.
131 */
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  {
141    printf("%s\n", elem->getName());
142    if (!strncasecmp (executionString, elem->className, strlen(elem->className)) &&
143         (*(executionString+strlen(elem->className)) == ' ' ||
144          *(executionString+strlen(elem->className)) == ':' ))
145    {
146      const char* commandBegin = executionString + strlen(elem->className);
147
148      PRINTF(4)("Class %s matches\n", elem->className);
149      BaseObject* objectPointer = NULL;
150      if (elem->isSingleton)
151      {
152        while(*commandBegin == ' ')
153          commandBegin++;
154        if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
155        {
156          elem = iterator->nextElement();
157          continue;
158        }
159        PRINTF(4)("Command %s matches\n", elem->getName());
160        // getting singleton-reference
161        tList<BaseObject>* list =  ClassList::getList(elem->classID);
162        if (list != NULL)
163          objectPointer = list->firstElement();
164      }
165      else
166      {
167        // checking for the Object
168        while(*commandBegin == ' ')
169          commandBegin++;
170        tList<BaseObject>* list =  ClassList::getList(elem->classID);
171        if (list == NULL)
172          break;
173        tIterator<BaseObject>* iterBO = list->getIterator();
174        BaseObject* enumBO = iterBO->firstElement();
175        while(enumBO != NULL)
176        {
177          if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName())))
178          {
179            PRINTF(4)("Object %s matches\n", enumBO->getName());
180            objectPointer = enumBO;
181            break;
182          }
183          enumBO = iterBO->nextElement();
184        }
185        delete iterBO;
186
187        // break on no object Found. We cannot operate on Classes, but on Objects
188        if (objectPointer == NULL)
189          break;
190        commandBegin = commandBegin + strlen(objectPointer->getName());
191        while(*commandBegin == ' ')
192          commandBegin++;
193        // checking for the requested function.
194        if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
195        {
196          elem = iterator->nextElement();
197          continue;
198        }
199        PRINTF(4)("Function '%s' found\n", commandBegin);
200      }
201      const char* paramBegin = strchr(commandBegin, ' ');
202      if (paramBegin == NULL)
203        paramBegin = commandBegin + strlen(elem->getName());
204      while (*paramBegin == ' ')
205        paramBegin++;
206
207      PRINTF(3)("Parameters to Pass: %s\n", paramBegin);
208      if (objectPointer != NULL && paramBegin != NULL)
209      {
210        elem->executeCommand(objectPointer, paramBegin);
211        delete iterator;
212        return true;
213      }
214    }
215    elem = iterator->nextElement();
216  }
217  delete iterator;
218  return true;
219}
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.