Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-31 08:01:54

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