Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: only retrieve Classes, that actually have a Command associated with them

File size: 13.3 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#include <string.h>
28
29using namespace std;
30
31/**
32 * creates a new ShellCommandClass
33 * @param className the Name of the command-class to create
34 */
35ShellCommandClass::ShellCommandClass(const char* className)
36{
37  this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass");
38  this->setName(className);
39
40  this->className = className;
41  this->classID = CL_NULL;
42  this->commandList = new tList<ShellCommandBase>;
43
44  ShellCommandClass::commandClassList->add(this);
45}
46
47/**
48 * destructs the shellCommandClass again
49 */
50ShellCommandClass::~ShellCommandClass()
51{
52  tIterator<ShellCommandBase>* iterator = this->commandList->getIterator();
53  ShellCommandBase* elem = iterator->firstElement();
54  while(elem != NULL)
55  {
56    delete elem;
57    elem = iterator->nextElement();
58  }
59  delete iterator;
60  delete this->commandList;
61}
62
63/**
64 * unregisters all Commands that exist
65 */
66void ShellCommandClass::unregisterAllCommands()
67{
68  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
69  ShellCommandClass* elem = iterator->firstElement();
70  while(elem != NULL)
71  {
72    delete elem;
73
74    elem = iterator->nextElement();
75  }
76  delete iterator;
77
78  delete ShellCommandClass::commandClassList;
79  ShellCommandClass::commandClassList = NULL;
80}
81
82const ShellCommandClass* ShellCommandClass::isRegistered(const char* className)
83{
84  if (ShellCommandClass::commandClassList == NULL)
85    initCommandClassList();
86
87  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
88  ShellCommandClass* elem = iterator->firstElement();
89  while(elem != NULL)
90  {
91    if (!strcmp(className, elem->className))
92    {
93      if (elem->classID == CL_NULL)
94        elem->classID = ClassList::StringToID(className);
95
96      delete iterator;
97      return elem;
98    }
99    elem = iterator->nextElement();
100  }
101  delete iterator;
102  return NULL;
103}
104
105/**
106 * searches for a CommandClass
107 * @param className the name of the CommandClass
108 * @returns the CommandClass if found, or a new CommandClass if not
109 */
110ShellCommandClass* ShellCommandClass::getCommandClass(const char* className)
111{
112  if (ShellCommandClass::commandClassList == NULL)
113    initCommandClassList();
114
115  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
116  ShellCommandClass* elem = iterator->firstElement();
117  while(elem != NULL)
118  {
119    if (!strcmp(className, elem->className))
120    {
121      delete iterator;
122      return elem;
123    }
124    elem = iterator->nextElement();
125  }
126  delete iterator;
127  return new ShellCommandClass(className);
128}
129
130/**
131 * initializes the CommandList (if it is NULL)
132 */
133void ShellCommandClass::initCommandClassList()
134{
135  if (ShellCommandClass::commandClassList == NULL)
136  {
137    ShellCommandClass::commandClassList = new tList<ShellCommandClass>;
138    ShellCommand<ShellCommandBase>::registerCommand("debug", "ShellCommand", &ShellCommandBase::debugDyn);
139  }
140}
141
142tList<ShellCommandClass>* ShellCommandClass::commandClassList = NULL;
143
144/**
145 * constructs and registers a new Command
146 * @param commandName the name of the Command
147 * @param className the name of the class to apply this command to
148 * @param paramCount the count of parameters this command takes
149 * @return self
150 */
151ShellCommandBase::ShellCommandBase(const char* commandName, const char* className, unsigned int paramCount, ...)
152{
153  this->setClassID(CL_SHELL_COMMAND, "ShellCommand");
154  this->setName(commandName);
155  this->description = NULL;
156
157//  this->classID = classID;
158  ShellCommandClass::getCommandClass(className)->commandList->add(this); //ClassList::IDToString(classID);
159
160  // handling parameters, and storing them:
161  if (paramCount > FUNCTOR_MAX_ARGUMENTS)
162    paramCount = FUNCTOR_MAX_ARGUMENTS;
163  this->paramCount = paramCount;
164  this->parameters = new unsigned int[paramCount];
165
166  va_list parameterList;
167  va_start(parameterList, paramCount);
168
169  for (unsigned int i = 0; i < paramCount; i++)
170  {
171    this->parameters[i] = va_arg(parameterList, int);
172
173    switch (this->parameters[i])
174    {
175      case ParameterBool:
176        this->defaultBools[i] = va_arg(parameterList, int);
177        break;
178      case ParameterChar:
179        this->defaultStrings[i] = new char[2];
180        sprintf(this->defaultStrings[0], "%c",  va_arg(parameterList, int));
181        break;
182      case ParameterString:
183        this->defaultStrings[i] = va_arg(parameterList, char*);
184        break;
185      case ParameterInt:
186        this->defaultInts[i] = va_arg(parameterList, int);
187        break;
188      case ParameterUInt:
189        this->defaultInts[i] = va_arg(parameterList, unsigned int);
190        break;
191      case ParameterFloat:
192        this->defaultFloats[i] = va_arg(parameterList, double);
193        break;
194      case ParameterLong:
195        this->defaultInts[i] = va_arg(parameterList, long);
196        break;
197      default:
198        break;
199    }
200  }
201}
202
203/**
204 * deconstructs a ShellCommand
205 * @return
206 */
207ShellCommandBase::~ShellCommandBase()
208{
209  delete[] this->parameters;
210}
211
212/**
213 * unregister an existing commandName
214 * @param className the name of the Class the command belongs to.
215 * @param commandName the name of the command itself
216 *
217 * @todo implement
218 */
219void ShellCommandBase::unregisterCommand(const char* commandName, const char* className)
220{
221  if (ShellCommandClass::commandClassList == NULL)
222    ShellCommandClass::initCommandClassList();
223
224 const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
225
226 if (checkClass != NULL)
227  {
228    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
229    ShellCommandBase* elem = iterator->firstElement();
230    while(elem != NULL)
231    {
232      if (!strcmp(commandName, elem->getName()))
233      {
234        checkClass->commandList->remove(elem);
235        delete elem;
236        break;
237      }
238      elem = iterator->nextElement();
239    }
240    delete iterator;
241
242    if (checkClass->commandList->getSize() == 0)
243    {
244      ShellCommandClass::commandClassList->remove(checkClass);
245      delete checkClass;
246    }
247  }
248}
249
250/**
251 * checks if a command has already been registered.
252 * @param commandName the name of the Command
253 * @param className the name of the Class the command should apply to.
254 * @param paramCount how many arguments the Command takes
255 * @returns true, if the command is registered/false otherwise
256 *
257 * This is used internally, to see, if we have multiple command subscriptions.
258 * This is checked in the registerCommand-function.
259 */
260bool ShellCommandBase::isRegistered(const char* commandName, const char* className, unsigned int paramCount, ...)
261{
262  if (ShellCommandClass::commandClassList == NULL)
263  {
264    ShellCommandClass::initCommandClassList();
265    return false;
266  }
267
268  const ShellCommandClass* checkClass = ShellCommandClass::isRegistered(className);
269  if (checkClass != NULL)
270  {
271    tIterator<ShellCommandBase>* iterator = checkClass->commandList->getIterator();
272    ShellCommandBase* elem = iterator->firstElement();
273    while(elem != NULL)
274   {
275     if (!strcmp(commandName, elem->getName()))
276     {
277       PRINTF(2)("Command already registered\n");
278       delete iterator;
279       return true;
280      }
281     elem = iterator->nextElement();
282   }
283   delete iterator;
284   return false;
285  }
286  else
287    return false;
288}
289
290
291/**
292 * executes commands
293 * @param executionString the string containing the following input
294 * ClassName [ObjectName] functionName [parameter1[,parameter2[,...]]]
295 * @return true on success, false otherwise.
296 */
297bool ShellCommandBase::execute(const char* executionString)
298{
299//   if (ShellCommandBase::commandList == NULL)
300//     return false;
301//
302//   tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
303//   ShellCommandBase* elem = iterator->firstElement();
304//   while(elem != NULL)
305//   {
306//     printf("%s::%s\n", elem->className, elem->getName());
307//     if (!strncasecmp (executionString, elem->className, strlen(elem->className)) &&
308//          (*(executionString+strlen(elem->className)) == ' ' ||
309//           *(executionString+strlen(elem->className)) == ':' ))
310//     {
311//       const char* commandBegin = executionString + strlen(elem->className);
312//
313//       PRINTF(4)("Class %s matches\n", elem->className);
314//       BaseObject* objectPointer = NULL;
315//       if (ClassList::StringToID(elem->className) & CL_MASK_SINGLETON == CL_MASK_SINGLETON)
316//       {
317//         while(*commandBegin == ' ')
318//           commandBegin++;
319//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())) ||
320//             *(commandBegin + strlen(elem->getName())) != ' ' &&
321//             *(commandBegin + strlen(elem->getName())) != '\0')
322//         {
323//           elem = iterator->nextElement();
324//           continue;
325//         }
326//         PRINTF(4)("Command %s matches\n", elem->getName());
327//         // getting singleton-reference
328//         tList<BaseObject>* list =  ClassList::getList(elem->className);
329//         if (list != NULL)
330//           objectPointer = list->firstElement();
331//       }
332//       else
333//       {
334//         // checking for the Object
335//         while(*commandBegin == ' ')
336//           commandBegin++;
337//         tList<BaseObject>* list = ClassList::getList(elem->className);
338//         if (list == NULL)
339//           break;
340//         tIterator<BaseObject>* iterBO = list->getIterator();
341//         BaseObject* enumBO = iterBO->firstElement();
342//         while(enumBO != NULL)
343//         {
344//           if(!strncmp(commandBegin, enumBO->getName(), strlen(enumBO->getName())))
345//           {
346//             PRINTF(4)("Object %s matches\n", enumBO->getName());
347//             objectPointer = enumBO;
348//             break;
349//           }
350//           enumBO = iterBO->nextElement();
351//         }
352//         delete iterBO;
353//
354//         // break on no object Found. We cannot operate on Classes, but on Objects
355//         if (objectPointer == NULL)
356//           break;
357//         commandBegin = commandBegin + strlen(objectPointer->getName());
358//         while(*commandBegin == ' ')
359//           commandBegin++;
360//         // checking for the requested function.
361//         if (strncmp (commandBegin, elem->getName(), strlen(elem->getName())))
362//         {
363//           elem = iterator->nextElement();
364//           continue;
365//         }
366//         PRINTF(4)("Function '%s' found\n", commandBegin);
367//       }
368//       const char* paramBegin = strchr(commandBegin, ' ');
369//       if (paramBegin == NULL)
370//         paramBegin = commandBegin + strlen(elem->getName());
371//       while (*paramBegin == ' ')
372//         paramBegin++;
373//
374//       PRINTF(3)("Parameters to Pass: %s\n", paramBegin);
375//       if (objectPointer != NULL && paramBegin != NULL)
376//       {
377//         elem->executeCommand(objectPointer, paramBegin);
378//         delete iterator;
379//         return true;
380//       }
381//     }
382//     elem = iterator->nextElement();
383//   }
384//   delete iterator;
385//   return true;
386}
387
388/**
389 * lets a command be described
390 * @param description the description of the Given command
391 */
392ShellCommandBase* ShellCommandBase::describe(const char* description)
393{
394  if (this == NULL)
395    return NULL;
396 else
397 {
398   this->description = description;
399   return this;
400 }
401}
402
403/**
404 * see ShellCommandBase::debug()
405 */
406void ShellCommandBase::debugDyn()
407{
408  this->debug();
409}
410
411/**
412 * prints out nice information about the Shells Commands
413 */
414void ShellCommandBase::debug()
415{
416  if (ShellCommandClass::commandClassList == NULL)
417  {
418    PRINT(0)("No Command registered.\n");
419    return;
420  }
421
422  tIterator<ShellCommandClass>* iteratorCL = ShellCommandClass::commandClassList->getIterator();
423  ShellCommandClass* elemCL = iteratorCL->firstElement();
424  while(elemCL != NULL)
425  {
426    PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
427    tIterator<ShellCommandBase>* iterator = elemCL->commandList->getIterator();
428    const ShellCommandBase* elem = iterator->firstElement();
429    while(elem != NULL)
430    {
431      PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->paramCount);
432      for (unsigned int i = 0; i< elem->paramCount; i++)
433       printf("%s ", ShellCommandBase::paramToString(elem->parameters[i]));
434      if (elem->description != NULL)
435       printf("- %s", elem->description);
436      printf("\n");
437
438      elem = iterator->nextElement();
439    }
440    delete iterator;
441    elemCL = iteratorCL->nextElement();
442  }
443  delete iteratorCL;
444}
445
446/**
447 * converts a Parameter to a String
448 * @param parameter the Parameter we have.
449 * @returns the Name of the Parameter at Hand
450 */
451const char* ShellCommandBase::paramToString(long parameter)
452{
453  switch (parameter)
454  {
455    case ParameterBool:
456      return "BOOL";
457      break;
458    case ParameterChar:
459      return "CHAR";
460      break;
461    case ParameterString:
462      return "STRING";
463      break;
464    case ParameterInt:
465      return "INT";
466      break;
467    case ParameterUInt:
468      return "UINT";
469      break;
470    case ParameterFloat:
471      return "FLOAT";
472      break;
473    case ParameterLong:
474      return "LONG";
475      break;
476    default:
477      return "NULL";
478      break;
479  }
480}
Note: See TracBrowser for help on using the repository browser.