Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/std/src/lib/shell/shell_command_class.cc @ 7219

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

orxonox/std: less char*

File size: 6.6 KB
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[5068]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[3955]16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
[1853]17
[5639]18#include "shell_command_class.h"
19
[5129]20#include "shell_command.h"
[1853]21
[5129]22#include "debug.h"
[5113]23#include "class_list.h"
[5780]24#include "compiler.h"
[5113]25
[5075]26#include <stdio.h>
[5174]27#include <string.h>
[5075]28
[1856]29using namespace std;
[1853]30
[5779]31std::list<ShellCommandClass*>* ShellCommandClass::commandClassList = NULL;
32std::list<ShellCommandAlias*>* ShellCommandClass::aliasList = NULL;
[5639]33
[5166]34/**
[5170]35 * creates a new ShellCommandClass
36 * @param className the Name of the command-class to create
37 */
[7216]38ShellCommandClass::ShellCommandClass(const std::string& className)
39  : className(className)
[5170]40{
[5188]41  this->setClassID(CL_SHELL_COMMAND_CLASS, "ShellCommandClass");
42  this->setName(className);
43
[5170]44  this->classID = CL_NULL;
45
[5779]46  ShellCommandClass::commandClassList->push_back(this);
[5170]47}
48
49/**
50 * destructs the shellCommandClass again
51 */
52ShellCommandClass::~ShellCommandClass()
53{
[5779]54  while(this->commandList.size() > 0)
[5170]55  {
[5779]56    delete this->commandList.front();
57    this->commandList.pop_front();
[5170]58  }
59}
60
[5197]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 */
[7216]67bool ShellCommandClass::getCommandListOfClass(const std::string& className, std::list<std::string>* stringList)
[5189]68{
[7216]69  if (stringList == NULL)
[5190]70    return false;
71
[5779]72  list<ShellCommandClass*>::iterator elem;
73  for(elem = ShellCommandClass::commandClassList->begin(); elem != ShellCommandClass::commandClassList->end(); elem++)
[5189]74  {
[7216]75    if (className == (*elem)->getName())
[5189]76    {
[5779]77      list<ShellCommand*>::iterator command;
78      for(command = (*elem)->commandList.begin(); command != (*elem)->commandList.end(); command++)
79        stringList->push_back((*command)->getName());
[5189]80    }
81  }
[5190]82  return true;
[5189]83}
84
[5197]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 */
[7216]90bool ShellCommandClass::getCommandListOfAlias(std::list<std::string>* stringList)
[5195]91{
[5196]92  if (stringList == NULL || ShellCommandClass::aliasList == NULL)
[5195]93    return false;
94
[5779]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;
[5195]99}
100
[5171]101/**
102 * unregisters all Commands that exist
103 */
104void ShellCommandClass::unregisterAllCommands()
105{
[5780]106   if (ShellCommandClass::commandClassList != NULL)
[5171]107  {
[5780]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);
[5636]112    delete ShellCommandClass::commandClassList;
113    ShellCommandClass::commandClassList = NULL;
[5171]114  }
115
[5195]116  // unregister all aliases (there should be nothing to do here :))
[5196]117  if (ShellCommandClass::aliasList != NULL)
[5195]118  {
[5780]119    std::list<ShellCommandAlias*>::iterator alias;
120    for (alias = ShellCommandClass::aliasList->begin(); alias != ShellCommandClass::aliasList->end(); alias++)
121      delete (*alias);
[5197]122    delete ShellCommandClass::aliasList;
123    ShellCommandClass::aliasList = NULL;
[5780]124  }
[5171]125}
126
[5197]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 */
[7219]132const ShellCommandClass* ShellCommandClass::isRegistered(const std::string& className)
[5170]133{
134  if (ShellCommandClass::commandClassList == NULL)
135    initCommandClassList();
136
[5779]137  list<ShellCommandClass*>::const_iterator classIT;
138  for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
[5170]139  {
[7216]140    if (className == (*classIT)->className)
[5170]141    {
[5779]142      if ((*classIT)->classID == CL_NULL)
143        (*classIT)->classID = ClassList::StringToID(className);
[5171]144
[5779]145      return (*classIT);
[5170]146    }
147  }
148  return NULL;
149}
150
[5172]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 */
[7216]156ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className)
[5170]157{
158  if (ShellCommandClass::commandClassList == NULL)
159    initCommandClassList();
160
[5779]161  list<ShellCommandClass*>::iterator classIT;
162  for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
[5170]163  {
[7216]164    if (className == (*classIT)->className)
[5170]165    {
[5779]166      return (*classIT);
[5170]167    }
168  }
169  return new ShellCommandClass(className);
170}
171
[5172]172/**
173 * initializes the CommandList (if it is NULL)
174 */
[5170]175void ShellCommandClass::initCommandClassList()
176{
177  if (ShellCommandClass::commandClassList == NULL)
178  {
[5779]179    ShellCommandClass::commandClassList = new std::list<ShellCommandClass*>;
[5641]180    ShellCommand::registerCommand("debug", "ShellCommand", ExecutorStatic<ShellCommand>(ShellCommand::debug));
[5170]181  }
182}
183
[5644]184/**
185 * displays help about ShellCommandClass
186 * @param className: the Class of Commands to show help about
187 */
[7216]188void ShellCommandClass::help(const std::string& className)
[5204]189{
190  if (likely(ShellCommandClass::commandClassList != NULL))
191  {
[5779]192    list<ShellCommandClass*>::iterator classIT;
193    for (classIT = ShellCommandClass::commandClassList->begin(); classIT != ShellCommandClass::commandClassList->end(); classIT++)
[5204]194    {
[7216]195      if (className == (*classIT)->className)
[5204]196      {
[7216]197        PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->className.c_str(), (*classIT)->commandList.size());
[5779]198        list<ShellCommand*>::const_iterator cmdIT;
199        for (cmdIT = (*classIT)->commandList.begin(); cmdIT != (*classIT)->commandList.end(); cmdIT++)
[5204]200        {
[5779]201          PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getName(), (*cmdIT)->executor->getParamCount());
[5642]202          /// FIXME
203          /*          for (unsigned int i = 0; i< elem->paramCount; i++)
204            PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
[7219]205          if (!(*cmdIT)->description.empty())
206            PRINT(0)("- %s", (*cmdIT)->description.c_str());
[5204]207          PRINT(0)("\n");
208        }
209        return;
210      }
211    }
[7216]212    PRINTF(3)("Class %s not found in Command's classes\n", className.c_str());
[5204]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.