Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: campaign now gets loaded via LoadParam

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