4 Scalable Building Simulator - Global Functions
5 The Skyscraper Project - Version 1.10 Alpha
6 Copyright (C)2004-2016 Ryan Thoryk
7 http://www.skyscrapersim.com
8 http://sourceforge.net/projects/skyscraper
9 Contact - ryan@tliquest.net
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 #include <OgreString.h>
34 #include <OgreStringConverter.h>
40 bool IsEven(int Number)
42 //Determine if the passed number is even.
43 //If number divides evenly, return true
45 if (((float)Number / 2) == int((float)Number / 2))
51 bool IsNumeric(char character)
53 //test to see if a character is numeric
55 if (character > 47 && character < 58)
60 bool IsNumeric(const std::string &string)
62 //test to see if a string is numeric
65 return IsNumeric(string, num);
68 bool IsNumeric(const std::string &string, int &number)
70 //test to see if a string is numeric, and return number as integer
73 bool result = IsNumeric(string, num);
78 bool IsNumeric(const std::string &string, float &number)
80 //test to see if a string is numeric, and return number as float
85 number = (float)std::strtod(string.c_str(), &end);
87 number = std::strtof(string.c_str(), &end);
90 return end != 0 && *end == 0;
93 bool IsNumeric(const std::string &string, double &number)
95 //test to see if a string is numeric, and return number as float
99 number = std::strtod(string.c_str(), &end);
101 return end != 0 && *end == 0;
104 std::string BoolToString(bool item)
112 float RadiansToDegrees(float radians)
114 //convert from radians to degrees
115 return radians * (180 / pi);
118 float DegreesToRadians(float degrees)
120 //convert from degrees to radians
121 return degrees * (pi / 180);
124 float Min3(float a, float b, float c)
126 //return smallest value
127 if (a <= b && a <= c)
129 if (b <= a && b <= c)
134 float Max3(float a, float b, float c)
136 //return largest value
137 if (a >= b && a >= c)
139 if (b >= a && b >= c)
144 float Min4(float a, float b, float c, float d)
146 //return smallest value
147 if (a <= b && a <= c && a <= d)
149 if (b <= a && b <= c && b <= d)
151 if (c <= a && c <= b && c <= d)
156 float Max4(float a, float b, float c, float d)
158 //return largest value
159 if (a >= b && a >= c && a >= d)
161 if (b >= a && b >= c && b >= d)
163 if (c >= a && c >= b && c >= d)
168 std::string SetCaseCopy(std::string string, bool uppercase)
170 //change case of a string
171 SetCase(string, uppercase);
175 void SetCase(std::string &string, bool uppercase)
177 //change case of a string
178 if (uppercase == true)
179 std::transform(string.begin(), string.end(), string.begin(), ::toupper);
181 std::transform(string.begin(), string.end(), string.begin(), ::tolower);
184 int FindWithCase(const std::string &string, bool uppercase, const std::string &key, int offset)
186 //change case of a string, and return location of search key
187 std::string newstring = SetCaseCopy(string, uppercase);
188 int loc = (int)newstring.find(key, offset);
189 if (loc == std::string::npos)
195 void TrimString(std::string &string)
197 //trim whitespace from string
198 Ogre::StringUtil::trim(string, true, true);
201 std::string TrimStringCopy(std::string string)
203 //trim whitespace from string
204 Ogre::StringUtil::trim(string, true, true);
208 void ReplaceAll(std::string &string, const std::string &original, const std::string &replacement)
210 //replace all occurrences of "original" with "replacement"
216 position = string.find(original, position);
217 if (position == string.npos)
219 string.replace(position, strlen(original.c_str()), replacement);
223 bool StartsWith(const std::string &string, const std::string &check_string, bool ignore_case)
225 //check if a string starts with the contents of "check_string"
229 if (ignore_case == true)
230 result = FindWithCase(string, false, check_string, 0);
232 result = (int)string.find(check_string, 0);
239 void SplitString(std::vector<std::string> &dest_array, const std::string &original_string, char separator)
241 //split a string into an array of strings, divided by "separator"
245 std::string newstring;
248 std::string original = original_string;
249 TrimString(original);
251 endpos = (int)original.find_first_of(separator, startpos);
254 newstring = original.substr(startpos, endpos - startpos);
255 TrimString(newstring);
256 dest_array.push_back(newstring);
261 newstring = original.substr(startpos, endpos - startpos);
262 TrimString(newstring);
263 dest_array.push_back(newstring); //add to vector
264 startpos = endpos + 1; //jump past separator
265 endpos = (int)original.find_first_of(separator, startpos); //find next
268 //last one, so no 2nd param required to go to end of string
269 newstring = original.substr(startpos);
270 TrimString(newstring);
271 dest_array.push_back(newstring);
276 std::string ToString(int number)
279 #if defined(__VISUALC__)
280 _snprintf_s(buffer, sizeof(buffer), 13, "%d", number);
282 snprintf(buffer, sizeof(buffer), "%d", number);
287 std::string ToString(float number)
290 #if defined(__VISUALC__)
291 _snprintf_s(buffer, sizeof(buffer), 13, "%g", number);
293 snprintf(buffer, sizeof(buffer), "%g", (double)number);
298 std::string ToString(double number)
301 #if defined(__VISUALC__)
302 _snprintf_s(buffer, sizeof(buffer), 13, "%g", number);
304 snprintf(buffer, sizeof(buffer), "%g", number);
309 float Log2(float number)
311 return logf(number) / logf(2.0f);
314 float Round(float number, int decimal_places)
316 //round float to specified decimal places
318 if (decimal_places <= 0)
319 return floorf(number + 0.5f);
321 float multiplier = powf(10.0f, (float)decimal_places);
322 float rounded = floorf((number * multiplier) + 0.5f) / multiplier;
326 Ogre::Vector3 Round(const Ogre::Vector3 &value, int decimal_places)
328 //round a 3D vector to specified decimal places
330 Ogre::Vector3 result;
331 result.x = Round(value.x, decimal_places);
332 result.y = Round(value.y, decimal_places);
333 result.z = Round(value.z, decimal_places);
337 Ogre::Vector2 Round(const Ogre::Vector2 &value, int decimal_places)
339 //round a 2D vector to specified decimal places
341 Ogre::Vector2 result;
342 result.x = Round(value.x, decimal_places);
343 result.y = Round(value.y, decimal_places);
347 bool IsBoolean(std::string string)
349 SetCase(string, false);
350 return (string == "true" || string == "false");
353 float ToFloat(const std::string &string)
355 return (float)atof(string.c_str());
358 int ToInt(const std::string &string)
360 return atoi(string.c_str());
363 bool ToBool(std::string string)
365 SetCase(string, false);
367 if (string == "true")
372 std::string TruncateNumber(float value, int decimals)
374 //truncates the numeric value to the specified number of decimal places (does not round)
376 if ((int)value == value)
377 decimals = 0; //value is an integer
379 std::stringstream buffer;
380 buffer.precision(decimals);
381 buffer << std::fixed << value;
386 std::string TruncateNumber(double value, int decimals)
388 //truncates the numeric value to the specified number of decimal places (does not round)
390 if ((int)value == value)
391 decimals = 0; //value is an integer
393 std::stringstream buffer;
394 buffer.precision(decimals);
395 buffer << std::fixed << value;
400 std::string TruncateNumber(const std::string &value, int decimals)
402 //truncates the numeric value to the specified number of decimal places (does not round)
403 std::string number = value;
407 number.erase((int)number.find(".") + decimals + 1);
408 if (number.at(number.length() - 1) == '.')
409 number = number.substr(0, number.length() - 1); //strip of extra decimal point if even
413 void Swap(int &first, int &second)
422 void Swap(float &first, float &second)