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 |
// *************** Chunk ************** |
// *************** Chunk ************** |
33 |
// * |
// * |
34 |
|
|
35 |
Chunk::Chunk() { |
Chunk::Chunk(File* pFile) { |
36 |
#if DEBUG |
#if DEBUG |
37 |
std::cout << "Chunk::Chunk()" << std::endl; |
std::cout << "Chunk::Chunk(File* pFile)" << std::endl; |
38 |
#endif // DEBUG |
#endif // DEBUG |
39 |
ulPos = 0; |
ulPos = 0; |
40 |
pParent = NULL; |
pParent = NULL; |
41 |
pChunkData = NULL; |
pChunkData = NULL; |
42 |
|
ulChunkDataSize = 0; |
43 |
|
ChunkID = CHUNK_ID_RIFF; |
44 |
|
this->pFile = pFile; |
45 |
} |
} |
46 |
|
|
47 |
#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 |
|
48 |
#if DEBUG |
#if DEBUG |
49 |
std::cout << "Chunk::Chunk(FILE,ulong,bool,List*),StartPos=" << StartPos << std::endl; |
std::cout << "Chunk::Chunk(File*,ulong,bool,List*),StartPos=" << StartPos << std::endl; |
50 |
#endif // DEBUG |
#endif // DEBUG |
51 |
Chunk::hFile = hFile; |
this->pFile = pFile; |
52 |
ulStartPos = StartPos + CHUNK_HEADER_SIZE; |
ulStartPos = StartPos + CHUNK_HEADER_SIZE; |
|
bEndianNative = EndianNative; |
|
53 |
pParent = Parent; |
pParent = Parent; |
54 |
ulPos = 0; |
ulPos = 0; |
55 |
pChunkData = NULL; |
pChunkData = NULL; |
56 |
|
ulChunkDataSize = 0; |
57 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
58 |
} |
} |
59 |
|
|
60 |
|
Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize) { |
61 |
|
this->pFile = pFile; |
62 |
|
ulStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk |
63 |
|
this->pParent = pParent; |
64 |
|
ulPos = 0; |
65 |
|
pChunkData = NULL; |
66 |
|
ulChunkDataSize = 0; |
67 |
|
ChunkID = uiChunkID; |
68 |
|
CurrentChunkSize = 0; |
69 |
|
NewChunkSize = uiBodySize; |
70 |
|
} |
71 |
|
|
72 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
73 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
74 |
} |
} |
78 |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
79 |
#endif // DEBUG |
#endif // DEBUG |
80 |
#if POSIX |
#if POSIX |
81 |
if (lseek(hFile, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
82 |
read(hFile, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
83 |
read(hFile, &ChunkSize, 4); |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
84 |
#else |
#else |
85 |
if (!fseek(hFile, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
86 |
fread(&ChunkID, 4, 1, hFile); |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
87 |
fread(&ChunkSize, 4, 1, hFile); |
fread(&CurrentChunkSize, 4, 1, pFile->hFileRead); |
88 |
#endif // POSIX |
#endif // POSIX |
89 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
90 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
92 |
} |
} |
93 |
#else // little endian |
#else // little endian |
94 |
if (ChunkID == CHUNK_ID_RIFX) { |
if (ChunkID == CHUNK_ID_RIFX) { |
95 |
bEndianNative = false; |
pFile->bEndianNative = false; |
96 |
ChunkID = CHUNK_ID_RIFF; |
ChunkID = CHUNK_ID_RIFF; |
97 |
} |
} |
98 |
#endif // WORDS_BIGENDIAN |
#endif // WORDS_BIGENDIAN |
99 |
if (!bEndianNative) { |
if (!pFile->bEndianNative) { |
100 |
//swapBytes_32(&ChunkID); |
//swapBytes_32(&ChunkID); |
101 |
swapBytes_32(&ChunkSize); |
swapBytes_32(&CurrentChunkSize); |
102 |
} |
} |
103 |
#if DEBUG |
#if DEBUG |
104 |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
105 |
std::cout << "ckSize=" << ChunkSize << " "; |
std::cout << "ckSize=" << ChunkSize << " "; |
106 |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
107 |
#endif // DEBUG |
#endif // DEBUG |
108 |
|
NewChunkSize = CurrentChunkSize; |
109 |
} |
} |
110 |
} |
} |
111 |
|
|
112 |
|
void Chunk::WriteHeader(unsigned long fPos) { |
113 |
|
uint32_t uiNewChunkID = ChunkID; |
114 |
|
if (ChunkID == CHUNK_ID_RIFF) { |
115 |
|
#if WORDS_BIGENDIAN |
116 |
|
if (pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX; |
117 |
|
#else // little endian |
118 |
|
if (!pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX; |
119 |
|
#endif // WORDS_BIGENDIAN |
120 |
|
} |
121 |
|
|
122 |
|
uint32_t uiNewChunkSize = NewChunkSize; |
123 |
|
if (!pFile->bEndianNative) { |
124 |
|
swapBytes_32(&uiNewChunkSize); |
125 |
|
} |
126 |
|
|
127 |
|
#if POSIX |
128 |
|
if (lseek(pFile->hFileWrite, fPos, SEEK_SET) != -1) { |
129 |
|
write(pFile->hFileWrite, &uiNewChunkID, 4); |
130 |
|
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
131 |
|
} |
132 |
|
#else |
133 |
|
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
134 |
|
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
135 |
|
fwrite(&uiNewChunkSize, 4, 1, pFile->hFileWrite); |
136 |
|
} |
137 |
|
#endif // POSIX |
138 |
|
} |
139 |
|
|
140 |
/** |
/** |
141 |
* Returns the String representation of the chunk's ID (e.g. "RIFF", |
* Returns the String representation of the chunk's ID (e.g. "RIFF", |
142 |
* "LIST"). |
* "LIST"). |
149 |
* Sets the position within the chunk body, thus within the data portion |
* Sets the position within the chunk body, thus within the data portion |
150 |
* of the chunk (in bytes). |
* of the chunk (in bytes). |
151 |
* |
* |
152 |
|
* <b>Caution:</b> the position will be reset to zero whenever |
153 |
|
* File::Save() was called. |
154 |
|
* |
155 |
* @param Where - position offset (in bytes) |
* @param Where - position offset (in bytes) |
156 |
* @param Whence - optional: defines to what <i>\a Where</i> relates to, |
* @param Whence - optional: defines to what <i>\a Where</i> relates to, |
157 |
* if omitted \a Where relates to beginning of the chunk |
* if omitted \a Where relates to beginning of the chunk |
166 |
ulPos += Where; |
ulPos += Where; |
167 |
break; |
break; |
168 |
case stream_end: |
case stream_end: |
169 |
ulPos = ChunkSize - 1 - Where; |
ulPos = CurrentChunkSize - 1 - Where; |
170 |
break; |
break; |
171 |
case stream_backward: |
case stream_backward: |
172 |
ulPos -= Where; |
ulPos -= Where; |
175 |
ulPos = Where; |
ulPos = Where; |
176 |
break; |
break; |
177 |
} |
} |
178 |
if (ulPos > ChunkSize) ulPos = ChunkSize; |
if (ulPos > CurrentChunkSize) ulPos = CurrentChunkSize; |
179 |
return ulPos; |
return ulPos; |
180 |
} |
} |
181 |
|
|
191 |
*/ |
*/ |
192 |
unsigned long Chunk::RemainingBytes() { |
unsigned long Chunk::RemainingBytes() { |
193 |
#if DEBUG |
#if DEBUG |
194 |
std::cout << "Chunk::Remainingbytes()=" << ChunkSize - ulPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
195 |
#endif // DEBUG |
#endif // DEBUG |
196 |
return ChunkSize - ulPos; |
return CurrentChunkSize - ulPos; |
197 |
} |
} |
198 |
|
|
199 |
/** |
/** |
212 |
std::cout << "Chunk::GetState()" << std::endl; |
std::cout << "Chunk::GetState()" << std::endl; |
213 |
#endif // DEBUG |
#endif // DEBUG |
214 |
#if POSIX |
#if POSIX |
215 |
if (hFile == 0) return stream_closed; |
if (pFile->hFileRead == 0) return stream_closed; |
216 |
#else |
#else |
217 |
if (hFile == NULL) return stream_closed; |
if (pFile->hFileRead == NULL) return stream_closed; |
218 |
#endif // POSIX |
#endif // POSIX |
219 |
if (ulPos < ChunkSize) return stream_ready; |
if (ulPos < CurrentChunkSize) return stream_ready; |
220 |
else return stream_end_reached; |
else return stream_end_reached; |
221 |
} |
} |
222 |
|
|
223 |
/** |
/** |
239 |
#if DEBUG |
#if DEBUG |
240 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
241 |
#endif // DEBUG |
#endif // DEBUG |
242 |
if (ulPos >= ChunkSize) return 0; |
if (ulPos >= CurrentChunkSize) return 0; |
243 |
if (ulPos + WordCount * WordSize >= ChunkSize) WordCount = (ChunkSize - ulPos) / WordSize; |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
244 |
#if POSIX |
#if POSIX |
245 |
if (lseek(hFile, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
246 |
unsigned long readWords = read(hFile, pData, WordCount * WordSize); |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
247 |
if (readWords < 1) return 0; |
if (readWords < 1) return 0; |
248 |
readWords /= WordSize; |
readWords /= WordSize; |
249 |
#else // standard C functions |
#else // standard C functions |
250 |
if (fseek(hFile, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
251 |
unsigned long readWords = fread(pData, WordSize, WordCount, hFile); |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
252 |
#endif // POSIX |
#endif // POSIX |
253 |
if (!bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
254 |
switch (WordSize) { |
switch (WordSize) { |
255 |
case 2: |
case 2: |
256 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
270 |
return readWords; |
return readWords; |
271 |
} |
} |
272 |
|
|
273 |
|
/** |
274 |
|
* Writes \a WordCount number of data words with given \a WordSize from |
275 |
|
* the buffer pointed by \a pData. Be sure to provide the correct |
276 |
|
* \a WordSize, as this will be important and taken into account for |
277 |
|
* eventual endian correction (swapping of bytes due to different |
278 |
|
* native byte order of a system). The position within the chunk will |
279 |
|
* automatically be incremented. |
280 |
|
* |
281 |
|
* @param pData source buffer (containing the data) |
282 |
|
* @param WordCount number of data words to write |
283 |
|
* @param WordSize size of each data word to write |
284 |
|
* @returns number of successfully written data words |
285 |
|
* @throws RIFF::Exception if write operation would exceed current |
286 |
|
* chunk size or any IO error occured |
287 |
|
* @see Resize() |
288 |
|
*/ |
289 |
|
unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) { |
290 |
|
if (pFile->Mode != stream_mode_read_write) |
291 |
|
throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first"); |
292 |
|
if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize > CurrentChunkSize) |
293 |
|
throw Exception("End of chunk reached while trying to write data"); |
294 |
|
if (!pFile->bEndianNative && WordSize != 1) { |
295 |
|
switch (WordSize) { |
296 |
|
case 2: |
297 |
|
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
298 |
|
swapBytes_16((uint16_t*) pData + iWord); |
299 |
|
break; |
300 |
|
case 4: |
301 |
|
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
302 |
|
swapBytes_32((uint32_t*) pData + iWord); |
303 |
|
break; |
304 |
|
default: |
305 |
|
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
306 |
|
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
307 |
|
break; |
308 |
|
} |
309 |
|
} |
310 |
|
#if POSIX |
311 |
|
if (lseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET) < 0) { |
312 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
313 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
314 |
|
} |
315 |
|
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
316 |
|
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
317 |
|
writtenWords /= WordSize; |
318 |
|
#else // standard C functions |
319 |
|
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
320 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
321 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
322 |
|
} |
323 |
|
unsigned long writtenWords = fwrite(pData, WordSize, WordCount, pFile->hFileWrite); |
324 |
|
#endif // POSIX |
325 |
|
SetPos(writtenWords * WordSize, stream_curpos); |
326 |
|
return writtenWords; |
327 |
|
} |
328 |
|
|
329 |
/** 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. */ |
330 |
unsigned long Chunk::ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize) { |
unsigned long Chunk::ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize) { |
331 |
unsigned long readWords = Read(pData, WordCount, WordSize); |
unsigned long readWords = Read(pData, WordCount, WordSize); |
352 |
} |
} |
353 |
|
|
354 |
/** |
/** |
355 |
|
* Writes \a WordCount number of 8 Bit signed integer words from the |
356 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
357 |
|
* actual "physical" file. The position within the chunk will |
358 |
|
* automatically be incremented. Note: you cannot write beyond the |
359 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
360 |
|
* before. |
361 |
|
* |
362 |
|
* @param pData source buffer (containing the data) |
363 |
|
* @param WordCount number of 8 Bit signed integers to write |
364 |
|
* @returns number of written integers |
365 |
|
* @throws RIFF::Exception if an IO error occured |
366 |
|
* @see Resize() |
367 |
|
*/ |
368 |
|
unsigned long Chunk::WriteInt8(int8_t* pData, unsigned long WordCount) { |
369 |
|
return Write(pData, WordCount, 1); |
370 |
|
} |
371 |
|
|
372 |
|
/** |
373 |
* Reads \a WordCount number of 8 Bit unsigned integer words and copies |
* Reads \a WordCount number of 8 Bit unsigned integer words and copies |
374 |
* 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 |
375 |
* allocated. The position within the chunk will automatically be |
* allocated. The position within the chunk will automatically be |
389 |
} |
} |
390 |
|
|
391 |
/** |
/** |
392 |
|
* Writes \a WordCount number of 8 Bit unsigned integer words from the |
393 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
394 |
|
* actual "physical" file. The position within the chunk will |
395 |
|
* automatically be incremented. Note: you cannot write beyond the |
396 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
397 |
|
* before. |
398 |
|
* |
399 |
|
* @param pData source buffer (containing the data) |
400 |
|
* @param WordCount number of 8 Bit unsigned integers to write |
401 |
|
* @returns number of written integers |
402 |
|
* @throws RIFF::Exception if an IO error occured |
403 |
|
* @see Resize() |
404 |
|
*/ |
405 |
|
unsigned long Chunk::WriteUint8(uint8_t* pData, unsigned long WordCount) { |
406 |
|
return Write(pData, WordCount, 1); |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
* Reads \a WordCount number of 16 Bit signed integer words and copies |
* Reads \a WordCount number of 16 Bit signed integer words and copies |
411 |
* 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 |
412 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
426 |
} |
} |
427 |
|
|
428 |
/** |
/** |
429 |
|
* Writes \a WordCount number of 16 Bit signed integer words from the |
430 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
431 |
|
* actual "physical" file. The position within the chunk will |
432 |
|
* automatically be incremented. Note: you cannot write beyond the |
433 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
434 |
|
* before. |
435 |
|
* |
436 |
|
* @param pData source buffer (containing the data) |
437 |
|
* @param WordCount number of 16 Bit signed integers to write |
438 |
|
* @returns number of written integers |
439 |
|
* @throws RIFF::Exception if an IO error occured |
440 |
|
* @see Resize() |
441 |
|
*/ |
442 |
|
unsigned long Chunk::WriteInt16(int16_t* pData, unsigned long WordCount) { |
443 |
|
return Write(pData, WordCount, 2); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
447 |
* Reads \a WordCount number of 16 Bit unsigned integer words and copies |
* Reads \a WordCount number of 16 Bit unsigned integer words and copies |
448 |
* 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 |
449 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
463 |
} |
} |
464 |
|
|
465 |
/** |
/** |
466 |
|
* Writes \a WordCount number of 16 Bit unsigned integer words from the |
467 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
468 |
|
* actual "physical" file. The position within the chunk will |
469 |
|
* automatically be incremented. Note: you cannot write beyond the |
470 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
471 |
|
* before. |
472 |
|
* |
473 |
|
* @param pData source buffer (containing the data) |
474 |
|
* @param WordCount number of 16 Bit unsigned integers to write |
475 |
|
* @returns number of written integers |
476 |
|
* @throws RIFF::Exception if an IO error occured |
477 |
|
* @see Resize() |
478 |
|
*/ |
479 |
|
unsigned long Chunk::WriteUint16(uint16_t* pData, unsigned long WordCount) { |
480 |
|
return Write(pData, WordCount, 2); |
481 |
|
} |
482 |
|
|
483 |
|
/** |
484 |
* Reads \a WordCount number of 32 Bit signed integer words and copies |
* Reads \a WordCount number of 32 Bit signed integer words and copies |
485 |
* 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 |
486 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
500 |
} |
} |
501 |
|
|
502 |
/** |
/** |
503 |
|
* Writes \a WordCount number of 32 Bit signed integer words from the |
504 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
505 |
|
* actual "physical" file. The position within the chunk will |
506 |
|
* automatically be incremented. Note: you cannot write beyond the |
507 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
508 |
|
* before. |
509 |
|
* |
510 |
|
* @param pData source buffer (containing the data) |
511 |
|
* @param WordCount number of 32 Bit signed integers to write |
512 |
|
* @returns number of written integers |
513 |
|
* @throws RIFF::Exception if an IO error occured |
514 |
|
* @see Resize() |
515 |
|
*/ |
516 |
|
unsigned long Chunk::WriteInt32(int32_t* pData, unsigned long WordCount) { |
517 |
|
return Write(pData, WordCount, 4); |
518 |
|
} |
519 |
|
|
520 |
|
/** |
521 |
* Reads \a WordCount number of 32 Bit unsigned integer words and copies |
* Reads \a WordCount number of 32 Bit unsigned integer words and copies |
522 |
* 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 |
523 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
537 |
} |
} |
538 |
|
|
539 |
/** |
/** |
540 |
|
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
541 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
542 |
|
* actual "physical" file. The position within the chunk will |
543 |
|
* automatically be incremented. Note: you cannot write beyond the |
544 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
545 |
|
* before. |
546 |
|
* |
547 |
|
* @param pData source buffer (containing the data) |
548 |
|
* @param WordCount number of 32 Bit unsigned integers to write |
549 |
|
* @returns number of written integers |
550 |
|
* @throws RIFF::Exception if an IO error occured |
551 |
|
* @see Resize() |
552 |
|
*/ |
553 |
|
unsigned long Chunk::WriteUint32(uint32_t* pData, unsigned long WordCount) { |
554 |
|
return Write(pData, WordCount, 4); |
555 |
|
} |
556 |
|
|
557 |
|
/** |
558 |
* Reads one 8 Bit signed integer word and increments the position within |
* Reads one 8 Bit signed integer word and increments the position within |
559 |
* the chunk. |
* the chunk. |
560 |
* |
* |
654 |
return word; |
return word; |
655 |
} |
} |
656 |
|
|
657 |
|
/** @brief Load chunk body into RAM. |
658 |
|
* |
659 |
|
* Loads the whole chunk body into memory. You can modify the data in |
660 |
|
* RAM and save the data by calling File::Save() afterwards. |
661 |
|
* |
662 |
|
* <b>Caution:</b> the buffer pointer will be invalidated once |
663 |
|
* File::Save() was called. You have to call LoadChunkData() again to |
664 |
|
* get a new, valid pointer whenever File::Save() was called. |
665 |
|
* |
666 |
|
* You can call LoadChunkData() again if you previously scheduled to |
667 |
|
* enlarge this chunk with a Resize() call. In that case the buffer will |
668 |
|
* be enlarged to the new, scheduled chunk size and you can already |
669 |
|
* place the new chunk data to the buffer and finally call File::Save() |
670 |
|
* to enlarge the chunk physically and write the new data in one rush. |
671 |
|
* This approach is definitely recommended if you have to enlarge and |
672 |
|
* write new data to a lot of chunks. |
673 |
|
* |
674 |
|
* @returns a pointer to the data in RAM on success, NULL otherwise |
675 |
|
* @throws Exception if data buffer could not be enlarged |
676 |
|
* @see ReleaseChunkData() |
677 |
|
*/ |
678 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
679 |
if (!pChunkData) { |
if (!pChunkData) { |
680 |
#if POSIX |
#if POSIX |
681 |
if (lseek(hFile, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
|
pChunkData = new uint8_t[GetSize()]; |
|
|
if (!pChunkData) return NULL; |
|
|
unsigned long readWords = read(hFile, pChunkData, GetSize()); |
|
682 |
#else |
#else |
683 |
if (fseek(hFile, ulStartPos, SEEK_SET)) return NULL; |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
684 |
pChunkData = new uint8_t[GetSize()]; |
#endif // POSIX |
685 |
|
unsigned long ulBufferSize = (CurrentChunkSize > NewChunkSize) ? CurrentChunkSize : NewChunkSize; |
686 |
|
pChunkData = new uint8_t[ulBufferSize]; |
687 |
if (!pChunkData) return NULL; |
if (!pChunkData) return NULL; |
688 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), hFile); |
memset(pChunkData, 0, ulBufferSize); |
689 |
|
#if POSIX |
690 |
|
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
691 |
|
#else |
692 |
|
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
693 |
#endif // POSIX |
#endif // POSIX |
694 |
if (readWords != GetSize()) { |
if (readWords != GetSize()) { |
695 |
delete[] pChunkData; |
delete[] pChunkData; |
696 |
return (pChunkData = NULL); |
return (pChunkData = NULL); |
697 |
} |
} |
698 |
|
ulChunkDataSize = ulBufferSize; |
699 |
|
} else if (NewChunkSize > ulChunkDataSize) { |
700 |
|
uint8_t* pNewBuffer = new uint8_t[NewChunkSize]; |
701 |
|
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(NewChunkSize) + " bytes"); |
702 |
|
memset(pNewBuffer, 0 , NewChunkSize); |
703 |
|
memcpy(pNewBuffer, pChunkData, ulChunkDataSize); |
704 |
|
delete[] pChunkData; |
705 |
|
pChunkData = pNewBuffer; |
706 |
|
ulChunkDataSize = NewChunkSize; |
707 |
} |
} |
708 |
return pChunkData; |
return pChunkData; |
709 |
} |
} |
710 |
|
|
711 |
|
/** @brief Free loaded chunk body from RAM. |
712 |
|
* |
713 |
|
* Frees loaded chunk body data from memory (RAM). You should call |
714 |
|
* File::Save() before calling this method if you modified the data to |
715 |
|
* make the changes persistent. |
716 |
|
*/ |
717 |
void Chunk::ReleaseChunkData() { |
void Chunk::ReleaseChunkData() { |
718 |
if (pChunkData) { |
if (pChunkData) { |
719 |
delete[] pChunkData; |
delete[] pChunkData; |
721 |
} |
} |
722 |
} |
} |
723 |
|
|
724 |
|
/** @brief Resize chunk. |
725 |
|
* |
726 |
|
* Resizes this chunk's body, that is the actual size of data possible |
727 |
|
* to be written to this chunk. This call will return immediately and |
728 |
|
* just schedule the resize operation. You should call File::Save() to |
729 |
|
* actually perform the resize operation(s) "physically" to the file. |
730 |
|
* As this can take a while on large files, it is recommended to call |
731 |
|
* Resize() first on all chunks which have to be resized and finally to |
732 |
|
* call File::Save() to perform all those resize operations in one rush. |
733 |
|
* |
734 |
|
* <b>Caution:</b> You cannot directly write to enlarged chunks before |
735 |
|
* calling File::Save() as this might exceed the current chunk's body |
736 |
|
* boundary! |
737 |
|
* |
738 |
|
* @param iNewSize - new chunk body size in bytes (must be greater than zero) |
739 |
|
* @throws RIFF::Exception if \a iNewSize is less than 1 |
740 |
|
* @see File::Save() |
741 |
|
*/ |
742 |
|
void Chunk::Resize(int iNewSize) { |
743 |
|
if (iNewSize <= 0) throw Exception("Chunk size must be at least one byte"); |
744 |
|
if (NewChunkSize == iNewSize) return; |
745 |
|
NewChunkSize = iNewSize; |
746 |
|
pFile->LogAsResized(this); |
747 |
|
} |
748 |
|
|
749 |
|
/** @brief Write chunk persistently e.g. to disk. |
750 |
|
* |
751 |
|
* Stores the chunk persistently to its actual "physical" file. |
752 |
|
* |
753 |
|
* @param ulWritePos - position within the "physical" file where this |
754 |
|
* chunk should be written to |
755 |
|
* @param ulCurrentDataOffset - offset of current (old) data within |
756 |
|
* the file |
757 |
|
* @returns new write position in the "physical" file, that is |
758 |
|
* \a ulWritePos incremented by this chunk's new size |
759 |
|
* (including its header size of course) |
760 |
|
*/ |
761 |
|
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
762 |
|
const unsigned long ulOriginalPos = ulWritePos; |
763 |
|
ulWritePos += CHUNK_HEADER_SIZE; |
764 |
|
|
765 |
|
if (pFile->Mode != stream_mode_read_write) |
766 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
767 |
|
|
768 |
|
// if the whole chunk body was loaded into RAM |
769 |
|
if (pChunkData) { |
770 |
|
// make sure chunk data buffer in RAM is at least as large as the new chunk size |
771 |
|
LoadChunkData(); |
772 |
|
// write chunk data from RAM persistently to the file |
773 |
|
#if POSIX |
774 |
|
lseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
775 |
|
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
776 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
777 |
|
} |
778 |
|
#else |
779 |
|
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
780 |
|
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
781 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
782 |
|
} |
783 |
|
#endif // POSIX |
784 |
|
} else { |
785 |
|
// move chunk data from the end of the file to the appropriate position |
786 |
|
int8_t* pCopyBuffer = new int8_t[4096]; |
787 |
|
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
788 |
|
int iBytesMoved = 1; |
789 |
|
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
790 |
|
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
791 |
|
#if POSIX |
792 |
|
lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
793 |
|
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
794 |
|
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
795 |
|
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
796 |
|
#else |
797 |
|
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
798 |
|
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
799 |
|
fseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
800 |
|
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, pFile->hFileWrite); |
801 |
|
#endif |
802 |
|
} |
803 |
|
delete[] pCopyBuffer; |
804 |
|
if (iBytesMoved < 0) throw Exception("Writing Chunk data (from file) failed"); |
805 |
|
} |
806 |
|
|
807 |
|
// update this chunk's header |
808 |
|
CurrentChunkSize = NewChunkSize; |
809 |
|
WriteHeader(ulOriginalPos); |
810 |
|
|
811 |
|
// update chunk's position pointers |
812 |
|
ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE; |
813 |
|
ulPos = 0; |
814 |
|
|
815 |
|
// add pad byte if needed |
816 |
|
if ((ulStartPos + NewChunkSize) % 2 != 0) { |
817 |
|
const char cPadByte = 0; |
818 |
|
#if POSIX |
819 |
|
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
820 |
|
write(pFile->hFileWrite, &cPadByte, 1); |
821 |
|
#else |
822 |
|
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
823 |
|
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
824 |
|
#endif |
825 |
|
return ulStartPos + NewChunkSize + 1; |
826 |
|
} |
827 |
|
|
828 |
|
return ulStartPos + NewChunkSize; |
829 |
|
} |
830 |
|
|
831 |
|
void Chunk::__resetPos() { |
832 |
|
ulPos = 0; |
833 |
|
} |
834 |
|
|
835 |
|
|
836 |
|
|
837 |
// *************** List *************** |
// *************** List *************** |
838 |
// * |
// * |
839 |
|
|
840 |
List::List() : Chunk() { |
List::List(File* pFile) : Chunk(pFile) { |
841 |
#if DEBUG |
#if DEBUG |
842 |
std::cout << "List::List()" << std::endl; |
std::cout << "List::List(File* pFile)" << std::endl; |
843 |
#endif // DEBUG |
#endif // DEBUG |
844 |
pSubChunks = NULL; |
pSubChunks = NULL; |
845 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
846 |
} |
} |
847 |
|
|
848 |
#if POSIX |
List::List(File* pFile, unsigned long StartPos, List* Parent) |
849 |
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) { |
|
850 |
#if DEBUG |
#if DEBUG |
851 |
std::cout << "List::List(FILE*,ulong,bool,List*)" << std::endl; |
std::cout << "List::List(File*,ulong,bool,List*)" << std::endl; |
852 |
#endif // DEBUG |
#endif // DEBUG |
853 |
pSubChunks = NULL; |
pSubChunks = NULL; |
854 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
856 |
ulStartPos = StartPos + LIST_HEADER_SIZE; |
ulStartPos = StartPos + LIST_HEADER_SIZE; |
857 |
} |
} |
858 |
|
|
859 |
|
List::List(File* pFile, List* pParent, uint32_t uiListID) |
860 |
|
: Chunk(pFile, pParent, CHUNK_ID_LIST, 0) { |
861 |
|
pSubChunks = NULL; |
862 |
|
pSubChunksMap = NULL; |
863 |
|
ListType = uiListID; |
864 |
|
} |
865 |
|
|
866 |
List::~List() { |
List::~List() { |
867 |
#if DEBUG |
#if DEBUG |
868 |
std::cout << "List::~List()" << std::endl; |
std::cout << "List::~List()" << std::endl; |
1006 |
} |
} |
1007 |
|
|
1008 |
/** |
/** |
1009 |
* Returns number subchunks within the list. |
* Returns number of subchunks within the list. |
1010 |
*/ |
*/ |
1011 |
unsigned int List::CountSubChunks() { |
unsigned int List::CountSubChunks() { |
1012 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1057 |
return result; |
return result; |
1058 |
} |
} |
1059 |
|
|
1060 |
|
/** @brief Creates a new sub chunk. |
1061 |
|
* |
1062 |
|
* Creates and adds a new sub chunk to this list chunk. Note that the |
1063 |
|
* chunk's body size given by \a uiBodySize must be greater than zero. |
1064 |
|
* You have to call File::Save() to make this change persistent to the |
1065 |
|
* actual file and <b>before</b> performing any data write operations |
1066 |
|
* on the new chunk! |
1067 |
|
* |
1068 |
|
* @param uiChunkID - chunk ID of the new chunk |
1069 |
|
* @param uiBodySize - size of the new chunk's body, that is its actual |
1070 |
|
* data size (without header) |
1071 |
|
* @throws RIFF::Exception if \a uiBodySize equals zero |
1072 |
|
*/ |
1073 |
|
Chunk* List::AddSubChunk(uint32_t uiChunkID, uint uiBodySize) { |
1074 |
|
if (uiBodySize == 0) throw Exception("Chunk body size must be at least 1 byte"); |
1075 |
|
if (!pSubChunks) LoadSubChunks(); |
1076 |
|
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0); |
1077 |
|
pSubChunks->push_back(pNewChunk); |
1078 |
|
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1079 |
|
pNewChunk->Resize(uiBodySize); |
1080 |
|
return pNewChunk; |
1081 |
|
} |
1082 |
|
|
1083 |
|
/** @brief Creates a new list sub chunk. |
1084 |
|
* |
1085 |
|
* Creates and adds a new list sub chunk to this list chunk. Note that |
1086 |
|
* you have to add sub chunks / sub list chunks to the new created chunk |
1087 |
|
* <b>before</b> trying to make this change persisten to the actual |
1088 |
|
* file with File::Save()! |
1089 |
|
* |
1090 |
|
* @param uiListType - list ID of the new list chunk |
1091 |
|
*/ |
1092 |
|
List* List::AddSubList(uint32_t uiListType) { |
1093 |
|
if (!pSubChunks) LoadSubChunks(); |
1094 |
|
List* pNewListChunk = new List(pFile, this, uiListType); |
1095 |
|
pSubChunks->push_back(pNewListChunk); |
1096 |
|
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1097 |
|
return pNewListChunk; |
1098 |
|
} |
1099 |
|
|
1100 |
|
/** @brief Removes a sub chunk. |
1101 |
|
* |
1102 |
|
* Removes the sub chunk given by \a pSubChunk from this list and frees |
1103 |
|
* it completely from RAM. The given chunk can either be a normal sub |
1104 |
|
* chunk or a list sub chunk. You should call File::Save() to make this |
1105 |
|
* change persistent at any time. |
1106 |
|
* |
1107 |
|
* @param pSubChunk - sub chunk or sub list chunk to be removed |
1108 |
|
*/ |
1109 |
|
void List::DeleteSubChunk(Chunk* pSubChunk) { |
1110 |
|
if (!pSubChunks) LoadSubChunks(); |
1111 |
|
pSubChunks->remove(pSubChunk); |
1112 |
|
if ((*pSubChunksMap)[pSubChunk->GetChunkID()] == pSubChunk) { |
1113 |
|
pSubChunksMap->erase(pSubChunk->GetChunkID()); |
1114 |
|
// try to find another chunk of the same chunk ID |
1115 |
|
ChunkList::iterator iter = pSubChunks->begin(); |
1116 |
|
ChunkList::iterator end = pSubChunks->end(); |
1117 |
|
for (; iter != end; ++iter) { |
1118 |
|
if ((*iter)->GetChunkID() == pSubChunk->GetChunkID()) { |
1119 |
|
(*pSubChunksMap)[pSubChunk->GetChunkID()] = *iter; |
1120 |
|
break; // we're done, stop search |
1121 |
|
} |
1122 |
|
} |
1123 |
|
} |
1124 |
|
delete pSubChunk; |
1125 |
|
} |
1126 |
|
|
1127 |
void List::ReadHeader(unsigned long fPos) { |
void List::ReadHeader(unsigned long fPos) { |
1128 |
#if DEBUG |
#if DEBUG |
1129 |
std::cout << "List::Readheader(ulong) "; |
std::cout << "List::Readheader(ulong) "; |
1130 |
#endif // DEBUG |
#endif // DEBUG |
1131 |
Chunk::ReadHeader(fPos); |
Chunk::ReadHeader(fPos); |
1132 |
ChunkSize -= 4; |
NewChunkSize = CurrentChunkSize -= 4; |
1133 |
#if POSIX |
#if POSIX |
1134 |
lseek(hFile, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1135 |
read(hFile, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1136 |
#else |
#else |
1137 |
fseek(hFile, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1138 |
fread(&ListType, 4, 1, hFile); |
fread(&ListType, 4, 1, pFile->hFileRead); |
1139 |
#endif // POSIX |
#endif // POSIX |
1140 |
#if DEBUG |
#if DEBUG |
1141 |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
1142 |
#endif // DEBUG |
#endif // DEBUG |
1143 |
if (!bEndianNative) { |
if (!pFile->bEndianNative) { |
1144 |
//swapBytes_32(&ListType); |
//swapBytes_32(&ListType); |
1145 |
} |
} |
1146 |
} |
} |
1147 |
|
|
1148 |
|
void List::WriteHeader(unsigned long fPos) { |
1149 |
|
// the four list type bytes officially belong the chunk's body in the RIFF format |
1150 |
|
NewChunkSize += 4; |
1151 |
|
Chunk::WriteHeader(fPos); |
1152 |
|
NewChunkSize -= 4; // just revert the +4 incrementation |
1153 |
|
#if POSIX |
1154 |
|
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1155 |
|
write(pFile->hFileWrite, &ListType, 4); |
1156 |
|
#else |
1157 |
|
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1158 |
|
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
1159 |
|
#endif // POSIX |
1160 |
|
} |
1161 |
|
|
1162 |
void List::LoadSubChunks() { |
void List::LoadSubChunks() { |
1163 |
#if DEBUG |
#if DEBUG |
1164 |
std::cout << "List::LoadSubChunks()"; |
std::cout << "List::LoadSubChunks()"; |
1166 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1167 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1168 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1169 |
|
if (!pFile->hFileRead) return; |
1170 |
|
unsigned long uiOriginalPos = GetPos(); |
1171 |
|
SetPos(0); // jump to beginning of list chunk body |
1172 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
1173 |
Chunk* ck; |
Chunk* ck; |
1174 |
uint32_t ckid; |
uint32_t ckid; |
1177 |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
1178 |
#endif // DEBUG |
#endif // DEBUG |
1179 |
if (ckid == CHUNK_ID_LIST) { |
if (ckid == CHUNK_ID_LIST) { |
1180 |
ck = new RIFF::List(hFile, ulStartPos + ulPos - 4, bEndianNative, this); |
ck = new RIFF::List(pFile, ulStartPos + ulPos - 4, this); |
1181 |
SetPos(ck->GetSize() + LIST_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + LIST_HEADER_SIZE - 4, RIFF::stream_curpos); |
1182 |
} |
} |
1183 |
else { // simple chunk |
else { // simple chunk |
1184 |
ck = new RIFF::Chunk(hFile, ulStartPos + ulPos - 4, bEndianNative, this); |
ck = new RIFF::Chunk(pFile, ulStartPos + ulPos - 4, this); |
1185 |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE - 4, RIFF::stream_curpos); |
1186 |
} |
} |
1187 |
pSubChunks->push_back(ck); |
pSubChunks->push_back(ck); |
1188 |
(*pSubChunksMap)[ckid] = ck; |
(*pSubChunksMap)[ckid] = ck; |
1189 |
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 |
1190 |
} |
} |
1191 |
|
SetPos(uiOriginalPos); // restore position before this call |
1192 |
|
} |
1193 |
|
} |
1194 |
|
|
1195 |
|
/** @brief Write list chunk persistently e.g. to disk. |
1196 |
|
* |
1197 |
|
* Stores the list chunk persistently to its actual "physical" file. All |
1198 |
|
* subchunks (including sub list chunks) will be stored recursively as |
1199 |
|
* well. |
1200 |
|
* |
1201 |
|
* @param ulWritePos - position within the "physical" file where this |
1202 |
|
* list chunk should be written to |
1203 |
|
* @param ulCurrentDataOffset - offset of current (old) data within |
1204 |
|
* the file |
1205 |
|
* @returns new write position in the "physical" file, that is |
1206 |
|
* \a ulWritePos incremented by this list chunk's new size |
1207 |
|
* (including its header size of course) |
1208 |
|
*/ |
1209 |
|
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
1210 |
|
const unsigned long ulOriginalPos = ulWritePos; |
1211 |
|
ulWritePos += LIST_HEADER_SIZE; |
1212 |
|
|
1213 |
|
if (pFile->Mode != stream_mode_read_write) |
1214 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
1215 |
|
|
1216 |
|
// write all subchunks (including sub list chunks) recursively |
1217 |
|
if (pSubChunks) { |
1218 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
1219 |
|
ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset); |
1220 |
|
} |
1221 |
|
} |
1222 |
|
|
1223 |
|
// update this list chunk's header |
1224 |
|
CurrentChunkSize = NewChunkSize = ulWritePos - ulOriginalPos - LIST_HEADER_SIZE; |
1225 |
|
WriteHeader(ulOriginalPos); |
1226 |
|
|
1227 |
|
// offset of this list chunk in new written file may have changed |
1228 |
|
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
1229 |
|
|
1230 |
|
return ulWritePos; |
1231 |
|
} |
1232 |
|
|
1233 |
|
void List::__resetPos() { |
1234 |
|
Chunk::__resetPos(); |
1235 |
|
if (pSubChunks) { |
1236 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
1237 |
|
(*iter)->__resetPos(); |
1238 |
|
} |
1239 |
} |
} |
1240 |
} |
} |
1241 |
|
|
1251 |
// *************** File *************** |
// *************** File *************** |
1252 |
// * |
// * |
1253 |
|
|
1254 |
File::File(const String& path) : List() { |
/** @brief Create new RIFF file. |
1255 |
|
* |
1256 |
|
* Use this constructor if you want to create a new RIFF file completely |
1257 |
|
* "from scratch". Note: there must be no empty chunks or empty list |
1258 |
|
* chunks when trying to make the new RIFF file persistent with Save()! |
1259 |
|
* |
1260 |
|
* @param FileType - four-byte identifier of the RIFF file type |
1261 |
|
* @see AddSubChunk(), AddSubList() |
1262 |
|
*/ |
1263 |
|
File::File(uint32_t FileType) : List(this) { |
1264 |
|
hFileRead = hFileWrite = 0; |
1265 |
|
Mode = stream_mode_closed; |
1266 |
|
bEndianNative = true; |
1267 |
|
ulStartPos = RIFF_HEADER_SIZE; |
1268 |
|
ListType = FileType; |
1269 |
|
} |
1270 |
|
|
1271 |
|
/** @brief Load existing RIFF file. |
1272 |
|
* |
1273 |
|
* Loads an existing RIFF file with all its chunks. |
1274 |
|
* |
1275 |
|
* @param path - path and file name of the RIFF file to open |
1276 |
|
* @throws RIFF::Exception if error occured while trying to load the |
1277 |
|
* given RIFF file |
1278 |
|
*/ |
1279 |
|
File::File(const String& path) : List(this), Filename(path) { |
1280 |
#if DEBUG |
#if DEBUG |
1281 |
std::cout << "File::File("<<path<<")" << std::endl; |
std::cout << "File::File("<<path<<")" << std::endl; |
1282 |
#endif // DEBUG |
#endif // DEBUG |
1283 |
bEndianNative = true; |
bEndianNative = true; |
1284 |
#if POSIX |
#if POSIX |
1285 |
hFile = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1286 |
if (hFile <= 0) { |
if (hFileRead <= 0) { |
1287 |
hFile = 0; |
hFileRead = hFileWrite = 0; |
1288 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1289 |
} |
} |
1290 |
#else |
#else |
1291 |
hFile = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1292 |
if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); |
1293 |
#endif // POSIX |
#endif // POSIX |
1294 |
|
Mode = stream_mode_read; |
1295 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1296 |
ReadHeader(0); |
ReadHeader(0); |
1297 |
if (ChunkID != CHUNK_ID_RIFF) { |
if (ChunkID != CHUNK_ID_RIFF) { |
1299 |
} |
} |
1300 |
} |
} |
1301 |
|
|
1302 |
|
String File::GetFileName() { |
1303 |
|
return Filename; |
1304 |
|
} |
1305 |
|
|
1306 |
|
stream_mode_t File::GetMode() { |
1307 |
|
return Mode; |
1308 |
|
} |
1309 |
|
|
1310 |
|
/** @brief Change file access mode. |
1311 |
|
* |
1312 |
|
* Changes files access mode either to read-only mode or to read/write |
1313 |
|
* mode. |
1314 |
|
* |
1315 |
|
* @param NewMode - new file access mode |
1316 |
|
* @returns true if mode was changed, false if current mode already |
1317 |
|
* equals new mode |
1318 |
|
* @throws RIFF::Exception if new file access mode is unknown |
1319 |
|
*/ |
1320 |
|
bool File::SetMode(stream_mode_t NewMode) { |
1321 |
|
if (NewMode != Mode) { |
1322 |
|
switch (NewMode) { |
1323 |
|
case stream_mode_read: |
1324 |
|
#if POSIX |
1325 |
|
if (hFileRead) close(hFileRead); |
1326 |
|
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1327 |
|
if (hFileRead < 0) { |
1328 |
|
hFileRead = hFileWrite = 0; |
1329 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1330 |
|
} |
1331 |
|
#else |
1332 |
|
if (hFileRead) fclose(hFileRead); |
1333 |
|
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1334 |
|
if (!hFileRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1335 |
|
#endif |
1336 |
|
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1337 |
|
break; |
1338 |
|
case stream_mode_read_write: |
1339 |
|
#if POSIX |
1340 |
|
if (hFileRead) close(hFileRead); |
1341 |
|
hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
1342 |
|
if (hFileRead < 0) { |
1343 |
|
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1344 |
|
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1345 |
|
} |
1346 |
|
#else |
1347 |
|
if (hFileRead) fclose(hFileRead); |
1348 |
|
hFileRead = hFileWrite = fopen(path.c_str(), "r+b"); |
1349 |
|
if (!hFileRead) { |
1350 |
|
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1351 |
|
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1352 |
|
} |
1353 |
|
#endif |
1354 |
|
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1355 |
|
break; |
1356 |
|
case stream_mode_closed: |
1357 |
|
#if POSIX |
1358 |
|
if (hFileRead) close(hFileRead); |
1359 |
|
if (hFileWrite) close(hFileWrite); |
1360 |
|
#else |
1361 |
|
if (hFileRead) fclose(hFileRead); |
1362 |
|
if (hFileWrite) fclose(hFileWrite); |
1363 |
|
#endif |
1364 |
|
hFileRead = hFileWrite = 0; |
1365 |
|
break; |
1366 |
|
default: |
1367 |
|
throw Exception("Unknown file access mode"); |
1368 |
|
} |
1369 |
|
Mode = NewMode; |
1370 |
|
return true; |
1371 |
|
} |
1372 |
|
return false; |
1373 |
|
} |
1374 |
|
|
1375 |
|
/** @brief Save changes to same file. |
1376 |
|
* |
1377 |
|
* Make all changes of all chunks persistent by writing them to the |
1378 |
|
* actual (same) file. The file might temporarily grow to a higher size |
1379 |
|
* than it will have at the end of the saving process, in case chunks |
1380 |
|
* were grown. |
1381 |
|
* |
1382 |
|
* @throws RIFF::Exception if there is an empty chunk or empty list |
1383 |
|
* chunk or any kind of IO error occured |
1384 |
|
*/ |
1385 |
|
void File::Save() { |
1386 |
|
// reopen file in write mode |
1387 |
|
SetMode(stream_mode_read_write); |
1388 |
|
|
1389 |
|
// to be able to save the whole file without loading everything into |
1390 |
|
// RAM and without having to store the data in a temporary file, we |
1391 |
|
// enlarge the file with the sum of all _positive_ chunk size |
1392 |
|
// changes, move current data towards the end of the file with the |
1393 |
|
// calculated sum and finally update / rewrite the file by copying |
1394 |
|
// the old data back to the right position at the beginning of the file |
1395 |
|
|
1396 |
|
// first we sum up all positive chunk size changes (and skip all negative ones) |
1397 |
|
unsigned long ulPositiveSizeDiff = 0; |
1398 |
|
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
1399 |
|
if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)"); |
1400 |
|
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
1401 |
|
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
1402 |
|
ulPositiveSizeDiff += ulDiff; |
1403 |
|
} |
1404 |
|
} |
1405 |
|
|
1406 |
|
unsigned long ulWorkingFileSize = GetFileSize(); |
1407 |
|
|
1408 |
|
// if there are positive size changes... |
1409 |
|
if (ulPositiveSizeDiff > 0) { |
1410 |
|
// ... we enlarge this file first ... |
1411 |
|
ulWorkingFileSize += ulPositiveSizeDiff; |
1412 |
|
ResizeFile(ulWorkingFileSize); |
1413 |
|
// ... and move current data by the same amount towards end of file. |
1414 |
|
int8_t* pCopyBuffer = new int8_t[4096]; |
1415 |
|
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
1416 |
|
int iBytesMoved = 1; |
1417 |
|
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
1418 |
|
const unsigned long ulToMove = ulFileSize - ulPos; |
1419 |
|
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
1420 |
|
#if POSIX |
1421 |
|
lseek(hFileRead, ulPos, SEEK_SET); |
1422 |
|
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1423 |
|
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1424 |
|
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1425 |
|
#else |
1426 |
|
fseek(hFileRead, ulPos, SEEK_SET); |
1427 |
|
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
1428 |
|
fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1429 |
|
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
1430 |
|
#endif |
1431 |
|
} |
1432 |
|
delete[] pCopyBuffer; |
1433 |
|
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
1434 |
|
} |
1435 |
|
|
1436 |
|
// rebuild / rewrite complete RIFF tree |
1437 |
|
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
1438 |
|
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1439 |
|
|
1440 |
|
// resize file to the final size |
1441 |
|
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1442 |
|
|
1443 |
|
// forget all resized chunks |
1444 |
|
ResizedChunks.clear(); |
1445 |
|
} |
1446 |
|
|
1447 |
|
/** @brief Save changes to another file. |
1448 |
|
* |
1449 |
|
* Make all changes of all chunks persistent by writing them to another |
1450 |
|
* file. <b>Caution:</b> this method is optimized for writing to |
1451 |
|
* <b>another</b> file, do not use it to save the changes to the same |
1452 |
|
* file! Use File::Save() in that case instead! Ignoring this might |
1453 |
|
* result in a corrupted file, especially in case chunks were resized! |
1454 |
|
* |
1455 |
|
* After calling this method, this File object will be associated with |
1456 |
|
* the new file (given by \a path) afterwards. |
1457 |
|
* |
1458 |
|
* @param path - path and file name where everything should be written to |
1459 |
|
*/ |
1460 |
|
void File::Save(const String& path) { |
1461 |
|
//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 |
1462 |
|
|
1463 |
|
if (Filename.length() > 0) SetMode(stream_mode_read); |
1464 |
|
// open the other (new) file for writing and truncate it to zero size |
1465 |
|
#if POSIX |
1466 |
|
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
1467 |
|
if (hFileWrite < 0) { |
1468 |
|
hFileWrite = hFileRead; |
1469 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1470 |
|
} |
1471 |
|
#else |
1472 |
|
hFileWrite = fopen(path.c_str(), "w+b"); |
1473 |
|
if (!hFileWrite) { |
1474 |
|
hFileWrite = hFileRead; |
1475 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1476 |
|
} |
1477 |
|
#endif // POSIX |
1478 |
|
Mode = stream_mode_read_write; |
1479 |
|
|
1480 |
|
// write complete RIFF tree to the other (new) file |
1481 |
|
unsigned long ulTotalSize = WriteChunk(0, 0); |
1482 |
|
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1483 |
|
|
1484 |
|
// resize file to the final size (if the file was originally larger) |
1485 |
|
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1486 |
|
|
1487 |
|
// forget all resized chunks |
1488 |
|
ResizedChunks.clear(); |
1489 |
|
|
1490 |
|
if (Filename.length() > 0) { |
1491 |
|
#if POSIX |
1492 |
|
close(hFileWrite); |
1493 |
|
#else |
1494 |
|
fclose(hFileWrite); |
1495 |
|
#endif |
1496 |
|
hFileWrite = hFileRead; |
1497 |
|
} |
1498 |
|
|
1499 |
|
// associate new file with this File object from now on |
1500 |
|
Filename = path; |
1501 |
|
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1502 |
|
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
1503 |
|
} |
1504 |
|
|
1505 |
|
void File::ResizeFile(unsigned long ulNewSize) { |
1506 |
|
#if POSIX |
1507 |
|
if (ftruncate(hFileWrite, ulNewSize) < 0) |
1508 |
|
throw Exception("Could not resize file \"" + Filename + "\""); |
1509 |
|
#else |
1510 |
|
# error Sorry, this version of libgig only supports POSIX systems yet. |
1511 |
|
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
1512 |
|
#endif |
1513 |
|
} |
1514 |
|
|
1515 |
File::~File() { |
File::~File() { |
1516 |
#if DEBUG |
#if DEBUG |
1517 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
1518 |
#endif // DEBUG |
#endif // DEBUG |
1519 |
#if POSIX |
#if POSIX |
1520 |
if (hFile) close(hFile); |
if (hFileRead) close(hFileRead); |
1521 |
#else |
#else |
1522 |
if (hFile) fclose(hFile); |
if (hFileRead) fclose(hFileRead); |
1523 |
#endif // POSIX |
#endif // POSIX |
1524 |
} |
} |
1525 |
|
|
1526 |
|
void File::LogAsResized(Chunk* pResizedChunk) { |
1527 |
|
ResizedChunks.push_back(pResizedChunk); |
1528 |
|
} |
1529 |
|
|
1530 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |
1531 |
#if POSIX |
return __GetFileSize(hFileRead); |
1532 |
|
} |
1533 |
|
|
1534 |
|
#if POSIX |
1535 |
|
unsigned long File::__GetFileSize(int hFile) { |
1536 |
struct stat filestat; |
struct stat filestat; |
1537 |
fstat(hFile, &filestat); |
fstat(hFile, &filestat); |
1538 |
long size = filestat.st_size; |
long size = filestat.st_size; |
1539 |
#else // standard C functions |
return size; |
1540 |
|
} |
1541 |
|
#else // standard C functions |
1542 |
|
unsigned long File::__GetFileSize(FILE* hFile) { |
1543 |
long curpos = ftell(hFile); |
long curpos = ftell(hFile); |
1544 |
fseek(hFile, 0, SEEK_END); |
fseek(hFile, 0, SEEK_END); |
1545 |
long size = ftell(hFile); |
long size = ftell(hFile); |
1546 |
fseek(hFile, curpos, SEEK_SET); |
fseek(hFile, curpos, SEEK_SET); |
|
#endif // POSIX |
|
1547 |
return size; |
return size; |
1548 |
} |
} |
1549 |
|
#endif |
1550 |
|
|
1551 |
|
|
1552 |
// *************** Exception *************** |
// *************** Exception *************** |
1556 |
std::cout << "RIFF::Exception: " << Message << std::endl; |
std::cout << "RIFF::Exception: " << Message << std::endl; |
1557 |
} |
} |
1558 |
|
|
1559 |
|
|
1560 |
|
// *************** functions *************** |
1561 |
|
// * |
1562 |
|
|
1563 |
|
/** |
1564 |
|
* Returns the name of this C++ library. This is usually "libgig" of |
1565 |
|
* course. This call is equivalent to DLS::libraryName() and |
1566 |
|
* gig::libraryName(). |
1567 |
|
*/ |
1568 |
|
String libraryName() { |
1569 |
|
return PACKAGE; |
1570 |
|
} |
1571 |
|
|
1572 |
|
/** |
1573 |
|
* Returns version of this C++ library. This call is equivalent to |
1574 |
|
* DLS::libraryVersion() and gig::libraryVersion(). |
1575 |
|
*/ |
1576 |
|
String libraryVersion() { |
1577 |
|
return VERSION; |
1578 |
|
} |
1579 |
|
|
1580 |
} // namespace RIFF |
} // namespace RIFF |
|
#endif |
|