Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/util/loading/load_param_description.cc @ 5549

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

orxonox/trunk: cleanup of 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_description.h"
17#include "list.h"
18#include <stdarg.h>
19#include "stdlibincl.h"
20#include "functor_list.h"
21
22/**
23 * @param paramName the name of the parameter to load
24 */
25LoadParamDescription::LoadParamDescription(const char* paramName)
26{
27  this->types = NULL;
28  this->description = NULL;
29  this->defaultValues = NULL;
30  this->paramName = new char[strlen(paramName)+1];
31  strcpy(this->paramName, paramName);
32}
33
34/**
35 *  removes all the alocated memory
36 */
37LoadParamDescription::~LoadParamDescription()
38{
39  if (this->defaultValues != NULL)
40  {
41    for(int i = 0; i < this->paramCount; i++)
42    {
43      delete[] this->defaultValues[i];
44    }
45  }
46
47  delete[] this->types;
48  delete[] this->defaultValues;
49  delete[] this->paramName;
50  delete[] this->description;
51}
52
53/**
54 * @param descriptionText The text to set as a description for this Parameter
55 */
56void LoadParamDescription::setDescription(const char* descriptionText)
57{
58  this->description = new char[strlen(descriptionText)+1];
59  strcpy(this->description, descriptionText);
60}
61
62/**
63 *  prints out this parameter, its input method and the description (if availiable)
64 */
65void LoadParamDescription::print() const
66{
67  PRINT(3)(" <%s>", this->paramName);
68  for (int i = 0; i < this->paramCount; i++)
69  {
70    if (i > 0)
71      PRINT(3)(",");
72    switch (this->types[i])
73    {
74      default:
75        PRINTF(3)("none");
76        break;
77      case ParameterBool:
78        PRINT(3)("bool");
79        break;
80      case ParameterChar:
81        PRINT(3)("char");
82        break;
83      case ParameterString:
84        PRINT(3)("string");
85        break;
86      case ParameterInt:
87        PRINT(3)("int");
88        break;
89      case ParameterUInt:
90        PRINT(3)("Uint");
91        break;
92      case ParameterFloat:
93        PRINT(3)("float");
94        break;
95      case ParameterLong:
96        PRINT(3)("long");
97        break;
98      case ParameterXML:
99        PRINT(3)("XML");
100        break;
101    }
102  }
103  PRINT(3)("</%s>", this->paramName);
104  if (this->description)
105    PRINT(3)(" -- %s", this->description);
106  // default values
107  if (this->paramCount > 0)
108  {
109    PRINT(3)(" (Default: ");
110    for (int i = 0; i < this->paramCount; i++)
111    {
112      if (i > 0)
113        PRINT(3)(", ");
114      if (this->types[i] & ParameterString)
115      { // leave brackets !!
116        PRINT(3)("\"%s\"", this->defaultValues[i]);
117      }
118      else
119      {
120        PRINT(3)("%s", this->defaultValues[i]);
121      }
122    }
123    PRINT(3)(")");
124  }
125  PRINT(3)("\n");
126}
127
128/**
129 *  A list, that holds all the classes that are loadable (classes not objects!!)
130 */
131tList<LoadClassDescription>* LoadClassDescription::classList = NULL;
132
133/**
134 *  if the description of Parameters should be executed
135 */
136bool LoadClassDescription::parametersDescription = false;
137
138/**
139 * @param className the name of the class to be loadable
140 */
141LoadClassDescription::LoadClassDescription(const char* className)
142{
143  this->className = new char[strlen(className)+1];
144  strcpy(this->className, className);
145
146  if (LoadClassDescription::classList == NULL)
147    LoadClassDescription::classList = new tList<LoadClassDescription>;
148
149  LoadClassDescription::classList->add(this);
150
151  this->paramList = new tList<LoadParamDescription>;
152}
153
154/**
155 *  deletes a classDescription (deletes all the parameterDescriptions as well
156 */
157LoadClassDescription::~LoadClassDescription()
158{
159  tIterator<LoadParamDescription>* iterator = this->paramList->getIterator();
160  LoadParamDescription* enumParamDesc = iterator->firstElement();
161  while (enumParamDesc)
162  {
163    delete enumParamDesc;
164    enumParamDesc = iterator->nextElement();
165  }
166  delete iterator;
167  delete this->paramList;
168
169  delete[] this->className;
170}
171
172void LoadClassDescription::deleteAllDescriptions()
173{
174  if (LoadClassDescription::classList != NULL)
175  {
176    tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
177    LoadClassDescription* delElem = iterator->firstElement();
178    while (delElem != NULL)
179    {
180      delete delElem;
181      delElem = iterator->nextElement();
182    }
183    delete iterator;
184    delete LoadClassDescription::classList;
185  }
186  LoadClassDescription::classList = NULL;
187}
188
189
190/**
191 *  adds a class to the list of loadable classes
192 * @param className The name of the class to add
193
194   this function searches for the className string, and if found just returns the appropriate Class.
195   Otherwise it returns a new classDescription
196 */
197LoadClassDescription* LoadClassDescription::addClass(const char* className)
198{
199  if (LoadClassDescription::classList != NULL)
200  {
201    tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
202    LoadClassDescription* enumClassDesc = iterator->firstElement();
203    while (enumClassDesc)
204    {
205      if (!strcmp(enumClassDesc->className, className))
206      {
207        delete iterator;
208        return enumClassDesc;
209      }
210      enumClassDesc = iterator->nextElement();
211    }
212    delete iterator;
213  }
214  return new LoadClassDescription(className);
215}
216
217/**
218 *  does the same as addClass(const char* className), but with params
219 * @param paramName the name of the parameter to add.
220 */
221LoadParamDescription* LoadClassDescription::addParam(const char* paramName)
222{
223  tIterator<LoadParamDescription>* iterator = this->paramList->getIterator();
224  LoadParamDescription* enumParamDesc = iterator->firstElement();
225  while (enumParamDesc)
226  {
227    if (!strcmp(enumParamDesc->paramName, paramName))
228    {
229      delete iterator;
230          //return enumParamDesc;
231      return NULL;
232    }
233    enumParamDesc = iterator->nextElement();
234  }
235  delete iterator;
236
237  LoadParamDescription* newParam = new LoadParamDescription(paramName);
238
239  this->paramList->add(newParam);
240  return newParam;
241}
242
243/**
244 *  prints out all loadable Classes, and their parameters
245 * @param fileName prints the output to a File
246 * @todo implement it
247 */
248void LoadClassDescription::printAll(const char* fileName)
249{
250  PRINT(3)("===============================================================\n");
251  PRINT(3)(" Listing all the Loadable Options (loaded since Game started).\n\n");
252  if (LoadClassDescription::classList != NULL)
253  {
254    tIterator<LoadClassDescription>* classIT = LoadClassDescription::classList->getIterator();
255    LoadClassDescription* enumClassDesc = classIT->firstElement();
256    while (enumClassDesc)
257    {
258      PRINT(3)("<%s>\n", enumClassDesc->className);
259      tIterator<LoadParamDescription>* paramIT = enumClassDesc->paramList->getIterator();
260      LoadParamDescription* enumParamDesc = paramIT->firstElement();
261      while (enumParamDesc)
262      {
263        enumParamDesc->print();
264        enumParamDesc = paramIT->nextElement();
265      }
266      delete paramIT;
267
268      PRINT(3)("</%s>\n\n", enumClassDesc->className);
269      enumClassDesc = classIT->nextElement();
270    }
271    delete classIT;
272  }
273  else
274    PRINT(3)("no Classes defined so far\n");
275  PRINT(3)("===============================================================\n");
276}
277
278/**
279 * searches for classes, which beginn with classNameBegin
280 * @param classNameBegin the beginning string of a Class
281 * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards,
282 * !! The strings MUST NOT be deleted !!
283 */
284tList<const char>* LoadClassDescription::searchClassWithShort(const char* classNameBegin)
285{
286  unsigned int searchLength = strlen(classNameBegin);
287  tList<const char>* retVal = new tList<const char>;
288
289  tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
290  LoadClassDescription* enumClassDesc = iterator->firstElement();
291  while (enumClassDesc)
292  {
293    if (strlen(enumClassDesc->className)>searchLength+1 &&
294        !strncasecmp(enumClassDesc->className, classNameBegin, searchLength))
295    {
296      retVal->add(enumClassDesc->className);
297    }
298    enumClassDesc = iterator->nextElement();
299  }
300  delete iterator;
301
302  return retVal;
303}
Note: See TracBrowser for help on using the repository browser.