Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: completions

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