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