ObjTools.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 == ' ' || token == '\n' || token == '\f' || token == '\r' ||
  47. token == '\t');
  48. }
  49. /** @brief Returns next word separated by a space
  50. * @param pBuffer Pointer to data buffer
  51. * @param pEnd Pointer to end of buffer
  52. * @return Pointer to next space
  53. */
  54. template<class Char_T>
  55. inline Char_T getNextWord(Char_T pBuffer, Char_T pEnd)
  56. {
  57. while (pBuffer != pEnd)
  58. {
  59. if (!isSpace(*pBuffer))
  60. break;
  61. pBuffer++;
  62. }
  63. return pBuffer;
  64. }
  65. /** @brief Returns ponter a next token
  66. * @param pBuffer Pointer to data buffer
  67. * @param pEnd Pointer to end of buffer
  68. * @return Pointer to next token
  69. */
  70. template<class Char_T>
  71. inline Char_T getNextToken(Char_T pBuffer, Char_T pEnd)
  72. {
  73. while (pBuffer != pEnd)
  74. {
  75. if (isSpace(*pBuffer))
  76. break;
  77. pBuffer++;
  78. }
  79. return getNextWord(pBuffer, pEnd);
  80. }
  81. /** @brief Skips a line
  82. * @param Iterator set to current position
  83. * @param Iterator set to end of scratch buffer for readout
  84. * @param Current linenumber in format
  85. * @return Current-iterator with new position
  86. */
  87. template<class char_t>
  88. inline char_t skipLine(char_t it, char_t end, unsigned int &uiLine)
  89. {
  90. while ( it != end && *it != '\n' )
  91. ++it;
  92. if ( it != end )
  93. {
  94. ++it;
  95. ++uiLine;
  96. }
  97. return it;
  98. }
  99. template<class char_t>
  100. inline char_t getName( char_t it, char_t end, std::string &name )
  101. {
  102. name = "";
  103. it = getNextToken<char_t>( it, end );
  104. if ( it == end )
  105. return end;
  106. char *pStart = &(*it);
  107. while ( !isSpace(*it) && it != end )
  108. ++it;
  109. // Get name
  110. std::string strName(pStart, &(*it));
  111. if ( strName.empty() )
  112. return it;
  113. else
  114. name = strName;
  115. return it;
  116. }
  117. template<class char_t>
  118. inline char_t CopyNextWord( char_t it, char_t end, char *pBuffer, size_t length )
  119. {
  120. size_t index = 0;
  121. it = getNextWord<char_t>( it, end );
  122. while (!isSpace( *it ) && it != end )
  123. {
  124. pBuffer[index] = *it ;
  125. index++;
  126. if (index == length-1)
  127. break;
  128. ++it;
  129. }
  130. pBuffer[index] = '\0';
  131. return it;
  132. }
  133. } // Namespace Assimp
  134. #endif