OgreParsingUtils.h 4.4 KB

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