Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: cycling read-in of parameters should work now… this is quite tricky, and the TrackManager has to be rewritten in some parts…. :(

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