ObjTools.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. namespace Assimp
  39. {
  40. /** @brief Returns true, if token is a space on any supported platform
  41. * @param token Token to search in
  42. * @return true, if token is a space
  43. */
  44. inline bool isSpace( char token )
  45. {
  46. return ( token == ' ' ||
  47. token == '\n' ||
  48. token == '\f' ||
  49. token == '\r' ||
  50. token == '\t' );
  51. }
  52. /** @brief Returns true, fi token id a new line marking token.
  53. * @param token Token to search in
  54. * @return true, if token is a newline token.
  55. */
  56. inline bool isNewLine( char token )
  57. {
  58. return ( token == '\n' || token == '\f' || token == '\r' );
  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 (pBuffer != pEnd)
  69. {
  70. if (!isSpace(*pBuffer))
  71. break;
  72. pBuffer++;
  73. }
  74. return pBuffer;
  75. }
  76. /** @brief Returns ponter 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 (pBuffer != pEnd)
  85. {
  86. if ( isSpace( *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 linenumber 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. {
  101. while ( it != end && *it != '\n' )
  102. ++it;
  103. if ( it != end )
  104. {
  105. ++it;
  106. ++uiLine;
  107. }
  108. return it;
  109. }
  110. /** @brief Get a name, must be separated with a blank.
  111. * @param it set to current position
  112. * @param end set to end of scratch buffer for readout
  113. * @param name Separated name
  114. * @return Current-iterator with new position
  115. */
  116. template<class char_t>
  117. inline char_t getName( char_t it, char_t end, std::string &name )
  118. {
  119. name = "";
  120. it = getNextToken<char_t>( it, end );
  121. if ( it == end )
  122. return end;
  123. char *pStart = &(*it);
  124. while ( !isSpace(*it) && it != end )
  125. ++it;
  126. // Get name
  127. std::string strName(pStart, &(*it));
  128. if ( strName.empty() )
  129. return it;
  130. else
  131. name = strName;
  132. return it;
  133. }
  134. /** @brief Get next word from given line
  135. * @param it set to current position
  136. * @param end set to end of scratch buffer for readout
  137. * @param pBuffer Buffer for next word
  138. * @param length Buffer length
  139. * @return Current-iterator with new position
  140. */
  141. template<class char_t>
  142. inline char_t CopyNextWord( char_t it, char_t end, char *pBuffer, size_t length )
  143. {
  144. size_t index = 0;
  145. it = getNextWord<char_t>( it, end );
  146. while (!isSpace( *it ) && it != end )
  147. {
  148. pBuffer[index] = *it ;
  149. index++;
  150. if (index == length-1)
  151. break;
  152. ++it;
  153. }
  154. pBuffer[index] = '\0';
  155. return it;
  156. }
  157. /** @brief Get next float from given line
  158. * @param it set to current position
  159. * @param end set to end of scratch buffer for readout
  160. * @param value Separated float value.
  161. * @return Current-iterator with new position
  162. */
  163. template<class char_t>
  164. inline char_t getFloat( char_t it, char_t end, float &value )
  165. {
  166. static const size_t BUFFERSIZE = 1024;
  167. char buffer[ BUFFERSIZE ];
  168. memset( buffer, '\0', BUFFERSIZE );
  169. it = CopyNextWord<char_t>( it, end, buffer, BUFFERSIZE );
  170. value = (float) fast_atof( buffer );
  171. return it;
  172. }
  173. } // Namespace Assimp
  174. #endif