1 |
schoenebeck |
803 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
schoenebeck |
933 |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
schoenebeck |
803 |
* * |
5 |
schoenebeck |
3474 |
* Copyright (C) 2003-2019 by Christian Schoenebeck * |
6 |
schoenebeck |
803 |
* <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 |
schoenebeck |
809 |
#include <string.h> |
28 |
schoenebeck |
803 |
#include <string> |
29 |
|
|
#include <sstream> |
30 |
schoenebeck |
3185 |
#include <algorithm> |
31 |
schoenebeck |
803 |
|
32 |
persson |
1330 |
#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 |
schoenebeck |
3200 |
#if HAVE_CONFIG_H /*&& !HAVE_VASPRINTF*/ && defined(WIN32) |
39 |
schoenebeck |
3199 |
# include <stdarg.h> |
40 |
|
|
int vasprintf(char** ret, const char* format, va_list arg); |
41 |
|
|
#endif |
42 |
|
|
|
43 |
schoenebeck |
929 |
#include "RIFF.h" |
44 |
|
|
|
45 |
schoenebeck |
803 |
// *************** 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 |
schoenebeck |
3474 |
// 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 |
schoenebeck |
3185 |
inline std::string toLowerCase(std::string s) { |
67 |
|
|
std::transform(s.begin(), s.end(), s.begin(), ::tolower); |
68 |
|
|
return s; |
69 |
|
|
} |
70 |
|
|
|
71 |
schoenebeck |
809 |
inline long Min(long A, long B) { |
72 |
|
|
return (A > B) ? B : A; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
inline long Abs(long val) { |
76 |
|
|
return (val > 0) ? val : -val; |
77 |
|
|
} |
78 |
|
|
|
79 |
schoenebeck |
2912 |
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 |
schoenebeck |
809 |
/** |
120 |
persson |
1179 |
* Stores a 16 bit integer in memory using little-endian format. |
121 |
|
|
* |
122 |
|
|
* @param pData - memory pointer |
123 |
|
|
* @param data - integer to be stored |
124 |
|
|
*/ |
125 |
|
|
inline void store16(uint8_t* pData, uint16_t data) { |
126 |
|
|
pData[0] = data; |
127 |
|
|
pData[1] = data >> 8; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/** |
131 |
|
|
* Stores a 32 bit integer in memory using little-endian format. |
132 |
|
|
* |
133 |
|
|
* @param pData - memory pointer |
134 |
|
|
* @param data - integer to be stored |
135 |
|
|
*/ |
136 |
|
|
inline void store32(uint8_t* pData, uint32_t data) { |
137 |
|
|
pData[0] = data; |
138 |
|
|
pData[1] = data >> 8; |
139 |
|
|
pData[2] = data >> 16; |
140 |
|
|
pData[3] = data >> 24; |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
/** |
144 |
schoenebeck |
3474 |
* 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 |
schoenebeck |
2912 |
* 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 |
schoenebeck |
809 |
* Swaps the order of the data words in the given memory area |
169 |
|
|
* with a granularity given by \a WordSize. |
170 |
|
|
* |
171 |
|
|
* @param pData - pointer to the memory area to be swapped |
172 |
|
|
* @param AreaSize - size of the memory area to be swapped (in bytes) |
173 |
|
|
* @param WordSize - size of the data words (in bytes) |
174 |
|
|
*/ |
175 |
|
|
inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) { |
176 |
schoenebeck |
1875 |
if (!AreaSize) return; // AreaSize==0 would cause a segfault here |
177 |
schoenebeck |
809 |
switch (WordSize) { // TODO: unefficient |
178 |
|
|
case 1: { |
179 |
|
|
uint8_t* pDst = (uint8_t*) pData; |
180 |
|
|
uint8_t cache; |
181 |
|
|
unsigned long lo = 0, hi = AreaSize - 1; |
182 |
|
|
for (; lo < hi; hi--, lo++) { |
183 |
|
|
cache = pDst[lo]; |
184 |
|
|
pDst[lo] = pDst[hi]; |
185 |
|
|
pDst[hi] = cache; |
186 |
|
|
} |
187 |
|
|
break; |
188 |
|
|
} |
189 |
|
|
case 2: { |
190 |
|
|
uint16_t* pDst = (uint16_t*) pData; |
191 |
|
|
uint16_t cache; |
192 |
|
|
unsigned long lo = 0, hi = (AreaSize >> 1) - 1; |
193 |
|
|
for (; lo < hi; hi--, lo++) { |
194 |
|
|
cache = pDst[lo]; |
195 |
|
|
pDst[lo] = pDst[hi]; |
196 |
|
|
pDst[hi] = cache; |
197 |
|
|
} |
198 |
|
|
break; |
199 |
|
|
} |
200 |
|
|
case 4: { |
201 |
|
|
uint32_t* pDst = (uint32_t*) pData; |
202 |
|
|
uint32_t cache; |
203 |
|
|
unsigned long lo = 0, hi = (AreaSize >> 2) - 1; |
204 |
|
|
for (; lo < hi; hi--, lo++) { |
205 |
|
|
cache = pDst[lo]; |
206 |
|
|
pDst[lo] = pDst[hi]; |
207 |
|
|
pDst[hi] = cache; |
208 |
|
|
} |
209 |
|
|
break; |
210 |
|
|
} |
211 |
|
|
default: { |
212 |
|
|
uint8_t* pCache = new uint8_t[WordSize]; // TODO: unefficient |
213 |
|
|
unsigned long lo = 0, hi = AreaSize - WordSize; |
214 |
|
|
for (; lo < hi; hi -= WordSize, lo += WordSize) { |
215 |
|
|
memcpy(pCache, (uint8_t*) pData + lo, WordSize); |
216 |
|
|
memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize); |
217 |
|
|
memcpy((uint8_t*) pData + hi, pCache, WordSize); |
218 |
|
|
} |
219 |
schoenebeck |
1875 |
if (pCache) delete[] pCache; |
220 |
schoenebeck |
809 |
break; |
221 |
|
|
} |
222 |
|
|
} |
223 |
|
|
} |
224 |
|
|
|
225 |
schoenebeck |
929 |
/** @brief Load given info field (string). |
226 |
|
|
* |
227 |
|
|
* Load info field string from given info chunk (\a ck) and save value to \a s. |
228 |
|
|
*/ |
229 |
|
|
inline void LoadString(RIFF::Chunk* ck, std::string& s) { |
230 |
|
|
if (ck) { |
231 |
|
|
const char* str = (char*)ck->LoadChunkData(); |
232 |
schoenebeck |
3348 |
if (!str) { |
233 |
|
|
ck->ReleaseChunkData(); |
234 |
|
|
s = ""; |
235 |
|
|
return; |
236 |
|
|
} |
237 |
schoenebeck |
3053 |
int size = (int) ck->GetSize(); |
238 |
schoenebeck |
929 |
int len; |
239 |
|
|
for (len = 0 ; len < size ; len++) |
240 |
|
|
if (str[len] == '\0') break; |
241 |
|
|
s.assign(str, len); |
242 |
|
|
ck->ReleaseChunkData(); |
243 |
|
|
} |
244 |
|
|
} |
245 |
|
|
|
246 |
|
|
/** @brief Apply given INFO field to the respective chunk. |
247 |
|
|
* |
248 |
|
|
* Apply given info value string to given info chunk, which is a |
249 |
|
|
* subchunk of INFO list chunk \a lstINFO. If the given chunk already |
250 |
|
|
* exists, value \a s will be applied. Otherwise if it doesn't exist yet |
251 |
|
|
* and either \a s or \a sDefault is not an empty string, such a chunk |
252 |
|
|
* will be created and either \a s or \a sDefault will be applied |
253 |
|
|
* (depending on which one is not an empty string, if both are not an |
254 |
|
|
* empty string \a s will be preferred). |
255 |
|
|
* |
256 |
|
|
* @param ChunkID - 32 bit RIFF chunk ID of INFO subchunk (only used in case \a ck is NULL) |
257 |
|
|
* @param ck - INFO (sub)chunk where string should be stored to |
258 |
|
|
* @param lstINFO - parent (INFO) RIFF list chunk |
259 |
|
|
* @param s - current value of info field |
260 |
|
|
* @param sDefault - default value |
261 |
|
|
* @param bUseFixedLengthStrings - should a specific string size be forced in the chunk? |
262 |
|
|
* @param size - wanted size of the INFO chunk. This is ignored if bUseFixedLengthStrings is false. |
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) { |
265 |
|
|
if (ck) { // if chunk exists already, use 's' as value |
266 |
schoenebeck |
3053 |
if (!bUseFixedLengthStrings) size = (int) s.size() + 1; |
267 |
schoenebeck |
929 |
ck->Resize(size); |
268 |
|
|
char* pData = (char*) ck->LoadChunkData(); |
269 |
|
|
strncpy(pData, s.c_str(), size); |
270 |
persson |
1180 |
} else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk |
271 |
schoenebeck |
929 |
const std::string& sToSave = (s != "") ? s : sDefault; |
272 |
schoenebeck |
3053 |
if (!bUseFixedLengthStrings) size = (int) sToSave.size() + 1; |
273 |
schoenebeck |
929 |
ck = lstINFO->AddSubChunk(ChunkID, size); |
274 |
|
|
char* pData = (char*) ck->LoadChunkData(); |
275 |
|
|
strncpy(pData, sToSave.c_str(), size); |
276 |
|
|
} |
277 |
|
|
} |
278 |
|
|
|
279 |
schoenebeck |
2682 |
// 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 |
schoenebeck |
3474 |
#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 |
schoenebeck |
803 |
#endif // __LIBGIG_HELPER_H__ |