Merge pull request #4086 from assimp/kimkulling-obj_refactorings

Update ObjTools.h
pull/4078/head^2
Kim Kulling 2021-09-21 12:53:51 +02:00 committed by GitHub
commit d286aadbdf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 70 additions and 49 deletions

View File

@ -2,7 +2,7 @@
Open Asset Import Library (assimp) Open Asset Import Library (assimp)
---------------------------------------------------------------------- ----------------------------------------------------------------------
Copyright (c) 2006-2020, assimp team Copyright (c) 2006-2021, assimp team
All rights reserved. All rights reserved.
@ -51,57 +51,62 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace Assimp { namespace Assimp {
/** @brief Returns true, if the last entry of the buffer is reached. /**
* @param it Iterator of current position. * @brief Returns true, if the last entry of the buffer is reached.
* @param end Iterator with end of buffer. * @param[in] it Iterator of current position.
* @param[in] end Iterator with end of buffer.
* @return true, if the end of the buffer is reached. * @return true, if the end of the buffer is reached.
*/ */
template <class char_t> template <class char_t>
inline bool isEndOfBuffer(char_t it, char_t end) { inline bool isEndOfBuffer(char_t it, char_t end) {
if (it == end) { if (it == end) {
return true; return true;
} else {
--end;
} }
--end;
return (it == end); return (it == end);
} }
/** @brief Returns next word separated by a space /**
* @param pBuffer Pointer to data buffer * @brief Returns next word separated by a space
* @param pEnd Pointer to end of buffer * @param[in] pBuffer Pointer to data buffer
* @param[in] pEnd Pointer to end of buffer
* @return Pointer to next space * @return Pointer to next space
*/ */
template <class Char_T> template <class Char_T>
inline Char_T getNextWord(Char_T pBuffer, Char_T pEnd) { inline Char_T getNextWord(Char_T pBuffer, Char_T pEnd) {
while (!isEndOfBuffer(pBuffer, pEnd)) { while (!isEndOfBuffer(pBuffer, pEnd)) {
if (!IsSpaceOrNewLine(*pBuffer) || IsLineEnd(*pBuffer)) { if (!IsSpaceOrNewLine(*pBuffer) || IsLineEnd(*pBuffer)) {
//if ( *pBuffer != '\\' )
break; break;
} }
pBuffer++; ++pBuffer;
} }
return pBuffer; return pBuffer;
} }
/** @brief Returns pointer a next token /**
* @param pBuffer Pointer to data buffer * @brief Returns pointer a next token
* @param pEnd Pointer to end of buffer * @param[in] pBuffer Pointer to data buffer
* @param[in] pEnd Pointer to end of buffer
* @return Pointer to next token * @return Pointer to next token
*/ */
template <class Char_T> template <class Char_T>
inline Char_T getNextToken(Char_T pBuffer, Char_T pEnd) { inline Char_T getNextToken(Char_T pBuffer, Char_T pEnd) {
while (!isEndOfBuffer(pBuffer, pEnd)) { while (!isEndOfBuffer(pBuffer, pEnd)) {
if (IsSpaceOrNewLine(*pBuffer)) if (IsSpaceOrNewLine(*pBuffer)) {
break; break;
pBuffer++; }
++pBuffer;
} }
return getNextWord(pBuffer, pEnd); return getNextWord(pBuffer, pEnd);
} }
/** @brief Skips a line /**
* @param it Iterator set to current position * @brief Skips a line
* @param end Iterator set to end of scratch buffer for readout * @param[in] it Iterator set to current position
* @param uiLine Current line number in format * @param[in] end Iterator set to end of scratch buffer for readout
* @param[out] uiLine Current line number in format
* @return Current-iterator with new position * @return Current-iterator with new position
*/ */
template <class char_t> template <class char_t>
@ -122,11 +127,12 @@ inline char_t skipLine(char_t it, char_t end, unsigned int &uiLine) {
return it; return it;
} }
/** @brief Get a name from the current line. Preserve space in the middle, /**
* @brief Get a name from the current line. Preserve space in the middle,
* but trim it at the end. * but trim it at the end.
* @param it set to current position * @param[in] it set to current position
* @param end set to end of scratch buffer for readout * @param[in] end set to end of scratch buffer for readout
* @param name Separated name * @param[out] name Separated name
* @return Current-iterator with new position * @return Current-iterator with new position
*/ */
template <class char_t> template <class char_t>
@ -150,15 +156,16 @@ inline char_t getName(char_t it, char_t end, std::string &name) {
++it; ++it;
} }
std::string strName(pStart, &(*it)); std::string strName(pStart, &(*it));
if (strName.empty()) if (!strName.empty()) {
return it;
else
name = strName; name = strName;
}
return it; return it;
} }
/** @brief Get a name from the current line. Do not preserve space /**
* @brief Get a name from the current line. Do not preserve space
* in the middle, but trim it at the end. * in the middle, but trim it at the end.
* @param it set to current position * @param it set to current position
* @param end set to end of scratch buffer for readout * @param end set to end of scratch buffer for readout
@ -188,19 +195,19 @@ inline char_t getNameNoSpace(char_t it, char_t end, std::string &name) {
++it; ++it;
} }
std::string strName(pStart, &(*it)); std::string strName(pStart, &(*it));
if (strName.empty()) if (!strName.empty()) {
return it;
else
name = strName; name = strName;
}
return it; return it;
} }
/** @brief Get next word from given line /**
* @param it set to current position * @brief Get next word from given line
* @param end set to end of scratch buffer for readout * @param[in] it set to current position
* @param pBuffer Buffer for next word * @param[in] end set to end of scratch buffer for readout
* @param length Buffer length * @param[in] pBuffer Buffer for next word
* @param[in] length Buffer length
* @return Current-iterator with new position * @return Current-iterator with new position
*/ */
template <class char_t> template <class char_t>
@ -209,19 +216,21 @@ inline char_t CopyNextWord(char_t it, char_t end, char *pBuffer, size_t length)
it = getNextWord<char_t>(it, end); it = getNextWord<char_t>(it, end);
while (!IsSpaceOrNewLine(*it) && !isEndOfBuffer(it, end)) { while (!IsSpaceOrNewLine(*it) && !isEndOfBuffer(it, end)) {
pBuffer[index] = *it; pBuffer[index] = *it;
index++; ++index;
if (index == length - 1) if (index == length - 1) {
break; break;
}
++it; ++it;
} }
pBuffer[index] = '\0'; pBuffer[index] = '\0';
return it; return it;
} }
/** @brief Get next float from given line /**
* @param it set to current position * @brief Get next float from given line
* @param end set to end of scratch buffer for readout * @param[in] it set to current position
* @param value Separated float value. * @param[in] end set to end of scratch buffer for readout
* @param[out] value Separated float value.
* @return Current-iterator with new position * @return Current-iterator with new position
*/ */
template <class char_t> template <class char_t>
@ -234,21 +243,33 @@ inline char_t getFloat(char_t it, char_t end, ai_real &value) {
return it; return it;
} }
/**
* @brief Will remove white-spaces for a string.
* @param[in] str The string to clean
* @return The trimmed string.
*/
template <class string_type> template <class string_type>
string_type trim_whitespaces(string_type str) { inline string_type trim_whitespaces(string_type str) {
while (!str.empty() && IsSpace(str[0])) while (!str.empty() && IsSpace(str[0])) {
str.erase(0); str.erase(0);
while (!str.empty() && IsSpace(str[str.length() - 1])) }
while (!str.empty() && IsSpace(str[str.length() - 1])) {
str.erase(str.length() - 1); str.erase(str.length() - 1);
}
return str; return str;
} }
/**
* @brief Checks for a line-end.
* @param[in] it Current iterator in string.
* @param[in] end End of the string.
* @return The trimmed string.
*/
template <class T> template <class T>
bool hasLineEnd(T it, T end) { bool hasLineEnd(T it, T end) {
bool hasLineEnd(false); bool hasLineEnd = false;
while (!isEndOfBuffer(it, end)) { while (!isEndOfBuffer(it, end)) {
it++; ++it;
if (IsLineEnd(it)) { if (IsLineEnd(it)) {
hasLineEnd = true; hasLineEnd = true;
break; break;