2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
* libgig - C++ cross-platform Gigasampler format file loader library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2005 by Christian Schoenebeck * |
* Copyright (C) 2003-2006 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 * |
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 |
|
#include "RIFF.h" |
32 |
|
|
33 |
// *************** Helper Functions ************** |
// *************** Helper Functions ************** |
34 |
// * |
// * |
35 |
|
|
39 |
return ss.str(); |
return ss.str(); |
40 |
} |
} |
41 |
|
|
42 |
|
inline long Min(long A, long B) { |
43 |
|
return (A > B) ? B : A; |
44 |
|
} |
45 |
|
|
46 |
|
inline long Abs(long val) { |
47 |
|
return (val > 0) ? val : -val; |
48 |
|
} |
49 |
|
|
50 |
|
/** |
51 |
|
* Swaps the order of the data words in the given memory area |
52 |
|
* with a granularity given by \a WordSize. |
53 |
|
* |
54 |
|
* @param pData - pointer to the memory area to be swapped |
55 |
|
* @param AreaSize - size of the memory area to be swapped (in bytes) |
56 |
|
* @param WordSize - size of the data words (in bytes) |
57 |
|
*/ |
58 |
|
inline void SwapMemoryArea(void* pData, unsigned long AreaSize, uint WordSize) { |
59 |
|
switch (WordSize) { // TODO: unefficient |
60 |
|
case 1: { |
61 |
|
uint8_t* pDst = (uint8_t*) pData; |
62 |
|
uint8_t cache; |
63 |
|
unsigned long lo = 0, hi = AreaSize - 1; |
64 |
|
for (; lo < hi; hi--, lo++) { |
65 |
|
cache = pDst[lo]; |
66 |
|
pDst[lo] = pDst[hi]; |
67 |
|
pDst[hi] = cache; |
68 |
|
} |
69 |
|
break; |
70 |
|
} |
71 |
|
case 2: { |
72 |
|
uint16_t* pDst = (uint16_t*) pData; |
73 |
|
uint16_t cache; |
74 |
|
unsigned long lo = 0, hi = (AreaSize >> 1) - 1; |
75 |
|
for (; lo < hi; hi--, lo++) { |
76 |
|
cache = pDst[lo]; |
77 |
|
pDst[lo] = pDst[hi]; |
78 |
|
pDst[hi] = cache; |
79 |
|
} |
80 |
|
break; |
81 |
|
} |
82 |
|
case 4: { |
83 |
|
uint32_t* pDst = (uint32_t*) pData; |
84 |
|
uint32_t cache; |
85 |
|
unsigned long lo = 0, hi = (AreaSize >> 2) - 1; |
86 |
|
for (; lo < hi; hi--, lo++) { |
87 |
|
cache = pDst[lo]; |
88 |
|
pDst[lo] = pDst[hi]; |
89 |
|
pDst[hi] = cache; |
90 |
|
} |
91 |
|
break; |
92 |
|
} |
93 |
|
default: { |
94 |
|
uint8_t* pCache = new uint8_t[WordSize]; // TODO: unefficient |
95 |
|
unsigned long lo = 0, hi = AreaSize - WordSize; |
96 |
|
for (; lo < hi; hi -= WordSize, lo += WordSize) { |
97 |
|
memcpy(pCache, (uint8_t*) pData + lo, WordSize); |
98 |
|
memcpy((uint8_t*) pData + lo, (uint8_t*) pData + hi, WordSize); |
99 |
|
memcpy((uint8_t*) pData + hi, pCache, WordSize); |
100 |
|
} |
101 |
|
delete[] pCache; |
102 |
|
break; |
103 |
|
} |
104 |
|
} |
105 |
|
} |
106 |
|
|
107 |
|
/** @brief Load given info field (string). |
108 |
|
* |
109 |
|
* Load info field string from given info chunk (\a ck) and save value to \a s. |
110 |
|
*/ |
111 |
|
inline void LoadString(RIFF::Chunk* ck, std::string& s) { |
112 |
|
if (ck) { |
113 |
|
const char* str = (char*)ck->LoadChunkData(); |
114 |
|
int size = ck->GetSize(); |
115 |
|
int len; |
116 |
|
for (len = 0 ; len < size ; len++) |
117 |
|
if (str[len] == '\0') break; |
118 |
|
s.assign(str, len); |
119 |
|
ck->ReleaseChunkData(); |
120 |
|
} |
121 |
|
} |
122 |
|
|
123 |
|
/** @brief Apply given INFO field to the respective chunk. |
124 |
|
* |
125 |
|
* Apply given info value string to given info chunk, which is a |
126 |
|
* subchunk of INFO list chunk \a lstINFO. If the given chunk already |
127 |
|
* exists, value \a s will be applied. Otherwise if it doesn't exist yet |
128 |
|
* and either \a s or \a sDefault is not an empty string, such a chunk |
129 |
|
* will be created and either \a s or \a sDefault will be applied |
130 |
|
* (depending on which one is not an empty string, if both are not an |
131 |
|
* empty string \a s will be preferred). |
132 |
|
* |
133 |
|
* @param ChunkID - 32 bit RIFF chunk ID of INFO subchunk (only used in case \a ck is NULL) |
134 |
|
* @param ck - INFO (sub)chunk where string should be stored to |
135 |
|
* @param lstINFO - parent (INFO) RIFF list chunk |
136 |
|
* @param s - current value of info field |
137 |
|
* @param sDefault - default value |
138 |
|
* @param bUseFixedLengthStrings - should a specific string size be forced in the chunk? |
139 |
|
* @param size - wanted size of the INFO chunk. This is ignored if bUseFixedLengthStrings is false. |
140 |
|
*/ |
141 |
|
inline void SaveString(uint32_t ChunkID, RIFF::Chunk* ck, RIFF::List* lstINFO, const std::string& s, const std::string& sDefault, bool bUseFixedLengthStrings, int size) { |
142 |
|
if (ck) { // if chunk exists already, use 's' as value |
143 |
|
if (!bUseFixedLengthStrings) size = s.size() + 1; |
144 |
|
ck->Resize(size); |
145 |
|
char* pData = (char*) ck->LoadChunkData(); |
146 |
|
strncpy(pData, s.c_str(), size); |
147 |
|
} else if (s != "" || sDefault != "") { // create chunk |
148 |
|
const std::string& sToSave = (s != "") ? s : sDefault; |
149 |
|
if (!bUseFixedLengthStrings) size = sToSave.size() + 1; |
150 |
|
ck = lstINFO->AddSubChunk(ChunkID, size); |
151 |
|
char* pData = (char*) ck->LoadChunkData(); |
152 |
|
strncpy(pData, sToSave.c_str(), size); |
153 |
|
} |
154 |
|
} |
155 |
|
|
156 |
#endif // __LIBGIG_HELPER_H__ |
#endif // __LIBGIG_HELPER_H__ |