2
0

ObjTools.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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. end--;
  50. return ( it == end );
  51. }
  52. /** @brief Returns true, if token is a space on any supported platform
  53. * @param token Token to search in
  54. * @return true, if token is a space
  55. */
  56. inline bool isSeparator( char token )
  57. {
  58. return ( token == ' ' ||
  59. token == '\n' ||
  60. token == '\f' ||
  61. token == '\r' ||
  62. token == '\t' );
  63. }
  64. /** @brief Returns true, fi token id a new line marking token.
  65. * @param token Token to search in
  66. * @return true, if token is a newline token.
  67. */
  68. inline bool isNewLine( char token )
  69. {
  70. return ( token == '\n' || token == '\f' || token == '\r' );
  71. }
  72. /** @brief Returns next word separated by a space
  73. * @param pBuffer Pointer to data buffer
  74. * @param pEnd Pointer to end of buffer
  75. * @return Pointer to next space
  76. */
  77. template<class Char_T>
  78. inline Char_T getNextWord( Char_T pBuffer, Char_T pEnd )
  79. {
  80. while ( !isEndOfBuffer( pBuffer, pEnd ) )
  81. {
  82. if ( !isSeparator( *pBuffer ) )
  83. break;
  84. pBuffer++;
  85. }
  86. return pBuffer;
  87. }
  88. /** @brief Returns ponter a next token
  89. * @param pBuffer Pointer to data buffer
  90. * @param pEnd Pointer to end of buffer
  91. * @return Pointer to next token
  92. */
  93. template<class Char_T>
  94. inline Char_T getNextToken( Char_T pBuffer, Char_T pEnd )
  95. {
  96. while ( !isEndOfBuffer( pBuffer, pEnd ) )
  97. {
  98. if ( isSeparator( *pBuffer ) )
  99. break;
  100. pBuffer++;
  101. }
  102. return getNextWord( pBuffer, pEnd );
  103. }
  104. /** @brief Skips a line
  105. * @param it Iterator set to current position
  106. * @param end Iterator set to end of scratch buffer for readout
  107. * @param uiLine Current linenumber in format
  108. * @return Current-iterator with new position
  109. */
  110. template<class char_t>
  111. inline char_t skipLine( char_t it, char_t end, unsigned int &uiLine )
  112. {
  113. while ( !isEndOfBuffer( it, end ) && *it != '\n' )
  114. ++it;
  115. if ( it != end )
  116. {
  117. ++it;
  118. ++uiLine;
  119. }
  120. // fix .. from time to time there are spaces at the beginning of a material line
  121. while ( it != end && (*it == '\t' || *it == ' ') )
  122. ++it;
  123. return it;
  124. }
  125. /** @brief Get a name, must be separated with a blank.
  126. * @param it set to current position
  127. * @param end set to end of scratch buffer for readout
  128. * @param name Separated name
  129. * @return Current-iterator with new position
  130. */
  131. template<class char_t>
  132. inline char_t getName( char_t it, char_t end, std::string &name )
  133. {
  134. name = "";
  135. it = getNextToken<char_t>( it, end );
  136. if ( isEndOfBuffer( it, end ) )
  137. return end;
  138. char *pStart = &( *it );
  139. while ( !isEndOfBuffer( it, end ) && !isSeparator( *it ) )
  140. ++it;
  141. // Get name
  142. std::string strName( pStart, &(*it) );
  143. if ( strName.empty() )
  144. return it;
  145. else
  146. name = strName;
  147. return it;
  148. }
  149. /** @brief Get next word from given line
  150. * @param it set to current position
  151. * @param end set to end of scratch buffer for readout
  152. * @param pBuffer Buffer for next word
  153. * @param length Buffer length
  154. * @return Current-iterator with new position
  155. */
  156. template<class char_t>
  157. inline char_t CopyNextWord( char_t it, char_t end, char *pBuffer, size_t length )
  158. {
  159. size_t index = 0;
  160. it = getNextWord<char_t>( it, end );
  161. while ( !isSeparator( *it ) && !isEndOfBuffer( it, end ) )
  162. {
  163. pBuffer[index] = *it ;
  164. index++;
  165. if (index == length-1)
  166. break;
  167. ++it;
  168. }
  169. pBuffer[ index ] = '\0';
  170. return it;
  171. }
  172. /** @brief Get next float from given line
  173. * @param it set to current position
  174. * @param end set to end of scratch buffer for readout
  175. * @param value Separated float value.
  176. * @return Current-iterator with new position
  177. */
  178. template<class char_t>
  179. inline char_t getFloat( char_t it, char_t end, float &value )
  180. {
  181. static const size_t BUFFERSIZE = 1024;
  182. char buffer[ BUFFERSIZE ];
  183. it = CopyNextWord<char_t>( it, end, buffer, BUFFERSIZE );
  184. value = (float) fast_atof( buffer );
  185. return it;
  186. }
  187. } // Namespace Assimp
  188. #endif