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: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
17 | |
---|
18 | #include "helper_functions.h" |
---|
19 | #include "stdlibincl.h" |
---|
20 | |
---|
21 | using namespace std; |
---|
22 | |
---|
23 | /** |
---|
24 | * @brief checks if the input was a bool |
---|
25 | * @param BOOL a String that holds a bool: must be one of those: 1,0,true,false(case-insensitive) |
---|
26 | * @param defaultValue a default value that is set, if BOOL is corrupt |
---|
27 | * @return returns the bool, if BOOL was correct otherwise defaultValue |
---|
28 | */ |
---|
29 | bool isBool(const std::string& BOOL, bool defaultValue) |
---|
30 | { |
---|
31 | if (BOOL.empty()) |
---|
32 | return defaultValue; |
---|
33 | if(BOOL[0] == '1' || BOOL == "true" ) |
---|
34 | return true; |
---|
35 | else if (BOOL[0] == '0' || BOOL == "false") |
---|
36 | return false; |
---|
37 | else |
---|
38 | return defaultValue; |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | /** |
---|
43 | * @brief checks if the input was a int |
---|
44 | * @param INT a String that holds an int. |
---|
45 | * @param defaultValue a default value that is set, if INT is corrupt |
---|
46 | * @return returns the contained int, if INT was correct otherwise defaultValue |
---|
47 | */ |
---|
48 | int isInt(const std::string& INT, int defaultValue) |
---|
49 | { |
---|
50 | if (INT.empty()) |
---|
51 | return defaultValue; |
---|
52 | char* endPtr = NULL; |
---|
53 | |
---|
54 | int result = strtol(INT.c_str(), &endPtr, 10); |
---|
55 | |
---|
56 | if ( endPtr >= INT.c_str() && endPtr < INT.c_str()+ INT.size()) |
---|
57 | return defaultValue; |
---|
58 | else |
---|
59 | return result; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | /** |
---|
64 | * @brief checks if the input was a float |
---|
65 | * @param FLOAT a String that holds an float. |
---|
66 | * @param defaultValue a default value that is set, if FLOAT is corrupt |
---|
67 | * @return returns the contained float, if FLOAT was correct otherwise defaultValue |
---|
68 | */ |
---|
69 | float isFloat(const std::string& FLOAT, float defaultValue) |
---|
70 | { |
---|
71 | if (FLOAT.empty()) |
---|
72 | return defaultValue; |
---|
73 | char* endPtr = NULL; |
---|
74 | double result = strtod(FLOAT.c_str(), &endPtr); |
---|
75 | |
---|
76 | if ( endPtr >= FLOAT.c_str() && endPtr < FLOAT.c_str() + FLOAT.size()) |
---|
77 | return defaultValue; |
---|
78 | else |
---|
79 | return result; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | /** |
---|
84 | * @brief checks if the input was a string |
---|
85 | * @param STING a String(char-array) that holds an string. |
---|
86 | * @param defaultValue a default value that is set, if STRING is corrupt |
---|
87 | * @return returns the contained string (char-array), if STRING was correct otherwise defaultValue |
---|
88 | */ |
---|
89 | const char* isCString(const std::string& STRING, const char* defaultValue) |
---|
90 | { |
---|
91 | if (STRING.size() > 0) |
---|
92 | return STRING.c_str(); |
---|
93 | else |
---|
94 | return defaultValue; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * @brief checks if the input was a string |
---|
99 | * @param STING a String(char-array) that holds an string. |
---|
100 | * @param defaultValue a default value that is set, if STRING is corrupt |
---|
101 | * @return returns the contained string (char-array), if STRING was correct otherwise defaultValue |
---|
102 | */ |
---|
103 | std::string isString(const std::string& STRING, const std::string& defaultValue) |
---|
104 | { |
---|
105 | if (STRING.size() > 0) |
---|
106 | return STRING; |
---|
107 | else |
---|
108 | return defaultValue; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|
112 | /** |
---|
113 | * @brief compares two strings without ignoring the case |
---|
114 | * @param s1 first string |
---|
115 | * @param s2 second string |
---|
116 | */ |
---|
117 | int nocase_cmp(const std::string& s1, const std::string& s2) |
---|
118 | { |
---|
119 | std::string::const_iterator it1=s1.begin(); |
---|
120 | std::string::const_iterator it2=s2.begin(); |
---|
121 | |
---|
122 | //stop when either string's end has been reached |
---|
123 | while ( (it1!=s1.end()) && (it2!=s2.end()) ) |
---|
124 | { |
---|
125 | if(::toupper(*it1) != ::toupper(*it2)) //letters differ? |
---|
126 | // return -1 to indicate smaller than, 1 otherwise |
---|
127 | return (::toupper(*it1) < ::toupper(*it2)) ? -1 : 1; |
---|
128 | //proceed to the next character in each string |
---|
129 | ++it1; |
---|
130 | ++it2; |
---|
131 | } |
---|
132 | size_t size1=s1.size(), size2=s2.size();// cache lengths |
---|
133 | //return -1,0 or 1 according to strings' lengths |
---|
134 | if (size1==size2) |
---|
135 | return 0; |
---|
136 | return (size1<size2) ? -1 : 1; |
---|
137 | } |
---|
138 | |
---|