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 | |
---|
24 | using namespace std; |
---|
25 | |
---|
26 | MultiType::MultiType() |
---|
27 | { |
---|
28 | this->type = MT_NULL; |
---|
29 | this->storedString = NULL; |
---|
30 | } |
---|
31 | |
---|
32 | |
---|
33 | MultiType::MultiType(bool value) |
---|
34 | { |
---|
35 | this->init(); |
---|
36 | this->setBool(value); |
---|
37 | } |
---|
38 | |
---|
39 | MultiType::MultiType(int value) |
---|
40 | { |
---|
41 | this->init(); |
---|
42 | this->setInt(value); |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | MultiType::MultiType(double value) |
---|
47 | { |
---|
48 | this->init(); |
---|
49 | this->setFloat(value); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | MultiType::MultiType(char value) |
---|
54 | { |
---|
55 | this->init(); |
---|
56 | this->setChar(value); |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | MultiType::MultiType(const char* value) |
---|
61 | { |
---|
62 | this->init(); |
---|
63 | this->setString(value); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * standard deconstructor |
---|
68 | */ |
---|
69 | MultiType::~MultiType () |
---|
70 | { |
---|
71 | if (this->storedString != NULL) |
---|
72 | delete[] this->storedString; |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * copy Constructor |
---|
77 | * @param mt: the entity to copy |
---|
78 | * @returns a Copy of itself. (strings inside are copied as well) |
---|
79 | */ |
---|
80 | MultiType MultiType::operator= (const MultiType& mt) |
---|
81 | { |
---|
82 | this->type = mt.type; |
---|
83 | this->value = mt.value; |
---|
84 | |
---|
85 | if (mt.type == MT_STRING) |
---|
86 | { |
---|
87 | this->storedString = new char[strlen (mt.storedString)+1]; |
---|
88 | strcpy(this->storedString, mt.storedString); |
---|
89 | this->value.String = this->storedString; |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | void MultiType::init() |
---|
94 | { |
---|
95 | this->type = MT_NULL; |
---|
96 | this->storedString = NULL; |
---|
97 | } |
---|
98 | |
---|
99 | |
---|
100 | |
---|
101 | void MultiType::setType(MT_Type) |
---|
102 | { |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | void MultiType::setBool(bool value) |
---|
107 | { |
---|
108 | this->type = MT_BOOL; |
---|
109 | this->value.Bool = value; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | void MultiType::setInt(int value) |
---|
114 | { |
---|
115 | this->type = MT_INT; |
---|
116 | this->value.Int = value; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | void MultiType::setFloat(float value) |
---|
121 | { |
---|
122 | this->type = MT_FLOAT; |
---|
123 | this->value.Float = value; |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | void MultiType::setChar(char value) |
---|
129 | { |
---|
130 | this->type = MT_CHAR; |
---|
131 | this->value.Char = value; |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | void MultiType::setString(const char* value) |
---|
136 | { |
---|
137 | this->type = MT_STRING; |
---|
138 | this->value.String = new char[strlen(value)+1]; |
---|
139 | strcpy(this->value.String, value); |
---|
140 | |
---|
141 | this->storedString = this->value.String; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | bool MultiType::getBool() const |
---|
150 | { |
---|
151 | // default case: |
---|
152 | if (this->type & MT_BOOL) |
---|
153 | return this->value.Bool; |
---|
154 | // Special Cases: |
---|
155 | else if (this->type & MT_INT) return (this->value.Int == 0)? false : true; |
---|
156 | else if (this->type & MT_FLOAT) return (this->value.Float == 0.0f)? false : true; |
---|
157 | else if (this->type & MT_CHAR) return (this->value.Char == '\0')? false : true; |
---|
158 | 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. |
---|
159 | |
---|
160 | return false; |
---|
161 | } |
---|
162 | |
---|
163 | |
---|
164 | int MultiType::getInt() const |
---|
165 | { |
---|
166 | // default case: |
---|
167 | if (this->type & MT_INT) |
---|
168 | return this->value.Int; |
---|
169 | if (this->type & MT_BOOL) return (this->value.Bool)? 1 : 0; |
---|
170 | else if (this->type & MT_FLOAT) return (int) this->value.Float; |
---|
171 | else if (this->type & MT_CHAR) return (int) this->value.Char; |
---|
172 | else if (this->type & MT_STRING) { |
---|
173 | char* endPtr = NULL; |
---|
174 | int result = strtol(this->value.String, &endPtr, 10); |
---|
175 | if ( endPtr >= this->value.String && endPtr < this->value.String + strlen(this->value.String)) |
---|
176 | return 0; |
---|
177 | else |
---|
178 | return result; |
---|
179 | } |
---|
180 | |
---|
181 | return 0; |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | float MultiType::getFloat() const |
---|
186 | { |
---|
187 | // default case: |
---|
188 | if (this->type & MT_FLOAT) |
---|
189 | return this->value.Float; |
---|
190 | if (this->type & MT_BOOL) return (this->value.Bool == true)? 1.0f : 0.0f; |
---|
191 | else if (this->type & MT_INT) return (float) this->value.Int; |
---|
192 | else if (this->type & MT_CHAR) return (float) this->value.Char; |
---|
193 | else if (this->type & MT_STRING) { |
---|
194 | char* endPtr = NULL; |
---|
195 | double result = strtod(this->value.String, &endPtr); |
---|
196 | if ( endPtr >= this->value.String && endPtr < this->value.String + strlen(this->value.String)) |
---|
197 | return 0.0f; |
---|
198 | else |
---|
199 | return result; |
---|
200 | } |
---|
201 | |
---|
202 | return 0.0f; |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | char MultiType::getChar() const |
---|
207 | { |
---|
208 | // default case: |
---|
209 | if (this->type & MT_INT) |
---|
210 | return this->value.Int; |
---|
211 | if (this->type & MT_BOOL) return (this->value.Bool)? 'y' : 'n'; |
---|
212 | else if (this->type & MT_INT) return (int) this->value.Int; |
---|
213 | else if (this->type & MT_FLOAT) return (char) this->value.Float; |
---|
214 | else if (this->type & MT_STRING) return *this->value.String; |
---|
215 | |
---|
216 | return '\0'; |
---|
217 | } |
---|
218 | |
---|
219 | const char* MultiType::getString() |
---|
220 | { |
---|
221 | // default case: |
---|
222 | if (this->type & MT_STRING) |
---|
223 | return this->value.String; |
---|
224 | else |
---|
225 | { |
---|
226 | if (this->type & MT_BOOL) return (this->value.Bool)? "true" : "false"; |
---|
227 | else if (this->type & MT_CHAR) &this->value.Char; |
---|
228 | |
---|
229 | char tmpString[128]; |
---|
230 | if (this->storedString != NULL) |
---|
231 | { |
---|
232 | delete[] this->storedString; |
---|
233 | this->storedString = NULL; |
---|
234 | } |
---|
235 | |
---|
236 | if (this->type & MT_INT) |
---|
237 | { |
---|
238 | sprintf(tmpString, "%d", this->value.Int); |
---|
239 | this->storedString = new char[strlen (tmpString)+1]; |
---|
240 | strcpy (this->storedString, tmpString); |
---|
241 | return this->storedString; |
---|
242 | } |
---|
243 | if (this->type & MT_FLOAT) |
---|
244 | { |
---|
245 | sprintf(tmpString, "%f", this->value.Float); |
---|
246 | this->storedString = new char[strlen (tmpString)+1]; |
---|
247 | strcpy (this->storedString, tmpString); |
---|
248 | return this->storedString; |
---|
249 | } |
---|
250 | } |
---|
251 | |
---|
252 | return ""; |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | void MultiType::debug() |
---|
257 | { |
---|
258 | printf("MultiType of Type: %s is: BOOL: %d, INT: %d, FLOAT: %f, CHAR: %c, STRING %s\n", |
---|
259 | MultiType::MultiTypeToString(this->type), |
---|
260 | this->getBool(), |
---|
261 | this->getInt(), |
---|
262 | this->getFloat(), |
---|
263 | this->getChar(), |
---|
264 | this->getString() |
---|
265 | ); |
---|
266 | |
---|
267 | |
---|
268 | } |
---|
269 | |
---|
270 | const char* MultiType::MultiTypeToString(MT_Type type) |
---|
271 | { |
---|
272 | switch (type) |
---|
273 | { |
---|
274 | default: |
---|
275 | return "NONE"; |
---|
276 | case MT_BOOL: |
---|
277 | return "bool"; |
---|
278 | case MT_INT: |
---|
279 | return "int"; |
---|
280 | case MT_FLOAT: |
---|
281 | return "float"; |
---|
282 | case MT_CHAR: |
---|
283 | return "char"; |
---|
284 | case MT_STRING: |
---|
285 | return "string"; |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | MT_Type MultiType::StringToMultiType(const char* type) |
---|
290 | { |
---|
291 | if (!strncmp(type, "bool", 4)) |
---|
292 | return MT_BOOL; |
---|
293 | if (!strncmp(type, "int", 3)) |
---|
294 | return MT_INT; |
---|
295 | if (!strncmp(type, "float", 5)) |
---|
296 | return MT_FLOAT; |
---|
297 | if (!strncmp(type, "char", 4)) |
---|
298 | return MT_CHAR; |
---|
299 | if (!strncmp(type, "string", 6)) |
---|
300 | return MT_STRING; |
---|
301 | } |
---|