ObjTools.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, 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. /** @brief Returns true, if the last entry of the buffer is reached.
  43. * @param it Iterator of current position.
  44. * @param end Iterator with end of buffer.
  45. * @return true, if the end of the buffer is reached.
  46. */
  47. template <class char_t>
  48. inline bool isEndOfBuffer(char_t it, char_t end) {
  49. if (it == end) {
  50. return true;
  51. } else {
  52. --end;
  53. }
  54. return (it == end);
  55. }
  56. /** @brief Returns next word separated by a space
  57. * @param pBuffer Pointer to data buffer
  58. * @param pEnd Pointer to end of buffer
  59. * @return Pointer to next space
  60. */
  61. template <class Char_T>
  62. inline Char_T getNextWord(Char_T pBuffer, Char_T pEnd) {
  63. while (!isEndOfBuffer(pBuffer, pEnd)) {
  64. if (!IsSpaceOrNewLine(*pBuffer) || IsLineEnd(*pBuffer)) {
  65. //if ( *pBuffer != '\\' )
  66. break;
  67. }
  68. pBuffer++;
  69. }
  70. return pBuffer;
  71. }
  72. /** @brief Returns pointer a next token
  73. * @param pBuffer Pointer to data buffer
  74. * @param pEnd Pointer to end of buffer
  75. * @return Pointer to next token
  76. */
  77. template <class Char_T>
  78. inline Char_T getNextToken(Char_T pBuffer, Char_T pEnd) {
  79. while (!isEndOfBuffer(pBuffer, pEnd)) {
  80. if (IsSpaceOrNewLine(*pBuffer))
  81. break;
  82. pBuffer++;
  83. }
  84. return getNextWord(pBuffer, pEnd);
  85. }
  86. /** @brief Skips a line
  87. * @param it Iterator set to current position
  88. * @param end Iterator set to end of scratch buffer for readout
  89. * @param uiLine Current line number in format
  90. * @return Current-iterator with new position
  91. */
  92. template <class char_t>
  93. inline char_t skipLine(char_t it, char_t end, unsigned int &uiLine) {
  94. while (!isEndOfBuffer(it, end) && !IsLineEnd(*it)) {
  95. ++it;
  96. }
  97. if (it != end) {
  98. ++it;
  99. ++uiLine;
  100. }
  101. // fix .. from time to time there are spaces at the beginning of a material line
  102. while (it != end && (*it == '\t' || *it == ' ')) {
  103. ++it;
  104. }
  105. return it;
  106. }
  107. /** @brief Get a name from the current line. Preserve space in the middle,
  108. * but trim it at the end.
  109. * @param it set to current position
  110. * @param end set to end of scratch buffer for readout
  111. * @param name Separated name
  112. * @return Current-iterator with new position
  113. */
  114. template <class char_t>
  115. inline char_t getName(char_t it, char_t end, std::string &name) {
  116. name = "";
  117. if (isEndOfBuffer(it, end)) {
  118. return end;
  119. }
  120. char *pStart = &(*it);
  121. while (!isEndOfBuffer(it, end) && !IsLineEnd(*it)) {
  122. ++it;
  123. }
  124. while (IsSpace(*it)) {
  125. --it;
  126. }
  127. // Get name
  128. // if there is no name, and the previous char is a separator, come back to start
  129. while (&(*it) < pStart) {
  130. ++it;
  131. }
  132. std::string strName(pStart, &(*it));
  133. if (strName.empty())
  134. return it;
  135. else
  136. name = strName;
  137. return it;
  138. }
  139. /** @brief Get a name from the current line. Do not preserve space
  140. * in the middle, but trim it at the end.
  141. * @param it set to current position
  142. * @param end set to end of scratch buffer for readout
  143. * @param name Separated name
  144. * @return Current-iterator with new position
  145. */
  146. template <class char_t>
  147. inline char_t getNameNoSpace(char_t it, char_t end, std::string &name) {
  148. name = "";
  149. if (isEndOfBuffer(it, end)) {
  150. return end;
  151. }
  152. char *pStart = &(*it);
  153. while (!isEndOfBuffer(it, end) && !IsLineEnd(*it) && !IsSpaceOrNewLine(*it)) {
  154. ++it;
  155. }
  156. while (isEndOfBuffer(it, end) || IsLineEnd(*it) || IsSpaceOrNewLine(*it)) {
  157. --it;
  158. }
  159. ++it;
  160. // Get name
  161. // if there is no name, and the previous char is a separator, come back to start
  162. while (&(*it) < pStart) {
  163. ++it;
  164. }
  165. std::string strName(pStart, &(*it));
  166. if (strName.empty())
  167. return it;
  168. else
  169. name = strName;
  170. return it;
  171. }
  172. /** @brief Get next word from given line
  173. * @param it set to current position
  174. * @param end set to end of scratch buffer for readout
  175. * @param pBuffer Buffer for next word
  176. * @param length Buffer length
  177. * @return Current-iterator with new position
  178. */
  179. template <class char_t>
  180. inline char_t CopyNextWord(char_t it, char_t end, char *pBuffer, size_t length) {
  181. size_t index = 0;
  182. it = getNextWord<char_t>(it, end);
  183. while (!IsSpaceOrNewLine(*it) && !isEndOfBuffer(it, end)) {
  184. pBuffer[index] = *it;
  185. index++;
  186. if (index == length - 1)
  187. break;
  188. ++it;
  189. }
  190. pBuffer[index] = '\0';
  191. return it;
  192. }
  193. /** @brief Get next float from given line
  194. * @param it set to current position
  195. * @param end set to end of scratch buffer for readout
  196. * @param value Separated float value.
  197. * @return Current-iterator with new position
  198. */
  199. template <class char_t>
  200. inline char_t getFloat(char_t it, char_t end, ai_real &value) {
  201. static const size_t BUFFERSIZE = 1024;
  202. char buffer[BUFFERSIZE];
  203. it = CopyNextWord<char_t>(it, end, buffer, BUFFERSIZE);
  204. value = (ai_real)fast_atof(buffer);
  205. return it;
  206. }
  207. /** @brief Will perform a simple tokenize.
  208. * @param str String to tokenize.
  209. * @param tokens Array with tokens, will be empty if no token was found.
  210. * @param delimiters Delimiter for tokenize.
  211. * @return Number of found token.
  212. */
  213. template <class string_type>
  214. unsigned int tokenize(const string_type &str, std::vector<string_type> &tokens,
  215. const string_type &delimiters) {
  216. // Skip delimiters at beginning.
  217. typename string_type::size_type lastPos = str.find_first_not_of(delimiters, 0);
  218. // Find first "non-delimiter".
  219. typename string_type::size_type pos = str.find_first_of(delimiters, lastPos);
  220. while (string_type::npos != pos || string_type::npos != lastPos) {
  221. // Found a token, add it to the vector.
  222. string_type tmp = str.substr(lastPos, pos - lastPos);
  223. if (!tmp.empty() && ' ' != tmp[0])
  224. tokens.push_back(tmp);
  225. // Skip delimiters. Note the "not_of"
  226. lastPos = str.find_first_not_of(delimiters, pos);
  227. // Find next "non-delimiter"
  228. pos = str.find_first_of(delimiters, lastPos);
  229. }
  230. return static_cast<unsigned int>(tokens.size());
  231. }
  232. template <class string_type>
  233. string_type trim_whitespaces(string_type str) {
  234. while (!str.empty() && IsSpace(str[0]))
  235. str.erase(0);
  236. while (!str.empty() && IsSpace(str[str.length() - 1]))
  237. str.erase(str.length() - 1);
  238. return str;
  239. }
  240. template <class T>
  241. bool hasLineEnd(T it, T end) {
  242. bool hasLineEnd(false);
  243. while (!isEndOfBuffer(it, end)) {
  244. it++;
  245. if (IsLineEnd(it)) {
  246. hasLineEnd = true;
  247. break;
  248. }
  249. }
  250. return hasLineEnd;
  251. }
  252. } // Namespace Assimp
  253. #endif // OBJ_TOOLS_H_INC