OgreParsingUtils.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2015, 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. // Modified by Lasse Oorni for Urho3D
  34. #ifndef AI_OGREPARSINGUTILS_H_INC
  35. #define AI_OGREPARSINGUTILS_H_INC
  36. #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
  37. #include "ParsingUtils.h"
  38. #include <functional>
  39. #include <algorithm>
  40. // Urho3D: VS2008 compatibility
  41. #if !defined(_MSC_VER) || (_MSC_VER >= 1600)
  42. #include <stdint.h>
  43. #else
  44. #include "../include/assimp/Compiler/pstdint.h"
  45. #endif
  46. #include <sstream>
  47. #include <cctype>
  48. namespace Assimp
  49. {
  50. namespace Ogre
  51. {
  52. /// Returns a lower cased copy of @s.
  53. static inline std::string ToLower(std::string s)
  54. {
  55. std::transform(s.begin(), s.end(), s.begin(), ::tolower);
  56. return s;
  57. }
  58. /// Returns if @c s ends with @c suffix. If @c caseSensitive is false, both strings will be lower cased before matching.
  59. static inline bool EndsWith(const std::string &s, const std::string &suffix, bool caseSensitive = true)
  60. {
  61. if (s.empty() || suffix.empty())
  62. {
  63. return false;
  64. }
  65. else if (s.length() < suffix.length())
  66. {
  67. return false;
  68. }
  69. if (!caseSensitive) {
  70. return EndsWith(ToLower(s), ToLower(suffix), true);
  71. }
  72. size_t len = suffix.length();
  73. std::string sSuffix = s.substr(s.length()-len, len);
  74. return (ASSIMP_stricmp(sSuffix, suffix) == 0);
  75. }
  76. // Below trim functions adapted from http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
  77. /// Trim from start
  78. static inline std::string &TrimLeft(std::string &s, bool newlines = true)
  79. {
  80. if (!newlines)
  81. {
  82. s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(Assimp::IsSpace<char>))));
  83. }
  84. else
  85. {
  86. s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(Assimp::IsSpaceOrNewLine<char>))));
  87. }
  88. return s;
  89. }
  90. /// Trim from end
  91. static inline std::string &TrimRight(std::string &s, bool newlines = true)
  92. {
  93. if (!newlines)
  94. {
  95. s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun(Assimp::IsSpace<char>))).base(),s.end());
  96. }
  97. else
  98. {
  99. s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun(Assimp::IsSpaceOrNewLine<char>))));
  100. }
  101. return s;
  102. }
  103. /// Trim from both ends
  104. static inline std::string &Trim(std::string &s, bool newlines = true)
  105. {
  106. return TrimLeft(TrimRight(s, newlines), newlines);
  107. }
  108. /// Skips a line from current @ss position until a newline. Returns the skipped part.
  109. static inline std::string SkipLine(std::stringstream &ss)
  110. {
  111. std::string skipped;
  112. getline(ss, skipped);
  113. return skipped;
  114. }
  115. /// Skips a line and reads next element from @c ss to @c nextElement.
  116. /** @return Skipped line content until newline. */
  117. static inline std::string NextAfterNewLine(std::stringstream &ss, std::string &nextElement)
  118. {
  119. std::string skipped = SkipLine(ss);
  120. ss >> nextElement;
  121. return skipped;
  122. }
  123. } // Ogre
  124. } // Assimp
  125. #endif // ASSIMP_BUILD_NO_OGRE_IMPORTER
  126. #endif // AI_OGREPARSINGUTILS_H_INC