--- libgig/trunk/src/helper.h 2006/10/24 22:24:45 929 +++ libgig/trunk/src/helper.h 2017/05/21 17:19:20 3200 @@ -1,8 +1,8 @@ /*************************************************************************** * * - * libgig - C++ cross-platform Gigasampler format file loader library * + * libgig - C++ cross-platform Gigasampler format file access library * * * - * Copyright (C) 2003-2006 by Christian Schoenebeck * + * Copyright (C) 2003-2017 by Christian Schoenebeck * * * * * * This library is free software; you can redistribute it and/or modify * @@ -27,6 +27,18 @@ #include #include #include +#include + +#if defined(WIN32) && !HAVE_CONFIG_H +# include "../win32/libgig_private.h" // like config.h, automatically generated by Dev-C++ +# define PACKAGE "libgig" +# define VERSION VER_STRING // VER_STRING defined in libgig_private.h +#endif // WIN32 + +#if HAVE_CONFIG_H /*&& !HAVE_VASPRINTF*/ && defined(WIN32) +# include +int vasprintf(char** ret, const char* format, va_list arg); +#endif #include "RIFF.h" @@ -39,6 +51,11 @@ return ss.str(); } +inline std::string toLowerCase(std::string s) { + std::transform(s.begin(), s.end(), s.begin(), ::tolower); + return s; +} + inline long Min(long A, long B) { return (A > B) ? B : A; } @@ -47,6 +64,83 @@ return (val > 0) ? val : -val; } +inline void swapBytes_16(void* Word) { + uint8_t byteCache = *((uint8_t*) Word); + *((uint8_t*) Word) = *((uint8_t*) Word + 1); + *((uint8_t*) Word + 1) = byteCache; +} + +inline void swapBytes_32(void* Word) { + uint8_t byteCache = *((uint8_t*) Word); + *((uint8_t*) Word) = *((uint8_t*) Word + 3); + *((uint8_t*) Word + 3) = byteCache; + byteCache = *((uint8_t*) Word + 1); + *((uint8_t*) Word + 1) = *((uint8_t*) Word + 2); + *((uint8_t*) Word + 2) = byteCache; +} + +inline void swapBytes_64(void* Word) { + uint8_t byteCache = ((uint8_t*)Word)[0]; + ((uint8_t*)Word)[0] = ((uint8_t*)Word)[7]; + ((uint8_t*)Word)[7] = byteCache; + byteCache = ((uint8_t*)Word)[1]; + ((uint8_t*)Word)[1] = ((uint8_t*)Word)[6]; + ((uint8_t*)Word)[6] = byteCache; + byteCache = ((uint8_t*)Word)[2]; + ((uint8_t*)Word)[2] = ((uint8_t*)Word)[5]; + ((uint8_t*)Word)[5] = byteCache; + byteCache = ((uint8_t*)Word)[3]; + ((uint8_t*)Word)[3] = ((uint8_t*)Word)[4]; + ((uint8_t*)Word)[4] = byteCache; +} + +inline void swapBytes(void* Word, uint64_t WordSize) { + uint8_t byteCache; + uint64_t lo = 0, hi = WordSize - 1; + for (; lo < hi; hi--, lo++) { + byteCache = *((uint8_t*) Word + lo); + *((uint8_t*) Word + lo) = *((uint8_t*) Word + hi); + *((uint8_t*) Word + hi) = byteCache; + } +} + +/** + * Stores a 16 bit integer in memory using little-endian format. + * + * @param pData - memory pointer + * @param data - integer to be stored + */ +inline void store16(uint8_t* pData, uint16_t data) { + pData[0] = data; + pData[1] = data >> 8; +} + +/** + * Stores a 32 bit integer in memory using little-endian format. + * + * @param pData - memory pointer + * @param data - integer to be stored + */ +inline void store32(uint8_t* pData, uint32_t data) { + pData[0] = data; + pData[1] = data >> 8; + pData[2] = data >> 16; + pData[3] = data >> 24; +} + +/** + * Loads a 32 bit integer in memory using little-endian format. + * + * @param pData - memory pointer + * @returns 32 bit data word + */ +inline uint32_t load32(uint8_t* pData) { + return uint32_t(pData[0]) | + uint32_t(pData[1]) << 8 | + uint32_t(pData[2]) << 16 | + uint32_t(pData[3]) << 24; +} + /** * Swaps the order of the data words in the given memory area * with a granularity given by \a WordSize. @@ -56,6 +150,7 @@ * @param WordSize - size of the data words (in bytes) */ inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) { + if (!AreaSize) return; // AreaSize==0 would cause a segfault here switch (WordSize) { // TODO: unefficient case 1: { uint8_t* pDst = (uint8_t*) pData; @@ -98,7 +193,7 @@ memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize); memcpy((uint8_t*) pData + hi, pCache, WordSize); } - delete[] pCache; + if (pCache) delete[] pCache; break; } } @@ -111,7 +206,7 @@ inline void LoadString(RIFF::Chunk* ck, std::string& s) { if (ck) { const char* str = (char*)ck->LoadChunkData(); - int size = ck->GetSize(); + int size = (int) ck->GetSize(); int len; for (len = 0 ; len < size ; len++) if (str[len] == '\0') break; @@ -140,17 +235,38 @@ */ inline void SaveString(uint32_t ChunkID, RIFF::Chunk* ck, RIFF::List* lstINFO, const std::string& s, const std::string& sDefault, bool bUseFixedLengthStrings, int size) { if (ck) { // if chunk exists already, use 's' as value - if (!bUseFixedLengthStrings) size = s.size() + 1; + if (!bUseFixedLengthStrings) size = (int) s.size() + 1; ck->Resize(size); char* pData = (char*) ck->LoadChunkData(); strncpy(pData, s.c_str(), size); - } else if (s != "" || sDefault != "") { // create chunk + } else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk const std::string& sToSave = (s != "") ? s : sDefault; - if (!bUseFixedLengthStrings) size = sToSave.size() + 1; + if (!bUseFixedLengthStrings) size = (int) sToSave.size() + 1; ck = lstINFO->AddSubChunk(ChunkID, size); char* pData = (char*) ck->LoadChunkData(); strncpy(pData, sToSave.c_str(), size); } } +// private helper function to convert progress of a subprocess into the global progress +inline void __notify_progress(RIFF::progress_t* pProgress, float subprogress) { + if (pProgress && pProgress->callback) { + const float totalrange = pProgress->__range_max - pProgress->__range_min; + const float totalprogress = pProgress->__range_min + subprogress * totalrange; + pProgress->factor = totalprogress; + pProgress->callback(pProgress); // now actually notify about the progress + } +} + +// private helper function to divide a progress into subprogresses +inline void __divide_progress(RIFF::progress_t* pParentProgress, RIFF::progress_t* pSubProgress, float totalTasks, float currentTask) { + if (pParentProgress && pParentProgress->callback) { + const float totalrange = pParentProgress->__range_max - pParentProgress->__range_min; + pSubProgress->callback = pParentProgress->callback; + pSubProgress->custom = pParentProgress->custom; + pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks; + pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks; + } +} + #endif // __LIBGIG_HELPER_H__