/[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 1875 by schoenebeck, Thu Mar 26 13:32:59 2009 UTC revision 3474 by schoenebeck, Wed Feb 20 16:04:19 2019 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-2009 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2019 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  #if defined(WIN32) && !HAVE_CONFIG_H
33  # include "../win32/libgig_private.h" // like config.h, automatically generated by Dev-C++  # include "../win32/libgig_private.h" // like config.h, automatically generated by Dev-C++
# Line 34  Line 35 
35  # define VERSION VER_STRING // VER_STRING defined in libgig_private.h  # define VERSION VER_STRING // VER_STRING defined in libgig_private.h
36  #endif // WIN32  #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    
45  // *************** Helper Functions **************  // *************** Helper Functions **************
# Line 45  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    // Behaves as printf() just that it returns it as string instead of writing to stdout.
55    inline std::string strPrint(const char* fmt, ...) {
56        va_list args;
57        va_start(args, fmt);
58        char* buf = NULL;
59        vasprintf(&buf, fmt, args);
60        std::string res = buf;
61        if (buf) free(buf);
62        va_end(args);
63        return res;
64    }
65    
66    inline std::string toLowerCase(std::string s) {
67        std::transform(s.begin(), s.end(), s.begin(), ::tolower);
68        return s;
69    }
70    
71  inline long Min(long A, long B) {  inline long Min(long A, long B) {
72      return (A > B) ? B : A;      return (A > B) ? B : A;
73  }  }
# Line 53  inline long Abs(long val) { Line 76  inline long Abs(long val) {
76      return (val > 0) ? val : -val;      return (val > 0) ? val : -val;
77  }  }
78    
79    inline void swapBytes_16(void* Word) {
80        uint8_t byteCache = *((uint8_t*) Word);
81        *((uint8_t*) Word)     = *((uint8_t*) Word + 1);
82        *((uint8_t*) Word + 1) = byteCache;
83    }
84    
85    inline void swapBytes_32(void* Word) {
86        uint8_t byteCache = *((uint8_t*) Word);
87        *((uint8_t*) Word)     = *((uint8_t*) Word + 3);
88        *((uint8_t*) Word + 3) = byteCache;
89        byteCache = *((uint8_t*) Word + 1);
90        *((uint8_t*) Word + 1) = *((uint8_t*) Word + 2);
91        *((uint8_t*) Word + 2) = byteCache;
92    }
93    
94    inline void swapBytes_64(void* Word) {
95        uint8_t byteCache = ((uint8_t*)Word)[0];
96        ((uint8_t*)Word)[0] = ((uint8_t*)Word)[7];
97        ((uint8_t*)Word)[7] = byteCache;
98        byteCache = ((uint8_t*)Word)[1];
99        ((uint8_t*)Word)[1] = ((uint8_t*)Word)[6];
100        ((uint8_t*)Word)[6] = byteCache;
101        byteCache = ((uint8_t*)Word)[2];
102        ((uint8_t*)Word)[2] = ((uint8_t*)Word)[5];
103        ((uint8_t*)Word)[5] = byteCache;
104        byteCache = ((uint8_t*)Word)[3];
105        ((uint8_t*)Word)[3] = ((uint8_t*)Word)[4];
106        ((uint8_t*)Word)[4] = byteCache;
107    }
108    
109    inline void swapBytes(void* Word, uint64_t WordSize) {
110        uint8_t byteCache;
111        uint64_t lo = 0, hi = WordSize - 1;
112        for (; lo < hi; hi--, lo++) {
113            byteCache = *((uint8_t*) Word + lo);
114            *((uint8_t*) Word + lo) = *((uint8_t*) Word + hi);
115            *((uint8_t*) Word + hi) = byteCache;
116        }
117    }
118    
119  /**  /**
120   * Stores a 16 bit integer in memory using little-endian format.   * Stores a 16 bit integer in memory using little-endian format.
121   *   *
# Line 78  inline void store32(uint8_t* pData, uint Line 141  inline void store32(uint8_t* pData, uint
141  }  }
142    
143  /**  /**
144     * Loads a 16 bit integer in memory using little-endian format.
145     *
146     * @param pData - memory pointer
147     * @returns 16 bit data word
148     */
149    inline uint16_t load16(uint8_t* pData) {
150        return uint16_t(pData[0]) |
151               uint16_t(pData[1]) << 8;
152    }
153    
154    /**
155     * Loads a 32 bit integer in memory using little-endian format.
156     *
157     * @param pData - memory pointer
158     * @returns 32 bit data word
159     */
160    inline uint32_t load32(uint8_t* pData) {
161        return uint32_t(pData[0])       |
162               uint32_t(pData[1]) << 8  |
163               uint32_t(pData[2]) << 16 |
164               uint32_t(pData[3]) << 24;
165    }
166    
167    /**
168   * Swaps the order of the data words in the given memory area   * Swaps the order of the data words in the given memory area
169   * with a granularity given by \a WordSize.   * with a granularity given by \a WordSize.
170   *   *
# Line 142  inline void SwapMemoryArea(void* pData, Line 229  inline void SwapMemoryArea(void* pData,
229  inline void LoadString(RIFF::Chunk* ck, std::string& s) {  inline void LoadString(RIFF::Chunk* ck, std::string& s) {
230      if (ck) {      if (ck) {
231          const char* str = (char*)ck->LoadChunkData();          const char* str = (char*)ck->LoadChunkData();
232          int size = ck->GetSize();          if (!str) {
233                ck->ReleaseChunkData();
234                s = "";
235                return;
236            }
237            int size = (int) ck->GetSize();
238          int len;          int len;
239          for (len = 0 ; len < size ; len++)          for (len = 0 ; len < size ; len++)
240              if (str[len] == '\0') break;              if (str[len] == '\0') break;
# Line 171  inline void LoadString(RIFF::Chunk* ck, Line 263  inline void LoadString(RIFF::Chunk* ck,
263   */   */
264  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) {
265      if (ck) { // if chunk exists already, use 's' as value      if (ck) { // if chunk exists already, use 's' as value
266          if (!bUseFixedLengthStrings) size = s.size() + 1;          if (!bUseFixedLengthStrings) size = (int) s.size() + 1;
267          ck->Resize(size);          ck->Resize(size);
268          char* pData = (char*) ck->LoadChunkData();          char* pData = (char*) ck->LoadChunkData();
269          strncpy(pData, s.c_str(), size);          strncpy(pData, s.c_str(), size);
270      } else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk      } else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk
271          const std::string& sToSave = (s != "") ? s : sDefault;          const std::string& sToSave = (s != "") ? s : sDefault;
272          if (!bUseFixedLengthStrings) size = sToSave.size() + 1;          if (!bUseFixedLengthStrings) size = (int) sToSave.size() + 1;
273          ck = lstINFO->AddSubChunk(ChunkID, size);          ck = lstINFO->AddSubChunk(ChunkID, size);
274          char* pData = (char*) ck->LoadChunkData();          char* pData = (char*) ck->LoadChunkData();
275          strncpy(pData, sToSave.c_str(), size);          strncpy(pData, sToSave.c_str(), size);
276      }      }
277  }  }
278    
279    // private helper function to convert progress of a subprocess into the global progress
280    inline void __notify_progress(RIFF::progress_t* pProgress, float subprogress) {
281        if (pProgress && pProgress->callback) {
282            const float totalrange    = pProgress->__range_max - pProgress->__range_min;
283            const float totalprogress = pProgress->__range_min + subprogress * totalrange;
284            pProgress->factor         = totalprogress;
285            pProgress->callback(pProgress); // now actually notify about the progress
286        }
287    }
288    
289    // private helper function to divide a progress into subprogresses
290    inline void __divide_progress(RIFF::progress_t* pParentProgress, RIFF::progress_t* pSubProgress, float totalTasks, float currentTask) {
291        if (pParentProgress && pParentProgress->callback) {
292            const float totalrange    = pParentProgress->__range_max - pParentProgress->__range_min;
293            pSubProgress->callback    = pParentProgress->callback;
294            pSubProgress->custom      = pParentProgress->custom;
295            pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks;
296            pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks;
297        }
298    }
299    
300    #ifdef _WIN32
301    # define NATIVE_PATH_SEPARATOR '\\'
302    #else
303    # define NATIVE_PATH_SEPARATOR '/'
304    #endif
305    
306    /**
307     * Returns the owning path of the given path (its parent path). So for example
308     * passing "/some/path" would return "/some".
309     */
310    inline std::string parentPath(const std::string path) {
311        std::size_t pos = path.find_last_of(NATIVE_PATH_SEPARATOR);
312        return (pos == std::string::npos) ? path : path.substr(0, pos);
313    }
314    
315    /**
316     * Returns the last (lowest) portion of the given path. So for example passing
317     * "/some/path" would return "path".
318     */
319    inline std::string lastPathComponent(const std::string path) {
320        std::size_t pos = path.find_last_of(NATIVE_PATH_SEPARATOR);
321        return (pos == std::string::npos) ? path : path.substr(pos+1);
322    }
323    
324    /**
325     * Returns the given path with the type extension being stripped from its end.
326     * So for example passing "/some/path.foo" would return "/some/path".
327     */
328    inline std::string pathWithoutExtension(const std::string path) {
329        std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR);
330        std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1;
331        std::size_t posDot = path.find_last_of(".", posBase);
332        return (posDot != std::string::npos && posDot > posBase)
333               ? path.substr(0, posDot) : path;
334    }
335    
336    /**
337     * Returns the type extension of the given path. So for example passing
338     * "/some/path.foo" would return "foo".
339     */
340    inline std::string extensionOfPath(const std::string path) {
341        std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR);
342        std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1;
343        std::size_t posDot = path.find_last_of(".", posBase);
344        return (posDot != std::string::npos && posDot > posBase)
345               ? path.substr(posDot+1) : "";
346    }
347    
348    /**
349     * Combines the two given paths with each other. So for example passing
350     * "/some/path" and "/another/one" would return "/some/path/another/one".
351     */
352    inline std::string concatPath(const std::string path1, const std::string path2) {
353        return (!path1.empty() && *(path1.rbegin()) != NATIVE_PATH_SEPARATOR &&
354                !path2.empty() && *(path2.begin())  != NATIVE_PATH_SEPARATOR)
355            ? path1 + NATIVE_PATH_SEPARATOR + path2
356            : path1 + path2;
357    }
358    
359  #endif // __LIBGIG_HELPER_H__  #endif // __LIBGIG_HELPER_H__

Legend:
Removed from v.1875  
changed lines
  Added in v.3474

  ViewVC Help
Powered by ViewVC