Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the std-branche back, it runs on windows and Linux

svn merge https://svn.orxonox.net/orxonox/branches/std . -r7202:HEAD

File size: 6.6 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 std::string& className)
39  : className(className)
40{
41  this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass");
42  this->setName(className);
43
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 std::string& className, std::list<std::string>* stringList)
68{
69  if (stringList == NULL)
70    return false;
71
72  list<ShellCommandClass*>::iterator elem;
73  for(elem = ShellCommandClass::commandClassList->begin(); elem != ShellCommandClass::commandClassList->end(); elem++)
74  {
75    if (className == (*elem)->getName())
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<std::string>* 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 std::string& 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 (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 std::string& 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 (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 std::string& className)
189{
190  if (likely(ShellCommandClass::commandClassList != NULL))
191  {
192    list<ShellCommandClass*>::iterator classIT;
193    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
194    {
195      if (className == (*classIT)->className)
196      {
197        PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
198        list<ShellCommand*>::const_iterator cmdIT;
199        for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
200        {
201          PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
202          /// FIXME
203          /*          for (unsigned int i = 0; i< elem->paramCount; i++)
204            PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
205          if (!(*cmdIT)->description.empty())
206            PRINT(0)("- %s", (*cmdIT)->description.c_str());
207          PRINT(0)("\n");
208        }
209        return;
210      }
211    }
212    PRINTF(3)("Class %s not found in Command's classes\n", className.c_str());
213  }
214  else
215  {
216    PRINTF(1)("List of commandClasses does not exist");
217  }
218}
219
Note: See TracBrowser for help on using the repository browser.