StringUtils.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. #pragma once
  34. #ifndef INCLUDED_AI_STRINGUTILS_H
  35. #define INCLUDED_AI_STRINGUTILS_H
  36. #ifdef __GNUC__
  37. # pragma GCC system_header
  38. #endif
  39. #include <assimp/defs.h>
  40. #include <sstream>
  41. #include <stdarg.h>
  42. #include <cstdlib>
  43. #ifdef _MSC_VER
  44. # define AI_SIZEFMT "%Iu"
  45. #else
  46. # define AI_SIZEFMT "%zu"
  47. #endif
  48. /// @fn ai_snprintf
  49. /// @brief The portable version of the function snprintf ( C99 standard ), which works on visual studio compilers 2013 and earlier.
  50. /// @param outBuf The buffer to write in
  51. /// @param size The buffer size
  52. /// @param format The format string
  53. /// @param ap The additional arguments.
  54. /// @return The number of written characters if the buffer size was big enough. If an encoding error occurs, a negative number is returned.
  55. #if defined(_MSC_VER) && _MSC_VER < 1900
  56. AI_FORCE_INLINE
  57. int c99_ai_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) {
  58. int count(-1);
  59. if (0 != size) {
  60. count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
  61. }
  62. if (count == -1) {
  63. count = _vscprintf(format, ap);
  64. }
  65. return count;
  66. }
  67. AI_FORCE_INLINE
  68. int ai_snprintf(char *outBuf, size_t size, const char *format, ...) {
  69. int count;
  70. va_list ap;
  71. va_start(ap, format);
  72. count = c99_ai_vsnprintf(outBuf, size, format, ap);
  73. va_end(ap);
  74. return count;
  75. }
  76. #elif defined(__MINGW32__)
  77. # define ai_snprintf __mingw_snprintf
  78. #else
  79. # define ai_snprintf snprintf
  80. #endif
  81. /// @fn to_string
  82. /// @brief The portable version of to_string ( some gcc-versions on embedded devices are not supporting this).
  83. /// @param value The value to write into the std::string.
  84. /// @return The value as a std::string
  85. template <typename T>
  86. AI_FORCE_INLINE
  87. std::string to_string( T value ) {
  88. std::ostringstream os;
  89. os << value;
  90. return os.str();
  91. }
  92. /// @fn ai_strtof
  93. /// @brief The portable version of strtof.
  94. /// @param begin The first character of the string.
  95. /// @param end The last character
  96. /// @return The float value, 0.0f in cas of an error.
  97. AI_FORCE_INLINE
  98. float ai_strtof( const char *begin, const char *end ) {
  99. if ( nullptr == begin ) {
  100. return 0.0f;
  101. }
  102. float val( 0.0f );
  103. if ( nullptr == end ) {
  104. val = static_cast< float >( ::atof( begin ) );
  105. } else {
  106. std::string::size_type len( end - begin );
  107. std::string token( begin, len );
  108. val = static_cast< float >( ::atof( token.c_str() ) );
  109. }
  110. return val;
  111. }
  112. /// @fn DecimalToHexa
  113. /// @brief The portable to convert a decimal value into a hexadecimal string.
  114. /// @param toConvert Value to convert
  115. /// @return The hexadecimal string, is empty in case of an error.
  116. template<class T>
  117. AI_FORCE_INLINE
  118. std::string DecimalToHexa( T toConvert ) {
  119. std::string result;
  120. std::stringstream ss;
  121. ss << std::hex << toConvert;
  122. ss >> result;
  123. for ( size_t i = 0; i < result.size(); ++i ) {
  124. result[ i ] = (char) toupper( result[ i ] );
  125. }
  126. return result;
  127. }
  128. /// @brief translate RGBA to String
  129. /// @param r aiColor.r
  130. /// @param g aiColor.g
  131. /// @param b aiColor.b
  132. /// @param a aiColor.a
  133. /// @param with_head #
  134. /// @return The hexadecimal string, is empty in case of an error.
  135. AI_FORCE_INLINE std::string Rgba2Hex(int r, int g, int b, int a, bool with_head) {
  136. std::stringstream ss;
  137. if (with_head) {
  138. ss << "#";
  139. }
  140. ss << std::hex << (r << 24 | g << 16 | b << 8 | a);
  141. return ss.str();
  142. }
  143. #endif // INCLUDED_AI_STRINGUTILS_H