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 * |
28 |
#include <string> |
#include <string> |
29 |
#include <sstream> |
#include <sstream> |
30 |
|
|
31 |
|
#include "RIFF.h" |
32 |
|
|
33 |
// *************** Helper Functions ************** |
// *************** Helper Functions ************** |
34 |
// * |
// * |
35 |
|
|
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__ |