Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5157 was 5155, checked in by bensch, 19 years ago

orxonox/trunk: now one should be able to create entities on the Fly

File size: 7.7 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::%s\n", elem->className, 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            *(commandBegin + strlen(elem->getName())) != ' ' &&
156            *(commandBegin + strlen(elem->getName())) != '\0')
157        {
158          elem = iterator->nextElement();
159          continue;
160        }
161        PRINTF(4)("Command %s matches\n", elem->getName());
162        // getting singleton-reference
163        tList<BaseObject>* list =  ClassList::getList(elem->classID);
164        if (list != NULL)
165          objectPointer = list->firstElement();
166      }
167      else
168      {
169        // checking for the Object
170        while(*commandBegin == ' ')
171          commandBegin++;
172        tList<BaseObject>* list = ClassList::getList(elem->classID);
173        if (list == NULL)
174          break;
175        tIterator<BaseObject>* iterBO = list->getIterator();
176        BaseObject* enumBO = iterBO->firstElement();
177        while(enumBO != NULL)
178        {
179          if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName())))
180          {
181            PRINTF(4)("Object %s matches\n", enumBO->getName());
182            objectPointer = enumBO;
183            break;
184          }
185          enumBO = iterBO->nextElement();
186        }
187        delete iterBO;
188
189        // break on no object Found. We cannot operate on Classes, but on Objects
190        if (objectPointer == NULL)
191          break;
192        commandBegin = commandBegin + strlen(objectPointer->getName());
193        while(*commandBegin == ' ')
194          commandBegin++;
195        // checking for the requested function.
196        if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
197        {
198          elem = iterator->nextElement();
199          continue;
200        }
201        PRINTF(4)("Function '%s' found\n", commandBegin);
202      }
203      const char* paramBegin = strchr(commandBegin, ' ');
204      if (paramBegin == NULL)
205        paramBegin = commandBegin + strlen(elem->getName());
206      while (*paramBegin == ' ')
207        paramBegin++;
208
209      PRINTF(3)("Parameters to Pass: %s\n", paramBegin);
210      if (objectPointer != NULL && paramBegin != NULL)
211      {
212        elem->executeCommand(objectPointer, paramBegin);
213        delete iterator;
214        return true;
215      }
216    }
217    elem = iterator->nextElement();
218  }
219  delete iterator;
220  return true;
221}
222
223
224void ShellCommandBase::debug()
225{
226  if (ShellCommandBase::commandList == NULL)
227  {
228    PRINT(0)("No Command registered so far\n");
229    return;
230  }
231
232  tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
233  ShellCommandBase* elem = iterator->firstElement();
234  while(elem != NULL)
235  {
236    PRINT(0)("Class %s registered command %s with %d parameters: ", elem->className, elem->getName(), elem->paramCount);
237    for (unsigned int i = 0; i< elem->paramCount; i++)
238      printf(ShellCommandBase::paramToString(elem->parameters[i]));
239    printf("\n");
240
241    elem = iterator->nextElement();
242  }
243  delete iterator;
244}
245
246
247
248const char* ShellCommandBase::paramToString(long parameter)
249{
250  switch (parameter)
251  {
252    case ParameterBool:
253      return "BOOL";
254      break;
255    case ParameterChar:
256      return "CHAR";
257      break;
258    case ParameterString:
259      return "STRING";
260      break;
261    case ParameterInt:
262      return "INT";
263      break;
264    case ParameterUInt:
265      return "UINT";
266      break;
267    case ParameterFloat:
268      return "FLOAT";
269      break;
270    case ParameterLong:
271      return "LONG";
272      break;
273    default:
274      return "NULL";
275      break;
276  }
277}
Note: See TracBrowser for help on using the repository browser.