[4597] | 1 | /* |
---|
[4250] | 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: |
---|
[4285] | 12 | main-programmer: Benjamin Grauer |
---|
[4250] | 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
[5546] | 16 | #include "load_param_description.h" |
---|
[5556] | 17 | |
---|
[4254] | 18 | #include "list.h" |
---|
| 19 | #include <stdarg.h> |
---|
[5549] | 20 | #include "stdlibincl.h" |
---|
| 21 | #include "functor_list.h" |
---|
[4254] | 22 | |
---|
[4860] | 23 | /** |
---|
[4836] | 24 | * @param paramName the name of the parameter to load |
---|
[5546] | 25 | */ |
---|
[4254] | 26 | LoadParamDescription::LoadParamDescription(const char* paramName) |
---|
| 27 | { |
---|
| 28 | this->types = NULL; |
---|
[4255] | 29 | this->description = NULL; |
---|
[5211] | 30 | this->defaultValues = NULL; |
---|
[4254] | 31 | this->paramName = new char[strlen(paramName)+1]; |
---|
| 32 | strcpy(this->paramName, paramName); |
---|
| 33 | } |
---|
| 34 | |
---|
[4256] | 35 | /** |
---|
[4836] | 36 | * removes all the alocated memory |
---|
[5546] | 37 | */ |
---|
[4746] | 38 | LoadParamDescription::~LoadParamDescription() |
---|
[4254] | 39 | { |
---|
[5099] | 40 | if (this->defaultValues != NULL) |
---|
[4623] | 41 | { |
---|
[5099] | 42 | for(int i = 0; i < this->paramCount; i++) |
---|
| 43 | { |
---|
[5218] | 44 | delete[] this->defaultValues[i]; |
---|
[5099] | 45 | } |
---|
[4623] | 46 | } |
---|
| 47 | |
---|
[5211] | 48 | delete[] this->types; |
---|
| 49 | delete[] this->defaultValues; |
---|
| 50 | delete[] this->paramName; |
---|
| 51 | delete[] this->description; |
---|
[4254] | 52 | } |
---|
| 53 | |
---|
[4256] | 54 | /** |
---|
[4836] | 55 | * @param descriptionText The text to set as a description for this Parameter |
---|
[5546] | 56 | */ |
---|
[4255] | 57 | void LoadParamDescription::setDescription(const char* descriptionText) |
---|
| 58 | { |
---|
[4256] | 59 | this->description = new char[strlen(descriptionText)+1]; |
---|
| 60 | strcpy(this->description, descriptionText); |
---|
[4255] | 61 | } |
---|
[4254] | 62 | |
---|
[4256] | 63 | /** |
---|
[4836] | 64 | * prints out this parameter, its input method and the description (if availiable) |
---|
[5546] | 65 | */ |
---|
[4746] | 66 | void LoadParamDescription::print() const |
---|
[4255] | 67 | { |
---|
| 68 | PRINT(3)(" <%s>", this->paramName); |
---|
| 69 | for (int i = 0; i < this->paramCount; i++) |
---|
[5546] | 70 | { |
---|
| 71 | if (i > 0) |
---|
| 72 | PRINT(3)(","); |
---|
[5634] | 73 | // FIXME |
---|
| 74 | // switch (this->types[i]) |
---|
| 75 | // { |
---|
| 76 | // default: |
---|
| 77 | // PRINTF(3)("none"); |
---|
| 78 | // break; |
---|
| 79 | // case ParameterBool: |
---|
| 80 | // PRINT(3)("bool"); |
---|
| 81 | // break; |
---|
| 82 | // case ParameterChar: |
---|
| 83 | // PRINT(3)("char"); |
---|
| 84 | // break; |
---|
| 85 | // case ParameterString: |
---|
| 86 | // PRINT(3)("string"); |
---|
| 87 | // break; |
---|
| 88 | // case ParameterInt: |
---|
| 89 | // PRINT(3)("int"); |
---|
| 90 | // break; |
---|
| 91 | // case ParameterUInt: |
---|
| 92 | // PRINT(3)("Uint"); |
---|
| 93 | // break; |
---|
| 94 | // case ParameterFloat: |
---|
| 95 | // PRINT(3)("float"); |
---|
| 96 | // break; |
---|
| 97 | // case ParameterLong: |
---|
| 98 | // PRINT(3)("long"); |
---|
| 99 | // break; |
---|
| 100 | // case ParameterXML: |
---|
| 101 | // PRINT(3)("XML"); |
---|
| 102 | // break; |
---|
| 103 | // } |
---|
[5546] | 104 | } |
---|
[4255] | 105 | PRINT(3)("</%s>", this->paramName); |
---|
| 106 | if (this->description) |
---|
| 107 | PRINT(3)(" -- %s", this->description); |
---|
[4623] | 108 | // default values |
---|
| 109 | if (this->paramCount > 0) |
---|
| 110 | { |
---|
[4637] | 111 | PRINT(3)(" (Default: "); |
---|
[4623] | 112 | for (int i = 0; i < this->paramCount; i++) |
---|
| 113 | { |
---|
| 114 | if (i > 0) |
---|
| 115 | PRINT(3)(", "); |
---|
[5634] | 116 | if (this->types[i] & MT_STRING) |
---|
[4625] | 117 | { // leave brackets !! |
---|
| 118 | PRINT(3)("\"%s\"", this->defaultValues[i]); |
---|
| 119 | } |
---|
| 120 | else |
---|
| 121 | { |
---|
| 122 | PRINT(3)("%s", this->defaultValues[i]); |
---|
| 123 | } |
---|
[4623] | 124 | } |
---|
| 125 | PRINT(3)(")"); |
---|
| 126 | } |
---|
[4255] | 127 | PRINT(3)("\n"); |
---|
| 128 | } |
---|
| 129 | |
---|
[4256] | 130 | /** |
---|
[4836] | 131 | * A list, that holds all the classes that are loadable (classes not objects!!) |
---|
[5546] | 132 | */ |
---|
[5226] | 133 | tList<LoadClassDescription>* LoadClassDescription::classList = NULL; |
---|
[4254] | 134 | |
---|
[4251] | 135 | /** |
---|
[4836] | 136 | * if the description of Parameters should be executed |
---|
[5546] | 137 | */ |
---|
[5534] | 138 | bool LoadClassDescription::parametersDescription = false; |
---|
[4254] | 139 | |
---|
[4256] | 140 | /** |
---|
[4836] | 141 | * @param className the name of the class to be loadable |
---|
[5546] | 142 | */ |
---|
[4254] | 143 | LoadClassDescription::LoadClassDescription(const char* className) |
---|
| 144 | { |
---|
| 145 | this->className = new char[strlen(className)+1]; |
---|
| 146 | strcpy(this->className, className); |
---|
| 147 | |
---|
[5226] | 148 | if (LoadClassDescription::classList == NULL) |
---|
| 149 | LoadClassDescription::classList = new tList<LoadClassDescription>; |
---|
[4254] | 150 | |
---|
[5226] | 151 | LoadClassDescription::classList->add(this); |
---|
| 152 | |
---|
[4254] | 153 | this->paramList = new tList<LoadParamDescription>; |
---|
| 154 | } |
---|
| 155 | |
---|
[4256] | 156 | /** |
---|
[4836] | 157 | * deletes a classDescription (deletes all the parameterDescriptions as well |
---|
[5546] | 158 | */ |
---|
[4746] | 159 | LoadClassDescription::~LoadClassDescription() |
---|
[4254] | 160 | { |
---|
| 161 | tIterator<LoadParamDescription>* iterator = this->paramList->getIterator(); |
---|
[5115] | 162 | LoadParamDescription* enumParamDesc = iterator->firstElement(); |
---|
[4254] | 163 | while (enumParamDesc) |
---|
[5546] | 164 | { |
---|
| 165 | delete enumParamDesc; |
---|
| 166 | enumParamDesc = iterator->nextElement(); |
---|
| 167 | } |
---|
[4254] | 168 | delete iterator; |
---|
[5227] | 169 | delete this->paramList; |
---|
[5226] | 170 | |
---|
| 171 | delete[] this->className; |
---|
[4254] | 172 | } |
---|
| 173 | |
---|
[5226] | 174 | void LoadClassDescription::deleteAllDescriptions() |
---|
| 175 | { |
---|
| 176 | if (LoadClassDescription::classList != NULL) |
---|
| 177 | { |
---|
| 178 | tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator(); |
---|
| 179 | LoadClassDescription* delElem = iterator->firstElement(); |
---|
| 180 | while (delElem != NULL) |
---|
| 181 | { |
---|
| 182 | delete delElem; |
---|
| 183 | delElem = iterator->nextElement(); |
---|
| 184 | } |
---|
| 185 | delete iterator; |
---|
| 186 | delete LoadClassDescription::classList; |
---|
| 187 | } |
---|
| 188 | LoadClassDescription::classList = NULL; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | |
---|
[4256] | 192 | /** |
---|
[4836] | 193 | * adds a class to the list of loadable classes |
---|
| 194 | * @param className The name of the class to add |
---|
[4254] | 195 | |
---|
[4256] | 196 | this function searches for the className string, and if found just returns the appropriate Class. |
---|
| 197 | Otherwise it returns a new classDescription |
---|
[5546] | 198 | */ |
---|
[4254] | 199 | LoadClassDescription* LoadClassDescription::addClass(const char* className) |
---|
| 200 | { |
---|
[5226] | 201 | if (LoadClassDescription::classList != NULL) |
---|
| 202 | { |
---|
| 203 | tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator(); |
---|
[5546] | 204 | LoadClassDescription* enumClassDesc = iterator->firstElement(); |
---|
| 205 | while (enumClassDesc) |
---|
| 206 | { |
---|
| 207 | if (!strcmp(enumClassDesc->className, className)) |
---|
| 208 | { |
---|
| 209 | delete iterator; |
---|
| 210 | return enumClassDesc; |
---|
| 211 | } |
---|
| 212 | enumClassDesc = iterator->nextElement(); |
---|
| 213 | } |
---|
| 214 | delete iterator; |
---|
[5226] | 215 | } |
---|
[4254] | 216 | return new LoadClassDescription(className); |
---|
| 217 | } |
---|
| 218 | |
---|
[4256] | 219 | /** |
---|
[4836] | 220 | * does the same as addClass(const char* className), but with params |
---|
| 221 | * @param paramName the name of the parameter to add. |
---|
[5546] | 222 | */ |
---|
[4254] | 223 | LoadParamDescription* LoadClassDescription::addParam(const char* paramName) |
---|
| 224 | { |
---|
| 225 | tIterator<LoadParamDescription>* iterator = this->paramList->getIterator(); |
---|
[5115] | 226 | LoadParamDescription* enumParamDesc = iterator->firstElement(); |
---|
[4254] | 227 | while (enumParamDesc) |
---|
[5546] | 228 | { |
---|
| 229 | if (!strcmp(enumParamDesc->paramName, paramName)) |
---|
[4254] | 230 | { |
---|
[5546] | 231 | delete iterator; |
---|
[5227] | 232 | //return enumParamDesc; |
---|
[5546] | 233 | return NULL; |
---|
[4254] | 234 | } |
---|
[5546] | 235 | enumParamDesc = iterator->nextElement(); |
---|
| 236 | } |
---|
[4254] | 237 | delete iterator; |
---|
| 238 | |
---|
[5227] | 239 | LoadParamDescription* newParam = new LoadParamDescription(paramName); |
---|
| 240 | |
---|
| 241 | this->paramList->add(newParam); |
---|
| 242 | return newParam; |
---|
[4254] | 243 | } |
---|
[4255] | 244 | |
---|
[4256] | 245 | /** |
---|
[4836] | 246 | * prints out all loadable Classes, and their parameters |
---|
[5100] | 247 | * @param fileName prints the output to a File |
---|
| 248 | * @todo implement it |
---|
[5546] | 249 | */ |
---|
[4260] | 250 | void LoadClassDescription::printAll(const char* fileName) |
---|
[4255] | 251 | { |
---|
[4259] | 252 | PRINT(3)("===============================================================\n"); |
---|
| 253 | PRINT(3)(" Listing all the Loadable Options (loaded since Game started).\n\n"); |
---|
[5226] | 254 | if (LoadClassDescription::classList != NULL) |
---|
| 255 | { |
---|
| 256 | tIterator<LoadClassDescription>* classIT = LoadClassDescription::classList->getIterator(); |
---|
| 257 | LoadClassDescription* enumClassDesc = classIT->firstElement(); |
---|
| 258 | while (enumClassDesc) |
---|
[5546] | 259 | { |
---|
| 260 | PRINT(3)("<%s>\n", enumClassDesc->className); |
---|
| 261 | tIterator<LoadParamDescription>* paramIT = enumClassDesc->paramList->getIterator(); |
---|
| 262 | LoadParamDescription* enumParamDesc = paramIT->firstElement(); |
---|
| 263 | while (enumParamDesc) |
---|
| 264 | { |
---|
| 265 | enumParamDesc->print(); |
---|
| 266 | enumParamDesc = paramIT->nextElement(); |
---|
| 267 | } |
---|
| 268 | delete paramIT; |
---|
[4255] | 269 | |
---|
[5546] | 270 | PRINT(3)("</%s>\n\n", enumClassDesc->className); |
---|
| 271 | enumClassDesc = classIT->nextElement(); |
---|
| 272 | } |
---|
[5226] | 273 | delete classIT; |
---|
| 274 | } |
---|
| 275 | else |
---|
| 276 | PRINT(3)("no Classes defined so far\n"); |
---|
[4259] | 277 | PRINT(3)("===============================================================\n"); |
---|
[4255] | 278 | } |
---|
[4492] | 279 | |
---|
[5100] | 280 | /** |
---|
| 281 | * searches for classes, which beginn with classNameBegin |
---|
| 282 | * @param classNameBegin the beginning string of a Class |
---|
[5113] | 283 | * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards, |
---|
[5100] | 284 | * !! The strings MUST NOT be deleted !! |
---|
| 285 | */ |
---|
[5113] | 286 | tList<const char>* LoadClassDescription::searchClassWithShort(const char* classNameBegin) |
---|
[5100] | 287 | { |
---|
| 288 | unsigned int searchLength = strlen(classNameBegin); |
---|
[5113] | 289 | tList<const char>* retVal = new tList<const char>; |
---|
[4492] | 290 | |
---|
[5100] | 291 | tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator(); |
---|
[5115] | 292 | LoadClassDescription* enumClassDesc = iterator->firstElement(); |
---|
[5100] | 293 | while (enumClassDesc) |
---|
| 294 | { |
---|
| 295 | if (strlen(enumClassDesc->className)>searchLength+1 && |
---|
| 296 | !strncasecmp(enumClassDesc->className, classNameBegin, searchLength)) |
---|
| 297 | { |
---|
[5113] | 298 | retVal->add(enumClassDesc->className); |
---|
[5100] | 299 | } |
---|
| 300 | enumClassDesc = iterator->nextElement(); |
---|
| 301 | } |
---|
| 302 | delete iterator; |
---|
[4492] | 303 | |
---|
[5100] | 304 | return retVal; |
---|
| 305 | } |
---|