| 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 "functor_list.h" | 
|---|
| 17 |  | 
|---|
| 18 | #include "load_param.h" | 
|---|
| 19 | #include "load_param_description.h" | 
|---|
| 20 |  | 
|---|
| 21 | #include "list.h" | 
|---|
| 22 |  | 
|---|
| 23 | #include <stdarg.h> | 
|---|
| 24 |  | 
|---|
| 25 | /** | 
|---|
| 26 |  * @param object The object this Parameter is loaded too. | 
|---|
| 27 |  * @param root: the XML-element to load this option from. | 
|---|
| 28 |  * @param paramName: The name of the parameter loaded. | 
|---|
| 29 |  * @param paramCount: how many parameters this loading-function takes | 
|---|
| 30 |  * @param multi: if false LoadParam assumes only one occurence of this parameter in root, if true it assumes multiple occurences. | 
|---|
| 31 |  * @param ...: the parameter information (1. Parameter, 2. Default Value for the Parameter, ...) | 
|---|
| 32 | */ | 
|---|
| 33 | LoadParamBase::LoadParamBase(const TiXmlElement* root, BaseObject* object, const char* paramName, | 
|---|
| 34 |                              int paramCount, bool multi, const void* pointerToParam, ...) | 
|---|
| 35 | { | 
|---|
| 36 |   this->setClassID(CL_LOAD_PARAM, "LoadParam"); | 
|---|
| 37 |  | 
|---|
| 38 |   this->loadString = NULL; | 
|---|
| 39 |   this->pointerToParam = pointerToParam; | 
|---|
| 40 |  | 
|---|
| 41 |   if (paramCount == 0 || this->pointerToParam != NULL) | 
|---|
| 42 |     this->loadString = "none"; | 
|---|
| 43 |   else | 
|---|
| 44 |     { | 
|---|
| 45 |       if (likely(!multi)) | 
|---|
| 46 |         this->loadString = grabParameter(root, paramName); | 
|---|
| 47 |       else | 
|---|
| 48 |         { | 
|---|
| 49 |           if (!strcmp(root->Value(), paramName)) | 
|---|
| 50 |             { | 
|---|
| 51 |               const TiXmlNode* val = root->FirstChild(); | 
|---|
| 52 |               if( val->ToText()) | 
|---|
| 53 |                 this->loadString = val->Value(); | 
|---|
| 54 |             } | 
|---|
| 55 |         } | 
|---|
| 56 |     } | 
|---|
| 57 |  | 
|---|
| 58 |   this->paramDesc = NULL; | 
|---|
| 59 |   if (LoadClassDescription::parametersDescription) | 
|---|
| 60 |   { | 
|---|
| 61 |     // locating the class | 
|---|
| 62 |     this->classDesc = LoadClassDescription::addClass(object->getClassName()); | 
|---|
| 63 |  | 
|---|
| 64 |     if ((this->paramDesc = this->classDesc->addParam(paramName)) != NULL) | 
|---|
| 65 |     { | 
|---|
| 66 |  | 
|---|
| 67 |       this->paramDesc->paramCount = paramCount; | 
|---|
| 68 |       this->paramDesc->types = new int[paramCount]; | 
|---|
| 69 |       this->paramDesc->defaultValues = new char*[paramCount]; | 
|---|
| 70 |  | 
|---|
| 71 |       va_list types; | 
|---|
| 72 |       va_start (types, pointerToParam); | 
|---|
| 73 |       char defaultVal[512]; | 
|---|
| 74 |       for(int i = 0; i < paramCount; i++) | 
|---|
| 75 |       { | 
|---|
| 76 |         defaultVal[0] = '\0'; | 
|---|
| 77 |           // parameters parsed | 
|---|
| 78 |         int tmpType = va_arg (types, int); | 
|---|
| 79 |         this->paramDesc->types[i] = tmpType; | 
|---|
| 80 |         switch (tmpType) | 
|---|
| 81 |         { | 
|---|
| 82 |           case ParameterInt: | 
|---|
| 83 |             sprintf(defaultVal, "%d", va_arg(types, int)); | 
|---|
| 84 |             break; | 
|---|
| 85 |           case ParameterLong: | 
|---|
| 86 |             sprintf(defaultVal, "%0.3f", va_arg(types, l_LONG_TYPE)); | 
|---|
| 87 |             break; | 
|---|
| 88 |           case ParameterFloat: | 
|---|
| 89 |             sprintf(defaultVal, "%0.3f", va_arg(types, double)); | 
|---|
| 90 |             break; | 
|---|
| 91 |           case ParameterString: | 
|---|
| 92 |             sprintf(defaultVal, "%s", va_arg(types, l_STRING_TYPE)); | 
|---|
| 93 |             break; | 
|---|
| 94 |           case ParameterXML: | 
|---|
| 95 |             sprintf(defaultVal, ""); | 
|---|
| 96 |             break; | 
|---|
| 97 |         } | 
|---|
| 98 |         this->paramDesc->defaultValues[i] = new char[strlen(defaultVal)+1]; | 
|---|
| 99 |         strcpy(this->paramDesc->defaultValues[i], defaultVal); | 
|---|
| 100 |       } | 
|---|
| 101 |       va_end(types); | 
|---|
| 102 |  | 
|---|
| 103 |       int argCount = 0; | 
|---|
| 104 |     } | 
|---|
| 105 |   } | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | /** | 
|---|
| 109 |  * @param descriptionText The text to set as a description for this Parameter | 
|---|
| 110 |  * @returns a pointer to itself. | 
|---|
| 111 | */ | 
|---|
| 112 | LoadParamBase* LoadParamBase::describe(const char* descriptionText) | 
|---|
| 113 | { | 
|---|
| 114 |   if (LoadClassDescription::parametersDescription && this->paramDesc && !this->paramDesc->getDescription()) | 
|---|
| 115 |     { | 
|---|
| 116 |       this->paramDesc->setDescription(descriptionText); | 
|---|
| 117 |     } | 
|---|
| 118 |   return this; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | // const LoadParamDescription* LoadParamDescription::getClass(const char* className) | 
|---|
| 122 | // { | 
|---|
| 123 | //   tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator(); | 
|---|
| 124 | //   LoadClassDescription* enumClassDesc = iterator->firstElement(); | 
|---|
| 125 | //   while (enumClassDesc) | 
|---|
| 126 | //   { | 
|---|
| 127 | //     if (!strcmp(enumClassDesc->className, classNameBegin, className)) | 
|---|
| 128 | //     { | 
|---|
| 129 | //       delete iterator; | 
|---|
| 130 | //       return enumClassDesc; | 
|---|
| 131 | //     } | 
|---|
| 132 | //     enumClassDesc = iterator->nextElement(); | 
|---|
| 133 | //   } | 
|---|
| 134 | //   delete iterator; | 
|---|
| 135 | // | 
|---|
| 136 | //   return NULL; | 
|---|
| 137 | // } | 
|---|
| 138 |  | 
|---|
| 139 | /** | 
|---|
| 140 |  * @param root: The XML-element to grab a parameter from | 
|---|
| 141 |  * @param parameterName: the parameter to grab | 
|---|
| 142 |  * @returns the Value of the parameter if found, NULL otherwise | 
|---|
| 143 | */ | 
|---|
| 144 | const char* grabParameter(const TiXmlElement* root, const char* parameterName) | 
|---|
| 145 | { | 
|---|
| 146 |   const TiXmlElement* element; | 
|---|
| 147 |   const TiXmlNode* node; | 
|---|
| 148 |  | 
|---|
| 149 |   if (root == NULL) | 
|---|
| 150 |     return NULL; | 
|---|
| 151 |   assert( parameterName != NULL); | 
|---|
| 152 |  | 
|---|
| 153 |   element = root->FirstChildElement( parameterName); | 
|---|
| 154 |   if( element == NULL) return NULL; | 
|---|
| 155 |  | 
|---|
| 156 |   node = element->FirstChild(); | 
|---|
| 157 |   while( node != NULL) | 
|---|
| 158 |     { | 
|---|
| 159 |       if( node->ToText()) return node->Value(); | 
|---|
| 160 |       node = node->NextSibling(); | 
|---|
| 161 |     } | 
|---|
| 162 |   return NULL; | 
|---|
| 163 | } | 
|---|