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 | #include "load_param.h" |
---|
17 | |
---|
18 | #include "list.h" |
---|
19 | #include "base_object.h" |
---|
20 | |
---|
21 | #include <stdarg.h> |
---|
22 | |
---|
23 | /** |
---|
24 | \param object The object this Parameter is loaded too. |
---|
25 | \param paramName The name of the parameter loaded. |
---|
26 | \param paramCount how many parameters this loading-function takes |
---|
27 | \param ... the parameter information |
---|
28 | */ |
---|
29 | BaseLoadParam::BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, ...) |
---|
30 | { |
---|
31 | this->loadString = grabParameter(root, paramName); |
---|
32 | |
---|
33 | this->paramDesc = NULL; |
---|
34 | if (LoadClassDescription::parametersDescription) |
---|
35 | { |
---|
36 | // locating the class |
---|
37 | this->classDesc = LoadClassDescription::addClass(object->getClassName()); |
---|
38 | this->paramDesc = this->classDesc->addParam(paramName); |
---|
39 | |
---|
40 | this->paramDesc->types = new char*[paramCount]; |
---|
41 | this->paramDesc->paramCount = paramCount; |
---|
42 | |
---|
43 | va_list types; |
---|
44 | va_start (types, paramCount); |
---|
45 | for(int i = 0; i < paramCount; i++) |
---|
46 | { |
---|
47 | const char* tmpTypeName = va_arg (types, const char*); |
---|
48 | this->paramDesc->types[i] = new char[strlen(tmpTypeName)+1]; |
---|
49 | strcpy(this->paramDesc->types[i], tmpTypeName); |
---|
50 | } |
---|
51 | va_end(types); |
---|
52 | |
---|
53 | int argCount = 0; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | /** |
---|
58 | \param descriptionText The text to set as a description for this Parameter |
---|
59 | \returns a pointer to itself. |
---|
60 | */ |
---|
61 | BaseLoadParam* BaseLoadParam::describe(const char* descriptionText) |
---|
62 | { |
---|
63 | if (LoadClassDescription::parametersDescription && this->paramDesc && !this->paramDesc->getDescription()) |
---|
64 | { |
---|
65 | this->paramDesc->setDescription(descriptionText); |
---|
66 | } |
---|
67 | return this; |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | \param paramName the name of the parameter to load |
---|
72 | */ |
---|
73 | LoadParamDescription::LoadParamDescription(const char* paramName) |
---|
74 | { |
---|
75 | this->types = NULL; |
---|
76 | this->description = NULL; |
---|
77 | this->paramName = new char[strlen(paramName)+1]; |
---|
78 | strcpy(this->paramName, paramName); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | \brief removes all the alocated memory |
---|
83 | */ |
---|
84 | LoadParamDescription::~LoadParamDescription(void) |
---|
85 | { |
---|
86 | for(int i = 0; i < this->paramCount; i++) |
---|
87 | { |
---|
88 | delete this->types[i]; |
---|
89 | } |
---|
90 | delete []this->types; |
---|
91 | delete []this->paramName; |
---|
92 | delete []this->description; |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | \param descriptionText The text to set as a description for this Parameter |
---|
97 | */ |
---|
98 | void LoadParamDescription::setDescription(const char* descriptionText) |
---|
99 | { |
---|
100 | this->description = new char[strlen(descriptionText)+1]; |
---|
101 | strcpy(this->description, descriptionText); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | \brief prints out this parameter, its input method and the description (if availiable) |
---|
106 | */ |
---|
107 | void LoadParamDescription::print(void) const |
---|
108 | { |
---|
109 | PRINT(3)(" <%s>", this->paramName); |
---|
110 | for (int i = 0; i < this->paramCount; i++) |
---|
111 | { |
---|
112 | if (i > 0) |
---|
113 | PRINT(3)(","); |
---|
114 | PRINT(3)("%s", this->types[i]); |
---|
115 | } |
---|
116 | PRINT(3)("</%s>", this->paramName); |
---|
117 | if (this->description) |
---|
118 | PRINT(3)(" -- %s", this->description); |
---|
119 | PRINT(3)("\n"); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | \brief A list, that holds all the classes that are loadable (classes not objects!!) |
---|
124 | */ |
---|
125 | tList<LoadClassDescription>* LoadClassDescription::classList = new tList<LoadClassDescription>; |
---|
126 | |
---|
127 | /** |
---|
128 | \brief if the description of Parameters should be executed |
---|
129 | */ |
---|
130 | bool LoadClassDescription::parametersDescription = true; |
---|
131 | |
---|
132 | /** |
---|
133 | \param className the name of the class to be loadable |
---|
134 | */ |
---|
135 | LoadClassDescription::LoadClassDescription(const char* className) |
---|
136 | { |
---|
137 | this->className = new char[strlen(className)+1]; |
---|
138 | strcpy(this->className, className); |
---|
139 | |
---|
140 | classList->add(this); |
---|
141 | |
---|
142 | this->paramList = new tList<LoadParamDescription>; |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | \brief deletes a classDescription (deletes all the parameterDescriptions as well |
---|
147 | */ |
---|
148 | LoadClassDescription::~LoadClassDescription(void) |
---|
149 | { |
---|
150 | delete []this->className; |
---|
151 | |
---|
152 | tIterator<LoadParamDescription>* iterator = this->paramList->getIterator(); |
---|
153 | LoadParamDescription* enumParamDesc = iterator->nextElement(); |
---|
154 | while (enumParamDesc) |
---|
155 | { |
---|
156 | delete enumParamDesc; |
---|
157 | enumParamDesc = iterator->nextElement(); |
---|
158 | } |
---|
159 | delete iterator; |
---|
160 | } |
---|
161 | |
---|
162 | /** |
---|
163 | \brief adds a class to the list of loadable classes |
---|
164 | \param className The name of the class to add |
---|
165 | |
---|
166 | this function searches for the className string, and if found just returns the appropriate Class. |
---|
167 | Otherwise it returns a new classDescription |
---|
168 | */ |
---|
169 | LoadClassDescription* LoadClassDescription::addClass(const char* className) |
---|
170 | { |
---|
171 | tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator(); |
---|
172 | LoadClassDescription* enumClassDesc = iterator->nextElement(); |
---|
173 | while (enumClassDesc) |
---|
174 | { |
---|
175 | if (!strcmp(enumClassDesc->className, className)) |
---|
176 | { |
---|
177 | delete iterator; |
---|
178 | return enumClassDesc; |
---|
179 | } |
---|
180 | enumClassDesc = iterator->nextElement(); |
---|
181 | } |
---|
182 | delete iterator; |
---|
183 | |
---|
184 | return new LoadClassDescription(className); |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | \brief does the same as addClass(const char* className), but with params |
---|
189 | \param paramName the name of the parameter to add. |
---|
190 | */ |
---|
191 | LoadParamDescription* LoadClassDescription::addParam(const char* paramName) |
---|
192 | { |
---|
193 | tIterator<LoadParamDescription>* iterator = this->paramList->getIterator(); |
---|
194 | LoadParamDescription* enumParamDesc = iterator->nextElement(); |
---|
195 | while (enumParamDesc) |
---|
196 | { |
---|
197 | if (!strcmp(enumParamDesc->paramName, paramName)) |
---|
198 | { |
---|
199 | delete iterator; |
---|
200 | return enumParamDesc; |
---|
201 | } |
---|
202 | enumParamDesc = iterator->nextElement(); |
---|
203 | } |
---|
204 | delete iterator; |
---|
205 | |
---|
206 | this->paramList->add(new LoadParamDescription(paramName)); |
---|
207 | return paramList->lastElement(); |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | \brief prints out all loadable Classes, and their parameters |
---|
212 | */ |
---|
213 | void LoadClassDescription::printAll(const char* fileName) |
---|
214 | { |
---|
215 | PRINT(3)("===============================================================\n"); |
---|
216 | PRINT(3)(" Listing all the Loadable Options (loaded since Game started).\n\n"); |
---|
217 | tIterator<LoadClassDescription>* classIT = LoadClassDescription::classList->getIterator(); |
---|
218 | LoadClassDescription* enumClassDesc = classIT->nextElement(); |
---|
219 | while (enumClassDesc) |
---|
220 | { |
---|
221 | PRINT(3)("<%s>\n", enumClassDesc->className); |
---|
222 | tIterator<LoadParamDescription>* paramIT = enumClassDesc->paramList->getIterator(); |
---|
223 | LoadParamDescription* enumParamDesc = paramIT->nextElement(); |
---|
224 | while (enumParamDesc) |
---|
225 | { |
---|
226 | enumParamDesc->print(); |
---|
227 | enumParamDesc = paramIT->nextElement(); |
---|
228 | } |
---|
229 | delete paramIT; |
---|
230 | |
---|
231 | PRINT(3)("</%s>\n\n", enumClassDesc->className); |
---|
232 | enumClassDesc = classIT->nextElement(); |
---|
233 | } |
---|
234 | delete classIT; |
---|
235 | PRINT(3)("===============================================================\n"); |
---|
236 | } |
---|