ObjTools.h 8.7 KB

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