1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Christian Schoenebeck * |
* Copyright (C) 2003-2007 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 * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
#if 1 |
|
24 |
|
#include <string.h> |
25 |
|
|
26 |
#include "RIFF.h" |
#include "RIFF.h" |
27 |
|
|
28 |
|
#include "helper.h" |
29 |
|
|
30 |
namespace RIFF { |
namespace RIFF { |
31 |
|
|
32 |
|
// *************** Internal functions ************** |
33 |
|
// * |
34 |
|
|
35 |
|
/// Returns a human readable path of the given chunk. |
36 |
|
String __resolveChunkPath(Chunk* pCk) { |
37 |
|
String sPath; |
38 |
|
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { |
39 |
|
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
40 |
|
List* pList = (List*) pChunk; |
41 |
|
sPath = "->'" + pList->GetListTypeString() + "'" + sPath; |
42 |
|
} else { |
43 |
|
sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath; |
44 |
|
} |
45 |
|
} |
46 |
|
return sPath; |
47 |
|
} |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
// *************** Chunk ************** |
// *************** Chunk ************** |
52 |
// * |
// * |
53 |
|
|
54 |
Chunk::Chunk() { |
Chunk::Chunk(File* pFile) { |
55 |
#if DEBUG |
#if DEBUG |
56 |
std::cout << "Chunk::Chunk()" << std::endl; |
std::cout << "Chunk::Chunk(File* pFile)" << std::endl; |
57 |
#endif // DEBUG |
#endif // DEBUG |
58 |
ulPos = 0; |
ulPos = 0; |
59 |
pParent = NULL; |
pParent = NULL; |
60 |
pChunkData = NULL; |
pChunkData = NULL; |
61 |
|
ulChunkDataSize = 0; |
62 |
|
ChunkID = CHUNK_ID_RIFF; |
63 |
|
this->pFile = pFile; |
64 |
} |
} |
65 |
|
|
66 |
#if POSIX |
Chunk::Chunk(File* pFile, unsigned long StartPos, List* Parent) { |
|
Chunk::Chunk(int hFile, unsigned long StartPos, bool EndianNative, List* Parent) { |
|
|
#else |
|
|
Chunk::Chunk(FILE* hFile, unsigned long StartPos, bool EndianNative, List* Parent) { |
|
|
#endif // POSIX |
|
67 |
#if DEBUG |
#if DEBUG |
68 |
std::cout << "Chunk::Chunk(FILE,ulong,bool,List*),StartPos=" << StartPos << std::endl; |
std::cout << "Chunk::Chunk(File*,ulong,bool,List*),StartPos=" << StartPos << std::endl; |
69 |
#endif // DEBUG |
#endif // DEBUG |
70 |
Chunk::hFile = hFile; |
this->pFile = pFile; |
71 |
ulStartPos = StartPos + CHUNK_HEADER_SIZE; |
ulStartPos = StartPos + CHUNK_HEADER_SIZE; |
|
bEndianNative = EndianNative; |
|
72 |
pParent = Parent; |
pParent = Parent; |
73 |
ulPos = 0; |
ulPos = 0; |
74 |
pChunkData = NULL; |
pChunkData = NULL; |
75 |
|
ulChunkDataSize = 0; |
76 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
77 |
} |
} |
78 |
|
|
79 |
|
Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize) { |
80 |
|
this->pFile = pFile; |
81 |
|
ulStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk |
82 |
|
this->pParent = pParent; |
83 |
|
ulPos = 0; |
84 |
|
pChunkData = NULL; |
85 |
|
ulChunkDataSize = 0; |
86 |
|
ChunkID = uiChunkID; |
87 |
|
CurrentChunkSize = 0; |
88 |
|
NewChunkSize = uiBodySize; |
89 |
|
} |
90 |
|
|
91 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
92 |
|
if (CurrentChunkSize != NewChunkSize) pFile->UnlogResized(this); |
93 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
94 |
} |
} |
95 |
|
|
98 |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
99 |
#endif // DEBUG |
#endif // DEBUG |
100 |
#if POSIX |
#if POSIX |
101 |
if (lseek(hFile, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
102 |
read(hFile, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
103 |
read(hFile, &ChunkSize, 4); |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
104 |
#else |
#elif defined(WIN32) |
105 |
if (!fseek(hFile, fPos, SEEK_SET)) { |
if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
106 |
fread(&ChunkID, 4, 1, hFile); |
DWORD dwBytesRead; |
107 |
fread(&ChunkSize, 4, 1, hFile); |
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
108 |
|
ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL); |
109 |
|
#else |
110 |
|
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
111 |
|
fread(&ChunkID, 4, 1, pFile->hFileRead); |
112 |
|
fread(&CurrentChunkSize, 4, 1, pFile->hFileRead); |
113 |
#endif // POSIX |
#endif // POSIX |
114 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
115 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
116 |
bEndianNative = false; |
pFile->bEndianNative = false; |
117 |
} |
} |
118 |
#else // little endian |
#else // little endian |
119 |
if (ChunkID == CHUNK_ID_RIFX) { |
if (ChunkID == CHUNK_ID_RIFX) { |
120 |
bEndianNative = false; |
pFile->bEndianNative = false; |
121 |
ChunkID = CHUNK_ID_RIFF; |
ChunkID = CHUNK_ID_RIFF; |
122 |
} |
} |
123 |
#endif // WORDS_BIGENDIAN |
#endif // WORDS_BIGENDIAN |
124 |
if (!bEndianNative) { |
if (!pFile->bEndianNative) { |
125 |
//swapBytes_32(&ChunkID); |
//swapBytes_32(&ChunkID); |
126 |
swapBytes_32(&ChunkSize); |
swapBytes_32(&CurrentChunkSize); |
127 |
} |
} |
128 |
#if DEBUG |
#if DEBUG |
129 |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
130 |
std::cout << "ckSize=" << ChunkSize << " "; |
std::cout << "ckSize=" << ChunkSize << " "; |
131 |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
132 |
#endif // DEBUG |
#endif // DEBUG |
133 |
|
NewChunkSize = CurrentChunkSize; |
134 |
|
} |
135 |
|
} |
136 |
|
|
137 |
|
void Chunk::WriteHeader(unsigned long fPos) { |
138 |
|
uint32_t uiNewChunkID = ChunkID; |
139 |
|
if (ChunkID == CHUNK_ID_RIFF) { |
140 |
|
#if WORDS_BIGENDIAN |
141 |
|
if (pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX; |
142 |
|
#else // little endian |
143 |
|
if (!pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX; |
144 |
|
#endif // WORDS_BIGENDIAN |
145 |
|
} |
146 |
|
|
147 |
|
uint32_t uiNewChunkSize = NewChunkSize; |
148 |
|
if (!pFile->bEndianNative) { |
149 |
|
swapBytes_32(&uiNewChunkSize); |
150 |
|
} |
151 |
|
|
152 |
|
#if POSIX |
153 |
|
if (lseek(pFile->hFileWrite, fPos, SEEK_SET) != -1) { |
154 |
|
write(pFile->hFileWrite, &uiNewChunkID, 4); |
155 |
|
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
156 |
} |
} |
157 |
|
#elif defined(WIN32) |
158 |
|
if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
159 |
|
DWORD dwBytesWritten; |
160 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
161 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL); |
162 |
|
} |
163 |
|
#else |
164 |
|
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
165 |
|
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
166 |
|
fwrite(&uiNewChunkSize, 4, 1, pFile->hFileWrite); |
167 |
|
} |
168 |
|
#endif // POSIX |
169 |
} |
} |
170 |
|
|
171 |
/** |
/** |
180 |
* Sets the position within the chunk body, thus within the data portion |
* Sets the position within the chunk body, thus within the data portion |
181 |
* of the chunk (in bytes). |
* of the chunk (in bytes). |
182 |
* |
* |
183 |
|
* <b>Caution:</b> the position will be reset to zero whenever |
184 |
|
* File::Save() was called. |
185 |
|
* |
186 |
* @param Where - position offset (in bytes) |
* @param Where - position offset (in bytes) |
187 |
* @param Whence - optional: defines to what <i>\a Where</i> relates to, |
* @param Whence - optional: defines to what <i>\a Where</i> relates to, |
188 |
* if omitted \a Where relates to beginning of the chunk |
* if omitted \a Where relates to beginning of the chunk |
197 |
ulPos += Where; |
ulPos += Where; |
198 |
break; |
break; |
199 |
case stream_end: |
case stream_end: |
200 |
ulPos = ChunkSize - 1 - Where; |
ulPos = CurrentChunkSize - 1 - Where; |
201 |
break; |
break; |
202 |
case stream_backward: |
case stream_backward: |
203 |
ulPos -= Where; |
ulPos -= Where; |
206 |
ulPos = Where; |
ulPos = Where; |
207 |
break; |
break; |
208 |
} |
} |
209 |
if (ulPos > ChunkSize) ulPos = ChunkSize; |
if (ulPos > CurrentChunkSize) ulPos = CurrentChunkSize; |
210 |
return ulPos; |
return ulPos; |
211 |
} |
} |
212 |
|
|
222 |
*/ |
*/ |
223 |
unsigned long Chunk::RemainingBytes() { |
unsigned long Chunk::RemainingBytes() { |
224 |
#if DEBUG |
#if DEBUG |
225 |
std::cout << "Chunk::Remainingbytes()=" << ChunkSize - ulPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
226 |
#endif // DEBUG |
#endif // DEBUG |
227 |
return ChunkSize - ulPos; |
return CurrentChunkSize - ulPos; |
228 |
} |
} |
229 |
|
|
230 |
/** |
/** |
243 |
std::cout << "Chunk::GetState()" << std::endl; |
std::cout << "Chunk::GetState()" << std::endl; |
244 |
#endif // DEBUG |
#endif // DEBUG |
245 |
#if POSIX |
#if POSIX |
246 |
if (hFile == 0) return stream_closed; |
if (pFile->hFileRead == 0) return stream_closed; |
247 |
|
#elif defined (WIN32) |
248 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) |
249 |
|
return stream_closed; |
250 |
#else |
#else |
251 |
if (hFile == NULL) return stream_closed; |
if (pFile->hFileRead == NULL) return stream_closed; |
252 |
#endif // POSIX |
#endif // POSIX |
253 |
if (ulPos < ChunkSize) return stream_ready; |
if (ulPos < CurrentChunkSize) return stream_ready; |
254 |
else return stream_end_reached; |
else return stream_end_reached; |
255 |
} |
} |
256 |
|
|
257 |
/** |
/** |
273 |
#if DEBUG |
#if DEBUG |
274 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
275 |
#endif // DEBUG |
#endif // DEBUG |
276 |
if (ulPos >= ChunkSize) return 0; |
if (ulPos >= CurrentChunkSize) return 0; |
277 |
if (ulPos + WordCount * WordSize >= ChunkSize) WordCount = (ChunkSize - ulPos) / WordSize; |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
278 |
#if POSIX |
#if POSIX |
279 |
if (lseek(hFile, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
280 |
unsigned long readWords = read(hFile, pData, WordCount * WordSize); |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
281 |
|
if (readWords < 1) return 0; |
282 |
|
readWords /= WordSize; |
283 |
|
#elif defined(WIN32) |
284 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
285 |
|
DWORD readWords; |
286 |
|
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); |
287 |
if (readWords < 1) return 0; |
if (readWords < 1) return 0; |
288 |
readWords /= WordSize; |
readWords /= WordSize; |
289 |
#else // standard C functions |
#else // standard C functions |
290 |
if (fseek(hFile, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
291 |
unsigned long readWords = fread(pData, WordSize, WordCount, hFile); |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
292 |
#endif // POSIX |
#endif // POSIX |
293 |
if (!bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
294 |
switch (WordSize) { |
switch (WordSize) { |
295 |
case 2: |
case 2: |
296 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
310 |
return readWords; |
return readWords; |
311 |
} |
} |
312 |
|
|
313 |
|
/** |
314 |
|
* Writes \a WordCount number of data words with given \a WordSize from |
315 |
|
* the buffer pointed by \a pData. Be sure to provide the correct |
316 |
|
* \a WordSize, as this will be important and taken into account for |
317 |
|
* eventual endian correction (swapping of bytes due to different |
318 |
|
* native byte order of a system). The position within the chunk will |
319 |
|
* automatically be incremented. |
320 |
|
* |
321 |
|
* @param pData source buffer (containing the data) |
322 |
|
* @param WordCount number of data words to write |
323 |
|
* @param WordSize size of each data word to write |
324 |
|
* @returns number of successfully written data words |
325 |
|
* @throws RIFF::Exception if write operation would exceed current |
326 |
|
* chunk size or any IO error occured |
327 |
|
* @see Resize() |
328 |
|
*/ |
329 |
|
unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) { |
330 |
|
if (pFile->Mode != stream_mode_read_write) |
331 |
|
throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first"); |
332 |
|
if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize > CurrentChunkSize) |
333 |
|
throw Exception("End of chunk reached while trying to write data"); |
334 |
|
if (!pFile->bEndianNative && WordSize != 1) { |
335 |
|
switch (WordSize) { |
336 |
|
case 2: |
337 |
|
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
338 |
|
swapBytes_16((uint16_t*) pData + iWord); |
339 |
|
break; |
340 |
|
case 4: |
341 |
|
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
342 |
|
swapBytes_32((uint32_t*) pData + iWord); |
343 |
|
break; |
344 |
|
default: |
345 |
|
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
346 |
|
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
347 |
|
break; |
348 |
|
} |
349 |
|
} |
350 |
|
#if POSIX |
351 |
|
if (lseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET) < 0) { |
352 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
353 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
354 |
|
} |
355 |
|
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
356 |
|
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
357 |
|
writtenWords /= WordSize; |
358 |
|
#elif defined(WIN32) |
359 |
|
if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
360 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
361 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
362 |
|
} |
363 |
|
DWORD writtenWords; |
364 |
|
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
365 |
|
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
366 |
|
writtenWords /= WordSize; |
367 |
|
#else // standard C functions |
368 |
|
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
369 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
370 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
371 |
|
} |
372 |
|
unsigned long writtenWords = fwrite(pData, WordSize, WordCount, pFile->hFileWrite); |
373 |
|
#endif // POSIX |
374 |
|
SetPos(writtenWords * WordSize, stream_curpos); |
375 |
|
return writtenWords; |
376 |
|
} |
377 |
|
|
378 |
/** 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. */ |
379 |
unsigned long Chunk::ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize) { |
unsigned long Chunk::ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize) { |
380 |
unsigned long readWords = Read(pData, WordCount, WordSize); |
unsigned long readWords = Read(pData, WordCount, WordSize); |
401 |
} |
} |
402 |
|
|
403 |
/** |
/** |
404 |
|
* Writes \a WordCount number of 8 Bit signed integer words from the |
405 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
406 |
|
* actual "physical" file. The position within the chunk will |
407 |
|
* automatically be incremented. Note: you cannot write beyond the |
408 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
409 |
|
* before. |
410 |
|
* |
411 |
|
* @param pData source buffer (containing the data) |
412 |
|
* @param WordCount number of 8 Bit signed integers to write |
413 |
|
* @returns number of written integers |
414 |
|
* @throws RIFF::Exception if an IO error occured |
415 |
|
* @see Resize() |
416 |
|
*/ |
417 |
|
unsigned long Chunk::WriteInt8(int8_t* pData, unsigned long WordCount) { |
418 |
|
return Write(pData, WordCount, 1); |
419 |
|
} |
420 |
|
|
421 |
|
/** |
422 |
* Reads \a WordCount number of 8 Bit unsigned integer words and copies |
* Reads \a WordCount number of 8 Bit unsigned integer words and copies |
423 |
* it into the buffer pointed by \a pData. The buffer has to be |
* it into the buffer pointed by \a pData. The buffer has to be |
424 |
* allocated. The position within the chunk will automatically be |
* allocated. The position within the chunk will automatically be |
438 |
} |
} |
439 |
|
|
440 |
/** |
/** |
441 |
|
* Writes \a WordCount number of 8 Bit unsigned integer words from the |
442 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
443 |
|
* actual "physical" file. The position within the chunk will |
444 |
|
* automatically be incremented. Note: you cannot write beyond the |
445 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
446 |
|
* before. |
447 |
|
* |
448 |
|
* @param pData source buffer (containing the data) |
449 |
|
* @param WordCount number of 8 Bit unsigned integers to write |
450 |
|
* @returns number of written integers |
451 |
|
* @throws RIFF::Exception if an IO error occured |
452 |
|
* @see Resize() |
453 |
|
*/ |
454 |
|
unsigned long Chunk::WriteUint8(uint8_t* pData, unsigned long WordCount) { |
455 |
|
return Write(pData, WordCount, 1); |
456 |
|
} |
457 |
|
|
458 |
|
/** |
459 |
* Reads \a WordCount number of 16 Bit signed integer words and copies |
* Reads \a WordCount number of 16 Bit signed integer words and copies |
460 |
* it into the buffer pointed by \a pData. The buffer has to be |
* it into the buffer pointed by \a pData. The buffer has to be |
461 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
475 |
} |
} |
476 |
|
|
477 |
/** |
/** |
478 |
|
* Writes \a WordCount number of 16 Bit signed integer words from the |
479 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
480 |
|
* actual "physical" file. The position within the chunk will |
481 |
|
* automatically be incremented. Note: you cannot write beyond the |
482 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
483 |
|
* before. |
484 |
|
* |
485 |
|
* @param pData source buffer (containing the data) |
486 |
|
* @param WordCount number of 16 Bit signed integers to write |
487 |
|
* @returns number of written integers |
488 |
|
* @throws RIFF::Exception if an IO error occured |
489 |
|
* @see Resize() |
490 |
|
*/ |
491 |
|
unsigned long Chunk::WriteInt16(int16_t* pData, unsigned long WordCount) { |
492 |
|
return Write(pData, WordCount, 2); |
493 |
|
} |
494 |
|
|
495 |
|
/** |
496 |
* Reads \a WordCount number of 16 Bit unsigned integer words and copies |
* Reads \a WordCount number of 16 Bit unsigned integer words and copies |
497 |
* it into the buffer pointed by \a pData. The buffer has to be |
* it into the buffer pointed by \a pData. The buffer has to be |
498 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
512 |
} |
} |
513 |
|
|
514 |
/** |
/** |
515 |
|
* Writes \a WordCount number of 16 Bit unsigned integer words from the |
516 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
517 |
|
* actual "physical" file. The position within the chunk will |
518 |
|
* automatically be incremented. Note: you cannot write beyond the |
519 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
520 |
|
* before. |
521 |
|
* |
522 |
|
* @param pData source buffer (containing the data) |
523 |
|
* @param WordCount number of 16 Bit unsigned integers to write |
524 |
|
* @returns number of written integers |
525 |
|
* @throws RIFF::Exception if an IO error occured |
526 |
|
* @see Resize() |
527 |
|
*/ |
528 |
|
unsigned long Chunk::WriteUint16(uint16_t* pData, unsigned long WordCount) { |
529 |
|
return Write(pData, WordCount, 2); |
530 |
|
} |
531 |
|
|
532 |
|
/** |
533 |
* Reads \a WordCount number of 32 Bit signed integer words and copies |
* Reads \a WordCount number of 32 Bit signed integer words and copies |
534 |
* it into the buffer pointed by \a pData. The buffer has to be |
* it into the buffer pointed by \a pData. The buffer has to be |
535 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
549 |
} |
} |
550 |
|
|
551 |
/** |
/** |
552 |
|
* Writes \a WordCount number of 32 Bit signed integer words from the |
553 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
554 |
|
* actual "physical" file. The position within the chunk will |
555 |
|
* automatically be incremented. Note: you cannot write beyond the |
556 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
557 |
|
* before. |
558 |
|
* |
559 |
|
* @param pData source buffer (containing the data) |
560 |
|
* @param WordCount number of 32 Bit signed integers to write |
561 |
|
* @returns number of written integers |
562 |
|
* @throws RIFF::Exception if an IO error occured |
563 |
|
* @see Resize() |
564 |
|
*/ |
565 |
|
unsigned long Chunk::WriteInt32(int32_t* pData, unsigned long WordCount) { |
566 |
|
return Write(pData, WordCount, 4); |
567 |
|
} |
568 |
|
|
569 |
|
/** |
570 |
* Reads \a WordCount number of 32 Bit unsigned integer words and copies |
* Reads \a WordCount number of 32 Bit unsigned integer words and copies |
571 |
* it into the buffer pointed by \a pData. The buffer has to be |
* it into the buffer pointed by \a pData. The buffer has to be |
572 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
586 |
} |
} |
587 |
|
|
588 |
/** |
/** |
589 |
|
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
590 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
591 |
|
* actual "physical" file. The position within the chunk will |
592 |
|
* automatically be incremented. Note: you cannot write beyond the |
593 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
594 |
|
* before. |
595 |
|
* |
596 |
|
* @param pData source buffer (containing the data) |
597 |
|
* @param WordCount number of 32 Bit unsigned integers to write |
598 |
|
* @returns number of written integers |
599 |
|
* @throws RIFF::Exception if an IO error occured |
600 |
|
* @see Resize() |
601 |
|
*/ |
602 |
|
unsigned long Chunk::WriteUint32(uint32_t* pData, unsigned long WordCount) { |
603 |
|
return Write(pData, WordCount, 4); |
604 |
|
} |
605 |
|
|
606 |
|
/** |
607 |
* Reads one 8 Bit signed integer word and increments the position within |
* Reads one 8 Bit signed integer word and increments the position within |
608 |
* the chunk. |
* the chunk. |
609 |
* |
* |
703 |
return word; |
return word; |
704 |
} |
} |
705 |
|
|
706 |
|
/** @brief Load chunk body into RAM. |
707 |
|
* |
708 |
|
* Loads the whole chunk body into memory. You can modify the data in |
709 |
|
* RAM and save the data by calling File::Save() afterwards. |
710 |
|
* |
711 |
|
* <b>Caution:</b> the buffer pointer will be invalidated once |
712 |
|
* File::Save() was called. You have to call LoadChunkData() again to |
713 |
|
* get a new, valid pointer whenever File::Save() was called. |
714 |
|
* |
715 |
|
* You can call LoadChunkData() again if you previously scheduled to |
716 |
|
* enlarge this chunk with a Resize() call. In that case the buffer will |
717 |
|
* be enlarged to the new, scheduled chunk size and you can already |
718 |
|
* place the new chunk data to the buffer and finally call File::Save() |
719 |
|
* to enlarge the chunk physically and write the new data in one rush. |
720 |
|
* This approach is definitely recommended if you have to enlarge and |
721 |
|
* write new data to a lot of chunks. |
722 |
|
* |
723 |
|
* @returns a pointer to the data in RAM on success, NULL otherwise |
724 |
|
* @throws Exception if data buffer could not be enlarged |
725 |
|
* @see ReleaseChunkData() |
726 |
|
*/ |
727 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
728 |
if (!pChunkData) { |
if (!pChunkData && pFile->Filename != "") { |
729 |
#if POSIX |
#if POSIX |
730 |
if (lseek(hFile, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
731 |
pChunkData = new uint8_t[GetSize()]; |
#elif defined(WIN32) |
732 |
if (!pChunkData) return NULL; |
if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL; |
|
unsigned long readWords = read(hFile, pChunkData, GetSize()); |
|
733 |
#else |
#else |
734 |
if (fseek(hFile, ulStartPos, SEEK_SET)) return NULL; |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
735 |
pChunkData = new uint8_t[GetSize()]; |
#endif // POSIX |
736 |
|
unsigned long ulBufferSize = (CurrentChunkSize > NewChunkSize) ? CurrentChunkSize : NewChunkSize; |
737 |
|
pChunkData = new uint8_t[ulBufferSize]; |
738 |
if (!pChunkData) return NULL; |
if (!pChunkData) return NULL; |
739 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), hFile); |
memset(pChunkData, 0, ulBufferSize); |
740 |
|
#if POSIX |
741 |
|
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
742 |
|
#elif defined(WIN32) |
743 |
|
DWORD readWords; |
744 |
|
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); |
745 |
|
#else |
746 |
|
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
747 |
#endif // POSIX |
#endif // POSIX |
748 |
if (readWords != GetSize()) { |
if (readWords != GetSize()) { |
749 |
delete[] pChunkData; |
delete[] pChunkData; |
750 |
return (pChunkData = NULL); |
return (pChunkData = NULL); |
751 |
} |
} |
752 |
|
ulChunkDataSize = ulBufferSize; |
753 |
|
} else if (NewChunkSize > ulChunkDataSize) { |
754 |
|
uint8_t* pNewBuffer = new uint8_t[NewChunkSize]; |
755 |
|
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(NewChunkSize) + " bytes"); |
756 |
|
memset(pNewBuffer, 0 , NewChunkSize); |
757 |
|
memcpy(pNewBuffer, pChunkData, ulChunkDataSize); |
758 |
|
delete[] pChunkData; |
759 |
|
pChunkData = pNewBuffer; |
760 |
|
ulChunkDataSize = NewChunkSize; |
761 |
} |
} |
762 |
return pChunkData; |
return pChunkData; |
763 |
} |
} |
764 |
|
|
765 |
|
/** @brief Free loaded chunk body from RAM. |
766 |
|
* |
767 |
|
* Frees loaded chunk body data from memory (RAM). You should call |
768 |
|
* File::Save() before calling this method if you modified the data to |
769 |
|
* make the changes persistent. |
770 |
|
*/ |
771 |
void Chunk::ReleaseChunkData() { |
void Chunk::ReleaseChunkData() { |
772 |
if (pChunkData) { |
if (pChunkData) { |
773 |
delete[] pChunkData; |
delete[] pChunkData; |
775 |
} |
} |
776 |
} |
} |
777 |
|
|
778 |
|
/** @brief Resize chunk. |
779 |
|
* |
780 |
|
* Resizes this chunk's body, that is the actual size of data possible |
781 |
|
* to be written to this chunk. This call will return immediately and |
782 |
|
* just schedule the resize operation. You should call File::Save() to |
783 |
|
* actually perform the resize operation(s) "physically" to the file. |
784 |
|
* As this can take a while on large files, it is recommended to call |
785 |
|
* Resize() first on all chunks which have to be resized and finally to |
786 |
|
* call File::Save() to perform all those resize operations in one rush. |
787 |
|
* |
788 |
|
* <b>Caution:</b> You cannot directly write to enlarged chunks before |
789 |
|
* calling File::Save() as this might exceed the current chunk's body |
790 |
|
* boundary! |
791 |
|
* |
792 |
|
* @param iNewSize - new chunk body size in bytes (must be greater than zero) |
793 |
|
* @throws RIFF::Exception if \a iNewSize is less than 1 |
794 |
|
* @see File::Save() |
795 |
|
*/ |
796 |
|
void Chunk::Resize(int iNewSize) { |
797 |
|
if (iNewSize <= 0) |
798 |
|
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this)); |
799 |
|
if (NewChunkSize == iNewSize) return; |
800 |
|
NewChunkSize = iNewSize; |
801 |
|
pFile->LogAsResized(this); |
802 |
|
} |
803 |
|
|
804 |
|
/** @brief Write chunk persistently e.g. to disk. |
805 |
|
* |
806 |
|
* Stores the chunk persistently to its actual "physical" file. |
807 |
|
* |
808 |
|
* @param ulWritePos - position within the "physical" file where this |
809 |
|
* chunk should be written to |
810 |
|
* @param ulCurrentDataOffset - offset of current (old) data within |
811 |
|
* the file |
812 |
|
* @returns new write position in the "physical" file, that is |
813 |
|
* \a ulWritePos incremented by this chunk's new size |
814 |
|
* (including its header size of course) |
815 |
|
*/ |
816 |
|
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
817 |
|
const unsigned long ulOriginalPos = ulWritePos; |
818 |
|
ulWritePos += CHUNK_HEADER_SIZE; |
819 |
|
|
820 |
|
if (pFile->Mode != stream_mode_read_write) |
821 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
822 |
|
|
823 |
|
// if the whole chunk body was loaded into RAM |
824 |
|
if (pChunkData) { |
825 |
|
// make sure chunk data buffer in RAM is at least as large as the new chunk size |
826 |
|
LoadChunkData(); |
827 |
|
// write chunk data from RAM persistently to the file |
828 |
|
#if POSIX |
829 |
|
lseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
830 |
|
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
831 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
832 |
|
} |
833 |
|
#elif defined(WIN32) |
834 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN); |
835 |
|
DWORD dwBytesWritten; |
836 |
|
WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL); |
837 |
|
if (dwBytesWritten != NewChunkSize) { |
838 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
839 |
|
} |
840 |
|
#else |
841 |
|
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
842 |
|
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
843 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
844 |
|
} |
845 |
|
#endif // POSIX |
846 |
|
} else { |
847 |
|
// move chunk data from the end of the file to the appropriate position |
848 |
|
int8_t* pCopyBuffer = new int8_t[4096]; |
849 |
|
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
850 |
|
#if defined(WIN32) |
851 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
852 |
|
#else |
853 |
|
int iBytesMoved = 1; |
854 |
|
#endif |
855 |
|
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
856 |
|
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
857 |
|
#if POSIX |
858 |
|
lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
859 |
|
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
860 |
|
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
861 |
|
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
862 |
|
#elif defined(WIN32) |
863 |
|
SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
864 |
|
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
865 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
866 |
|
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
867 |
|
#else |
868 |
|
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
869 |
|
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
870 |
|
fseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
871 |
|
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, pFile->hFileWrite); |
872 |
|
#endif |
873 |
|
} |
874 |
|
delete[] pCopyBuffer; |
875 |
|
if (iBytesMoved < 0) throw Exception("Writing Chunk data (from file) failed"); |
876 |
|
} |
877 |
|
|
878 |
|
// update this chunk's header |
879 |
|
CurrentChunkSize = NewChunkSize; |
880 |
|
WriteHeader(ulOriginalPos); |
881 |
|
|
882 |
|
// update chunk's position pointers |
883 |
|
ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE; |
884 |
|
ulPos = 0; |
885 |
|
|
886 |
|
// add pad byte if needed |
887 |
|
if ((ulStartPos + NewChunkSize) % 2 != 0) { |
888 |
|
const char cPadByte = 0; |
889 |
|
#if POSIX |
890 |
|
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
891 |
|
write(pFile->hFileWrite, &cPadByte, 1); |
892 |
|
#elif defined(WIN32) |
893 |
|
SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN); |
894 |
|
DWORD dwBytesWritten; |
895 |
|
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
896 |
|
#else |
897 |
|
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
898 |
|
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
899 |
|
#endif |
900 |
|
return ulStartPos + NewChunkSize + 1; |
901 |
|
} |
902 |
|
|
903 |
|
return ulStartPos + NewChunkSize; |
904 |
|
} |
905 |
|
|
906 |
|
void Chunk::__resetPos() { |
907 |
|
ulPos = 0; |
908 |
|
} |
909 |
|
|
910 |
|
|
911 |
|
|
912 |
// *************** List *************** |
// *************** List *************** |
913 |
// * |
// * |
914 |
|
|
915 |
List::List() : Chunk() { |
List::List(File* pFile) : Chunk(pFile) { |
916 |
#if DEBUG |
#if DEBUG |
917 |
std::cout << "List::List()" << std::endl; |
std::cout << "List::List(File* pFile)" << std::endl; |
918 |
#endif // DEBUG |
#endif // DEBUG |
919 |
pSubChunks = NULL; |
pSubChunks = NULL; |
920 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
921 |
} |
} |
922 |
|
|
923 |
#if POSIX |
List::List(File* pFile, unsigned long StartPos, List* Parent) |
924 |
List::List(int hFile, unsigned long StartPos, bool EndianNative, List* Parent) |
: Chunk(pFile, StartPos, Parent) { |
|
#else |
|
|
List::List(FILE* hFile, unsigned long StartPos, bool EndianNative, List* Parent) |
|
|
#endif // POSIX |
|
|
: Chunk(hFile, StartPos, EndianNative, Parent) { |
|
925 |
#if DEBUG |
#if DEBUG |
926 |
std::cout << "List::List(FILE*,ulong,bool,List*)" << std::endl; |
std::cout << "List::List(File*,ulong,bool,List*)" << std::endl; |
927 |
#endif // DEBUG |
#endif // DEBUG |
928 |
pSubChunks = NULL; |
pSubChunks = NULL; |
929 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
931 |
ulStartPos = StartPos + LIST_HEADER_SIZE; |
ulStartPos = StartPos + LIST_HEADER_SIZE; |
932 |
} |
} |
933 |
|
|
934 |
|
List::List(File* pFile, List* pParent, uint32_t uiListID) |
935 |
|
: Chunk(pFile, pParent, CHUNK_ID_LIST, 0) { |
936 |
|
pSubChunks = NULL; |
937 |
|
pSubChunksMap = NULL; |
938 |
|
ListType = uiListID; |
939 |
|
} |
940 |
|
|
941 |
List::~List() { |
List::~List() { |
942 |
#if DEBUG |
#if DEBUG |
943 |
std::cout << "List::~List()" << std::endl; |
std::cout << "List::~List()" << std::endl; |
1081 |
} |
} |
1082 |
|
|
1083 |
/** |
/** |
1084 |
* Returns number subchunks within the list. |
* Returns number of subchunks within the list. |
1085 |
*/ |
*/ |
1086 |
unsigned int List::CountSubChunks() { |
unsigned int List::CountSubChunks() { |
1087 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1132 |
return result; |
return result; |
1133 |
} |
} |
1134 |
|
|
1135 |
|
/** @brief Creates a new sub chunk. |
1136 |
|
* |
1137 |
|
* Creates and adds a new sub chunk to this list chunk. Note that the |
1138 |
|
* chunk's body size given by \a uiBodySize must be greater than zero. |
1139 |
|
* You have to call File::Save() to make this change persistent to the |
1140 |
|
* actual file and <b>before</b> performing any data write operations |
1141 |
|
* on the new chunk! |
1142 |
|
* |
1143 |
|
* @param uiChunkID - chunk ID of the new chunk |
1144 |
|
* @param uiBodySize - size of the new chunk's body, that is its actual |
1145 |
|
* data size (without header) |
1146 |
|
* @throws RIFF::Exception if \a uiBodySize equals zero |
1147 |
|
*/ |
1148 |
|
Chunk* List::AddSubChunk(uint32_t uiChunkID, uint uiBodySize) { |
1149 |
|
if (uiBodySize == 0) throw Exception("Chunk body size must be at least 1 byte"); |
1150 |
|
if (!pSubChunks) LoadSubChunks(); |
1151 |
|
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0); |
1152 |
|
pSubChunks->push_back(pNewChunk); |
1153 |
|
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1154 |
|
pNewChunk->Resize(uiBodySize); |
1155 |
|
return pNewChunk; |
1156 |
|
} |
1157 |
|
|
1158 |
|
/** @brief Moves a sub chunk. |
1159 |
|
* |
1160 |
|
* Moves a sub chunk from one position in a list to another |
1161 |
|
* position in the same list. The pSrc chunk is placed before the |
1162 |
|
* pDst chunk. |
1163 |
|
* |
1164 |
|
* @param pSrc - sub chunk to be moved |
1165 |
|
* @param pDst - the position to move to. pSrc will be placed |
1166 |
|
* before pDst. If pDst is 0, pSrc will be placed |
1167 |
|
* last in list. |
1168 |
|
*/ |
1169 |
|
void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) { |
1170 |
|
if (!pSubChunks) LoadSubChunks(); |
1171 |
|
pSubChunks->remove(pSrc); |
1172 |
|
ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst); |
1173 |
|
pSubChunks->insert(iter, pSrc); |
1174 |
|
} |
1175 |
|
|
1176 |
|
/** @brief Creates a new list sub chunk. |
1177 |
|
* |
1178 |
|
* Creates and adds a new list sub chunk to this list chunk. Note that |
1179 |
|
* you have to add sub chunks / sub list chunks to the new created chunk |
1180 |
|
* <b>before</b> trying to make this change persisten to the actual |
1181 |
|
* file with File::Save()! |
1182 |
|
* |
1183 |
|
* @param uiListType - list ID of the new list chunk |
1184 |
|
*/ |
1185 |
|
List* List::AddSubList(uint32_t uiListType) { |
1186 |
|
if (!pSubChunks) LoadSubChunks(); |
1187 |
|
List* pNewListChunk = new List(pFile, this, uiListType); |
1188 |
|
pSubChunks->push_back(pNewListChunk); |
1189 |
|
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1190 |
|
return pNewListChunk; |
1191 |
|
} |
1192 |
|
|
1193 |
|
/** @brief Removes a sub chunk. |
1194 |
|
* |
1195 |
|
* Removes the sub chunk given by \a pSubChunk from this list and frees |
1196 |
|
* it completely from RAM. The given chunk can either be a normal sub |
1197 |
|
* chunk or a list sub chunk. In case the given chunk is a list chunk, |
1198 |
|
* all its subchunks (if any) will be removed recursively as well. You |
1199 |
|
* should call File::Save() to make this change persistent at any time. |
1200 |
|
* |
1201 |
|
* @param pSubChunk - sub chunk or sub list chunk to be removed |
1202 |
|
*/ |
1203 |
|
void List::DeleteSubChunk(Chunk* pSubChunk) { |
1204 |
|
if (!pSubChunks) LoadSubChunks(); |
1205 |
|
pSubChunks->remove(pSubChunk); |
1206 |
|
if ((*pSubChunksMap)[pSubChunk->GetChunkID()] == pSubChunk) { |
1207 |
|
pSubChunksMap->erase(pSubChunk->GetChunkID()); |
1208 |
|
// try to find another chunk of the same chunk ID |
1209 |
|
ChunkList::iterator iter = pSubChunks->begin(); |
1210 |
|
ChunkList::iterator end = pSubChunks->end(); |
1211 |
|
for (; iter != end; ++iter) { |
1212 |
|
if ((*iter)->GetChunkID() == pSubChunk->GetChunkID()) { |
1213 |
|
(*pSubChunksMap)[pSubChunk->GetChunkID()] = *iter; |
1214 |
|
break; // we're done, stop search |
1215 |
|
} |
1216 |
|
} |
1217 |
|
} |
1218 |
|
delete pSubChunk; |
1219 |
|
} |
1220 |
|
|
1221 |
void List::ReadHeader(unsigned long fPos) { |
void List::ReadHeader(unsigned long fPos) { |
1222 |
#if DEBUG |
#if DEBUG |
1223 |
std::cout << "List::Readheader(ulong) "; |
std::cout << "List::Readheader(ulong) "; |
1224 |
#endif // DEBUG |
#endif // DEBUG |
1225 |
Chunk::ReadHeader(fPos); |
Chunk::ReadHeader(fPos); |
1226 |
ChunkSize -= 4; |
NewChunkSize = CurrentChunkSize -= 4; |
1227 |
#if POSIX |
#if POSIX |
1228 |
lseek(hFile, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1229 |
read(hFile, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1230 |
|
#elif defined(WIN32) |
1231 |
|
SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1232 |
|
DWORD dwBytesRead; |
1233 |
|
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
1234 |
#else |
#else |
1235 |
fseek(hFile, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1236 |
fread(&ListType, 4, 1, hFile); |
fread(&ListType, 4, 1, pFile->hFileRead); |
1237 |
#endif // POSIX |
#endif // POSIX |
1238 |
#if DEBUG |
#if DEBUG |
1239 |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
1240 |
#endif // DEBUG |
#endif // DEBUG |
1241 |
if (!bEndianNative) { |
if (!pFile->bEndianNative) { |
1242 |
//swapBytes_32(&ListType); |
//swapBytes_32(&ListType); |
1243 |
} |
} |
1244 |
} |
} |
1245 |
|
|
1246 |
|
void List::WriteHeader(unsigned long fPos) { |
1247 |
|
// the four list type bytes officially belong the chunk's body in the RIFF format |
1248 |
|
NewChunkSize += 4; |
1249 |
|
Chunk::WriteHeader(fPos); |
1250 |
|
NewChunkSize -= 4; // just revert the +4 incrementation |
1251 |
|
#if POSIX |
1252 |
|
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1253 |
|
write(pFile->hFileWrite, &ListType, 4); |
1254 |
|
#elif defined(WIN32) |
1255 |
|
SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1256 |
|
DWORD dwBytesWritten; |
1257 |
|
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
1258 |
|
#else |
1259 |
|
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1260 |
|
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
1261 |
|
#endif // POSIX |
1262 |
|
} |
1263 |
|
|
1264 |
void List::LoadSubChunks() { |
void List::LoadSubChunks() { |
1265 |
#if DEBUG |
#if DEBUG |
1266 |
std::cout << "List::LoadSubChunks()"; |
std::cout << "List::LoadSubChunks()"; |
1268 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1269 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1270 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1271 |
|
if (!pFile->hFileRead) return; |
1272 |
|
unsigned long uiOriginalPos = GetPos(); |
1273 |
|
SetPos(0); // jump to beginning of list chunk body |
1274 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
1275 |
Chunk* ck; |
Chunk* ck; |
1276 |
uint32_t ckid; |
uint32_t ckid; |
1279 |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
1280 |
#endif // DEBUG |
#endif // DEBUG |
1281 |
if (ckid == CHUNK_ID_LIST) { |
if (ckid == CHUNK_ID_LIST) { |
1282 |
ck = new RIFF::List(hFile, ulStartPos + ulPos - 4, bEndianNative, this); |
ck = new RIFF::List(pFile, ulStartPos + ulPos - 4, this); |
1283 |
SetPos(ck->GetSize() + LIST_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + LIST_HEADER_SIZE - 4, RIFF::stream_curpos); |
1284 |
} |
} |
1285 |
else { // simple chunk |
else { // simple chunk |
1286 |
ck = new RIFF::Chunk(hFile, ulStartPos + ulPos - 4, bEndianNative, this); |
ck = new RIFF::Chunk(pFile, ulStartPos + ulPos - 4, this); |
1287 |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE - 4, RIFF::stream_curpos); |
1288 |
} |
} |
1289 |
pSubChunks->push_back(ck); |
pSubChunks->push_back(ck); |
1290 |
(*pSubChunksMap)[ckid] = ck; |
(*pSubChunksMap)[ckid] = ck; |
1291 |
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 |
1292 |
} |
} |
1293 |
|
SetPos(uiOriginalPos); // restore position before this call |
1294 |
|
} |
1295 |
|
} |
1296 |
|
|
1297 |
|
void List::LoadSubChunksRecursively() { |
1298 |
|
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList()) |
1299 |
|
pList->LoadSubChunksRecursively(); |
1300 |
|
} |
1301 |
|
|
1302 |
|
/** @brief Write list chunk persistently e.g. to disk. |
1303 |
|
* |
1304 |
|
* Stores the list chunk persistently to its actual "physical" file. All |
1305 |
|
* subchunks (including sub list chunks) will be stored recursively as |
1306 |
|
* well. |
1307 |
|
* |
1308 |
|
* @param ulWritePos - position within the "physical" file where this |
1309 |
|
* list chunk should be written to |
1310 |
|
* @param ulCurrentDataOffset - offset of current (old) data within |
1311 |
|
* the file |
1312 |
|
* @returns new write position in the "physical" file, that is |
1313 |
|
* \a ulWritePos incremented by this list chunk's new size |
1314 |
|
* (including its header size of course) |
1315 |
|
*/ |
1316 |
|
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
1317 |
|
const unsigned long ulOriginalPos = ulWritePos; |
1318 |
|
ulWritePos += LIST_HEADER_SIZE; |
1319 |
|
|
1320 |
|
if (pFile->Mode != stream_mode_read_write) |
1321 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
1322 |
|
|
1323 |
|
// write all subchunks (including sub list chunks) recursively |
1324 |
|
if (pSubChunks) { |
1325 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
1326 |
|
ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset); |
1327 |
|
} |
1328 |
|
} |
1329 |
|
|
1330 |
|
// update this list chunk's header |
1331 |
|
CurrentChunkSize = NewChunkSize = ulWritePos - ulOriginalPos - LIST_HEADER_SIZE; |
1332 |
|
WriteHeader(ulOriginalPos); |
1333 |
|
|
1334 |
|
// offset of this list chunk in new written file may have changed |
1335 |
|
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
1336 |
|
|
1337 |
|
return ulWritePos; |
1338 |
|
} |
1339 |
|
|
1340 |
|
void List::__resetPos() { |
1341 |
|
Chunk::__resetPos(); |
1342 |
|
if (pSubChunks) { |
1343 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
1344 |
|
(*iter)->__resetPos(); |
1345 |
|
} |
1346 |
} |
} |
1347 |
} |
} |
1348 |
|
|
1358 |
// *************** File *************** |
// *************** File *************** |
1359 |
// * |
// * |
1360 |
|
|
1361 |
File::File(const String& path) : List() { |
/** @brief Create new RIFF file. |
1362 |
|
* |
1363 |
|
* Use this constructor if you want to create a new RIFF file completely |
1364 |
|
* "from scratch". Note: there must be no empty chunks or empty list |
1365 |
|
* chunks when trying to make the new RIFF file persistent with Save()! |
1366 |
|
* |
1367 |
|
* Note: by default, the RIFF file will be saved in native endian |
1368 |
|
* format; that is, as a RIFF file on little-endian machines and |
1369 |
|
* as a RIFX file on big-endian. To change this behaviour, call |
1370 |
|
* SetByteOrder() before calling Save(). |
1371 |
|
* |
1372 |
|
* @param FileType - four-byte identifier of the RIFF file type |
1373 |
|
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1374 |
|
*/ |
1375 |
|
File::File(uint32_t FileType) : List(this) { |
1376 |
|
#if defined(WIN32) |
1377 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1378 |
|
#else |
1379 |
|
hFileRead = hFileWrite = 0; |
1380 |
|
#endif |
1381 |
|
Mode = stream_mode_closed; |
1382 |
|
bEndianNative = true; |
1383 |
|
ulStartPos = RIFF_HEADER_SIZE; |
1384 |
|
ListType = FileType; |
1385 |
|
} |
1386 |
|
|
1387 |
|
/** @brief Load existing RIFF file. |
1388 |
|
* |
1389 |
|
* Loads an existing RIFF file with all its chunks. |
1390 |
|
* |
1391 |
|
* @param path - path and file name of the RIFF file to open |
1392 |
|
* @throws RIFF::Exception if error occured while trying to load the |
1393 |
|
* given RIFF file |
1394 |
|
*/ |
1395 |
|
File::File(const String& path) : List(this), Filename(path) { |
1396 |
#if DEBUG |
#if DEBUG |
1397 |
std::cout << "File::File("<<path<<")" << std::endl; |
std::cout << "File::File("<<path<<")" << std::endl; |
1398 |
#endif // DEBUG |
#endif // DEBUG |
1399 |
bEndianNative = true; |
bEndianNative = true; |
1400 |
#if POSIX |
#if POSIX |
1401 |
hFile = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1402 |
if (hFile <= 0) { |
if (hFileRead <= 0) { |
1403 |
hFile = 0; |
hFileRead = hFileWrite = 0; |
1404 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1405 |
|
} |
1406 |
|
#elif defined(WIN32) |
1407 |
|
hFileRead = hFileWrite = CreateFile( |
1408 |
|
path.c_str(), GENERIC_READ, |
1409 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1410 |
|
NULL, OPEN_EXISTING, |
1411 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1412 |
|
); |
1413 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1414 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1415 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1416 |
} |
} |
1417 |
#else |
#else |
1418 |
hFile = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1419 |
if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1420 |
#endif // POSIX |
#endif // POSIX |
1421 |
|
Mode = stream_mode_read; |
1422 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1423 |
ReadHeader(0); |
ReadHeader(0); |
1424 |
if (ChunkID != CHUNK_ID_RIFF) { |
if (ChunkID != CHUNK_ID_RIFF) { |
1426 |
} |
} |
1427 |
} |
} |
1428 |
|
|
1429 |
|
String File::GetFileName() { |
1430 |
|
return Filename; |
1431 |
|
} |
1432 |
|
|
1433 |
|
stream_mode_t File::GetMode() { |
1434 |
|
return Mode; |
1435 |
|
} |
1436 |
|
|
1437 |
|
/** @brief Change file access mode. |
1438 |
|
* |
1439 |
|
* Changes files access mode either to read-only mode or to read/write |
1440 |
|
* mode. |
1441 |
|
* |
1442 |
|
* @param NewMode - new file access mode |
1443 |
|
* @returns true if mode was changed, false if current mode already |
1444 |
|
* equals new mode |
1445 |
|
* @throws RIFF::Exception if new file access mode is unknown |
1446 |
|
*/ |
1447 |
|
bool File::SetMode(stream_mode_t NewMode) { |
1448 |
|
if (NewMode != Mode) { |
1449 |
|
switch (NewMode) { |
1450 |
|
case stream_mode_read: |
1451 |
|
#if POSIX |
1452 |
|
if (hFileRead) close(hFileRead); |
1453 |
|
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1454 |
|
if (hFileRead < 0) { |
1455 |
|
hFileRead = hFileWrite = 0; |
1456 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1457 |
|
} |
1458 |
|
#elif defined(WIN32) |
1459 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1460 |
|
hFileRead = hFileWrite = CreateFile( |
1461 |
|
Filename.c_str(), GENERIC_READ, |
1462 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1463 |
|
NULL, OPEN_EXISTING, |
1464 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1465 |
|
); |
1466 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1467 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1468 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1469 |
|
} |
1470 |
|
#else |
1471 |
|
if (hFileRead) fclose(hFileRead); |
1472 |
|
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1473 |
|
if (!hFileRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1474 |
|
#endif |
1475 |
|
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1476 |
|
break; |
1477 |
|
case stream_mode_read_write: |
1478 |
|
#if POSIX |
1479 |
|
if (hFileRead) close(hFileRead); |
1480 |
|
hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
1481 |
|
if (hFileRead < 0) { |
1482 |
|
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1483 |
|
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1484 |
|
} |
1485 |
|
#elif defined(WIN32) |
1486 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1487 |
|
hFileRead = hFileWrite = CreateFile( |
1488 |
|
Filename.c_str(), |
1489 |
|
GENERIC_READ | GENERIC_WRITE, |
1490 |
|
FILE_SHARE_READ, |
1491 |
|
NULL, OPEN_ALWAYS, |
1492 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1493 |
|
); |
1494 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1495 |
|
hFileRead = hFileWrite = CreateFile( |
1496 |
|
Filename.c_str(), GENERIC_READ, |
1497 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1498 |
|
NULL, OPEN_EXISTING, |
1499 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1500 |
|
); |
1501 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1502 |
|
} |
1503 |
|
#else |
1504 |
|
if (hFileRead) fclose(hFileRead); |
1505 |
|
hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b"); |
1506 |
|
if (!hFileRead) { |
1507 |
|
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1508 |
|
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1509 |
|
} |
1510 |
|
#endif |
1511 |
|
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1512 |
|
break; |
1513 |
|
case stream_mode_closed: |
1514 |
|
#if POSIX |
1515 |
|
if (hFileRead) close(hFileRead); |
1516 |
|
if (hFileWrite) close(hFileWrite); |
1517 |
|
#elif defined(WIN32) |
1518 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1519 |
|
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1520 |
|
#else |
1521 |
|
if (hFileRead) fclose(hFileRead); |
1522 |
|
if (hFileWrite) fclose(hFileWrite); |
1523 |
|
#endif |
1524 |
|
hFileRead = hFileWrite = 0; |
1525 |
|
break; |
1526 |
|
default: |
1527 |
|
throw Exception("Unknown file access mode"); |
1528 |
|
} |
1529 |
|
Mode = NewMode; |
1530 |
|
return true; |
1531 |
|
} |
1532 |
|
return false; |
1533 |
|
} |
1534 |
|
|
1535 |
|
/** @brief Set the byte order to be used when saving. |
1536 |
|
* |
1537 |
|
* Set the byte order to be used in the file. A value of |
1538 |
|
* endian_little will create a RIFF file, endian_big a RIFX file |
1539 |
|
* and endian_native will create a RIFF file on little-endian |
1540 |
|
* machines and RIFX on big-endian machines. |
1541 |
|
* |
1542 |
|
* @param Endian - endianess to use when file is saved. |
1543 |
|
*/ |
1544 |
|
void File::SetByteOrder(endian_t Endian) { |
1545 |
|
#if WORDS_BIGENDIAN |
1546 |
|
bEndianNative = Endian != endian_little; |
1547 |
|
#else |
1548 |
|
bEndianNative = Endian != endian_big; |
1549 |
|
#endif |
1550 |
|
} |
1551 |
|
|
1552 |
|
/** @brief Save changes to same file. |
1553 |
|
* |
1554 |
|
* Make all changes of all chunks persistent by writing them to the |
1555 |
|
* actual (same) file. The file might temporarily grow to a higher size |
1556 |
|
* than it will have at the end of the saving process, in case chunks |
1557 |
|
* were grown. |
1558 |
|
* |
1559 |
|
* @throws RIFF::Exception if there is an empty chunk or empty list |
1560 |
|
* chunk or any kind of IO error occured |
1561 |
|
*/ |
1562 |
|
void File::Save() { |
1563 |
|
// make sure the RIFF tree is built (from the original file) |
1564 |
|
LoadSubChunksRecursively(); |
1565 |
|
|
1566 |
|
// reopen file in write mode |
1567 |
|
SetMode(stream_mode_read_write); |
1568 |
|
|
1569 |
|
// to be able to save the whole file without loading everything into |
1570 |
|
// RAM and without having to store the data in a temporary file, we |
1571 |
|
// enlarge the file with the sum of all _positive_ chunk size |
1572 |
|
// changes, move current data towards the end of the file with the |
1573 |
|
// calculated sum and finally update / rewrite the file by copying |
1574 |
|
// the old data back to the right position at the beginning of the file |
1575 |
|
|
1576 |
|
// first we sum up all positive chunk size changes (and skip all negative ones) |
1577 |
|
unsigned long ulPositiveSizeDiff = 0; |
1578 |
|
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
1579 |
|
if ((*iter)->GetNewSize() == 0) { |
1580 |
|
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
1581 |
|
} |
1582 |
|
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
1583 |
|
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
1584 |
|
ulPositiveSizeDiff += ulDiff; |
1585 |
|
} |
1586 |
|
} |
1587 |
|
|
1588 |
|
unsigned long ulWorkingFileSize = GetFileSize(); |
1589 |
|
|
1590 |
|
// if there are positive size changes... |
1591 |
|
if (ulPositiveSizeDiff > 0) { |
1592 |
|
// ... we enlarge this file first ... |
1593 |
|
ulWorkingFileSize += ulPositiveSizeDiff; |
1594 |
|
ResizeFile(ulWorkingFileSize); |
1595 |
|
// ... and move current data by the same amount towards end of file. |
1596 |
|
int8_t* pCopyBuffer = new int8_t[4096]; |
1597 |
|
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
1598 |
|
#if defined(WIN32) |
1599 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1600 |
|
#else |
1601 |
|
int iBytesMoved = 1; |
1602 |
|
#endif |
1603 |
|
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
1604 |
|
const unsigned long ulToMove = ulFileSize - ulPos; |
1605 |
|
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
1606 |
|
#if POSIX |
1607 |
|
lseek(hFileRead, ulPos, SEEK_SET); |
1608 |
|
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1609 |
|
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1610 |
|
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1611 |
|
#elif defined(WIN32) |
1612 |
|
SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN); |
1613 |
|
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1614 |
|
SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN); |
1615 |
|
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1616 |
|
#else |
1617 |
|
fseek(hFileRead, ulPos, SEEK_SET); |
1618 |
|
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
1619 |
|
fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1620 |
|
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
1621 |
|
#endif |
1622 |
|
} |
1623 |
|
delete[] pCopyBuffer; |
1624 |
|
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
1625 |
|
} |
1626 |
|
|
1627 |
|
// rebuild / rewrite complete RIFF tree |
1628 |
|
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
1629 |
|
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1630 |
|
|
1631 |
|
// resize file to the final size |
1632 |
|
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1633 |
|
|
1634 |
|
// forget all resized chunks |
1635 |
|
ResizedChunks.clear(); |
1636 |
|
} |
1637 |
|
|
1638 |
|
/** @brief Save changes to another file. |
1639 |
|
* |
1640 |
|
* Make all changes of all chunks persistent by writing them to another |
1641 |
|
* file. <b>Caution:</b> this method is optimized for writing to |
1642 |
|
* <b>another</b> file, do not use it to save the changes to the same |
1643 |
|
* file! Use File::Save() in that case instead! Ignoring this might |
1644 |
|
* result in a corrupted file, especially in case chunks were resized! |
1645 |
|
* |
1646 |
|
* After calling this method, this File object will be associated with |
1647 |
|
* the new file (given by \a path) afterwards. |
1648 |
|
* |
1649 |
|
* @param path - path and file name where everything should be written to |
1650 |
|
*/ |
1651 |
|
void File::Save(const String& path) { |
1652 |
|
//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 |
1653 |
|
|
1654 |
|
// make sure the RIFF tree is built (from the original file) |
1655 |
|
LoadSubChunksRecursively(); |
1656 |
|
|
1657 |
|
if (Filename.length() > 0) SetMode(stream_mode_read); |
1658 |
|
// open the other (new) file for writing and truncate it to zero size |
1659 |
|
#if POSIX |
1660 |
|
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
1661 |
|
if (hFileWrite < 0) { |
1662 |
|
hFileWrite = hFileRead; |
1663 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1664 |
|
} |
1665 |
|
#elif defined(WIN32) |
1666 |
|
hFileWrite = CreateFile( |
1667 |
|
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1668 |
|
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL |
1669 |
|
); |
1670 |
|
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1671 |
|
hFileWrite = hFileRead; |
1672 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1673 |
|
} |
1674 |
|
#else |
1675 |
|
hFileWrite = fopen(path.c_str(), "w+b"); |
1676 |
|
if (!hFileWrite) { |
1677 |
|
hFileWrite = hFileRead; |
1678 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1679 |
|
} |
1680 |
|
#endif // POSIX |
1681 |
|
Mode = stream_mode_read_write; |
1682 |
|
|
1683 |
|
// write complete RIFF tree to the other (new) file |
1684 |
|
unsigned long ulTotalSize = WriteChunk(0, 0); |
1685 |
|
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1686 |
|
|
1687 |
|
// resize file to the final size (if the file was originally larger) |
1688 |
|
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1689 |
|
|
1690 |
|
// forget all resized chunks |
1691 |
|
ResizedChunks.clear(); |
1692 |
|
|
1693 |
|
if (Filename.length() > 0) { |
1694 |
|
#if POSIX |
1695 |
|
close(hFileWrite); |
1696 |
|
#elif defined(WIN32) |
1697 |
|
CloseHandle(hFileWrite); |
1698 |
|
#else |
1699 |
|
fclose(hFileWrite); |
1700 |
|
#endif |
1701 |
|
hFileWrite = hFileRead; |
1702 |
|
} |
1703 |
|
|
1704 |
|
// associate new file with this File object from now on |
1705 |
|
Filename = path; |
1706 |
|
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1707 |
|
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
1708 |
|
} |
1709 |
|
|
1710 |
|
void File::ResizeFile(unsigned long ulNewSize) { |
1711 |
|
#if POSIX |
1712 |
|
if (ftruncate(hFileWrite, ulNewSize) < 0) |
1713 |
|
throw Exception("Could not resize file \"" + Filename + "\""); |
1714 |
|
#elif defined(WIN32) |
1715 |
|
if ( |
1716 |
|
SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER || |
1717 |
|
!SetEndOfFile(hFileWrite) |
1718 |
|
) throw Exception("Could not resize file \"" + Filename + "\""); |
1719 |
|
#else |
1720 |
|
# error Sorry, this version of libgig only supports POSIX and Windows systems yet. |
1721 |
|
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
1722 |
|
#endif |
1723 |
|
} |
1724 |
|
|
1725 |
File::~File() { |
File::~File() { |
1726 |
#if DEBUG |
#if DEBUG |
1727 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
1728 |
#endif // DEBUG |
#endif // DEBUG |
1729 |
#if POSIX |
#if POSIX |
1730 |
if (hFile) close(hFile); |
if (hFileRead) close(hFileRead); |
1731 |
|
#elif defined(WIN32) |
1732 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1733 |
#else |
#else |
1734 |
if (hFile) fclose(hFile); |
if (hFileRead) fclose(hFileRead); |
1735 |
#endif // POSIX |
#endif // POSIX |
1736 |
} |
} |
1737 |
|
|
1738 |
|
void File::LogAsResized(Chunk* pResizedChunk) { |
1739 |
|
ResizedChunks.push_back(pResizedChunk); |
1740 |
|
} |
1741 |
|
|
1742 |
|
void File::UnlogResized(Chunk* pResizedChunk) { |
1743 |
|
ResizedChunks.remove(pResizedChunk); |
1744 |
|
} |
1745 |
|
|
1746 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |
1747 |
#if POSIX |
return __GetFileSize(hFileRead); |
1748 |
|
} |
1749 |
|
|
1750 |
|
#if POSIX |
1751 |
|
unsigned long File::__GetFileSize(int hFile) { |
1752 |
struct stat filestat; |
struct stat filestat; |
1753 |
fstat(hFile, &filestat); |
fstat(hFile, &filestat); |
1754 |
long size = filestat.st_size; |
long size = filestat.st_size; |
1755 |
#else // standard C functions |
return size; |
1756 |
|
} |
1757 |
|
#elif defined(WIN32) |
1758 |
|
unsigned long File::__GetFileSize(HANDLE hFile) { |
1759 |
|
DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/); |
1760 |
|
if (dwSize == INVALID_FILE_SIZE) |
1761 |
|
throw Exception("Windows FS error: could not determine file size"); |
1762 |
|
return dwSize; |
1763 |
|
} |
1764 |
|
#else // standard C functions |
1765 |
|
unsigned long File::__GetFileSize(FILE* hFile) { |
1766 |
long curpos = ftell(hFile); |
long curpos = ftell(hFile); |
1767 |
fseek(hFile, 0, SEEK_END); |
fseek(hFile, 0, SEEK_END); |
1768 |
long size = ftell(hFile); |
long size = ftell(hFile); |
1769 |
fseek(hFile, curpos, SEEK_SET); |
fseek(hFile, curpos, SEEK_SET); |
|
#endif // POSIX |
|
1770 |
return size; |
return size; |
1771 |
} |
} |
1772 |
|
#endif |
1773 |
|
|
1774 |
|
|
1775 |
// *************** Exception *************** |
// *************** Exception *************** |
1779 |
std::cout << "RIFF::Exception: " << Message << std::endl; |
std::cout << "RIFF::Exception: " << Message << std::endl; |
1780 |
} |
} |
1781 |
|
|
1782 |
|
|
1783 |
|
// *************** functions *************** |
1784 |
|
// * |
1785 |
|
|
1786 |
|
/** |
1787 |
|
* Returns the name of this C++ library. This is usually "libgig" of |
1788 |
|
* course. This call is equivalent to DLS::libraryName() and |
1789 |
|
* gig::libraryName(). |
1790 |
|
*/ |
1791 |
|
String libraryName() { |
1792 |
|
return PACKAGE; |
1793 |
|
} |
1794 |
|
|
1795 |
|
/** |
1796 |
|
* Returns version of this C++ library. This call is equivalent to |
1797 |
|
* DLS::libraryVersion() and gig::libraryVersion(). |
1798 |
|
*/ |
1799 |
|
String libraryVersion() { |
1800 |
|
return VERSION; |
1801 |
|
} |
1802 |
|
|
1803 |
} // namespace RIFF |
} // namespace RIFF |
|
#endif |
|