Back to home page

EIC code displayed by LXR

 
 

    


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 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 #pragma once
0042 #ifndef INCLUDED_AI_STRINGUTILS_H
0043 #define INCLUDED_AI_STRINGUTILS_H
0044 
0045 #ifdef __GNUC__
0046 #pragma GCC system_header
0047 #endif
0048 
0049 #include <assimp/defs.h>
0050 
0051 #include <cstdarg>
0052 #include <algorithm>
0053 #include <cctype>
0054 #include <cstdlib>
0055 #include <locale>
0056 #include <sstream>
0057 #include <iomanip>
0058 
0059 #if defined(_MSC_VER) && !defined(__clang__)
0060 #  define AI_SIZEFMT "%Iu"
0061 #else
0062 #  define AI_SIZEFMT "%zu"
0063 #endif
0064 
0065 // ---------------------------------------------------------------------------------
0066 /// @fn     ai_snprintf
0067 /// @brief  The portable version of the function snprintf ( C99 standard ), which
0068 ///         works on visual studio compilers 2013 and earlier.
0069 /// @param  outBuf      The buffer to write in
0070 /// @param  size        The buffer size
0071 /// @param  format      The format string
0072 /// @param  ap          The additional arguments.
0073 /// @return The number of written characters if the buffer size was big enough.
0074 ///         If an encoding error occurs, a negative number is returned.
0075 // ---------------------------------------------------------------------------------
0076 #if defined(_MSC_VER) && _MSC_VER < 1900
0077 
0078 inline int c99_ai_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) {
0079     int count(-1);
0080     if (0 != size) {
0081         count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
0082     }
0083     if (count == -1) {
0084         count = _vscprintf(format, ap);
0085     }
0086 
0087     return count;
0088 }
0089 
0090 inline int ai_snprintf(char *outBuf, size_t size, const char *format, ...) {
0091     int count;
0092     va_list ap;
0093 
0094     va_start(ap, format);
0095     count = c99_ai_vsnprintf(outBuf, size, format, ap);
0096     va_end(ap);
0097 
0098     return count;
0099 }
0100 
0101 #elif defined(__MINGW32__)
0102 #  define ai_snprintf __mingw_snprintf
0103 #else
0104 #  define ai_snprintf snprintf
0105 #endif
0106 
0107 // ---------------------------------------------------------------------------------
0108 /// @fn     to_string
0109 /// @brief  The portable version of to_string ( some gcc-versions on embedded
0110 ///         devices are not supporting this).
0111 /// @param  value   The value to write into the std::string.
0112 /// @return The value as a std::string
0113 // ---------------------------------------------------------------------------------
0114 template <typename T>
0115 AI_FORCE_INLINE std::string ai_to_string(T value) {
0116     std::ostringstream os;
0117     os << value;
0118 
0119     return os.str();
0120 }
0121 
0122 // ---------------------------------------------------------------------------------
0123 /// @fn     ai_strtof
0124 /// @brief  The portable version of strtof.
0125 /// @param  begin   The first character of the string.
0126 /// @param  end     The last character
0127 /// @return The float value, 0.0f in case of an error.
0128 // ---------------------------------------------------------------------------------
0129 AI_FORCE_INLINE
0130 float ai_strtof(const char *begin, const char *end) {
0131     if (nullptr == begin) {
0132         return 0.0f;
0133     }
0134     float val(0.0f);
0135     if (nullptr == end) {
0136         val = static_cast<float>(::atof(begin));
0137     } else {
0138         std::string::size_type len(end - begin);
0139         std::string token(begin, len);
0140         val = static_cast<float>(::atof(token.c_str()));
0141     }
0142 
0143     return val;
0144 }
0145 
0146 // ---------------------------------------------------------------------------------
0147 /// @fn     DecimalToHexa
0148 /// @brief  The portable to convert a decimal value into a hexadecimal string.
0149 /// @param  toConvert   Value to convert
0150 /// @return The hexadecimal string, is empty in case of an error.
0151 // ---------------------------------------------------------------------------------
0152 template <class T>
0153 AI_FORCE_INLINE std::string ai_decimal_to_hexa(T toConvert) {
0154     std::string result;
0155     std::stringstream ss;
0156     ss << std::hex << toConvert;
0157     ss >> result;
0158 
0159     for (size_t i = 0; i < result.size(); ++i) {
0160         result[i] = (char)toupper((unsigned char)result[i]);
0161     }
0162 
0163     return result;
0164 }
0165 
0166 // ---------------------------------------------------------------------------------
0167 /// @brief  translate RGBA to String
0168 /// @param  r   aiColor.r
0169 /// @param  g   aiColor.g
0170 /// @param  b   aiColor.b
0171 /// @param  a   aiColor.a
0172 /// @param  with_head   #
0173 /// @return The hexadecimal string, is empty in case of an error.
0174 // ---------------------------------------------------------------------------------
0175 AI_FORCE_INLINE std::string ai_rgba2hex(int r, int g, int b, int a, bool with_head) {
0176     std::stringstream ss;
0177     if (with_head) {
0178         ss << "#";
0179     }
0180     ss << std::hex << std::setfill('0') << std::setw(8) << (r << 24 | g << 16 | b << 8 | a);
0181 
0182     return ss.str();
0183 }
0184 
0185 // ---------------------------------------------------------------------------------
0186 /// @brief   Performs a trim from start (in place)
0187 /// @param  s   string to trim.
0188 // ---------------------------------------------------------------------------------
0189 AI_FORCE_INLINE void ai_trim_left(std::string &s) {
0190     s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
0191         return !std::isspace(ch);
0192     }));
0193 }
0194 
0195 // ---------------------------------------------------------------------------------
0196 /// @brief  Performs a trim from end (in place).
0197 /// @param  s   string to trim.
0198 // ---------------------------------------------------------------------------------
0199 AI_FORCE_INLINE void ai_trim_right(std::string &s) {
0200     s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
0201         return !std::isspace(ch);
0202     }).base(), s.end());
0203 }
0204 
0205 // ---------------------------------------------------------------------------------
0206 /// @brief  Performs a trim from both ends (in place).
0207 /// @param  s   string to trim.
0208 // ---------------------------------------------------------------------------------
0209 AI_FORCE_INLINE std::string ai_trim(std::string &s) {
0210     std::string out(s);
0211     ai_trim_left(out);
0212     ai_trim_right(out);
0213 
0214     return out;
0215 }
0216 
0217 // ---------------------------------------------------------------------------------
0218 /// @brief Performs a to lower operation onto on single character.
0219 /// @param in  The character
0220 /// @return the character as lower-case.
0221 // ---------------------------------------------------------------------------------
0222 template <class char_t>
0223 AI_FORCE_INLINE char_t ai_tolower(char_t in) {
0224     return (in >= (char_t)'A' && in <= (char_t)'Z') ? (char_t)(in + 0x20) : in;
0225 }
0226 
0227 // ---------------------------------------------------------------------------------
0228 /// @brief  Performs a ToLower-operation and return the lower-case string.
0229 /// @param  in  The incoming string.
0230 /// @return The string as lowercase.
0231 // ---------------------------------------------------------------------------------
0232 AI_FORCE_INLINE std::string ai_tolower(const std::string &in) {
0233     std::string out(in);
0234     ai_trim_left(out);
0235     ai_trim_right(out);
0236     std::transform(out.begin(), out.end(), out.begin(), [](unsigned char c) { return ai_tolower(c); });
0237     return out;
0238 }
0239 
0240 // ---------------------------------------------------------------------------------
0241 /// @brief Performs a to upper operation onto on single character.
0242 /// @param in  The character
0243 /// @return the character as upper-case.
0244 // ---------------------------------------------------------------------------------
0245 template <class char_t>
0246 AI_FORCE_INLINE char_t ai_toupper(char_t in) {
0247     return (in >= (char_t)'a' && in <= (char_t)'z') ? (char_t)(in - 0x20) : in;
0248 }
0249 
0250 // ---------------------------------------------------------------------------------
0251 /// @brief  Performs a ToLower-operation and return the upper-case string.
0252 /// @param  in  The incoming string.
0253 /// @return The string as uppercase.
0254 // ---------------------------------------------------------------------------------
0255 AI_FORCE_INLINE std::string ai_str_toupper(const std::string &in) {
0256     std::string out(in);
0257     std::transform(out.begin(), out.end(), out.begin(), [](char c) { return ai_toupper(c); });
0258     return out;
0259 }
0260 
0261 // ---------------------------------------------------------------------------------
0262 /// @brief  Make a string printable by replacing all non-printable characters with
0263 ///         the specified placeholder character.
0264 /// @param  in  The incoming string.
0265 /// @param  placeholder  Placeholder character, default is a question mark.
0266 /// @return The string, with all non-printable characters replaced.
0267 // ---------------------------------------------------------------------------------
0268 AI_FORCE_INLINE std::string ai_str_toprintable(const std::string &in, char placeholder = '?') {
0269     std::string out(in);
0270     std::transform(out.begin(), out.end(), out.begin(), [placeholder] (unsigned char c) {
0271         return isprint(c) ? (char)c :  placeholder;
0272     });
0273     return out;
0274 }
0275 
0276 // ---------------------------------------------------------------------------------
0277 /// @brief  Make a string printable by replacing all non-printable characters with
0278 ///         the specified placeholder character.
0279 /// @param  in  The incoming string.
0280 /// @param  len The length of the incoming string.
0281 /// @param  placeholder  Placeholder character, default is a question mark.
0282 /// @return The string, with all non-printable characters replaced. Will return an
0283 ///         empty string if in is null or len is <= 0.
0284 // ---------------------------------------------------------------------------------
0285 AI_FORCE_INLINE std::string ai_str_toprintable(const char *in, int len, char placeholder = '?') {
0286     return (in && len > 0) ? ai_str_toprintable(std::string(in, len), placeholder) : std::string();
0287 }
0288 
0289 #endif // INCLUDED_AI_STRINGUTILS_H