00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __LIBGIG_HELPER_H__
00025 #define __LIBGIG_HELPER_H__
00026
00027 #include <string.h>
00028 #include <string>
00029 #include <sstream>
00030
00031 #include "RIFF.h"
00032
00033
00034
00035
00036 template<class T> inline std::string ToString(T o) {
00037 std::stringstream ss;
00038 ss << o;
00039 return ss.str();
00040 }
00041
00042 inline long Min(long A, long B) {
00043 return (A > B) ? B : A;
00044 }
00045
00046 inline long Abs(long val) {
00047 return (val > 0) ? val : -val;
00048 }
00049
00058 inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) {
00059 switch (WordSize) {
00060 case 1: {
00061 uint8_t* pDst = (uint8_t*) pData;
00062 uint8_t cache;
00063 unsigned long lo = 0, hi = AreaSize - 1;
00064 for (; lo < hi; hi--, lo++) {
00065 cache = pDst[lo];
00066 pDst[lo] = pDst[hi];
00067 pDst[hi] = cache;
00068 }
00069 break;
00070 }
00071 case 2: {
00072 uint16_t* pDst = (uint16_t*) pData;
00073 uint16_t cache;
00074 unsigned long lo = 0, hi = (AreaSize >> 1) - 1;
00075 for (; lo < hi; hi--, lo++) {
00076 cache = pDst[lo];
00077 pDst[lo] = pDst[hi];
00078 pDst[hi] = cache;
00079 }
00080 break;
00081 }
00082 case 4: {
00083 uint32_t* pDst = (uint32_t*) pData;
00084 uint32_t cache;
00085 unsigned long lo = 0, hi = (AreaSize >> 2) - 1;
00086 for (; lo < hi; hi--, lo++) {
00087 cache = pDst[lo];
00088 pDst[lo] = pDst[hi];
00089 pDst[hi] = cache;
00090 }
00091 break;
00092 }
00093 default: {
00094 uint8_t* pCache = new uint8_t[WordSize];
00095 unsigned long lo = 0, hi = AreaSize - WordSize;
00096 for (; lo < hi; hi -= WordSize, lo += WordSize) {
00097 memcpy(pCache, (uint8_t*) pData + lo, WordSize);
00098 memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);
00099 memcpy((uint8_t*) pData + hi, pCache, WordSize);
00100 }
00101 delete[] pCache;
00102 break;
00103 }
00104 }
00105 }
00106
00111 inline void LoadString(RIFF::Chunk* ck, std::string& s) {
00112 if (ck) {
00113 const char* str = (char*)ck->LoadChunkData();
00114 int size = ck->GetSize();
00115 int len;
00116 for (len = 0 ; len < size ; len++)
00117 if (str[len] == '\0') break;
00118 s.assign(str, len);
00119 ck->ReleaseChunkData();
00120 }
00121 }
00122
00141 inline void SaveString(uint32_t ChunkID, RIFF::Chunk* ck, RIFF::List* lstINFO, const std::string& s, const std::string& sDefault, bool bUseFixedLengthStrings, int size) {
00142 if (ck) {
00143 if (!bUseFixedLengthStrings) size = s.size() + 1;
00144 ck->Resize(size);
00145 char* pData = (char*) ck->LoadChunkData();
00146 strncpy(pData, s.c_str(), size);
00147 } else if (s != "" || sDefault != "") {
00148 const std::string& sToSave = (s != "") ? s : sDefault;
00149 if (!bUseFixedLengthStrings) size = sToSave.size() + 1;
00150 ck = lstINFO->AddSubChunk(ChunkID, size);
00151 char* pData = (char*) ck->LoadChunkData();
00152 strncpy(pData, sToSave.c_str(), size);
00153 }
00154 }
00155
00156 #endif // __LIBGIG_HELPER_H__