Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Executor works just fine.
Changed: Paramerte* to MT_*, to make things more general

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