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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3200 - (show annotations) (download) (as text)
Sun May 21 17:19:20 2017 UTC (6 years, 10 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 10456 byte(s)
- Disabled HAVE_VASPRINTF macro check for Windows due
  to a cross compiler issue.

1 /***************************************************************************
2 * *
3 * libgig - C++ cross-platform Gigasampler format file access library *
4 * *
5 * Copyright (C) 2003-2017 by Christian Schoenebeck *
6 * <cuse@users.sourceforge.net> *
7 * *
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 ***************************************************************************/
23
24 #ifndef __LIBGIG_HELPER_H__
25 #define __LIBGIG_HELPER_H__
26
27 #include <string.h>
28 #include <string>
29 #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"
44
45 // *************** Helper Functions **************
46 // *
47
48 template<class T> inline std::string ToString(T o) {
49 std::stringstream ss;
50 ss << o;
51 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) {
60 return (A > B) ? B : A;
61 }
62
63 inline long Abs(long val) {
64 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.
109 *
110 * @param pData - memory pointer
111 * @param data - integer to be stored
112 */
113 inline void store16(uint8_t* pData, uint16_t data) {
114 pData[0] = data;
115 pData[1] = data >> 8;
116 }
117
118 /**
119 * Stores a 32 bit integer in memory using little-endian format.
120 *
121 * @param pData - memory pointer
122 * @param data - integer to be stored
123 */
124 inline void store32(uint8_t* pData, uint32_t data) {
125 pData[0] = data;
126 pData[1] = data >> 8;
127 pData[2] = data >> 16;
128 pData[3] = data >> 24;
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
146 * with a granularity given by \a WordSize.
147 *
148 * @param pData - pointer to the memory area to be swapped
149 * @param AreaSize - size of the memory area to be swapped (in bytes)
150 * @param WordSize - size of the data words (in bytes)
151 */
152 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
155 case 1: {
156 uint8_t* pDst = (uint8_t*) pData;
157 uint8_t cache;
158 unsigned long lo = 0, hi = AreaSize - 1;
159 for (; lo < hi; hi--, lo++) {
160 cache = pDst[lo];
161 pDst[lo] = pDst[hi];
162 pDst[hi] = cache;
163 }
164 break;
165 }
166 case 2: {
167 uint16_t* pDst = (uint16_t*) pData;
168 uint16_t cache;
169 unsigned long lo = 0, hi = (AreaSize >> 1) - 1;
170 for (; lo < hi; hi--, lo++) {
171 cache = pDst[lo];
172 pDst[lo] = pDst[hi];
173 pDst[hi] = cache;
174 }
175 break;
176 }
177 case 4: {
178 uint32_t* pDst = (uint32_t*) pData;
179 uint32_t cache;
180 unsigned long lo = 0, hi = (AreaSize >> 2) - 1;
181 for (; lo < hi; hi--, lo++) {
182 cache = pDst[lo];
183 pDst[lo] = pDst[hi];
184 pDst[hi] = cache;
185 }
186 break;
187 }
188 default: {
189 uint8_t* pCache = new uint8_t[WordSize]; // TODO: unefficient
190 unsigned long lo = 0, hi = AreaSize - WordSize;
191 for (; lo < hi; hi -= WordSize, lo += WordSize) {
192 memcpy(pCache, (uint8_t*) pData + lo, WordSize);
193 memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize);
194 memcpy((uint8_t*) pData + hi, pCache, WordSize);
195 }
196 if (pCache) delete[] pCache;
197 break;
198 }
199 }
200 }
201
202 /** @brief Load given info field (string).
203 *
204 * Load info field string from given info chunk (\a ck) and save value to \a s.
205 */
206 inline void LoadString(RIFF::Chunk* ck, std::string& s) {
207 if (ck) {
208 const char* str = (char*)ck->LoadChunkData();
209 int size = (int) ck->GetSize();
210 int len;
211 for (len = 0 ; len < size ; len++)
212 if (str[len] == '\0') break;
213 s.assign(str, len);
214 ck->ReleaseChunkData();
215 }
216 }
217
218 /** @brief Apply given INFO field to the respective chunk.
219 *
220 * Apply given info value string to given info chunk, which is a
221 * subchunk of INFO list chunk \a lstINFO. If the given chunk already
222 * exists, value \a s will be applied. Otherwise if it doesn't exist yet
223 * and either \a s or \a sDefault is not an empty string, such a chunk
224 * will be created and either \a s or \a sDefault will be applied
225 * (depending on which one is not an empty string, if both are not an
226 * empty string \a s will be preferred).
227 *
228 * @param ChunkID - 32 bit RIFF chunk ID of INFO subchunk (only used in case \a ck is NULL)
229 * @param ck - INFO (sub)chunk where string should be stored to
230 * @param lstINFO - parent (INFO) RIFF list chunk
231 * @param s - current value of info field
232 * @param sDefault - default value
233 * @param bUseFixedLengthStrings - should a specific string size be forced in the chunk?
234 * @param size - wanted size of the INFO chunk. This is ignored if bUseFixedLengthStrings is false.
235 */
236 inline void SaveString(uint32_t ChunkID, RIFF::Chunk* ck, RIFF::List* lstINFO, const std::string& s, const std::string& sDefault, bool bUseFixedLengthStrings, int size) {
237 if (ck) { // if chunk exists already, use 's' as value
238 if (!bUseFixedLengthStrings) size = (int) s.size() + 1;
239 ck->Resize(size);
240 char* pData = (char*) ck->LoadChunkData();
241 strncpy(pData, s.c_str(), size);
242 } else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk
243 const std::string& sToSave = (s != "") ? s : sDefault;
244 if (!bUseFixedLengthStrings) size = (int) sToSave.size() + 1;
245 ck = lstINFO->AddSubChunk(ChunkID, size);
246 char* pData = (char*) ck->LoadChunkData();
247 strncpy(pData, sToSave.c_str(), size);
248 }
249 }
250
251 // private helper function to convert progress of a subprocess into the global progress
252 inline void __notify_progress(RIFF::progress_t* pProgress, float subprogress) {
253 if (pProgress && pProgress->callback) {
254 const float totalrange = pProgress->__range_max - pProgress->__range_min;
255 const float totalprogress = pProgress->__range_min + subprogress * totalrange;
256 pProgress->factor = totalprogress;
257 pProgress->callback(pProgress); // now actually notify about the progress
258 }
259 }
260
261 // private helper function to divide a progress into subprogresses
262 inline void __divide_progress(RIFF::progress_t* pParentProgress, RIFF::progress_t* pSubProgress, float totalTasks, float currentTask) {
263 if (pParentProgress && pParentProgress->callback) {
264 const float totalrange = pParentProgress->__range_max - pParentProgress->__range_min;
265 pSubProgress->callback = pParentProgress->callback;
266 pSubProgress->custom = pParentProgress->custom;
267 pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks;
268 pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks;
269 }
270 }
271
272 #endif // __LIBGIG_HELPER_H__

  ViewVC Help
Powered by ViewVC