Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/height_map/src/lib/shell/shell_command_class.cc @ 6461

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

orxonox/trunk: no more segfault in the Shell

File size: 6.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_class.h"
19
20#include "shell_command.h"
21
22#include "debug.h"
23#include "class_list.h"
24#include "compiler.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   if (ShellCommandClass::commandClassList != NULL)
107  {
108    // unregister all commands and Classes
109    std::list<ShellCommandClass*>::iterator classIT;
110    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
111      delete (*classIT);
112    delete ShellCommandClass::commandClassList;
113    ShellCommandClass::commandClassList = NULL;
114  }
115
116  // unregister all aliases (there should be nothing to do here :))
117  if (ShellCommandClass::aliasList != NULL)
118  {
119    std::list<ShellCommandAlias*>::iterator alias;
120    for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++)
121      delete (*alias);
122    delete ShellCommandClass::aliasList;
123    ShellCommandClass::aliasList = NULL;
124  }
125}
126
127/**
128 * checks if a Class is already registered to the Commands' class-stack
129 * @param className the Name of the Class to check for
130 * @returns the CommandClass if found, NULL otherwise
131 */
132const ShellCommandClass* ShellCommandClass::isRegistered(const char* className)
133{
134  if (ShellCommandClass::commandClassList == NULL)
135    initCommandClassList();
136
137  list<ShellCommandClass*>::const_iterator classIT;
138  for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
139  {
140    if (!strcmp(className, (*classIT)->className))
141    {
142      if ((*classIT)->classID == CL_NULL)
143        (*classIT)->classID = ClassList::StringToID(className);
144
145      return (*classIT);
146    }
147  }
148  return NULL;
149}
150
151/**
152 * searches for a CommandClass
153 * @param className the name of the CommandClass
154 * @returns the CommandClass if found, or a new CommandClass if not
155 */
156ShellCommandClass* ShellCommandClass::getCommandClass(const char* className)
157{
158  if (ShellCommandClass::commandClassList == NULL)
159    initCommandClassList();
160
161  list<ShellCommandClass*>::iterator classIT;
162  for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
163  {
164    if (!strcmp(className, (*classIT)->className))
165    {
166      return (*classIT);
167    }
168  }
169  return new ShellCommandClass(className);
170}
171
172/**
173 * initializes the CommandList (if it is NULL)
174 */
175void ShellCommandClass::initCommandClassList()
176{
177  if (ShellCommandClass::commandClassList == NULL)
178  {
179    ShellCommandClass::commandClassList = new std::list<ShellCommandClass*>;
180    ShellCommand::registerCommand("debug", "ShellCommand", ExecutorStatic<ShellCommand>(ShellCommand::debug));
181  }
182}
183
184/**
185 * displays help about ShellCommandClass
186 * @param className: the Class of Commands to show help about
187 */
188void ShellCommandClass::help(const char* className)
189{
190  if (className == NULL)
191    return;
192  if (likely(ShellCommandClass::commandClassList != NULL))
193  {
194    list<ShellCommandClass*>::iterator classIT;
195    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
196    {
197      if ((*classIT)->className && !strcasecmp(className, (*classIT)->className))
198      {
199        PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className, (*classIT)->commandList.size());
200        list<ShellCommand*>::const_iterator cmdIT;
201        for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
202        {
203          PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
204          /// FIXME
205          /*          for (unsigned int i = 0; i< elem->paramCount; i++)
206            PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
207          if ((*cmdIT)->description != NULL)
208            PRINT(0)("- %s", (*cmdIT)->description);
209          PRINT(0)("\n");
210        }
211        return;
212      }
213    }
214    PRINTF(3)("Class %s not found in Command's classes\n", className);
215  }
216  else
217  {
218    PRINTF(1)("List of commandClasses does not exist");
219  }
220}
221
Note: See TracBrowser for help on using the repository browser.