StringUtils.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /// @fn ai_snprintf
  44. /// @brief The portable version of the function snprintf ( C99 standard ), which works on visual studio compilers 2013 and earlier.
  45. /// @param outBuf The buffer to write in
  46. /// @param size The buffer size
  47. /// @param format The format string
  48. /// @param ap The additional arguments.
  49. /// @return The number of written characters if the buffer size was big enough. If an encoding error occurs, a negative number is returned.
  50. #if defined(_MSC_VER) && _MSC_VER < 1900
  51. AI_FORCE_INLINE
  52. int c99_ai_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) {
  53. int count(-1);
  54. if (0 != size) {
  55. count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
  56. }
  57. if (count == -1) {
  58. count = _vscprintf(format, ap);
  59. }
  60. return count;
  61. }
  62. AI_FORCE_INLINE
  63. int ai_snprintf(char *outBuf, size_t size, const char *format, ...) {
  64. int count;
  65. va_list ap;
  66. va_start(ap, format);
  67. count = c99_ai_vsnprintf(outBuf, size, format, ap);
  68. va_end(ap);
  69. return count;
  70. }
  71. #else
  72. # define ai_snprintf snprintf
  73. #endif
  74. /// @fn to_string
  75. /// @brief The portable version of to_string ( some gcc-versions on embedded devices are not supporting this).
  76. /// @param value The value to write into the std::string.
  77. /// @return The value as a std::string
  78. template <typename T>
  79. AI_FORCE_INLINE
  80. std::string to_string( T value ) {
  81. std::ostringstream os;
  82. os << value;
  83. return os.str();
  84. }
  85. /// @fn ai_strtof
  86. /// @brief The portable version of strtof.
  87. /// @param begin The first character of the string.
  88. /// @param end The last character
  89. /// @return The float value, 0.0f in cas of an error.
  90. AI_FORCE_INLINE
  91. float ai_strtof( const char *begin, const char *end ) {
  92. if ( nullptr == begin ) {
  93. return 0.0f;
  94. }
  95. float val( 0.0f );
  96. if ( nullptr == end ) {
  97. val = static_cast< float >( ::atof( begin ) );
  98. } else {
  99. std::string::size_type len( end - begin );
  100. std::string token( begin, len );
  101. val = static_cast< float >( ::atof( token.c_str() ) );
  102. }
  103. return val;
  104. }
  105. /// @fn DecimalToHexa
  106. /// @brief The portable to convert a decimal value into a hexadecimal string.
  107. /// @param toConvert Value to convert
  108. /// @return The hexadecimal string, is empty in case of an error.
  109. template<class T>
  110. AI_FORCE_INLINE
  111. std::string DecimalToHexa( T toConvert ) {
  112. std::string result;
  113. std::stringstream ss;
  114. ss << std::hex << toConvert;
  115. ss >> result;
  116. for ( size_t i = 0; i < result.size(); ++i ) {
  117. result[ i ] = (char) toupper( result[ i ] );
  118. }
  119. return result;
  120. }
  121. /// @brief translate RGBA to String
  122. /// @param r aiColor.r
  123. /// @param g aiColor.g
  124. /// @param b aiColor.b
  125. /// @param a aiColor.a
  126. /// @param with_head #
  127. /// @return The hexadecimal string, is empty in case of an error.
  128. AI_FORCE_INLINE std::string Rgba2Hex(int r, int g, int b, int a, bool with_head) {
  129. std::stringstream ss;
  130. if (with_head) {
  131. ss << "#";
  132. }
  133. ss << std::hex << (r << 24 | g << 16 | b << 8 | a);
  134. return ss.str();
  135. }
  136. #endif // INCLUDED_AI_STRINGUTILS_H