Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5779 was 5779, checked in by bensch, 18 years ago

orxonox/trunk: ClassList is now in std::list style
ShellCommand is now in std::list style

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