vrshoot

view libs/assimp/StringComparison.h @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 /** @file Definition of platform independent string workers:
43 ASSIMP_itoa10
44 ASSIMP_stricmp
45 ASSIMP_strincmp
47 These functions are not consistently available on all platforms,
48 or the provided implementations behave too differently.
49 */
50 #ifndef INCLUDED_AI_STRING_WORKERS_H
51 #define INCLUDED_AI_STRING_WORKERS_H
53 #include "assimp/ai_assert.h"
55 namespace Assimp {
57 // -------------------------------------------------------------------------------
58 /** @brief itoa with a fixed base 10
59 * 'itoa' is not consistently available on all platforms so it is quite useful
60 * to have a small replacement function here. No need to use a full sprintf()
61 * if we just want to print a number ...
62 * @param out Output buffer
63 * @param max Maximum number of characters to be written, including '\0'.
64 * This parameter may not be 0.
65 * @param number Number to be written
66 * @return Length of the output string, excluding the '\0'
67 */
68 inline unsigned int ASSIMP_itoa10( char* out, unsigned int max, int32_t number)
69 {
70 ai_assert(NULL != out);
72 // write the unary minus to indicate we have a negative number
73 unsigned int written = 1u;
74 if (number < 0 && written < max) {
75 *out++ = '-';
76 ++written;
77 number = -number;
78 }
80 // We begin with the largest number that is not zero.
81 int32_t cur = 1000000000; // 2147483648
82 bool mustPrint = false;
83 while (written < max) {
85 const unsigned int digit = number / cur;
86 if (mustPrint || digit > 0 || 1 == cur) {
87 // print all future zeroes from now
88 mustPrint = true;
90 *out++ = '0'+static_cast<char>(digit);
92 ++written;
93 number -= digit*cur;
94 if (1 == cur) {
95 break;
96 }
97 }
98 cur /= 10;
99 }
101 // append a terminal zero
102 *out++ = '\0';
103 return written-1;
104 }
106 // -------------------------------------------------------------------------------
107 /** @brief itoa with a fixed base 10 (Secure template overload)
108 * The compiler should choose this function if he or she is able to determine the
109 * size of the array automatically.
110 */
111 template <size_t length>
112 inline unsigned int ASSIMP_itoa10( char(& out)[length], int32_t number)
113 {
114 return ASSIMP_itoa10(out,length,number);
115 }
117 // -------------------------------------------------------------------------------
118 /** @brief Helper function to do platform independent string comparison.
119 *
120 * This is required since stricmp() is not consistently available on
121 * all platforms. Some platforms use the '_' prefix, others don't even
122 * have such a function.
123 *
124 * @param s1 First input string
125 * @param s2 Second input string
126 * @return 0 if the given strings are identical
127 */
128 inline int ASSIMP_stricmp(const char *s1, const char *s2)
129 {
130 ai_assert(NULL != s1 && NULL != s2);
132 #if (defined _MSC_VER)
134 return ::_stricmp(s1,s2);
135 #elif defined( __GNUC__ )
137 return ::strcasecmp(s1,s2);
138 #else
140 register char c1, c2;
141 do {
142 c1 = tolower(*s1++);
143 c2 = tolower(*s2++);
144 }
145 while ( c1 && (c1 == c2) );
146 return c1 - c2;
147 #endif
148 }
150 // -------------------------------------------------------------------------------
151 /** @brief Case independent comparison of two std::strings
152 *
153 * @param a First string
154 * @param b Second string
155 * @return 0 if a == b
156 */
157 inline int ASSIMP_stricmp(const std::string& a, const std::string& b)
158 {
159 register int i = (int)b.length()-(int)a.length();
160 return (i ? i : ASSIMP_stricmp(a.c_str(),b.c_str()));
161 }
163 // -------------------------------------------------------------------------------
164 /** @brief Helper function to do platform independent string comparison.
165 *
166 * This is required since strincmp() is not consistently available on
167 * all platforms. Some platforms use the '_' prefix, others don't even
168 * have such a function.
169 *
170 * @param s1 First input string
171 * @param s2 Second input string
172 * @param n Macimum number of characters to compare
173 * @return 0 if the given strings are identical
174 */
175 inline int ASSIMP_strincmp(const char *s1, const char *s2, unsigned int n)
176 {
177 ai_assert(NULL != s1 && NULL != s2);
178 if (!n)return 0;
180 #if (defined _MSC_VER)
182 return ::_strnicmp(s1,s2,n);
184 #elif defined( __GNUC__ )
186 return ::strncasecmp(s1,s2, n);
188 #else
189 register char c1, c2;
190 unsigned int p = 0;
191 do
192 {
193 if (p++ >= n)return 0;
194 c1 = tolower(*s1++);
195 c2 = tolower(*s2++);
196 }
197 while ( c1 && (c1 == c2) );
199 return c1 - c2;
200 #endif
201 }
204 // -------------------------------------------------------------------------------
205 /** @brief Evaluates an integer power
206 *
207 * todo: move somewhere where it fits better in than here
208 */
209 inline unsigned int integer_pow (unsigned int base, unsigned int power)
210 {
211 unsigned int res = 1;
212 for (unsigned int i = 0; i < power;++i)
213 res *= base;
215 return res;
216 }
217 } // end of namespace
219 #endif // ! AI_STRINGCOMPARISON_H_INC