Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now all new commands get subscribed

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