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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2682 - (hide annotations) (download)
Mon Dec 29 16:25:51 2014 UTC (9 years, 3 months ago) by schoenebeck
File size: 86781 byte(s)
* gig: Added support for custom progress notification while saving to
  gig file.
* DLS: Added support for custom progress notification while saving to
  DLS file.
* RIFF: Added support for custom progress notification while saving to
  RIFF file.
* Bumped version (3.3.0.svn22).

1 schoenebeck 2 /***************************************************************************
2     * *
3 schoenebeck 933 * libgig - C++ cross-platform Gigasampler format file access library *
4 schoenebeck 2 * *
5 schoenebeck 2543 * Copyright (C) 2003-2014 by Christian Schoenebeck *
6 schoenebeck 384 * <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 persson 1713 #include <algorithm>
25 schoenebeck 1953 #include <set>
26 schoenebeck 780 #include <string.h>
27    
28 schoenebeck 2 #include "RIFF.h"
29    
30 schoenebeck 800 #include "helper.h"
31    
32 schoenebeck 2675 #if POSIX
33     # include <errno.h>
34     #endif
35    
36 schoenebeck 2 namespace RIFF {
37    
38 schoenebeck 1105 // *************** Internal functions **************
39     // *
40    
41     /// Returns a human readable path of the given chunk.
42 schoenebeck 1383 static String __resolveChunkPath(Chunk* pCk) {
43 schoenebeck 1105 String sPath;
44     for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) {
45     if (pChunk->GetChunkID() == CHUNK_ID_LIST) {
46     List* pList = (List*) pChunk;
47     sPath = "->'" + pList->GetListTypeString() + "'" + sPath;
48     } else {
49     sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath;
50     }
51     }
52     return sPath;
53     }
54    
55    
56    
57 schoenebeck 2682 // *************** progress_t ***************
58     // *
59    
60     progress_t::progress_t() {
61     callback = NULL;
62     custom = NULL;
63     __range_min = 0.0f;
64     __range_max = 1.0f;
65     }
66    
67    
68    
69 schoenebeck 2 // *************** Chunk **************
70     // *
71    
72 schoenebeck 780 Chunk::Chunk(File* pFile) {
73 schoenebeck 2 #if DEBUG
74 schoenebeck 780 std::cout << "Chunk::Chunk(File* pFile)" << std::endl;
75 schoenebeck 2 #endif // DEBUG
76     ulPos = 0;
77     pParent = NULL;
78     pChunkData = NULL;
79 schoenebeck 1863 CurrentChunkSize = 0;
80     NewChunkSize = 0;
81 schoenebeck 800 ulChunkDataSize = 0;
82 schoenebeck 798 ChunkID = CHUNK_ID_RIFF;
83 schoenebeck 780 this->pFile = pFile;
84 schoenebeck 2 }
85    
86 schoenebeck 780 Chunk::Chunk(File* pFile, unsigned long StartPos, List* Parent) {
87 schoenebeck 2 #if DEBUG
88 schoenebeck 780 std::cout << "Chunk::Chunk(File*,ulong,bool,List*),StartPos=" << StartPos << std::endl;
89 schoenebeck 2 #endif // DEBUG
90 schoenebeck 780 this->pFile = pFile;
91 schoenebeck 2 ulStartPos = StartPos + CHUNK_HEADER_SIZE;
92     pParent = Parent;
93     ulPos = 0;
94     pChunkData = NULL;
95 schoenebeck 1863 CurrentChunkSize = 0;
96     NewChunkSize = 0;
97 schoenebeck 800 ulChunkDataSize = 0;
98 schoenebeck 2 ReadHeader(StartPos);
99     }
100    
101 schoenebeck 780 Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize) {
102     this->pFile = pFile;
103     ulStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk
104     this->pParent = pParent;
105     ulPos = 0;
106     pChunkData = NULL;
107 schoenebeck 1863 ChunkID = uiChunkID;
108 schoenebeck 800 ulChunkDataSize = 0;
109 schoenebeck 780 CurrentChunkSize = 0;
110     NewChunkSize = uiBodySize;
111     }
112    
113 schoenebeck 2 Chunk::~Chunk() {
114 persson 1869 if (pFile) pFile->UnlogResized(this);
115 schoenebeck 2 if (pChunkData) delete[] pChunkData;
116     }
117    
118     void Chunk::ReadHeader(unsigned long fPos) {
119     #if DEBUG
120     std::cout << "Chunk::Readheader(" << fPos << ") ";
121     #endif // DEBUG
122 schoenebeck 1863 ChunkID = 0;
123     NewChunkSize = CurrentChunkSize = 0;
124 schoenebeck 2 #if POSIX
125 schoenebeck 780 if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) {
126     read(pFile->hFileRead, &ChunkID, 4);
127     read(pFile->hFileRead, &CurrentChunkSize, 4);
128 schoenebeck 1050 #elif defined(WIN32)
129     if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) {
130     DWORD dwBytesRead;
131     ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL);
132     ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL);
133 schoenebeck 2 #else
134 schoenebeck 780 if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) {
135     fread(&ChunkID, 4, 1, pFile->hFileRead);
136     fread(&CurrentChunkSize, 4, 1, pFile->hFileRead);
137 schoenebeck 2 #endif // POSIX
138     #if WORDS_BIGENDIAN
139     if (ChunkID == CHUNK_ID_RIFF) {
140 schoenebeck 808 pFile->bEndianNative = false;
141 schoenebeck 2 }
142     #else // little endian
143     if (ChunkID == CHUNK_ID_RIFX) {
144 schoenebeck 780 pFile->bEndianNative = false;
145 schoenebeck 2 ChunkID = CHUNK_ID_RIFF;
146     }
147     #endif // WORDS_BIGENDIAN
148 schoenebeck 780 if (!pFile->bEndianNative) {
149 schoenebeck 11 //swapBytes_32(&ChunkID);
150 schoenebeck 780 swapBytes_32(&CurrentChunkSize);
151 schoenebeck 2 }
152 schoenebeck 11 #if DEBUG
153     std::cout << "ckID=" << convertToString(ChunkID) << " ";
154 schoenebeck 1863 std::cout << "ckSize=" << CurrentChunkSize << " ";
155     std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl;
156 schoenebeck 11 #endif // DEBUG
157 schoenebeck 780 NewChunkSize = CurrentChunkSize;
158 schoenebeck 2 }
159     }
160    
161 schoenebeck 780 void Chunk::WriteHeader(unsigned long fPos) {
162     uint32_t uiNewChunkID = ChunkID;
163     if (ChunkID == CHUNK_ID_RIFF) {
164     #if WORDS_BIGENDIAN
165     if (pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX;
166     #else // little endian
167     if (!pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX;
168     #endif // WORDS_BIGENDIAN
169     }
170    
171     uint32_t uiNewChunkSize = NewChunkSize;
172     if (!pFile->bEndianNative) {
173     swapBytes_32(&uiNewChunkSize);
174     }
175    
176     #if POSIX
177     if (lseek(pFile->hFileWrite, fPos, SEEK_SET) != -1) {
178     write(pFile->hFileWrite, &uiNewChunkID, 4);
179     write(pFile->hFileWrite, &uiNewChunkSize, 4);
180     }
181 schoenebeck 1050 #elif defined(WIN32)
182     if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) {
183     DWORD dwBytesWritten;
184     WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL);
185     WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL);
186     }
187 schoenebeck 780 #else
188     if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) {
189     fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite);
190     fwrite(&uiNewChunkSize, 4, 1, pFile->hFileWrite);
191     }
192     #endif // POSIX
193     }
194    
195 schoenebeck 11 /**
196     * Returns the String representation of the chunk's ID (e.g. "RIFF",
197     * "LIST").
198     */
199 schoenebeck 2 String Chunk::GetChunkIDString() {
200     return convertToString(ChunkID);
201     }
202    
203 schoenebeck 11 /**
204     * Sets the position within the chunk body, thus within the data portion
205     * of the chunk (in bytes).
206     *
207 schoenebeck 780 * <b>Caution:</b> the position will be reset to zero whenever
208     * File::Save() was called.
209     *
210 schoenebeck 11 * @param Where - position offset (in bytes)
211     * @param Whence - optional: defines to what <i>\a Where</i> relates to,
212     * if omitted \a Where relates to beginning of the chunk
213     * data
214     */
215 schoenebeck 2 unsigned long Chunk::SetPos(unsigned long Where, stream_whence_t Whence) {
216     #if DEBUG
217     std::cout << "Chunk::SetPos(ulong)" << std::endl;
218     #endif // DEBUG
219     switch (Whence) {
220     case stream_curpos:
221     ulPos += Where;
222     break;
223     case stream_end:
224 schoenebeck 780 ulPos = CurrentChunkSize - 1 - Where;
225 schoenebeck 2 break;
226     case stream_backward:
227     ulPos -= Where;
228     break;
229     case stream_start: default:
230     ulPos = Where;
231     break;
232     }
233 schoenebeck 780 if (ulPos > CurrentChunkSize) ulPos = CurrentChunkSize;
234 schoenebeck 2 return ulPos;
235     }
236    
237 schoenebeck 11 /**
238     * Returns the number of bytes left to read in the chunk body.
239     * When reading data from the chunk using the Read*() Methods, the
240     * position within the chunk data (that is the chunk body) will be
241     * incremented by the number of read bytes and RemainingBytes() returns
242     * how much data is left to read from the current position to the end
243     * of the chunk data.
244     *
245     * @returns number of bytes left to read
246     */
247 schoenebeck 2 unsigned long Chunk::RemainingBytes() {
248     #if DEBUG
249 schoenebeck 780 std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl;
250 schoenebeck 2 #endif // DEBUG
251 schoenebeck 1863 return (CurrentChunkSize > ulPos) ? CurrentChunkSize - ulPos : 0;
252 schoenebeck 2 }
253    
254 schoenebeck 11 /**
255     * Returns the current state of the chunk object.
256     * Following values are possible:
257     * - RIFF::stream_ready :
258     * chunk data can be read (this is the usual case)
259     * - RIFF::stream_closed :
260     * the data stream was closed somehow, no more reading possible
261     * - RIFF::stream_end_reached :
262 schoenebeck 1383 * already reached the end of the chunk data, no more reading
263 schoenebeck 11 * possible without SetPos()
264     */
265 schoenebeck 2 stream_state_t Chunk::GetState() {
266     #if DEBUG
267     std::cout << "Chunk::GetState()" << std::endl;
268     #endif // DEBUG
269     #if POSIX
270 schoenebeck 1050 if (pFile->hFileRead == 0) return stream_closed;
271     #elif defined (WIN32)
272     if (pFile->hFileRead == INVALID_HANDLE_VALUE)
273     return stream_closed;
274 schoenebeck 2 #else
275 schoenebeck 780 if (pFile->hFileRead == NULL) return stream_closed;
276 schoenebeck 2 #endif // POSIX
277 schoenebeck 780 if (ulPos < CurrentChunkSize) return stream_ready;
278     else return stream_end_reached;
279 schoenebeck 2 }
280    
281     /**
282     * Reads \a WordCount number of data words with given \a WordSize and
283     * copies it into a buffer pointed by \a pData. The buffer has to be
284     * allocated and be sure to provide the correct \a WordSize, as this
285     * will be important and taken into account for eventual endian
286     * correction (swapping of bytes due to different native byte order of
287     * a system). The position within the chunk will automatically be
288     * incremented.
289     *
290     * @param pData destination buffer
291     * @param WordCount number of data words to read
292     * @param WordSize size of each data word to read
293     * @returns number of successfully read data words or 0 if end
294     * of file reached or error occured
295     */
296     unsigned long Chunk::Read(void* pData, unsigned long WordCount, unsigned long WordSize) {
297     #if DEBUG
298     std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl;
299     #endif // DEBUG
300 schoenebeck 2543 //if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet)
301 schoenebeck 780 if (ulPos >= CurrentChunkSize) return 0;
302     if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize;
303 schoenebeck 2 #if POSIX
304 schoenebeck 780 if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0;
305     unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize);
306 schoenebeck 2 if (readWords < 1) return 0;
307     readWords /= WordSize;
308 schoenebeck 1050 #elif defined(WIN32)
309     if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0;
310     DWORD readWords;
311     ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL);
312     if (readWords < 1) return 0;
313     readWords /= WordSize;
314 schoenebeck 2 #else // standard C functions
315 schoenebeck 780 if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0;
316     unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead);
317 schoenebeck 2 #endif // POSIX
318 schoenebeck 780 if (!pFile->bEndianNative && WordSize != 1) {
319 schoenebeck 2 switch (WordSize) {
320     case 2:
321     for (unsigned long iWord = 0; iWord < readWords; iWord++)
322     swapBytes_16((uint16_t*) pData + iWord);
323     break;
324     case 4:
325     for (unsigned long iWord = 0; iWord < readWords; iWord++)
326     swapBytes_32((uint32_t*) pData + iWord);
327     break;
328     default:
329     for (unsigned long iWord = 0; iWord < readWords; iWord++)
330     swapBytes((uint8_t*) pData + iWord * WordSize, WordSize);
331     break;
332     }
333     }
334     SetPos(readWords * WordSize, stream_curpos);
335     return readWords;
336     }
337    
338 schoenebeck 780 /**
339     * Writes \a WordCount number of data words with given \a WordSize from
340     * the buffer pointed by \a pData. Be sure to provide the correct
341     * \a WordSize, as this will be important and taken into account for
342     * eventual endian correction (swapping of bytes due to different
343     * native byte order of a system). The position within the chunk will
344     * automatically be incremented.
345     *
346     * @param pData source buffer (containing the data)
347     * @param WordCount number of data words to write
348     * @param WordSize size of each data word to write
349     * @returns number of successfully written data words
350     * @throws RIFF::Exception if write operation would exceed current
351     * chunk size or any IO error occured
352     * @see Resize()
353     */
354     unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) {
355 schoenebeck 798 if (pFile->Mode != stream_mode_read_write)
356     throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first");
357     if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize > CurrentChunkSize)
358 schoenebeck 780 throw Exception("End of chunk reached while trying to write data");
359     if (!pFile->bEndianNative && WordSize != 1) {
360     switch (WordSize) {
361     case 2:
362     for (unsigned long iWord = 0; iWord < WordCount; iWord++)
363     swapBytes_16((uint16_t*) pData + iWord);
364     break;
365     case 4:
366     for (unsigned long iWord = 0; iWord < WordCount; iWord++)
367     swapBytes_32((uint32_t*) pData + iWord);
368     break;
369     default:
370     for (unsigned long iWord = 0; iWord < WordCount; iWord++)
371     swapBytes((uint8_t*) pData + iWord * WordSize, WordSize);
372     break;
373     }
374     }
375     #if POSIX
376     if (lseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET) < 0) {
377     throw Exception("Could not seek to position " + ToString(ulPos) +
378     " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");
379     }
380     unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize);
381     if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data");
382     writtenWords /= WordSize;
383 schoenebeck 1050 #elif defined(WIN32)
384     if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
385     throw Exception("Could not seek to position " + ToString(ulPos) +
386 schoenebeck 1459 " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");
387 schoenebeck 1050 }
388     DWORD writtenWords;
389     WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL);
390     if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data");
391     writtenWords /= WordSize;
392 schoenebeck 780 #else // standard C functions
393     if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) {
394     throw Exception("Could not seek to position " + ToString(ulPos) +
395     " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");
396     }
397     unsigned long writtenWords = fwrite(pData, WordSize, WordCount, pFile->hFileWrite);
398     #endif // POSIX
399     SetPos(writtenWords * WordSize, stream_curpos);
400     return writtenWords;
401     }
402    
403 schoenebeck 2 /** Just an internal wrapper for the main <i>Read()</i> method with additional Exception throwing on errors. */
404     unsigned long Chunk::ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize) {
405     unsigned long readWords = Read(pData, WordCount, WordSize);
406     if (readWords != WordCount) throw RIFF::Exception("End of chunk data reached.");
407     return readWords;
408     }
409    
410     /**
411     * Reads \a WordCount number of 8 Bit signed integer words and copies it
412     * into the buffer pointed by \a pData. The buffer has to be allocated.
413     * The position within the chunk will automatically be incremented.
414     *
415     * @param pData destination buffer
416     * @param WordCount number of 8 Bit signed integers to read
417     * @returns number of read integers
418     * @throws RIFF::Exception if an error occured or less than
419     * \a WordCount integers could be read!
420     */
421     unsigned long Chunk::ReadInt8(int8_t* pData, unsigned long WordCount) {
422     #if DEBUG
423     std::cout << "Chunk::ReadInt8(int8_t*,ulong)" << std::endl;
424     #endif // DEBUG
425     return ReadSceptical(pData, WordCount, 1);
426     }
427    
428     /**
429 schoenebeck 780 * Writes \a WordCount number of 8 Bit signed integer words from the
430     * buffer pointed by \a pData to the chunk's body, directly to the
431     * actual "physical" file. The position within the chunk will
432     * automatically be incremented. Note: you cannot write beyond the
433     * boundaries of the chunk, to append data to the chunk call Resize()
434     * before.
435     *
436     * @param pData source buffer (containing the data)
437     * @param WordCount number of 8 Bit signed integers to write
438     * @returns number of written integers
439     * @throws RIFF::Exception if an IO error occured
440     * @see Resize()
441     */
442     unsigned long Chunk::WriteInt8(int8_t* pData, unsigned long WordCount) {
443     return Write(pData, WordCount, 1);
444     }
445    
446     /**
447 schoenebeck 2 * Reads \a WordCount number of 8 Bit unsigned integer words and copies
448     * it into the buffer pointed by \a pData. The buffer has to be
449     * allocated. The position within the chunk will automatically be
450     * incremented.
451     *
452     * @param pData destination buffer
453     * @param WordCount number of 8 Bit unsigned integers to read
454     * @returns number of read integers
455     * @throws RIFF::Exception if an error occured or less than
456     * \a WordCount integers could be read!
457     */
458     unsigned long Chunk::ReadUint8(uint8_t* pData, unsigned long WordCount) {
459     #if DEBUG
460     std::cout << "Chunk::ReadUint8(uint8_t*,ulong)" << std::endl;
461     #endif // DEBUG
462     return ReadSceptical(pData, WordCount, 1);
463     }
464    
465     /**
466 schoenebeck 780 * Writes \a WordCount number of 8 Bit unsigned integer words from the
467     * buffer pointed by \a pData to the chunk's body, directly to the
468     * actual "physical" file. The position within the chunk will
469     * automatically be incremented. Note: you cannot write beyond the
470     * boundaries of the chunk, to append data to the chunk call Resize()
471     * before.
472     *
473     * @param pData source buffer (containing the data)
474     * @param WordCount number of 8 Bit unsigned integers to write
475     * @returns number of written integers
476     * @throws RIFF::Exception if an IO error occured
477     * @see Resize()
478     */
479     unsigned long Chunk::WriteUint8(uint8_t* pData, unsigned long WordCount) {
480     return Write(pData, WordCount, 1);
481     }
482    
483     /**
484 schoenebeck 2 * Reads \a WordCount number of 16 Bit signed integer words and copies
485     * it into the buffer pointed by \a pData. The buffer has to be
486     * allocated. Endian correction will automatically be done if needed.
487     * The position within the chunk will automatically be incremented.
488     *
489     * @param pData destination buffer
490     * @param WordCount number of 16 Bit signed integers to read
491     * @returns number of read integers
492     * @throws RIFF::Exception if an error occured or less than
493     * \a WordCount integers could be read!
494     */
495     unsigned long Chunk::ReadInt16(int16_t* pData, unsigned long WordCount) {
496     #if DEBUG
497     std::cout << "Chunk::ReadInt16(int16_t*,ulong)" << std::endl;
498     #endif // DEBUG
499     return ReadSceptical(pData, WordCount, 2);
500     }
501    
502     /**
503 schoenebeck 780 * Writes \a WordCount number of 16 Bit signed integer words from the
504     * buffer pointed by \a pData to the chunk's body, directly to the
505     * actual "physical" file. The position within the chunk will
506     * automatically be incremented. Note: you cannot write beyond the
507     * boundaries of the chunk, to append data to the chunk call Resize()
508     * before.
509     *
510     * @param pData source buffer (containing the data)
511     * @param WordCount number of 16 Bit signed integers to write
512     * @returns number of written integers
513     * @throws RIFF::Exception if an IO error occured
514     * @see Resize()
515     */
516     unsigned long Chunk::WriteInt16(int16_t* pData, unsigned long WordCount) {
517     return Write(pData, WordCount, 2);
518     }
519    
520     /**
521 schoenebeck 2 * Reads \a WordCount number of 16 Bit unsigned integer words and copies
522     * it into the buffer pointed by \a pData. The buffer has to be
523     * allocated. Endian correction will automatically be done if needed.
524     * The position within the chunk will automatically be incremented.
525     *
526     * @param pData destination buffer
527     * @param WordCount number of 8 Bit unsigned integers to read
528     * @returns number of read integers
529     * @throws RIFF::Exception if an error occured or less than
530     * \a WordCount integers could be read!
531     */
532     unsigned long Chunk::ReadUint16(uint16_t* pData, unsigned long WordCount) {
533     #if DEBUG
534     std::cout << "Chunk::ReadUint16(uint16_t*,ulong)" << std::endl;
535     #endif // DEBUG
536     return ReadSceptical(pData, WordCount, 2);
537     }
538    
539     /**
540 schoenebeck 780 * Writes \a WordCount number of 16 Bit unsigned integer words from the
541     * buffer pointed by \a pData to the chunk's body, directly to the
542     * actual "physical" file. The position within the chunk will
543     * automatically be incremented. Note: you cannot write beyond the
544     * boundaries of the chunk, to append data to the chunk call Resize()
545     * before.
546     *
547     * @param pData source buffer (containing the data)
548     * @param WordCount number of 16 Bit unsigned integers to write
549     * @returns number of written integers
550     * @throws RIFF::Exception if an IO error occured
551     * @see Resize()
552     */
553     unsigned long Chunk::WriteUint16(uint16_t* pData, unsigned long WordCount) {
554     return Write(pData, WordCount, 2);
555     }
556    
557     /**
558 schoenebeck 2 * Reads \a WordCount number of 32 Bit signed integer words and copies
559     * it into the buffer pointed by \a pData. The buffer has to be
560     * allocated. Endian correction will automatically be done if needed.
561     * The position within the chunk will automatically be incremented.
562     *
563     * @param pData destination buffer
564     * @param WordCount number of 32 Bit signed integers to read
565     * @returns number of read integers
566     * @throws RIFF::Exception if an error occured or less than
567     * \a WordCount integers could be read!
568     */
569     unsigned long Chunk::ReadInt32(int32_t* pData, unsigned long WordCount) {
570     #if DEBUG
571     std::cout << "Chunk::ReadInt32(int32_t*,ulong)" << std::endl;
572     #endif // DEBUG
573     return ReadSceptical(pData, WordCount, 4);
574     }
575    
576     /**
577 schoenebeck 780 * Writes \a WordCount number of 32 Bit signed integer words from the
578     * buffer pointed by \a pData to the chunk's body, directly to the
579     * actual "physical" file. The position within the chunk will
580     * automatically be incremented. Note: you cannot write beyond the
581     * boundaries of the chunk, to append data to the chunk call Resize()
582     * before.
583     *
584     * @param pData source buffer (containing the data)
585     * @param WordCount number of 32 Bit signed integers to write
586     * @returns number of written integers
587     * @throws RIFF::Exception if an IO error occured
588     * @see Resize()
589     */
590     unsigned long Chunk::WriteInt32(int32_t* pData, unsigned long WordCount) {
591     return Write(pData, WordCount, 4);
592     }
593    
594     /**
595 schoenebeck 2 * Reads \a WordCount number of 32 Bit unsigned integer words and copies
596     * it into the buffer pointed by \a pData. The buffer has to be
597     * allocated. Endian correction will automatically be done if needed.
598     * The position within the chunk will automatically be incremented.
599     *
600     * @param pData destination buffer
601     * @param WordCount number of 32 Bit unsigned integers to read
602     * @returns number of read integers
603     * @throws RIFF::Exception if an error occured or less than
604     * \a WordCount integers could be read!
605     */
606     unsigned long Chunk::ReadUint32(uint32_t* pData, unsigned long WordCount) {
607     #if DEBUG
608     std::cout << "Chunk::ReadUint32(uint32_t*,ulong)" << std::endl;
609     #endif // DEBUG
610     return ReadSceptical(pData, WordCount, 4);
611     }
612    
613     /**
614 persson 2450 * Reads a null-padded string of size characters and copies it
615     * into the string \a s. The position within the chunk will
616     * automatically be incremented.
617     *
618     * @param s destination string
619     * @param size number of characters to read
620     * @throws RIFF::Exception if an error occured or less than
621     * \a size characters could be read!
622     */
623     void Chunk::ReadString(String& s, int size) {
624     char* buf = new char[size];
625     ReadSceptical(buf, 1, size);
626     s.assign(buf, std::find(buf, buf + size, '\0'));
627     delete[] buf;
628     }
629    
630     /**
631 schoenebeck 780 * Writes \a WordCount number of 32 Bit unsigned integer words from the
632     * buffer pointed by \a pData to the chunk's body, directly to the
633     * actual "physical" file. The position within the chunk will
634     * automatically be incremented. Note: you cannot write beyond the
635     * boundaries of the chunk, to append data to the chunk call Resize()
636     * before.
637     *
638     * @param pData source buffer (containing the data)
639     * @param WordCount number of 32 Bit unsigned integers to write
640     * @returns number of written integers
641     * @throws RIFF::Exception if an IO error occured
642     * @see Resize()
643     */
644     unsigned long Chunk::WriteUint32(uint32_t* pData, unsigned long WordCount) {
645     return Write(pData, WordCount, 4);
646     }
647    
648     /**
649 schoenebeck 2 * Reads one 8 Bit signed integer word and increments the position within
650     * the chunk.
651     *
652     * @returns read integer word
653     * @throws RIFF::Exception if an error occured
654     */
655     int8_t Chunk::ReadInt8() {
656     #if DEBUG
657     std::cout << "Chunk::ReadInt8()" << std::endl;
658     #endif // DEBUG
659     int8_t word;
660     ReadSceptical(&word,1,1);
661     return word;
662     }
663    
664     /**
665     * Reads one 8 Bit unsigned integer word and increments the position
666     * within the chunk.
667     *
668     * @returns read integer word
669     * @throws RIFF::Exception if an error occured
670     */
671     uint8_t Chunk::ReadUint8() {
672     #if DEBUG
673     std::cout << "Chunk::ReadUint8()" << std::endl;
674     #endif // DEBUG
675     uint8_t word;
676     ReadSceptical(&word,1,1);
677     return word;
678     }
679    
680     /**
681     * Reads one 16 Bit signed integer word and increments the position
682     * within the chunk. Endian correction will automatically be done if
683     * needed.
684     *
685     * @returns read integer word
686     * @throws RIFF::Exception if an error occured
687     */
688     int16_t Chunk::ReadInt16() {
689     #if DEBUG
690     std::cout << "Chunk::ReadInt16()" << std::endl;
691     #endif // DEBUG
692     int16_t word;
693     ReadSceptical(&word,1,2);
694     return word;
695     }
696    
697     /**
698     * Reads one 16 Bit unsigned integer word and increments the position
699     * within the chunk. Endian correction will automatically be done if
700     * needed.
701     *
702     * @returns read integer word
703     * @throws RIFF::Exception if an error occured
704     */
705     uint16_t Chunk::ReadUint16() {
706     #if DEBUG
707     std::cout << "Chunk::ReadUint16()" << std::endl;
708     #endif // DEBUG
709     uint16_t word;
710     ReadSceptical(&word,1,2);
711     return word;
712     }
713    
714     /**
715     * Reads one 32 Bit signed integer word and increments the position
716     * within the chunk. Endian correction will automatically be done if
717     * needed.
718     *
719     * @returns read integer word
720     * @throws RIFF::Exception if an error occured
721     */
722     int32_t Chunk::ReadInt32() {
723     #if DEBUG
724     std::cout << "Chunk::ReadInt32()" << std::endl;
725     #endif // DEBUG
726     int32_t word;
727     ReadSceptical(&word,1,4);
728     return word;
729     }
730    
731     /**
732     * Reads one 32 Bit unsigned integer word and increments the position
733     * within the chunk. Endian correction will automatically be done if
734     * needed.
735     *
736     * @returns read integer word
737     * @throws RIFF::Exception if an error occured
738     */
739     uint32_t Chunk::ReadUint32() {
740     #if DEBUG
741     std::cout << "Chunk::ReadUint32()" << std::endl;
742     #endif // DEBUG
743     uint32_t word;
744     ReadSceptical(&word,1,4);
745     return word;
746     }
747    
748 schoenebeck 780 /** @brief Load chunk body into RAM.
749     *
750     * Loads the whole chunk body into memory. You can modify the data in
751     * RAM and save the data by calling File::Save() afterwards.
752     *
753     * <b>Caution:</b> the buffer pointer will be invalidated once
754     * File::Save() was called. You have to call LoadChunkData() again to
755 schoenebeck 800 * get a new, valid pointer whenever File::Save() was called.
756 schoenebeck 780 *
757 schoenebeck 800 * You can call LoadChunkData() again if you previously scheduled to
758     * enlarge this chunk with a Resize() call. In that case the buffer will
759     * be enlarged to the new, scheduled chunk size and you can already
760     * place the new chunk data to the buffer and finally call File::Save()
761     * to enlarge the chunk physically and write the new data in one rush.
762     * This approach is definitely recommended if you have to enlarge and
763     * write new data to a lot of chunks.
764     *
765 schoenebeck 780 * @returns a pointer to the data in RAM on success, NULL otherwise
766 schoenebeck 800 * @throws Exception if data buffer could not be enlarged
767 schoenebeck 780 * @see ReleaseChunkData()
768     */
769 schoenebeck 2 void* Chunk::LoadChunkData() {
770 schoenebeck 2543 if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) {
771 schoenebeck 2 #if POSIX
772 schoenebeck 780 if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;
773 schoenebeck 1050 #elif defined(WIN32)
774     if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL;
775 schoenebeck 800 #else
776     if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL;
777     #endif // POSIX
778     unsigned long ulBufferSize = (CurrentChunkSize > NewChunkSize) ? CurrentChunkSize : NewChunkSize;
779     pChunkData = new uint8_t[ulBufferSize];
780 schoenebeck 2 if (!pChunkData) return NULL;
781 schoenebeck 800 memset(pChunkData, 0, ulBufferSize);
782     #if POSIX
783 schoenebeck 780 unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize());
784 schoenebeck 1050 #elif defined(WIN32)
785     DWORD readWords;
786     ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL);
787 schoenebeck 2 #else
788 schoenebeck 780 unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead);
789 schoenebeck 2 #endif // POSIX
790     if (readWords != GetSize()) {
791     delete[] pChunkData;
792     return (pChunkData = NULL);
793     }
794 schoenebeck 800 ulChunkDataSize = ulBufferSize;
795     } else if (NewChunkSize > ulChunkDataSize) {
796     uint8_t* pNewBuffer = new uint8_t[NewChunkSize];
797     if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(NewChunkSize) + " bytes");
798     memset(pNewBuffer, 0 , NewChunkSize);
799     memcpy(pNewBuffer, pChunkData, ulChunkDataSize);
800     delete[] pChunkData;
801     pChunkData = pNewBuffer;
802     ulChunkDataSize = NewChunkSize;
803 schoenebeck 2 }
804     return pChunkData;
805     }
806    
807 schoenebeck 780 /** @brief Free loaded chunk body from RAM.
808     *
809     * Frees loaded chunk body data from memory (RAM). You should call
810     * File::Save() before calling this method if you modified the data to
811     * make the changes persistent.
812     */
813 schoenebeck 2 void Chunk::ReleaseChunkData() {
814     if (pChunkData) {
815     delete[] pChunkData;
816     pChunkData = NULL;
817     }
818     }
819    
820 schoenebeck 780 /** @brief Resize chunk.
821     *
822     * Resizes this chunk's body, that is the actual size of data possible
823     * to be written to this chunk. This call will return immediately and
824     * just schedule the resize operation. You should call File::Save() to
825     * actually perform the resize operation(s) "physically" to the file.
826     * As this can take a while on large files, it is recommended to call
827     * Resize() first on all chunks which have to be resized and finally to
828     * call File::Save() to perform all those resize operations in one rush.
829     *
830     * <b>Caution:</b> You cannot directly write to enlarged chunks before
831     * calling File::Save() as this might exceed the current chunk's body
832 schoenebeck 800 * boundary!
833 schoenebeck 780 *
834     * @param iNewSize - new chunk body size in bytes (must be greater than zero)
835     * @throws RIFF::Exception if \a iNewSize is less than 1
836     * @see File::Save()
837     */
838     void Chunk::Resize(int iNewSize) {
839 schoenebeck 1105 if (iNewSize <= 0)
840     throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this));
841 schoenebeck 800 if (NewChunkSize == iNewSize) return;
842 schoenebeck 780 NewChunkSize = iNewSize;
843     pFile->LogAsResized(this);
844     }
845 schoenebeck 2
846 schoenebeck 780 /** @brief Write chunk persistently e.g. to disk.
847     *
848     * Stores the chunk persistently to its actual "physical" file.
849     *
850     * @param ulWritePos - position within the "physical" file where this
851     * chunk should be written to
852     * @param ulCurrentDataOffset - offset of current (old) data within
853     * the file
854 schoenebeck 2682 * @param pProgress - optional: callback function for progress notification
855 schoenebeck 780 * @returns new write position in the "physical" file, that is
856     * \a ulWritePos incremented by this chunk's new size
857     * (including its header size of course)
858     */
859 schoenebeck 2682 unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset, progress_t* pProgress) {
860 schoenebeck 798 const unsigned long ulOriginalPos = ulWritePos;
861 schoenebeck 780 ulWritePos += CHUNK_HEADER_SIZE;
862 schoenebeck 2
863 schoenebeck 798 if (pFile->Mode != stream_mode_read_write)
864     throw Exception("Cannot write list chunk, file has to be opened in read+write mode");
865    
866 schoenebeck 780 // if the whole chunk body was loaded into RAM
867     if (pChunkData) {
868 schoenebeck 800 // make sure chunk data buffer in RAM is at least as large as the new chunk size
869     LoadChunkData();
870 schoenebeck 780 // write chunk data from RAM persistently to the file
871     #if POSIX
872     lseek(pFile->hFileWrite, ulWritePos, SEEK_SET);
873     if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) {
874     throw Exception("Writing Chunk data (from RAM) failed");
875     }
876 schoenebeck 1050 #elif defined(WIN32)
877     SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN);
878     DWORD dwBytesWritten;
879     WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL);
880     if (dwBytesWritten != NewChunkSize) {
881     throw Exception("Writing Chunk data (from RAM) failed");
882     }
883 schoenebeck 780 #else
884     fseek(pFile->hFileWrite, ulWritePos, SEEK_SET);
885     if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) {
886     throw Exception("Writing Chunk data (from RAM) failed");
887     }
888     #endif // POSIX
889     } else {
890     // move chunk data from the end of the file to the appropriate position
891     int8_t* pCopyBuffer = new int8_t[4096];
892     unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize;
893 schoenebeck 1050 #if defined(WIN32)
894     DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
895     #else
896 schoenebeck 780 int iBytesMoved = 1;
897 schoenebeck 1050 #endif
898 persson 2120 for (unsigned long ulOffset = 0; ulToMove > 0 && iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) {
899 schoenebeck 780 iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;
900     #if POSIX
901     lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);
902     iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved);
903     lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET);
904     iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved);
905 schoenebeck 1050 #elif defined(WIN32)
906     SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN);
907     ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
908     SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN);
909     WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
910 schoenebeck 780 #else
911     fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);
912     iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead);
913     fseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET);
914     iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, pFile->hFileWrite);
915     #endif
916     }
917     delete[] pCopyBuffer;
918     if (iBytesMoved < 0) throw Exception("Writing Chunk data (from file) failed");
919     }
920    
921     // update this chunk's header
922     CurrentChunkSize = NewChunkSize;
923     WriteHeader(ulOriginalPos);
924    
925 schoenebeck 2682 __notify_progress(pProgress, 1.0); // notify done
926    
927 schoenebeck 780 // update chunk's position pointers
928     ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE;
929     ulPos = 0;
930    
931     // add pad byte if needed
932     if ((ulStartPos + NewChunkSize) % 2 != 0) {
933 schoenebeck 798 const char cPadByte = 0;
934 schoenebeck 780 #if POSIX
935 schoenebeck 798 lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);
936 schoenebeck 780 write(pFile->hFileWrite, &cPadByte, 1);
937 schoenebeck 1050 #elif defined(WIN32)
938     SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN);
939     DWORD dwBytesWritten;
940     WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL);
941 schoenebeck 780 #else
942 schoenebeck 798 fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);
943 schoenebeck 780 fwrite(&cPadByte, 1, 1, pFile->hFileWrite);
944     #endif
945     return ulStartPos + NewChunkSize + 1;
946     }
947    
948     return ulStartPos + NewChunkSize;
949     }
950    
951     void Chunk::__resetPos() {
952     ulPos = 0;
953     }
954    
955    
956    
957 schoenebeck 2 // *************** List ***************
958     // *
959    
960 schoenebeck 780 List::List(File* pFile) : Chunk(pFile) {
961 schoenebeck 2 #if DEBUG
962 schoenebeck 780 std::cout << "List::List(File* pFile)" << std::endl;
963 schoenebeck 2 #endif // DEBUG
964     pSubChunks = NULL;
965     pSubChunksMap = NULL;
966     }
967    
968 schoenebeck 780 List::List(File* pFile, unsigned long StartPos, List* Parent)
969     : Chunk(pFile, StartPos, Parent) {
970 schoenebeck 2 #if DEBUG
971 schoenebeck 780 std::cout << "List::List(File*,ulong,bool,List*)" << std::endl;
972 schoenebeck 2 #endif // DEBUG
973     pSubChunks = NULL;
974     pSubChunksMap = NULL;
975     ReadHeader(StartPos);
976     ulStartPos = StartPos + LIST_HEADER_SIZE;
977     }
978    
979 schoenebeck 780 List::List(File* pFile, List* pParent, uint32_t uiListID)
980     : Chunk(pFile, pParent, CHUNK_ID_LIST, 0) {
981     pSubChunks = NULL;
982     pSubChunksMap = NULL;
983     ListType = uiListID;
984     }
985    
986 schoenebeck 2 List::~List() {
987     #if DEBUG
988     std::cout << "List::~List()" << std::endl;
989     #endif // DEBUG
990 persson 1869 DeleteChunkList();
991     }
992    
993     void List::DeleteChunkList() {
994 schoenebeck 2 if (pSubChunks) {
995     ChunkList::iterator iter = pSubChunks->begin();
996     ChunkList::iterator end = pSubChunks->end();
997     while (iter != end) {
998     delete *iter;
999     iter++;
1000     }
1001     delete pSubChunks;
1002 persson 1869 pSubChunks = NULL;
1003 schoenebeck 2 }
1004 persson 1869 if (pSubChunksMap) {
1005     delete pSubChunksMap;
1006     pSubChunksMap = NULL;
1007     }
1008 schoenebeck 2 }
1009    
1010 schoenebeck 11 /**
1011     * Returns subchunk with chunk ID <i>\a ChunkID</i> within this chunk
1012     * list. Use this method if you expect only one subchunk of that type in
1013     * the list. It there are more than one, it's undetermined which one of
1014     * them will be returned! If there are no subchunks with that desired
1015     * chunk ID, NULL will be returned.
1016     *
1017     * @param ChunkID - chunk ID of the sought subchunk
1018     * @returns pointer to the subchunk or NULL if there is none of
1019     * that ID
1020     */
1021 schoenebeck 2 Chunk* List::GetSubChunk(uint32_t ChunkID) {
1022     #if DEBUG
1023     std::cout << "List::GetSubChunk(uint32_t)" << std::endl;
1024     #endif // DEBUG
1025     if (!pSubChunksMap) LoadSubChunks();
1026     return (*pSubChunksMap)[ChunkID];
1027     }
1028    
1029 schoenebeck 11 /**
1030     * Returns sublist chunk with list type <i>\a ListType</i> within this
1031     * chunk list. Use this method if you expect only one sublist chunk of
1032     * that type in the list. It there are more than one, it's undetermined
1033     * which one of them will be returned! If there are no sublists with
1034     * that desired list type, NULL will be returned.
1035     *
1036     * @param ListType - list type of the sought sublist
1037     * @returns pointer to the sublist or NULL if there is none of
1038     * that type
1039     */
1040 schoenebeck 2 List* List::GetSubList(uint32_t ListType) {
1041     #if DEBUG
1042     std::cout << "List::GetSubList(uint32_t)" << std::endl;
1043     #endif // DEBUG
1044     if (!pSubChunks) LoadSubChunks();
1045     ChunkList::iterator iter = pSubChunks->begin();
1046     ChunkList::iterator end = pSubChunks->end();
1047     while (iter != end) {
1048     if ((*iter)->GetChunkID() == CHUNK_ID_LIST) {
1049     List* l = (List*) *iter;
1050     if (l->GetListType() == ListType) return l;
1051     }
1052     iter++;
1053     }
1054     return NULL;
1055     }
1056    
1057 schoenebeck 11 /**
1058     * Returns the first subchunk within the list. You have to call this
1059     * method before you can call GetNextSubChunk(). Recall it when you want
1060     * to start from the beginning of the list again.
1061     *
1062     * @returns pointer to the first subchunk within the list, NULL
1063     * otherwise
1064     */
1065 schoenebeck 2 Chunk* List::GetFirstSubChunk() {
1066     #if DEBUG
1067     std::cout << "List::GetFirstSubChunk()" << std::endl;
1068     #endif // DEBUG
1069     if (!pSubChunks) LoadSubChunks();
1070     ChunksIterator = pSubChunks->begin();
1071     return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
1072     }
1073    
1074 schoenebeck 11 /**
1075     * Returns the next subchunk within the list. You have to call
1076     * GetFirstSubChunk() before you can use this method!
1077     *
1078     * @returns pointer to the next subchunk within the list or NULL if
1079     * end of list is reached
1080     */
1081 schoenebeck 2 Chunk* List::GetNextSubChunk() {
1082     #if DEBUG
1083     std::cout << "List::GetNextSubChunk()" << std::endl;
1084     #endif // DEBUG
1085     if (!pSubChunks) return NULL;
1086     ChunksIterator++;
1087     return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
1088     }
1089    
1090 schoenebeck 11 /**
1091     * Returns the first sublist within the list (that is a subchunk with
1092     * chunk ID "LIST"). You have to call this method before you can call
1093     * GetNextSubList(). Recall it when you want to start from the beginning
1094     * of the list again.
1095     *
1096     * @returns pointer to the first sublist within the list, NULL
1097     * otherwise
1098     */
1099 schoenebeck 2 List* List::GetFirstSubList() {
1100     #if DEBUG
1101     std::cout << "List::GetFirstSubList()" << std::endl;
1102     #endif // DEBUG
1103     if (!pSubChunks) LoadSubChunks();
1104     ListIterator = pSubChunks->begin();
1105     ChunkList::iterator end = pSubChunks->end();
1106     while (ListIterator != end) {
1107     if ((*ListIterator)->GetChunkID() == CHUNK_ID_LIST) return (List*) *ListIterator;
1108     ListIterator++;
1109     }
1110     return NULL;
1111     }
1112    
1113 schoenebeck 11 /**
1114     * Returns the next sublist (that is a subchunk with chunk ID "LIST")
1115     * within the list. You have to call GetFirstSubList() before you can
1116     * use this method!
1117     *
1118     * @returns pointer to the next sublist within the list, NULL if
1119     * end of list is reached
1120     */
1121 schoenebeck 2 List* List::GetNextSubList() {
1122     #if DEBUG
1123     std::cout << "List::GetNextSubList()" << std::endl;
1124     #endif // DEBUG
1125     if (!pSubChunks) return NULL;
1126     if (ListIterator == pSubChunks->end()) return NULL;
1127     ListIterator++;
1128     ChunkList::iterator end = pSubChunks->end();
1129     while (ListIterator != end) {
1130     if ((*ListIterator)->GetChunkID() == CHUNK_ID_LIST) return (List*) *ListIterator;
1131     ListIterator++;
1132     }
1133     return NULL;
1134     }
1135    
1136 schoenebeck 11 /**
1137 schoenebeck 515 * Returns number of subchunks within the list.
1138 schoenebeck 11 */
1139 schoenebeck 2 unsigned int List::CountSubChunks() {
1140     if (!pSubChunks) LoadSubChunks();
1141     return pSubChunks->size();
1142     }
1143    
1144 schoenebeck 11 /**
1145     * Returns number of subchunks within the list with chunk ID
1146     * <i>\a ChunkId</i>.
1147     */
1148 schoenebeck 2 unsigned int List::CountSubChunks(uint32_t ChunkID) {
1149     unsigned int result = 0;
1150     if (!pSubChunks) LoadSubChunks();
1151     ChunkList::iterator iter = pSubChunks->begin();
1152     ChunkList::iterator end = pSubChunks->end();
1153     while (iter != end) {
1154     if ((*iter)->GetChunkID() == ChunkID) {
1155     result++;
1156     }
1157     iter++;
1158     }
1159     return result;
1160     }
1161    
1162 schoenebeck 11 /**
1163     * Returns number of sublists within the list.
1164     */
1165 schoenebeck 2 unsigned int List::CountSubLists() {
1166     return CountSubChunks(CHUNK_ID_LIST);
1167     }
1168    
1169 schoenebeck 11 /**
1170     * Returns number of sublists within the list with list type
1171     * <i>\a ListType</i>
1172     */
1173 schoenebeck 2 unsigned int List::CountSubLists(uint32_t ListType) {
1174     unsigned int result = 0;
1175     if (!pSubChunks) LoadSubChunks();
1176     ChunkList::iterator iter = pSubChunks->begin();
1177     ChunkList::iterator end = pSubChunks->end();
1178     while (iter != end) {
1179     if ((*iter)->GetChunkID() == CHUNK_ID_LIST) {
1180     List* l = (List*) *iter;
1181     if (l->GetListType() == ListType) result++;
1182     }
1183     iter++;
1184     }
1185     return result;
1186     }
1187    
1188 schoenebeck 780 /** @brief Creates a new sub chunk.
1189     *
1190     * Creates and adds a new sub chunk to this list chunk. Note that the
1191     * chunk's body size given by \a uiBodySize must be greater than zero.
1192     * You have to call File::Save() to make this change persistent to the
1193     * actual file and <b>before</b> performing any data write operations
1194     * on the new chunk!
1195     *
1196     * @param uiChunkID - chunk ID of the new chunk
1197     * @param uiBodySize - size of the new chunk's body, that is its actual
1198     * data size (without header)
1199     * @throws RIFF::Exception if \a uiBodySize equals zero
1200     */
1201     Chunk* List::AddSubChunk(uint32_t uiChunkID, uint uiBodySize) {
1202     if (uiBodySize == 0) throw Exception("Chunk body size must be at least 1 byte");
1203     if (!pSubChunks) LoadSubChunks();
1204 schoenebeck 798 Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0);
1205 schoenebeck 780 pSubChunks->push_back(pNewChunk);
1206     (*pSubChunksMap)[uiChunkID] = pNewChunk;
1207 schoenebeck 798 pNewChunk->Resize(uiBodySize);
1208 persson 1678 NewChunkSize += CHUNK_HEADER_SIZE;
1209     pFile->LogAsResized(this);
1210 schoenebeck 780 return pNewChunk;
1211     }
1212    
1213 schoenebeck 2584 /** @brief Moves a sub chunk witin this list.
1214 persson 1102 *
1215 schoenebeck 2584 * Moves a sub chunk from one position in this list to another
1216 persson 1102 * position in the same list. The pSrc chunk is placed before the
1217     * pDst chunk.
1218     *
1219     * @param pSrc - sub chunk to be moved
1220     * @param pDst - the position to move to. pSrc will be placed
1221     * before pDst. If pDst is 0, pSrc will be placed
1222     * last in list.
1223     */
1224     void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) {
1225     if (!pSubChunks) LoadSubChunks();
1226     pSubChunks->remove(pSrc);
1227     ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst);
1228     pSubChunks->insert(iter, pSrc);
1229     }
1230    
1231 schoenebeck 2584 /** @brief Moves a sub chunk from this list to another list.
1232     *
1233     * Moves a sub chunk from this list list to the end of another
1234     * list.
1235     *
1236     * @param pSrc - sub chunk to be moved
1237     * @param pDst - destination list where the chunk shall be moved to
1238     */
1239     void List::MoveSubChunk(Chunk* pSrc, List* pNewParent) {
1240     if (pNewParent == this || !pNewParent) return;
1241     if (!pSubChunks) LoadSubChunks();
1242     if (!pNewParent->pSubChunks) pNewParent->LoadSubChunks();
1243     pSubChunks->remove(pSrc);
1244     pNewParent->pSubChunks->push_back(pSrc);
1245     // update chunk id map of this List
1246     if ((*pSubChunksMap)[pSrc->GetChunkID()] == pSrc) {
1247     pSubChunksMap->erase(pSrc->GetChunkID());
1248     // try to find another chunk of the same chunk ID
1249     ChunkList::iterator iter = pSubChunks->begin();
1250     ChunkList::iterator end = pSubChunks->end();
1251     for (; iter != end; ++iter) {
1252     if ((*iter)->GetChunkID() == pSrc->GetChunkID()) {
1253     (*pSubChunksMap)[pSrc->GetChunkID()] = *iter;
1254     break; // we're done, stop search
1255     }
1256     }
1257     }
1258     // update chunk id map of other list
1259     if (!(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()])
1260     (*pNewParent->pSubChunksMap)[pSrc->GetChunkID()] = pSrc;
1261     }
1262    
1263 schoenebeck 780 /** @brief Creates a new list sub chunk.
1264     *
1265     * Creates and adds a new list sub chunk to this list chunk. Note that
1266     * you have to add sub chunks / sub list chunks to the new created chunk
1267     * <b>before</b> trying to make this change persisten to the actual
1268     * file with File::Save()!
1269     *
1270     * @param uiListType - list ID of the new list chunk
1271     */
1272     List* List::AddSubList(uint32_t uiListType) {
1273     if (!pSubChunks) LoadSubChunks();
1274     List* pNewListChunk = new List(pFile, this, uiListType);
1275     pSubChunks->push_back(pNewListChunk);
1276     (*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk;
1277 persson 1678 NewChunkSize += LIST_HEADER_SIZE;
1278     pFile->LogAsResized(this);
1279 schoenebeck 780 return pNewListChunk;
1280     }
1281    
1282     /** @brief Removes a sub chunk.
1283     *
1284     * Removes the sub chunk given by \a pSubChunk from this list and frees
1285     * it completely from RAM. The given chunk can either be a normal sub
1286 schoenebeck 1093 * chunk or a list sub chunk. In case the given chunk is a list chunk,
1287     * all its subchunks (if any) will be removed recursively as well. You
1288     * should call File::Save() to make this change persistent at any time.
1289 schoenebeck 780 *
1290     * @param pSubChunk - sub chunk or sub list chunk to be removed
1291     */
1292     void List::DeleteSubChunk(Chunk* pSubChunk) {
1293     if (!pSubChunks) LoadSubChunks();
1294     pSubChunks->remove(pSubChunk);
1295     if ((*pSubChunksMap)[pSubChunk->GetChunkID()] == pSubChunk) {
1296     pSubChunksMap->erase(pSubChunk->GetChunkID());
1297     // try to find another chunk of the same chunk ID
1298     ChunkList::iterator iter = pSubChunks->begin();
1299     ChunkList::iterator end = pSubChunks->end();
1300     for (; iter != end; ++iter) {
1301     if ((*iter)->GetChunkID() == pSubChunk->GetChunkID()) {
1302     (*pSubChunksMap)[pSubChunk->GetChunkID()] = *iter;
1303     break; // we're done, stop search
1304     }
1305     }
1306     }
1307     delete pSubChunk;
1308     }
1309    
1310 schoenebeck 2 void List::ReadHeader(unsigned long fPos) {
1311     #if DEBUG
1312     std::cout << "List::Readheader(ulong) ";
1313     #endif // DEBUG
1314     Chunk::ReadHeader(fPos);
1315 persson 1885 if (CurrentChunkSize < 4) return;
1316 schoenebeck 780 NewChunkSize = CurrentChunkSize -= 4;
1317 schoenebeck 2 #if POSIX
1318 schoenebeck 780 lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1319     read(pFile->hFileRead, &ListType, 4);
1320 schoenebeck 1050 #elif defined(WIN32)
1321     SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN);
1322     DWORD dwBytesRead;
1323     ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL);
1324 schoenebeck 2 #else
1325 schoenebeck 780 fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1326     fread(&ListType, 4, 1, pFile->hFileRead);
1327 schoenebeck 2 #endif // POSIX
1328     #if DEBUG
1329     std::cout << "listType=" << convertToString(ListType) << std::endl;
1330     #endif // DEBUG
1331 schoenebeck 780 if (!pFile->bEndianNative) {
1332 schoenebeck 11 //swapBytes_32(&ListType);
1333 schoenebeck 2 }
1334     }
1335    
1336 schoenebeck 780 void List::WriteHeader(unsigned long fPos) {
1337     // the four list type bytes officially belong the chunk's body in the RIFF format
1338     NewChunkSize += 4;
1339     Chunk::WriteHeader(fPos);
1340     NewChunkSize -= 4; // just revert the +4 incrementation
1341     #if POSIX
1342     lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1343     write(pFile->hFileWrite, &ListType, 4);
1344 schoenebeck 1050 #elif defined(WIN32)
1345     SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN);
1346     DWORD dwBytesWritten;
1347     WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL);
1348 schoenebeck 780 #else
1349     fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET);
1350     fwrite(&ListType, 4, 1, pFile->hFileWrite);
1351     #endif // POSIX
1352     }
1353    
1354 schoenebeck 2682 void List::LoadSubChunks(progress_t* pProgress) {
1355 schoenebeck 2 #if DEBUG
1356     std::cout << "List::LoadSubChunks()";
1357     #endif // DEBUG
1358     if (!pSubChunks) {
1359     pSubChunks = new ChunkList();
1360     pSubChunksMap = new ChunkMap();
1361 persson 1301 #if defined(WIN32)
1362     if (pFile->hFileRead == INVALID_HANDLE_VALUE) return;
1363     #else
1364 schoenebeck 780 if (!pFile->hFileRead) return;
1365 persson 1301 #endif
1366 schoenebeck 515 unsigned long uiOriginalPos = GetPos();
1367     SetPos(0); // jump to beginning of list chunk body
1368 schoenebeck 2 while (RemainingBytes() >= CHUNK_HEADER_SIZE) {
1369     Chunk* ck;
1370 schoenebeck 11 uint32_t ckid;
1371     Read(&ckid, 4, 1);
1372 schoenebeck 2 #if DEBUG
1373     std::cout << " ckid=" << convertToString(ckid) << std::endl;
1374     #endif // DEBUG
1375     if (ckid == CHUNK_ID_LIST) {
1376 schoenebeck 780 ck = new RIFF::List(pFile, ulStartPos + ulPos - 4, this);
1377 schoenebeck 2 SetPos(ck->GetSize() + LIST_HEADER_SIZE - 4, RIFF::stream_curpos);
1378     }
1379     else { // simple chunk
1380 schoenebeck 780 ck = new RIFF::Chunk(pFile, ulStartPos + ulPos - 4, this);
1381 schoenebeck 2 SetPos(ck->GetSize() + CHUNK_HEADER_SIZE - 4, RIFF::stream_curpos);
1382     }
1383     pSubChunks->push_back(ck);
1384     (*pSubChunksMap)[ckid] = ck;
1385     if (GetPos() % 2 != 0) SetPos(1, RIFF::stream_curpos); // jump over pad byte
1386     }
1387 schoenebeck 515 SetPos(uiOriginalPos); // restore position before this call
1388 schoenebeck 2 }
1389 schoenebeck 2682 __notify_progress(pProgress, 1.0); // notify done
1390 schoenebeck 2 }
1391    
1392 schoenebeck 2682 void List::LoadSubChunksRecursively(progress_t* pProgress) {
1393     const int n = CountSubLists();
1394     int i = 0;
1395     for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) {
1396     // divide local progress into subprogress
1397     progress_t subprogress;
1398     __divide_progress(pProgress, &subprogress, n, i);
1399     // do the actual work
1400     pList->LoadSubChunksRecursively(&subprogress);
1401     }
1402     __notify_progress(pProgress, 1.0); // notify done
1403 schoenebeck 833 }
1404    
1405 schoenebeck 780 /** @brief Write list chunk persistently e.g. to disk.
1406     *
1407     * Stores the list chunk persistently to its actual "physical" file. All
1408     * subchunks (including sub list chunks) will be stored recursively as
1409     * well.
1410     *
1411     * @param ulWritePos - position within the "physical" file where this
1412     * list chunk should be written to
1413     * @param ulCurrentDataOffset - offset of current (old) data within
1414     * the file
1415 schoenebeck 2682 * @param pProgress - optional: callback function for progress notification
1416 schoenebeck 780 * @returns new write position in the "physical" file, that is
1417     * \a ulWritePos incremented by this list chunk's new size
1418     * (including its header size of course)
1419     */
1420 schoenebeck 2682 unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset, progress_t* pProgress) {
1421 schoenebeck 798 const unsigned long ulOriginalPos = ulWritePos;
1422 schoenebeck 780 ulWritePos += LIST_HEADER_SIZE;
1423    
1424 schoenebeck 798 if (pFile->Mode != stream_mode_read_write)
1425     throw Exception("Cannot write list chunk, file has to be opened in read+write mode");
1426    
1427 schoenebeck 780 // write all subchunks (including sub list chunks) recursively
1428     if (pSubChunks) {
1429 schoenebeck 2682 int i = 0;
1430     const int n = pSubChunks->size();
1431     for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) {
1432     // divide local progress into subprogress for loading current Instrument
1433     progress_t subprogress;
1434     __divide_progress(pProgress, &subprogress, n, i);
1435     // do the actual work
1436     ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset, &subprogress);
1437 schoenebeck 780 }
1438     }
1439    
1440     // update this list chunk's header
1441     CurrentChunkSize = NewChunkSize = ulWritePos - ulOriginalPos - LIST_HEADER_SIZE;
1442     WriteHeader(ulOriginalPos);
1443    
1444 schoenebeck 798 // offset of this list chunk in new written file may have changed
1445     ulStartPos = ulOriginalPos + LIST_HEADER_SIZE;
1446    
1447 schoenebeck 2682 __notify_progress(pProgress, 1.0); // notify done
1448    
1449 schoenebeck 780 return ulWritePos;
1450     }
1451    
1452     void List::__resetPos() {
1453     Chunk::__resetPos();
1454     if (pSubChunks) {
1455     for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) {
1456     (*iter)->__resetPos();
1457     }
1458     }
1459     }
1460    
1461 schoenebeck 11 /**
1462     * Returns string representation of the lists's id
1463     */
1464 schoenebeck 2 String List::GetListTypeString() {
1465     return convertToString(ListType);
1466     }
1467    
1468    
1469    
1470     // *************** File ***************
1471     // *
1472    
1473 schoenebeck 1953 //HACK: to avoid breaking DLL compatibility to older versions of libgig we roll the new std::set<Chunk*> into the old std::list<Chunk*> container, should be replaced on member variable level soon though
1474     #define _GET_RESIZED_CHUNKS() \
1475     (reinterpret_cast<std::set<Chunk*>*>(ResizedChunks.front()))
1476    
1477 schoenebeck 780 /** @brief Create new RIFF file.
1478     *
1479     * Use this constructor if you want to create a new RIFF file completely
1480     * "from scratch". Note: there must be no empty chunks or empty list
1481     * chunks when trying to make the new RIFF file persistent with Save()!
1482     *
1483 persson 1207 * Note: by default, the RIFF file will be saved in native endian
1484     * format; that is, as a RIFF file on little-endian machines and
1485     * as a RIFX file on big-endian. To change this behaviour, call
1486     * SetByteOrder() before calling Save().
1487     *
1488 schoenebeck 798 * @param FileType - four-byte identifier of the RIFF file type
1489 persson 1207 * @see AddSubChunk(), AddSubList(), SetByteOrder()
1490 schoenebeck 780 */
1491 schoenebeck 2543 File::File(uint32_t FileType)
1492     : List(this), bIsNewFile(true), Layout(layout_standard)
1493     {
1494 schoenebeck 1953 //HACK: see _GET_RESIZED_CHUNKS() comment
1495     ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>));
1496 schoenebeck 1050 #if defined(WIN32)
1497     hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1498     #else
1499 schoenebeck 780 hFileRead = hFileWrite = 0;
1500 schoenebeck 1050 #endif
1501 schoenebeck 798 Mode = stream_mode_closed;
1502 schoenebeck 780 bEndianNative = true;
1503     ulStartPos = RIFF_HEADER_SIZE;
1504 schoenebeck 798 ListType = FileType;
1505 schoenebeck 780 }
1506    
1507     /** @brief Load existing RIFF file.
1508     *
1509     * Loads an existing RIFF file with all its chunks.
1510     *
1511     * @param path - path and file name of the RIFF file to open
1512     * @throws RIFF::Exception if error occured while trying to load the
1513     * given RIFF file
1514     */
1515 schoenebeck 2543 File::File(const String& path)
1516     : List(this), Filename(path), bIsNewFile(false), Layout(layout_standard)
1517     {
1518 persson 2155 #if DEBUG
1519     std::cout << "File::File("<<path<<")" << std::endl;
1520     #endif // DEBUG
1521 schoenebeck 2543 bEndianNative = true;
1522 persson 2155 try {
1523 schoenebeck 2543 __openExistingFile(path);
1524 persson 2155 if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) {
1525     throw RIFF::Exception("Not a RIFF file");
1526     }
1527 schoenebeck 2 }
1528 persson 2155 catch (...) {
1529     Cleanup();
1530     throw;
1531 schoenebeck 1050 }
1532 schoenebeck 2 }
1533    
1534 schoenebeck 2543 /** @brief Load existing RIFF-like file.
1535     *
1536     * Loads an existing file, which is not a "real" RIFF file, but similar to
1537     * an ordinary RIFF file.
1538     *
1539     * A "real" RIFF file contains at top level a List chunk either with chunk
1540     * ID "RIFF" or "RIFX". The simple constructor above expects this to be
1541     * case, and if it finds the toplevel List chunk to have another chunk ID
1542     * than one of those two expected ones, it would throw an Exception and
1543     * would refuse to load the file accordingly.
1544     *
1545     * Since there are however a lot of file formats which use the same simple
1546     * principles of the RIFF format, with another toplevel List chunk ID
1547     * though, you can use this alternative constructor here to be able to load
1548     * and handle those files in the same way as you would do with "real" RIFF
1549     * files.
1550     *
1551     * @param path - path and file name of the RIFF-alike file to be opened
1552     * @param FileType - expected toplevel List chunk ID (this is the very
1553     * first chunk found in the file)
1554     * @param Endian - whether the file uses little endian or big endian layout
1555     * @param layout - general file structure type
1556     * @throws RIFF::Exception if error occured while trying to load the
1557     * given RIFF-alike file
1558     */
1559     File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout)
1560     : List(this), Filename(path), bIsNewFile(false), Layout(layout)
1561     {
1562     SetByteOrder(Endian);
1563     try {
1564     __openExistingFile(path, &FileType);
1565     }
1566     catch (...) {
1567     Cleanup();
1568     throw;
1569     }
1570     }
1571    
1572     /**
1573     * Opens an already existing RIFF file or RIFF-alike file. This method
1574     * shall only be called once (in a File class constructor).
1575     *
1576     * @param path - path and file name of the RIFF file or RIFF-alike file to
1577     * be opened
1578     * @param FileType - (optional) expected chunk ID of first chunk in file
1579     * @throws RIFF::Exception if error occured while trying to load the
1580     * given RIFF file or RIFF-alike file
1581     */
1582     void File::__openExistingFile(const String& path, uint32_t* FileType) {
1583     //HACK: see _GET_RESIZED_CHUNKS() comment
1584     ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>));
1585     #if POSIX
1586     hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK);
1587 schoenebeck 2675 if (hFileRead == -1) {
1588 schoenebeck 2543 hFileRead = hFileWrite = 0;
1589 schoenebeck 2675 String sError = strerror(errno);
1590     throw RIFF::Exception("Can't open \"" + path + "\": " + sError);
1591 schoenebeck 2543 }
1592     #elif defined(WIN32)
1593     hFileRead = hFileWrite = CreateFile(
1594     path.c_str(), GENERIC_READ,
1595     FILE_SHARE_READ | FILE_SHARE_WRITE,
1596     NULL, OPEN_EXISTING,
1597     FILE_ATTRIBUTE_NORMAL |
1598     FILE_FLAG_RANDOM_ACCESS, NULL
1599     );
1600     if (hFileRead == INVALID_HANDLE_VALUE) {
1601     hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1602     throw RIFF::Exception("Can't open \"" + path + "\"");
1603     }
1604     #else
1605     hFileRead = hFileWrite = fopen(path.c_str(), "rb");
1606     if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\"");
1607     #endif // POSIX
1608     Mode = stream_mode_read;
1609     switch (Layout) {
1610     case layout_standard: // this is a normal RIFF file
1611     ulStartPos = RIFF_HEADER_SIZE;
1612     ReadHeader(0);
1613     if (FileType && ChunkID != *FileType)
1614     throw RIFF::Exception("Invalid file container ID");
1615     break;
1616     case layout_flat: // non-standard RIFF-alike file
1617     ulStartPos = 0;
1618     NewChunkSize = CurrentChunkSize = GetFileSize();
1619     if (FileType) {
1620     uint32_t ckid;
1621     if (Read(&ckid, 4, 1) != 4) {
1622     throw RIFF::Exception("Invalid file header ID (premature end of header)");
1623     } else if (ckid != *FileType) {
1624     String s = " (expected '" + convertToString(*FileType) + "' but got '" + convertToString(ckid) + "')";
1625     throw RIFF::Exception("Invalid file header ID" + s);
1626     }
1627     SetPos(0); // reset to first byte of file
1628     }
1629     LoadSubChunks();
1630     break;
1631     }
1632     }
1633    
1634 schoenebeck 780 String File::GetFileName() {
1635     return Filename;
1636     }
1637 schoenebeck 2482
1638     void File::SetFileName(const String& path) {
1639     Filename = path;
1640     }
1641 schoenebeck 780
1642     stream_mode_t File::GetMode() {
1643     return Mode;
1644     }
1645    
1646 schoenebeck 2543 layout_t File::GetLayout() const {
1647     return Layout;
1648     }
1649    
1650 schoenebeck 780 /** @brief Change file access mode.
1651     *
1652     * Changes files access mode either to read-only mode or to read/write
1653     * mode.
1654     *
1655     * @param NewMode - new file access mode
1656     * @returns true if mode was changed, false if current mode already
1657     * equals new mode
1658     * @throws RIFF::Exception if new file access mode is unknown
1659     */
1660     bool File::SetMode(stream_mode_t NewMode) {
1661     if (NewMode != Mode) {
1662     switch (NewMode) {
1663     case stream_mode_read:
1664     #if POSIX
1665     if (hFileRead) close(hFileRead);
1666     hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);
1667 schoenebeck 2675 if (hFileRead == -1) {
1668 schoenebeck 780 hFileRead = hFileWrite = 0;
1669 schoenebeck 2675 String sError = strerror(errno);
1670     throw Exception("Could not (re)open file \"" + Filename + "\" in read mode: " + sError);
1671 schoenebeck 780 }
1672 schoenebeck 1050 #elif defined(WIN32)
1673     if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1674     hFileRead = hFileWrite = CreateFile(
1675     Filename.c_str(), GENERIC_READ,
1676     FILE_SHARE_READ | FILE_SHARE_WRITE,
1677     NULL, OPEN_EXISTING,
1678 schoenebeck 1459 FILE_ATTRIBUTE_NORMAL |
1679     FILE_FLAG_RANDOM_ACCESS,
1680     NULL
1681 schoenebeck 1050 );
1682     if (hFileRead == INVALID_HANDLE_VALUE) {
1683     hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1684     throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
1685     }
1686 schoenebeck 780 #else
1687     if (hFileRead) fclose(hFileRead);
1688 schoenebeck 1050 hFileRead = hFileWrite = fopen(Filename.c_str(), "rb");
1689 schoenebeck 780 if (!hFileRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode");
1690     #endif
1691     __resetPos(); // reset read/write position of ALL 'Chunk' objects
1692 schoenebeck 798 break;
1693 schoenebeck 780 case stream_mode_read_write:
1694     #if POSIX
1695     if (hFileRead) close(hFileRead);
1696     hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK);
1697 schoenebeck 2675 if (hFileRead == -1) {
1698 schoenebeck 780 hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK);
1699 schoenebeck 2675 String sError = strerror(errno);
1700     throw Exception("Could not open file \"" + Filename + "\" in read+write mode: " + sError);
1701 schoenebeck 780 }
1702 schoenebeck 1050 #elif defined(WIN32)
1703     if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1704     hFileRead = hFileWrite = CreateFile(
1705     Filename.c_str(),
1706     GENERIC_READ | GENERIC_WRITE,
1707     FILE_SHARE_READ,
1708     NULL, OPEN_ALWAYS,
1709 schoenebeck 1459 FILE_ATTRIBUTE_NORMAL |
1710     FILE_FLAG_RANDOM_ACCESS,
1711     NULL
1712 schoenebeck 1050 );
1713     if (hFileRead == INVALID_HANDLE_VALUE) {
1714     hFileRead = hFileWrite = CreateFile(
1715     Filename.c_str(), GENERIC_READ,
1716     FILE_SHARE_READ | FILE_SHARE_WRITE,
1717     NULL, OPEN_EXISTING,
1718 schoenebeck 1459 FILE_ATTRIBUTE_NORMAL |
1719     FILE_FLAG_RANDOM_ACCESS,
1720     NULL
1721 schoenebeck 1050 );
1722 persson 1301 throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode");
1723 schoenebeck 1050 }
1724 schoenebeck 780 #else
1725     if (hFileRead) fclose(hFileRead);
1726 schoenebeck 1050 hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b");
1727 schoenebeck 780 if (!hFileRead) {
1728 schoenebeck 1050 hFileRead = hFileWrite = fopen(Filename.c_str(), "rb");
1729 schoenebeck 780 throw Exception("Could not open file \"" + Filename + "\" in read+write mode");
1730     }
1731     #endif
1732     __resetPos(); // reset read/write position of ALL 'Chunk' objects
1733 schoenebeck 798 break;
1734     case stream_mode_closed:
1735     #if POSIX
1736     if (hFileRead) close(hFileRead);
1737     if (hFileWrite) close(hFileWrite);
1738 schoenebeck 1050 #elif defined(WIN32)
1739     if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1740     if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite);
1741 schoenebeck 798 #else
1742     if (hFileRead) fclose(hFileRead);
1743     if (hFileWrite) fclose(hFileWrite);
1744     #endif
1745     hFileRead = hFileWrite = 0;
1746     break;
1747 schoenebeck 780 default:
1748     throw Exception("Unknown file access mode");
1749     }
1750 schoenebeck 798 Mode = NewMode;
1751     return true;
1752 schoenebeck 780 }
1753     return false;
1754     }
1755    
1756 persson 1184 /** @brief Set the byte order to be used when saving.
1757     *
1758     * Set the byte order to be used in the file. A value of
1759     * endian_little will create a RIFF file, endian_big a RIFX file
1760     * and endian_native will create a RIFF file on little-endian
1761     * machines and RIFX on big-endian machines.
1762     *
1763     * @param Endian - endianess to use when file is saved.
1764     */
1765     void File::SetByteOrder(endian_t Endian) {
1766     #if WORDS_BIGENDIAN
1767     bEndianNative = Endian != endian_little;
1768     #else
1769     bEndianNative = Endian != endian_big;
1770     #endif
1771     }
1772    
1773 schoenebeck 780 /** @brief Save changes to same file.
1774     *
1775     * Make all changes of all chunks persistent by writing them to the
1776     * actual (same) file. The file might temporarily grow to a higher size
1777     * than it will have at the end of the saving process, in case chunks
1778     * were grown.
1779     *
1780 schoenebeck 2682 * @param pProgress - optional: callback function for progress notification
1781 schoenebeck 780 * @throws RIFF::Exception if there is an empty chunk or empty list
1782     * chunk or any kind of IO error occured
1783     */
1784 schoenebeck 2682 void File::Save(progress_t* pProgress) {
1785 schoenebeck 2543 //TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files)
1786     if (Layout == layout_flat)
1787     throw Exception("Saving a RIFF file with layout_flat is not implemented yet");
1788    
1789 schoenebeck 833 // make sure the RIFF tree is built (from the original file)
1790 schoenebeck 2682 {
1791     // divide progress into subprogress
1792     progress_t subprogress;
1793     __divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress
1794     // do the actual work
1795     LoadSubChunksRecursively(&subprogress);
1796     // notify subprogress done
1797     __notify_progress(&subprogress, 1.f);
1798     }
1799 schoenebeck 833
1800 schoenebeck 780 // reopen file in write mode
1801 schoenebeck 798 SetMode(stream_mode_read_write);
1802 schoenebeck 780
1803     // to be able to save the whole file without loading everything into
1804     // RAM and without having to store the data in a temporary file, we
1805     // enlarge the file with the sum of all _positive_ chunk size
1806     // changes, move current data towards the end of the file with the
1807     // calculated sum and finally update / rewrite the file by copying
1808     // the old data back to the right position at the beginning of the file
1809    
1810     // first we sum up all positive chunk size changes (and skip all negative ones)
1811     unsigned long ulPositiveSizeDiff = 0;
1812 schoenebeck 1953 std::set<Chunk*>* resizedChunks = _GET_RESIZED_CHUNKS();
1813     for (std::set<Chunk*>::const_iterator iter = resizedChunks->begin(), end = resizedChunks->end(); iter != end; ++iter) {
1814 schoenebeck 1095 if ((*iter)->GetNewSize() == 0) {
1815 schoenebeck 1105 throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter));
1816 schoenebeck 1095 }
1817 persson 1859 unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2;
1818     unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2;
1819     if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded;
1820 schoenebeck 780 }
1821    
1822     unsigned long ulWorkingFileSize = GetFileSize();
1823    
1824     // if there are positive size changes...
1825     if (ulPositiveSizeDiff > 0) {
1826 schoenebeck 2682 // divide progress into subprogress
1827     progress_t subprogress;
1828     __divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress
1829    
1830 schoenebeck 780 // ... we enlarge this file first ...
1831     ulWorkingFileSize += ulPositiveSizeDiff;
1832     ResizeFile(ulWorkingFileSize);
1833     // ... and move current data by the same amount towards end of file.
1834     int8_t* pCopyBuffer = new int8_t[4096];
1835     const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE;
1836 schoenebeck 1050 #if defined(WIN32)
1837     DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
1838     #else
1839 schoenebeck 1459 int iBytesMoved = 1;
1840 schoenebeck 1050 #endif
1841 schoenebeck 2682 for (unsigned long ulPos = ulFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) {
1842 persson 1678 iBytesMoved = (ulPos < 4096) ? ulPos : 4096;
1843     ulPos -= iBytesMoved;
1844 schoenebeck 780 #if POSIX
1845     lseek(hFileRead, ulPos, SEEK_SET);
1846     iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved);
1847     lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET);
1848     iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved);
1849 schoenebeck 1050 #elif defined(WIN32)
1850     SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN);
1851     ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
1852     SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN);
1853     WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
1854 schoenebeck 780 #else
1855     fseek(hFileRead, ulPos, SEEK_SET);
1856     iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead);
1857     fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET);
1858     iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite);
1859     #endif
1860 schoenebeck 2682 if (!(iNotif % 8) && iBytesMoved > 0)
1861     __notify_progress(&subprogress, float(ulFileSize - ulPos) / float(ulFileSize));
1862 schoenebeck 780 }
1863     delete[] pCopyBuffer;
1864     if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it");
1865 schoenebeck 2682
1866     __notify_progress(&subprogress, 1.f); // notify subprogress done
1867 schoenebeck 780 }
1868    
1869 schoenebeck 2682 // rebuild / rewrite complete RIFF tree ...
1870    
1871     // divide progress into subprogress
1872     progress_t subprogress;
1873     __divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress
1874     // do the actual work
1875     unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff, &subprogress);
1876 schoenebeck 798 unsigned long ulActualSize = __GetFileSize(hFileWrite);
1877 schoenebeck 2682 // notify subprogress done
1878     __notify_progress(&subprogress, 1.f);
1879 schoenebeck 780
1880     // resize file to the final size
1881 schoenebeck 798 if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);
1882 schoenebeck 780
1883     // forget all resized chunks
1884 schoenebeck 1953 resizedChunks->clear();
1885 schoenebeck 2682
1886     __notify_progress(pProgress, 1.0); // notify done
1887 schoenebeck 780 }
1888    
1889     /** @brief Save changes to another file.
1890     *
1891     * Make all changes of all chunks persistent by writing them to another
1892     * file. <b>Caution:</b> this method is optimized for writing to
1893     * <b>another</b> file, do not use it to save the changes to the same
1894 schoenebeck 798 * file! Use File::Save() in that case instead! Ignoring this might
1895     * result in a corrupted file, especially in case chunks were resized!
1896 schoenebeck 780 *
1897     * After calling this method, this File object will be associated with
1898     * the new file (given by \a path) afterwards.
1899     *
1900     * @param path - path and file name where everything should be written to
1901 schoenebeck 2682 * @param pProgress - optional: callback function for progress notification
1902 schoenebeck 780 */
1903 schoenebeck 2682 void File::Save(const String& path, progress_t* pProgress) {
1904 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
1905    
1906 schoenebeck 2543 //TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files)
1907     if (Layout == layout_flat)
1908     throw Exception("Saving a RIFF file with layout_flat is not implemented yet");
1909    
1910 schoenebeck 833 // make sure the RIFF tree is built (from the original file)
1911 schoenebeck 2682 {
1912     // divide progress into subprogress
1913     progress_t subprogress;
1914     __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress
1915     // do the actual work
1916     LoadSubChunksRecursively(&subprogress);
1917     // notify subprogress done
1918     __notify_progress(&subprogress, 1.f);
1919     }
1920 schoenebeck 833
1921 schoenebeck 2482 if (!bIsNewFile) SetMode(stream_mode_read);
1922 schoenebeck 780 // open the other (new) file for writing and truncate it to zero size
1923     #if POSIX
1924 schoenebeck 798 hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP);
1925 schoenebeck 2675 if (hFileWrite == -1) {
1926 schoenebeck 780 hFileWrite = hFileRead;
1927 schoenebeck 2675 String sError = strerror(errno);
1928     throw Exception("Could not open file \"" + path + "\" for writing: " + sError);
1929 schoenebeck 780 }
1930 schoenebeck 1050 #elif defined(WIN32)
1931     hFileWrite = CreateFile(
1932     path.c_str(), GENERIC_WRITE, FILE_SHARE_READ,
1933 schoenebeck 1459 NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL |
1934     FILE_FLAG_RANDOM_ACCESS, NULL
1935 schoenebeck 1050 );
1936     if (hFileWrite == INVALID_HANDLE_VALUE) {
1937     hFileWrite = hFileRead;
1938     throw Exception("Could not open file \"" + path + "\" for writing");
1939     }
1940 schoenebeck 780 #else
1941     hFileWrite = fopen(path.c_str(), "w+b");
1942     if (!hFileWrite) {
1943     hFileWrite = hFileRead;
1944     throw Exception("Could not open file \"" + path + "\" for writing");
1945     }
1946     #endif // POSIX
1947 schoenebeck 798 Mode = stream_mode_read_write;
1948 schoenebeck 780
1949     // write complete RIFF tree to the other (new) file
1950 schoenebeck 2682 unsigned long ulTotalSize;
1951     {
1952     // divide progress into subprogress
1953     progress_t subprogress;
1954     __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress
1955     // do the actual work
1956     ulTotalSize = WriteChunk(0, 0, &subprogress);
1957     // notify subprogress done
1958     __notify_progress(&subprogress, 1.f);
1959     }
1960 schoenebeck 798 unsigned long ulActualSize = __GetFileSize(hFileWrite);
1961 schoenebeck 780
1962 schoenebeck 798 // resize file to the final size (if the file was originally larger)
1963     if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize);
1964    
1965 schoenebeck 780 // forget all resized chunks
1966 schoenebeck 1953 _GET_RESIZED_CHUNKS()->clear();
1967 schoenebeck 780
1968 persson 1301 #if POSIX
1969     if (hFileWrite) close(hFileWrite);
1970     #elif defined(WIN32)
1971     if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite);
1972     #else
1973     if (hFileWrite) fclose(hFileWrite);
1974     #endif
1975     hFileWrite = hFileRead;
1976 schoenebeck 798
1977 schoenebeck 780 // associate new file with this File object from now on
1978     Filename = path;
1979 schoenebeck 2482 bIsNewFile = false;
1980 schoenebeck 798 Mode = (stream_mode_t) -1; // Just set it to an undefined mode ...
1981     SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles.
1982 schoenebeck 2682
1983     __notify_progress(pProgress, 1.0); // notify done
1984 schoenebeck 780 }
1985    
1986     void File::ResizeFile(unsigned long ulNewSize) {
1987     #if POSIX
1988     if (ftruncate(hFileWrite, ulNewSize) < 0)
1989     throw Exception("Could not resize file \"" + Filename + "\"");
1990 schoenebeck 1050 #elif defined(WIN32)
1991     if (
1992     SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER ||
1993     !SetEndOfFile(hFileWrite)
1994     ) throw Exception("Could not resize file \"" + Filename + "\"");
1995 schoenebeck 780 #else
1996 schoenebeck 1050 # error Sorry, this version of libgig only supports POSIX and Windows systems yet.
1997 schoenebeck 798 # error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)!
1998 schoenebeck 780 #endif
1999     }
2000    
2001 schoenebeck 2 File::~File() {
2002     #if DEBUG
2003     std::cout << "File::~File()" << std::endl;
2004     #endif // DEBUG
2005 persson 2155 Cleanup();
2006     }
2007 schoenebeck 2482
2008     /**
2009     * Returns @c true if this file has been created new from scratch and
2010     * has not been stored to disk yet.
2011     */
2012     bool File::IsNew() const {
2013     return bIsNewFile;
2014     }
2015 persson 2155
2016     void File::Cleanup() {
2017 schoenebeck 2 #if POSIX
2018 schoenebeck 780 if (hFileRead) close(hFileRead);
2019 schoenebeck 1050 #elif defined(WIN32)
2020     if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
2021 schoenebeck 2 #else
2022 schoenebeck 780 if (hFileRead) fclose(hFileRead);
2023 schoenebeck 2 #endif // POSIX
2024 persson 1869 DeleteChunkList();
2025     pFile = NULL;
2026 schoenebeck 1953 //HACK: see _GET_RESIZED_CHUNKS() comment
2027     delete _GET_RESIZED_CHUNKS();
2028 schoenebeck 2 }
2029    
2030 schoenebeck 780 void File::LogAsResized(Chunk* pResizedChunk) {
2031 schoenebeck 1953 _GET_RESIZED_CHUNKS()->insert(pResizedChunk);
2032 schoenebeck 780 }
2033    
2034 schoenebeck 1095 void File::UnlogResized(Chunk* pResizedChunk) {
2035 schoenebeck 1953 _GET_RESIZED_CHUNKS()->erase(pResizedChunk);
2036 schoenebeck 1095 }
2037    
2038 schoenebeck 2 unsigned long File::GetFileSize() {
2039 schoenebeck 798 return __GetFileSize(hFileRead);
2040     }
2041    
2042     #if POSIX
2043     unsigned long File::__GetFileSize(int hFile) {
2044 schoenebeck 2 struct stat filestat;
2045 schoenebeck 798 fstat(hFile, &filestat);
2046 schoenebeck 2 long size = filestat.st_size;
2047     return size;
2048     }
2049 schoenebeck 1050 #elif defined(WIN32)
2050     unsigned long File::__GetFileSize(HANDLE hFile) {
2051 schoenebeck 1063 DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/);
2052     if (dwSize == INVALID_FILE_SIZE)
2053     throw Exception("Windows FS error: could not determine file size");
2054     return dwSize;
2055 schoenebeck 1050 }
2056 schoenebeck 798 #else // standard C functions
2057     unsigned long File::__GetFileSize(FILE* hFile) {
2058     long curpos = ftell(hFile);
2059     fseek(hFile, 0, SEEK_END);
2060     long size = ftell(hFile);
2061     fseek(hFile, curpos, SEEK_SET);
2062     return size;
2063     }
2064     #endif
2065 schoenebeck 2
2066    
2067     // *************** Exception ***************
2068     // *
2069    
2070     void Exception::PrintMessage() {
2071     std::cout << "RIFF::Exception: " << Message << std::endl;
2072     }
2073    
2074 schoenebeck 518
2075     // *************** functions ***************
2076     // *
2077    
2078     /**
2079     * Returns the name of this C++ library. This is usually "libgig" of
2080     * course. This call is equivalent to DLS::libraryName() and
2081     * gig::libraryName().
2082     */
2083     String libraryName() {
2084     return PACKAGE;
2085     }
2086    
2087     /**
2088     * Returns version of this C++ library. This call is equivalent to
2089     * DLS::libraryVersion() and gig::libraryVersion().
2090     */
2091     String libraryVersion() {
2092     return VERSION;
2093     }
2094    
2095 schoenebeck 2 } // namespace RIFF

  ViewVC Help
Powered by ViewVC