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 | this->setClassID(CL_SHELL_COMMAND, "ShellCommand"); |
---|
33 | this->setName(commandName); |
---|
34 | |
---|
35 | va_list parameters; |
---|
36 | va_start(parameters, paramCount); |
---|
37 | |
---|
38 | this->classID = classID; |
---|
39 | this->className = ClassList::IDToString(classID); |
---|
40 | |
---|
41 | if (classID & CL_MASK_SINGLETON == CL_MASK_SINGLETON) |
---|
42 | this->isSingleton = true; |
---|
43 | else |
---|
44 | this->isSingleton = false; |
---|
45 | |
---|
46 | // handling parameters, and storing them: |
---|
47 | this->paramCount = paramCount; |
---|
48 | this->parameters = new ParameterType[paramCount]; |
---|
49 | |
---|
50 | for (unsigned int i = 0; i < paramCount; i++) |
---|
51 | parameters[i] = va_arg(parameters, long); |
---|
52 | |
---|
53 | // adding this ShellCommand to the list of known Commands |
---|
54 | ShellCommandBase::commandList->add(this); |
---|
55 | } |
---|
56 | |
---|
57 | ShellCommandBase::~ShellCommandBase() |
---|
58 | { |
---|
59 | delete[] this->parameters; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | tList<ShellCommandBase>* ShellCommandBase::commandList = NULL; |
---|
64 | |
---|
65 | bool ShellCommandBase::isRegistered(const char* commandName, ClassID classID, unsigned int paramCount, ...) |
---|
66 | { |
---|
67 | |
---|
68 | va_list parameters; |
---|
69 | va_start(parameters, paramCount); |
---|
70 | |
---|
71 | if (ShellCommandBase::commandList == NULL) |
---|
72 | { |
---|
73 | ShellCommandBase::commandList = new tList<ShellCommandBase>; |
---|
74 | return false; |
---|
75 | } |
---|
76 | |
---|
77 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
78 | ShellCommandBase* elem = iterator->firstElement(); |
---|
79 | while(elem != NULL) |
---|
80 | { |
---|
81 | if (classID == elem->classID && !strcmp(commandName, elem->getName())) |
---|
82 | { |
---|
83 | PRINTF(2)("Command already registered\n"); |
---|
84 | delete iterator; |
---|
85 | return true; |
---|
86 | } |
---|
87 | elem = iterator->nextElement(); |
---|
88 | } |
---|
89 | delete iterator; |
---|
90 | return false; |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | bool ShellCommandBase::execute(const char* executionString) |
---|
95 | { |
---|
96 | if (ShellCommandBase::commandList == NULL) |
---|
97 | return false; |
---|
98 | |
---|
99 | const char* commandEnd = strchr(executionString, ' '); |
---|
100 | if (commandEnd == NULL) |
---|
101 | commandEnd = executionString + strlen(executionString); |
---|
102 | |
---|
103 | tIterator<ShellCommandBase>* iterator = ShellCommandBase::commandList->getIterator(); |
---|
104 | ShellCommandBase* elem = iterator->firstElement(); |
---|
105 | while(elem != NULL) |
---|
106 | { |
---|
107 | printf("%s\n", elem->getName()); |
---|
108 | if (!strncmp(executionString, elem->getName(), strlen(elem->getName()))) |
---|
109 | { |
---|
110 | elem->executeCommand(commandEnd); |
---|
111 | delete iterator; |
---|
112 | return true; |
---|
113 | } |
---|
114 | elem = iterator->nextElement(); |
---|
115 | } |
---|
116 | delete iterator; |
---|
117 | return true; |
---|
118 | } |
---|