Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: more elaborate MultiType, all get*() written

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