/[svn]/libgig/trunk/src/RIFF.cpp
ViewVC logotype

Annotation of /libgig/trunk/src/RIFF.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 798 - (hide annotations) (download)
Thu Nov 3 23:49:11 2005 UTC (18 years, 4 months ago) by schoenebeck
File size: 62285 byte(s)
* fixed write support in RIFF classes

1 schoenebeck 2 /***************************************************************************
2     * *
3     * libgig - C++ cross-platform Gigasampler format file loader library *
4     * *
5 schoenebeck 384 * Copyright (C) 2003-2005 by Christian Schoenebeck *
6     * <cuse@users.sourceforge.net> *
7 schoenebeck 2 * *
8     * 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 *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This library is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this library; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23 schoenebeck 780
24     #include <string.h>
25     #include <sstream>
26    
27 schoenebeck 2 #include "RIFF.h"
28    
29     namespace RIFF {
30    
31 schoenebeck 780 // *************** Helper Functions **************
32     // *
33    
34     template<class T> inline String ToString(T o) {
35     std::stringstream ss;
36     ss << o;
37     return ss.str();
38     }
39    
40    
41    
42 schoenebeck 2 // *************** Chunk **************
43     // *
44    
45 schoenebeck 780 Chunk::Chunk(File* pFile) {
46 schoenebeck 2 #if DEBUG
47 schoenebeck 780 std::cout << "Chunk::Chunk(File* pFile)" << std::endl;
48 schoenebeck 2 #endif // DEBUG
49     ulPos = 0;
50     pParent = NULL;
51     pChunkData = NULL;
52 schoenebeck 798 ChunkID = CHUNK_ID_RIFF;
53 schoenebeck 780 this->pFile = pFile;
54 schoenebeck 2 }
55    
56 schoenebeck 780 Chunk::Chunk(File* pFile, unsigned long StartPos, List* Parent) {
57 schoenebeck 2 #if DEBUG
58 schoenebeck 780 std::cout << "Chunk::Chunk(File*,ulong,bool,List*),StartPos=" << StartPos << std::endl;
59 schoenebeck 2 #endif // DEBUG
60 schoenebeck 780 this->pFile = pFile;
61 schoenebeck 2 ulStartPos = StartPos + CHUNK_HEADER_SIZE;
62     pParent = Parent;
63     ulPos = 0;
64     pChunkData = NULL;
65     ReadHeader(StartPos);
66     }
67    
68 schoenebeck 780 Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize) {
69     this->pFile = pFile;
70     ulStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk
71     this->pParent = pParent;
72     ulPos = 0;
73     pChunkData = NULL;
74     ChunkID = uiChunkID;
75     CurrentChunkSize = 0;
76     NewChunkSize = uiBodySize;
77     }
78    
79 schoenebeck 2 Chunk::~Chunk() {
80     if (pChunkData) delete[] pChunkData;
81     }
82    
83     void Chunk::ReadHeader(unsigned long fPos) {
84     #if DEBUG
85     std::cout << "Chunk::Readheader(" << fPos << ") ";
86     #endif // DEBUG
87     #if POSIX
88 schoenebeck 780 if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) {
89     read(pFile->hFileRead, &ChunkID, 4);
90     read(pFile->hFileRead, &CurrentChunkSize, 4);
91 schoenebeck 2 #else
92 schoenebeck 780 if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) {
93     fread(&ChunkID, 4, 1, pFile->hFileRead);
94     fread(&CurrentChunkSize, 4, 1, pFile->hFileRead);
95 schoenebeck 2 #endif // POSIX
96     #if WORDS_BIGENDIAN
97     if (ChunkID == CHUNK_ID_RIFF) {
98     bEndianNative = false;
99     }
100     #else // little endian
101     if (ChunkID == CHUNK_ID_RIFX) {
102 schoenebeck 780 pFile->bEndianNative = false;
103 schoenebeck 2 ChunkID = CHUNK_ID_RIFF;
104     }
105     #endif // WORDS_BIGENDIAN
106 schoenebeck 780 if (!pFile->bEndianNative) {
107 schoenebeck 11 //swapBytes_32(&ChunkID);
108 schoenebeck 780 swapBytes_32(&CurrentChunkSize);
109 schoenebeck 2 }
110 schoenebeck 11 #if DEBUG
111     std::cout << "ckID=" << convertToString(ChunkID) << " ";
112     std::cout << "ckSize=" << ChunkSize << " ";
113     std::cout << "bEndianNative=" << bEndianNative << std::endl;
114     #endif // DEBUG
115 schoenebeck 780 NewChunkSize = CurrentChunkSize;
116 schoenebeck 2 }
117     }
118    
119 schoenebeck 780 void Chunk::WriteHeader(unsigned long fPos) {
120     uint32_t uiNewChunkID = ChunkID;
121     if (ChunkID == CHUNK_ID_RIFF) {
122     #if WORDS_BIGENDIAN
123     if (pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX;
124     #else // little endian
125     if (!pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX;
126     #endif // WORDS_BIGENDIAN
127     }
128    
129     uint32_t uiNewChunkSize = NewChunkSize;
130     if (!pFile->bEndianNative) {
131     swapBytes_32(&uiNewChunkSize);
132     }
133    
134     #if POSIX
135     if (lseek(pFile->hFileWrite, fPos, SEEK_SET) != -1) {
136     write(pFile->hFileWrite, &uiNewChunkID, 4);
137     write(pFile->hFileWrite, &uiNewChunkSize, 4);
138     }
139     #else
140     if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) {
141     fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite);
142     fwrite(&uiNewChunkSize, 4, 1, pFile->hFileWrite);
143     }
144     #endif // POSIX
145     }
146    
147 schoenebeck 11 /**
148     * Returns the String representation of the chunk's ID (e.g. "RIFF",
149     * "LIST").
150     */
151 schoenebeck 2 String Chunk::GetChunkIDString() {
152     return convertToString(ChunkID);
153     }
154    
155 schoenebeck 11 /**
156     * Sets the position within the chunk body, thus within the data portion
157     * of the chunk (in bytes).
158     *
159 schoenebeck 780 * <b>Caution:</b> the position will be reset to zero whenever
160     * File::Save() was called.
161     *
162 schoenebeck 11 * @param Where - position offset (in bytes)
163     * @param Whence - optional: defines to what <i>\a Where</i> relates to,
164     * if omitted \a Where relates to beginning of the chunk
165     * data
166     */
167 schoenebeck 2 unsigned long Chunk::SetPos(unsigned long Where, stream_whence_t Whence) {
168     #if DEBUG
169     std::cout << "Chunk::SetPos(ulong)" << std::endl;
170     #endif // DEBUG
171     switch (Whence) {
172     case stream_curpos:
173     ulPos += Where;
174     break;
175     case stream_end:
176 schoenebeck 780 ulPos = CurrentChunkSize - 1 - Where;
177 schoenebeck 2 break;
178     case stream_backward:
179     ulPos -= Where;
180     break;
181     case stream_start: default:
182     ulPos = Where;
183     break;
184     }
185 schoenebeck 780 if (ulPos > CurrentChunkSize) ulPos = CurrentChunkSize;
186 schoenebeck 2 return ulPos;
187     }
188    
189 schoenebeck 11 /**
190     * Returns the number of bytes left to read in the chunk body.
191     * When reading data from the chunk using the Read*() Methods, the
192     * position within the chunk data (that is the chunk body) will be
193     * incremented by the number of read bytes and RemainingBytes() returns
194     * how much data is left to read from the current position to the end
195     * of the chunk data.
196     *
197     * @returns number of bytes left to read
198     */
199 schoenebeck 2 unsigned long Chunk::RemainingBytes() {
200     #if DEBUG
201 schoenebeck 780 std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl;
202 schoenebeck 2 #endif // DEBUG
203 schoenebeck 780 return CurrentChunkSize - ulPos;
204 schoenebeck 2 }
205    
206 schoenebeck 11 /**
207     * Returns the current state of the chunk object.
208     * Following values are possible:
209     * - RIFF::stream_ready :
210     * chunk data can be read (this is the usual case)
211     * - RIFF::stream_closed :
212     * the data stream was closed somehow, no more reading possible
213     * - RIFF::stream_end_reached :
214     * alreaady reached the end of the chunk data, no more reading
215     * possible without SetPos()
216     */
217 schoenebeck 2 stream_state_t Chunk::GetState() {
218     #if DEBUG
219     std::cout << "Chunk::GetState()" << std::endl;
220     #endif // DEBUG
221     #if POSIX
222 schoenebeck 780 if (pFile->hFileRead == 0) return stream_closed;
223 schoenebeck 2 #else
224 schoenebeck 780 if (pFile->hFileRead == NULL) return stream_closed;
225 schoenebeck 2 #endif // POSIX
226 schoenebeck 780 if (ulPos < CurrentChunkSize) return stream_ready;
227     else return stream_end_reached;
228 schoenebeck 2 }
229    
230     /**
231     * Reads \a WordCount number of data words with given \a WordSize and
232     * copies it into a buffer pointed by \a pData. The buffer has to be
233     * allocated and be sure to provide the correct \a WordSize, as this
234     * will be important and taken into account for eventual endian
235     * correction (swapping of bytes due to different native byte order of
236     * a system). The position within the chunk will automatically be
237     * incremented.
238     *
239     * @param pData destination buffer
240     * @param WordCount number of data words to read
241     * @param WordSize size of each data word to read
242     * @returns number of successfully read data words or 0 if end
243     * of file reached or error occured
244     */
245     unsigned long Chunk::Read(void* pData, unsigned long WordCount, unsigned long WordSize) {
246     #if DEBUG
247     std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl;
248     #endif // DEBUG
249 schoenebeck 780 if (ulPos >= CurrentChunkSize) return 0;
250     if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize;
251 schoenebeck 2 #if POSIX
252 schoenebeck 780 if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0;
253     unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize);
254 schoenebeck 2 if (readWords < 1) return 0;
255     readWords /= WordSize;
256     #else // standard C functions
257 schoenebeck 780 if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0;
258     unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead);
259 schoenebeck 2 #endif // POSIX
260 schoenebeck 780 if (!pFile->bEndianNative && WordSize != 1) {
261 schoenebeck 2 switch (WordSize) {
262     case 2:
263     for (unsigned long iWord = 0; iWord < readWords; iWord++)
264     swapBytes_16((uint16_t*) pData + iWord);
265     break;
266     case 4:
267     for (unsigned long iWord = 0; iWord < readWords; iWord++)
268     swapBytes_32((uint32_t*) pData + iWord);
269     break;
270     default:
271     for (unsigned long iWord = 0; iWord < readWords; iWord++)
272     swapBytes((uint8_t*) pData + iWord * WordSize, WordSize);
273     break;
274     }
275     }
276     SetPos(readWords * WordSize, stream_curpos);
277     return readWords;
278     }
279    
280 schoenebeck 780 /**
281     * Writes \a WordCount number of data words with given \a WordSize from
282     * the buffer pointed by \a pData. Be sure to provide the correct
283     * \a WordSize, as this will be important and taken into account for
284     * eventual endian correction (swapping of bytes due to different
285     * native byte order of a system). The position within the chunk will
286     * automatically be incremented.
287     *
288     * @param pData source buffer (containing the data)
289     * @param WordCount number of data words to write
290     * @param WordSize size of each data word to write
291     * @returns number of successfully written data words
292     * @throws RIFF::Exception if write operation would exceed current
293     * chunk size or any IO error occured
294     * @see Resize()
295     */
296     unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) {
297 schoenebeck 798 if (pFile->Mode != stream_mode_read_write)
298     throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first");
299     if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize > CurrentChunkSize)
300 schoenebeck 780 throw Exception("End of chunk reached while trying to write data");
301     if (!pFile->bEndianNative && WordSize != 1) {
302     switch (WordSize) {
303     case 2:
304     for (unsigned long iWord = 0; iWord < WordCount; iWord++)
305     swapBytes_16((uint16_t*) pData + iWord);
306     break;
307     case 4:
308     for (unsigned long iWord = 0; iWord < WordCount; iWord++)
309     swapBytes_32((uint32_t*) pData + iWord);
310     break;
311     default:
312     for (unsigned long iWord = 0; iWord < WordCount; iWord++)
313     swapBytes((uint8_t*) pData + iWord * WordSize, WordSize);
314     break;
315     }
316     }
317     #if POSIX
318     if (lseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET) < 0) {
319     throw Exception("Could not seek to position " + ToString(ulPos) +
320     " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");
321     }
322     unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize);
323     if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data");
324     writtenWords /= WordSize;
325     #else // standard C functions
326     if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) {
327     throw Exception("Could not seek to position " + ToString(ulPos) +
328     " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");
329     }
330     unsigned long writtenWords = fwrite(pData, WordSize, WordCount, pFile->hFileWrite);
331     #endif // POSIX
332     SetPos(writtenWords * WordSize, stream_curpos);
333     return writtenWords;
334     }
335    
336 schoenebeck 2 /** Just an internal wrapper for the main <i>Read()</i> method with additional Exception throwing on errors. */
337     unsigned long Chunk::ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize) {
338     unsigned long readWords = Read(pData, WordCount, WordSize);
339     if (readWords != WordCount) throw RIFF::Exception("End of chunk data reached.");
340     return readWords;
341     }
342    
343     /**
344     * Reads \a WordCount number of 8 Bit signed integer words and copies it
345     * into the buffer pointed by \a pData. The buffer has to be allocated.
346     * The position within the chunk will automatically be incremented.
347     *
348     * @param pData destination buffer
349     * @param WordCount number of 8 Bit signed integers to read
350     * @returns number of read integers
351     * @throws RIFF::Exception if an error occured or less than
352     * \a WordCount integers could be read!
353     */
354     unsigned long Chunk::ReadInt8(int8_t* pData, unsigned long WordCount) {
355     #if DEBUG
356     std::cout << "Chunk::ReadInt8(int8_t*,ulong)" << std::endl;
357     #endif // DEBUG
358     return ReadSceptical(pData, WordCount, 1);
359     }
360    
361     /**
362 schoenebeck 780 * Writes \a WordCount number of 8 Bit signed integer words from the
363     * buffer pointed by \a pData to the chunk's body, directly to the
364     * actual "physical" file. The position within the chunk will
365     * automatically be incremented. Note: you cannot write beyond the
366     * boundaries of the chunk, to append data to the chunk call Resize()
367     * before.
368     *
369     * @param pData source buffer (containing the data)
370     * @param WordCount number of 8 Bit signed integers to write
371     * @returns number of written integers
372     * @throws RIFF::Exception if an IO error occured
373     * @see Resize()
374     */
375     unsigned long Chunk::WriteInt8(int8_t* pData, unsigned long WordCount) {
376     return Write(pData, WordCount, 1);
377     }
378    
379     /**
380 schoenebeck 2 * Reads \a WordCount number of 8 Bit unsigned integer words and copies
381     * it into the buffer pointed by \a pData. The buffer has to be
382     * allocated. The position within the chunk will automatically be
383     * incremented.
384     *
385     * @param pData destination buffer
386     * @param WordCount number of 8 Bit unsigned integers to read
387     * @returns number of read integers
388     * @throws RIFF::Exception if an error occured or less than
389     * \a WordCount integers could be read!
390     */
391     unsigned long Chunk::ReadUint8(uint8_t* pData, unsigned long WordCount) {
392     #if DEBUG
393     std::cout << "Chunk::ReadUint8(uint8_t*,ulong)" << std::endl;
394     #endif // DEBUG
395     return ReadSceptical(pData, WordCount, 1);
396     }
397    
398     /**
399 schoenebeck 780 * Writes \a WordCount number of 8 Bit unsigned integer words from the
400     * buffer pointed by \a pData to the chunk's body, directly to the
401     * actual "physical" file. The position within the chunk will
402     * automatically be incremented. Note: you cannot write beyond the
403     * boundaries of the chunk, to append data to the chunk call Resize()
404     * before.
405     *
406     * @param pData source buffer (containing the data)
407     * @param WordCount number of 8 Bit unsigned integers to write
408     * @returns number of written integers
409     * @throws RIFF::Exception if an IO error occured
410     * @see Resize()
411     */
412     unsigned long Chunk::WriteUint8(uint8_t* pData, unsigned long WordCount) {
413     return Write(pData, WordCount, 1);
414     }
415    
416     /**
417 schoenebeck 2 * Reads \a WordCount number of 16 Bit signed integer words and copies
418     * it into the buffer pointed by \a pData. The buffer has to be
419     * allocated. Endian correction will automatically be done if needed.
420     * The position within the chunk will automatically be incremented.
421     *
422     * @param pData destination buffer
423     * @param WordCount number of 16 Bit signed integers to read
424     * @returns number of read integers
425     * @throws RIFF::Exception if an error occured or less than
426     * \a WordCount integers could be read!
427     */
428     unsigned long Chunk::ReadInt16(int16_t* pData, unsigned long WordCount) {
429     #if DEBUG
430     std::cout << "Chunk::ReadInt16(int16_t*,ulong)" << std::endl;
431     #endif // DEBUG
432     return ReadSceptical(pData, WordCount, 2);
433     }
434    
435     /**
436 schoenebeck 780 * Writes \a WordCount number of 16 Bit signed integer words from the
437     * buffer pointed by \a pData to the chunk's body, directly to the
438     * actual "physical" file. The position within the chunk will
439     * automatically be incremented. Note: you cannot write beyond the
440     * boundaries of the chunk, to append data to the chunk call Resize()
441     * before.
442     *
443     * @param pData source buffer (containing the data)
444     * @param WordCount number of 16 Bit signed integers to write
445     * @returns number of written integers
446     * @throws RIFF::Exception if an IO error occured
447     * @see Resize()
448     */
449     unsigned long Chunk::WriteInt16(int16_t* pData, unsigned long WordCount) {
450     return Write(pData, WordCount, 2);
451     }
452    
453     /**
454 schoenebeck 2 * Reads \a WordCount number of 16 Bit unsigned integer words and copies
455     * it into the buffer pointed by \a pData. The buffer has to be
456     * allocated. Endian correction will automatically be done if needed.
457     * The position within the chunk will automatically be incremented.
458     *
459     * @param pData destination buffer
460     * @param WordCount number of 8 Bit unsigned integers to read
461     * @returns number of read integers
462     * @throws RIFF::Exception if an error occured or less than
463     * \a WordCount integers could be read!
464     */
465     unsigned long Chunk::ReadUint16(uint16_t* pData, unsigned long WordCount) {
466     #if DEBUG
467     std::cout << "Chunk::ReadUint16(uint16_t*,ulong)" << std::endl;
468     #endif // DEBUG
469     return ReadSceptical(pData, WordCount, 2);
470     }
471    
472     /**
473 schoenebeck 780 * Writes \a WordCount number of 16 Bit unsigned integer words from the
474     * buffer pointed by \a pData to the chunk's body, directly to the
475     * actual "physical" file. The position within the chunk will
476     * automatically be incremented. Note: you cannot write beyond the
477     * boundaries of the chunk, to append data to the chunk call Resize()
478     * before.
479     *
480     * @param pData source buffer (containing the data)
481     * @param WordCount number of 16 Bit unsigned integers to write
482     * @returns number of written integers
483     * @throws RIFF::Exception if an IO error occured
484     * @see Resize()
485     */
486     unsigned long Chunk::WriteUint16(uint16_t* pData, unsigned long WordCount) {
487     return Write(pData, WordCount, 2);
488     }
489    
490     /**
491 schoenebeck 2 * Reads \a WordCount number of 32 Bit signed integer words and copies
492     * it into the buffer pointed by \a pData. The buffer has to be
493     * allocated. Endian correction will automatically be done if needed.
494     * The position within the chunk will automatically be incremented.
495     *
496     * @param pData destination buffer
497     * @param WordCount number of 32 Bit signed integers to read
498     * @returns number of read integers
499     * @throws RIFF::Exception if an error occured or less than
500     * \a WordCount integers could be read!
501     */
502     unsigned long Chunk::ReadInt32(int32_t* pData, unsigned long WordCount) {
503     #if DEBUG
504     std::cout << "Chunk::ReadInt32(int32_t*,ulong)" << std::endl;
505     #endif // DEBUG
506     return ReadSceptical(pData, WordCount, 4);
507     }
508    
509     /**
510 schoenebeck 780 * Writes \a WordCount number of 32 Bit signed integer words from the
511     * buffer pointed by \a pData to the chunk's body, directly to the
512     * actual "physical" file. The position within the chunk will
513     * automatically be incremented. Note: you cannot write beyond the
514     * boundaries of the chunk, to append data to the chunk call Resize()
515     * before.
516     *
517     * @param pData source buffer (containing the data)
518     * @param WordCount number of 32 Bit signed integers to write
519     * @returns number of written integers
520     * @throws RIFF::Exception if an IO error occured
521     * @see Resize()
522     */
523     unsigned long Chunk::WriteInt32(int32_t* pData, unsigned long WordCount) {
524     return Write(pData, WordCount, 4);
525     }
526    
527     /**
528 schoenebeck 2 * Reads \a WordCount number of 32 Bit unsigned integer words and copies
529     * it into the buffer pointed by \a pData. The buffer has to be
530     * allocated. Endian correction will automatically be done if needed.
531     * The position within the chunk will automatically be incremented.
532     *
533     * @param pData destination buffer
534     * @param WordCount number of 32 Bit unsigned integers to read
535     * @returns number of read integers
536     * @throws RIFF::Exception if an error occured or less than
537     * \a WordCount integers could be read!
538     */
539     unsigned long Chunk::ReadUint32(uint32_t* pData, unsigned long WordCount) {
540     #if DEBUG
541     std::cout << "Chunk::ReadUint32(uint32_t*,ulong)" << std::endl;
542     #endif // DEBUG
543     return ReadSceptical(pData, WordCount, 4);
544     }
545    
546     /**
547 schoenebeck 780 * Writes \a WordCount number of 32 Bit unsigned integer words from the
548     * buffer pointed by \a pData to the chunk's body, directly to the
549     * actual "physical" file. The position within the chunk will
550     * automatically be incremented. Note: you cannot write beyond the
551     * boundaries of the chunk, to append data to the chunk call Resize()
552     * before.
553     *
554     * @param pData source buffer (containing the data)
555     * @param WordCount number of 32 Bit unsigned integers to write
556     * @returns number of written integers
557     * @throws RIFF::Exception if an IO error occured
558     * @see Resize()
559     */
560     unsigned long Chunk::WriteUint32(uint32_t* pData, unsigned long WordCount) {
561     return Write(pData, WordCount, 4);
562     }
563    
564     /**
565 schoenebeck 2 * Reads one 8 Bit signed integer word and increments the position within
566     * the chunk.
567     *
568     * @returns read integer word
569     * @throws RIFF::Exception if an error occured
570     */
571     int8_t Chunk::ReadInt8() {
572     #if DEBUG
573     std::cout << "Chunk::ReadInt8()" << std::endl;
574     #endif // DEBUG
575     int8_t word;
576     ReadSceptical(&word,1,1);
577     return word;
578     }
579    
580     /**
581     * Reads one 8 Bit unsigned integer word and increments the position
582     * within the chunk.
583     *
584     * @returns read integer word
585     * @throws RIFF::Exception if an error occured
586     */
587     uint8_t Chunk::ReadUint8() {
588     #if DEBUG
589     std::cout << "Chunk::ReadUint8()" << std::endl;
590     #endif // DEBUG
591     uint8_t word;
592     ReadSceptical(&word,1,1);
593     return word;
594     }
595    
596     /**
597     * Reads one 16 Bit signed integer word and increments the position
598     * within the chunk. Endian correction will automatically be done if
599     * needed.
600     *
601     * @returns read integer word
602     * @throws RIFF::Exception if an error occured
603     */
604     int16_t Chunk::ReadInt16() {
605     #if DEBUG
606     std::cout << "Chunk::ReadInt16()" << std::endl;
607     #endif // DEBUG
608     int16_t word;
609     ReadSceptical(&word,1,2);
610     return word;
611     }
612    
613     /**
614     * Reads one 16 Bit unsigned integer word and increments the position
615     * within the chunk. Endian correction will automatically be done if
616     * needed.
617     *
618     * @returns read integer word
619     * @throws RIFF::Exception if an error occured
620     */
621     uint16_t Chunk::ReadUint16() {
622     #if DEBUG
623     std::cout << "Chunk::ReadUint16()" << std::endl;
624     #endif // DEBUG
625     uint16_t word;
626     ReadSceptical(&word,1,2);
627     return word;
628     }
629    
630     /**
631     * Reads one 32 Bit signed integer word and increments the position
632     * within the chunk. Endian correction will automatically be done if
633     * needed.
634     *
635     * @returns read integer word
636     * @throws RIFF::Exception if an error occured
637     */
638     int32_t Chunk::ReadInt32() {
639     #if DEBUG
640     std::cout << "Chunk::ReadInt32()" << std::endl;
641     #endif // DEBUG
642     int32_t word;
643     ReadSceptical(&word,1,4);
644     return word;
645     }
646    
647     /**
648     * Reads one 32 Bit unsigned integer word and increments the position
649     * within the chunk. Endian correction will automatically be done if
650     * needed.
651     *
652     * @returns read integer word
653     * @throws RIFF::Exception if an error occured
654     */
655     uint32_t Chunk::ReadUint32() {
656     #if DEBUG
657     std::cout << "Chunk::ReadUint32()" << std::endl;
658     #endif // DEBUG
659     uint32_t word;
660     ReadSceptical(&word,1,4);
661     return word;
662     }
663    
664 schoenebeck 780 /** @brief Load chunk body into RAM.
665     *
666     * Loads the whole chunk body into memory. You can modify the data in
667     * RAM and save the data by calling File::Save() afterwards.
668     *
669     * <b>Caution:</b> the buffer pointer will be invalidated once
670     * File::Save() was called. You have to call LoadChunkData() again to
671     * get a new pointer whenever File::Save() was called.
672     *
673     * @returns a pointer to the data in RAM on success, NULL otherwise
674     * @see ReleaseChunkData()
675     */
676 schoenebeck 2 void* Chunk::LoadChunkData() {
677     if (!pChunkData) {
678     #if POSIX
679 schoenebeck 780 if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;
680 schoenebeck 2 pChunkData = new uint8_t[GetSize()];
681     if (!pChunkData) return NULL;
682 schoenebeck 780 unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize());
683 schoenebeck 2 #else
684 schoenebeck 780 if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL;
685 schoenebeck 2 pChunkData = new uint8_t[GetSize()];
686     if (!pChunkData) return NULL;
687 schoenebeck 780 unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead);
688 schoenebeck 2 #endif // POSIX
689     if (readWords != GetSize()) {
690     delete[] pChunkData;
691     return (pChunkData = NULL);
692     }
693     }
694     return pChunkData;
695     }
696    
697 schoenebeck 780 /** @brief Free loaded chunk body from RAM.
698     *
699     * Frees loaded chunk body data from memory (RAM). You should call
700     * File::Save() before calling this method if you modified the data to
701     * make the changes persistent.
702     */
703 schoenebeck 2 void Chunk::ReleaseChunkData() {
704     if (pChunkData) {
705     delete[] pChunkData;
706     pChunkData = NULL;
707     }
708     }
709    
710 schoenebeck 780 /** @brief Resize chunk.
711     *
712     * Resizes this chunk's body, that is the actual size of data possible
713     * to be written to this chunk. This call will return immediately and
714     * just schedule the resize operation. You should call File::Save() to
715     * actually perform the resize operation(s) "physically" to the file.
716     * As this can take a while on large files, it is recommended to call
717     * Resize() first on all chunks which have to be resized and finally to
718     * call File::Save() to perform all those resize operations in one rush.
719     *
720     * <b>Caution:</b> You cannot directly write to enlarged chunks before
721     * calling File::Save() as this might exceed the current chunk's body
722     * boundary.
723     *
724     * @param iNewSize - new chunk body size in bytes (must be greater than zero)
725     * @throws RIFF::Exception if \a iNewSize is less than 1
726     * @see File::Save()
727     */
728     void Chunk::Resize(int iNewSize) {
729     if (iNewSize <= 0) throw Exception("Chunk size must be at least one byte");
730     NewChunkSize = iNewSize;
731     pFile->LogAsResized(this);
732     }
733 schoenebeck 2
734 schoenebeck 780 /** @brief Write chunk persistently e.g. to disk.
735     *
736     * Stores the chunk persistently to its actual "physical" file.
737     *
738     * @param ulWritePos - position within the "physical" file where this
739     * chunk should be written to
740     * @param ulCurrentDataOffset - offset of current (old) data within
741     * the file
742     * @returns new write position in the "physical" file, that is
743     * \a ulWritePos incremented by this chunk's new size
744     * (including its header size of course)
745     */
746     unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) {
747 schoenebeck 798 const unsigned long ulOriginalPos = ulWritePos;
748 schoenebeck 780 ulWritePos += CHUNK_HEADER_SIZE;
749 schoenebeck 2
750 schoenebeck 798 if (pFile->Mode != stream_mode_read_write)
751     throw Exception("Cannot write list chunk, file has to be opened in read+write mode");
752    
753 schoenebeck 780 // if the whole chunk body was loaded into RAM
754     if (pChunkData) {
755     // in case the chunk size was changed, reallocate the data in RAM with the chunk's new size
756     if (NewChunkSize != CurrentChunkSize) {
757     uint8_t* pNewBuffer = new uint8_t[NewChunkSize];
758     if (NewChunkSize > CurrentChunkSize) {
759     memcpy(pNewBuffer, pChunkData, CurrentChunkSize);
760     memset(pNewBuffer + CurrentChunkSize, 0, NewChunkSize - CurrentChunkSize);
761     } else {
762     memcpy(pNewBuffer, pChunkData, NewChunkSize);
763     }
764     delete[] pChunkData;
765     pChunkData = pNewBuffer;
766     }
767    
768     // write chunk data from RAM persistently to the file
769     #if POSIX
770     lseek(pFile->hFileWrite, ulWritePos, SEEK_SET);
771     if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) {
772     throw Exception("Writing Chunk data (from RAM) failed");
773     }
774     #else
775     fseek(pFile->hFileWrite, ulWritePos, SEEK_SET);
776     if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) {
777     throw Exception("Writing Chunk data (from RAM) failed");
778     }
779     #endif // POSIX
780     } else {
781     // move chunk data from the end of the file to the appropriate position
782     int8_t* pCopyBuffer = new int8_t[4096];
783     unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize;
784     int iBytesMoved = 1;
785     for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) {
786     iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;
787     #if POSIX
788     lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);
789     iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved);
790     lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET);
791     iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved);
792     #else
793     fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);
794     iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead);
795     fseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET);
796     iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, pFile->hFileWrite);
797     #endif
798     }
799     delete[] pCopyBuffer;
800     if (iBytesMoved < 0) throw Exception("Writing Chunk data (from file) failed");
801     }
802    
803     // update this chunk's header
804     CurrentChunkSize = NewChunkSize;
805     WriteHeader(ulOriginalPos);
806    
807     // update chunk's position pointers
808     ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE;
809     ulPos = 0;
810    
811     // add pad byte if needed
812     if ((ulStartPos + NewChunkSize) % 2 != 0) {
813 schoenebeck 798 const char cPadByte = 0;
814 schoenebeck 780 #if POSIX
815 schoenebeck 798 lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);
816 schoenebeck 780 write(pFile->hFileWrite, &cPadByte, 1);
817     #else
818 schoenebeck 798 fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);
819 schoenebeck 780 fwrite(&cPadByte, 1, 1, pFile->hFileWrite);
820     #endif
821     return ulStartPos + NewChunkSize + 1;
822     }
823    
824     return ulStartPos + NewChunkSize;
825     }
826    
827     void Chunk::__resetPos() {
828     ulPos = 0;
829     }
830    
831    
832    
833 schoenebeck 2 // *************** List ***************
834     // *
835    
836 schoenebeck 780 List::List(File* pFile) : Chunk(pFile) {
837 schoenebeck 2 #if DEBUG
838 schoenebeck 780 std::cout << "List::List(File* pFile)" << std::endl;
839 schoenebeck 2 #endif // DEBUG
840     pSubChunks = NULL;
841     pSubChunksMap = NULL;
842     }
843    
844 schoenebeck 780 List::List(File* pFile, unsigned long StartPos, List* Parent)
845     : Chunk(pFile, StartPos, Parent) {
846 schoenebeck 2 #if DEBUG
847 schoenebeck 780 std::cout << "List::List(File*,ulong,bool,List*)" << std::endl;
848 schoenebeck 2 #endif // DEBUG
849     pSubChunks = NULL;
850     pSubChunksMap = NULL;
851     ReadHeader(StartPos);
852     ulStartPos = StartPos + LIST_HEADER_SIZE;
853     }
854    
855 schoenebeck 780 List::List(File* pFile, List* pParent, uint32_t uiListID)
856     : Chunk(pFile, pParent, CHUNK_ID_LIST, 0) {
857     pSubChunks = NULL;
858     pSubChunksMap = NULL;
859     ListType = uiListID;
860     }
861    
862 schoenebeck 2 List::~List() {
863     #if DEBUG
864     std::cout << "List::~List()" << std::endl;
865     #endif // DEBUG
866     if (pSubChunks) {
867     ChunkList::iterator iter = pSubChunks->begin();
868     ChunkList::iterator end = pSubChunks->end();
869     while (iter != end) {
870     delete *iter;
871     iter++;
872     }
873     delete pSubChunks;
874     }
875     if (pSubChunksMap) delete pSubChunksMap;
876     }
877    
878 schoenebeck 11 /**
879     * Returns subchunk with chunk ID <i>\a ChunkID</i> within this chunk
880     * list. Use this method if you expect only one subchunk of that type in
881     * the list. It there are more than one, it's undetermined which one of
882     * them will be returned! If there are no subchunks with that desired
883     * chunk ID, NULL will be returned.
884     *
885     * @param ChunkID - chunk ID of the sought subchunk
886     * @returns pointer to the subchunk or NULL if there is none of
887     * that ID
888     */
889 schoenebeck 2 Chunk* List::GetSubChunk(uint32_t ChunkID) {
890     #if DEBUG
891     std::cout << "List::GetSubChunk(uint32_t)" << std::endl;
892     #endif // DEBUG
893     if (!pSubChunksMap) LoadSubChunks();
894     return (*pSubChunksMap)[ChunkID];
895     }
896    
897 schoenebeck 11 /**
898     * Returns sublist chunk with list type <i>\a ListType</i> within this
899     * chunk list. Use this method if you expect only one sublist chunk of
900     * that type in the list. It there are more than one, it's undetermined
901     * which one of them will be returned! If there are no sublists with
902     * that desired list type, NULL will be returned.
903     *
904     * @param ListType - list type of the sought sublist
905     * @returns pointer to the sublist or NULL if there is none of
906     * that type
907     */
908 schoenebeck 2 List* List::GetSubList(uint32_t ListType) {
909     #if DEBUG
910     std::cout << "List::GetSubList(uint32_t)" << std::endl;
911     #endif // DEBUG
912     if (!pSubChunks) LoadSubChunks();
913     ChunkList::iterator iter = pSubChunks->begin();
914     ChunkList::iterator end = pSubChunks->end();
915     while (iter != end) {
916     if ((*iter)->GetChunkID() == CHUNK_ID_LIST) {
917     List* l = (List*) *iter;
918     if (l->GetListType() == ListType) return l;
919     }
920     iter++;
921     }
922     return NULL;
923     }
924    
925 schoenebeck 11 /**
926     * Returns the first subchunk within the list. You have to call this
927     * method before you can call GetNextSubChunk(). Recall it when you want
928     * to start from the beginning of the list again.
929     *
930     * @returns pointer to the first subchunk within the list, NULL
931     * otherwise
932     */
933 schoenebeck 2 Chunk* List::GetFirstSubChunk() {
934     #if DEBUG
935     std::cout << "List::GetFirstSubChunk()" << std::endl;
936     #endif // DEBUG
937     if (!pSubChunks) LoadSubChunks();
938     ChunksIterator = pSubChunks->begin();
939     return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
940     }
941    
942 schoenebeck 11 /**
943     * Returns the next subchunk within the list. You have to call
944     * GetFirstSubChunk() before you can use this method!
945     *
946     * @returns pointer to the next subchunk within the list or NULL if
947     * end of list is reached
948     */
949 schoenebeck 2 Chunk* List::GetNextSubChunk() {
950     #if DEBUG
951     std::cout << "List::GetNextSubChunk()" << std::endl;
952     #endif // DEBUG
953     if (!pSubChunks) return NULL;
954     ChunksIterator++;
955     return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
956     }
957    
958 schoenebeck 11 /**
959     * Returns the first sublist within the list (that is a subchunk with
960     * chunk ID "LIST"). You have to call this method before you can call
961     * GetNextSubList(). Recall it when you want to start from the beginning
962     * of the list again.
963     *
964     * @returns pointer to the first sublist within the list, NULL
965     * otherwise
966     */
967 schoenebeck 2 List* List::GetFirstSubList() {
968     #if DEBUG
969     std::cout << "List::GetFirstSubList()" << std::endl;
970     #endif // DEBUG
971     if (!pSubChunks) LoadSubChunks();
972     ListIterator = pSubChunks->begin();
973     ChunkList::iterator end = pSubChunks->end();
974     while (ListIterator != end) {
975     if ((*ListIterator)->GetChunkID() == CHUNK_ID_LIST) return (List*) *ListIterator;
976     ListIterator++;
977     }
978     return NULL;
979     }
980    
981 schoenebeck 11 /**
982     * Returns the next sublist (that is a subchunk with chunk ID "LIST")
983     * within the list. You have to call GetFirstSubList() before you can
984     * use this method!
985     *
986     * @returns pointer to the next sublist within the list, NULL if
987     * end of list is reached
988     */
989 schoenebeck 2 List* List::GetNextSubList() {
990     #if DEBUG
991     std::cout << "List::GetNextSubList()" << std::endl;
992     #endif // DEBUG
993     if (!pSubChunks) return NULL;
994     if (ListIterator == pSubChunks->end()) return NULL;
995     ListIterator++;
996     ChunkList::iterator end = pSubChunks->end();
997     while (ListIterator != end) {
998     if ((*ListIterator)->GetChunkID() == CHUNK_ID_LIST) return (List*) *ListIterator;
999     ListIterator++;
1000     }
1001     return NULL;
1002     }
1003    
1004 schoenebeck 11 /**
1005 schoenebeck 515 * Returns number of subchunks within the list.
1006 schoenebeck 11 */
1007 schoenebeck 2 unsigned int List::CountSubChunks() {
1008     if (!pSubChunks) LoadSubChunks();
1009     return pSubChunks->size();
1010     }
1011    
1012 schoenebeck 11 /**
1013     * Returns number of subchunks within the list with chunk ID
1014     * <i>\a ChunkId</i>.
1015     */
1016 schoenebeck 2 unsigned int List::CountSubChunks(uint32_t ChunkID) {
1017     unsigned int result = 0;
1018     if (!pSubChunks) LoadSubChunks();
1019     ChunkList::iterator iter = pSubChunks->begin();
1020     ChunkList::iterator end = pSubChunks->end();
1021     while (iter != end) {
1022     if ((*iter)->GetChunkID() == ChunkID) {
1023     result++;
1024     }
1025     iter++;
1026     }
1027     return result;
1028     }
1029    
1030 schoenebeck 11 /**
1031     * Returns number of sublists within the list.
1032     */
1033 schoenebeck 2 unsigned int List::CountSubLists() {
1034     return CountSubChunks(CHUNK_ID_LIST);
1035     }
1036    
1037 schoenebeck 11 /**
1038     * Returns number of sublists within the list with list type
1039     * <i>\a ListType</i>
1040     */
1041 schoenebeck 2 unsigned int List::CountSubLists(uint32_t ListType) {
1042     unsigned int result = 0;
1043     if (!pSubChunks) LoadSubChunks();
1044     ChunkList::iterator iter = pSubChunks->begin();
1045     ChunkList::iterator end = pSubChunks->end();
1046     while (iter != end) {
1047     if ((*iter)->GetChunkID() == CHUNK_ID_LIST) {
1048     List* l = (List*) *iter;
1049     if (l->GetListType() == ListType) result++;
1050     }
1051     iter++;
1052     }
1053     return result;
1054     }
1055    
1056 schoenebeck 780 /** @brief Creates a new sub chunk.
1057     *
1058     * Creates and adds a new sub chunk to this list chunk. Note that the
1059     * chunk's body size given by \a uiBodySize must be greater than zero.
1060     * You have to call File::Save() to make this change persistent to the
1061     * actual file and <b>before</b> performing any data write operations
1062     * on the new chunk!
1063     *
1064     * @param uiChunkID - chunk ID of the new chunk
1065     * @param uiBodySize - size of the new chunk's body, that is its actual
1066     * data size (without header)
1067     * @throws RIFF::Exception if \a uiBodySize equals zero
1068     */
1069     Chunk* List::AddSubChunk(uint32_t uiChunkID, uint uiBodySize) {
1070     if (uiBodySize == 0) throw Exception("Chunk body size must be at least 1 byte");
1071     if (!pSubChunks) LoadSubChunks();
1072 schoenebeck 798 Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0);
1073 schoenebeck 780 pSubChunks->push_back(pNewChunk);
1074     (*pSubChunksMap)[uiChunkID] = pNewChunk;
1075 schoenebeck 798 pNewChunk->Resize(uiBodySize);
1076 schoenebeck 780 return pNewChunk;
1077     }
1078    
1079     /** @brief Creates a new list sub chunk.
1080     *
1081     * Creates and adds a new list sub chunk to this list chunk. Note that
1082     * you have to add sub chunks / sub list chunks to the new created chunk
1083     * <b>before</b> trying to make this change persisten to the actual
1084     * file with File::Save()!
1085     *
1086     * @param uiListType - list ID of the new list chunk
1087     */
1088     List* List::AddSubList(uint32_t uiListType) {
1089     if (!pSubChunks) LoadSubChunks();
1090     List* pNewListChunk = new List(pFile, this, uiListType);
1091     pSubChunks->push_back(pNewListChunk);
1092     (*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk;
1093     return pNewListChunk;
1094     }
1095    
1096     /** @brief Removes a sub chunk.
1097     *
1098     * Removes the sub chunk given by \a pSubChunk from this list and frees
1099     * it completely from RAM. The given chunk can either be a normal sub
1100     * chunk or a list sub chunk. You should call File::Save() to make this
1101     * change persistent at any time.
1102     *
1103     * @param pSubChunk - sub chunk or sub list chunk to be removed
1104     */
1105     void List::DeleteSubChunk(Chunk* pSubChunk) {
1106     if (!pSubChunks) LoadSubChunks();
1107     pSubChunks->remove(pSubChunk);
1108     if ((*pSubChunksMap)[pSubChunk->GetChunkID()] == pSubChunk) {
1109     pSubChunksMap->erase(pSubChunk->GetChunkID());
1110     // try to find another chunk of the same chunk ID
1111     ChunkList::iterator iter = pSubChunks->begin();
1112     ChunkList::iterator end = pSubChunks->end();
1113     for (; iter != end; ++iter) {
1114     if ((*iter)->GetChunkID() == pSubChunk->GetChunkID()) {
1115     (*pSubChunksMap)[pSubChunk->GetChunkID()] = *iter;
1116     break; // we're done, stop search
1117     }
1118     }
1119     }
1120     delete pSubChunk;
1121     }
1122    
1123 schoenebeck 2 void List::ReadHeader(unsigned long fPos) {
1124     #if DEBUG
1125     std::cout << "List::Readheader(ulong) ";
1126     #endif // DEBUG
1127     Chunk::ReadHeader(fPos);
1128 schoenebeck 780 NewChunkSize = CurrentChunkSize -= 4;
1129 schoenebeck 2 #if POSIX
1130 schoenebeck 780 lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1131     read(pFile->hFileRead, &ListType, 4);
1132 schoenebeck 2 #else
1133 schoenebeck 780 fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1134     fread(&ListType, 4, 1, pFile->hFileRead);
1135 schoenebeck 2 #endif // POSIX
1136     #if DEBUG
1137     std::cout << "listType=" << convertToString(ListType) << std::endl;
1138     #endif // DEBUG
1139 schoenebeck 780 if (!pFile->bEndianNative) {
1140 schoenebeck 11 //swapBytes_32(&ListType);
1141 schoenebeck 2 }
1142     }
1143    
1144 schoenebeck 780 void List::WriteHeader(unsigned long fPos) {
1145     // the four list type bytes officially belong the chunk's body in the RIFF format
1146     NewChunkSize += 4;
1147     Chunk::WriteHeader(fPos);
1148     NewChunkSize -= 4; // just revert the +4 incrementation
1149     #if POSIX
1150     lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1151     write(pFile->hFileWrite, &ListType, 4);
1152     #else
1153     fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1154     fwrite(&ListType, 4, 1, pFile->hFileWrite);
1155     #endif // POSIX
1156     }
1157    
1158 schoenebeck 2 void List::LoadSubChunks() {
1159     #if DEBUG
1160     std::cout << "List::LoadSubChunks()";
1161     #endif // DEBUG
1162     if (!pSubChunks) {
1163     pSubChunks = new ChunkList();
1164     pSubChunksMap = new ChunkMap();
1165 schoenebeck 780 if (!pFile->hFileRead) return;
1166 schoenebeck 515 unsigned long uiOriginalPos = GetPos();
1167     SetPos(0); // jump to beginning of list chunk body
1168 schoenebeck 2 while (RemainingBytes() >= CHUNK_HEADER_SIZE) {
1169     Chunk* ck;
1170 schoenebeck 11 uint32_t ckid;
1171     Read(&ckid, 4, 1);
1172 schoenebeck 2 #if DEBUG
1173     std::cout << " ckid=" << convertToString(ckid) << std::endl;
1174     #endif // DEBUG
1175     if (ckid == CHUNK_ID_LIST) {
1176 schoenebeck 780 ck = new RIFF::List(pFile, ulStartPos + ulPos - 4, this);
1177 schoenebeck 2 SetPos(ck->GetSize() + LIST_HEADER_SIZE - 4, RIFF::stream_curpos);
1178     }
1179     else { // simple chunk
1180 schoenebeck 780 ck = new RIFF::Chunk(pFile, ulStartPos + ulPos - 4, this);
1181 schoenebeck 2 SetPos(ck->GetSize() + CHUNK_HEADER_SIZE - 4, RIFF::stream_curpos);
1182     }
1183     pSubChunks->push_back(ck);
1184     (*pSubChunksMap)[ckid] = ck;
1185     if (GetPos() % 2 != 0) SetPos(1, RIFF::stream_curpos); // jump over pad byte
1186     }
1187 schoenebeck 515 SetPos(uiOriginalPos); // restore position before this call
1188 schoenebeck 2 }
1189     }
1190    
1191 schoenebeck 780 /** @brief Write list chunk persistently e.g. to disk.
1192     *
1193     * Stores the list chunk persistently to its actual "physical" file. All
1194     * subchunks (including sub list chunks) will be stored recursively as
1195     * well.
1196     *
1197     * @param ulWritePos - position within the "physical" file where this
1198     * list chunk should be written to
1199     * @param ulCurrentDataOffset - offset of current (old) data within
1200     * the file
1201     * @returns new write position in the "physical" file, that is
1202     * \a ulWritePos incremented by this list chunk's new size
1203     * (including its header size of course)
1204     */
1205     unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) {
1206 schoenebeck 798 const unsigned long ulOriginalPos = ulWritePos;
1207 schoenebeck 780 ulWritePos += LIST_HEADER_SIZE;
1208    
1209 schoenebeck 798 if (pFile->Mode != stream_mode_read_write)
1210     throw Exception("Cannot write list chunk, file has to be opened in read+write mode");
1211    
1212 schoenebeck 780 // write all subchunks (including sub list chunks) recursively
1213     if (pSubChunks) {
1214     for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) {
1215     ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset);
1216     }
1217     }
1218    
1219     // update this list chunk's header
1220     CurrentChunkSize = NewChunkSize = ulWritePos - ulOriginalPos - LIST_HEADER_SIZE;
1221     WriteHeader(ulOriginalPos);
1222    
1223 schoenebeck 798 // offset of this list chunk in new written file may have changed
1224     ulStartPos = ulOriginalPos + LIST_HEADER_SIZE;
1225    
1226 schoenebeck 780 return ulWritePos;
1227     }
1228    
1229     void List::__resetPos() {
1230     Chunk::__resetPos();
1231     if (pSubChunks) {
1232     for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) {
1233     (*iter)->__resetPos();
1234     }
1235     }
1236     }
1237    
1238 schoenebeck 11 /**
1239     * Returns string representation of the lists's id
1240     */
1241 schoenebeck 2 String List::GetListTypeString() {
1242     return convertToString(ListType);
1243     }
1244    
1245    
1246    
1247     // *************** File ***************
1248     // *
1249    
1250 schoenebeck 780 /** @brief Create new RIFF file.
1251     *
1252     * Use this constructor if you want to create a new RIFF file completely
1253     * "from scratch". Note: there must be no empty chunks or empty list
1254     * chunks when trying to make the new RIFF file persistent with Save()!
1255     *
1256 schoenebeck 798 * @param FileType - four-byte identifier of the RIFF file type
1257 schoenebeck 780 * @see AddSubChunk(), AddSubList()
1258     */
1259 schoenebeck 798 File::File(uint32_t FileType) : List(this) {
1260 schoenebeck 780 hFileRead = hFileWrite = 0;
1261 schoenebeck 798 Mode = stream_mode_closed;
1262 schoenebeck 780 bEndianNative = true;
1263     ulStartPos = RIFF_HEADER_SIZE;
1264 schoenebeck 798 ListType = FileType;
1265 schoenebeck 780 }
1266    
1267     /** @brief Load existing RIFF file.
1268     *
1269     * Loads an existing RIFF file with all its chunks.
1270     *
1271     * @param path - path and file name of the RIFF file to open
1272     * @throws RIFF::Exception if error occured while trying to load the
1273     * given RIFF file
1274     */
1275     File::File(const String& path) : List(this), Filename(path) {
1276 schoenebeck 2 #if DEBUG
1277     std::cout << "File::File("<<path<<")" << std::endl;
1278     #endif // DEBUG
1279     bEndianNative = true;
1280     #if POSIX
1281 schoenebeck 780 hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK);
1282     if (hFileRead <= 0) {
1283     hFileRead = hFileWrite = 0;
1284 schoenebeck 2 throw RIFF::Exception("Can't open \"" + path + "\"");
1285     }
1286     #else
1287 schoenebeck 780 hFileRead = hFileWrite = fopen(path.c_str(), "rb");
1288 schoenebeck 2 if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\"");
1289     #endif // POSIX
1290 schoenebeck 798 Mode = stream_mode_read;
1291 schoenebeck 2 ulStartPos = RIFF_HEADER_SIZE;
1292     ReadHeader(0);
1293     if (ChunkID != CHUNK_ID_RIFF) {
1294     throw RIFF::Exception("Not a RIFF file");
1295     }
1296     }
1297    
1298 schoenebeck 780 String File::GetFileName() {
1299     return Filename;
1300     }
1301    
1302     stream_mode_t File::GetMode() {
1303     return Mode;
1304     }
1305    
1306     /** @brief Change file access mode.
1307     *
1308     * Changes files access mode either to read-only mode or to read/write
1309     * mode.
1310     *
1311     * @param NewMode - new file access mode
1312     * @returns true if mode was changed, false if current mode already
1313     * equals new mode
1314     * @throws RIFF::Exception if new file access mode is unknown
1315     */
1316     bool File::SetMode(stream_mode_t NewMode) {
1317     if (NewMode != Mode) {
1318     switch (NewMode) {
1319     case stream_mode_read:
1320     #if POSIX
1321     if (hFileRead) close(hFileRead);
1322     hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);
1323     if (hFileRead < 0) {
1324     hFileRead = hFileWrite = 0;
1325     throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
1326     }
1327     #else
1328     if (hFileRead) fclose(hFileRead);
1329     hFileRead = hFileWrite = fopen(path.c_str(), "rb");
1330     if (!hFileRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
1331     #endif
1332     __resetPos(); // reset read/write position of ALL 'Chunk' objects
1333 schoenebeck 798 break;
1334 schoenebeck 780 case stream_mode_read_write:
1335     #if POSIX
1336     if (hFileRead) close(hFileRead);
1337     hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK);
1338     if (hFileRead < 0) {
1339     hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);
1340     throw Exception("Could not open file \"" + Filename + "\" in read+write mode");
1341     }
1342     #else
1343     if (hFileRead) fclose(hFileRead);
1344     hFileRead = hFileWrite = fopen(path.c_str(), "r+b");
1345     if (!hFileRead) {
1346     hFileRead = hFileWrite = fopen(path.c_str(), "rb");
1347     throw Exception("Could not open file \"" + Filename + "\" in read+write mode");
1348     }
1349     #endif
1350     __resetPos(); // reset read/write position of ALL 'Chunk' objects
1351 schoenebeck 798 break;
1352     case stream_mode_closed:
1353     #if POSIX
1354     if (hFileRead) close(hFileRead);
1355     if (hFileWrite) close(hFileWrite);
1356     #else
1357     if (hFileRead) fclose(hFileRead);
1358     if (hFileWrite) fclose(hFileWrite);
1359     #endif
1360     hFileRead = hFileWrite = 0;
1361     break;
1362 schoenebeck 780 default:
1363     throw Exception("Unknown file access mode");
1364     }
1365 schoenebeck 798 Mode = NewMode;
1366     return true;
1367 schoenebeck 780 }
1368     return false;
1369     }
1370    
1371     /** @brief Save changes to same file.
1372     *
1373     * Make all changes of all chunks persistent by writing them to the
1374     * actual (same) file. The file might temporarily grow to a higher size
1375     * than it will have at the end of the saving process, in case chunks
1376     * were grown.
1377     *
1378     * @throws RIFF::Exception if there is an empty chunk or empty list
1379     * chunk or any kind of IO error occured
1380     */
1381     void File::Save() {
1382     // reopen file in write mode
1383 schoenebeck 798 SetMode(stream_mode_read_write);
1384 schoenebeck 780
1385     // to be able to save the whole file without loading everything into
1386     // RAM and without having to store the data in a temporary file, we
1387     // enlarge the file with the sum of all _positive_ chunk size
1388     // changes, move current data towards the end of the file with the
1389     // calculated sum and finally update / rewrite the file by copying
1390     // the old data back to the right position at the beginning of the file
1391    
1392     // first we sum up all positive chunk size changes (and skip all negative ones)
1393     unsigned long ulPositiveSizeDiff = 0;
1394     for (ChunkList::iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) {
1395     if ((*iter)->GetNewSize() == 0) throw Exception("There is at least one empty chunk (zero size)");
1396 schoenebeck 798 if ((*iter)->GetNewSize() + 1L > (*iter)->GetSize()) {
1397     unsigned long ulDiff = (*iter)->GetNewSize() - (*iter)->GetSize() + 1L; // +1 in case we have to add a pad byte
1398     ulPositiveSizeDiff += ulDiff;
1399     }
1400 schoenebeck 780 }
1401    
1402     unsigned long ulWorkingFileSize = GetFileSize();
1403    
1404     // if there are positive size changes...
1405     if (ulPositiveSizeDiff > 0) {
1406     // ... we enlarge this file first ...
1407     ulWorkingFileSize += ulPositiveSizeDiff;
1408     ResizeFile(ulWorkingFileSize);
1409     // ... and move current data by the same amount towards end of file.
1410     int8_t* pCopyBuffer = new int8_t[4096];
1411     const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE;
1412     int iBytesMoved = 1;
1413     for (unsigned long ulPos = 0; iBytesMoved > 0; ulPos += iBytesMoved) {
1414     const unsigned long ulToMove = ulFileSize - ulPos;
1415     iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;
1416     #if POSIX
1417     lseek(hFileRead, ulPos, SEEK_SET);
1418     iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);
1419     lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET);
1420     iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved);
1421     #else
1422     fseek(hFileRead, ulPos, SEEK_SET);
1423     iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead);
1424     fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET);
1425     iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite);
1426     #endif
1427     }
1428     delete[] pCopyBuffer;
1429     if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it");
1430     }
1431    
1432     // rebuild / rewrite complete RIFF tree
1433 schoenebeck 798 unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff);
1434     unsigned long ulActualSize = __GetFileSize(hFileWrite);
1435 schoenebeck 780
1436     // resize file to the final size
1437 schoenebeck 798 if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);
1438 schoenebeck 780
1439     // forget all resized chunks
1440     ResizedChunks.clear();
1441     }
1442    
1443     /** @brief Save changes to another file.
1444     *
1445     * Make all changes of all chunks persistent by writing them to another
1446     * file. <b>Caution:</b> this method is optimized for writing to
1447     * <b>another</b> file, do not use it to save the changes to the same
1448 schoenebeck 798 * file! Use File::Save() in that case instead! Ignoring this might
1449     * result in a corrupted file, especially in case chunks were resized!
1450 schoenebeck 780 *
1451     * After calling this method, this File object will be associated with
1452     * the new file (given by \a path) afterwards.
1453     *
1454     * @param path - path and file name where everything should be written to
1455     */
1456     void File::Save(const String& path) {
1457 schoenebeck 798 //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
1458    
1459     if (Filename.length() > 0) SetMode(stream_mode_read);
1460 schoenebeck 780 // open the other (new) file for writing and truncate it to zero size
1461     #if POSIX
1462 schoenebeck 798 hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
1463 schoenebeck 780 if (hFileWrite < 0) {
1464     hFileWrite = hFileRead;
1465     throw Exception("Could not open file \"" + path + "\" for writing");
1466     }
1467     #else
1468     hFileWrite = fopen(path.c_str(), "w+b");
1469     if (!hFileWrite) {
1470     hFileWrite = hFileRead;
1471     throw Exception("Could not open file \"" + path + "\" for writing");
1472     }
1473     #endif // POSIX
1474 schoenebeck 798 Mode = stream_mode_read_write;
1475 schoenebeck 780
1476     // write complete RIFF tree to the other (new) file
1477 schoenebeck 798 unsigned long ulTotalSize = WriteChunk(0, 0);
1478     unsigned long ulActualSize = __GetFileSize(hFileWrite);
1479 schoenebeck 780
1480 schoenebeck 798 // resize file to the final size (if the file was originally larger)
1481     if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);
1482    
1483 schoenebeck 780 // forget all resized chunks
1484     ResizedChunks.clear();
1485    
1486 schoenebeck 798 if (Filename.length() > 0) {
1487     #if POSIX
1488     close(hFileWrite);
1489     #else
1490     fclose(hFileWrite);
1491     #endif
1492     hFileWrite = hFileRead;
1493     }
1494    
1495 schoenebeck 780 // associate new file with this File object from now on
1496     Filename = path;
1497 schoenebeck 798 Mode = (stream_mode_t) -1; // Just set it to an undefined mode ...
1498     SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles.
1499 schoenebeck 780 }
1500    
1501     void File::ResizeFile(unsigned long ulNewSize) {
1502     #if POSIX
1503     if (ftruncate(hFileWrite, ulNewSize) < 0)
1504     throw Exception("Could not resize file \"" + Filename + "\"");
1505     #else
1506     # error Sorry, this version of libgig only supports POSIX systems yet.
1507 schoenebeck 798 # error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)!
1508 schoenebeck 780 #endif
1509     }
1510    
1511 schoenebeck 2 File::~File() {
1512     #if DEBUG
1513     std::cout << "File::~File()" << std::endl;
1514     #endif // DEBUG
1515     #if POSIX
1516 schoenebeck 780 if (hFileRead) close(hFileRead);
1517 schoenebeck 2 #else
1518 schoenebeck 780 if (hFileRead) fclose(hFileRead);
1519 schoenebeck 2 #endif // POSIX
1520     }
1521    
1522 schoenebeck 780 void File::LogAsResized(Chunk* pResizedChunk) {
1523     ResizedChunks.push_back(pResizedChunk);
1524     }
1525    
1526 schoenebeck 2 unsigned long File::GetFileSize() {
1527 schoenebeck 798 return __GetFileSize(hFileRead);
1528     }
1529    
1530     #if POSIX
1531     unsigned long File::__GetFileSize(int hFile) {
1532 schoenebeck 2 struct stat filestat;
1533 schoenebeck 798 fstat(hFile, &filestat);
1534 schoenebeck 2 long size = filestat.st_size;
1535     return size;
1536     }
1537 schoenebeck 798 #else // standard C functions
1538     unsigned long File::__GetFileSize(FILE* hFile) {
1539     long curpos = ftell(hFile);
1540     fseek(hFile, 0, SEEK_END);
1541     long size = ftell(hFile);
1542     fseek(hFile, curpos, SEEK_SET);
1543     return size;
1544     }
1545     #endif
1546 schoenebeck 2
1547    
1548     // *************** Exception ***************
1549     // *
1550    
1551     void Exception::PrintMessage() {
1552     std::cout << "RIFF::Exception: " << Message << std::endl;
1553     }
1554    
1555 schoenebeck 518
1556     // *************** functions ***************
1557     // *
1558    
1559     /**
1560     * Returns the name of this C++ library. This is usually "libgig" of
1561     * course. This call is equivalent to DLS::libraryName() and
1562     * gig::libraryName().
1563     */
1564     String libraryName() {
1565     return PACKAGE;
1566     }
1567    
1568     /**
1569     * Returns version of this C++ library. This call is equivalent to
1570     * DLS::libraryVersion() and gig::libraryVersion().
1571     */
1572     String libraryVersion() {
1573     return VERSION;
1574     }
1575    
1576 schoenebeck 2 } // namespace RIFF

  ViewVC Help
Powered by ViewVC