Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: cleanup of LoadParam

File size: 8.1 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
[5546]16#include "load_param_description.h"
[4254]17#include "list.h"
18#include <stdarg.h>
[5549]19#include "stdlibincl.h"
20#include "functor_list.h"
[4254]21
[4860]22/**
[4836]23 * @param paramName the name of the parameter to load
[5546]24 */
[4254]25LoadParamDescription::LoadParamDescription(const char* paramName)
26{
27  this->types = NULL;
[4255]28  this->description = NULL;
[5211]29  this->defaultValues = NULL;
[4254]30  this->paramName = new char[strlen(paramName)+1];
31  strcpy(this->paramName, paramName);
32}
33
[4256]34/**
[4836]35 *  removes all the alocated memory
[5546]36 */
[4746]37LoadParamDescription::~LoadParamDescription()
[4254]38{
[5099]39  if (this->defaultValues != NULL)
[4623]40  {
[5099]41    for(int i = 0; i < this->paramCount; i++)
42    {
[5218]43      delete[] this->defaultValues[i];
[5099]44    }
[4623]45  }
46
[5211]47  delete[] this->types;
48  delete[] this->defaultValues;
49  delete[] this->paramName;
50  delete[] this->description;
[4254]51}
52
[4256]53/**
[4836]54 * @param descriptionText The text to set as a description for this Parameter
[5546]55 */
[4255]56void LoadParamDescription::setDescription(const char* descriptionText)
57{
[4256]58  this->description = new char[strlen(descriptionText)+1];
59  strcpy(this->description, descriptionText);
[4255]60}
[4254]61
[4256]62/**
[4836]63 *  prints out this parameter, its input method and the description (if availiable)
[5546]64 */
[4746]65void LoadParamDescription::print() const
[4255]66{
67  PRINT(3)(" <%s>", this->paramName);
68  for (int i = 0; i < this->paramCount; i++)
[5546]69  {
70    if (i > 0)
71      PRINT(3)(",");
72    switch (this->types[i])
[4255]73    {
[5546]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;
[4255]101    }
[5546]102  }
[4255]103  PRINT(3)("</%s>", this->paramName);
104  if (this->description)
105    PRINT(3)(" -- %s", this->description);
[4623]106  // default values
107  if (this->paramCount > 0)
108  {
[4637]109    PRINT(3)(" (Default: ");
[4623]110    for (int i = 0; i < this->paramCount; i++)
111    {
112      if (i > 0)
113        PRINT(3)(", ");
[5332]114      if (this->types[i] & ParameterString)
[4625]115      { // leave brackets !!
116        PRINT(3)("\"%s\"", this->defaultValues[i]);
117      }
118      else
119      {
120        PRINT(3)("%s", this->defaultValues[i]);
121      }
[4623]122    }
123    PRINT(3)(")");
124  }
[4255]125  PRINT(3)("\n");
126}
127
[4256]128/**
[4836]129 *  A list, that holds all the classes that are loadable (classes not objects!!)
[5546]130 */
[5226]131tList<LoadClassDescription>* LoadClassDescription::classList = NULL;
[4254]132
[4251]133/**
[4836]134 *  if the description of Parameters should be executed
[5546]135 */
[5534]136bool LoadClassDescription::parametersDescription = false;
[4254]137
[4256]138/**
[4836]139 * @param className the name of the class to be loadable
[5546]140 */
[4254]141LoadClassDescription::LoadClassDescription(const char* className)
142{
143  this->className = new char[strlen(className)+1];
144  strcpy(this->className, className);
145
[5226]146  if (LoadClassDescription::classList == NULL)
147    LoadClassDescription::classList = new tList<LoadClassDescription>;
[4254]148
[5226]149  LoadClassDescription::classList->add(this);
150
[4254]151  this->paramList = new tList<LoadParamDescription>;
152}
153
[4256]154/**
[4836]155 *  deletes a classDescription (deletes all the parameterDescriptions as well
[5546]156 */
[4746]157LoadClassDescription::~LoadClassDescription()
[4254]158{
159  tIterator<LoadParamDescription>* iterator = this->paramList->getIterator();
[5115]160  LoadParamDescription* enumParamDesc = iterator->firstElement();
[4254]161  while (enumParamDesc)
[5546]162  {
163    delete enumParamDesc;
164    enumParamDesc = iterator->nextElement();
165  }
[4254]166  delete iterator;
[5227]167  delete this->paramList;
[5226]168
169  delete[] this->className;
[4254]170}
171
[5226]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
[4256]190/**
[4836]191 *  adds a class to the list of loadable classes
192 * @param className The name of the class to add
[4254]193
[4256]194   this function searches for the className string, and if found just returns the appropriate Class.
195   Otherwise it returns a new classDescription
[5546]196 */
[4254]197LoadClassDescription* LoadClassDescription::addClass(const char* className)
198{
[5226]199  if (LoadClassDescription::classList != NULL)
200  {
201    tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
[5546]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;
[5226]213  }
[4254]214  return new LoadClassDescription(className);
215}
216
[4256]217/**
[4836]218 *  does the same as addClass(const char* className), but with params
219 * @param paramName the name of the parameter to add.
[5546]220 */
[4254]221LoadParamDescription* LoadClassDescription::addParam(const char* paramName)
222{
223  tIterator<LoadParamDescription>* iterator = this->paramList->getIterator();
[5115]224  LoadParamDescription* enumParamDesc = iterator->firstElement();
[4254]225  while (enumParamDesc)
[5546]226  {
227    if (!strcmp(enumParamDesc->paramName, paramName))
[4254]228    {
[5546]229      delete iterator;
[5227]230          //return enumParamDesc;
[5546]231      return NULL;
[4254]232    }
[5546]233    enumParamDesc = iterator->nextElement();
234  }
[4254]235  delete iterator;
236
[5227]237  LoadParamDescription* newParam = new LoadParamDescription(paramName);
238
239  this->paramList->add(newParam);
240  return newParam;
[4254]241}
[4255]242
[4256]243/**
[4836]244 *  prints out all loadable Classes, and their parameters
[5100]245 * @param fileName prints the output to a File
246 * @todo implement it
[5546]247 */
[4260]248void LoadClassDescription::printAll(const char* fileName)
[4255]249{
[4259]250  PRINT(3)("===============================================================\n");
251  PRINT(3)(" Listing all the Loadable Options (loaded since Game started).\n\n");
[5226]252  if (LoadClassDescription::classList != NULL)
253  {
254    tIterator<LoadClassDescription>* classIT = LoadClassDescription::classList->getIterator();
255    LoadClassDescription* enumClassDesc = classIT->firstElement();
256    while (enumClassDesc)
[5546]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;
[4255]267
[5546]268      PRINT(3)("</%s>\n\n", enumClassDesc->className);
269      enumClassDesc = classIT->nextElement();
270    }
[5226]271    delete classIT;
272  }
273  else
274    PRINT(3)("no Classes defined so far\n");
[4259]275  PRINT(3)("===============================================================\n");
[4255]276}
[4492]277
[5100]278/**
279 * searches for classes, which beginn with classNameBegin
280 * @param classNameBegin the beginning string of a Class
[5113]281 * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards,
[5100]282 * !! The strings MUST NOT be deleted !!
283 */
[5113]284tList<const char>* LoadClassDescription::searchClassWithShort(const char* classNameBegin)
[5100]285{
286  unsigned int searchLength = strlen(classNameBegin);
[5113]287  tList<const char>* retVal = new tList<const char>;
[4492]288
[5100]289  tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
[5115]290  LoadClassDescription* enumClassDesc = iterator->firstElement();
[5100]291  while (enumClassDesc)
292  {
293    if (strlen(enumClassDesc->className)>searchLength+1 &&
294        !strncasecmp(enumClassDesc->className, classNameBegin, searchLength))
295    {
[5113]296      retVal->add(enumClassDesc->className);
[5100]297    }
298    enumClassDesc = iterator->nextElement();
299  }
300  delete iterator;
[4492]301
[5100]302  return retVal;
303}
Note: See TracBrowser for help on using the repository browser.