vrshoot
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/assimp/StringComparison.h Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,219 @@ 1.4 +/* 1.5 +Open Asset Import Library (assimp) 1.6 +---------------------------------------------------------------------- 1.7 + 1.8 +Copyright (c) 2006-2012, assimp team 1.9 +All rights reserved. 1.10 + 1.11 +Redistribution and use of this software in source and binary forms, 1.12 +with or without modification, are permitted provided that the 1.13 +following conditions are met: 1.14 + 1.15 +* Redistributions of source code must retain the above 1.16 + copyright notice, this list of conditions and the 1.17 + following disclaimer. 1.18 + 1.19 +* Redistributions in binary form must reproduce the above 1.20 + copyright notice, this list of conditions and the 1.21 + following disclaimer in the documentation and/or other 1.22 + materials provided with the distribution. 1.23 + 1.24 +* Neither the name of the assimp team, nor the names of its 1.25 + contributors may be used to endorse or promote products 1.26 + derived from this software without specific prior 1.27 + written permission of the assimp team. 1.28 + 1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.40 + 1.41 +---------------------------------------------------------------------- 1.42 +*/ 1.43 + 1.44 +/** @file Definition of platform independent string workers: 1.45 + 1.46 + ASSIMP_itoa10 1.47 + ASSIMP_stricmp 1.48 + ASSIMP_strincmp 1.49 + 1.50 + These functions are not consistently available on all platforms, 1.51 + or the provided implementations behave too differently. 1.52 +*/ 1.53 +#ifndef INCLUDED_AI_STRING_WORKERS_H 1.54 +#define INCLUDED_AI_STRING_WORKERS_H 1.55 + 1.56 +#include "assimp/ai_assert.h" 1.57 + 1.58 +namespace Assimp { 1.59 + 1.60 +// ------------------------------------------------------------------------------- 1.61 +/** @brief itoa with a fixed base 10 1.62 + * 'itoa' is not consistently available on all platforms so it is quite useful 1.63 + * to have a small replacement function here. No need to use a full sprintf() 1.64 + * if we just want to print a number ... 1.65 + * @param out Output buffer 1.66 + * @param max Maximum number of characters to be written, including '\0'. 1.67 + * This parameter may not be 0. 1.68 + * @param number Number to be written 1.69 + * @return Length of the output string, excluding the '\0' 1.70 + */ 1.71 +inline unsigned int ASSIMP_itoa10( char* out, unsigned int max, int32_t number) 1.72 +{ 1.73 + ai_assert(NULL != out); 1.74 + 1.75 + // write the unary minus to indicate we have a negative number 1.76 + unsigned int written = 1u; 1.77 + if (number < 0 && written < max) { 1.78 + *out++ = '-'; 1.79 + ++written; 1.80 + number = -number; 1.81 + } 1.82 + 1.83 + // We begin with the largest number that is not zero. 1.84 + int32_t cur = 1000000000; // 2147483648 1.85 + bool mustPrint = false; 1.86 + while (written < max) { 1.87 + 1.88 + const unsigned int digit = number / cur; 1.89 + if (mustPrint || digit > 0 || 1 == cur) { 1.90 + // print all future zeroes from now 1.91 + mustPrint = true; 1.92 + 1.93 + *out++ = '0'+static_cast<char>(digit); 1.94 + 1.95 + ++written; 1.96 + number -= digit*cur; 1.97 + if (1 == cur) { 1.98 + break; 1.99 + } 1.100 + } 1.101 + cur /= 10; 1.102 + } 1.103 + 1.104 + // append a terminal zero 1.105 + *out++ = '\0'; 1.106 + return written-1; 1.107 +} 1.108 + 1.109 +// ------------------------------------------------------------------------------- 1.110 +/** @brief itoa with a fixed base 10 (Secure template overload) 1.111 + * The compiler should choose this function if he or she is able to determine the 1.112 + * size of the array automatically. 1.113 + */ 1.114 +template <size_t length> 1.115 +inline unsigned int ASSIMP_itoa10( char(& out)[length], int32_t number) 1.116 +{ 1.117 + return ASSIMP_itoa10(out,length,number); 1.118 +} 1.119 + 1.120 +// ------------------------------------------------------------------------------- 1.121 +/** @brief Helper function to do platform independent string comparison. 1.122 + * 1.123 + * This is required since stricmp() is not consistently available on 1.124 + * all platforms. Some platforms use the '_' prefix, others don't even 1.125 + * have such a function. 1.126 + * 1.127 + * @param s1 First input string 1.128 + * @param s2 Second input string 1.129 + * @return 0 if the given strings are identical 1.130 + */ 1.131 +inline int ASSIMP_stricmp(const char *s1, const char *s2) 1.132 +{ 1.133 + ai_assert(NULL != s1 && NULL != s2); 1.134 + 1.135 +#if (defined _MSC_VER) 1.136 + 1.137 + return ::_stricmp(s1,s2); 1.138 +#elif defined( __GNUC__ ) 1.139 + 1.140 + return ::strcasecmp(s1,s2); 1.141 +#else 1.142 + 1.143 + register char c1, c2; 1.144 + do { 1.145 + c1 = tolower(*s1++); 1.146 + c2 = tolower(*s2++); 1.147 + } 1.148 + while ( c1 && (c1 == c2) ); 1.149 + return c1 - c2; 1.150 +#endif 1.151 +} 1.152 + 1.153 +// ------------------------------------------------------------------------------- 1.154 +/** @brief Case independent comparison of two std::strings 1.155 + * 1.156 + * @param a First string 1.157 + * @param b Second string 1.158 + * @return 0 if a == b 1.159 + */ 1.160 +inline int ASSIMP_stricmp(const std::string& a, const std::string& b) 1.161 +{ 1.162 + register int i = (int)b.length()-(int)a.length(); 1.163 + return (i ? i : ASSIMP_stricmp(a.c_str(),b.c_str())); 1.164 +} 1.165 + 1.166 +// ------------------------------------------------------------------------------- 1.167 +/** @brief Helper function to do platform independent string comparison. 1.168 + * 1.169 + * This is required since strincmp() is not consistently available on 1.170 + * all platforms. Some platforms use the '_' prefix, others don't even 1.171 + * have such a function. 1.172 + * 1.173 + * @param s1 First input string 1.174 + * @param s2 Second input string 1.175 + * @param n Macimum number of characters to compare 1.176 + * @return 0 if the given strings are identical 1.177 + */ 1.178 +inline int ASSIMP_strincmp(const char *s1, const char *s2, unsigned int n) 1.179 +{ 1.180 + ai_assert(NULL != s1 && NULL != s2); 1.181 + if (!n)return 0; 1.182 + 1.183 +#if (defined _MSC_VER) 1.184 + 1.185 + return ::_strnicmp(s1,s2,n); 1.186 + 1.187 +#elif defined( __GNUC__ ) 1.188 + 1.189 + return ::strncasecmp(s1,s2, n); 1.190 + 1.191 +#else 1.192 + register char c1, c2; 1.193 + unsigned int p = 0; 1.194 + do 1.195 + { 1.196 + if (p++ >= n)return 0; 1.197 + c1 = tolower(*s1++); 1.198 + c2 = tolower(*s2++); 1.199 + } 1.200 + while ( c1 && (c1 == c2) ); 1.201 + 1.202 + return c1 - c2; 1.203 +#endif 1.204 +} 1.205 + 1.206 + 1.207 +// ------------------------------------------------------------------------------- 1.208 +/** @brief Evaluates an integer power 1.209 + * 1.210 + * todo: move somewhere where it fits better in than here 1.211 + */ 1.212 +inline unsigned int integer_pow (unsigned int base, unsigned int power) 1.213 +{ 1.214 + unsigned int res = 1; 1.215 + for (unsigned int i = 0; i < power;++i) 1.216 + res *= base; 1.217 + 1.218 + return res; 1.219 +} 1.220 +} // end of namespace 1.221 + 1.222 +#endif // ! AI_STRINGCOMPARISON_H_INC