/[svn]/libgig/trunk/src/helper.h
ViewVC logotype

Diff of /libgig/trunk/src/helper.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1179 by persson, Sat May 12 11:25:04 2007 UTC revision 3348 by schoenebeck, Tue Oct 3 15:05:45 2017 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file access library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2006 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2017 by Christian Schoenebeck                      *
6   *                              <cuse@users.sourceforge.net>               *   *                              <cuse@users.sourceforge.net>               *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
# Line 27  Line 27 
27  #include <string.h>  #include <string.h>
28  #include <string>  #include <string>
29  #include <sstream>  #include <sstream>
30    #include <algorithm>
31    
32    #if defined(WIN32) && !HAVE_CONFIG_H
33    # include "../win32/libgig_private.h" // like config.h, automatically generated by Dev-C++
34    # define PACKAGE "libgig"
35    # define VERSION VER_STRING // VER_STRING defined in libgig_private.h
36    #endif // WIN32
37    
38    #if HAVE_CONFIG_H /*&& !HAVE_VASPRINTF*/ && defined(WIN32)
39    # include <stdarg.h>
40    int vasprintf(char** ret, const char* format, va_list arg);
41    #endif
42    
43  #include "RIFF.h"  #include "RIFF.h"
44    
# Line 39  template<class T> inline std::string ToS Line 51  template<class T> inline std::string ToS
51      return ss.str();      return ss.str();
52  }  }
53    
54    inline std::string toLowerCase(std::string s) {
55        std::transform(s.begin(), s.end(), s.begin(), ::tolower);
56        return s;
57    }
58    
59  inline long Min(long A, long B) {  inline long Min(long A, long B) {
60      return (A > B) ? B : A;      return (A > B) ? B : A;
61  }  }
# Line 47  inline long Abs(long val) { Line 64  inline long Abs(long val) {
64      return (val > 0) ? val : -val;      return (val > 0) ? val : -val;
65  }  }
66    
67    inline void swapBytes_16(void* Word) {
68        uint8_t byteCache = *((uint8_t*) Word);
69        *((uint8_t*) Word)     = *((uint8_t*) Word + 1);
70        *((uint8_t*) Word + 1) = byteCache;
71    }
72    
73    inline void swapBytes_32(void* Word) {
74        uint8_t byteCache = *((uint8_t*) Word);
75        *((uint8_t*) Word)     = *((uint8_t*) Word + 3);
76        *((uint8_t*) Word + 3) = byteCache;
77        byteCache = *((uint8_t*) Word + 1);
78        *((uint8_t*) Word + 1) = *((uint8_t*) Word + 2);
79        *((uint8_t*) Word + 2) = byteCache;
80    }
81    
82    inline void swapBytes_64(void* Word) {
83        uint8_t byteCache = ((uint8_t*)Word)[0];
84        ((uint8_t*)Word)[0] = ((uint8_t*)Word)[7];
85        ((uint8_t*)Word)[7] = byteCache;
86        byteCache = ((uint8_t*)Word)[1];
87        ((uint8_t*)Word)[1] = ((uint8_t*)Word)[6];
88        ((uint8_t*)Word)[6] = byteCache;
89        byteCache = ((uint8_t*)Word)[2];
90        ((uint8_t*)Word)[2] = ((uint8_t*)Word)[5];
91        ((uint8_t*)Word)[5] = byteCache;
92        byteCache = ((uint8_t*)Word)[3];
93        ((uint8_t*)Word)[3] = ((uint8_t*)Word)[4];
94        ((uint8_t*)Word)[4] = byteCache;
95    }
96    
97    inline void swapBytes(void* Word, uint64_t WordSize) {
98        uint8_t byteCache;
99        uint64_t lo = 0, hi = WordSize - 1;
100        for (; lo < hi; hi--, lo++) {
101            byteCache = *((uint8_t*) Word + lo);
102            *((uint8_t*) Word + lo) = *((uint8_t*) Word + hi);
103            *((uint8_t*) Word + hi) = byteCache;
104        }
105    }
106    
107  /**  /**
108   * Stores a 16 bit integer in memory using little-endian format.   * Stores a 16 bit integer in memory using little-endian format.
109   *   *
# Line 72  inline void store32(uint8_t* pData, uint Line 129  inline void store32(uint8_t* pData, uint
129  }  }
130    
131  /**  /**
132     * Loads a 32 bit integer in memory using little-endian format.
133     *
134     * @param pData - memory pointer
135     * @returns 32 bit data word
136     */
137    inline uint32_t load32(uint8_t* pData) {
138        return uint32_t(pData[0])       |
139               uint32_t(pData[1]) << 8  |
140               uint32_t(pData[2]) << 16 |
141               uint32_t(pData[3]) << 24;
142    }
143    
144    /**
145   * Swaps the order of the data words in the given memory area   * Swaps the order of the data words in the given memory area
146   * with a granularity given by \a WordSize.   * with a granularity given by \a WordSize.
147   *   *
# Line 80  inline void store32(uint8_t* pData, uint Line 150  inline void store32(uint8_t* pData, uint
150   * @param WordSize - size of the data words (in bytes)   * @param WordSize - size of the data words (in bytes)
151   */   */
152  inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) {  inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) {
153        if (!AreaSize) return; // AreaSize==0 would cause a segfault here
154      switch (WordSize) { // TODO: unefficient      switch (WordSize) { // TODO: unefficient
155          case 1: {          case 1: {
156              uint8_t* pDst = (uint8_t*) pData;              uint8_t* pDst = (uint8_t*) pData;
# Line 122  inline void SwapMemoryArea(void* pData, Line 193  inline void SwapMemoryArea(void* pData,
193                  memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);                  memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);
194                  memcpy((uint8_t*) pData + hi, pCache, WordSize);                  memcpy((uint8_t*) pData + hi, pCache, WordSize);
195              }              }
196              delete[] pCache;              if (pCache) delete[] pCache;
197              break;              break;
198          }          }
199      }      }
# Line 135  inline void SwapMemoryArea(void* pData, Line 206  inline void SwapMemoryArea(void* pData,
206  inline void LoadString(RIFF::Chunk* ck, std::string& s) {  inline void LoadString(RIFF::Chunk* ck, std::string& s) {
207      if (ck) {      if (ck) {
208          const char* str = (char*)ck->LoadChunkData();          const char* str = (char*)ck->LoadChunkData();
209          int size = ck->GetSize();          if (!str) {
210                ck->ReleaseChunkData();
211                s = "";
212                return;
213            }
214            int size = (int) ck->GetSize();
215          int len;          int len;
216          for (len = 0 ; len < size ; len++)          for (len = 0 ; len < size ; len++)
217              if (str[len] == '\0') break;              if (str[len] == '\0') break;
# Line 164  inline void LoadString(RIFF::Chunk* ck, Line 240  inline void LoadString(RIFF::Chunk* ck,
240   */   */
241  inline void SaveString(uint32_t ChunkID, RIFF::Chunk* ck, RIFF::List* lstINFO, const std::string& s, const std::string& sDefault, bool bUseFixedLengthStrings, int size) {  inline void SaveString(uint32_t ChunkID, RIFF::Chunk* ck, RIFF::List* lstINFO, const std::string& s, const std::string& sDefault, bool bUseFixedLengthStrings, int size) {
242      if (ck) { // if chunk exists already, use 's' as value      if (ck) { // if chunk exists already, use 's' as value
243          if (!bUseFixedLengthStrings) size = s.size() + 1;          if (!bUseFixedLengthStrings) size = (int) s.size() + 1;
244          ck->Resize(size);          ck->Resize(size);
245          char* pData = (char*) ck->LoadChunkData();          char* pData = (char*) ck->LoadChunkData();
246          strncpy(pData, s.c_str(), size);          strncpy(pData, s.c_str(), size);
247      } else if (s != "" || sDefault != "") { // create chunk      } else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk
248          const std::string& sToSave = (s != "") ? s : sDefault;          const std::string& sToSave = (s != "") ? s : sDefault;
249          if (!bUseFixedLengthStrings) size = sToSave.size() + 1;          if (!bUseFixedLengthStrings) size = (int) sToSave.size() + 1;
250          ck = lstINFO->AddSubChunk(ChunkID, size);          ck = lstINFO->AddSubChunk(ChunkID, size);
251          char* pData = (char*) ck->LoadChunkData();          char* pData = (char*) ck->LoadChunkData();
252          strncpy(pData, sToSave.c_str(), size);          strncpy(pData, sToSave.c_str(), size);
253      }      }
254  }  }
255    
256    // private helper function to convert progress of a subprocess into the global progress
257    inline void __notify_progress(RIFF::progress_t* pProgress, float subprogress) {
258        if (pProgress && pProgress->callback) {
259            const float totalrange    = pProgress->__range_max - pProgress->__range_min;
260            const float totalprogress = pProgress->__range_min + subprogress * totalrange;
261            pProgress->factor         = totalprogress;
262            pProgress->callback(pProgress); // now actually notify about the progress
263        }
264    }
265    
266    // private helper function to divide a progress into subprogresses
267    inline void __divide_progress(RIFF::progress_t* pParentProgress, RIFF::progress_t* pSubProgress, float totalTasks, float currentTask) {
268        if (pParentProgress && pParentProgress->callback) {
269            const float totalrange    = pParentProgress->__range_max - pParentProgress->__range_min;
270            pSubProgress->callback    = pParentProgress->callback;
271            pSubProgress->custom      = pParentProgress->custom;
272            pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks;
273            pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks;
274        }
275    }
276    
277  #endif // __LIBGIG_HELPER_H__  #endif // __LIBGIG_HELPER_H__

Legend:
Removed from v.1179  
changed lines
  Added in v.3348

  ViewVC Help
Powered by ViewVC