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