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 * |
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 && !defined(_MSC_VER) |
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++ |
34 |
# define PACKAGE "libgig" |
# define PACKAGE "libgig" |
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)) || defined(_MSC_VER) |
39 |
|
# include <stdarg.h> |
40 |
|
int vasprintf(char** ret, const char* format, va_list arg); |
41 |
|
#endif |
42 |
|
|
43 |
|
#if defined(_MSC_VER) |
44 |
|
# if _MSC_VER < 1900 |
45 |
|
# error versions prior to msvc 2015 have not been tested |
46 |
|
# else |
47 |
|
# include <BaseTsd.h> |
48 |
|
typedef SSIZE_T ssize_t; |
49 |
|
# endif |
50 |
|
#endif |
51 |
|
|
52 |
#include "RIFF.h" |
#include "RIFF.h" |
53 |
|
|
54 |
// *************** Helper Functions ************** |
// *************** Helper Functions ************** |
60 |
return ss.str(); |
return ss.str(); |
61 |
} |
} |
62 |
|
|
63 |
|
// Behaves as printf() just that it returns it as string instead of writing to stdout. |
64 |
|
inline std::string strPrint(const char* fmt, ...) { |
65 |
|
va_list args; |
66 |
|
va_start(args, fmt); |
67 |
|
char* buf = NULL; |
68 |
|
vasprintf(&buf, fmt, args); |
69 |
|
std::string res = buf; |
70 |
|
if (buf) free(buf); |
71 |
|
va_end(args); |
72 |
|
return res; |
73 |
|
} |
74 |
|
|
75 |
|
inline std::string toLowerCase(std::string s) { |
76 |
|
std::transform(s.begin(), s.end(), s.begin(), ::tolower); |
77 |
|
return s; |
78 |
|
} |
79 |
|
|
80 |
inline long Min(long A, long B) { |
inline long Min(long A, long B) { |
81 |
return (A > B) ? B : A; |
return (A > B) ? B : A; |
82 |
} |
} |
85 |
return (val > 0) ? val : -val; |
return (val > 0) ? val : -val; |
86 |
} |
} |
87 |
|
|
88 |
|
inline void swapBytes_16(void* Word) { |
89 |
|
uint8_t byteCache = *((uint8_t*) Word); |
90 |
|
*((uint8_t*) Word) = *((uint8_t*) Word + 1); |
91 |
|
*((uint8_t*) Word + 1) = byteCache; |
92 |
|
} |
93 |
|
|
94 |
|
inline void swapBytes_32(void* Word) { |
95 |
|
uint8_t byteCache = *((uint8_t*) Word); |
96 |
|
*((uint8_t*) Word) = *((uint8_t*) Word + 3); |
97 |
|
*((uint8_t*) Word + 3) = byteCache; |
98 |
|
byteCache = *((uint8_t*) Word + 1); |
99 |
|
*((uint8_t*) Word + 1) = *((uint8_t*) Word + 2); |
100 |
|
*((uint8_t*) Word + 2) = byteCache; |
101 |
|
} |
102 |
|
|
103 |
|
inline void swapBytes_64(void* Word) { |
104 |
|
uint8_t byteCache = ((uint8_t*)Word)[0]; |
105 |
|
((uint8_t*)Word)[0] = ((uint8_t*)Word)[7]; |
106 |
|
((uint8_t*)Word)[7] = byteCache; |
107 |
|
byteCache = ((uint8_t*)Word)[1]; |
108 |
|
((uint8_t*)Word)[1] = ((uint8_t*)Word)[6]; |
109 |
|
((uint8_t*)Word)[6] = byteCache; |
110 |
|
byteCache = ((uint8_t*)Word)[2]; |
111 |
|
((uint8_t*)Word)[2] = ((uint8_t*)Word)[5]; |
112 |
|
((uint8_t*)Word)[5] = byteCache; |
113 |
|
byteCache = ((uint8_t*)Word)[3]; |
114 |
|
((uint8_t*)Word)[3] = ((uint8_t*)Word)[4]; |
115 |
|
((uint8_t*)Word)[4] = byteCache; |
116 |
|
} |
117 |
|
|
118 |
|
inline void swapBytes(void* Word, uint64_t WordSize) { |
119 |
|
uint8_t byteCache; |
120 |
|
uint64_t lo = 0, hi = WordSize - 1; |
121 |
|
for (; lo < hi; hi--, lo++) { |
122 |
|
byteCache = *((uint8_t*) Word + lo); |
123 |
|
*((uint8_t*) Word + lo) = *((uint8_t*) Word + hi); |
124 |
|
*((uint8_t*) Word + hi) = byteCache; |
125 |
|
} |
126 |
|
} |
127 |
|
|
128 |
/** |
/** |
129 |
* Stores a 16 bit integer in memory using little-endian format. |
* Stores a 16 bit integer in memory using little-endian format. |
130 |
* |
* |
150 |
} |
} |
151 |
|
|
152 |
/** |
/** |
153 |
|
* Loads a 16 bit integer in memory using little-endian format. |
154 |
|
* |
155 |
|
* @param pData - memory pointer |
156 |
|
* @returns 16 bit data word |
157 |
|
*/ |
158 |
|
inline uint16_t load16(uint8_t* pData) { |
159 |
|
return uint16_t(pData[0]) | |
160 |
|
uint16_t(pData[1]) << 8; |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
|
* Loads a 32 bit integer in memory using little-endian format. |
165 |
|
* |
166 |
|
* @param pData - memory pointer |
167 |
|
* @returns 32 bit data word |
168 |
|
*/ |
169 |
|
inline uint32_t load32(uint8_t* pData) { |
170 |
|
return uint32_t(pData[0]) | |
171 |
|
uint32_t(pData[1]) << 8 | |
172 |
|
uint32_t(pData[2]) << 16 | |
173 |
|
uint32_t(pData[3]) << 24; |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
* Swaps the order of the data words in the given memory area |
* Swaps the order of the data words in the given memory area |
178 |
* with a granularity given by \a WordSize. |
* with a granularity given by \a WordSize. |
179 |
* |
* |
238 |
inline void LoadString(RIFF::Chunk* ck, std::string& s) { |
inline void LoadString(RIFF::Chunk* ck, std::string& s) { |
239 |
if (ck) { |
if (ck) { |
240 |
const char* str = (char*)ck->LoadChunkData(); |
const char* str = (char*)ck->LoadChunkData(); |
241 |
int size = ck->GetSize(); |
if (!str) { |
242 |
|
ck->ReleaseChunkData(); |
243 |
|
s = ""; |
244 |
|
return; |
245 |
|
} |
246 |
|
int size = (int) ck->GetSize(); |
247 |
int len; |
int len; |
248 |
for (len = 0 ; len < size ; len++) |
for (len = 0 ; len < size ; len++) |
249 |
if (str[len] == '\0') break; |
if (str[len] == '\0') break; |
272 |
*/ |
*/ |
273 |
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) { |
274 |
if (ck) { // if chunk exists already, use 's' as value |
if (ck) { // if chunk exists already, use 's' as value |
275 |
if (!bUseFixedLengthStrings) size = s.size() + 1; |
if (!bUseFixedLengthStrings) size = (int) s.size() + 1; |
276 |
ck->Resize(size); |
ck->Resize(size); |
277 |
char* pData = (char*) ck->LoadChunkData(); |
char* pData = (char*) ck->LoadChunkData(); |
278 |
strncpy(pData, s.c_str(), size); |
strncpy(pData, s.c_str(), size); |
279 |
} else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk |
} else if (s != "" || sDefault != "" || bUseFixedLengthStrings) { // create chunk |
280 |
const std::string& sToSave = (s != "") ? s : sDefault; |
const std::string& sToSave = (s != "") ? s : sDefault; |
281 |
if (!bUseFixedLengthStrings) size = sToSave.size() + 1; |
if (!bUseFixedLengthStrings) size = (int) sToSave.size() + 1; |
282 |
ck = lstINFO->AddSubChunk(ChunkID, size); |
ck = lstINFO->AddSubChunk(ChunkID, size); |
283 |
char* pData = (char*) ck->LoadChunkData(); |
char* pData = (char*) ck->LoadChunkData(); |
284 |
strncpy(pData, sToSave.c_str(), size); |
strncpy(pData, sToSave.c_str(), size); |
285 |
} |
} |
286 |
} |
} |
287 |
|
|
288 |
|
// private helper function to convert progress of a subprocess into the global progress |
289 |
|
inline void __notify_progress(RIFF::progress_t* pProgress, float subprogress) { |
290 |
|
if (pProgress && pProgress->callback) { |
291 |
|
const float totalrange = pProgress->__range_max - pProgress->__range_min; |
292 |
|
const float totalprogress = pProgress->__range_min + subprogress * totalrange; |
293 |
|
pProgress->factor = totalprogress; |
294 |
|
pProgress->callback(pProgress); // now actually notify about the progress |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
|
// private helper function to divide a progress into subprogresses |
299 |
|
inline void __divide_progress(RIFF::progress_t* pParentProgress, RIFF::progress_t* pSubProgress, float totalTasks, float currentTask) { |
300 |
|
if (pParentProgress && pParentProgress->callback) { |
301 |
|
const float totalrange = pParentProgress->__range_max - pParentProgress->__range_min; |
302 |
|
pSubProgress->callback = pParentProgress->callback; |
303 |
|
pSubProgress->custom = pParentProgress->custom; |
304 |
|
pSubProgress->__range_min = pParentProgress->__range_min + totalrange * currentTask / totalTasks; |
305 |
|
pSubProgress->__range_max = pSubProgress->__range_min + totalrange / totalTasks; |
306 |
|
} |
307 |
|
} |
308 |
|
|
309 |
|
// private helper function to divide a progress into subprogresses |
310 |
|
inline void __divide_progress(RIFF::progress_t* pParentProgress, RIFF::progress_t* pSubProgress, float total, float lo, float hi) { |
311 |
|
if (pParentProgress && pParentProgress->callback) { |
312 |
|
const float totalrange = pParentProgress->__range_max - pParentProgress->__range_min; |
313 |
|
pSubProgress->callback = pParentProgress->callback; |
314 |
|
pSubProgress->custom = pParentProgress->custom; |
315 |
|
pSubProgress->abort = pParentProgress->abort; |
316 |
|
pSubProgress->__range_min = pParentProgress->__range_min + totalrange * (lo / total); |
317 |
|
pSubProgress->__range_max = pSubProgress->__range_min + totalrange * ((hi-lo) / total); |
318 |
|
} |
319 |
|
} |
320 |
|
|
321 |
|
#ifdef _WIN32 |
322 |
|
# define NATIVE_PATH_SEPARATOR '\\' |
323 |
|
#else |
324 |
|
# define NATIVE_PATH_SEPARATOR '/' |
325 |
|
#endif |
326 |
|
|
327 |
|
/** |
328 |
|
* Returns the owning path of the given path (its parent path). So for example |
329 |
|
* passing "/some/path" would return "/some". |
330 |
|
*/ |
331 |
|
inline std::string parentPath(const std::string path) { |
332 |
|
std::size_t pos = path.find_last_of(NATIVE_PATH_SEPARATOR); |
333 |
|
return (pos == std::string::npos) ? path : path.substr(0, pos); |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
|
* Returns the last (lowest) portion of the given path. So for example passing |
338 |
|
* "/some/path" would return "path". |
339 |
|
*/ |
340 |
|
inline std::string lastPathComponent(const std::string path) { |
341 |
|
std::size_t pos = path.find_last_of(NATIVE_PATH_SEPARATOR); |
342 |
|
return (pos == std::string::npos) ? path : path.substr(pos+1); |
343 |
|
} |
344 |
|
|
345 |
|
/** |
346 |
|
* Returns the given path with the type extension being stripped from its end. |
347 |
|
* So for example passing "/some/path.foo" would return "/some/path". |
348 |
|
*/ |
349 |
|
inline std::string pathWithoutExtension(const std::string path) { |
350 |
|
std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR); |
351 |
|
std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1; |
352 |
|
std::size_t posDot = path.find_last_of(".", posBase); |
353 |
|
return (posDot != std::string::npos && posDot > posBase) |
354 |
|
? path.substr(0, posDot) : path; |
355 |
|
} |
356 |
|
|
357 |
|
/** |
358 |
|
* Returns the type extension of the given path. So for example passing |
359 |
|
* "/some/path.foo" would return "foo". |
360 |
|
*/ |
361 |
|
inline std::string extensionOfPath(const std::string path) { |
362 |
|
std::size_t posSep = path.find_last_of(NATIVE_PATH_SEPARATOR); |
363 |
|
std::size_t posBase = (posSep == std::string::npos) ? 0 : posSep+1; |
364 |
|
std::size_t posDot = path.find_last_of(".", posBase); |
365 |
|
return (posDot != std::string::npos && posDot > posBase) |
366 |
|
? path.substr(posDot+1) : ""; |
367 |
|
} |
368 |
|
|
369 |
|
/** |
370 |
|
* Combines the two given paths with each other. So for example passing |
371 |
|
* "/some/path" and "/another/one" would return "/some/path/another/one". |
372 |
|
*/ |
373 |
|
inline std::string concatPath(const std::string path1, const std::string path2) { |
374 |
|
return (!path1.empty() && *(path1.rbegin()) != NATIVE_PATH_SEPARATOR && |
375 |
|
!path2.empty() && *(path2.begin()) != NATIVE_PATH_SEPARATOR) |
376 |
|
? path1 + NATIVE_PATH_SEPARATOR + path2 |
377 |
|
: path1 + path2; |
378 |
|
} |
379 |
|
|
380 |
#endif // __LIBGIG_HELPER_H__ |
#endif // __LIBGIG_HELPER_H__ |