Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/loading/load_param.cc @ 5577

Last change on this file since 5577 was 5549, checked in by bensch, 19 years ago

orxonox/trunk: cleanup of LoadParam

File size: 4.7 KB
RevLine 
[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"
[4250]22
[4254]23#include <stdarg.h>
24
[4256]25/**
[4836]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, ...)
[4256]32*/
[5545]33LoadParamBase::LoadParamBase(const TiXmlElement* root, BaseObject* object, const char* paramName,
[4598]34                             int paramCount, bool multi, const void* pointerToParam, ...)
[4251]35{
[4597]36  this->setClassID(CL_LOAD_PARAM, "LoadParam");
[4637]37
[4496]38  this->loadString = NULL;
[4598]39  this->pointerToParam = pointerToParam;
[4299]40
[5549]41  if (paramCount == 0 || this->pointerToParam != NULL)
[4501]42    this->loadString = "none";
[4496]43  else
44    {
[4501]45      if (likely(!multi))
[4597]46        this->loadString = grabParameter(root, paramName);
[4501]47      else
[4597]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        }
[4496]56    }
57
[4255]58  this->paramDesc = NULL;
[4254]59  if (LoadClassDescription::parametersDescription)
[4623]60  {
[4625]61    // locating the class
[4623]62    this->classDesc = LoadClassDescription::addClass(object->getClassName());
[4254]63
[4623]64    if ((this->paramDesc = this->classDesc->addParam(paramName)) != NULL)
65    {
66
67      this->paramDesc->paramCount = paramCount;
[5332]68      this->paramDesc->types = new int[paramCount];
[4623]69      this->paramDesc->defaultValues = new char*[paramCount];
[4254]70
71      va_list types;
[4598]72      va_start (types, pointerToParam);
[4623]73      char defaultVal[512];
[4254]74      for(int i = 0; i < paramCount; i++)
[4623]75      {
[5334]76        defaultVal[0] = '\0';
[4623]77          // parameters parsed
[5332]78        int tmpType = va_arg (types, int);
79        this->paramDesc->types[i] = tmpType;
80        switch (tmpType)
[4597]81        {
[5332]82          case ParameterInt:
[5334]83            sprintf(defaultVal, "%d", va_arg(types, int));
[5332]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;
[4597]97        }
[4623]98        this->paramDesc->defaultValues[i] = new char[strlen(defaultVal)+1];
99        strcpy(this->paramDesc->defaultValues[i], defaultVal);
100      }
[4299]101      va_end(types);
[4254]102
103      int argCount = 0;
104    }
[4623]105  }
[4251]106}
[4250]107
[4860]108/**
[4836]109 * @param descriptionText The text to set as a description for this Parameter
110 * @returns a pointer to itself.
[4256]111*/
[5545]112LoadParamBase* LoadParamBase::describe(const char* descriptionText)
[4254]113{
[4255]114  if (LoadClassDescription::parametersDescription && this->paramDesc && !this->paramDesc->getDescription())
[4254]115    {
[4255]116      this->paramDesc->setDescription(descriptionText);
[4254]117    }
[4260]118  return this;
[4254]119}
120
[5100]121// const LoadParamDescription* LoadParamDescription::getClass(const char* className)
122// {
123//   tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
[5115]124//   LoadClassDescription* enumClassDesc = iterator->firstElement();
[5100]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
[4492]139/**
[4836]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
[4492]143*/
144const char* grabParameter(const TiXmlElement* root, const char* parameterName)
145{
146  const TiXmlElement* element;
147  const TiXmlNode* node;
[4597]148
[4492]149  if (root == NULL)
150    return NULL;
151  assert( parameterName != NULL);
[4597]152
[4492]153  element = root->FirstChildElement( parameterName);
154  if( element == NULL) return NULL;
[4597]155
[4492]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}
Note: See TracBrowser for help on using the repository browser.