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-2014 by Christian Schoenebeck * |
* Copyright (C) 2003-2021 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 * |
29 |
|
|
30 |
#include "helper.h" |
#include "helper.h" |
31 |
|
|
32 |
|
#if POSIX |
33 |
|
# include <errno.h> |
34 |
|
#endif |
35 |
|
|
36 |
namespace RIFF { |
namespace RIFF { |
37 |
|
|
38 |
// *************** Internal functions ************** |
// *************** Internal functions ************** |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
// *************** progress_t *************** |
58 |
|
// * |
59 |
|
|
60 |
|
progress_t::progress_t() { |
61 |
|
callback = NULL; |
62 |
|
custom = NULL; |
63 |
|
__range_min = 0.0f; |
64 |
|
__range_max = 1.0f; |
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
|
* Divides this progress task into the requested amount of equal weighted |
69 |
|
* sub-progress tasks and returns a vector with those subprogress tasks. |
70 |
|
* |
71 |
|
* @param iSubtasks - total amount sub tasks this task should be subdivided |
72 |
|
* @returns subtasks |
73 |
|
*/ |
74 |
|
std::vector<progress_t> progress_t::subdivide(int iSubtasks) { |
75 |
|
std::vector<progress_t> v; |
76 |
|
for (int i = 0; i < iSubtasks; ++i) { |
77 |
|
progress_t p; |
78 |
|
__divide_progress(this, &p, iSubtasks, i); |
79 |
|
v.push_back(p); |
80 |
|
} |
81 |
|
return v; |
82 |
|
} |
83 |
|
|
84 |
|
/** |
85 |
|
* Divides this progress task into the requested amount of sub-progress |
86 |
|
* tasks, where each one of those new sub-progress tasks is created with its |
87 |
|
* requested individual weight / portion, and finally returns a vector |
88 |
|
* with those new subprogress tasks. |
89 |
|
* |
90 |
|
* The amount of subprogresses to be created is determined by this method |
91 |
|
* by calling @c vSubTaskPortions.size() . |
92 |
|
* |
93 |
|
* Example: consider you wanted to create 3 subprogresses where the 1st |
94 |
|
* subtask should be assigned 10% of the new 3 subprogresses' overall |
95 |
|
* progress, the 2nd subtask should be assigned 50% of the new 3 |
96 |
|
* subprogresses' overall progress, and the 3rd subtask should be assigned |
97 |
|
* 40%, then you might call this method like this: |
98 |
|
* @code |
99 |
|
* std::vector<progress_t> subprogresses = progress.subdivide({0.1, 0.5, 0.4}); |
100 |
|
* @endcode |
101 |
|
* |
102 |
|
* @param vSubTaskPortions - amount and individual weight of subtasks to be |
103 |
|
* created |
104 |
|
* @returns subtasks |
105 |
|
*/ |
106 |
|
std::vector<progress_t> progress_t::subdivide(std::vector<float> vSubTaskPortions) { |
107 |
|
float fTotal = 0.f; // usually 1.0, but we sum the portions up below to be sure |
108 |
|
for (int i = 0; i < vSubTaskPortions.size(); ++i) |
109 |
|
fTotal += vSubTaskPortions[i]; |
110 |
|
|
111 |
|
float fLow = 0.f, fHigh = 0.f; |
112 |
|
std::vector<progress_t> v; |
113 |
|
for (int i = 0; i < vSubTaskPortions.size(); ++i) { |
114 |
|
fLow = fHigh; |
115 |
|
fHigh = vSubTaskPortions[i]; |
116 |
|
progress_t p; |
117 |
|
__divide_progress(this, &p, fTotal, fLow, fHigh); |
118 |
|
v.push_back(p); |
119 |
|
} |
120 |
|
return v; |
121 |
|
} |
122 |
|
|
123 |
|
|
124 |
|
|
125 |
// *************** Chunk ************** |
// *************** Chunk ************** |
126 |
// * |
// * |
127 |
|
|
128 |
Chunk::Chunk(File* pFile) { |
Chunk::Chunk(File* pFile) { |
129 |
#if DEBUG |
#if DEBUG_RIFF |
130 |
std::cout << "Chunk::Chunk(File* pFile)" << std::endl; |
std::cout << "Chunk::Chunk(File* pFile)" << std::endl; |
131 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
132 |
ulPos = 0; |
ullPos = 0; |
133 |
pParent = NULL; |
pParent = NULL; |
134 |
pChunkData = NULL; |
pChunkData = NULL; |
135 |
CurrentChunkSize = 0; |
ullCurrentChunkSize = 0; |
136 |
NewChunkSize = 0; |
ullNewChunkSize = 0; |
137 |
ulChunkDataSize = 0; |
ullChunkDataSize = 0; |
138 |
ChunkID = CHUNK_ID_RIFF; |
ChunkID = CHUNK_ID_RIFF; |
139 |
this->pFile = pFile; |
this->pFile = pFile; |
140 |
} |
} |
141 |
|
|
142 |
Chunk::Chunk(File* pFile, unsigned long StartPos, List* Parent) { |
Chunk::Chunk(File* pFile, file_offset_t StartPos, List* Parent) { |
143 |
#if DEBUG |
#if DEBUG_RIFF |
144 |
std::cout << "Chunk::Chunk(File*,ulong,bool,List*),StartPos=" << StartPos << std::endl; |
std::cout << "Chunk::Chunk(File*,file_offset_t,List*),StartPos=" << StartPos << std::endl; |
145 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
146 |
this->pFile = pFile; |
this->pFile = pFile; |
147 |
ulStartPos = StartPos + CHUNK_HEADER_SIZE; |
ullStartPos = StartPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
148 |
pParent = Parent; |
pParent = Parent; |
149 |
ulPos = 0; |
ullPos = 0; |
150 |
pChunkData = NULL; |
pChunkData = NULL; |
151 |
CurrentChunkSize = 0; |
ullCurrentChunkSize = 0; |
152 |
NewChunkSize = 0; |
ullNewChunkSize = 0; |
153 |
ulChunkDataSize = 0; |
ullChunkDataSize = 0; |
154 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
155 |
} |
} |
156 |
|
|
157 |
Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize) { |
Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize) { |
158 |
this->pFile = pFile; |
this->pFile = pFile; |
159 |
ulStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk |
ullStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk |
160 |
this->pParent = pParent; |
this->pParent = pParent; |
161 |
ulPos = 0; |
ullPos = 0; |
162 |
pChunkData = NULL; |
pChunkData = NULL; |
163 |
ChunkID = uiChunkID; |
ChunkID = uiChunkID; |
164 |
ulChunkDataSize = 0; |
ullChunkDataSize = 0; |
165 |
CurrentChunkSize = 0; |
ullCurrentChunkSize = 0; |
166 |
NewChunkSize = uiBodySize; |
ullNewChunkSize = ullBodySize; |
167 |
} |
} |
168 |
|
|
169 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
|
if (pFile) pFile->UnlogResized(this); |
|
170 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
171 |
} |
} |
172 |
|
|
173 |
void Chunk::ReadHeader(unsigned long fPos) { |
void Chunk::ReadHeader(file_offset_t filePos) { |
174 |
#if DEBUG |
#if DEBUG_RIFF |
175 |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
std::cout << "Chunk::Readheader(" << filePos << ") "; |
176 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
177 |
ChunkID = 0; |
ChunkID = 0; |
178 |
NewChunkSize = CurrentChunkSize = 0; |
ullNewChunkSize = ullCurrentChunkSize = 0; |
179 |
#if POSIX |
#if POSIX |
180 |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, filePos, SEEK_SET) != -1) { |
181 |
read(pFile->hFileRead, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
182 |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
read(pFile->hFileRead, &ullCurrentChunkSize, pFile->FileOffsetSize); |
183 |
#elif defined(WIN32) |
#elif defined(WIN32) |
184 |
if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
LARGE_INTEGER liFilePos; |
185 |
|
liFilePos.QuadPart = filePos; |
186 |
|
if (SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
187 |
DWORD dwBytesRead; |
DWORD dwBytesRead; |
188 |
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
189 |
ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL); |
ReadFile(pFile->hFileRead, &ullCurrentChunkSize, pFile->FileOffsetSize, &dwBytesRead, NULL); |
190 |
#else |
#else |
191 |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
if (!fseeko(pFile->hFileRead, filePos, SEEK_SET)) { |
192 |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
193 |
fread(&CurrentChunkSize, 4, 1, pFile->hFileRead); |
fread(&ullCurrentChunkSize, pFile->FileOffsetSize, 1, pFile->hFileRead); |
194 |
#endif // POSIX |
#endif // POSIX |
195 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
196 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
204 |
#endif // WORDS_BIGENDIAN |
#endif // WORDS_BIGENDIAN |
205 |
if (!pFile->bEndianNative) { |
if (!pFile->bEndianNative) { |
206 |
//swapBytes_32(&ChunkID); |
//swapBytes_32(&ChunkID); |
207 |
swapBytes_32(&CurrentChunkSize); |
if (pFile->FileOffsetSize == 4) |
208 |
|
swapBytes_32(&ullCurrentChunkSize); |
209 |
|
else |
210 |
|
swapBytes_64(&ullCurrentChunkSize); |
211 |
} |
} |
212 |
#if DEBUG |
#if DEBUG_RIFF |
213 |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
214 |
std::cout << "ckSize=" << CurrentChunkSize << " "; |
std::cout << "ckSize=" << ullCurrentChunkSize << " "; |
215 |
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; |
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; |
216 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
217 |
NewChunkSize = CurrentChunkSize; |
ullNewChunkSize = ullCurrentChunkSize; |
218 |
} |
} |
219 |
} |
} |
220 |
|
|
221 |
void Chunk::WriteHeader(unsigned long fPos) { |
void Chunk::WriteHeader(file_offset_t filePos) { |
222 |
uint32_t uiNewChunkID = ChunkID; |
uint32_t uiNewChunkID = ChunkID; |
223 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
224 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
228 |
#endif // WORDS_BIGENDIAN |
#endif // WORDS_BIGENDIAN |
229 |
} |
} |
230 |
|
|
231 |
uint32_t uiNewChunkSize = NewChunkSize; |
uint64_t ullNewChunkSize = this->ullNewChunkSize; |
232 |
if (!pFile->bEndianNative) { |
if (!pFile->bEndianNative) { |
233 |
swapBytes_32(&uiNewChunkSize); |
if (pFile->FileOffsetSize == 4) |
234 |
|
swapBytes_32(&ullNewChunkSize); |
235 |
|
else |
236 |
|
swapBytes_64(&ullNewChunkSize); |
237 |
} |
} |
238 |
|
|
239 |
#if POSIX |
#if POSIX |
240 |
if (lseek(pFile->hFileWrite, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileWrite, filePos, SEEK_SET) != -1) { |
241 |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
242 |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
write(pFile->hFileWrite, &ullNewChunkSize, pFile->FileOffsetSize); |
243 |
} |
} |
244 |
#elif defined(WIN32) |
#elif defined(WIN32) |
245 |
if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
LARGE_INTEGER liFilePos; |
246 |
|
liFilePos.QuadPart = filePos; |
247 |
|
if (SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
248 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
249 |
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
250 |
WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &ullNewChunkSize, pFile->FileOffsetSize, &dwBytesWritten, NULL); |
251 |
} |
} |
252 |
#else |
#else |
253 |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
if (!fseeko(pFile->hFileWrite, filePos, SEEK_SET)) { |
254 |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
255 |
fwrite(&uiNewChunkSize, 4, 1, pFile->hFileWrite); |
fwrite(&ullNewChunkSize, pFile->FileOffsetSize, 1, pFile->hFileWrite); |
256 |
} |
} |
257 |
#endif // POSIX |
#endif // POSIX |
258 |
} |
} |
261 |
* Returns the String representation of the chunk's ID (e.g. "RIFF", |
* Returns the String representation of the chunk's ID (e.g. "RIFF", |
262 |
* "LIST"). |
* "LIST"). |
263 |
*/ |
*/ |
264 |
String Chunk::GetChunkIDString() { |
String Chunk::GetChunkIDString() const { |
265 |
return convertToString(ChunkID); |
return convertToString(ChunkID); |
266 |
} |
} |
267 |
|
|
277 |
* if omitted \a Where relates to beginning of the chunk |
* if omitted \a Where relates to beginning of the chunk |
278 |
* data |
* data |
279 |
*/ |
*/ |
280 |
unsigned long Chunk::SetPos(unsigned long Where, stream_whence_t Whence) { |
file_offset_t Chunk::SetPos(file_offset_t Where, stream_whence_t Whence) { |
281 |
#if DEBUG |
#if DEBUG_RIFF |
282 |
std::cout << "Chunk::SetPos(ulong)" << std::endl; |
std::cout << "Chunk::SetPos(file_offset_t,stream_whence_t)" << std::endl; |
283 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
284 |
switch (Whence) { |
switch (Whence) { |
285 |
case stream_curpos: |
case stream_curpos: |
286 |
ulPos += Where; |
ullPos += Where; |
287 |
break; |
break; |
288 |
case stream_end: |
case stream_end: |
289 |
ulPos = CurrentChunkSize - 1 - Where; |
ullPos = ullCurrentChunkSize - 1 - Where; |
290 |
break; |
break; |
291 |
case stream_backward: |
case stream_backward: |
292 |
ulPos -= Where; |
ullPos -= Where; |
293 |
break; |
break; |
294 |
case stream_start: default: |
case stream_start: default: |
295 |
ulPos = Where; |
ullPos = Where; |
296 |
break; |
break; |
297 |
} |
} |
298 |
if (ulPos > CurrentChunkSize) ulPos = CurrentChunkSize; |
if (ullPos > ullCurrentChunkSize) ullPos = ullCurrentChunkSize; |
299 |
return ulPos; |
return ullPos; |
300 |
} |
} |
301 |
|
|
302 |
/** |
/** |
309 |
* |
* |
310 |
* @returns number of bytes left to read |
* @returns number of bytes left to read |
311 |
*/ |
*/ |
312 |
unsigned long Chunk::RemainingBytes() { |
file_offset_t Chunk::RemainingBytes() const { |
313 |
#if DEBUG |
#if DEBUG_RIFF |
314 |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << ullCurrentChunkSize - ullPos << std::endl; |
315 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
316 |
return (CurrentChunkSize > ulPos) ? CurrentChunkSize - ulPos : 0; |
return (ullCurrentChunkSize > ullPos) ? ullCurrentChunkSize - ullPos : 0; |
317 |
|
} |
318 |
|
|
319 |
|
/** |
320 |
|
* Returns the actual total size in bytes (including header) of this Chunk |
321 |
|
* if being stored to a file. |
322 |
|
* |
323 |
|
* @param fileOffsetSize - RIFF file offset size (in bytes) assumed when |
324 |
|
* being saved to a file |
325 |
|
*/ |
326 |
|
file_offset_t Chunk::RequiredPhysicalSize(int fileOffsetSize) { |
327 |
|
return CHUNK_HEADER_SIZE(fileOffsetSize) + // RIFF chunk header |
328 |
|
ullNewChunkSize + // chunks's actual data body |
329 |
|
ullNewChunkSize % 2; // optional pad byte |
330 |
} |
} |
331 |
|
|
332 |
/** |
/** |
340 |
* already reached the end of the chunk data, no more reading |
* already reached the end of the chunk data, no more reading |
341 |
* possible without SetPos() |
* possible without SetPos() |
342 |
*/ |
*/ |
343 |
stream_state_t Chunk::GetState() { |
stream_state_t Chunk::GetState() const { |
344 |
#if DEBUG |
#if DEBUG_RIFF |
345 |
std::cout << "Chunk::GetState()" << std::endl; |
std::cout << "Chunk::GetState()" << std::endl; |
346 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
347 |
#if POSIX |
#if POSIX |
348 |
if (pFile->hFileRead == 0) return stream_closed; |
if (pFile->hFileRead == 0) return stream_closed; |
349 |
#elif defined (WIN32) |
#elif defined (WIN32) |
352 |
#else |
#else |
353 |
if (pFile->hFileRead == NULL) return stream_closed; |
if (pFile->hFileRead == NULL) return stream_closed; |
354 |
#endif // POSIX |
#endif // POSIX |
355 |
if (ulPos < CurrentChunkSize) return stream_ready; |
if (ullPos < ullCurrentChunkSize) return stream_ready; |
356 |
else return stream_end_reached; |
else return stream_end_reached; |
357 |
} |
} |
358 |
|
|
359 |
/** |
/** |
369 |
* @param WordCount number of data words to read |
* @param WordCount number of data words to read |
370 |
* @param WordSize size of each data word to read |
* @param WordSize size of each data word to read |
371 |
* @returns number of successfully read data words or 0 if end |
* @returns number of successfully read data words or 0 if end |
372 |
* of file reached or error occured |
* of file reached or error occurred |
373 |
*/ |
*/ |
374 |
unsigned long Chunk::Read(void* pData, unsigned long WordCount, unsigned long WordSize) { |
file_offset_t Chunk::Read(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
375 |
#if DEBUG |
#if DEBUG_RIFF |
376 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,file_offset_t,file_offset_t)" << std::endl; |
377 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
378 |
//if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
//if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
379 |
if (ulPos >= CurrentChunkSize) return 0; |
if (ullPos >= ullCurrentChunkSize) return 0; |
380 |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
if (ullPos + WordCount * WordSize >= ullCurrentChunkSize) WordCount = (ullCurrentChunkSize - ullPos) / WordSize; |
381 |
#if POSIX |
#if POSIX |
382 |
if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
if (lseek(pFile->hFileRead, ullStartPos + ullPos, SEEK_SET) < 0) return 0; |
383 |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
ssize_t readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
384 |
if (readWords < 1) return 0; |
if (readWords < 1) { |
385 |
|
#if DEBUG_RIFF |
386 |
|
std::cerr << "POSIX read() failed: " << strerror(errno) << std::endl << std::flush; |
387 |
|
#endif // DEBUG_RIFF |
388 |
|
return 0; |
389 |
|
} |
390 |
readWords /= WordSize; |
readWords /= WordSize; |
391 |
#elif defined(WIN32) |
#elif defined(WIN32) |
392 |
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
LARGE_INTEGER liFilePos; |
393 |
|
liFilePos.QuadPart = ullStartPos + ullPos; |
394 |
|
if (!SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) |
395 |
|
return 0; |
396 |
DWORD readWords; |
DWORD readWords; |
397 |
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); |
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); //FIXME: does not work for reading buffers larger than 2GB (even though this should rarely be the case in practice) |
398 |
if (readWords < 1) return 0; |
if (readWords < 1) return 0; |
399 |
readWords /= WordSize; |
readWords /= WordSize; |
400 |
#else // standard C functions |
#else // standard C functions |
401 |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseeko(pFile->hFileRead, ullStartPos + ullPos, SEEK_SET)) return 0; |
402 |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
file_offset_t readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
403 |
#endif // POSIX |
#endif // POSIX |
404 |
if (!pFile->bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
405 |
switch (WordSize) { |
switch (WordSize) { |
406 |
case 2: |
case 2: |
407 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
408 |
swapBytes_16((uint16_t*) pData + iWord); |
swapBytes_16((uint16_t*) pData + iWord); |
409 |
break; |
break; |
410 |
case 4: |
case 4: |
411 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
412 |
swapBytes_32((uint32_t*) pData + iWord); |
swapBytes_32((uint32_t*) pData + iWord); |
413 |
break; |
break; |
414 |
|
case 8: |
415 |
|
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
416 |
|
swapBytes_64((uint64_t*) pData + iWord); |
417 |
|
break; |
418 |
default: |
default: |
419 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
420 |
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
421 |
break; |
break; |
422 |
} |
} |
438 |
* @param WordSize size of each data word to write |
* @param WordSize size of each data word to write |
439 |
* @returns number of successfully written data words |
* @returns number of successfully written data words |
440 |
* @throws RIFF::Exception if write operation would exceed current |
* @throws RIFF::Exception if write operation would exceed current |
441 |
* chunk size or any IO error occured |
* chunk size or any IO error occurred |
442 |
* @see Resize() |
* @see Resize() |
443 |
*/ |
*/ |
444 |
unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) { |
file_offset_t Chunk::Write(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
445 |
if (pFile->Mode != stream_mode_read_write) |
if (pFile->Mode != stream_mode_read_write) |
446 |
throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first"); |
throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first"); |
447 |
if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize > CurrentChunkSize) |
if (ullPos >= ullCurrentChunkSize || ullPos + WordCount * WordSize > ullCurrentChunkSize) |
448 |
throw Exception("End of chunk reached while trying to write data"); |
throw Exception("End of chunk reached while trying to write data"); |
449 |
if (!pFile->bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
450 |
switch (WordSize) { |
switch (WordSize) { |
451 |
case 2: |
case 2: |
452 |
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
453 |
swapBytes_16((uint16_t*) pData + iWord); |
swapBytes_16((uint16_t*) pData + iWord); |
454 |
break; |
break; |
455 |
case 4: |
case 4: |
456 |
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
457 |
swapBytes_32((uint32_t*) pData + iWord); |
swapBytes_32((uint32_t*) pData + iWord); |
458 |
break; |
break; |
459 |
|
case 8: |
460 |
|
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
461 |
|
swapBytes_64((uint64_t*) pData + iWord); |
462 |
|
break; |
463 |
default: |
default: |
464 |
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
465 |
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
466 |
break; |
break; |
467 |
} |
} |
468 |
} |
} |
469 |
#if POSIX |
#if POSIX |
470 |
if (lseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET) < 0) { |
if (lseek(pFile->hFileWrite, ullStartPos + ullPos, SEEK_SET) < 0) { |
471 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
throw Exception("Could not seek to position " + ToString(ullPos) + |
472 |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
" in chunk (" + ToString(ullStartPos + ullPos) + " in file)"); |
473 |
} |
} |
474 |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
ssize_t writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
475 |
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
476 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
477 |
#elif defined(WIN32) |
#elif defined(WIN32) |
478 |
if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
LARGE_INTEGER liFilePos; |
479 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
liFilePos.QuadPart = ullStartPos + ullPos; |
480 |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
if (!SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
481 |
|
throw Exception("Could not seek to position " + ToString(ullPos) + |
482 |
|
" in chunk (" + ToString(ullStartPos + ullPos) + " in file)"); |
483 |
} |
} |
484 |
DWORD writtenWords; |
DWORD writtenWords; |
485 |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); //FIXME: does not work for writing buffers larger than 2GB (even though this should rarely be the case in practice) |
486 |
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
487 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
488 |
#else // standard C functions |
#else // standard C functions |
489 |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
if (fseeko(pFile->hFileWrite, ullStartPos + ullPos, SEEK_SET)) { |
490 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
throw Exception("Could not seek to position " + ToString(ullPos) + |
491 |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
" in chunk (" + ToString(ullStartPos + ullPos) + " in file)"); |
492 |
} |
} |
493 |
unsigned long writtenWords = fwrite(pData, WordSize, WordCount, pFile->hFileWrite); |
file_offset_t writtenWords = fwrite(pData, WordSize, WordCount, pFile->hFileWrite); |
494 |
#endif // POSIX |
#endif // POSIX |
495 |
SetPos(writtenWords * WordSize, stream_curpos); |
SetPos(writtenWords * WordSize, stream_curpos); |
496 |
return writtenWords; |
return writtenWords; |
497 |
} |
} |
498 |
|
|
499 |
/** Just an internal wrapper for the main <i>Read()</i> method with additional Exception throwing on errors. */ |
/** Just an internal wrapper for the main <i>Read()</i> method with additional Exception throwing on errors. */ |
500 |
unsigned long Chunk::ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize) { |
file_offset_t Chunk::ReadSceptical(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
501 |
unsigned long readWords = Read(pData, WordCount, WordSize); |
file_offset_t readWords = Read(pData, WordCount, WordSize); |
502 |
if (readWords != WordCount) throw RIFF::Exception("End of chunk data reached."); |
if (readWords != WordCount) throw RIFF::Exception("End of chunk data reached."); |
503 |
return readWords; |
return readWords; |
504 |
} |
} |
511 |
* @param pData destination buffer |
* @param pData destination buffer |
512 |
* @param WordCount number of 8 Bit signed integers to read |
* @param WordCount number of 8 Bit signed integers to read |
513 |
* @returns number of read integers |
* @returns number of read integers |
514 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
515 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
516 |
*/ |
*/ |
517 |
unsigned long Chunk::ReadInt8(int8_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadInt8(int8_t* pData, file_offset_t WordCount) { |
518 |
#if DEBUG |
#if DEBUG_RIFF |
519 |
std::cout << "Chunk::ReadInt8(int8_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadInt8(int8_t*,file_offset_t)" << std::endl; |
520 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
521 |
return ReadSceptical(pData, WordCount, 1); |
return ReadSceptical(pData, WordCount, 1); |
522 |
} |
} |
523 |
|
|
532 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
533 |
* @param WordCount number of 8 Bit signed integers to write |
* @param WordCount number of 8 Bit signed integers to write |
534 |
* @returns number of written integers |
* @returns number of written integers |
535 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
536 |
* @see Resize() |
* @see Resize() |
537 |
*/ |
*/ |
538 |
unsigned long Chunk::WriteInt8(int8_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteInt8(int8_t* pData, file_offset_t WordCount) { |
539 |
return Write(pData, WordCount, 1); |
return Write(pData, WordCount, 1); |
540 |
} |
} |
541 |
|
|
548 |
* @param pData destination buffer |
* @param pData destination buffer |
549 |
* @param WordCount number of 8 Bit unsigned integers to read |
* @param WordCount number of 8 Bit unsigned integers to read |
550 |
* @returns number of read integers |
* @returns number of read integers |
551 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
552 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
553 |
*/ |
*/ |
554 |
unsigned long Chunk::ReadUint8(uint8_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadUint8(uint8_t* pData, file_offset_t WordCount) { |
555 |
#if DEBUG |
#if DEBUG_RIFF |
556 |
std::cout << "Chunk::ReadUint8(uint8_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadUint8(uint8_t*,file_offset_t)" << std::endl; |
557 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
558 |
return ReadSceptical(pData, WordCount, 1); |
return ReadSceptical(pData, WordCount, 1); |
559 |
} |
} |
560 |
|
|
569 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
570 |
* @param WordCount number of 8 Bit unsigned integers to write |
* @param WordCount number of 8 Bit unsigned integers to write |
571 |
* @returns number of written integers |
* @returns number of written integers |
572 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
573 |
* @see Resize() |
* @see Resize() |
574 |
*/ |
*/ |
575 |
unsigned long Chunk::WriteUint8(uint8_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteUint8(uint8_t* pData, file_offset_t WordCount) { |
576 |
return Write(pData, WordCount, 1); |
return Write(pData, WordCount, 1); |
577 |
} |
} |
578 |
|
|
585 |
* @param pData destination buffer |
* @param pData destination buffer |
586 |
* @param WordCount number of 16 Bit signed integers to read |
* @param WordCount number of 16 Bit signed integers to read |
587 |
* @returns number of read integers |
* @returns number of read integers |
588 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
589 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
590 |
*/ |
*/ |
591 |
unsigned long Chunk::ReadInt16(int16_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadInt16(int16_t* pData, file_offset_t WordCount) { |
592 |
#if DEBUG |
#if DEBUG_RIFF |
593 |
std::cout << "Chunk::ReadInt16(int16_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadInt16(int16_t*,file_offset_t)" << std::endl; |
594 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
595 |
return ReadSceptical(pData, WordCount, 2); |
return ReadSceptical(pData, WordCount, 2); |
596 |
} |
} |
597 |
|
|
606 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
607 |
* @param WordCount number of 16 Bit signed integers to write |
* @param WordCount number of 16 Bit signed integers to write |
608 |
* @returns number of written integers |
* @returns number of written integers |
609 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
610 |
* @see Resize() |
* @see Resize() |
611 |
*/ |
*/ |
612 |
unsigned long Chunk::WriteInt16(int16_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteInt16(int16_t* pData, file_offset_t WordCount) { |
613 |
return Write(pData, WordCount, 2); |
return Write(pData, WordCount, 2); |
614 |
} |
} |
615 |
|
|
622 |
* @param pData destination buffer |
* @param pData destination buffer |
623 |
* @param WordCount number of 8 Bit unsigned integers to read |
* @param WordCount number of 8 Bit unsigned integers to read |
624 |
* @returns number of read integers |
* @returns number of read integers |
625 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
626 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
627 |
*/ |
*/ |
628 |
unsigned long Chunk::ReadUint16(uint16_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadUint16(uint16_t* pData, file_offset_t WordCount) { |
629 |
#if DEBUG |
#if DEBUG_RIFF |
630 |
std::cout << "Chunk::ReadUint16(uint16_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadUint16(uint16_t*,file_offset_t)" << std::endl; |
631 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
632 |
return ReadSceptical(pData, WordCount, 2); |
return ReadSceptical(pData, WordCount, 2); |
633 |
} |
} |
634 |
|
|
643 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
644 |
* @param WordCount number of 16 Bit unsigned integers to write |
* @param WordCount number of 16 Bit unsigned integers to write |
645 |
* @returns number of written integers |
* @returns number of written integers |
646 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
647 |
* @see Resize() |
* @see Resize() |
648 |
*/ |
*/ |
649 |
unsigned long Chunk::WriteUint16(uint16_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteUint16(uint16_t* pData, file_offset_t WordCount) { |
650 |
return Write(pData, WordCount, 2); |
return Write(pData, WordCount, 2); |
651 |
} |
} |
652 |
|
|
659 |
* @param pData destination buffer |
* @param pData destination buffer |
660 |
* @param WordCount number of 32 Bit signed integers to read |
* @param WordCount number of 32 Bit signed integers to read |
661 |
* @returns number of read integers |
* @returns number of read integers |
662 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
663 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
664 |
*/ |
*/ |
665 |
unsigned long Chunk::ReadInt32(int32_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadInt32(int32_t* pData, file_offset_t WordCount) { |
666 |
#if DEBUG |
#if DEBUG_RIFF |
667 |
std::cout << "Chunk::ReadInt32(int32_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadInt32(int32_t*,file_offset_t)" << std::endl; |
668 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
669 |
return ReadSceptical(pData, WordCount, 4); |
return ReadSceptical(pData, WordCount, 4); |
670 |
} |
} |
671 |
|
|
680 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
681 |
* @param WordCount number of 32 Bit signed integers to write |
* @param WordCount number of 32 Bit signed integers to write |
682 |
* @returns number of written integers |
* @returns number of written integers |
683 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
684 |
* @see Resize() |
* @see Resize() |
685 |
*/ |
*/ |
686 |
unsigned long Chunk::WriteInt32(int32_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteInt32(int32_t* pData, file_offset_t WordCount) { |
687 |
return Write(pData, WordCount, 4); |
return Write(pData, WordCount, 4); |
688 |
} |
} |
689 |
|
|
696 |
* @param pData destination buffer |
* @param pData destination buffer |
697 |
* @param WordCount number of 32 Bit unsigned integers to read |
* @param WordCount number of 32 Bit unsigned integers to read |
698 |
* @returns number of read integers |
* @returns number of read integers |
699 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
700 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
701 |
*/ |
*/ |
702 |
unsigned long Chunk::ReadUint32(uint32_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadUint32(uint32_t* pData, file_offset_t WordCount) { |
703 |
#if DEBUG |
#if DEBUG_RIFF |
704 |
std::cout << "Chunk::ReadUint32(uint32_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadUint32(uint32_t*,file_offset_t)" << std::endl; |
705 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
706 |
return ReadSceptical(pData, WordCount, 4); |
return ReadSceptical(pData, WordCount, 4); |
707 |
} |
} |
708 |
|
|
713 |
* |
* |
714 |
* @param s destination string |
* @param s destination string |
715 |
* @param size number of characters to read |
* @param size number of characters to read |
716 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
717 |
* \a size characters could be read! |
* \a size characters could be read! |
718 |
*/ |
*/ |
719 |
void Chunk::ReadString(String& s, int size) { |
void Chunk::ReadString(String& s, int size) { |
734 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
735 |
* @param WordCount number of 32 Bit unsigned integers to write |
* @param WordCount number of 32 Bit unsigned integers to write |
736 |
* @returns number of written integers |
* @returns number of written integers |
737 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
738 |
* @see Resize() |
* @see Resize() |
739 |
*/ |
*/ |
740 |
unsigned long Chunk::WriteUint32(uint32_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteUint32(uint32_t* pData, file_offset_t WordCount) { |
741 |
return Write(pData, WordCount, 4); |
return Write(pData, WordCount, 4); |
742 |
} |
} |
743 |
|
|
746 |
* the chunk. |
* the chunk. |
747 |
* |
* |
748 |
* @returns read integer word |
* @returns read integer word |
749 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
750 |
*/ |
*/ |
751 |
int8_t Chunk::ReadInt8() { |
int8_t Chunk::ReadInt8() { |
752 |
#if DEBUG |
#if DEBUG_RIFF |
753 |
std::cout << "Chunk::ReadInt8()" << std::endl; |
std::cout << "Chunk::ReadInt8()" << std::endl; |
754 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
755 |
int8_t word; |
int8_t word; |
756 |
ReadSceptical(&word,1,1); |
ReadSceptical(&word,1,1); |
757 |
return word; |
return word; |
762 |
* within the chunk. |
* within the chunk. |
763 |
* |
* |
764 |
* @returns read integer word |
* @returns read integer word |
765 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
766 |
*/ |
*/ |
767 |
uint8_t Chunk::ReadUint8() { |
uint8_t Chunk::ReadUint8() { |
768 |
#if DEBUG |
#if DEBUG_RIFF |
769 |
std::cout << "Chunk::ReadUint8()" << std::endl; |
std::cout << "Chunk::ReadUint8()" << std::endl; |
770 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
771 |
uint8_t word; |
uint8_t word; |
772 |
ReadSceptical(&word,1,1); |
ReadSceptical(&word,1,1); |
773 |
return word; |
return word; |
779 |
* needed. |
* needed. |
780 |
* |
* |
781 |
* @returns read integer word |
* @returns read integer word |
782 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
783 |
*/ |
*/ |
784 |
int16_t Chunk::ReadInt16() { |
int16_t Chunk::ReadInt16() { |
785 |
#if DEBUG |
#if DEBUG_RIFF |
786 |
std::cout << "Chunk::ReadInt16()" << std::endl; |
std::cout << "Chunk::ReadInt16()" << std::endl; |
787 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
788 |
int16_t word; |
int16_t word; |
789 |
ReadSceptical(&word,1,2); |
ReadSceptical(&word,1,2); |
790 |
return word; |
return word; |
796 |
* needed. |
* needed. |
797 |
* |
* |
798 |
* @returns read integer word |
* @returns read integer word |
799 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
800 |
*/ |
*/ |
801 |
uint16_t Chunk::ReadUint16() { |
uint16_t Chunk::ReadUint16() { |
802 |
#if DEBUG |
#if DEBUG_RIFF |
803 |
std::cout << "Chunk::ReadUint16()" << std::endl; |
std::cout << "Chunk::ReadUint16()" << std::endl; |
804 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
805 |
uint16_t word; |
uint16_t word; |
806 |
ReadSceptical(&word,1,2); |
ReadSceptical(&word,1,2); |
807 |
return word; |
return word; |
813 |
* needed. |
* needed. |
814 |
* |
* |
815 |
* @returns read integer word |
* @returns read integer word |
816 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
817 |
*/ |
*/ |
818 |
int32_t Chunk::ReadInt32() { |
int32_t Chunk::ReadInt32() { |
819 |
#if DEBUG |
#if DEBUG_RIFF |
820 |
std::cout << "Chunk::ReadInt32()" << std::endl; |
std::cout << "Chunk::ReadInt32()" << std::endl; |
821 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
822 |
int32_t word; |
int32_t word; |
823 |
ReadSceptical(&word,1,4); |
ReadSceptical(&word,1,4); |
824 |
return word; |
return word; |
830 |
* needed. |
* needed. |
831 |
* |
* |
832 |
* @returns read integer word |
* @returns read integer word |
833 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
834 |
*/ |
*/ |
835 |
uint32_t Chunk::ReadUint32() { |
uint32_t Chunk::ReadUint32() { |
836 |
#if DEBUG |
#if DEBUG_RIFF |
837 |
std::cout << "Chunk::ReadUint32()" << std::endl; |
std::cout << "Chunk::ReadUint32()" << std::endl; |
838 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
839 |
uint32_t word; |
uint32_t word; |
840 |
ReadSceptical(&word,1,4); |
ReadSceptical(&word,1,4); |
841 |
return word; |
return word; |
865 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
866 |
if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) { |
if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) { |
867 |
#if POSIX |
#if POSIX |
868 |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ullStartPos, SEEK_SET) == -1) return NULL; |
869 |
#elif defined(WIN32) |
#elif defined(WIN32) |
870 |
if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL; |
LARGE_INTEGER liFilePos; |
871 |
|
liFilePos.QuadPart = ullStartPos; |
872 |
|
if (!SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) return NULL; |
873 |
#else |
#else |
874 |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
if (fseeko(pFile->hFileRead, ullStartPos, SEEK_SET)) return NULL; |
875 |
#endif // POSIX |
#endif // POSIX |
876 |
unsigned long ulBufferSize = (CurrentChunkSize > NewChunkSize) ? CurrentChunkSize : NewChunkSize; |
file_offset_t ullBufferSize = (ullCurrentChunkSize > ullNewChunkSize) ? ullCurrentChunkSize : ullNewChunkSize; |
877 |
pChunkData = new uint8_t[ulBufferSize]; |
pChunkData = new uint8_t[ullBufferSize]; |
878 |
if (!pChunkData) return NULL; |
if (!pChunkData) return NULL; |
879 |
memset(pChunkData, 0, ulBufferSize); |
memset(pChunkData, 0, ullBufferSize); |
880 |
#if POSIX |
#if POSIX |
881 |
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
file_offset_t readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
882 |
#elif defined(WIN32) |
#elif defined(WIN32) |
883 |
DWORD readWords; |
DWORD readWords; |
884 |
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); |
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); //FIXME: won't load chunks larger than 2GB ! |
885 |
#else |
#else |
886 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
file_offset_t readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
887 |
#endif // POSIX |
#endif // POSIX |
888 |
if (readWords != GetSize()) { |
if (readWords != GetSize()) { |
889 |
delete[] pChunkData; |
delete[] pChunkData; |
890 |
return (pChunkData = NULL); |
return (pChunkData = NULL); |
891 |
} |
} |
892 |
ulChunkDataSize = ulBufferSize; |
ullChunkDataSize = ullBufferSize; |
893 |
} else if (NewChunkSize > ulChunkDataSize) { |
} else if (ullNewChunkSize > ullChunkDataSize) { |
894 |
uint8_t* pNewBuffer = new uint8_t[NewChunkSize]; |
uint8_t* pNewBuffer = new uint8_t[ullNewChunkSize]; |
895 |
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(NewChunkSize) + " bytes"); |
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(ullNewChunkSize) + " bytes"); |
896 |
memset(pNewBuffer, 0 , NewChunkSize); |
memset(pNewBuffer, 0 , ullNewChunkSize); |
897 |
memcpy(pNewBuffer, pChunkData, ulChunkDataSize); |
if (pChunkData) { |
898 |
delete[] pChunkData; |
memcpy(pNewBuffer, pChunkData, ullChunkDataSize); |
899 |
pChunkData = pNewBuffer; |
delete[] pChunkData; |
900 |
ulChunkDataSize = NewChunkSize; |
} |
901 |
|
pChunkData = pNewBuffer; |
902 |
|
ullChunkDataSize = ullNewChunkSize; |
903 |
} |
} |
904 |
return pChunkData; |
return pChunkData; |
905 |
} |
} |
931 |
* calling File::Save() as this might exceed the current chunk's body |
* calling File::Save() as this might exceed the current chunk's body |
932 |
* boundary! |
* boundary! |
933 |
* |
* |
934 |
* @param iNewSize - new chunk body size in bytes (must be greater than zero) |
* @param NewSize - new chunk body size in bytes (must be greater than zero) |
935 |
* @throws RIFF::Exception if \a iNewSize is less than 1 |
* @throws RIFF::Exception if \a NewSize is less than 1 or unrealistic large |
936 |
* @see File::Save() |
* @see File::Save() |
937 |
*/ |
*/ |
938 |
void Chunk::Resize(int iNewSize) { |
void Chunk::Resize(file_offset_t NewSize) { |
939 |
if (iNewSize <= 0) |
if (NewSize == 0) |
940 |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this)); |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this)); |
941 |
if (NewChunkSize == iNewSize) return; |
if ((NewSize >> 48) != 0) |
942 |
NewChunkSize = iNewSize; |
throw Exception("Unrealistic high chunk size detected: " + __resolveChunkPath(this)); |
943 |
pFile->LogAsResized(this); |
if (ullNewChunkSize == NewSize) return; |
944 |
|
ullNewChunkSize = NewSize; |
945 |
} |
} |
946 |
|
|
947 |
/** @brief Write chunk persistently e.g. to disk. |
/** @brief Write chunk persistently e.g. to disk. |
948 |
* |
* |
949 |
* Stores the chunk persistently to its actual "physical" file. |
* Stores the chunk persistently to its actual "physical" file. |
950 |
* |
* |
951 |
* @param ulWritePos - position within the "physical" file where this |
* @param ullWritePos - position within the "physical" file where this |
952 |
* chunk should be written to |
* chunk should be written to |
953 |
* @param ulCurrentDataOffset - offset of current (old) data within |
* @param ullCurrentDataOffset - offset of current (old) data within |
954 |
* the file |
* the file |
955 |
|
* @param pProgress - optional: callback function for progress notification |
956 |
* @returns new write position in the "physical" file, that is |
* @returns new write position in the "physical" file, that is |
957 |
* \a ulWritePos incremented by this chunk's new size |
* \a ullWritePos incremented by this chunk's new size |
958 |
* (including its header size of course) |
* (including its header size of course) |
959 |
*/ |
*/ |
960 |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
file_offset_t Chunk::WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress) { |
961 |
const unsigned long ulOriginalPos = ulWritePos; |
const file_offset_t ullOriginalPos = ullWritePos; |
962 |
ulWritePos += CHUNK_HEADER_SIZE; |
ullWritePos += CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
963 |
|
|
964 |
if (pFile->Mode != stream_mode_read_write) |
if (pFile->Mode != stream_mode_read_write) |
965 |
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
970 |
LoadChunkData(); |
LoadChunkData(); |
971 |
// write chunk data from RAM persistently to the file |
// write chunk data from RAM persistently to the file |
972 |
#if POSIX |
#if POSIX |
973 |
lseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
lseek(pFile->hFileWrite, ullWritePos, SEEK_SET); |
974 |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
if (write(pFile->hFileWrite, pChunkData, ullNewChunkSize) != ullNewChunkSize) { |
975 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
976 |
} |
} |
977 |
#elif defined(WIN32) |
#elif defined(WIN32) |
978 |
SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
979 |
|
liFilePos.QuadPart = ullWritePos; |
980 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
981 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
982 |
WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, pChunkData, ullNewChunkSize, &dwBytesWritten, NULL); //FIXME: won't save chunks larger than 2GB ! |
983 |
if (dwBytesWritten != NewChunkSize) { |
if (dwBytesWritten != ullNewChunkSize) { |
984 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
985 |
} |
} |
986 |
#else |
#else |
987 |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
fseeko(pFile->hFileWrite, ullWritePos, SEEK_SET); |
988 |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
if (fwrite(pChunkData, 1, ullNewChunkSize, pFile->hFileWrite) != ullNewChunkSize) { |
989 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
990 |
} |
} |
991 |
#endif // POSIX |
#endif // POSIX |
992 |
} else { |
} else { |
993 |
// move chunk data from the end of the file to the appropriate position |
// move chunk data from the end of the file to the appropriate position |
994 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
995 |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
file_offset_t ullToMove = (ullNewChunkSize < ullCurrentChunkSize) ? ullNewChunkSize : ullCurrentChunkSize; |
996 |
#if defined(WIN32) |
#if defined(WIN32) |
997 |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
998 |
#else |
#else |
999 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
1000 |
#endif |
#endif |
1001 |
for (unsigned long ulOffset = 0; ulToMove > 0 && iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
for (file_offset_t ullOffset = 0; ullToMove > 0 && iBytesMoved > 0; ullOffset += iBytesMoved, ullToMove -= iBytesMoved) { |
1002 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ullToMove < 4096) ? int(ullToMove) : 4096; |
1003 |
#if POSIX |
#if POSIX |
1004 |
lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
lseek(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
1005 |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = (int) read(pFile->hFileRead, pCopyBuffer, (size_t) iBytesMoved); |
1006 |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
lseek(pFile->hFileWrite, ullWritePos + ullOffset, SEEK_SET); |
1007 |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = (int) write(pFile->hFileWrite, pCopyBuffer, (size_t) iBytesMoved); |
1008 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1009 |
SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
1010 |
|
liFilePos.QuadPart = ullStartPos + ullCurrentDataOffset + ullOffset; |
1011 |
|
SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1012 |
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1013 |
SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
liFilePos.QuadPart = ullWritePos + ullOffset; |
1014 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1015 |
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1016 |
#else |
#else |
1017 |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
fseeko(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
1018 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
1019 |
fseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
fseeko(pFile->hFileWrite, ullWritePos + ullOffset, SEEK_SET); |
1020 |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, pFile->hFileWrite); |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, pFile->hFileWrite); |
1021 |
#endif |
#endif |
1022 |
} |
} |
1025 |
} |
} |
1026 |
|
|
1027 |
// update this chunk's header |
// update this chunk's header |
1028 |
CurrentChunkSize = NewChunkSize; |
ullCurrentChunkSize = ullNewChunkSize; |
1029 |
WriteHeader(ulOriginalPos); |
WriteHeader(ullOriginalPos); |
1030 |
|
|
1031 |
|
if (pProgress) |
1032 |
|
__notify_progress(pProgress, 1.0); // notify done |
1033 |
|
|
1034 |
// update chunk's position pointers |
// update chunk's position pointers |
1035 |
ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE; |
ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1036 |
ulPos = 0; |
ullPos = 0; |
1037 |
|
|
1038 |
// add pad byte if needed |
// add pad byte if needed |
1039 |
if ((ulStartPos + NewChunkSize) % 2 != 0) { |
if ((ullStartPos + ullNewChunkSize) % 2 != 0) { |
1040 |
const char cPadByte = 0; |
const char cPadByte = 0; |
1041 |
#if POSIX |
#if POSIX |
1042 |
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
lseek(pFile->hFileWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
1043 |
write(pFile->hFileWrite, &cPadByte, 1); |
write(pFile->hFileWrite, &cPadByte, 1); |
1044 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1045 |
SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
1046 |
|
liFilePos.QuadPart = ullStartPos + ullNewChunkSize; |
1047 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1048 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
1049 |
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
1050 |
#else |
#else |
1051 |
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
fseeko(pFile->hFileWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
1052 |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
1053 |
#endif |
#endif |
1054 |
return ulStartPos + NewChunkSize + 1; |
return ullStartPos + ullNewChunkSize + 1; |
1055 |
} |
} |
1056 |
|
|
1057 |
return ulStartPos + NewChunkSize; |
return ullStartPos + ullNewChunkSize; |
1058 |
} |
} |
1059 |
|
|
1060 |
void Chunk::__resetPos() { |
void Chunk::__resetPos() { |
1061 |
ulPos = 0; |
ullPos = 0; |
1062 |
} |
} |
1063 |
|
|
1064 |
|
|
1067 |
// * |
// * |
1068 |
|
|
1069 |
List::List(File* pFile) : Chunk(pFile) { |
List::List(File* pFile) : Chunk(pFile) { |
1070 |
#if DEBUG |
#if DEBUG_RIFF |
1071 |
std::cout << "List::List(File* pFile)" << std::endl; |
std::cout << "List::List(File* pFile)" << std::endl; |
1072 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1073 |
pSubChunks = NULL; |
pSubChunks = NULL; |
1074 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
1075 |
} |
} |
1076 |
|
|
1077 |
List::List(File* pFile, unsigned long StartPos, List* Parent) |
List::List(File* pFile, file_offset_t StartPos, List* Parent) |
1078 |
: Chunk(pFile, StartPos, Parent) { |
: Chunk(pFile, StartPos, Parent) { |
1079 |
#if DEBUG |
#if DEBUG_RIFF |
1080 |
std::cout << "List::List(File*,ulong,bool,List*)" << std::endl; |
std::cout << "List::List(File*,file_offset_t,List*)" << std::endl; |
1081 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1082 |
pSubChunks = NULL; |
pSubChunks = NULL; |
1083 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
1084 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
1085 |
ulStartPos = StartPos + LIST_HEADER_SIZE; |
ullStartPos = StartPos + LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1086 |
} |
} |
1087 |
|
|
1088 |
List::List(File* pFile, List* pParent, uint32_t uiListID) |
List::List(File* pFile, List* pParent, uint32_t uiListID) |
1093 |
} |
} |
1094 |
|
|
1095 |
List::~List() { |
List::~List() { |
1096 |
#if DEBUG |
#if DEBUG_RIFF |
1097 |
std::cout << "List::~List()" << std::endl; |
std::cout << "List::~List()" << std::endl; |
1098 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1099 |
DeleteChunkList(); |
DeleteChunkList(); |
1100 |
} |
} |
1101 |
|
|
1128 |
* that ID |
* that ID |
1129 |
*/ |
*/ |
1130 |
Chunk* List::GetSubChunk(uint32_t ChunkID) { |
Chunk* List::GetSubChunk(uint32_t ChunkID) { |
1131 |
#if DEBUG |
#if DEBUG_RIFF |
1132 |
std::cout << "List::GetSubChunk(uint32_t)" << std::endl; |
std::cout << "List::GetSubChunk(uint32_t)" << std::endl; |
1133 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1134 |
if (!pSubChunksMap) LoadSubChunks(); |
if (!pSubChunksMap) LoadSubChunks(); |
1135 |
return (*pSubChunksMap)[ChunkID]; |
return (*pSubChunksMap)[ChunkID]; |
1136 |
} |
} |
1138 |
/** |
/** |
1139 |
* Returns sublist chunk with list type <i>\a ListType</i> within this |
* Returns sublist chunk with list type <i>\a ListType</i> within this |
1140 |
* chunk list. Use this method if you expect only one sublist chunk of |
* chunk list. Use this method if you expect only one sublist chunk of |
1141 |
* that type in the list. It there are more than one, it's undetermined |
* that type in the list. If there are more than one, it's undetermined |
1142 |
* which one of them will be returned! If there are no sublists with |
* which one of them will be returned! If there are no sublists with |
1143 |
* that desired list type, NULL will be returned. |
* that desired list type, NULL will be returned. |
1144 |
* |
* |
1147 |
* that type |
* that type |
1148 |
*/ |
*/ |
1149 |
List* List::GetSubList(uint32_t ListType) { |
List* List::GetSubList(uint32_t ListType) { |
1150 |
#if DEBUG |
#if DEBUG_RIFF |
1151 |
std::cout << "List::GetSubList(uint32_t)" << std::endl; |
std::cout << "List::GetSubList(uint32_t)" << std::endl; |
1152 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1153 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1154 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1155 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1164 |
} |
} |
1165 |
|
|
1166 |
/** |
/** |
1167 |
* Returns the first subchunk within the list. You have to call this |
* Returns the first subchunk within the list (which may be an ordinary |
1168 |
|
* chunk as well as a list chunk). You have to call this |
1169 |
* method before you can call GetNextSubChunk(). Recall it when you want |
* method before you can call GetNextSubChunk(). Recall it when you want |
1170 |
* to start from the beginning of the list again. |
* to start from the beginning of the list again. |
1171 |
* |
* |
1173 |
* otherwise |
* otherwise |
1174 |
*/ |
*/ |
1175 |
Chunk* List::GetFirstSubChunk() { |
Chunk* List::GetFirstSubChunk() { |
1176 |
#if DEBUG |
#if DEBUG_RIFF |
1177 |
std::cout << "List::GetFirstSubChunk()" << std::endl; |
std::cout << "List::GetFirstSubChunk()" << std::endl; |
1178 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1179 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1180 |
ChunksIterator = pSubChunks->begin(); |
ChunksIterator = pSubChunks->begin(); |
1181 |
return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL; |
return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL; |
1182 |
} |
} |
1183 |
|
|
1184 |
/** |
/** |
1185 |
* Returns the next subchunk within the list. You have to call |
* Returns the next subchunk within the list (which may be an ordinary |
1186 |
|
* chunk as well as a list chunk). You have to call |
1187 |
* GetFirstSubChunk() before you can use this method! |
* GetFirstSubChunk() before you can use this method! |
1188 |
* |
* |
1189 |
* @returns pointer to the next subchunk within the list or NULL if |
* @returns pointer to the next subchunk within the list or NULL if |
1190 |
* end of list is reached |
* end of list is reached |
1191 |
*/ |
*/ |
1192 |
Chunk* List::GetNextSubChunk() { |
Chunk* List::GetNextSubChunk() { |
1193 |
#if DEBUG |
#if DEBUG_RIFF |
1194 |
std::cout << "List::GetNextSubChunk()" << std::endl; |
std::cout << "List::GetNextSubChunk()" << std::endl; |
1195 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1196 |
if (!pSubChunks) return NULL; |
if (!pSubChunks) return NULL; |
1197 |
ChunksIterator++; |
ChunksIterator++; |
1198 |
return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL; |
return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL; |
1208 |
* otherwise |
* otherwise |
1209 |
*/ |
*/ |
1210 |
List* List::GetFirstSubList() { |
List* List::GetFirstSubList() { |
1211 |
#if DEBUG |
#if DEBUG_RIFF |
1212 |
std::cout << "List::GetFirstSubList()" << std::endl; |
std::cout << "List::GetFirstSubList()" << std::endl; |
1213 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1214 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1215 |
ListIterator = pSubChunks->begin(); |
ListIterator = pSubChunks->begin(); |
1216 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1230 |
* end of list is reached |
* end of list is reached |
1231 |
*/ |
*/ |
1232 |
List* List::GetNextSubList() { |
List* List::GetNextSubList() { |
1233 |
#if DEBUG |
#if DEBUG_RIFF |
1234 |
std::cout << "List::GetNextSubList()" << std::endl; |
std::cout << "List::GetNextSubList()" << std::endl; |
1235 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1236 |
if (!pSubChunks) return NULL; |
if (!pSubChunks) return NULL; |
1237 |
if (ListIterator == pSubChunks->end()) return NULL; |
if (ListIterator == pSubChunks->end()) return NULL; |
1238 |
ListIterator++; |
ListIterator++; |
1245 |
} |
} |
1246 |
|
|
1247 |
/** |
/** |
1248 |
* Returns number of subchunks within the list. |
* Returns number of subchunks within the list (including list chunks). |
1249 |
*/ |
*/ |
1250 |
unsigned int List::CountSubChunks() { |
size_t List::CountSubChunks() { |
1251 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1252 |
return pSubChunks->size(); |
return pSubChunks->size(); |
1253 |
} |
} |
1256 |
* Returns number of subchunks within the list with chunk ID |
* Returns number of subchunks within the list with chunk ID |
1257 |
* <i>\a ChunkId</i>. |
* <i>\a ChunkId</i>. |
1258 |
*/ |
*/ |
1259 |
unsigned int List::CountSubChunks(uint32_t ChunkID) { |
size_t List::CountSubChunks(uint32_t ChunkID) { |
1260 |
unsigned int result = 0; |
size_t result = 0; |
1261 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1262 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1263 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1273 |
/** |
/** |
1274 |
* Returns number of sublists within the list. |
* Returns number of sublists within the list. |
1275 |
*/ |
*/ |
1276 |
unsigned int List::CountSubLists() { |
size_t List::CountSubLists() { |
1277 |
return CountSubChunks(CHUNK_ID_LIST); |
return CountSubChunks(CHUNK_ID_LIST); |
1278 |
} |
} |
1279 |
|
|
1281 |
* Returns number of sublists within the list with list type |
* Returns number of sublists within the list with list type |
1282 |
* <i>\a ListType</i> |
* <i>\a ListType</i> |
1283 |
*/ |
*/ |
1284 |
unsigned int List::CountSubLists(uint32_t ListType) { |
size_t List::CountSubLists(uint32_t ListType) { |
1285 |
unsigned int result = 0; |
size_t result = 0; |
1286 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1287 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1288 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1299 |
/** @brief Creates a new sub chunk. |
/** @brief Creates a new sub chunk. |
1300 |
* |
* |
1301 |
* Creates and adds a new sub chunk to this list chunk. Note that the |
* Creates and adds a new sub chunk to this list chunk. Note that the |
1302 |
* chunk's body size given by \a uiBodySize must be greater than zero. |
* chunk's body size given by \a ullBodySize must be greater than zero. |
1303 |
* You have to call File::Save() to make this change persistent to the |
* You have to call File::Save() to make this change persistent to the |
1304 |
* actual file and <b>before</b> performing any data write operations |
* actual file and <b>before</b> performing any data write operations |
1305 |
* on the new chunk! |
* on the new chunk! |
1306 |
* |
* |
1307 |
* @param uiChunkID - chunk ID of the new chunk |
* @param uiChunkID - chunk ID of the new chunk |
1308 |
* @param uiBodySize - size of the new chunk's body, that is its actual |
* @param ullBodySize - size of the new chunk's body, that is its actual |
1309 |
* data size (without header) |
* data size (without header) |
1310 |
* @throws RIFF::Exception if \a uiBodySize equals zero |
* @throws RIFF::Exception if \a ullBodySize equals zero |
1311 |
*/ |
*/ |
1312 |
Chunk* List::AddSubChunk(uint32_t uiChunkID, uint uiBodySize) { |
Chunk* List::AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize) { |
1313 |
if (uiBodySize == 0) throw Exception("Chunk body size must be at least 1 byte"); |
if (ullBodySize == 0) throw Exception("Chunk body size must be at least 1 byte"); |
1314 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1315 |
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0); |
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0); |
1316 |
pSubChunks->push_back(pNewChunk); |
pSubChunks->push_back(pNewChunk); |
1317 |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1318 |
pNewChunk->Resize(uiBodySize); |
pNewChunk->Resize(ullBodySize); |
1319 |
NewChunkSize += CHUNK_HEADER_SIZE; |
ullNewChunkSize += CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
|
pFile->LogAsResized(this); |
|
1320 |
return pNewChunk; |
return pNewChunk; |
1321 |
} |
} |
1322 |
|
|
1384 |
List* pNewListChunk = new List(pFile, this, uiListType); |
List* pNewListChunk = new List(pFile, this, uiListType); |
1385 |
pSubChunks->push_back(pNewListChunk); |
pSubChunks->push_back(pNewListChunk); |
1386 |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1387 |
NewChunkSize += LIST_HEADER_SIZE; |
ullNewChunkSize += LIST_HEADER_SIZE(pFile->FileOffsetSize); |
|
pFile->LogAsResized(this); |
|
1388 |
return pNewListChunk; |
return pNewListChunk; |
1389 |
} |
} |
1390 |
|
|
1416 |
delete pSubChunk; |
delete pSubChunk; |
1417 |
} |
} |
1418 |
|
|
1419 |
void List::ReadHeader(unsigned long fPos) { |
/** |
1420 |
#if DEBUG |
* Returns the actual total size in bytes (including List chunk header and |
1421 |
std::cout << "List::Readheader(ulong) "; |
* all subchunks) of this List Chunk if being stored to a file. |
1422 |
#endif // DEBUG |
* |
1423 |
Chunk::ReadHeader(fPos); |
* @param fileOffsetSize - RIFF file offset size (in bytes) assumed when |
1424 |
if (CurrentChunkSize < 4) return; |
* being saved to a file |
1425 |
NewChunkSize = CurrentChunkSize -= 4; |
*/ |
1426 |
|
file_offset_t List::RequiredPhysicalSize(int fileOffsetSize) { |
1427 |
|
if (!pSubChunks) LoadSubChunks(); |
1428 |
|
file_offset_t size = LIST_HEADER_SIZE(fileOffsetSize); |
1429 |
|
ChunkList::iterator iter = pSubChunks->begin(); |
1430 |
|
ChunkList::iterator end = pSubChunks->end(); |
1431 |
|
for (; iter != end; ++iter) |
1432 |
|
size += (*iter)->RequiredPhysicalSize(fileOffsetSize); |
1433 |
|
return size; |
1434 |
|
} |
1435 |
|
|
1436 |
|
void List::ReadHeader(file_offset_t filePos) { |
1437 |
|
#if DEBUG_RIFF |
1438 |
|
std::cout << "List::Readheader(file_offset_t) "; |
1439 |
|
#endif // DEBUG_RIFF |
1440 |
|
Chunk::ReadHeader(filePos); |
1441 |
|
if (ullCurrentChunkSize < 4) return; |
1442 |
|
ullNewChunkSize = ullCurrentChunkSize -= 4; |
1443 |
#if POSIX |
#if POSIX |
1444 |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1445 |
read(pFile->hFileRead, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1446 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1447 |
SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
1448 |
|
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1449 |
|
SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1450 |
DWORD dwBytesRead; |
DWORD dwBytesRead; |
1451 |
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
1452 |
#else |
#else |
1453 |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseeko(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1454 |
fread(&ListType, 4, 1, pFile->hFileRead); |
fread(&ListType, 4, 1, pFile->hFileRead); |
1455 |
#endif // POSIX |
#endif // POSIX |
1456 |
#if DEBUG |
#if DEBUG_RIFF |
1457 |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
1458 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1459 |
if (!pFile->bEndianNative) { |
if (!pFile->bEndianNative) { |
1460 |
//swapBytes_32(&ListType); |
//swapBytes_32(&ListType); |
1461 |
} |
} |
1462 |
} |
} |
1463 |
|
|
1464 |
void List::WriteHeader(unsigned long fPos) { |
void List::WriteHeader(file_offset_t filePos) { |
1465 |
// the four list type bytes officially belong the chunk's body in the RIFF format |
// the four list type bytes officially belong the chunk's body in the RIFF format |
1466 |
NewChunkSize += 4; |
ullNewChunkSize += 4; |
1467 |
Chunk::WriteHeader(fPos); |
Chunk::WriteHeader(filePos); |
1468 |
NewChunkSize -= 4; // just revert the +4 incrementation |
ullNewChunkSize -= 4; // just revert the +4 incrementation |
1469 |
#if POSIX |
#if POSIX |
1470 |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1471 |
write(pFile->hFileWrite, &ListType, 4); |
write(pFile->hFileWrite, &ListType, 4); |
1472 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1473 |
SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
1474 |
|
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1475 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1476 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
1477 |
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
1478 |
#else |
#else |
1479 |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseeko(pFile->hFileWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1480 |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
1481 |
#endif // POSIX |
#endif // POSIX |
1482 |
} |
} |
1483 |
|
|
1484 |
void List::LoadSubChunks() { |
void List::LoadSubChunks(progress_t* pProgress) { |
1485 |
#if DEBUG |
#if DEBUG_RIFF |
1486 |
std::cout << "List::LoadSubChunks()"; |
std::cout << "List::LoadSubChunks()"; |
1487 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1488 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1489 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1490 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1493 |
#else |
#else |
1494 |
if (!pFile->hFileRead) return; |
if (!pFile->hFileRead) return; |
1495 |
#endif |
#endif |
1496 |
unsigned long uiOriginalPos = GetPos(); |
file_offset_t ullOriginalPos = GetPos(); |
1497 |
SetPos(0); // jump to beginning of list chunk body |
SetPos(0); // jump to beginning of list chunk body |
1498 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE(pFile->FileOffsetSize)) { |
1499 |
Chunk* ck; |
Chunk* ck; |
1500 |
uint32_t ckid; |
uint32_t ckid; |
1501 |
Read(&ckid, 4, 1); |
Read(&ckid, 4, 1); |
1502 |
#if DEBUG |
#if DEBUG_RIFF |
1503 |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
1504 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1505 |
if (ckid == CHUNK_ID_LIST) { |
if (ckid == CHUNK_ID_LIST) { |
1506 |
ck = new RIFF::List(pFile, ulStartPos + ulPos - 4, this); |
ck = new RIFF::List(pFile, ullStartPos + ullPos - 4, this); |
1507 |
SetPos(ck->GetSize() + LIST_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + LIST_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos); |
1508 |
} |
} |
1509 |
else { // simple chunk |
else { // simple chunk |
1510 |
ck = new RIFF::Chunk(pFile, ulStartPos + ulPos - 4, this); |
ck = new RIFF::Chunk(pFile, ullStartPos + ullPos - 4, this); |
1511 |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos); |
1512 |
} |
} |
1513 |
pSubChunks->push_back(ck); |
pSubChunks->push_back(ck); |
1514 |
(*pSubChunksMap)[ckid] = ck; |
(*pSubChunksMap)[ckid] = ck; |
1515 |
if (GetPos() % 2 != 0) SetPos(1, RIFF::stream_curpos); // jump over pad byte |
if (GetPos() % 2 != 0) SetPos(1, RIFF::stream_curpos); // jump over pad byte |
1516 |
} |
} |
1517 |
SetPos(uiOriginalPos); // restore position before this call |
SetPos(ullOriginalPos); // restore position before this call |
1518 |
} |
} |
1519 |
|
if (pProgress) |
1520 |
|
__notify_progress(pProgress, 1.0); // notify done |
1521 |
} |
} |
1522 |
|
|
1523 |
void List::LoadSubChunksRecursively() { |
void List::LoadSubChunksRecursively(progress_t* pProgress) { |
1524 |
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList()) |
const int n = (int) CountSubLists(); |
1525 |
pList->LoadSubChunksRecursively(); |
int i = 0; |
1526 |
|
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) { |
1527 |
|
if (pProgress) { |
1528 |
|
// divide local progress into subprogress |
1529 |
|
progress_t subprogress; |
1530 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1531 |
|
// do the actual work |
1532 |
|
pList->LoadSubChunksRecursively(&subprogress); |
1533 |
|
} else |
1534 |
|
pList->LoadSubChunksRecursively(NULL); |
1535 |
|
} |
1536 |
|
if (pProgress) |
1537 |
|
__notify_progress(pProgress, 1.0); // notify done |
1538 |
} |
} |
1539 |
|
|
1540 |
/** @brief Write list chunk persistently e.g. to disk. |
/** @brief Write list chunk persistently e.g. to disk. |
1543 |
* subchunks (including sub list chunks) will be stored recursively as |
* subchunks (including sub list chunks) will be stored recursively as |
1544 |
* well. |
* well. |
1545 |
* |
* |
1546 |
* @param ulWritePos - position within the "physical" file where this |
* @param ullWritePos - position within the "physical" file where this |
1547 |
* list chunk should be written to |
* list chunk should be written to |
1548 |
* @param ulCurrentDataOffset - offset of current (old) data within |
* @param ullCurrentDataOffset - offset of current (old) data within |
1549 |
* the file |
* the file |
1550 |
|
* @param pProgress - optional: callback function for progress notification |
1551 |
* @returns new write position in the "physical" file, that is |
* @returns new write position in the "physical" file, that is |
1552 |
* \a ulWritePos incremented by this list chunk's new size |
* \a ullWritePos incremented by this list chunk's new size |
1553 |
* (including its header size of course) |
* (including its header size of course) |
1554 |
*/ |
*/ |
1555 |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
file_offset_t List::WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress) { |
1556 |
const unsigned long ulOriginalPos = ulWritePos; |
const file_offset_t ullOriginalPos = ullWritePos; |
1557 |
ulWritePos += LIST_HEADER_SIZE; |
ullWritePos += LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1558 |
|
|
1559 |
if (pFile->Mode != stream_mode_read_write) |
if (pFile->Mode != stream_mode_read_write) |
1560 |
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
1561 |
|
|
1562 |
// write all subchunks (including sub list chunks) recursively |
// write all subchunks (including sub list chunks) recursively |
1563 |
if (pSubChunks) { |
if (pSubChunks) { |
1564 |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
size_t i = 0; |
1565 |
ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset); |
const size_t n = pSubChunks->size(); |
1566 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) { |
1567 |
|
if (pProgress) { |
1568 |
|
// divide local progress into subprogress for loading current Instrument |
1569 |
|
progress_t subprogress; |
1570 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1571 |
|
// do the actual work |
1572 |
|
ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress); |
1573 |
|
} else |
1574 |
|
ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, NULL); |
1575 |
} |
} |
1576 |
} |
} |
1577 |
|
|
1578 |
// update this list chunk's header |
// update this list chunk's header |
1579 |
CurrentChunkSize = NewChunkSize = ulWritePos - ulOriginalPos - LIST_HEADER_SIZE; |
ullCurrentChunkSize = ullNewChunkSize = ullWritePos - ullOriginalPos - LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1580 |
WriteHeader(ulOriginalPos); |
WriteHeader(ullOriginalPos); |
1581 |
|
|
1582 |
// offset of this list chunk in new written file may have changed |
// offset of this list chunk in new written file may have changed |
1583 |
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1584 |
|
|
1585 |
|
if (pProgress) |
1586 |
|
__notify_progress(pProgress, 1.0); // notify done |
1587 |
|
|
1588 |
return ulWritePos; |
return ullWritePos; |
1589 |
} |
} |
1590 |
|
|
1591 |
void List::__resetPos() { |
void List::__resetPos() { |
1600 |
/** |
/** |
1601 |
* Returns string representation of the lists's id |
* Returns string representation of the lists's id |
1602 |
*/ |
*/ |
1603 |
String List::GetListTypeString() { |
String List::GetListTypeString() const { |
1604 |
return convertToString(ListType); |
return convertToString(ListType); |
1605 |
} |
} |
1606 |
|
|
1609 |
// *************** File *************** |
// *************** File *************** |
1610 |
// * |
// * |
1611 |
|
|
|
//HACK: to avoid breaking DLL compatibility to older versions of libgig we roll the new std::set<Chunk*> into the old std::list<Chunk*> container, should be replaced on member variable level soon though |
|
|
#define _GET_RESIZED_CHUNKS() \ |
|
|
(reinterpret_cast<std::set<Chunk*>*>(ResizedChunks.front())) |
|
|
|
|
1612 |
/** @brief Create new RIFF file. |
/** @brief Create new RIFF file. |
1613 |
* |
* |
1614 |
* Use this constructor if you want to create a new RIFF file completely |
* Use this constructor if you want to create a new RIFF file completely |
1624 |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1625 |
*/ |
*/ |
1626 |
File::File(uint32_t FileType) |
File::File(uint32_t FileType) |
1627 |
: List(this), bIsNewFile(true), Layout(layout_standard) |
: List(this), bIsNewFile(true), Layout(layout_standard), |
1628 |
|
FileOffsetPreference(offset_size_auto) |
1629 |
{ |
{ |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
|
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
|
1630 |
#if defined(WIN32) |
#if defined(WIN32) |
1631 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1632 |
#else |
#else |
1634 |
#endif |
#endif |
1635 |
Mode = stream_mode_closed; |
Mode = stream_mode_closed; |
1636 |
bEndianNative = true; |
bEndianNative = true; |
|
ulStartPos = RIFF_HEADER_SIZE; |
|
1637 |
ListType = FileType; |
ListType = FileType; |
1638 |
|
FileOffsetSize = 4; |
1639 |
|
ullStartPos = RIFF_HEADER_SIZE(FileOffsetSize); |
1640 |
} |
} |
1641 |
|
|
1642 |
/** @brief Load existing RIFF file. |
/** @brief Load existing RIFF file. |
1644 |
* Loads an existing RIFF file with all its chunks. |
* Loads an existing RIFF file with all its chunks. |
1645 |
* |
* |
1646 |
* @param path - path and file name of the RIFF file to open |
* @param path - path and file name of the RIFF file to open |
1647 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occurred while trying to load the |
1648 |
* given RIFF file |
* given RIFF file |
1649 |
*/ |
*/ |
1650 |
File::File(const String& path) |
File::File(const String& path) |
1651 |
: List(this), Filename(path), bIsNewFile(false), Layout(layout_standard) |
: List(this), Filename(path), bIsNewFile(false), Layout(layout_standard), |
1652 |
|
FileOffsetPreference(offset_size_auto) |
1653 |
{ |
{ |
1654 |
#if DEBUG |
#if DEBUG_RIFF |
1655 |
std::cout << "File::File("<<path<<")" << std::endl; |
std::cout << "File::File("<<path<<")" << std::endl; |
1656 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1657 |
bEndianNative = true; |
bEndianNative = true; |
1658 |
|
FileOffsetSize = 4; |
1659 |
try { |
try { |
1660 |
__openExistingFile(path); |
__openExistingFile(path); |
1661 |
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1690 |
* first chunk found in the file) |
* first chunk found in the file) |
1691 |
* @param Endian - whether the file uses little endian or big endian layout |
* @param Endian - whether the file uses little endian or big endian layout |
1692 |
* @param layout - general file structure type |
* @param layout - general file structure type |
1693 |
* @throws RIFF::Exception if error occured while trying to load the |
* @param fileOffsetSize - (optional) preference how to deal with large files |
1694 |
|
* @throws RIFF::Exception if error occurred while trying to load the |
1695 |
* given RIFF-alike file |
* given RIFF-alike file |
1696 |
*/ |
*/ |
1697 |
File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout) |
File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize) |
1698 |
: List(this), Filename(path), bIsNewFile(false), Layout(layout) |
: List(this), Filename(path), bIsNewFile(false), Layout(layout), |
1699 |
|
FileOffsetPreference(fileOffsetSize) |
1700 |
{ |
{ |
1701 |
SetByteOrder(Endian); |
SetByteOrder(Endian); |
1702 |
|
if (fileOffsetSize < offset_size_auto || fileOffsetSize > offset_size_64bit) |
1703 |
|
throw Exception("Invalid RIFF::offset_size_t"); |
1704 |
|
FileOffsetSize = 4; |
1705 |
try { |
try { |
1706 |
__openExistingFile(path, &FileType); |
__openExistingFile(path, &FileType); |
1707 |
} |
} |
1718 |
* @param path - path and file name of the RIFF file or RIFF-alike file to |
* @param path - path and file name of the RIFF file or RIFF-alike file to |
1719 |
* be opened |
* be opened |
1720 |
* @param FileType - (optional) expected chunk ID of first chunk in file |
* @param FileType - (optional) expected chunk ID of first chunk in file |
1721 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occurred while trying to load the |
1722 |
* given RIFF file or RIFF-alike file |
* given RIFF file or RIFF-alike file |
1723 |
*/ |
*/ |
1724 |
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
|
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
|
1725 |
#if POSIX |
#if POSIX |
1726 |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1727 |
if (hFileRead <= 0) { |
if (hFileRead == -1) { |
1728 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1729 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
String sError = strerror(errno); |
1730 |
|
throw RIFF::Exception("Can't open \"" + path + "\": " + sError); |
1731 |
} |
} |
1732 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1733 |
hFileRead = hFileWrite = CreateFile( |
hFileRead = hFileWrite = CreateFile( |
1746 |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1747 |
#endif // POSIX |
#endif // POSIX |
1748 |
Mode = stream_mode_read; |
Mode = stream_mode_read; |
1749 |
|
|
1750 |
|
// determine RIFF file offset size to be used (in RIFF chunk headers) |
1751 |
|
// according to the current file offset preference |
1752 |
|
FileOffsetSize = FileOffsetSizeFor(GetCurrentFileSize()); |
1753 |
|
|
1754 |
switch (Layout) { |
switch (Layout) { |
1755 |
case layout_standard: // this is a normal RIFF file |
case layout_standard: // this is a normal RIFF file |
1756 |
ulStartPos = RIFF_HEADER_SIZE; |
ullStartPos = RIFF_HEADER_SIZE(FileOffsetSize); |
1757 |
ReadHeader(0); |
ReadHeader(0); |
1758 |
if (FileType && ChunkID != *FileType) |
if (FileType && ChunkID != *FileType) |
1759 |
throw RIFF::Exception("Invalid file container ID"); |
throw RIFF::Exception("Invalid file container ID"); |
1760 |
break; |
break; |
1761 |
case layout_flat: // non-standard RIFF-alike file |
case layout_flat: // non-standard RIFF-alike file |
1762 |
ulStartPos = 0; |
ullStartPos = 0; |
1763 |
NewChunkSize = CurrentChunkSize = GetFileSize(); |
ullNewChunkSize = ullCurrentChunkSize = GetCurrentFileSize(); |
1764 |
if (FileType) { |
if (FileType) { |
1765 |
uint32_t ckid; |
uint32_t ckid; |
1766 |
if (Read(&ckid, 4, 1) != 4) { |
if (Read(&ckid, 4, 1) != 4) { |
1776 |
} |
} |
1777 |
} |
} |
1778 |
|
|
1779 |
String File::GetFileName() { |
String File::GetFileName() const { |
1780 |
return Filename; |
return Filename; |
1781 |
} |
} |
1782 |
|
|
1784 |
Filename = path; |
Filename = path; |
1785 |
} |
} |
1786 |
|
|
1787 |
stream_mode_t File::GetMode() { |
stream_mode_t File::GetMode() const { |
1788 |
return Mode; |
return Mode; |
1789 |
} |
} |
1790 |
|
|
1809 |
#if POSIX |
#if POSIX |
1810 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1811 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1812 |
if (hFileRead < 0) { |
if (hFileRead == -1) { |
1813 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1814 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
String sError = strerror(errno); |
1815 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode: " + sError); |
1816 |
} |
} |
1817 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1818 |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1839 |
#if POSIX |
#if POSIX |
1840 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1841 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
1842 |
if (hFileRead < 0) { |
if (hFileRead == -1) { |
1843 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1844 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
String sError = strerror(errno); |
1845 |
|
throw Exception("Could not open file \"" + Filename + "\" in read+write mode: " + sError); |
1846 |
} |
} |
1847 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1848 |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1880 |
#if POSIX |
#if POSIX |
1881 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1882 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
1883 |
|
hFileRead = hFileWrite = 0; |
1884 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1885 |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1886 |
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1887 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1888 |
#else |
#else |
1889 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
1890 |
if (hFileWrite) fclose(hFileWrite); |
if (hFileWrite) fclose(hFileWrite); |
1891 |
|
hFileRead = hFileWrite = NULL; |
1892 |
#endif |
#endif |
|
hFileRead = hFileWrite = 0; |
|
1893 |
break; |
break; |
1894 |
default: |
default: |
1895 |
throw Exception("Unknown file access mode"); |
throw Exception("Unknown file access mode"); |
1920 |
/** @brief Save changes to same file. |
/** @brief Save changes to same file. |
1921 |
* |
* |
1922 |
* Make all changes of all chunks persistent by writing them to the |
* Make all changes of all chunks persistent by writing them to the |
1923 |
* actual (same) file. The file might temporarily grow to a higher size |
* actual (same) file. |
|
* than it will have at the end of the saving process, in case chunks |
|
|
* were grown. |
|
1924 |
* |
* |
1925 |
|
* @param pProgress - optional: callback function for progress notification |
1926 |
* @throws RIFF::Exception if there is an empty chunk or empty list |
* @throws RIFF::Exception if there is an empty chunk or empty list |
1927 |
* chunk or any kind of IO error occured |
* chunk or any kind of IO error occurred |
1928 |
*/ |
*/ |
1929 |
void File::Save() { |
void File::Save(progress_t* pProgress) { |
1930 |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1931 |
if (Layout == layout_flat) |
if (Layout == layout_flat) |
1932 |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1933 |
|
|
1934 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1935 |
LoadSubChunksRecursively(); |
if (pProgress) { |
1936 |
|
// divide progress into subprogress |
1937 |
|
progress_t subprogress; |
1938 |
|
__divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress |
1939 |
|
// do the actual work |
1940 |
|
LoadSubChunksRecursively(&subprogress); |
1941 |
|
// notify subprogress done |
1942 |
|
__notify_progress(&subprogress, 1.f); |
1943 |
|
} else |
1944 |
|
LoadSubChunksRecursively(NULL); |
1945 |
|
|
1946 |
// reopen file in write mode |
// reopen file in write mode |
1947 |
SetMode(stream_mode_read_write); |
SetMode(stream_mode_read_write); |
1948 |
|
|
1949 |
|
// get the current file size as it is now still physically stored on disk |
1950 |
|
const file_offset_t workingFileSize = GetCurrentFileSize(); |
1951 |
|
|
1952 |
|
// get the overall file size required to save this file |
1953 |
|
const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference); |
1954 |
|
|
1955 |
|
// determine whether this file will yield in a large file (>=4GB) and |
1956 |
|
// the RIFF file offset size to be used accordingly for all chunks |
1957 |
|
FileOffsetSize = FileOffsetSizeFor(newFileSize); |
1958 |
|
|
1959 |
// to be able to save the whole file without loading everything into |
// to be able to save the whole file without loading everything into |
1960 |
// RAM and without having to store the data in a temporary file, we |
// RAM and without having to store the data in a temporary file, we |
1961 |
// enlarge the file with the sum of all _positive_ chunk size |
// enlarge the file with the overall positive file size change, |
1962 |
// changes, move current data towards the end of the file with the |
// then move current data towards the end of the file by the calculated |
1963 |
// calculated sum and finally update / rewrite the file by copying |
// positive file size difference and finally update / rewrite the file |
1964 |
// the old data back to the right position at the beginning of the file |
// by copying the old data back to the right position at the beginning |
1965 |
|
// of the file |
|
// first we sum up all positive chunk size changes (and skip all negative ones) |
|
|
unsigned long ulPositiveSizeDiff = 0; |
|
|
std::set<Chunk*>* resizedChunks = _GET_RESIZED_CHUNKS(); |
|
|
for (std::set<Chunk*>::const_iterator iter = resizedChunks->begin(), end = resizedChunks->end(); iter != end; ++iter) { |
|
|
if ((*iter)->GetNewSize() == 0) { |
|
|
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
|
|
} |
|
|
unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; |
|
|
unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2; |
|
|
if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; |
|
|
} |
|
|
|
|
|
unsigned long ulWorkingFileSize = GetFileSize(); |
|
1966 |
|
|
1967 |
// if there are positive size changes... |
// if there are positive size changes... |
1968 |
if (ulPositiveSizeDiff > 0) { |
file_offset_t positiveSizeDiff = 0; |
1969 |
|
if (newFileSize > workingFileSize) { |
1970 |
|
positiveSizeDiff = newFileSize - workingFileSize; |
1971 |
|
|
1972 |
|
// divide progress into subprogress |
1973 |
|
progress_t subprogress; |
1974 |
|
if (pProgress) |
1975 |
|
__divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress |
1976 |
|
|
1977 |
// ... we enlarge this file first ... |
// ... we enlarge this file first ... |
1978 |
ulWorkingFileSize += ulPositiveSizeDiff; |
ResizeFile(newFileSize); |
1979 |
ResizeFile(ulWorkingFileSize); |
|
1980 |
// ... and move current data by the same amount towards end of file. |
// ... and move current data by the same amount towards end of file. |
1981 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
|
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
|
1982 |
#if defined(WIN32) |
#if defined(WIN32) |
1983 |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1984 |
#else |
#else |
1985 |
int iBytesMoved = 1; |
ssize_t iBytesMoved = 1; |
1986 |
#endif |
#endif |
1987 |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
for (file_offset_t ullPos = workingFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) { |
1988 |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
iBytesMoved = (ullPos < 4096) ? ullPos : 4096; |
1989 |
ulPos -= iBytesMoved; |
ullPos -= iBytesMoved; |
1990 |
#if POSIX |
#if POSIX |
1991 |
lseek(hFileRead, ulPos, SEEK_SET); |
lseek(hFileRead, ullPos, SEEK_SET); |
1992 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1993 |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
lseek(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET); |
1994 |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1995 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1996 |
SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
1997 |
|
liFilePos.QuadPart = ullPos; |
1998 |
|
SetFilePointerEx(hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1999 |
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
2000 |
SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN); |
liFilePos.QuadPart = ullPos + positiveSizeDiff; |
2001 |
|
SetFilePointerEx(hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
2002 |
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
2003 |
#else |
#else |
2004 |
fseek(hFileRead, ulPos, SEEK_SET); |
fseeko(hFileRead, ullPos, SEEK_SET); |
2005 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
2006 |
fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
fseeko(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET); |
2007 |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
2008 |
#endif |
#endif |
2009 |
|
if (pProgress && !(iNotif % 8) && iBytesMoved > 0) |
2010 |
|
__notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize)); |
2011 |
} |
} |
2012 |
delete[] pCopyBuffer; |
delete[] pCopyBuffer; |
2013 |
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
2014 |
|
|
2015 |
|
if (pProgress) |
2016 |
|
__notify_progress(&subprogress, 1.f); // notify subprogress done |
2017 |
} |
} |
2018 |
|
|
2019 |
// rebuild / rewrite complete RIFF tree |
// rebuild / rewrite complete RIFF tree ... |
2020 |
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
|
2021 |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
// divide progress into subprogress |
2022 |
|
progress_t subprogress; |
2023 |
|
if (pProgress) |
2024 |
|
__divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress |
2025 |
|
// do the actual work |
2026 |
|
const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, pProgress ? &subprogress : NULL); |
2027 |
|
const file_offset_t finalActualSize = __GetFileSize(hFileWrite); |
2028 |
|
// notify subprogress done |
2029 |
|
if (pProgress) |
2030 |
|
__notify_progress(&subprogress, 1.f); |
2031 |
|
|
2032 |
// resize file to the final size |
// resize file to the final size |
2033 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (finalSize < finalActualSize) ResizeFile(finalSize); |
2034 |
|
|
2035 |
// forget all resized chunks |
if (pProgress) |
2036 |
resizedChunks->clear(); |
__notify_progress(pProgress, 1.0); // notify done |
2037 |
} |
} |
2038 |
|
|
2039 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
2048 |
* the new file (given by \a path) afterwards. |
* the new file (given by \a path) afterwards. |
2049 |
* |
* |
2050 |
* @param path - path and file name where everything should be written to |
* @param path - path and file name where everything should be written to |
2051 |
|
* @param pProgress - optional: callback function for progress notification |
2052 |
*/ |
*/ |
2053 |
void File::Save(const String& path) { |
void File::Save(const String& path, progress_t* pProgress) { |
2054 |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
2055 |
|
|
2056 |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
2058 |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
2059 |
|
|
2060 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
2061 |
LoadSubChunksRecursively(); |
if (pProgress) { |
2062 |
|
// divide progress into subprogress |
2063 |
|
progress_t subprogress; |
2064 |
|
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress |
2065 |
|
// do the actual work |
2066 |
|
LoadSubChunksRecursively(&subprogress); |
2067 |
|
// notify subprogress done |
2068 |
|
__notify_progress(&subprogress, 1.f); |
2069 |
|
} else |
2070 |
|
LoadSubChunksRecursively(NULL); |
2071 |
|
|
2072 |
if (!bIsNewFile) SetMode(stream_mode_read); |
if (!bIsNewFile) SetMode(stream_mode_read); |
2073 |
// open the other (new) file for writing and truncate it to zero size |
// open the other (new) file for writing and truncate it to zero size |
2074 |
#if POSIX |
#if POSIX |
2075 |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
2076 |
if (hFileWrite < 0) { |
if (hFileWrite == -1) { |
2077 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
2078 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
String sError = strerror(errno); |
2079 |
|
throw Exception("Could not open file \"" + path + "\" for writing: " + sError); |
2080 |
} |
} |
2081 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2082 |
hFileWrite = CreateFile( |
hFileWrite = CreateFile( |
2097 |
#endif // POSIX |
#endif // POSIX |
2098 |
Mode = stream_mode_read_write; |
Mode = stream_mode_read_write; |
2099 |
|
|
2100 |
|
// get the overall file size required to save this file |
2101 |
|
const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference); |
2102 |
|
|
2103 |
|
// determine whether this file will yield in a large file (>=4GB) and |
2104 |
|
// the RIFF file offset size to be used accordingly for all chunks |
2105 |
|
FileOffsetSize = FileOffsetSizeFor(newFileSize); |
2106 |
|
|
2107 |
// write complete RIFF tree to the other (new) file |
// write complete RIFF tree to the other (new) file |
2108 |
unsigned long ulTotalSize = WriteChunk(0, 0); |
file_offset_t ullTotalSize; |
2109 |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
if (pProgress) { |
2110 |
|
// divide progress into subprogress |
2111 |
|
progress_t subprogress; |
2112 |
|
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress |
2113 |
|
// do the actual work |
2114 |
|
ullTotalSize = WriteChunk(0, 0, &subprogress); |
2115 |
|
// notify subprogress done |
2116 |
|
__notify_progress(&subprogress, 1.f); |
2117 |
|
} else |
2118 |
|
ullTotalSize = WriteChunk(0, 0, NULL); |
2119 |
|
|
2120 |
// resize file to the final size (if the file was originally larger) |
file_offset_t ullActualSize = __GetFileSize(hFileWrite); |
|
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
|
2121 |
|
|
2122 |
// forget all resized chunks |
// resize file to the final size (if the file was originally larger) |
2123 |
_GET_RESIZED_CHUNKS()->clear(); |
if (ullActualSize > ullTotalSize) ResizeFile(ullTotalSize); |
2124 |
|
|
2125 |
#if POSIX |
#if POSIX |
2126 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
2136 |
bIsNewFile = false; |
bIsNewFile = false; |
2137 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
2138 |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
2139 |
|
|
2140 |
|
if (pProgress) |
2141 |
|
__notify_progress(pProgress, 1.0); // notify done |
2142 |
} |
} |
2143 |
|
|
2144 |
void File::ResizeFile(unsigned long ulNewSize) { |
void File::ResizeFile(file_offset_t ullNewSize) { |
2145 |
#if POSIX |
#if POSIX |
2146 |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
if (ftruncate(hFileWrite, ullNewSize) < 0) |
2147 |
throw Exception("Could not resize file \"" + Filename + "\""); |
throw Exception("Could not resize file \"" + Filename + "\""); |
2148 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2149 |
|
LARGE_INTEGER liFilePos; |
2150 |
|
liFilePos.QuadPart = ullNewSize; |
2151 |
if ( |
if ( |
2152 |
SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER || |
!SetFilePointerEx(hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN) || |
2153 |
!SetEndOfFile(hFileWrite) |
!SetEndOfFile(hFileWrite) |
2154 |
) throw Exception("Could not resize file \"" + Filename + "\""); |
) throw Exception("Could not resize file \"" + Filename + "\""); |
2155 |
#else |
#else |
2159 |
} |
} |
2160 |
|
|
2161 |
File::~File() { |
File::~File() { |
2162 |
#if DEBUG |
#if DEBUG_RIFF |
2163 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
2164 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
2165 |
Cleanup(); |
Cleanup(); |
2166 |
} |
} |
2167 |
|
|
2168 |
/** |
/** |
2169 |
* Returns @c true if this file has been created new from scratch and |
* Returns @c true if this file has been created new from scratch and |
2170 |
* has not been stored to disk yet. |
* has not been stored to disk yet. |
2183 |
#endif // POSIX |
#endif // POSIX |
2184 |
DeleteChunkList(); |
DeleteChunkList(); |
2185 |
pFile = NULL; |
pFile = NULL; |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
|
|
delete _GET_RESIZED_CHUNKS(); |
|
2186 |
} |
} |
2187 |
|
|
2188 |
void File::LogAsResized(Chunk* pResizedChunk) { |
/** |
2189 |
_GET_RESIZED_CHUNKS()->insert(pResizedChunk); |
* Returns the current size of this file (in bytes) as it is currently |
2190 |
|
* yet stored on disk. If this file does not yet exist on disk (i.e. when |
2191 |
|
* this RIFF File has just been created from scratch and Save() has not |
2192 |
|
* been called yet) then this method returns 0. |
2193 |
|
*/ |
2194 |
|
file_offset_t File::GetCurrentFileSize() const { |
2195 |
|
file_offset_t size = 0; |
2196 |
|
try { |
2197 |
|
size = __GetFileSize(hFileRead); |
2198 |
|
} catch (...) { |
2199 |
|
size = 0; |
2200 |
|
} |
2201 |
|
return size; |
2202 |
|
} |
2203 |
|
|
2204 |
|
/** |
2205 |
|
* Returns the required size (in bytes) for this RIFF File to be saved to |
2206 |
|
* disk. The precise size of the final file on disk depends on the RIFF |
2207 |
|
* file offset size actually used internally in all headers of the RIFF |
2208 |
|
* chunks. By default libgig handles the required file offset size |
2209 |
|
* automatically for you; that means it is using 32 bit offsets for files |
2210 |
|
* smaller than 4 GB and 64 bit offsets for files equal or larger than |
2211 |
|
* 4 GB. You may however also override this default behavior by passing the |
2212 |
|
* respective option to the RIFF File constructor to force one particular |
2213 |
|
* offset size. In the latter case this method will return the file size |
2214 |
|
* for the requested forced file offset size that will be used when calling |
2215 |
|
* Save() later on. |
2216 |
|
* |
2217 |
|
* You may also use the overridden method below to get the file size for |
2218 |
|
* an arbitrary other file offset size instead. |
2219 |
|
* |
2220 |
|
* @see offset_size_t |
2221 |
|
* @see GetFileOffsetSize() |
2222 |
|
*/ |
2223 |
|
file_offset_t File::GetRequiredFileSize() { |
2224 |
|
return GetRequiredFileSize(FileOffsetPreference); |
2225 |
|
} |
2226 |
|
|
2227 |
|
/** |
2228 |
|
* Returns the rquired size (in bytes) for this RIFF file to be saved to |
2229 |
|
* disk, assuming the passed @a fileOffsestSize would be used for the |
2230 |
|
* Save() operation. |
2231 |
|
* |
2232 |
|
* This overridden method essentialy behaves like the above method, with |
2233 |
|
* the difference that you must provide a specific RIFF @a fileOffsetSize |
2234 |
|
* for calculating the theoretical final file size. |
2235 |
|
* |
2236 |
|
* @see GetFileOffsetSize() |
2237 |
|
*/ |
2238 |
|
file_offset_t File::GetRequiredFileSize(offset_size_t fileOffsetSize) { |
2239 |
|
switch (fileOffsetSize) { |
2240 |
|
case offset_size_auto: { |
2241 |
|
file_offset_t fileSize = GetRequiredFileSize(offset_size_32bit); |
2242 |
|
if (fileSize >> 32) |
2243 |
|
return GetRequiredFileSize(offset_size_64bit); |
2244 |
|
else |
2245 |
|
return fileSize; |
2246 |
|
} |
2247 |
|
case offset_size_32bit: break; |
2248 |
|
case offset_size_64bit: break; |
2249 |
|
default: throw Exception("Internal error: Invalid RIFF::offset_size_t"); |
2250 |
|
} |
2251 |
|
return RequiredPhysicalSize(FileOffsetSize); |
2252 |
|
} |
2253 |
|
|
2254 |
|
int File::FileOffsetSizeFor(file_offset_t fileSize) const { |
2255 |
|
switch (FileOffsetPreference) { |
2256 |
|
case offset_size_auto: |
2257 |
|
return (fileSize >> 32) ? 8 : 4; |
2258 |
|
case offset_size_32bit: |
2259 |
|
return 4; |
2260 |
|
case offset_size_64bit: |
2261 |
|
return 8; |
2262 |
|
default: |
2263 |
|
throw Exception("Internal error: Invalid RIFF::offset_size_t"); |
2264 |
|
} |
2265 |
} |
} |
2266 |
|
|
2267 |
void File::UnlogResized(Chunk* pResizedChunk) { |
/** |
2268 |
_GET_RESIZED_CHUNKS()->erase(pResizedChunk); |
* Returns the current size (in bytes) of file offsets stored in the |
2269 |
|
* headers of all chunks of this file. |
2270 |
|
* |
2271 |
|
* Most RIFF files are using 32 bit file offsets internally, which limits |
2272 |
|
* them to a maximum file size of less than 4 GB though. In contrast to the |
2273 |
|
* common standard, this RIFF File class implementation supports handling of |
2274 |
|
* RIFF files equal or larger than 4 GB. In such cases 64 bit file offsets |
2275 |
|
* have to be used in all headers of all RIFF Chunks when being stored to a |
2276 |
|
* physical file. libgig by default automatically selects the correct file |
2277 |
|
* offset size for you. You may however also force one particular file |
2278 |
|
* offset size by supplying the respective option to the RIFF::File |
2279 |
|
* constructor. |
2280 |
|
* |
2281 |
|
* This method can be used to check which RIFF file offset size is currently |
2282 |
|
* being used for this RIFF File. |
2283 |
|
* |
2284 |
|
* @returns current RIFF file offset size used (in bytes) |
2285 |
|
* @see offset_size_t |
2286 |
|
*/ |
2287 |
|
int File::GetFileOffsetSize() const { |
2288 |
|
return FileOffsetSize; |
2289 |
} |
} |
2290 |
|
|
2291 |
unsigned long File::GetFileSize() { |
/** |
2292 |
return __GetFileSize(hFileRead); |
* Returns the required size (in bytes) of file offsets stored in the |
2293 |
|
* headers of all chunks of this file if the current RIFF tree would be |
2294 |
|
* saved to disk by calling Save(). |
2295 |
|
* |
2296 |
|
* See GetFileOffsetSize() for mor details about RIFF file offsets. |
2297 |
|
* |
2298 |
|
* @returns RIFF file offset size required (in bytes) if being saved |
2299 |
|
* @see offset_size_t |
2300 |
|
*/ |
2301 |
|
int File::GetRequiredFileOffsetSize() { |
2302 |
|
return FileOffsetSizeFor(GetCurrentFileSize()); |
2303 |
} |
} |
2304 |
|
|
2305 |
#if POSIX |
#if POSIX |
2306 |
unsigned long File::__GetFileSize(int hFile) { |
file_offset_t File::__GetFileSize(int hFile) const { |
2307 |
struct stat filestat; |
struct stat filestat; |
2308 |
fstat(hFile, &filestat); |
if (fstat(hFile, &filestat) == -1) |
2309 |
long size = filestat.st_size; |
throw Exception("POSIX FS error: could not determine file size"); |
2310 |
return size; |
return filestat.st_size; |
2311 |
} |
} |
2312 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2313 |
unsigned long File::__GetFileSize(HANDLE hFile) { |
file_offset_t File::__GetFileSize(HANDLE hFile) const { |
2314 |
DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/); |
LARGE_INTEGER size; |
2315 |
if (dwSize == INVALID_FILE_SIZE) |
if (!GetFileSizeEx(hFile, &size)) |
2316 |
throw Exception("Windows FS error: could not determine file size"); |
throw Exception("Windows FS error: could not determine file size"); |
2317 |
return dwSize; |
return size.QuadPart; |
2318 |
} |
} |
2319 |
#else // standard C functions |
#else // standard C functions |
2320 |
unsigned long File::__GetFileSize(FILE* hFile) { |
file_offset_t File::__GetFileSize(FILE* hFile) const { |
2321 |
long curpos = ftell(hFile); |
off_t curpos = ftello(hFile); |
2322 |
fseek(hFile, 0, SEEK_END); |
if (fseeko(hFile, 0, SEEK_END) == -1) |
2323 |
long size = ftell(hFile); |
throw Exception("FS error: could not determine file size"); |
2324 |
fseek(hFile, curpos, SEEK_SET); |
off_t size = ftello(hFile); |
2325 |
|
fseeko(hFile, curpos, SEEK_SET); |
2326 |
return size; |
return size; |
2327 |
} |
} |
2328 |
#endif |
#endif |
2331 |
// *************** Exception *************** |
// *************** Exception *************** |
2332 |
// * |
// * |
2333 |
|
|
2334 |
|
Exception::Exception() { |
2335 |
|
} |
2336 |
|
|
2337 |
|
Exception::Exception(String format, ...) { |
2338 |
|
va_list arg; |
2339 |
|
va_start(arg, format); |
2340 |
|
Message = assemble(format, arg); |
2341 |
|
va_end(arg); |
2342 |
|
} |
2343 |
|
|
2344 |
|
Exception::Exception(String format, va_list arg) { |
2345 |
|
Message = assemble(format, arg); |
2346 |
|
} |
2347 |
|
|
2348 |
void Exception::PrintMessage() { |
void Exception::PrintMessage() { |
2349 |
std::cout << "RIFF::Exception: " << Message << std::endl; |
std::cout << "RIFF::Exception: " << Message << std::endl; |
2350 |
} |
} |
2351 |
|
|
2352 |
|
String Exception::assemble(String format, va_list arg) { |
2353 |
|
char* buf = NULL; |
2354 |
|
vasprintf(&buf, format.c_str(), arg); |
2355 |
|
String s = buf; |
2356 |
|
free(buf); |
2357 |
|
return s; |
2358 |
|
} |
2359 |
|
|
2360 |
|
|
2361 |
// *************** functions *************** |
// *************** functions *************** |
2362 |
// * |
// * |