Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

263 rader
7.1 KiB

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