Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: match on Singleton dynamically

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