Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/shell/shell_command_class.cc @ 5712

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

orxonox/trunk: better loadparam start and end-cycle.

File size: 7.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_class.h"
19
20#include "shell_command.h"
21
22#include "list.h"
23#include "debug.h"
24#include "class_list.h"
25
26#include <stdio.h>
27#include <string.h>
28
29using namespace std;
30
31tList<ShellCommandClass>* ShellCommandClass::commandClassList = NULL;
32tList<ShellCommandAlias>* ShellCommandClass::aliasList = NULL;
33
34/**
35 * creates a new ShellCommandClass
36 * @param className the Name of the command-class to create
37 */
38ShellCommandClass::ShellCommandClass(const char* className)
39{
40  this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass");
41  this->setName(className);
42
43  this->className = className;
44  this->classID = CL_NULL;
45  this->commandList = new tList<ShellCommand>;
46
47  ShellCommandClass::commandClassList->add(this);
48}
49
50/**
51 * destructs the shellCommandClass again
52 */
53ShellCommandClass::~ShellCommandClass()
54{
55  tIterator<ShellCommand>* iterator = this->commandList->getIterator();
56  ShellCommand* elem = iterator->firstElement();
57  while(elem != NULL)
58  {
59    delete elem;
60    elem = iterator->nextElement();
61  }
62  delete iterator;
63  delete this->commandList;
64}
65
66/**
67 * collects the Commands registered to some class.
68 * @param className the name of the Class to collect the Commands from.
69 * @param stringList a List to paste the Commands into.
70 * @returns true on success, false otherwise
71 */
72bool ShellCommandClass::getCommandListOfClass(const char* className, tList<const char>* stringList)
73{
74  if (stringList == NULL || className == NULL)
75    return false;
76
77  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
78  ShellCommandClass* elem = iterator->firstElement();
79  while(elem != NULL)
80  {
81    if (!strcmp (elem->getName(), className))
82    {
83      tIterator<ShellCommand>* itFkt = elem->commandList->getIterator();
84      ShellCommand* command = itFkt->firstElement();
85      while (command != NULL)
86      {
87        stringList->add(command->getName());
88        command = itFkt->nextElement();
89      }
90      delete itFkt;
91    }
92
93    elem = iterator->nextElement();
94  }
95  delete iterator;
96  return true;
97}
98
99/**
100 * collects the Aliases registered to the ShellCommands
101 * @param stringList a List to paste the Aliases into.
102 * @returns true on success, false otherwise
103 */
104bool ShellCommandClass::getCommandListOfAlias(tList<const char>* stringList)
105{
106  if (stringList == NULL || ShellCommandClass::aliasList == NULL)
107    return false;
108
109  tIterator<ShellCommandAlias>* iterator = ShellCommandClass::aliasList->getIterator();
110   ShellCommandAlias* elem = iterator->firstElement();
111   while(elem != NULL)
112   {
113     stringList->add(elem->getName());
114     elem = iterator->nextElement();
115   }
116   delete iterator;
117   return true;
118}
119
120/**
121 * unregisters all Commands that exist
122 */
123void ShellCommandClass::unregisterAllCommands()
124{
125  if (ShellCommandClass::commandClassList != NULL)
126  {
127    // unregister all commands
128    tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
129    ShellCommandClass* elem = iterator->firstElement();
130    while(elem != NULL)
131    {
132      delete elem;
133
134      elem = iterator->nextElement();
135    }
136    delete iterator;
137
138    delete ShellCommandClass::commandClassList;
139    ShellCommandClass::commandClassList = NULL;
140  }
141
142  // unregister all aliases (there should be nothing to do here :))
143  if (ShellCommandClass::aliasList != NULL)
144  {
145    tIterator<ShellCommandAlias>* itAL = ShellCommandClass::aliasList->getIterator();
146    ShellCommandAlias* elemAL = itAL->firstElement();
147    while(elemAL != NULL)
148    {
149      delete elemAL;
150      elemAL = itAL->nextElement();
151    }
152    delete itAL;
153    delete ShellCommandClass::aliasList;
154    ShellCommandClass::aliasList = NULL;
155  }
156}
157
158/**
159 * checks if a Class is already registered to the Commands' class-stack
160 * @param className the Name of the Class to check for
161 * @returns the CommandClass if found, NULL otherwise
162 */
163const ShellCommandClass* ShellCommandClass::isRegistered(const char* className)
164{
165  if (ShellCommandClass::commandClassList == NULL)
166    initCommandClassList();
167
168  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
169  ShellCommandClass* elem = iterator->firstElement();
170  while(elem != NULL)
171  {
172    if (!strcmp(className, elem->className))
173    {
174      if (elem->classID == CL_NULL)
175        elem->classID = ClassList::StringToID(className);
176
177      delete iterator;
178      return elem;
179    }
180    elem = iterator->nextElement();
181  }
182  delete iterator;
183  return NULL;
184}
185
186/**
187 * searches for a CommandClass
188 * @param className the name of the CommandClass
189 * @returns the CommandClass if found, or a new CommandClass if not
190 */
191ShellCommandClass* ShellCommandClass::getCommandClass(const char* className)
192{
193  if (ShellCommandClass::commandClassList == NULL)
194    initCommandClassList();
195
196  tIterator<ShellCommandClass>* iterator = ShellCommandClass::commandClassList->getIterator();
197  ShellCommandClass* elem = iterator->firstElement();
198  while(elem != NULL)
199  {
200    if (!strcmp(className, elem->className))
201    {
202      delete iterator;
203      return elem;
204    }
205    elem = iterator->nextElement();
206  }
207  delete iterator;
208  return new ShellCommandClass(className);
209}
210
211/**
212 * initializes the CommandList (if it is NULL)
213 */
214void ShellCommandClass::initCommandClassList()
215{
216  if (ShellCommandClass::commandClassList == NULL)
217  {
218    ShellCommandClass::commandClassList = new tList<ShellCommandClass>;
219    ShellCommand::registerCommand("debug", "ShellCommand", ExecutorStatic<ShellCommand>(ShellCommand::debug));
220  }
221}
222
223/**
224 * displays help about ShellCommandClass
225 * @param className: the Class of Commands to show help about
226 */
227void ShellCommandClass::help(const char* className)
228{
229  if (className == NULL)
230    return;
231  if (likely(ShellCommandClass::commandClassList != NULL))
232  {
233    tIterator<ShellCommandClass>* itCL = ShellCommandClass::commandClassList->getIterator();
234    ShellCommandClass* elemCL = itCL->firstElement();
235    while(elemCL != NULL)
236    {
237      if (elemCL->className && !strcasecmp(className, elemCL->className))
238      {
239        PRINT(0)("Class:'%s' registered %d commands: \n", elemCL->className, elemCL->commandList->getSize());
240        tIterator<ShellCommand>* iterator = elemCL->commandList->getIterator();
241        const ShellCommand* elem = iterator->firstElement();
242        while(elem != NULL)
243        {
244          PRINT(0)("  command:'%s' : params:%d: ", elem->getName(), elem->executor->getParamCount());
245          /// FIXME
246          /*          for (unsigned int i = 0; i< elem->paramCount; i++)
247            PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
248          if (elem->description != NULL)
249            PRINT(0)("- %s", elem->description);
250          PRINT(0)("\n");
251          elem = iterator->nextElement();
252        }
253        delete iterator;
254
255        delete itCL;
256        return;
257      }
258      elemCL = itCL->nextElement();
259    }
260    delete itCL;
261    PRINTF(3)("Class %s not found in Command's classes\n", className);
262  }
263  else
264  {
265    PRINTF(1)("List of commandClasses does not exist");
266  }
267}
268
Note: See TracBrowser for help on using the repository browser.