Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/multi_type.cc @ 5659

Last change on this file since 5659 was 5659, checked in by bensch, 18 years ago

orxonox/trunk: valgrind sweep - no more Use of Uninitialized Value in MultiType

File size: 9.0 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//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "multi_type.h"
19
20//#include "stdincl.h"
21#include <stddef.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdio.h>
25
26#ifdef DEBUG
27#include "debug.h"
28#endif
29
30using namespace std;
31
32/**
33 * creates a multiType without any stored value at all.
34 */
35MultiType::MultiType()
36{
37  this->init();
38}
39/**
40 * creates a multiType out of a boolean
41 * @param value the Value of this MulitType
42 */
43MultiType::MultiType(bool value)
44{
45  this->init();
46  this->setBool(value);
47}
48
49/**
50 * creates a multiType out of an integer
51 * @param value the Value of this MulitType
52 */
53MultiType::MultiType(int value)
54{
55  this->init();
56  this->setInt(value);
57}
58
59/**
60 * creates a multiType out of a float (double)
61 * @param value the Value of this MulitType
62 */
63MultiType::MultiType(double value)
64{
65  this->init();
66  this->setFloat(value);
67}
68
69/**
70 * creates a multiType out of a char
71 * @param value the Value of this MulitType
72 */
73MultiType::MultiType(char value)
74{
75  this->init();
76  this->setChar(value);
77}
78
79/**
80 * creates a multiType out of a String
81 * @param value the Value of this MulitType
82 */
83MultiType::MultiType(const char* value)
84{
85  this->init();
86  this->setString(value);
87}
88
89MultiType::MultiType(const MultiType& multiType)
90{
91  this->init();
92  printf("test2");
93  *this = multiType;
94}
95
96/**
97 * standard deconstructor
98*/
99MultiType::~MultiType ()
100{
101  if (this->storedString != NULL)
102    delete[] this->storedString;
103}
104
105/**
106 * copy Constructor
107 * @param mt: the entity to copy
108 * @returns a Copy of itself. (strings inside are copied as well)
109 */
110MultiType& MultiType::operator= (const MultiType& mt)
111{
112  this->type = mt.type;
113  this->value = mt.value;
114
115  printf("test\n");
116  if (mt.type == MT_STRING && mt.storedString != NULL)
117  {
118    this->setString(mt.storedString);
119  }
120  else
121    this->storedString = NULL;
122  return *this;
123}
124
125/**
126 * initializes the MultiType
127 */
128void MultiType::init()
129{
130  this->type = MT_NULL;
131  this->storedString = NULL;
132  this->value.Float = 0.0f;
133}
134
135
136/**
137 * sets the type of this MultiType and resets to the default value
138 * @param type the new Type
139 */
140void MultiType::setType(int type)
141{
142  if (this->type != type)
143    this->type = (MT_Type)type;
144}
145
146/**
147 * sets a new Value to the MultiType
148 * @param value the new Value as a bool
149 */
150void MultiType::setBool(bool value)
151{
152  this->type = MT_BOOL;
153  this->value.Bool = value;
154}
155
156/**
157 * sets a new Value to the MultiType
158 * @param value the new Value as an int
159 */
160void MultiType::setInt(int value)
161{
162  this->type = MT_INT;
163  this->value.Int = value;
164}
165
166/**
167 * sets a new Value to the MultiType
168 * @param value the new Value as a float
169 */
170void MultiType::setFloat(float value)
171{
172  this->type = MT_FLOAT;
173  this->value.Float = value;
174}
175
176/**
177 * sets a new Value to the MultiType
178 * @param value the new Value as a char
179 */
180void MultiType::setChar(char value)
181{
182  this->type = MT_CHAR;
183  this->value.Char = value;
184}
185
186/**
187 * sets a new Value to the MultiType
188 * @param value the new Value as a String
189 */
190void MultiType::setString(const char* value)
191{
192  this->type = MT_STRING;
193
194  if (this->storedString != NULL)
195    delete[] this->storedString;
196
197  if (value == NULL)
198  {
199    this->storedString = new char[1];
200    this->storedString[0] = '\0';
201    this->value.String = this->storedString;
202    return;
203  }
204  else
205  {
206    this->storedString = new char[strlen(value)+1];
207    strcpy(storedString, value);
208    this->value.String = this->storedString;
209  }
210}
211
212
213
214
215
216/**
217 * @returns the Value of this MultiType as a int
218 */
219bool MultiType::getBool() const
220{
221  // default case:
222  if (this->type & MT_BOOL)
223    return this->value.Bool;
224  // Special Cases:
225  else if (this->type & MT_INT) return (this->value.Int == 0)? false : true;
226  else if (this->type & MT_FLOAT) return (this->value.Float == 0.0f)? false : true;
227  else if (this->type & MT_CHAR) return (this->value.Char == '\0')? false : true;
228  else if (this->type & MT_STRING) return (!strncmp(this->value.String, "true", 4) || !strncmp(this->value.String, "TRUE", 4) || !strncmp(this->value.String, "1", 1))? true : false; //! @TODO make this better.
229
230  return false;
231}
232
233/**
234 * @returns the Value of this MultiType as a int
235 */
236int MultiType::getInt() const
237{
238  // default case:
239  if (this->type & MT_INT)
240    return this->value.Int;
241  if (this->type & MT_BOOL) return (this->value.Bool)? 1 : 0;
242  else if (this->type & MT_FLOAT) return (int) this->value.Float;
243  else if (this->type & MT_CHAR) return (int) this->value.Char;
244  else if (this->type & MT_STRING) {
245    char* endPtr = NULL;
246    int result = strtol(this->value.String, &endPtr, 10);
247    if ( endPtr >= this->value.String && endPtr < this->value.String + strlen(this->value.String))
248      return 0;
249    else
250      return result;
251  }
252
253  return 0;
254}
255
256/**
257 * @returns the Value of this MultiType as a float
258 */
259float MultiType::getFloat() const
260{
261 // default case:
262  if (this->type & MT_FLOAT)
263    return this->value.Float;
264  if (this->type & MT_BOOL) return (this->value.Bool == true)? 1.0f : 0.0f;
265  else if (this->type & MT_INT) return (float) this->value.Int;
266  else if (this->type & MT_CHAR) return (float) this->value.Char;
267  else if (this->type & MT_STRING) {
268    char* endPtr = NULL;
269    double result = strtod(this->value.String, &endPtr);
270    if ( endPtr >= this->value.String && endPtr < this->value.String + strlen(this->value.String))
271      return 0.0f;
272    else
273      return result;
274  }
275
276  return 0.0f;
277}
278
279
280/**
281 * @returns the Value of this MultiType as a char
282 */
283char MultiType::getChar() const
284{
285 // default case:
286  if (this->type & MT_INT)
287    return this->value.Int;
288  if (this->type & MT_BOOL) return (this->value.Bool)? 'y' : 'n';
289  else if (this->type & MT_INT) return (int) this->value.Int;
290  else if (this->type & MT_FLOAT) return (char) this->value.Float;
291  else if (this->type & MT_STRING) return *this->value.String;
292
293  return '\0';
294}
295
296/**
297 * @returns the Value of this MultiType as a String
298 */
299const char* MultiType::getString()
300{
301 // default case:
302  if (this->type & MT_STRING)
303    return (this->value.String != NULL)? this->value.String : "";
304  else
305  {
306    if (this->type & MT_BOOL) return (this->value.Bool)? "true" : "false";
307    char tmpString[128];
308    if (this->storedString != NULL)
309    {
310      delete[] this->storedString;
311      this->storedString = NULL;
312    }
313    if (this->type & MT_CHAR)
314    {
315      this->storedString = new char[2];
316      this->storedString[0] = this->value.Char;
317      this->storedString[1] = '\0';
318      return this->storedString;
319    }
320    else if (this->type & MT_INT)
321    {
322      sprintf(tmpString, "%d", this->value.Int);
323      this->storedString = new char[strlen(tmpString)+1];
324      strcpy (this->storedString, tmpString);
325      return this->storedString;
326    }
327    if (this->type & MT_FLOAT)
328    {
329      sprintf(tmpString, "%f", this->value.Float);
330      this->storedString = new char[strlen (tmpString)+1];
331      strcpy (this->storedString, tmpString);
332      return this->storedString;
333    }
334  }
335
336  return "";
337}
338
339/**
340 * prints out some nice debug output
341 */
342void MultiType::debug()
343{
344#ifdef DEBUG
345  PRINT(0)
346#else
347  printf
348#endif
349      ("MultiType of Type '%s' :: Values: BOOL: '%d', INT: '%d', FLOAT: '%f', CHAR: '%c', STRING '%s'\n",
350         MultiType::MultiTypeToString(this->type),
351         this->getBool(),
352         this->getInt(),
353         this->getFloat(),
354         this->getChar(),
355         this->getString()
356      );
357
358
359}
360
361/**
362 * Resets the MultiType to default values.
363 */
364void MultiType::reset()
365{
366  switch ( this->type )
367  {
368    case MT_BOOL:
369      this->setBool(false);
370    case MT_INT:
371      this->setInt(0);
372      break;
373    case MT_FLOAT:
374      this->setFloat(0.0f);
375      break;
376    case MT_CHAR:
377      this->setChar('\0');
378      break;
379    case MT_STRING:
380      this->setString("");
381      break;
382    default:
383#ifdef DEBUG
384      PRINTF(2)("Unknown Type not reseting\n");
385#endif
386      break;
387  }
388}
389
390/**
391 * converts a MT_Type into a String
392 * @param type: the MT_Type
393 * @returns: the Type as a constant String (do not delete)
394 */
395const char* MultiType::MultiTypeToString(MT_Type type)
396{
397  switch ( type )
398  {
399   default:
400    return "NONE";
401   case MT_BOOL:
402    return "bool";
403   case MT_INT:
404    return "int";
405   case MT_FLOAT:
406    return "float";
407   case MT_CHAR:
408    return "char";
409   case MT_STRING:
410    return "string";
411  }
412}
413
414/**
415 * converts a String into a MT_Type
416 * @param type: the Type as a String
417 * @returns: the Type as MT_Type
418 */
419MT_Type MultiType::StringToMultiType(const char* type)
420{
421  if (!strncmp(type, "bool", 4))
422    return MT_BOOL;
423  if (!strncmp(type, "int", 3))
424    return MT_INT;
425  if (!strncmp(type, "float", 5))
426    return MT_FLOAT;
427  if (!strncmp(type, "char", 4))
428    return MT_CHAR;
429  if (!strncmp(type, "string", 6))
430    return MT_STRING;
431}
Note: See TracBrowser for help on using the repository browser.