ObjTools.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #include "ParsingUtils.h"
  40. namespace Assimp
  41. {
  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. {
  50. if ( it == end )
  51. {
  52. return true;
  53. }
  54. else
  55. {
  56. end--;
  57. }
  58. return ( it == end );
  59. }
  60. /** @brief Returns next word separated by a space
  61. * @param pBuffer Pointer to data buffer
  62. * @param pEnd Pointer to end of buffer
  63. * @return Pointer to next space
  64. */
  65. template<class Char_T>
  66. inline Char_T getNextWord( Char_T pBuffer, Char_T pEnd )
  67. {
  68. while ( !isEndOfBuffer( pBuffer, pEnd ) )
  69. {
  70. if( !IsSpaceOrNewLine( *pBuffer ) || IsLineEnd( *pBuffer ) )
  71. break;
  72. pBuffer++;
  73. }
  74. return pBuffer;
  75. }
  76. /** @brief Returns pointer a next token
  77. * @param pBuffer Pointer to data buffer
  78. * @param pEnd Pointer to end of buffer
  79. * @return Pointer to next token
  80. */
  81. template<class Char_T>
  82. inline Char_T getNextToken( Char_T pBuffer, Char_T pEnd )
  83. {
  84. while ( !isEndOfBuffer( pBuffer, pEnd ) )
  85. {
  86. if( IsSpaceOrNewLine( *pBuffer ) )
  87. break;
  88. pBuffer++;
  89. }
  90. return getNextWord( pBuffer, pEnd );
  91. }
  92. /** @brief Skips a line
  93. * @param it Iterator set to current position
  94. * @param end Iterator set to end of scratch buffer for readout
  95. * @param uiLine Current line number in format
  96. * @return Current-iterator with new position
  97. */
  98. template<class char_t>
  99. inline char_t skipLine( char_t it, char_t end, unsigned int &uiLine ) {
  100. while( !isEndOfBuffer( it, end ) && !IsLineEnd( *it ) ) {
  101. ++it;
  102. }
  103. if ( it != end )
  104. {
  105. ++it;
  106. ++uiLine;
  107. }
  108. // fix .. from time to time there are spaces at the beginning of a material line
  109. while ( it != end && (*it == '\t' || *it == ' ') )
  110. ++it;
  111. return it;
  112. }
  113. /** @brief Get a name from the current line. Preserve space in the middle,
  114. * but trim it at the end.
  115. * @param it set to current position
  116. * @param end set to end of scratch buffer for readout
  117. * @param name Separated name
  118. * @return Current-iterator with new position
  119. */
  120. template<class char_t>
  121. inline char_t getName( char_t it, char_t end, std::string &name )
  122. {
  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( isEndOfBuffer( it, end ) || IsLineEnd( *it ) || IsSpaceOrNewLine( *it ) ) {
  132. --it;
  133. }
  134. ++it;
  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 next word from given line
  148. * @param it set to current position
  149. * @param end set to end of scratch buffer for readout
  150. * @param pBuffer Buffer for next word
  151. * @param length Buffer length
  152. * @return Current-iterator with new position
  153. */
  154. template<class char_t>
  155. inline char_t CopyNextWord( char_t it, char_t end, char *pBuffer, size_t length )
  156. {
  157. size_t index = 0;
  158. it = getNextWord<char_t>( it, end );
  159. while( !IsSpaceOrNewLine( *it ) && !isEndOfBuffer( it, end ) )
  160. {
  161. pBuffer[index] = *it ;
  162. index++;
  163. if (index == length-1)
  164. break;
  165. ++it;
  166. }
  167. pBuffer[ index ] = '\0';
  168. return it;
  169. }
  170. /** @brief Get next float from given line
  171. * @param it set to current position
  172. * @param end set to end of scratch buffer for readout
  173. * @param value Separated float value.
  174. * @return Current-iterator with new position
  175. */
  176. template<class char_t>
  177. inline char_t getFloat( char_t it, char_t end, float &value )
  178. {
  179. static const size_t BUFFERSIZE = 1024;
  180. char buffer[ BUFFERSIZE ];
  181. it = CopyNextWord<char_t>( it, end, buffer, BUFFERSIZE );
  182. value = (float) fast_atof( buffer );
  183. return it;
  184. }
  185. /** @brief Will perform a simple tokenize.
  186. * @param str String to tokenize.
  187. * @param tokens Array with tokens, will be empty if no token was found.
  188. * @param delimiters Delimiter for tokenize.
  189. * @return Number of found token.
  190. */
  191. template<class string_type>
  192. unsigned int tokenize( const string_type& str, std::vector<string_type>& tokens,
  193. const string_type& delimiters )
  194. {
  195. // Skip delimiters at beginning.
  196. typename string_type::size_type lastPos = str.find_first_not_of( delimiters, 0 );
  197. // Find first "non-delimiter".
  198. typename string_type::size_type pos = str.find_first_of( delimiters, lastPos );
  199. while ( string_type::npos != pos || string_type::npos != lastPos )
  200. {
  201. // Found a token, add it to the vector.
  202. string_type tmp = str.substr(lastPos, pos - lastPos);
  203. if ( !tmp.empty() && ' ' != tmp[ 0 ] )
  204. tokens.push_back( tmp );
  205. // Skip delimiters. Note the "not_of"
  206. lastPos = str.find_first_not_of( delimiters, pos );
  207. // Find next "non-delimiter"
  208. pos = str.find_first_of( delimiters, lastPos );
  209. }
  210. return static_cast<unsigned int>( tokens.size() );
  211. }
  212. } // Namespace Assimp
  213. #endif // OBJ_TOOLS_H_INC