From dfaea27dfc4e85bb3894250c98299f2cdc99fec2 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Thu, 3 Nov 2016 23:29:03 +0100 Subject: [PATCH] MaterialSystem: fix some findings. --- code/MaterialSystem.cpp | 51 ++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/code/MaterialSystem.cpp b/code/MaterialSystem.cpp index 088336ef3..672ebd9c5 100644 --- a/code/MaterialSystem.cpp +++ b/code/MaterialSystem.cpp @@ -42,8 +42,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @brief Implementation of the material system of the library */ - - #include "Hash.h" #include "fast_atof.h" #include "ParsingUtils.h" @@ -71,7 +69,7 @@ aiReturn aiGetMaterialProperty(const aiMaterial* pMat, * could be improved by hashing, but it's possibly * no worth the effort (we're bound to C structures, * thus std::map or derivates are not applicable. */ - for (unsigned int i = 0; i < pMat->mNumProperties;++i) { + for ( unsigned int i = 0; i < pMat->mNumProperties; ++i ) { aiMaterialProperty* prop = pMat->mProperties[i]; if (prop /* just for safety ... */ @@ -151,14 +149,15 @@ aiReturn aiGetMaterialFloatArray(const aiMaterial* pMat, iWrite = *pMax; } // strings are zero-terminated with a 32 bit length prefix, so this is safe - const char* cur = prop->mData+4; - ai_assert(prop->mDataLength>=5 && !prop->mData[prop->mDataLength-1]); - for (unsigned int a = 0; ;++a) { + const char *cur = prop->mData + 4; + ai_assert( prop->mDataLength >= 5 ); + ai_assert( !prop->mData[ prop->mDataLength - 1 ] ); + for ( unsigned int a = 0; ;++a) { cur = fast_atoreal_move(cur,pOut[a]); - if(a==iWrite-1) { + if ( a==iWrite-1 ) { break; } - if(!IsSpace(*cur)) { + if ( !IsSpace(*cur) ) { DefaultLogger::get()->error("Material property" + std::string(pKey) + " is a string; failed to parse a float array out of it."); return AI_FAILURE; @@ -170,7 +169,6 @@ aiReturn aiGetMaterialFloatArray(const aiMaterial* pMat, } } return AI_SUCCESS; - } // ------------------------------------------------------------------------------------------------ @@ -224,8 +222,9 @@ aiReturn aiGetMaterialIntegerArray(const aiMaterial* pMat, iWrite = *pMax; } // strings are zero-terminated with a 32 bit length prefix, so this is safe - const char* cur = prop->mData+4; - ai_assert(prop->mDataLength>=5 && !prop->mData[prop->mDataLength-1]); + const char *cur = prop->mData+4; + ai_assert( prop->mDataLength >= 5 ); + ai_assert( !prop->mData[ prop->mDataLength - 1 ] ); for (unsigned int a = 0; ;++a) { pOut[a] = strtol10(cur,&cur); if(a==iWrite-1) { @@ -298,7 +297,8 @@ aiReturn aiGetMaterialString(const aiMaterial* pMat, // The string is stored as 32 but length prefix followed by zero-terminated UTF8 data pOut->length = static_cast(*reinterpret_cast(prop->mData)); - ai_assert(pOut->length+1+4==prop->mDataLength && !prop->mData[prop->mDataLength-1]); + ai_assert( pOut->length+1+4==prop->mDataLength ); + ai_assert( !prop->mData[ prop->mDataLength - 1 ] ); memcpy(pOut->data,prop->mData+4,pOut->length+1); } else { @@ -317,12 +317,12 @@ ASSIMP_API unsigned int aiGetMaterialTextureCount(const C_STRUCT aiMaterial* pMa { ai_assert (pMat != NULL); - /* Textures are always stored with ascending indices (ValidateDS provides a check, so we don't need to do it again) */ + // Textures are always stored with ascending indices (ValidateDS provides a check, so we don't need to do it again) unsigned int max = 0; for (unsigned int i = 0; i < pMat->mNumProperties;++i) { aiMaterialProperty* prop = pMat->mProperties[i]; - if (prop /* just a sanity check ... */ + if ( prop /* just a sanity check ... */ && 0 == strcmp( prop->mKey.data, _AI_MATKEY_TEXTURE_BASE ) && prop->mSemantic == type) { @@ -381,14 +381,17 @@ aiReturn aiGetMaterialTexture(const C_STRUCT aiMaterial* mat, return AI_SUCCESS; } +static const unsigned int DefaultNumAllocated = 5; + // ------------------------------------------------------------------------------------------------ // Construction. Actually the one and only way to get an aiMaterial instance -aiMaterial::aiMaterial() +aiMaterial::aiMaterial() +: mNumProperties( 0 ) +, mNumAllocated( DefaultNumAllocated ) +, mProperties( NULL ) { // Allocate 5 entries by default - mNumProperties = 0; - mNumAllocated = 5; - mProperties = new aiMaterialProperty*[5]; + mProperties = new aiMaterialProperty*[ DefaultNumAllocated ]; } // ------------------------------------------------------------------------------------------------ @@ -543,10 +546,10 @@ aiReturn aiMaterial::AddProperty (const aiString* pInput, } // ------------------------------------------------------------------------------------------------ -uint32_t Assimp :: ComputeMaterialHash(const aiMaterial* mat, bool includeMatName /*= false*/) +uint32_t Assimp::ComputeMaterialHash(const aiMaterial* mat, bool includeMatName /*= false*/) { uint32_t hash = 1503; // magic start value, chosen to be my birthday :-) - for (unsigned int i = 0; i < mat->mNumProperties;++i) { + for ( unsigned int i = 0; i < mat->mNumProperties; ++i ) { aiMaterialProperty* prop; // Exclude all properties whose first character is '?' from the hash @@ -585,15 +588,16 @@ void aiMaterial::CopyPropertyList(aiMaterial* pcDest, } } - if(pcOld) - delete[] pcOld; + if ( pcOld ) { + delete[] pcOld; + } for (unsigned int i = iOldNum; i< pcDest->mNumProperties;++i) { aiMaterialProperty* propSrc = pcSrc->mProperties[i]; // search whether we have already a property with this name -> if yes, overwrite it aiMaterialProperty* prop; - for (unsigned int q = 0; q < iOldNum;++q) { + for ( unsigned int q = 0; q < iOldNum; ++q ) { prop = pcDest->mProperties[q]; if (prop /* just for safety */ && prop->mKey == propSrc->mKey && prop->mSemantic == propSrc->mSemantic && prop->mIndex == propSrc->mIndex) { @@ -617,5 +621,4 @@ void aiMaterial::CopyPropertyList(aiMaterial* pcDest, prop->mData = new char[propSrc->mDataLength]; memcpy(prop->mData,propSrc->mData,prop->mDataLength); } - return; }