Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/heathaze/src/lib/shell/shell_command_class.cc @ 10292

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

orxonox/trunk: shell works again.

File size: 6.5 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_SHELL
17
18#include "shell_command_class.h"
19
20#include "shell_command.h"
21
22#include "debug.h"
23#include "compiler.h"
24
25
26
27namespace OrxShell
28{
29  ObjectListDefinition(ShellCommandClass);
30
31  CmdClassList* ShellCommandClass::_commandClassList = NULL;
32
33  /**
34   * @brief creates a new ShellCommandClass
35   * @param className the Name of the command-class to create
36   */
37  ShellCommandClass::ShellCommandClass(const std::string& className)
38      : _className(className)
39  {
40    this->registerObject(this, ShellCommandClass::_objectList);
41    this->setName(className);
42
43    if (ShellCommandClass::_commandClassList == NULL)
44      ShellCommandClass::_commandClassList = new CmdClassList;
45    ShellCommandClass::_commandClassList->push_back(this);
46  }
47
48  /**
49   * destructs the shellCommandClass again
50   */
51  ShellCommandClass::~ShellCommandClass()
52  {
53    while(!this->_commandList.empty())
54      delete this->_commandList.back();
55
56    if (ShellCommandClass::_commandClassList != NULL)
57    {
58      CmdClassList::iterator delClass = std::find(ShellCommandClass::_commandClassList->begin(), ShellCommandClass::_commandClassList->end(), this);
59      if (delClass != ShellCommandClass::_commandClassList->end())
60        ShellCommandClass::_commandClassList->erase(delClass);
61    }
62  }
63
64  /**
65   * @param command the Command to register.
66   */
67  void ShellCommandClass::registerCommand(ShellCommand* command)
68  {
69    this->_commandList.push_back(command);
70  }
71
72  /**
73   * @brief Unregisters a command.
74   * @param command the Command to unregister.
75   */
76  void ShellCommandClass::unregisterCommand(ShellCommand* command)
77  {
78    CmdList::iterator delC = std::find(this->_commandList.begin(), this->_commandList.end(), command);
79    if (delC != this->_commandList.end())
80      this->_commandList.erase(delC);
81  }
82
83  /**
84   * @brief unregisters all Commands that exist
85   */
86  void ShellCommandClass::unregisterAllCommands()
87  {
88    // unregister all commands and Classes
89    CmdClassList::iterator classIT;
90    if (ShellCommandClass::_commandClassList == NULL)
91      return;
92
93    while (!ShellCommandClass::_commandClassList->empty())
94      delete ShellCommandClass::_commandClassList->back();
95    delete ShellCommandClass::_commandClassList;
96    ShellCommandClass::_commandClassList = NULL;
97  }
98
99
100  /**
101   * @brief collects the Commands registered to some class.
102   * @param className the name of the Class to collect the Commands from.
103   * @param stringList a List to paste the Commands into.
104   * @returns true on success, false otherwise
105   */
106  bool ShellCommandClass::getCommandListOfClass(const std::string& className, std::list<std::string>& stringList)
107  {
108    if (ShellCommandClass::_commandClassList == NULL)
109      return false;
110
111
112    CmdClassList::const_iterator elem;
113    for(elem = ShellCommandClass::_commandClassList->begin(); elem != ShellCommandClass::_commandClassList->end(); elem++)
114    {
115      if (className == (*elem)->getName())
116      {
117        CmdList::iterator command;
118        for(command = (*elem)->_commandList.begin(); command != (*elem)->_commandList.end(); command++)
119          stringList.push_back((*command)->getName());
120        return true;
121      }
122    }
123    return false;
124  }
125
126
127  /**
128   * @brief 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   */
132  ShellCommandClass* ShellCommandClass::getCommandClass(const std::string& className)
133  {
134    if (ShellCommandClass::_commandClassList == NULL)
135      return false;
136
137
138    CmdClassList::const_iterator classIT;
139    for (classIT = ShellCommandClass::_commandClassList->begin(); classIT != ShellCommandClass::_commandClassList->end(); classIT++)
140      if (className == (*classIT)->_className)
141        return (*classIT);
142    return NULL;
143  }
144
145  /**
146   * @brief checks if a Class is already registered to the Commands' class-stack
147   * @param className the Name of the Class to check for
148   * @returns the CommandClass if found, NULL otherwise
149   */
150  bool ShellCommandClass::exists(const std::string& className)
151  {
152    return (ShellCommandClass::getCommandClass(className) != NULL);
153  }
154
155
156  /**
157   * @brief searches for a CommandClass
158   * @param className the name of the CommandClass
159   * @returns the CommandClass if found, or a new CommandClass if not
160   */
161  ShellCommandClass* ShellCommandClass::acquireCommandClass(const std::string& className)
162  {
163    ShellCommandClass* cmdClass = ShellCommandClass::getCommandClass(className);
164    if (cmdClass != NULL)
165      return (cmdClass);
166    return new ShellCommandClass(className);
167  }
168
169  /**
170   * @brief displays help about ShellCommandClass
171   * @param className: the Class of Commands to show help about
172   */
173  void ShellCommandClass::help(const std::string& className)
174  {
175    if (ShellCommandClass::_commandClassList == NULL)
176    {
177      PRINT(0)("No Commands Registered\n");
178      return;
179    }
180    if (className.empty())
181      PRINT(0)("===== Displaying %d registered Classes:\n", ShellCommandClass::_commandClassList->size());
182
183
184    CmdClassList::iterator classIT;
185    for (classIT = ShellCommandClass::_commandClassList->begin(); classIT != ShellCommandClass::_commandClassList->end(); classIT++)
186    {
187      if (className.empty() || className == (*classIT)->_className)
188      {
189        PRINT(0)("Class:'%s' registered %d commands: \n", (*classIT)->_className.c_str(), (*classIT)->_commandList.size());
190        CmdList::const_iterator cmdIT;
191        for (cmdIT = (*classIT)->_commandList.begin(); cmdIT != (*classIT)->_commandList.end(); cmdIT++)
192        {
193          PRINT(0)("  command:'%s' : params:%d: ", (*cmdIT)->getCName(), (*cmdIT)->executor->getParamCount());
194          /// FIXME
195          /*          for (unsigned int i = 0; i< elem->paramCount; i++)
196            PRINT(0)("%s ", ShellCommand::paramToString(elem->parameters[i]));*/
197          if (!(*cmdIT)->description.empty())
198            PRINT(0)("- %s", (*cmdIT)->description.c_str());
199          PRINT(0)("\n");
200        }
201        if (likely(!className.empty()))
202          return;
203      }
204    }
205    PRINTF(3)("Class '%s' not found in Command's classes\n", className.c_str());
206  }
207}
208
209
210
211
Note: See TracBrowser for help on using the repository browser.