2
0

ObjTools.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. /** @file ObjTools.h
  34. * @brief Some helpful templates for text parsing
  35. */
  36. #ifndef OBJ_TOOLS_H_INC
  37. #define OBJ_TOOLS_H_INC
  38. #include <assimp/ParsingUtils.h>
  39. #include <assimp/fast_atof.h>
  40. #include <vector>
  41. namespace Assimp {
  42. /**
  43. * @brief Returns true, if the last entry of the buffer is reached.
  44. * @param[in] it Iterator of current position.
  45. * @param[in] end Iterator with end of buffer.
  46. * @return true, if the end of the buffer is reached.
  47. */
  48. template <class char_t>
  49. inline bool isEndOfBuffer(char_t it, char_t end) {
  50. if (it == end) {
  51. return true;
  52. }
  53. --end;
  54. return (it == end);
  55. }
  56. /**
  57. * @brief Returns next word separated by a space
  58. * @param[in] pBuffer Pointer to data buffer
  59. * @param[in] pEnd Pointer to end of buffer
  60. * @return Pointer to next space
  61. */
  62. template <class Char_T>
  63. inline Char_T getNextWord(Char_T pBuffer, Char_T pEnd) {
  64. while (!isEndOfBuffer(pBuffer, pEnd)) {
  65. if (!IsSpaceOrNewLine(*pBuffer) || IsLineEnd(*pBuffer)) {
  66. break;
  67. }
  68. ++pBuffer;
  69. }
  70. return pBuffer;
  71. }
  72. /**
  73. * @brief Returns pointer a next token
  74. * @param[in] pBuffer Pointer to data buffer
  75. * @param[in] pEnd Pointer to end of buffer
  76. * @return Pointer to next token
  77. */
  78. template <class Char_T>
  79. inline Char_T getNextToken(Char_T pBuffer, Char_T pEnd) {
  80. while (!isEndOfBuffer(pBuffer, pEnd)) {
  81. if (IsSpaceOrNewLine(*pBuffer)) {
  82. break;
  83. }
  84. ++pBuffer;
  85. }
  86. return getNextWord(pBuffer, pEnd);
  87. }
  88. /**
  89. * @brief Skips a line
  90. * @param[in] it Iterator set to current position
  91. * @param[in] end Iterator set to end of scratch buffer for readout
  92. * @param[out] uiLine Current line number in format
  93. * @return Current-iterator with new position
  94. */
  95. template <class char_t>
  96. inline char_t skipLine(char_t it, char_t end, unsigned int &uiLine) {
  97. if (it >= end) {
  98. return it;
  99. }
  100. while (!isEndOfBuffer(it, end) && !IsLineEnd(*it)) {
  101. ++it;
  102. }
  103. if (it != end) {
  104. ++it;
  105. ++uiLine;
  106. }
  107. // fix .. from time to time there are spaces at the beginning of a material line
  108. while (it != end && (*it == '\t' || *it == ' ')) {
  109. ++it;
  110. }
  111. return it;
  112. }
  113. /**
  114. * @brief Get a name from the current line. Preserve space in the middle,
  115. * but trim it at the end.
  116. * @param[in] it set to current position
  117. * @param[in] end set to end of scratch buffer for readout
  118. * @param[out] name Separated name
  119. * @return Current-iterator with new position
  120. */
  121. template <class char_t>
  122. inline char_t getName(char_t it, char_t end, std::string &name) {
  123. name = "";
  124. if (isEndOfBuffer(it, end)) {
  125. return end;
  126. }
  127. char *pStart = &(*it);
  128. while (!isEndOfBuffer(it, end) && !IsLineEnd(*it)) {
  129. ++it;
  130. }
  131. while (IsSpace(*it)) {
  132. --it;
  133. }
  134. // Get name
  135. // if there is no name, and the previous char is a separator, come back to start
  136. while (&(*it) < pStart) {
  137. ++it;
  138. }
  139. std::string strName(pStart, &(*it));
  140. if (!strName.empty()) {
  141. name = strName;
  142. }
  143. return it;
  144. }
  145. /**
  146. * @brief Get a name from the current line. Do not preserve space
  147. * in the middle, but trim it at the end.
  148. * @param it set to current position
  149. * @param end set to end of scratch buffer for readout
  150. * @param name Separated name
  151. * @return Current-iterator with new position
  152. */
  153. template <class char_t>
  154. inline char_t getNameNoSpace(char_t it, char_t end, std::string &name) {
  155. name = "";
  156. if (isEndOfBuffer(it, end)) {
  157. return end;
  158. }
  159. char *pStart = &(*it);
  160. while (!isEndOfBuffer(it, end) && !IsLineEnd(*it) && !IsSpaceOrNewLine(*it)) {
  161. ++it;
  162. }
  163. while (isEndOfBuffer(it, end) || IsLineEnd(*it) || IsSpaceOrNewLine(*it)) {
  164. --it;
  165. }
  166. ++it;
  167. // Get name
  168. // if there is no name, and the previous char is a separator, come back to start
  169. while (&(*it) < pStart) {
  170. ++it;
  171. }
  172. std::string strName(pStart, &(*it));
  173. if (!strName.empty()) {
  174. name = strName;
  175. }
  176. return it;
  177. }
  178. /**
  179. * @brief Get next word from given line
  180. * @param[in] it set to current position
  181. * @param[in] end set to end of scratch buffer for readout
  182. * @param[in] pBuffer Buffer for next word
  183. * @param[in] length Buffer length
  184. * @return Current-iterator with new position
  185. */
  186. template <class char_t>
  187. inline char_t CopyNextWord(char_t it, char_t end, char *pBuffer, size_t length) {
  188. size_t index = 0;
  189. it = getNextWord<char_t>(it, end);
  190. while (!IsSpaceOrNewLine(*it) && !isEndOfBuffer(it, end)) {
  191. pBuffer[index] = *it;
  192. ++index;
  193. if (index == length - 1) {
  194. break;
  195. }
  196. ++it;
  197. }
  198. pBuffer[index] = '\0';
  199. return it;
  200. }
  201. /**
  202. * @brief Get next float from given line
  203. * @param[in] it set to current position
  204. * @param[in] end set to end of scratch buffer for readout
  205. * @param[out] value Separated float value.
  206. * @return Current-iterator with new position
  207. */
  208. template <class char_t>
  209. inline char_t getFloat(char_t it, char_t end, ai_real &value) {
  210. static const size_t BUFFERSIZE = 1024;
  211. char buffer[BUFFERSIZE] = {};
  212. it = CopyNextWord<char_t>(it, end, buffer, BUFFERSIZE);
  213. value = (ai_real)fast_atof(buffer);
  214. return it;
  215. }
  216. /**
  217. * @brief Checks for a line-end.
  218. * @param[in] it Current iterator in string.
  219. * @param[in] end End of the string.
  220. * @return The trimmed string.
  221. */
  222. template <class T>
  223. bool hasLineEnd(T it, T end) {
  224. bool hasLineEnd = false;
  225. while (!isEndOfBuffer(it, end)) {
  226. ++it;
  227. if (IsLineEnd(it)) {
  228. hasLineEnd = true;
  229. break;
  230. }
  231. }
  232. return hasLineEnd;
  233. }
  234. } // Namespace Assimp
  235. #endif // OBJ_TOOLS_H_INC