StringUtils.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2019, 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. #ifndef INCLUDED_AI_STRINGUTILS_H
  34. #define INCLUDED_AI_STRINGUTILS_H
  35. #include <assimp/defs.h>
  36. #include <sstream>
  37. #include <stdarg.h>
  38. #include <cstdlib>
  39. /// @fn ai_snprintf
  40. /// @brief The portable version of the function snprintf ( C99 standard ), which works on visual studio compilers 2013 and earlier.
  41. /// @param outBuf The buffer to write in
  42. /// @param size The buffer size
  43. /// @param format The format string
  44. /// @param ap The additional arguments.
  45. /// @return The number of written characters if the buffer size was big enough. If an encoding error occurs, a negative number is returned.
  46. #if defined(_MSC_VER) && _MSC_VER < 1900
  47. AI_FORCE_INLINE
  48. int c99_ai_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) {
  49. int count(-1);
  50. if (0 != size) {
  51. count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
  52. }
  53. if (count == -1) {
  54. count = _vscprintf(format, ap);
  55. }
  56. return count;
  57. }
  58. AI_FORCE_INLINE
  59. int ai_snprintf(char *outBuf, size_t size, const char *format, ...) {
  60. int count;
  61. va_list ap;
  62. va_start(ap, format);
  63. count = c99_ai_vsnprintf(outBuf, size, format, ap);
  64. va_end(ap);
  65. return count;
  66. }
  67. #else
  68. # define ai_snprintf snprintf
  69. #endif
  70. /// @fn to_string
  71. /// @brief The portable version of to_string ( some gcc-versions on embedded devices are not supporting this).
  72. /// @param value The value to write into the std::string.
  73. /// @return The value as a std::string
  74. template <typename T>
  75. AI_FORCE_INLINE
  76. std::string to_string( T value ) {
  77. std::ostringstream os;
  78. os << value;
  79. return os.str();
  80. }
  81. /// @fn ai_strtof
  82. /// @brief The portable version of strtof.
  83. /// @param begin The first character of the string.
  84. /// @param end The last character
  85. /// @return The float value, 0.0f in cas of an error.
  86. AI_FORCE_INLINE
  87. float ai_strtof( const char *begin, const char *end ) {
  88. if ( nullptr == begin ) {
  89. return 0.0f;
  90. }
  91. float val( 0.0f );
  92. if ( nullptr == end ) {
  93. val = static_cast< float >( ::atof( begin ) );
  94. } else {
  95. std::string::size_type len( end - begin );
  96. std::string token( begin, len );
  97. val = static_cast< float >( ::atof( token.c_str() ) );
  98. }
  99. return val;
  100. }
  101. /// @fn DecimalToHexa
  102. /// @brief The portable to convert a decimal value into a hexadecimal string.
  103. /// @param toConvert Value to convert
  104. /// @return The hexadecimal string, is empty in case of an error.
  105. template<class T>
  106. AI_FORCE_INLINE
  107. std::string DecimalToHexa( T toConvert ) {
  108. std::string result;
  109. std::stringstream ss;
  110. ss << std::hex << toConvert;
  111. ss >> result;
  112. for ( size_t i = 0; i < result.size(); ++i ) {
  113. result[ i ] = toupper( result[ i ] );
  114. }
  115. return result;
  116. }
  117. #endif // INCLUDED_AI_STRINGUTILS_H