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 | |
---|
28 | using namespace std; |
---|
29 | |
---|
30 | ShellCommandBase::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 | this->commandName = new char[strlen(commandName)+1]; |
---|
37 | strcpy(this->commandName, commandName); |
---|
38 | |
---|
39 | // handling parameters, and storing them: |
---|
40 | this->paramCount = paramCount; |
---|
41 | this->parameters = new ParameterType[paramCount]; |
---|
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 |
---|
47 | ShellCommandBase::commandList->add(this); |
---|
48 | } |
---|
49 | |
---|
50 | ShellCommandBase::~ShellCommandBase() |
---|
51 | { |
---|
52 | delete[] this->commandName; |
---|
53 | delete[] this->parameters; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | tList<ShellCommandBase>* ShellCommandBase::commandList = NULL; |
---|
58 | |
---|
59 | bool ShellCommandBase::isRegistered(const char* commandName, ClassID classID, unsigned int paramCount, ...) |
---|
60 | { |
---|
61 | |
---|
62 | va_list parameters; |
---|
63 | va_start(parameters, paramCount); |
---|
64 | |
---|
65 | if (ShellCommandBase::commandList == NULL) |
---|
66 | { |
---|
67 | ShellCommandBase::commandList = new tList<ShellCommandBase>; |
---|
68 | return false; |
---|
69 | } |
---|
70 | |
---|
71 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
72 | ShellCommandBase* elem = iterator->firstElement(); |
---|
73 | while(elem != NULL) |
---|
74 | { |
---|
75 | if (classID == elem->classID && !strcmp(commandName, elem->commandName)) |
---|
76 | { |
---|
77 | // PRINTF(2)("Command already registered\n"); |
---|
78 | delete iterator; |
---|
79 | return true; |
---|
80 | } |
---|
81 | elem = iterator->nextElement(); |
---|
82 | } |
---|
83 | delete iterator; |
---|
84 | return true; |
---|
85 | } |
---|
86 | |
---|
87 | bool 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 | { |
---|
100 | if (!strncmp(executionString, elem->commandName, strlen(elem->commandName))) |
---|
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 | |
---|