OgreParsingUtils.h 4.5 KB

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