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