Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: taken out LoadParamDescription into a file of its own

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