Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/shell/shell_command_class.cc @ 7913

Last change on this file since 7913 was 7374, checked in by bensch, 19 years ago

orxonox/trunk: added new Files shell_completion_plugin for the new Plugin Structure.
Also created the first namespace: OrxShell

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