/[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 803 by schoenebeck, Sat Nov 12 12:36:49 2005 UTC revision 2682 by schoenebeck, Mon Dec 29 16:25:51 2014 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file loader library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2005 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2014 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 24  Line 24 
24  #ifndef __LIBGIG_HELPER_H__  #ifndef __LIBGIG_HELPER_H__
25  #define __LIBGIG_HELPER_H__  #define __LIBGIG_HELPER_H__
26    
27    #include <string.h>
28  #include <string>  #include <string>
29  #include <sstream>  #include <sstream>
30    
31    #if defined(WIN32) && !HAVE_CONFIG_H
32    # include "../win32/libgig_private.h" // like config.h, automatically generated by Dev-C++
33    # define PACKAGE "libgig"
34    # define VERSION VER_STRING // VER_STRING defined in libgig_private.h
35    #endif // WIN32
36    
37    #include "RIFF.h"
38    
39  // *************** Helper Functions **************  // *************** Helper Functions **************
40  // *  // *
41    
# Line 36  template<class T> inline std::string ToS Line 45  template<class T> inline std::string ToS
45      return ss.str();      return ss.str();
46  }  }
47    
48    inline long Min(long A, long B) {
49        return (A > B) ? B : A;
50    }
51    
52    inline long Abs(long val) {
53        return (val > 0) ? val : -val;
54    }
55    
56    /**
57     * Stores a 16 bit integer in memory using little-endian format.
58     *
59     * @param pData - memory pointer
60     * @param data  - integer to be stored
61     */
62    inline void store16(uint8_t* pData, uint16_t data) {
63        pData[0] = data;
64        pData[1] = data >> 8;
65    }
66    
67    /**
68     * Stores a 32 bit integer in memory using little-endian format.
69     *
70     * @param pData - memory pointer
71     * @param data  - integer to be stored
72     */
73    inline void store32(uint8_t* pData, uint32_t data) {
74        pData[0] = data;
75        pData[1] = data >> 8;
76        pData[2] = data >> 16;
77        pData[3] = data >> 24;
78    }
79    
80    /**
81     * Swaps the order of the data words in the given memory area
82     * with a granularity given by \a WordSize.
83     *
84     * @param pData    - pointer to the memory area to be swapped
85     * @param AreaSize - size of the memory area to be swapped (in bytes)
86     * @param WordSize - size of the data words (in bytes)
87     */
88    inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) {
89        if (!AreaSize) return; // AreaSize==0 would cause a segfault here
90        switch (WordSize) { // TODO: unefficient
91            case 1: {
92                uint8_t* pDst = (uint8_t*) pData;
93                uint8_t  cache;
94                unsigned long lo = 0, hi = AreaSize - 1;
95                for (; lo < hi; hi--, lo++) {
96                    cache    = pDst[lo];
97                    pDst[lo] = pDst[hi];
98                    pDst[hi] = cache;
99                }
100                break;
101            }
102            case 2: {
103                uint16_t* pDst = (uint16_t*) pData;
104                uint16_t  cache;
105                unsigned long lo = 0, hi = (AreaSize >> 1) - 1;
106                for (; lo < hi; hi--, lo++) {
107                    cache    = pDst[lo];
108                    pDst[lo] = pDst[hi];
109                    pDst[hi] = cache;
110                }
111                break;
112            }
113            case 4: {
114                uint32_t* pDst = (uint32_t*) pData;
115                uint32_t  cache;
116                unsigned long lo = 0, hi = (AreaSize >> 2) - 1;
117                for (; lo < hi; hi--, lo++) {
118                    cache    = pDst[lo];
119                    pDst[lo] = pDst[hi];
120                    pDst[hi] = cache;
121                }
122                break;
123            }
124            default: {
125                uint8_t* pCache = new uint8_t[WordSize]; // TODO: unefficient
126                unsigned long lo = 0, hi = AreaSize - WordSize;
127                for (; lo < hi; hi -= WordSize, lo += WordSize) {
128                    memcpy(pCache, (uint8_t*) pData + lo, WordSize);
129                    memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);
130                    memcpy((uint8_t*) pData + hi, pCache, WordSize);
131                }
132                if (pCache) delete[] pCache;
133                break;
134            }
135        }
136    }
137    
138    /** @brief Load given info field (string).
139     *
140     * Load info field string from given info chunk (\a ck) and save value to \a s.
141     */
142    inline void LoadString(RIFF::Chunk* ck, std::string& s) {
143        if (ck) {
144            const char* str = (char*)ck->LoadChunkData();
145            int size = ck->GetSize();
146            int len;
147            for (len = 0 ; len < size ; len++)
148                if (str[len] == '\0') break;
149            s.assign(str, len);
150            ck->ReleaseChunkData();
151        }
152    }
153    
154    /** @brief Apply given INFO field to the respective chunk.
155     *
156     * Apply given info value string to given info chunk, which is a
157     * subchunk of INFO list chunk \a lstINFO. If the given chunk already
158     * exists, value \a s will be applied. Otherwise if it doesn't exist yet
159     * and either \a s or \a sDefault is not an empty string, such a chunk
160     * will be created and either \a s or \a sDefault will be applied
161     * (depending on which one is not an empty string, if both are not an
162     * empty string \a s will be preferred).
163     *
164     * @param ChunkID  - 32 bit RIFF chunk ID of INFO subchunk (only used in case \a ck is NULL)
165     * @param ck       - INFO (sub)chunk where string should be stored to
166     * @param lstINFO  - parent (INFO) RIFF list chunk
167     * @param s        - current value of info field
168     * @param sDefault - default value
169     * @param bUseFixedLengthStrings - should a specific string size be forced in the chunk?
170     * @param size     - wanted size of the INFO chunk. This is ignored if bUseFixedLengthStrings is false.
171     */
172    inline void SaveString(uint32_t ChunkID, RIFF::Chunk* ck, RIFF::List* lstINFO, const std::string& s, const std::string& sDefault, bool bUseFixedLengthStrings, int size) {
173        if (ck) { // if chunk exists already, use 's' as value
174            if (!bUseFixedLengthStrings) size = s.size() + 1;
175            ck->Resize(size);
176            char* pData = (char*) ck->LoadChunkData();
177            strncpy(pData, s.c_str(), size);
178        } else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk
179            const std::string& sToSave = (s != "") ? s : sDefault;
180            if (!bUseFixedLengthStrings) size = sToSave.size() + 1;
181            ck = lstINFO->AddSubChunk(ChunkID, size);
182            char* pData = (char*) ck->LoadChunkData();
183            strncpy(pData, sToSave.c_str(), size);
184        }
185    }
186    
187    // private helper function to convert progress of a subprocess into the global progress
188    inline void __notify_progress(RIFF::progress_t* pProgress, float subprogress) {
189        if (pProgress && pProgress->callback) {
190            const float totalrange    = pProgress->__range_max - pProgress->__range_min;
191            const float totalprogress = pProgress->__range_min + subprogress * totalrange;
192            pProgress->factor         = totalprogress;
193            pProgress->callback(pProgress); // now actually notify about the progress
194        }
195    }
196    
197    // private helper function to divide a progress into subprogresses
198    inline void __divide_progress(RIFF::progress_t* pParentProgress, RIFF::progress_t* pSubProgress, float totalTasks, float currentTask) {
199        if (pParentProgress && pParentProgress->callback) {
200            const float totalrange    = pParentProgress->__range_max - pParentProgress->__range_min;
201            pSubProgress->callback    = pParentProgress->callback;
202            pSubProgress->custom      = pParentProgress->custom;
203            pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks;
204            pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks;
205        }
206    }
207    
208  #endif // __LIBGIG_HELPER_H__  #endif // __LIBGIG_HELPER_H__

Legend:
Removed from v.803  
changed lines
  Added in v.2682

  ViewVC Help
Powered by ViewVC