![]() |
|
|||
File indexing completed on 2025-02-21 09:30:10
0001 /* 0002 Open Asset Import Library (assimp) 0003 ---------------------------------------------------------------------- 0004 0005 Copyright (c) 2006-2024, assimp team 0006 0007 0008 All rights reserved. 0009 0010 Redistribution and use of this software in source and binary forms, 0011 with or without modification, are permitted provided that the 0012 following conditions are met: 0013 0014 * Redistributions of source code must retain the above 0015 copyright notice, this list of conditions and the 0016 following disclaimer. 0017 0018 * Redistributions in binary form must reproduce the above 0019 copyright notice, this list of conditions and the 0020 following disclaimer in the documentation and/or other 0021 materials provided with the distribution. 0022 0023 * Neither the name of the assimp team, nor the names of its 0024 contributors may be used to endorse or promote products 0025 derived from this software without specific prior 0026 written permission of the assimp team. 0027 0028 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 0029 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 0030 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 0031 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 0032 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 0033 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 0034 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0035 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0036 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0037 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 0038 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0039 0040 ---------------------------------------------------------------------- 0041 */ 0042 0043 /** @file Definition of platform independent string workers: 0044 0045 ASSIMP_itoa10 0046 ASSIMP_stricmp 0047 ASSIMP_strincmp 0048 0049 These functions are not consistently available on all platforms, 0050 or the provided implementations behave too differently. 0051 */ 0052 #pragma once 0053 #ifndef INCLUDED_AI_STRING_WORKERS_H 0054 #define INCLUDED_AI_STRING_WORKERS_H 0055 0056 #ifdef __GNUC__ 0057 #pragma GCC system_header 0058 #endif 0059 0060 #include <assimp/ai_assert.h> 0061 #include <assimp/defs.h> 0062 0063 #include <cstdint> 0064 #include <cstring> 0065 #include <string> 0066 0067 namespace Assimp { 0068 0069 // ------------------------------------------------------------------------------- 0070 /** @brief itoa with a fixed base 10 0071 * 'itoa' is not consistently available on all platforms so it is quite useful 0072 * to have a small replacement function here. No need to use a full sprintf() 0073 * if we just want to print a number ... 0074 * @param out Output buffer 0075 * @param max Maximum number of characters to be written, including '\0'. 0076 * This parameter may not be 0. 0077 * @param number Number to be written 0078 * @return Length of the output string, excluding the '\0' 0079 */ 0080 inline unsigned int ASSIMP_itoa10(char *out, unsigned int max, int32_t number) { 0081 ai_assert(nullptr != out); 0082 0083 // write the unary minus to indicate we have a negative number 0084 unsigned int written = 1u; 0085 if (number < 0 && written < max) { 0086 *out++ = '-'; 0087 ++written; 0088 number = -number; 0089 } 0090 0091 // We begin with the largest number that is not zero. 0092 int32_t cur = 1000000000; // 2147483648 0093 bool mustPrint = false; 0094 while (written < max) { 0095 0096 const unsigned int digit = number / cur; 0097 if (mustPrint || digit > 0 || 1 == cur) { 0098 // print all future zero's from now 0099 mustPrint = true; 0100 0101 *out++ = '0' + static_cast<char>(digit); 0102 0103 ++written; 0104 number -= digit * cur; 0105 if (1 == cur) { 0106 break; 0107 } 0108 } 0109 cur /= 10; 0110 } 0111 0112 // append a terminal zero 0113 *out++ = '\0'; 0114 return written - 1; 0115 } 0116 0117 // ------------------------------------------------------------------------------- 0118 /** @brief itoa with a fixed base 10 (Secure template overload) 0119 * The compiler should choose this function if he or she is able to determine the 0120 * size of the array automatically. 0121 */ 0122 template <size_t length> 0123 inline unsigned int ASSIMP_itoa10(char (&out)[length], int32_t number) { 0124 return ASSIMP_itoa10(out, length, number); 0125 } 0126 0127 // ------------------------------------------------------------------------------- 0128 /** @brief Helper function to do platform independent string comparison. 0129 * 0130 * This is required since stricmp() is not consistently available on 0131 * all platforms. Some platforms use the '_' prefix, others don't even 0132 * have such a function. 0133 * 0134 * @param s1 First input string 0135 * @param s2 Second input string 0136 * @return 0 if the given strings are identical 0137 */ 0138 inline int ASSIMP_stricmp(const char *s1, const char *s2) { 0139 ai_assert(nullptr != s1); 0140 ai_assert(nullptr != s2); 0141 0142 #if (defined _MSC_VER) 0143 0144 return ::_stricmp(s1, s2); 0145 #else 0146 char c1, c2; 0147 do { 0148 c1 = tolower((unsigned char)*(s1++)); 0149 c2 = tolower((unsigned char)*(s2++)); 0150 } while (c1 && (c1 == c2)); 0151 return c1 - c2; 0152 #endif 0153 } 0154 0155 // ------------------------------------------------------------------------------- 0156 /** @brief Case independent comparison of two std::strings 0157 * 0158 * @param a First string 0159 * @param b Second string 0160 * @return 0 if a == b 0161 */ 0162 inline int ASSIMP_stricmp(const std::string &a, const std::string &b) { 0163 int i = (int)b.length() - (int)a.length(); 0164 return (i ? i : ASSIMP_stricmp(a.c_str(), b.c_str())); 0165 } 0166 0167 // ------------------------------------------------------------------------------- 0168 /** @brief Helper function to do platform independent string comparison. 0169 * 0170 * This is required since strincmp() is not consistently available on 0171 * all platforms. Some platforms use the '_' prefix, others don't even 0172 * have such a function. 0173 * 0174 * @param s1 First input string 0175 * @param s2 Second input string 0176 * @param n Maximum number of characters to compare 0177 * @return 0 if the given strings are identical 0178 */ 0179 inline int ASSIMP_strincmp(const char *s1, const char *s2, unsigned int n) { 0180 ai_assert(nullptr != s1); 0181 ai_assert(nullptr != s2); 0182 if (!n) { 0183 return 0; 0184 } 0185 0186 #if (defined _MSC_VER) 0187 0188 return ::_strnicmp(s1, s2, n); 0189 0190 #elif defined(__GNUC__) 0191 0192 return ::strncasecmp(s1, s2, n); 0193 0194 #else 0195 char c1, c2; 0196 unsigned int p = 0; 0197 do { 0198 if (p++ >= n) return 0; 0199 c1 = tolower((unsigned char)*(s1++)); 0200 c2 = tolower((unsigned char)*(s2++)); 0201 } while (c1 && (c1 == c2)); 0202 0203 return c1 - c2; 0204 #endif 0205 } 0206 0207 // ------------------------------------------------------------------------------- 0208 /** @brief Evaluates an integer power 0209 * 0210 * todo: move somewhere where it fits better in than here 0211 */ 0212 inline unsigned int integer_pow(unsigned int base, unsigned int power) { 0213 unsigned int res = 1; 0214 for (unsigned int i = 0; i < power; ++i) { 0215 res *= base; 0216 } 0217 0218 return res; 0219 } 0220 0221 } // namespace Assimp 0222 0223 #endif // ! AI_STRINGCOMPARISON_H_INC
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |