Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: some minor typo's in the MultiType class

File size: 4.3 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: ...
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "multi_type.h"
19#include <stddef.h>
20#include <stdlib.h>
21#include <string.h>
22#include <stdio.h>
23
24using namespace std;
25
26MultiType::MultiType(bool value)
27{
28  this->init();
29  this->type = MT_BOOL;
30  this->value.Bool = value;
31
32}
33
34MultiType::MultiType(int value)
35{
36  this->init();
37  this->type = MT_INT;
38  this->value.Int = value;
39}
40
41
42MultiType::MultiType(double value)
43{
44  this->init();
45  this->type = MT_FLOAT;
46  this->value.Float = value;
47}
48
49
50MultiType::MultiType(char value)
51{
52  this->init();
53  this->type = MT_CHAR;
54  this->value.Char = value;
55}
56
57
58MultiType::MultiType(const char* value)
59{
60  this->init();
61  this->type = MT_STRING;
62  this->value.String = new char[strlen(value)+1];
63  strcpy(this->value.String, value);
64
65  this->storedString = this->value.String;
66}
67
68/**
69 * standard deconstructor
70*/
71MultiType::~MultiType ()
72{
73  if (this->storedString != NULL)
74    delete this->storedString;
75}
76
77void MultiType::init()
78{
79  this->storedString = NULL;
80}
81
82
83bool MultiType::getBool()
84{
85  // default case:
86  if (this->type & MT_BOOL)
87    return this->value.Bool;
88  // Special Cases:
89  else if (this->type & MT_INT) return (this->value.Int == 0)? false : true;
90  else if (this->type & MT_FLOAT) return (this->value.Float == 0.0f)? false : true;
91  else if (this->type & MT_CHAR) return (this->value.Char == '\0')? false : true;
92  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.
93
94  return false;
95}
96
97
98int MultiType::getInt()
99{
100  // default case:
101  if (this->type & MT_INT)
102    return this->value.Int;
103  if (this->type & MT_BOOL) return (this->value.Bool)? 1 : 0;
104  else if (this->type & MT_FLOAT) return (int) this->value.Float;
105  else if (this->type & MT_CHAR) return (int) this->value.Char;
106  else if (this->type & MT_STRING) {
107    char* endPtr = NULL;
108    int result = strtol(this->value.String, &endPtr, 10);
109    if ( endPtr >= this->value.String && endPtr < this->value.String + strlen(this->value.String))
110      return 0;
111    else
112      return result;
113  }
114
115  return 0;
116}
117
118
119float MultiType::getFloat()
120{
121 // default case:
122  if (this->type & MT_FLOAT)
123    return this->value.Float;
124  if (this->type & MT_BOOL) return (this->value.Bool == true)? 1.0f : 0.0f;
125  else if (this->type & MT_INT) return (float) this->value.Int;
126  else if (this->type & MT_CHAR) return (float) this->value.Char;
127  else if (this->type & MT_STRING) {
128    char* endPtr = NULL;
129    double result = strtod(this->value.String, &endPtr);
130    if ( endPtr >= this->value.String && endPtr < this->value.String + strlen(this->value.String))
131      return 0.0f;
132    else
133      return result;
134  }
135
136  return 0.0f;
137}
138
139
140char MultiType::getChar()
141{
142 // default case:
143  if (this->type & MT_INT)
144    return this->value.Int;
145  if (this->type & MT_BOOL) return (this->value.Bool)? 'y' : 'n';
146  else if (this->type & MT_INT) return (int) this->value.Int;
147  else if (this->type & MT_FLOAT) return (char) this->value.Float;
148  else if (this->type & MT_STRING) return *this->value.String;
149
150  return '\0';
151}
152
153const char* MultiType::getString()
154{
155 // default case:
156  if (this->type & MT_STRING)
157    return this->value.String;
158  else
159  {
160    if (this->type & MT_BOOL) return (this->value.Bool)? "true" : "false";
161    else if (this->type & MT_CHAR) &this->value.Char;
162
163    char tmpString[128];
164    if (this->storedString != NULL)
165    {
166      delete[] this->storedString;
167      this->storedString = NULL;
168    }
169
170    if (this->type & MT_INT)
171    {
172      sprintf(tmpString, "%d", this->value.Int);
173      this->storedString = new char[strlen (tmpString)+1];
174      strcpy (this->storedString, tmpString);
175      return this->storedString;
176    }
177    if (this->type & MT_FLOAT)
178    {
179      sprintf(tmpString, "%f", this->value.Float);
180      this->storedString = new char[strlen (tmpString)+1];
181      strcpy (this->storedString, tmpString);
182      return this->storedString;
183    }
184  }
185
186  return "";
187}
188
Note: See TracBrowser for help on using the repository browser.