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