Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/shell_command.cc @ 5139

Last change on this file since 5139 was 5138, checked in by bensch, 19 years ago

orxonox/trunk: clear is now registered for real

File size: 2.8 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
[5129]18#include "shell_command.h"
[1853]19
[5072]20#include "list.h"
[5129]21#include "debug.h"
[5113]22#include "class_list.h"
23
24#include "key_names.h"
[5075]25#include <stdarg.h>
26#include <stdio.h>
27
[1856]28using namespace std;
[1853]29
[5135]30ShellCommandBase::ShellCommandBase(const char* commandName, ClassID classID, unsigned int paramCount, ...)
[3365]31{
[5135]32  va_list parameters;
33  va_start(parameters, paramCount);
34
[5129]35  this->classID = classID;
36  this->commandName = new char[strlen(commandName)+1];
37  strcpy(this->commandName, commandName);
[5072]38
[5130]39  // handling parameters, and storing them:
40  this->paramCount = paramCount;
[5135]41  this->parameters = new ParameterType[paramCount];
[5130]42
43  for (unsigned int i = 0; i < paramCount; i++)
44    parameters[i] = va_arg(parameters, long);
45
46  // adding this ShellCommand to the list of known Commands
[5129]47  ShellCommandBase::commandList->add(this);
[5068]48}
[4320]49
[5130]50ShellCommandBase::~ShellCommandBase()
51{
52  delete[] this->commandName;
53  delete[] this->parameters;
54}
[1853]55
[5093]56
[5129]57tList<ShellCommandBase>* ShellCommandBase::commandList = NULL;
[5079]58
[5135]59bool ShellCommandBase::isRegistered(const char* commandName, ClassID classID, unsigned int paramCount, ...)
[5113]60{
[5135]61
62  va_list parameters;
63  va_start(parameters, paramCount);
64
[5129]65  if (ShellCommandBase::commandList == NULL)
[5072]66  {
[5129]67    ShellCommandBase::commandList = new tList<ShellCommandBase>;
[5113]68    return false;
69  }
[5105]70
[5129]71  tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
72  ShellCommandBase* elem = iterator->firstElement();
73  while(elem != NULL)
[5113]74  {
[5129]75    if (classID == elem->classID && !strcmp(commandName, elem->commandName))
[5113]76    {
[5129]77//      PRINTF(2)("Command already registered\n");
78      delete iterator;
79      return true;
[5113]80    }
[5129]81    elem = iterator->nextElement();
[5113]82  }
[5129]83  delete iterator;
[5113]84  return true;
85}
86
[5135]87bool ShellCommandBase::execute(const char* executionString)
88{
89  if (ShellCommandBase::commandList == NULL)
90    return false;
91
92  const char* commandEnd = strchr(executionString, ' ');
93  if (commandEnd == NULL)
94    commandEnd = executionString + strlen(executionString);
95
96  tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator();
97  ShellCommandBase* elem = iterator->firstElement();
98  while(elem != NULL)
99  {
[5138]100    if (!strncmp(executionString, elem->commandName, strlen(elem->commandName)))
[5135]101    {
102      elem->executeCommand(commandEnd);
103      delete iterator;
104      return true;
105    }
106    elem = iterator->nextElement();
107  }
108  delete iterator;
109  return true;
110}
111
Note: See TracBrowser for help on using the repository browser.