Skyscraper 2.0
globals.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Global Functions
3 The Skyscraper Project - Version 2.0
4 Copyright (C)2004-2024 Ryan Thoryk
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22*/
23
24#include <algorithm>
25#include <string>
26#include <ctype.h>
27#include <stdlib.h>
28#include <errno.h>
29#include <limits.h>
30#include <math.h>
31#include <OgreString.h>
32#include <OgreStringConverter.h>
33#include "globals.h"
34
35namespace SBS {
36
37bool IsEven(int Number)
38{
39 //Determine if the passed number is even.
40 //If number divides evenly, return true
41
42 if (((Real)Number / 2) == int((Real)Number / 2))
43 return true;
44 else
45 return false;
46}
47
48bool IsNumeric(char character)
49{
50 //test to see if a character is numeric
51
52 if (character > 47 && character < 58)
53 return true;
54 return false;
55}
56
57bool IsNumeric(const std::string &string)
58{
59 //test to see if a string is numeric
60
61 Real num = 0;
62 return IsNumeric(string, num);
63}
64
65bool IsNumeric(const std::string &string, int &number)
66{
67 //test to see if a string is numeric, and return number as integer
68
69 Real num = 0;
70 bool result = IsNumeric(string, num);
71 number = (int)num;
72 return result;
73}
74
75bool IsNumeric(const std::string &string, float &number)
76{
77 //test to see if a string is numeric, and return number as float
78
79 char* end = 0;
80
81#ifdef _WIN32
82 number = (float)std::strtod(string.c_str(), &end);
83#else
84 number = std::strtof(string.c_str(), &end);
85#endif
86
87 return end != 0 && *end == 0;
88}
89
90bool IsNumeric(const std::string &string, double &number)
91{
92 //test to see if a string is numeric, and return number as float
93
94 char* end = 0;
95
96 number = std::strtod(string.c_str(), &end);
97
98 return end != 0 && *end == 0;
99}
100
101std::string BoolToString(bool item)
102{
103 if (item == true)
104 return "true";
105 else
106 return "false";
107}
108
110{
111 //convert from radians to degrees
112 return radians * (180 / pi);
113}
114
116{
117 //convert from degrees to radians
118 return degrees * (pi / 180);
119}
120
122{
123 //return smallest value
124 if (a <= b && a <= c)
125 return a;
126 if (b <= a && b <= c)
127 return b;
128 return c;
129}
130
132{
133 //return largest value
134 if (a >= b && a >= c)
135 return a;
136 if (b >= a && b >= c)
137 return b;
138 return c;
139}
140
142{
143 //return smallest value
144 if (a <= b && a <= c && a <= d)
145 return a;
146 if (b <= a && b <= c && b <= d)
147 return b;
148 if (c <= a && c <= b && c <= d)
149 return c;
150 return d;
151}
152
154{
155 //return largest value
156 if (a >= b && a >= c && a >= d)
157 return a;
158 if (b >= a && b >= c && b >= d)
159 return b;
160 if (c >= a && c >= b && c >= d)
161 return c;
162 return d;
163}
164
165std::string SetCaseCopy(std::string string, bool uppercase)
166{
167 //change case of a string
168 SetCase(string, uppercase);
169 return string;
170}
171
172void SetCase(std::string &string, bool uppercase)
173{
174 //change case of a string
175 if (uppercase == true)
176 std::transform(string.begin(), string.end(), string.begin(), ::toupper);
177 else
178 std::transform(string.begin(), string.end(), string.begin(), ::tolower);
179}
180
181int FindWithCase(const std::string &string, bool uppercase, const std::string &key, int offset)
182{
183 //change case of a string, and return location of search key
184 std::string newstring = SetCaseCopy(string, uppercase);
185 return (int)newstring.find(key, offset);
186}
187
188void TrimString(std::string &string)
189{
190 //trim whitespace from string
191 Ogre::StringUtil::trim(string, true, true);
192}
193
194std::string TrimStringCopy(std::string string)
195{
196 //trim whitespace from string
197 Ogre::StringUtil::trim(string, true, true);
198 return string;
199}
200
201void ReplaceAll(std::string &string, const std::string &original, const std::string &replacement)
202{
203 //replace all occurrences of "original" with "replacement"
204
205 size_t position = 0;
206
207 while(true)
208 {
209 position = string.find(original, position);
210 if (position == string.npos)
211 break;
212 string.replace(position, strlen(original.c_str()), replacement);
213 }
214}
215
216bool StartsWith(const std::string &string, const std::string &check_string, bool ignore_case)
217{
218 //check if a string starts with the contents of "check_string"
219
220 if (ignore_case == true)
221 {
222 std::string lower = string.substr(0, check_string.size());
223 SetCase(lower, false);
224
225 if (lower == SetCaseCopy(check_string, false))
226 return true;
227 }
228 else
229 {
230 if (string.substr(0, check_string.size()) == check_string)
231 return true;
232 }
233
234 return false;
235}
236
237bool StartsWithNoCase(const std::string &string, const std::string &check_string)
238{
239 return StartsWith(string, check_string, true);
240}
241
242void SplitString(std::vector<std::string> &dest_array, const std::string &original_string, char separator)
243{
244 //split a string into an array of strings, divided by "separator"
245
246 size_t startpos = 0;
247 size_t endpos = 0;
248 std::string newstring;
249
250 dest_array.clear();
251 std::string original = original_string;
252 TrimString(original);
253
254 endpos = original.find_first_of(separator, startpos);
255 if (endpos == std::string::npos)
256 {
257 newstring = original.substr(startpos, endpos - startpos);
258 TrimString(newstring);
259 dest_array.emplace_back(newstring);
260 }
261
262 while (endpos != std::string::npos)
263 {
264 newstring = original.substr(startpos, endpos - startpos);
265 TrimString(newstring);
266 dest_array.emplace_back(newstring); //add to vector
267 startpos = endpos + 1; //jump past separator
268 endpos = original.find_first_of(separator, startpos); //find next
269 if(endpos == std::string::npos)
270 {
271 //last one, so no 2nd param required to go to end of string
272 newstring = original.substr(startpos);
273 TrimString(newstring);
274 dest_array.emplace_back(newstring);
275 }
276 }
277}
278
279std::string ToString(int number)
280{
281 char buffer[50];
282#if defined(__VISUALC__)
283 _snprintf_s(buffer, sizeof(buffer), 13, "%d", number);
284#else
285 snprintf(buffer, sizeof(buffer), "%d", number);
286#endif
287 return buffer;
288}
289
290std::string ToString(float number)
291{
292 char buffer[50];
293#if defined(__VISUALC__)
294 _snprintf_s(buffer, sizeof(buffer), 13, "%g", number);
295#else
296 snprintf(buffer, sizeof(buffer), "%g", (double)number);
297#endif
298 return buffer;
299}
300
301std::string ToString(double number)
302{
303 char buffer[50];
304#if defined(__VISUALC__)
305 _snprintf_s(buffer, sizeof(buffer), 13, "%g", number);
306#else
307 snprintf(buffer, sizeof(buffer), "%g", number);
308#endif
309 return buffer;
310}
311
312#if defined(__VISUALC__)
313std::string ToString(size_t number)
314{
315 char buffer[50];
316#if defined(__VISUALC__)
317 _snprintf_s(buffer, sizeof(buffer), 13, "%lu", number);
318#else
319 snprintf(buffer, sizeof(buffer), "%lu", number);
320#endif
321 return buffer;
322}
323#endif
324
325std::string ToString(unsigned long number)
326{
327 char buffer[50];
328#if defined(__VISUALC__)
329 _snprintf_s(buffer, sizeof(buffer), 13, "%lu", number);
330#else
331 snprintf(buffer, sizeof(buffer), "%lu", number);
332#endif
333 return buffer;
334}
335
336float Log2(float number)
337{
338 return logf(number) / logf(2.0f);
339}
340
341double Log2(double number)
342{
343 return log(number) / log(2.0);
344}
345
346float Round(float number, int decimal_places)
347{
348 //round float to specified decimal places
349
350 if (decimal_places <= 0)
351 return floorf(number + 0.5f);
352
353 float multiplier = powf(10.0f, (float)decimal_places);
354 float rounded = floorf((number * multiplier) + 0.5f) / multiplier;
355 return rounded;
356}
357
358double Round(double number, int decimal_places)
359{
360 //round float to specified decimal places
361
362 if (decimal_places <= 0)
363 return floor(number + 0.5);
364
365 double multiplier = pow(10.0, decimal_places);
366 double rounded = floor((number * multiplier) + 0.5) / multiplier;
367 return rounded;
368}
369
370Vector3 Round(const Vector3 &value, int decimal_places)
371{
372 //round a 3D vector to specified decimal places
373
374 Vector3 result;
375 result.x = Round(value.x, decimal_places);
376 result.y = Round(value.y, decimal_places);
377 result.z = Round(value.z, decimal_places);
378 return result;
379}
380
381Vector2 Round(const Vector2 &value, int decimal_places)
382{
383 //round a 2D vector to specified decimal places
384
385 Vector2 result;
386 result.x = Round(value.x, decimal_places);
387 result.y = Round(value.y, decimal_places);
388 return result;
389}
390
391bool IsBoolean(std::string string)
392{
393 SetCase(string, false);
394 return (string == "true" || string == "false");
395}
396
397Real ToFloat(const std::string &string)
398{
399 return atof(string.c_str());
400}
401
402int ToInt(const std::string &string)
403{
404 return atoi(string.c_str());
405}
406
407bool ToBool(std::string string)
408{
409 SetCase(string, false);
410
411 if (string == "true")
412 return true;
413 return false;
414}
415
416std::string TruncateNumber(float value, int decimals)
417{
418 //truncates the numeric value to the specified number of decimal places (does not round)
419
420 if ((int)value == value)
421 decimals = 0; //value is an integer
422
423 std::stringstream buffer;
424 buffer.precision(decimals);
425 buffer << std::fixed << value;
426
427 return buffer.str();
428}
429
430std::string TruncateNumber(double value, int decimals)
431{
432 //truncates the numeric value to the specified number of decimal places (does not round)
433
434 if ((int)value == value)
435 decimals = 0; //value is an integer
436
437 std::stringstream buffer;
438 buffer.precision(decimals);
439 buffer << std::fixed << value;
440
441 return buffer.str();
442}
443
444std::string TruncateNumber(const std::string &value, int decimals)
445{
446 //truncates the numeric value to the specified number of decimal places (does not round)
447
448 std::string number = value;
449
450 if (decimals < 1)
451 return number;
452
453 int decimal = number.find(".");
454 if (decimal < 0)
455 return number;
456
457 number.erase(decimal + decimals + 1);
458 if (number.at(number.length() - 1) == '.')
459 number = number.substr(0, number.length() - 1); //strip of extra decimal point if even
460 return number;
461}
462
463}
Ogre::Vector3 Vector3
Definition globals.h:58
Ogre::Real Real
Definition globals.h:57
Ogre::Vector2 Vector2
Definition globals.h:59
bool IsBoolean(std::string string)
Definition globals.cpp:391
Real DegreesToRadians(Real degrees)
Definition globals.cpp:115
const Real pi
Definition globals.h:71
bool StartsWithNoCase(const std::string &string, const std::string &check_string)
Definition globals.cpp:237
std::string TruncateNumber(float value, int decimals)
Definition globals.cpp:416
int FindWithCase(const std::string &string, bool uppercase, const std::string &key, int offset)
Definition globals.cpp:181
void SplitString(std::vector< std::string > &dest_array, const std::string &original_string, char separator)
Definition globals.cpp:242
Real Min(Real a, Real b, Real c)
Definition globals.cpp:121
void ReplaceAll(std::string &string, const std::string &original, const std::string &replacement)
Definition globals.cpp:201
int ToInt(const std::string &string)
Definition globals.cpp:402
Real Max(Real a, Real b, Real c)
Definition globals.cpp:131
void SetCase(std::string &string, bool uppercase)
Definition globals.cpp:172
std::string ToString(int number)
Definition globals.cpp:279
float Round(float number, int decimal_places)
Definition globals.cpp:346
std::string SetCaseCopy(std::string string, bool uppercase)
Definition globals.cpp:165
Real ToFloat(const std::string &string)
Definition globals.cpp:397
bool IsNumeric(char character)
Definition globals.cpp:48
bool StartsWith(const std::string &string, const std::string &check_string, bool ignore_case)
Definition globals.cpp:216
std::string BoolToString(bool item)
Definition globals.cpp:101
void TrimString(std::string &string)
Definition globals.cpp:188
std::string TrimStringCopy(std::string string)
Definition globals.cpp:194
bool ToBool(std::string string)
Definition globals.cpp:407
bool IsEven(int Number)
Definition globals.cpp:37
Real RadiansToDegrees(Real radians)
Definition globals.cpp:109
float Log2(float number)
Definition globals.cpp:336