helper.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   libgig - C++ cross-platform Gigasampler format file access library    *
00004  *                                                                         *
00005  *   Copyright (C) 2003-2006 by Christian Schoenebeck                      *
00006  *                              <cuse@users.sourceforge.net>               *
00007  *                                                                         *
00008  *   This library is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This library is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this library; if not, write to the Free Software           *
00020  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston,                 *
00021  *   MA  02111-1307  USA                                                   *
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 // *************** Helper Functions **************
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) { // TODO: unefficient
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]; // TODO: unefficient
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) { // if chunk exists already, use 's' as value
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 != "") { // create chunk
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__

Generated on Wed Dec 6 19:25:57 2006 for libgig by  doxygen 1.5.1