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 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 |
// *************** 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 (CurrentChunkSize != NewChunkSize) pFile->UnlogResized(this); |
74 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
75 |
} |
} |
76 |
|
|
79 |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
80 |
#endif // DEBUG |
#endif // DEBUG |
81 |
#if POSIX |
#if POSIX |
82 |
if (lseek(hFile, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
83 |
read(hFile, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
84 |
read(hFile, &ChunkSize, 4); |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
85 |
|
#elif defined(WIN32) |
86 |
|
if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
87 |
|
DWORD dwBytesRead; |
88 |
|
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
89 |
|
ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL); |
90 |
#else |
#else |
91 |
if (!fseek(hFile, fPos, SEEK_SET)) { |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
92 |
fread(&ChunkID, 4, 1, hFile); |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
93 |
fread(&ChunkSize, 4, 1, hFile); |
fread(&CurrentChunkSize, 4, 1, pFile->hFileRead); |
94 |
#endif // POSIX |
#endif // POSIX |
95 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
96 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
97 |
bEndianNative = false; |
pFile->bEndianNative = false; |
98 |
} |
} |
99 |
#else // little endian |
#else // little endian |
100 |
if (ChunkID == CHUNK_ID_RIFX) { |
if (ChunkID == CHUNK_ID_RIFX) { |
101 |
bEndianNative = false; |
pFile->bEndianNative = false; |
102 |
ChunkID = CHUNK_ID_RIFF; |
ChunkID = CHUNK_ID_RIFF; |
103 |
} |
} |
104 |
#endif // WORDS_BIGENDIAN |
#endif // WORDS_BIGENDIAN |
105 |
if (!bEndianNative) { |
if (!pFile->bEndianNative) { |
106 |
//swapBytes_32(&ChunkID); |
//swapBytes_32(&ChunkID); |
107 |
swapBytes_32(&ChunkSize); |
swapBytes_32(&CurrentChunkSize); |
108 |
} |
} |
109 |
#if DEBUG |
#if DEBUG |
110 |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
111 |
std::cout << "ckSize=" << ChunkSize << " "; |
std::cout << "ckSize=" << ChunkSize << " "; |
112 |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
113 |
#endif // DEBUG |
#endif // DEBUG |
114 |
|
NewChunkSize = CurrentChunkSize; |
115 |
} |
} |
116 |
} |
} |
117 |
|
|
118 |
|
void Chunk::WriteHeader(unsigned long fPos) { |
119 |
|
uint32_t uiNewChunkID = ChunkID; |
120 |
|
if (ChunkID == CHUNK_ID_RIFF) { |
121 |
|
#if WORDS_BIGENDIAN |
122 |
|
if (pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX; |
123 |
|
#else // little endian |
124 |
|
if (!pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX; |
125 |
|
#endif // WORDS_BIGENDIAN |
126 |
|
} |
127 |
|
|
128 |
|
uint32_t uiNewChunkSize = NewChunkSize; |
129 |
|
if (!pFile->bEndianNative) { |
130 |
|
swapBytes_32(&uiNewChunkSize); |
131 |
|
} |
132 |
|
|
133 |
|
#if POSIX |
134 |
|
if (lseek(pFile->hFileWrite, fPos, SEEK_SET) != -1) { |
135 |
|
write(pFile->hFileWrite, &uiNewChunkID, 4); |
136 |
|
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
137 |
|
} |
138 |
|
#elif defined(WIN32) |
139 |
|
if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
140 |
|
DWORD dwBytesWritten; |
141 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
142 |
|
WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL); |
143 |
|
} |
144 |
|
#else |
145 |
|
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
146 |
|
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
147 |
|
fwrite(&uiNewChunkSize, 4, 1, pFile->hFileWrite); |
148 |
|
} |
149 |
|
#endif // POSIX |
150 |
|
} |
151 |
|
|
152 |
/** |
/** |
153 |
* Returns the String representation of the chunk's ID (e.g. "RIFF", |
* Returns the String representation of the chunk's ID (e.g. "RIFF", |
154 |
* "LIST"). |
* "LIST"). |
161 |
* Sets the position within the chunk body, thus within the data portion |
* Sets the position within the chunk body, thus within the data portion |
162 |
* of the chunk (in bytes). |
* of the chunk (in bytes). |
163 |
* |
* |
164 |
|
* <b>Caution:</b> the position will be reset to zero whenever |
165 |
|
* File::Save() was called. |
166 |
|
* |
167 |
* @param Where - position offset (in bytes) |
* @param Where - position offset (in bytes) |
168 |
* @param Whence - optional: defines to what <i>\a Where</i> relates to, |
* @param Whence - optional: defines to what <i>\a Where</i> relates to, |
169 |
* if omitted \a Where relates to beginning of the chunk |
* if omitted \a Where relates to beginning of the chunk |
178 |
ulPos += Where; |
ulPos += Where; |
179 |
break; |
break; |
180 |
case stream_end: |
case stream_end: |
181 |
ulPos = ChunkSize - 1 - Where; |
ulPos = CurrentChunkSize - 1 - Where; |
182 |
break; |
break; |
183 |
case stream_backward: |
case stream_backward: |
184 |
ulPos -= Where; |
ulPos -= Where; |
187 |
ulPos = Where; |
ulPos = Where; |
188 |
break; |
break; |
189 |
} |
} |
190 |
if (ulPos > ChunkSize) ulPos = ChunkSize; |
if (ulPos > CurrentChunkSize) ulPos = CurrentChunkSize; |
191 |
return ulPos; |
return ulPos; |
192 |
} |
} |
193 |
|
|
203 |
*/ |
*/ |
204 |
unsigned long Chunk::RemainingBytes() { |
unsigned long Chunk::RemainingBytes() { |
205 |
#if DEBUG |
#if DEBUG |
206 |
std::cout << "Chunk::Remainingbytes()=" << ChunkSize - ulPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
207 |
#endif // DEBUG |
#endif // DEBUG |
208 |
return ChunkSize - ulPos; |
return CurrentChunkSize - ulPos; |
209 |
} |
} |
210 |
|
|
211 |
/** |
/** |
224 |
std::cout << "Chunk::GetState()" << std::endl; |
std::cout << "Chunk::GetState()" << std::endl; |
225 |
#endif // DEBUG |
#endif // DEBUG |
226 |
#if POSIX |
#if POSIX |
227 |
if (hFile == 0) return stream_closed; |
if (pFile->hFileRead == 0) return stream_closed; |
228 |
|
#elif defined (WIN32) |
229 |
|
if (pFile->hFileRead == INVALID_HANDLE_VALUE) |
230 |
|
return stream_closed; |
231 |
#else |
#else |
232 |
if (hFile == NULL) return stream_closed; |
if (pFile->hFileRead == NULL) return stream_closed; |
233 |
#endif // POSIX |
#endif // POSIX |
234 |
if (ulPos < ChunkSize) return stream_ready; |
if (ulPos < CurrentChunkSize) return stream_ready; |
235 |
else return stream_end_reached; |
else return stream_end_reached; |
236 |
} |
} |
237 |
|
|
238 |
/** |
/** |
254 |
#if DEBUG |
#if DEBUG |
255 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
256 |
#endif // DEBUG |
#endif // DEBUG |
257 |
if (ulPos >= ChunkSize) return 0; |
if (ulPos >= CurrentChunkSize) return 0; |
258 |
if (ulPos + WordCount * WordSize >= ChunkSize) WordCount = (ChunkSize - ulPos) / WordSize; |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
259 |
#if POSIX |
#if POSIX |
260 |
if (lseek(hFile, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
261 |
unsigned long readWords = read(hFile, pData, WordCount * WordSize); |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
262 |
|
if (readWords < 1) return 0; |
263 |
|
readWords /= WordSize; |
264 |
|
#elif defined(WIN32) |
265 |
|
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
266 |
|
DWORD readWords; |
267 |
|
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); |
268 |
if (readWords < 1) return 0; |
if (readWords < 1) return 0; |
269 |
readWords /= WordSize; |
readWords /= WordSize; |
270 |
#else // standard C functions |
#else // standard C functions |
271 |
if (fseek(hFile, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
272 |
unsigned long readWords = fread(pData, WordSize, WordCount, hFile); |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
273 |
#endif // POSIX |
#endif // POSIX |
274 |
if (!bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
275 |
switch (WordSize) { |
switch (WordSize) { |
276 |
case 2: |
case 2: |
277 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
291 |
return readWords; |
return readWords; |
292 |
} |
} |
293 |
|
|
294 |
|
/** |
295 |
|
* Writes \a WordCount number of data words with given \a WordSize from |
296 |
|
* the buffer pointed by \a pData. Be sure to provide the correct |
297 |
|
* \a WordSize, as this will be important and taken into account for |
298 |
|
* eventual endian correction (swapping of bytes due to different |
299 |
|
* native byte order of a system). The position within the chunk will |
300 |
|
* automatically be incremented. |
301 |
|
* |
302 |
|
* @param pData source buffer (containing the data) |
303 |
|
* @param WordCount number of data words to write |
304 |
|
* @param WordSize size of each data word to write |
305 |
|
* @returns number of successfully written data words |
306 |
|
* @throws RIFF::Exception if write operation would exceed current |
307 |
|
* chunk size or any IO error occured |
308 |
|
* @see Resize() |
309 |
|
*/ |
310 |
|
unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) { |
311 |
|
if (pFile->Mode != stream_mode_read_write) |
312 |
|
throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first"); |
313 |
|
if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize > CurrentChunkSize) |
314 |
|
throw Exception("End of chunk reached while trying to write data"); |
315 |
|
if (!pFile->bEndianNative && WordSize != 1) { |
316 |
|
switch (WordSize) { |
317 |
|
case 2: |
318 |
|
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
319 |
|
swapBytes_16((uint16_t*) pData + iWord); |
320 |
|
break; |
321 |
|
case 4: |
322 |
|
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
323 |
|
swapBytes_32((uint32_t*) pData + iWord); |
324 |
|
break; |
325 |
|
default: |
326 |
|
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
327 |
|
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
328 |
|
break; |
329 |
|
} |
330 |
|
} |
331 |
|
#if POSIX |
332 |
|
if (lseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET) < 0) { |
333 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
334 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
335 |
|
} |
336 |
|
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
337 |
|
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
338 |
|
writtenWords /= WordSize; |
339 |
|
#elif defined(WIN32) |
340 |
|
if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
341 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
342 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
343 |
|
} |
344 |
|
DWORD writtenWords; |
345 |
|
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
346 |
|
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
347 |
|
writtenWords /= WordSize; |
348 |
|
#else // standard C functions |
349 |
|
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
350 |
|
throw Exception("Could not seek to position " + ToString(ulPos) + |
351 |
|
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
352 |
|
} |
353 |
|
unsigned long writtenWords = fwrite(pData, WordSize, WordCount, pFile->hFileWrite); |
354 |
|
#endif // POSIX |
355 |
|
SetPos(writtenWords * WordSize, stream_curpos); |
356 |
|
return writtenWords; |
357 |
|
} |
358 |
|
|
359 |
/** 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. */ |
360 |
unsigned long Chunk::ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize) { |
unsigned long Chunk::ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize) { |
361 |
unsigned long readWords = Read(pData, WordCount, WordSize); |
unsigned long readWords = Read(pData, WordCount, WordSize); |
382 |
} |
} |
383 |
|
|
384 |
/** |
/** |
385 |
|
* Writes \a WordCount number of 8 Bit signed integer words from the |
386 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
387 |
|
* actual "physical" file. The position within the chunk will |
388 |
|
* automatically be incremented. Note: you cannot write beyond the |
389 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
390 |
|
* before. |
391 |
|
* |
392 |
|
* @param pData source buffer (containing the data) |
393 |
|
* @param WordCount number of 8 Bit signed integers to write |
394 |
|
* @returns number of written integers |
395 |
|
* @throws RIFF::Exception if an IO error occured |
396 |
|
* @see Resize() |
397 |
|
*/ |
398 |
|
unsigned long Chunk::WriteInt8(int8_t* pData, unsigned long WordCount) { |
399 |
|
return Write(pData, WordCount, 1); |
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
* Reads \a WordCount number of 8 Bit unsigned integer words and copies |
* Reads \a WordCount number of 8 Bit unsigned integer words and copies |
404 |
* 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 |
405 |
* allocated. The position within the chunk will automatically be |
* allocated. The position within the chunk will automatically be |
419 |
} |
} |
420 |
|
|
421 |
/** |
/** |
422 |
|
* Writes \a WordCount number of 8 Bit unsigned integer words from the |
423 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
424 |
|
* actual "physical" file. The position within the chunk will |
425 |
|
* automatically be incremented. Note: you cannot write beyond the |
426 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
427 |
|
* before. |
428 |
|
* |
429 |
|
* @param pData source buffer (containing the data) |
430 |
|
* @param WordCount number of 8 Bit unsigned integers to write |
431 |
|
* @returns number of written integers |
432 |
|
* @throws RIFF::Exception if an IO error occured |
433 |
|
* @see Resize() |
434 |
|
*/ |
435 |
|
unsigned long Chunk::WriteUint8(uint8_t* pData, unsigned long WordCount) { |
436 |
|
return Write(pData, WordCount, 1); |
437 |
|
} |
438 |
|
|
439 |
|
/** |
440 |
* Reads \a WordCount number of 16 Bit signed integer words and copies |
* Reads \a WordCount number of 16 Bit signed integer words and copies |
441 |
* 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 |
442 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
456 |
} |
} |
457 |
|
|
458 |
/** |
/** |
459 |
|
* Writes \a WordCount number of 16 Bit signed integer words from the |
460 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
461 |
|
* actual "physical" file. The position within the chunk will |
462 |
|
* automatically be incremented. Note: you cannot write beyond the |
463 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
464 |
|
* before. |
465 |
|
* |
466 |
|
* @param pData source buffer (containing the data) |
467 |
|
* @param WordCount number of 16 Bit signed integers to write |
468 |
|
* @returns number of written integers |
469 |
|
* @throws RIFF::Exception if an IO error occured |
470 |
|
* @see Resize() |
471 |
|
*/ |
472 |
|
unsigned long Chunk::WriteInt16(int16_t* pData, unsigned long WordCount) { |
473 |
|
return Write(pData, WordCount, 2); |
474 |
|
} |
475 |
|
|
476 |
|
/** |
477 |
* Reads \a WordCount number of 16 Bit unsigned integer words and copies |
* Reads \a WordCount number of 16 Bit unsigned integer words and copies |
478 |
* 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 |
479 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
493 |
} |
} |
494 |
|
|
495 |
/** |
/** |
496 |
|
* Writes \a WordCount number of 16 Bit unsigned integer words from the |
497 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
498 |
|
* actual "physical" file. The position within the chunk will |
499 |
|
* automatically be incremented. Note: you cannot write beyond the |
500 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
501 |
|
* before. |
502 |
|
* |
503 |
|
* @param pData source buffer (containing the data) |
504 |
|
* @param WordCount number of 16 Bit unsigned integers to write |
505 |
|
* @returns number of written integers |
506 |
|
* @throws RIFF::Exception if an IO error occured |
507 |
|
* @see Resize() |
508 |
|
*/ |
509 |
|
unsigned long Chunk::WriteUint16(uint16_t* pData, unsigned long WordCount) { |
510 |
|
return Write(pData, WordCount, 2); |
511 |
|
} |
512 |
|
|
513 |
|
/** |
514 |
* Reads \a WordCount number of 32 Bit signed integer words and copies |
* Reads \a WordCount number of 32 Bit signed integer words and copies |
515 |
* 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 |
516 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
530 |
} |
} |
531 |
|
|
532 |
/** |
/** |
533 |
|
* Writes \a WordCount number of 32 Bit signed integer words from the |
534 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
535 |
|
* actual "physical" file. The position within the chunk will |
536 |
|
* automatically be incremented. Note: you cannot write beyond the |
537 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
538 |
|
* before. |
539 |
|
* |
540 |
|
* @param pData source buffer (containing the data) |
541 |
|
* @param WordCount number of 32 Bit signed integers to write |
542 |
|
* @returns number of written integers |
543 |
|
* @throws RIFF::Exception if an IO error occured |
544 |
|
* @see Resize() |
545 |
|
*/ |
546 |
|
unsigned long Chunk::WriteInt32(int32_t* pData, unsigned long WordCount) { |
547 |
|
return Write(pData, WordCount, 4); |
548 |
|
} |
549 |
|
|
550 |
|
/** |
551 |
* Reads \a WordCount number of 32 Bit unsigned integer words and copies |
* Reads \a WordCount number of 32 Bit unsigned integer words and copies |
552 |
* 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 |
553 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
567 |
} |
} |
568 |
|
|
569 |
/** |
/** |
570 |
|
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
571 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
572 |
|
* actual "physical" file. The position within the chunk will |
573 |
|
* automatically be incremented. Note: you cannot write beyond the |
574 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
575 |
|
* before. |
576 |
|
* |
577 |
|
* @param pData source buffer (containing the data) |
578 |
|
* @param WordCount number of 32 Bit unsigned integers to write |
579 |
|
* @returns number of written integers |
580 |
|
* @throws RIFF::Exception if an IO error occured |
581 |
|
* @see Resize() |
582 |
|
*/ |
583 |
|
unsigned long Chunk::WriteUint32(uint32_t* pData, unsigned long WordCount) { |
584 |
|
return Write(pData, WordCount, 4); |
585 |
|
} |
586 |
|
|
587 |
|
/** |
588 |
* Reads one 8 Bit signed integer word and increments the position within |
* Reads one 8 Bit signed integer word and increments the position within |
589 |
* the chunk. |
* the chunk. |
590 |
* |
* |
684 |
return word; |
return word; |
685 |
} |
} |
686 |
|
|
687 |
|
/** @brief Load chunk body into RAM. |
688 |
|
* |
689 |
|
* Loads the whole chunk body into memory. You can modify the data in |
690 |
|
* RAM and save the data by calling File::Save() afterwards. |
691 |
|
* |
692 |
|
* <b>Caution:</b> the buffer pointer will be invalidated once |
693 |
|
* File::Save() was called. You have to call LoadChunkData() again to |
694 |
|
* get a new, valid pointer whenever File::Save() was called. |
695 |
|
* |
696 |
|
* You can call LoadChunkData() again if you previously scheduled to |
697 |
|
* enlarge this chunk with a Resize() call. In that case the buffer will |
698 |
|
* be enlarged to the new, scheduled chunk size and you can already |
699 |
|
* place the new chunk data to the buffer and finally call File::Save() |
700 |
|
* to enlarge the chunk physically and write the new data in one rush. |
701 |
|
* This approach is definitely recommended if you have to enlarge and |
702 |
|
* write new data to a lot of chunks. |
703 |
|
* |
704 |
|
* @returns a pointer to the data in RAM on success, NULL otherwise |
705 |
|
* @throws Exception if data buffer could not be enlarged |
706 |
|
* @see ReleaseChunkData() |
707 |
|
*/ |
708 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
709 |
if (!pChunkData) { |
if (!pChunkData && pFile->Filename != "") { |
710 |
#if POSIX |
#if POSIX |
711 |
if (lseek(hFile, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
712 |
pChunkData = new uint8_t[GetSize()]; |
#elif defined(WIN32) |
713 |
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()); |
|
714 |
#else |
#else |
715 |
if (fseek(hFile, ulStartPos, SEEK_SET)) return NULL; |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
716 |
pChunkData = new uint8_t[GetSize()]; |
#endif // POSIX |
717 |
|
unsigned long ulBufferSize = (CurrentChunkSize > NewChunkSize) ? CurrentChunkSize : NewChunkSize; |
718 |
|
pChunkData = new uint8_t[ulBufferSize]; |
719 |
if (!pChunkData) return NULL; |
if (!pChunkData) return NULL; |
720 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), hFile); |
memset(pChunkData, 0, ulBufferSize); |
721 |
|
#if POSIX |
722 |
|
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
723 |
|
#elif defined(WIN32) |
724 |
|
DWORD readWords; |
725 |
|
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); |
726 |
|
#else |
727 |
|
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
728 |
#endif // POSIX |
#endif // POSIX |
729 |
if (readWords != GetSize()) { |
if (readWords != GetSize()) { |
730 |
delete[] pChunkData; |
delete[] pChunkData; |
731 |
return (pChunkData = NULL); |
return (pChunkData = NULL); |
732 |
} |
} |
733 |
|
ulChunkDataSize = ulBufferSize; |
734 |
|
} else if (NewChunkSize > ulChunkDataSize) { |
735 |
|
uint8_t* pNewBuffer = new uint8_t[NewChunkSize]; |
736 |
|
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(NewChunkSize) + " bytes"); |
737 |
|
memset(pNewBuffer, 0 , NewChunkSize); |
738 |
|
memcpy(pNewBuffer, pChunkData, ulChunkDataSize); |
739 |
|
delete[] pChunkData; |
740 |
|
pChunkData = pNewBuffer; |
741 |
|
ulChunkDataSize = NewChunkSize; |
742 |
} |
} |
743 |
return pChunkData; |
return pChunkData; |
744 |
} |
} |
745 |
|
|
746 |
|
/** @brief Free loaded chunk body from RAM. |
747 |
|
* |
748 |
|
* Frees loaded chunk body data from memory (RAM). You should call |
749 |
|
* File::Save() before calling this method if you modified the data to |
750 |
|
* make the changes persistent. |
751 |
|
*/ |
752 |
void Chunk::ReleaseChunkData() { |
void Chunk::ReleaseChunkData() { |
753 |
if (pChunkData) { |
if (pChunkData) { |
754 |
delete[] pChunkData; |
delete[] pChunkData; |
756 |
} |
} |
757 |
} |
} |
758 |
|
|
759 |
|
/** @brief Resize chunk. |
760 |
|
* |
761 |
|
* Resizes this chunk's body, that is the actual size of data possible |
762 |
|
* to be written to this chunk. This call will return immediately and |
763 |
|
* just schedule the resize operation. You should call File::Save() to |
764 |
|
* actually perform the resize operation(s) "physically" to the file. |
765 |
|
* As this can take a while on large files, it is recommended to call |
766 |
|
* Resize() first on all chunks which have to be resized and finally to |
767 |
|
* call File::Save() to perform all those resize operations in one rush. |
768 |
|
* |
769 |
|
* <b>Caution:</b> You cannot directly write to enlarged chunks before |
770 |
|
* calling File::Save() as this might exceed the current chunk's body |
771 |
|
* boundary! |
772 |
|
* |
773 |
|
* @param iNewSize - new chunk body size in bytes (must be greater than zero) |
774 |
|
* @throws RIFF::Exception if \a iNewSize is less than 1 |
775 |
|
* @see File::Save() |
776 |
|
*/ |
777 |
|
void Chunk::Resize(int iNewSize) { |
778 |
|
if (iNewSize <= 0) throw Exception("Chunk size must be at least one byte"); |
779 |
|
if (NewChunkSize == iNewSize) return; |
780 |
|
NewChunkSize = iNewSize; |
781 |
|
pFile->LogAsResized(this); |
782 |
|
} |
783 |
|
|
784 |
|
/** @brief Write chunk persistently e.g. to disk. |
785 |
|
* |
786 |
|
* Stores the chunk persistently to its actual "physical" file. |
787 |
|
* |
788 |
|
* @param ulWritePos - position within the "physical" file where this |
789 |
|
* chunk should be written to |
790 |
|
* @param ulCurrentDataOffset - offset of current (old) data within |
791 |
|
* the file |
792 |
|
* @returns new write position in the "physical" file, that is |
793 |
|
* \a ulWritePos incremented by this chunk's new size |
794 |
|
* (including its header size of course) |
795 |
|
*/ |
796 |
|
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
797 |
|
const unsigned long ulOriginalPos = ulWritePos; |
798 |
|
ulWritePos += CHUNK_HEADER_SIZE; |
799 |
|
|
800 |
|
if (pFile->Mode != stream_mode_read_write) |
801 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
802 |
|
|
803 |
|
// if the whole chunk body was loaded into RAM |
804 |
|
if (pChunkData) { |
805 |
|
// make sure chunk data buffer in RAM is at least as large as the new chunk size |
806 |
|
LoadChunkData(); |
807 |
|
// write chunk data from RAM persistently to the file |
808 |
|
#if POSIX |
809 |
|
lseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
810 |
|
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
811 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
812 |
|
} |
813 |
|
#elif defined(WIN32) |
814 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN); |
815 |
|
DWORD dwBytesWritten; |
816 |
|
WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL); |
817 |
|
if (dwBytesWritten != NewChunkSize) { |
818 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
819 |
|
} |
820 |
|
#else |
821 |
|
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
822 |
|
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
823 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
824 |
|
} |
825 |
|
#endif // POSIX |
826 |
|
} else { |
827 |
|
// move chunk data from the end of the file to the appropriate position |
828 |
|
int8_t* pCopyBuffer = new int8_t[4096]; |
829 |
|
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
830 |
|
#if defined(WIN32) |
831 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
832 |
|
#else |
833 |
|
int iBytesMoved = 1; |
834 |
|
#endif |
835 |
|
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
836 |
|
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
837 |
|
#if POSIX |
838 |
|
lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
839 |
|
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
840 |
|
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
841 |
|
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
842 |
|
#elif defined(WIN32) |
843 |
|
SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
844 |
|
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
845 |
|
SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
846 |
|
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
847 |
|
#else |
848 |
|
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
849 |
|
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
850 |
|
fseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
851 |
|
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, pFile->hFileWrite); |
852 |
|
#endif |
853 |
|
} |
854 |
|
delete[] pCopyBuffer; |
855 |
|
if (iBytesMoved < 0) throw Exception("Writing Chunk data (from file) failed"); |
856 |
|
} |
857 |
|
|
858 |
|
// update this chunk's header |
859 |
|
CurrentChunkSize = NewChunkSize; |
860 |
|
WriteHeader(ulOriginalPos); |
861 |
|
|
862 |
|
// update chunk's position pointers |
863 |
|
ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE; |
864 |
|
ulPos = 0; |
865 |
|
|
866 |
|
// add pad byte if needed |
867 |
|
if ((ulStartPos + NewChunkSize) % 2 != 0) { |
868 |
|
const char cPadByte = 0; |
869 |
|
#if POSIX |
870 |
|
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
871 |
|
write(pFile->hFileWrite, &cPadByte, 1); |
872 |
|
#elif defined(WIN32) |
873 |
|
SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN); |
874 |
|
DWORD dwBytesWritten; |
875 |
|
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
876 |
|
#else |
877 |
|
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
878 |
|
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
879 |
|
#endif |
880 |
|
return ulStartPos + NewChunkSize + 1; |
881 |
|
} |
882 |
|
|
883 |
|
return ulStartPos + NewChunkSize; |
884 |
|
} |
885 |
|
|
886 |
|
void Chunk::__resetPos() { |
887 |
|
ulPos = 0; |
888 |
|
} |
889 |
|
|
890 |
|
|
891 |
|
|
892 |
// *************** List *************** |
// *************** List *************** |
893 |
// * |
// * |
894 |
|
|
895 |
List::List() : Chunk() { |
List::List(File* pFile) : Chunk(pFile) { |
896 |
#if DEBUG |
#if DEBUG |
897 |
std::cout << "List::List()" << std::endl; |
std::cout << "List::List(File* pFile)" << std::endl; |
898 |
#endif // DEBUG |
#endif // DEBUG |
899 |
pSubChunks = NULL; |
pSubChunks = NULL; |
900 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
901 |
} |
} |
902 |
|
|
903 |
#if POSIX |
List::List(File* pFile, unsigned long StartPos, List* Parent) |
904 |
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) { |
|
905 |
#if DEBUG |
#if DEBUG |
906 |
std::cout << "List::List(FILE*,ulong,bool,List*)" << std::endl; |
std::cout << "List::List(File*,ulong,bool,List*)" << std::endl; |
907 |
#endif // DEBUG |
#endif // DEBUG |
908 |
pSubChunks = NULL; |
pSubChunks = NULL; |
909 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
911 |
ulStartPos = StartPos + LIST_HEADER_SIZE; |
ulStartPos = StartPos + LIST_HEADER_SIZE; |
912 |
} |
} |
913 |
|
|
914 |
|
List::List(File* pFile, List* pParent, uint32_t uiListID) |
915 |
|
: Chunk(pFile, pParent, CHUNK_ID_LIST, 0) { |
916 |
|
pSubChunks = NULL; |
917 |
|
pSubChunksMap = NULL; |
918 |
|
ListType = uiListID; |
919 |
|
} |
920 |
|
|
921 |
List::~List() { |
List::~List() { |
922 |
#if DEBUG |
#if DEBUG |
923 |
std::cout << "List::~List()" << std::endl; |
std::cout << "List::~List()" << std::endl; |
1061 |
} |
} |
1062 |
|
|
1063 |
/** |
/** |
1064 |
* Returns number subchunks within the list. |
* Returns number of subchunks within the list. |
1065 |
*/ |
*/ |
1066 |
unsigned int List::CountSubChunks() { |
unsigned int List::CountSubChunks() { |
1067 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1112 |
return result; |
return result; |
1113 |
} |
} |
1114 |
|
|
1115 |
|
/** @brief Creates a new sub chunk. |
1116 |
|
* |
1117 |
|
* Creates and adds a new sub chunk to this list chunk. Note that the |
1118 |
|
* chunk's body size given by \a uiBodySize must be greater than zero. |
1119 |
|
* You have to call File::Save() to make this change persistent to the |
1120 |
|
* actual file and <b>before</b> performing any data write operations |
1121 |
|
* on the new chunk! |
1122 |
|
* |
1123 |
|
* @param uiChunkID - chunk ID of the new chunk |
1124 |
|
* @param uiBodySize - size of the new chunk's body, that is its actual |
1125 |
|
* data size (without header) |
1126 |
|
* @throws RIFF::Exception if \a uiBodySize equals zero |
1127 |
|
*/ |
1128 |
|
Chunk* List::AddSubChunk(uint32_t uiChunkID, uint uiBodySize) { |
1129 |
|
if (uiBodySize == 0) throw Exception("Chunk body size must be at least 1 byte"); |
1130 |
|
if (!pSubChunks) LoadSubChunks(); |
1131 |
|
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0); |
1132 |
|
pSubChunks->push_back(pNewChunk); |
1133 |
|
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1134 |
|
pNewChunk->Resize(uiBodySize); |
1135 |
|
return pNewChunk; |
1136 |
|
} |
1137 |
|
|
1138 |
|
/** @brief Creates a new list sub chunk. |
1139 |
|
* |
1140 |
|
* Creates and adds a new list sub chunk to this list chunk. Note that |
1141 |
|
* you have to add sub chunks / sub list chunks to the new created chunk |
1142 |
|
* <b>before</b> trying to make this change persisten to the actual |
1143 |
|
* file with File::Save()! |
1144 |
|
* |
1145 |
|
* @param uiListType - list ID of the new list chunk |
1146 |
|
*/ |
1147 |
|
List* List::AddSubList(uint32_t uiListType) { |
1148 |
|
if (!pSubChunks) LoadSubChunks(); |
1149 |
|
List* pNewListChunk = new List(pFile, this, uiListType); |
1150 |
|
pSubChunks->push_back(pNewListChunk); |
1151 |
|
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1152 |
|
return pNewListChunk; |
1153 |
|
} |
1154 |
|
|
1155 |
|
/** @brief Removes a sub chunk. |
1156 |
|
* |
1157 |
|
* Removes the sub chunk given by \a pSubChunk from this list and frees |
1158 |
|
* it completely from RAM. The given chunk can either be a normal sub |
1159 |
|
* chunk or a list sub chunk. In case the given chunk is a list chunk, |
1160 |
|
* all its subchunks (if any) will be removed recursively as well. You |
1161 |
|
* should call File::Save() to make this change persistent at any time. |
1162 |
|
* |
1163 |
|
* @param pSubChunk - sub chunk or sub list chunk to be removed |
1164 |
|
*/ |
1165 |
|
void List::DeleteSubChunk(Chunk* pSubChunk) { |
1166 |
|
if (!pSubChunks) LoadSubChunks(); |
1167 |
|
pSubChunks->remove(pSubChunk); |
1168 |
|
if ((*pSubChunksMap)[pSubChunk->GetChunkID()] == pSubChunk) { |
1169 |
|
pSubChunksMap->erase(pSubChunk->GetChunkID()); |
1170 |
|
// try to find another chunk of the same chunk ID |
1171 |
|
ChunkList::iterator iter = pSubChunks->begin(); |
1172 |
|
ChunkList::iterator end = pSubChunks->end(); |
1173 |
|
for (; iter != end; ++iter) { |
1174 |
|
if ((*iter)->GetChunkID() == pSubChunk->GetChunkID()) { |
1175 |
|
(*pSubChunksMap)[pSubChunk->GetChunkID()] = *iter; |
1176 |
|
break; // we're done, stop search |
1177 |
|
} |
1178 |
|
} |
1179 |
|
} |
1180 |
|
delete pSubChunk; |
1181 |
|
} |
1182 |
|
|
1183 |
void List::ReadHeader(unsigned long fPos) { |
void List::ReadHeader(unsigned long fPos) { |
1184 |
#if DEBUG |
#if DEBUG |
1185 |
std::cout << "List::Readheader(ulong) "; |
std::cout << "List::Readheader(ulong) "; |
1186 |
#endif // DEBUG |
#endif // DEBUG |
1187 |
Chunk::ReadHeader(fPos); |
Chunk::ReadHeader(fPos); |
1188 |
ChunkSize -= 4; |
NewChunkSize = CurrentChunkSize -= 4; |
1189 |
#if POSIX |
#if POSIX |
1190 |
lseek(hFile, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1191 |
read(hFile, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1192 |
|
#elif defined(WIN32) |
1193 |
|
SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1194 |
|
DWORD dwBytesRead; |
1195 |
|
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
1196 |
#else |
#else |
1197 |
fseek(hFile, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1198 |
fread(&ListType, 4, 1, hFile); |
fread(&ListType, 4, 1, pFile->hFileRead); |
1199 |
#endif // POSIX |
#endif // POSIX |
1200 |
#if DEBUG |
#if DEBUG |
1201 |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
1202 |
#endif // DEBUG |
#endif // DEBUG |
1203 |
if (!bEndianNative) { |
if (!pFile->bEndianNative) { |
1204 |
//swapBytes_32(&ListType); |
//swapBytes_32(&ListType); |
1205 |
} |
} |
1206 |
} |
} |
1207 |
|
|
1208 |
|
void List::WriteHeader(unsigned long fPos) { |
1209 |
|
// the four list type bytes officially belong the chunk's body in the RIFF format |
1210 |
|
NewChunkSize += 4; |
1211 |
|
Chunk::WriteHeader(fPos); |
1212 |
|
NewChunkSize -= 4; // just revert the +4 incrementation |
1213 |
|
#if POSIX |
1214 |
|
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1215 |
|
write(pFile->hFileWrite, &ListType, 4); |
1216 |
|
#elif defined(WIN32) |
1217 |
|
SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
1218 |
|
DWORD dwBytesWritten; |
1219 |
|
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
1220 |
|
#else |
1221 |
|
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
1222 |
|
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
1223 |
|
#endif // POSIX |
1224 |
|
} |
1225 |
|
|
1226 |
void List::LoadSubChunks() { |
void List::LoadSubChunks() { |
1227 |
#if DEBUG |
#if DEBUG |
1228 |
std::cout << "List::LoadSubChunks()"; |
std::cout << "List::LoadSubChunks()"; |
1230 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1231 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1232 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1233 |
|
if (!pFile->hFileRead) return; |
1234 |
|
unsigned long uiOriginalPos = GetPos(); |
1235 |
|
SetPos(0); // jump to beginning of list chunk body |
1236 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
1237 |
Chunk* ck; |
Chunk* ck; |
1238 |
uint32_t ckid; |
uint32_t ckid; |
1241 |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
1242 |
#endif // DEBUG |
#endif // DEBUG |
1243 |
if (ckid == CHUNK_ID_LIST) { |
if (ckid == CHUNK_ID_LIST) { |
1244 |
ck = new RIFF::List(hFile, ulStartPos + ulPos - 4, bEndianNative, this); |
ck = new RIFF::List(pFile, ulStartPos + ulPos - 4, this); |
1245 |
SetPos(ck->GetSize() + LIST_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + LIST_HEADER_SIZE - 4, RIFF::stream_curpos); |
1246 |
} |
} |
1247 |
else { // simple chunk |
else { // simple chunk |
1248 |
ck = new RIFF::Chunk(hFile, ulStartPos + ulPos - 4, bEndianNative, this); |
ck = new RIFF::Chunk(pFile, ulStartPos + ulPos - 4, this); |
1249 |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE - 4, RIFF::stream_curpos); |
1250 |
} |
} |
1251 |
pSubChunks->push_back(ck); |
pSubChunks->push_back(ck); |
1252 |
(*pSubChunksMap)[ckid] = ck; |
(*pSubChunksMap)[ckid] = ck; |
1253 |
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 |
1254 |
} |
} |
1255 |
|
SetPos(uiOriginalPos); // restore position before this call |
1256 |
|
} |
1257 |
|
} |
1258 |
|
|
1259 |
|
void List::LoadSubChunksRecursively() { |
1260 |
|
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList()) |
1261 |
|
pList->LoadSubChunksRecursively(); |
1262 |
|
} |
1263 |
|
|
1264 |
|
/** @brief Write list chunk persistently e.g. to disk. |
1265 |
|
* |
1266 |
|
* Stores the list chunk persistently to its actual "physical" file. All |
1267 |
|
* subchunks (including sub list chunks) will be stored recursively as |
1268 |
|
* well. |
1269 |
|
* |
1270 |
|
* @param ulWritePos - position within the "physical" file where this |
1271 |
|
* list chunk should be written to |
1272 |
|
* @param ulCurrentDataOffset - offset of current (old) data within |
1273 |
|
* the file |
1274 |
|
* @returns new write position in the "physical" file, that is |
1275 |
|
* \a ulWritePos incremented by this list chunk's new size |
1276 |
|
* (including its header size of course) |
1277 |
|
*/ |
1278 |
|
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
1279 |
|
const unsigned long ulOriginalPos = ulWritePos; |
1280 |
|
ulWritePos += LIST_HEADER_SIZE; |
1281 |
|
|
1282 |
|
if (pFile->Mode != stream_mode_read_write) |
1283 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
1284 |
|
|
1285 |
|
// write all subchunks (including sub list chunks) recursively |
1286 |
|
if (pSubChunks) { |
1287 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
1288 |
|
ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset); |
1289 |
|
} |
1290 |
|
} |
1291 |
|
|
1292 |
|
// update this list chunk's header |
1293 |
|
CurrentChunkSize = NewChunkSize = ulWritePos - ulOriginalPos - LIST_HEADER_SIZE; |
1294 |
|
WriteHeader(ulOriginalPos); |
1295 |
|
|
1296 |
|
// offset of this list chunk in new written file may have changed |
1297 |
|
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
1298 |
|
|
1299 |
|
return ulWritePos; |
1300 |
|
} |
1301 |
|
|
1302 |
|
void List::__resetPos() { |
1303 |
|
Chunk::__resetPos(); |
1304 |
|
if (pSubChunks) { |
1305 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
1306 |
|
(*iter)->__resetPos(); |
1307 |
|
} |
1308 |
} |
} |
1309 |
} |
} |
1310 |
|
|
1320 |
// *************** File *************** |
// *************** File *************** |
1321 |
// * |
// * |
1322 |
|
|
1323 |
File::File(const String& path) : List() { |
/** @brief Create new RIFF file. |
1324 |
|
* |
1325 |
|
* Use this constructor if you want to create a new RIFF file completely |
1326 |
|
* "from scratch". Note: there must be no empty chunks or empty list |
1327 |
|
* chunks when trying to make the new RIFF file persistent with Save()! |
1328 |
|
* |
1329 |
|
* @param FileType - four-byte identifier of the RIFF file type |
1330 |
|
* @see AddSubChunk(), AddSubList() |
1331 |
|
*/ |
1332 |
|
File::File(uint32_t FileType) : List(this) { |
1333 |
|
#if defined(WIN32) |
1334 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1335 |
|
#else |
1336 |
|
hFileRead = hFileWrite = 0; |
1337 |
|
#endif |
1338 |
|
Mode = stream_mode_closed; |
1339 |
|
bEndianNative = true; |
1340 |
|
ulStartPos = RIFF_HEADER_SIZE; |
1341 |
|
ListType = FileType; |
1342 |
|
} |
1343 |
|
|
1344 |
|
/** @brief Load existing RIFF file. |
1345 |
|
* |
1346 |
|
* Loads an existing RIFF file with all its chunks. |
1347 |
|
* |
1348 |
|
* @param path - path and file name of the RIFF file to open |
1349 |
|
* @throws RIFF::Exception if error occured while trying to load the |
1350 |
|
* given RIFF file |
1351 |
|
*/ |
1352 |
|
File::File(const String& path) : List(this), Filename(path) { |
1353 |
#if DEBUG |
#if DEBUG |
1354 |
std::cout << "File::File("<<path<<")" << std::endl; |
std::cout << "File::File("<<path<<")" << std::endl; |
1355 |
#endif // DEBUG |
#endif // DEBUG |
1356 |
bEndianNative = true; |
bEndianNative = true; |
1357 |
#if POSIX |
#if POSIX |
1358 |
hFile = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1359 |
if (hFile <= 0) { |
if (hFileRead <= 0) { |
1360 |
hFile = 0; |
hFileRead = hFileWrite = 0; |
1361 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1362 |
|
} |
1363 |
|
#elif defined(WIN32) |
1364 |
|
hFileRead = hFileWrite = CreateFile( |
1365 |
|
path.c_str(), GENERIC_READ, |
1366 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1367 |
|
NULL, OPEN_EXISTING, |
1368 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1369 |
|
); |
1370 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1371 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1372 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1373 |
} |
} |
1374 |
#else |
#else |
1375 |
hFile = fopen(path.c_str(), "rb"); |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1376 |
if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1377 |
#endif // POSIX |
#endif // POSIX |
1378 |
|
Mode = stream_mode_read; |
1379 |
ulStartPos = RIFF_HEADER_SIZE; |
ulStartPos = RIFF_HEADER_SIZE; |
1380 |
ReadHeader(0); |
ReadHeader(0); |
1381 |
if (ChunkID != CHUNK_ID_RIFF) { |
if (ChunkID != CHUNK_ID_RIFF) { |
1383 |
} |
} |
1384 |
} |
} |
1385 |
|
|
1386 |
|
String File::GetFileName() { |
1387 |
|
return Filename; |
1388 |
|
} |
1389 |
|
|
1390 |
|
stream_mode_t File::GetMode() { |
1391 |
|
return Mode; |
1392 |
|
} |
1393 |
|
|
1394 |
|
/** @brief Change file access mode. |
1395 |
|
* |
1396 |
|
* Changes files access mode either to read-only mode or to read/write |
1397 |
|
* mode. |
1398 |
|
* |
1399 |
|
* @param NewMode - new file access mode |
1400 |
|
* @returns true if mode was changed, false if current mode already |
1401 |
|
* equals new mode |
1402 |
|
* @throws RIFF::Exception if new file access mode is unknown |
1403 |
|
*/ |
1404 |
|
bool File::SetMode(stream_mode_t NewMode) { |
1405 |
|
if (NewMode != Mode) { |
1406 |
|
switch (NewMode) { |
1407 |
|
case stream_mode_read: |
1408 |
|
#if POSIX |
1409 |
|
if (hFileRead) close(hFileRead); |
1410 |
|
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1411 |
|
if (hFileRead < 0) { |
1412 |
|
hFileRead = hFileWrite = 0; |
1413 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1414 |
|
} |
1415 |
|
#elif defined(WIN32) |
1416 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1417 |
|
hFileRead = hFileWrite = CreateFile( |
1418 |
|
Filename.c_str(), GENERIC_READ, |
1419 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1420 |
|
NULL, OPEN_EXISTING, |
1421 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1422 |
|
); |
1423 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1424 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1425 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1426 |
|
} |
1427 |
|
#else |
1428 |
|
if (hFileRead) fclose(hFileRead); |
1429 |
|
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1430 |
|
if (!hFileRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1431 |
|
#endif |
1432 |
|
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1433 |
|
break; |
1434 |
|
case stream_mode_read_write: |
1435 |
|
#if POSIX |
1436 |
|
if (hFileRead) close(hFileRead); |
1437 |
|
hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
1438 |
|
if (hFileRead < 0) { |
1439 |
|
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1440 |
|
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1441 |
|
} |
1442 |
|
#elif defined(WIN32) |
1443 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1444 |
|
hFileRead = hFileWrite = CreateFile( |
1445 |
|
Filename.c_str(), |
1446 |
|
GENERIC_READ | GENERIC_WRITE, |
1447 |
|
FILE_SHARE_READ, |
1448 |
|
NULL, OPEN_ALWAYS, |
1449 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1450 |
|
); |
1451 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1452 |
|
hFileRead = hFileWrite = CreateFile( |
1453 |
|
Filename.c_str(), GENERIC_READ, |
1454 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1455 |
|
NULL, OPEN_EXISTING, |
1456 |
|
FILE_ATTRIBUTE_NORMAL, NULL |
1457 |
|
); |
1458 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
1459 |
|
} |
1460 |
|
#else |
1461 |
|
if (hFileRead) fclose(hFileRead); |
1462 |
|
hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b"); |
1463 |
|
if (!hFileRead) { |
1464 |
|
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
1465 |
|
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
1466 |
|
} |
1467 |
|
#endif |
1468 |
|
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
1469 |
|
break; |
1470 |
|
case stream_mode_closed: |
1471 |
|
#if POSIX |
1472 |
|
if (hFileRead) close(hFileRead); |
1473 |
|
if (hFileWrite) close(hFileWrite); |
1474 |
|
#elif defined(WIN32) |
1475 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1476 |
|
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
1477 |
|
#else |
1478 |
|
if (hFileRead) fclose(hFileRead); |
1479 |
|
if (hFileWrite) fclose(hFileWrite); |
1480 |
|
#endif |
1481 |
|
hFileRead = hFileWrite = 0; |
1482 |
|
break; |
1483 |
|
default: |
1484 |
|
throw Exception("Unknown file access mode"); |
1485 |
|
} |
1486 |
|
Mode = NewMode; |
1487 |
|
return true; |
1488 |
|
} |
1489 |
|
return false; |
1490 |
|
} |
1491 |
|
|
1492 |
|
/** @brief Save changes to same file. |
1493 |
|
* |
1494 |
|
* Make all changes of all chunks persistent by writing them to the |
1495 |
|
* actual (same) file. The file might temporarily grow to a higher size |
1496 |
|
* than it will have at the end of the saving process, in case chunks |
1497 |
|
* were grown. |
1498 |
|
* |
1499 |
|
* @throws RIFF::Exception if there is an empty chunk or empty list |
1500 |
|
* chunk or any kind of IO error occured |
1501 |
|
*/ |
1502 |
|
void File::Save() { |
1503 |
|
// make sure the RIFF tree is built (from the original file) |
1504 |
|
LoadSubChunksRecursively(); |
1505 |
|
|
1506 |
|
// reopen file in write mode |
1507 |
|
SetMode(stream_mode_read_write); |
1508 |
|
|
1509 |
|
// to be able to save the whole file without loading everything into |
1510 |
|
// RAM and without having to store the data in a temporary file, we |
1511 |
|
// enlarge the file with the sum of all _positive_ chunk size |
1512 |
|
// changes, move current data towards the end of the file with the |
1513 |
|
// calculated sum and finally update / rewrite the file by copying |
1514 |
|
// the old data back to the right position at the beginning of the file |
1515 |
|
|
1516 |
|
// first we sum up all positive chunk size changes (and skip all negative ones) |
1517 |
|
unsigned long ulPositiveSizeDiff = 0; |
1518 |
|
for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
1519 |
|
if ((*iter)->GetNewSize() == 0) { |
1520 |
|
// just to make the exception message a bit more verbose: resolve the chunk's path |
1521 |
|
String sChunkPath; |
1522 |
|
for (Chunk* pChunk = *iter; pChunk; pChunk = pChunk->GetParent()) { |
1523 |
|
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
1524 |
|
List* pList = (List*) pChunk; |
1525 |
|
sChunkPath = "->'" + pList->GetListTypeString() + "'" + sChunkPath; |
1526 |
|
} else { |
1527 |
|
sChunkPath = "->'" + pChunk->GetChunkIDString() + "'" + sChunkPath; |
1528 |
|
} |
1529 |
|
} |
1530 |
|
throw Exception("There is at least one empty chunk (zero size): " + sChunkPath); |
1531 |
|
} |
1532 |
|
if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) { |
1533 |
|
unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte |
1534 |
|
ulPositiveSizeDiff += ulDiff; |
1535 |
|
} |
1536 |
|
} |
1537 |
|
|
1538 |
|
unsigned long ulWorkingFileSize = GetFileSize(); |
1539 |
|
|
1540 |
|
// if there are positive size changes... |
1541 |
|
if (ulPositiveSizeDiff > 0) { |
1542 |
|
// ... we enlarge this file first ... |
1543 |
|
ulWorkingFileSize += ulPositiveSizeDiff; |
1544 |
|
ResizeFile(ulWorkingFileSize); |
1545 |
|
// ... and move current data by the same amount towards end of file. |
1546 |
|
int8_t* pCopyBuffer = new int8_t[4096]; |
1547 |
|
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
1548 |
|
#if defined(WIN32) |
1549 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1550 |
|
#else |
1551 |
|
int iBytesMoved = 1; |
1552 |
|
#endif |
1553 |
|
for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) { |
1554 |
|
const unsigned long ulToMove = ulFileSize - ulPos; |
1555 |
|
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
1556 |
|
#if POSIX |
1557 |
|
lseek(hFileRead, ulPos, SEEK_SET); |
1558 |
|
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1559 |
|
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1560 |
|
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1561 |
|
#elif defined(WIN32) |
1562 |
|
SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN); |
1563 |
|
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1564 |
|
SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN); |
1565 |
|
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1566 |
|
#else |
1567 |
|
fseek(hFileRead, ulPos, SEEK_SET); |
1568 |
|
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
1569 |
|
fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
1570 |
|
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
1571 |
|
#endif |
1572 |
|
} |
1573 |
|
delete[] pCopyBuffer; |
1574 |
|
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
1575 |
|
} |
1576 |
|
|
1577 |
|
// rebuild / rewrite complete RIFF tree |
1578 |
|
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
1579 |
|
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1580 |
|
|
1581 |
|
// resize file to the final size |
1582 |
|
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1583 |
|
|
1584 |
|
// forget all resized chunks |
1585 |
|
ResizedChunks.clear(); |
1586 |
|
} |
1587 |
|
|
1588 |
|
/** @brief Save changes to another file. |
1589 |
|
* |
1590 |
|
* Make all changes of all chunks persistent by writing them to another |
1591 |
|
* file. <b>Caution:</b> this method is optimized for writing to |
1592 |
|
* <b>another</b> file, do not use it to save the changes to the same |
1593 |
|
* file! Use File::Save() in that case instead! Ignoring this might |
1594 |
|
* result in a corrupted file, especially in case chunks were resized! |
1595 |
|
* |
1596 |
|
* After calling this method, this File object will be associated with |
1597 |
|
* the new file (given by \a path) afterwards. |
1598 |
|
* |
1599 |
|
* @param path - path and file name where everything should be written to |
1600 |
|
*/ |
1601 |
|
void File::Save(const String& path) { |
1602 |
|
//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 |
1603 |
|
|
1604 |
|
// make sure the RIFF tree is built (from the original file) |
1605 |
|
LoadSubChunksRecursively(); |
1606 |
|
|
1607 |
|
if (Filename.length() > 0) SetMode(stream_mode_read); |
1608 |
|
// open the other (new) file for writing and truncate it to zero size |
1609 |
|
#if POSIX |
1610 |
|
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
1611 |
|
if (hFileWrite < 0) { |
1612 |
|
hFileWrite = hFileRead; |
1613 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1614 |
|
} |
1615 |
|
#elif defined(WIN32) |
1616 |
|
hFileWrite = CreateFile( |
1617 |
|
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
1618 |
|
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL |
1619 |
|
); |
1620 |
|
if (hFileWrite == INVALID_HANDLE_VALUE) { |
1621 |
|
hFileWrite = hFileRead; |
1622 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1623 |
|
} |
1624 |
|
#else |
1625 |
|
hFileWrite = fopen(path.c_str(), "w+b"); |
1626 |
|
if (!hFileWrite) { |
1627 |
|
hFileWrite = hFileRead; |
1628 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
1629 |
|
} |
1630 |
|
#endif // POSIX |
1631 |
|
Mode = stream_mode_read_write; |
1632 |
|
|
1633 |
|
// write complete RIFF tree to the other (new) file |
1634 |
|
unsigned long ulTotalSize = WriteChunk(0, 0); |
1635 |
|
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
1636 |
|
|
1637 |
|
// resize file to the final size (if the file was originally larger) |
1638 |
|
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
1639 |
|
|
1640 |
|
// forget all resized chunks |
1641 |
|
ResizedChunks.clear(); |
1642 |
|
|
1643 |
|
if (Filename.length() > 0) { |
1644 |
|
#if POSIX |
1645 |
|
close(hFileWrite); |
1646 |
|
#elif defined(WIN32) |
1647 |
|
CloseHandle(hFileWrite); |
1648 |
|
#else |
1649 |
|
fclose(hFileWrite); |
1650 |
|
#endif |
1651 |
|
hFileWrite = hFileRead; |
1652 |
|
} |
1653 |
|
|
1654 |
|
// associate new file with this File object from now on |
1655 |
|
Filename = path; |
1656 |
|
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1657 |
|
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
1658 |
|
} |
1659 |
|
|
1660 |
|
void File::ResizeFile(unsigned long ulNewSize) { |
1661 |
|
#if POSIX |
1662 |
|
if (ftruncate(hFileWrite, ulNewSize) < 0) |
1663 |
|
throw Exception("Could not resize file \"" + Filename + "\""); |
1664 |
|
#elif defined(WIN32) |
1665 |
|
if ( |
1666 |
|
SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER || |
1667 |
|
!SetEndOfFile(hFileWrite) |
1668 |
|
) throw Exception("Could not resize file \"" + Filename + "\""); |
1669 |
|
#else |
1670 |
|
# error Sorry, this version of libgig only supports POSIX and Windows systems yet. |
1671 |
|
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
1672 |
|
#endif |
1673 |
|
} |
1674 |
|
|
1675 |
File::~File() { |
File::~File() { |
1676 |
#if DEBUG |
#if DEBUG |
1677 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
1678 |
#endif // DEBUG |
#endif // DEBUG |
1679 |
#if POSIX |
#if POSIX |
1680 |
if (hFile) close(hFile); |
if (hFileRead) close(hFileRead); |
1681 |
|
#elif defined(WIN32) |
1682 |
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1683 |
#else |
#else |
1684 |
if (hFile) fclose(hFile); |
if (hFileRead) fclose(hFileRead); |
1685 |
#endif // POSIX |
#endif // POSIX |
1686 |
} |
} |
1687 |
|
|
1688 |
|
void File::LogAsResized(Chunk* pResizedChunk) { |
1689 |
|
ResizedChunks.push_back(pResizedChunk); |
1690 |
|
} |
1691 |
|
|
1692 |
|
void File::UnlogResized(Chunk* pResizedChunk) { |
1693 |
|
ResizedChunks.remove(pResizedChunk); |
1694 |
|
} |
1695 |
|
|
1696 |
unsigned long File::GetFileSize() { |
unsigned long File::GetFileSize() { |
1697 |
#if POSIX |
return __GetFileSize(hFileRead); |
1698 |
|
} |
1699 |
|
|
1700 |
|
#if POSIX |
1701 |
|
unsigned long File::__GetFileSize(int hFile) { |
1702 |
struct stat filestat; |
struct stat filestat; |
1703 |
fstat(hFile, &filestat); |
fstat(hFile, &filestat); |
1704 |
long size = filestat.st_size; |
long size = filestat.st_size; |
1705 |
#else // standard C functions |
return size; |
1706 |
|
} |
1707 |
|
#elif defined(WIN32) |
1708 |
|
unsigned long File::__GetFileSize(HANDLE hFile) { |
1709 |
|
DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/); |
1710 |
|
if (dwSize == INVALID_FILE_SIZE) |
1711 |
|
throw Exception("Windows FS error: could not determine file size"); |
1712 |
|
return dwSize; |
1713 |
|
} |
1714 |
|
#else // standard C functions |
1715 |
|
unsigned long File::__GetFileSize(FILE* hFile) { |
1716 |
long curpos = ftell(hFile); |
long curpos = ftell(hFile); |
1717 |
fseek(hFile, 0, SEEK_END); |
fseek(hFile, 0, SEEK_END); |
1718 |
long size = ftell(hFile); |
long size = ftell(hFile); |
1719 |
fseek(hFile, curpos, SEEK_SET); |
fseek(hFile, curpos, SEEK_SET); |
|
#endif // POSIX |
|
1720 |
return size; |
return size; |
1721 |
} |
} |
1722 |
|
#endif |
1723 |
|
|
1724 |
|
|
1725 |
// *************** Exception *************** |
// *************** Exception *************** |
1729 |
std::cout << "RIFF::Exception: " << Message << std::endl; |
std::cout << "RIFF::Exception: " << Message << std::endl; |
1730 |
} |
} |
1731 |
|
|
1732 |
|
|
1733 |
|
// *************** functions *************** |
1734 |
|
// * |
1735 |
|
|
1736 |
|
/** |
1737 |
|
* Returns the name of this C++ library. This is usually "libgig" of |
1738 |
|
* course. This call is equivalent to DLS::libraryName() and |
1739 |
|
* gig::libraryName(). |
1740 |
|
*/ |
1741 |
|
String libraryName() { |
1742 |
|
return PACKAGE; |
1743 |
|
} |
1744 |
|
|
1745 |
|
/** |
1746 |
|
* Returns version of this C++ library. This call is equivalent to |
1747 |
|
* DLS::libraryVersion() and gig::libraryVersion(). |
1748 |
|
*/ |
1749 |
|
String libraryVersion() { |
1750 |
|
return VERSION; |
1751 |
|
} |
1752 |
|
|
1753 |
} // namespace RIFF |
} // namespace RIFF |
|
#endif |
|