Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/util/loading/load_param_description.cc @ 6110

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

orxonox/trunk: moved functor_list.h

File size: 8.2 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
18#include "multi_type.h"
19#include "list.h"
20#include <stdarg.h>
21#include "stdlibincl.h"
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    // 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//     }
103  }
104  PRINT(3)("</%s>", this->paramName);
105  if (this->description)
106    PRINT(3)(" -- %s", this->description);
107  // default values
108  if (this->paramCount > 0)
109  {
110    PRINT(3)(" (Default: ");
111    for (int i = 0; i < this->paramCount; i++)
112    {
113      if (i > 0)
114        PRINT(3)(", ");
115      if (this->types[i] & MT_STRING)
116      { // leave brackets !!
117        PRINT(3)("\"%s\"", this->defaultValues[i]);
118      }
119      else
120      {
121        PRINT(3)("%s", this->defaultValues[i]);
122      }
123    }
124    PRINT(3)(")");
125  }
126  PRINT(3)("\n");
127}
128
129/**
130 *  A list, that holds all the classes that are loadable (classes not objects!!)
131 */
132tList<LoadClassDescription>* LoadClassDescription::classList = NULL;
133
134/**
135 *  if the description of Parameters should be executed
136 */
137bool LoadClassDescription::parametersDescription = false;
138
139/**
140 * @param className the name of the class to be loadable
141 */
142LoadClassDescription::LoadClassDescription(const char* className)
143{
144  this->className = new char[strlen(className)+1];
145  strcpy(this->className, className);
146
147  if (LoadClassDescription::classList == NULL)
148    LoadClassDescription::classList = new tList<LoadClassDescription>;
149
150  LoadClassDescription::classList->add(this);
151
152  this->paramList = new tList<LoadParamDescription>;
153}
154
155/**
156 *  deletes a classDescription (deletes all the parameterDescriptions as well
157 */
158LoadClassDescription::~LoadClassDescription()
159{
160  tIterator<LoadParamDescription>* iterator = this->paramList->getIterator();
161  LoadParamDescription* enumParamDesc = iterator->firstElement();
162  while (enumParamDesc)
163  {
164    delete enumParamDesc;
165    enumParamDesc = iterator->nextElement();
166  }
167  delete iterator;
168  delete this->paramList;
169
170  delete[] this->className;
171}
172
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
191/**
192 *  adds a class to the list of loadable classes
193 * @param className The name of the class to add
194
195   this function searches for the className string, and if found just returns the appropriate Class.
196   Otherwise it returns a new classDescription
197 */
198LoadClassDescription* LoadClassDescription::addClass(const char* className)
199{
200  if (LoadClassDescription::classList != NULL)
201  {
202    tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
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;
214  }
215  return new LoadClassDescription(className);
216}
217
218/**
219 *  does the same as addClass(const char* className), but with params
220 * @param paramName the name of the parameter to add.
221 */
222LoadParamDescription* LoadClassDescription::addParam(const char* paramName)
223{
224  tIterator<LoadParamDescription>* iterator = this->paramList->getIterator();
225  LoadParamDescription* enumParamDesc = iterator->firstElement();
226  while (enumParamDesc)
227  {
228    if (!strcmp(enumParamDesc->paramName, paramName))
229    {
230      delete iterator;
231          //return enumParamDesc;
232      return NULL;
233    }
234    enumParamDesc = iterator->nextElement();
235  }
236  delete iterator;
237
238  LoadParamDescription* newParam = new LoadParamDescription(paramName);
239
240  this->paramList->add(newParam);
241  return newParam;
242}
243
244/**
245 *  prints out all loadable Classes, and their parameters
246 * @param fileName prints the output to a File
247 * @todo implement it
248 */
249void LoadClassDescription::printAll(const char* fileName)
250{
251  PRINT(3)("===============================================================\n");
252  PRINT(3)(" Listing all the Loadable Options (loaded since Game started).\n\n");
253  if (LoadClassDescription::classList != NULL)
254  {
255    tIterator<LoadClassDescription>* classIT = LoadClassDescription::classList->getIterator();
256    LoadClassDescription* enumClassDesc = classIT->firstElement();
257    while (enumClassDesc)
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;
268
269      PRINT(3)("</%s>\n\n", enumClassDesc->className);
270      enumClassDesc = classIT->nextElement();
271    }
272    delete classIT;
273  }
274  else
275    PRINT(3)("no Classes defined so far\n");
276  PRINT(3)("===============================================================\n");
277}
278
279/**
280 * searches for classes, which beginn with classNameBegin
281 * @param classNameBegin the beginning string of a Class
282 * @return a NEW char-array with ClassNames. The LIST should be deleted afterwards,
283 * !! The strings MUST NOT be deleted !!
284 */
285tList<const char>* LoadClassDescription::searchClassWithShort(const char* classNameBegin)
286{
287  unsigned int searchLength = strlen(classNameBegin);
288  tList<const char>* retVal = new tList<const char>;
289
290  tIterator<LoadClassDescription>* iterator = LoadClassDescription::classList->getIterator();
291  LoadClassDescription* enumClassDesc = iterator->firstElement();
292  while (enumClassDesc)
293  {
294    if (strlen(enumClassDesc->className)>searchLength+1 &&
295        !strncasecmp(enumClassDesc->className, classNameBegin, searchLength))
296    {
297      retVal->add(enumClassDesc->className);
298    }
299    enumClassDesc = iterator->nextElement();
300  }
301  delete iterator;
302
303  return retVal;
304}
Note: See TracBrowser for help on using the repository browser.