Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: moved functor_list.h

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