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