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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2915 by schoenebeck, Tue May 17 19:22:17 2016 UTC revision 3488 by schoenebeck, Thu Feb 28 17:49:07 2019 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   libgig - C++ cross-platform Gigasampler format file access library    *   *   libgig - C++ cross-platform Gigasampler format file access library    *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003-2016 by Christian Schoenebeck                      *   *   Copyright (C) 2003-2019 by Christian Schoenebeck                      *
6   *                              <cuse@users.sourceforge.net>               *   *                              <cuse@users.sourceforge.net>               *
7   *                                                                         *   *                                                                         *
8   *   This library is free software; you can redistribute it and/or modify  *   *   This library is free software; you can redistribute it and/or modify  *
# Line 64  namespace RIFF { Line 64  namespace RIFF {
64          __range_max = 1.0f;          __range_max = 1.0f;
65      }      }
66    
67        /**
68         * Divides this progress task into the requested amount of equal weighted
69         * sub-progress tasks and returns a vector with those subprogress tasks.
70         *
71         * @param iSubtasks - total amount sub tasks this task should be subdivided
72         * @returns subtasks
73         */
74        std::vector<progress_t> progress_t::subdivide(int iSubtasks) {
75            std::vector<progress_t> v;
76            for (int i = 0; i < iSubtasks; ++i) {
77                progress_t p;
78                __divide_progress(this, &p, iSubtasks, i);
79                v.push_back(p);
80            }
81            return v;
82        }
83    
84        /**
85         * Divides this progress task into the requested amount of sub-progress
86         * tasks, where each one of those new sub-progress tasks is created with its
87         * requested individual weight / portion, and finally returns a vector
88         * with those new subprogress tasks.
89         *
90         * The amount of subprogresses to be created is determined by this method
91         * by calling @c vSubTaskPortions.size() .
92         *
93         * Example: consider you wanted to create 3 subprogresses where the 1st
94         * subtask should be assigned 10% of the new 3 subprogresses' overall
95         * progress, the 2nd subtask should be assigned 50% of the new 3
96         * subprogresses' overall progress, and the 3rd subtask should be assigned
97         * 40%, then you might call this method like this:
98         * @code
99         * std::vector<progress_t> subprogresses = progress.subdivide({0.1, 0.5, 0.4});
100         * @endcode
101         *
102         * @param vSubTaskPortions - amount and individual weight of subtasks to be
103         *                           created
104         * @returns subtasks
105         */
106        std::vector<progress_t> progress_t::subdivide(std::vector<float> vSubTaskPortions) {
107            float fTotal = 0.f; // usually 1.0, but we sum the portions up below to be sure
108            for (int i = 0; i < vSubTaskPortions.size(); ++i)
109                fTotal += vSubTaskPortions[i];
110    
111            float fLow = 0.f, fHigh = 0.f;
112            std::vector<progress_t> v;
113            for (int i = 0; i < vSubTaskPortions.size(); ++i) {
114                fLow  = fHigh;
115                fHigh = vSubTaskPortions[i];
116                progress_t p;
117                __divide_progress(this, &p, fTotal, fLow, fHigh);
118                v.push_back(p);
119            }
120            return v;
121        }
122    
123    
124    
125  // *************** Chunk **************  // *************** Chunk **************
126  // *  // *
127    
128      Chunk::Chunk(File* pFile) {      Chunk::Chunk(File* pFile) {
129          #if DEBUG          #if DEBUG_RIFF
130          std::cout << "Chunk::Chunk(File* pFile)" << std::endl;          std::cout << "Chunk::Chunk(File* pFile)" << std::endl;
131          #endif // DEBUG          #endif // DEBUG_RIFF
132          ullPos     = 0;          ullPos     = 0;
133          pParent    = NULL;          pParent    = NULL;
134          pChunkData = NULL;          pChunkData = NULL;
# Line 84  namespace RIFF { Line 140  namespace RIFF {
140      }      }
141    
142      Chunk::Chunk(File* pFile, file_offset_t StartPos, List* Parent) {      Chunk::Chunk(File* pFile, file_offset_t StartPos, List* Parent) {
143          #if DEBUG          #if DEBUG_RIFF
144          std::cout << "Chunk::Chunk(File*,file_offset_t,List*),StartPos=" << StartPos << std::endl;          std::cout << "Chunk::Chunk(File*,file_offset_t,List*),StartPos=" << StartPos << std::endl;
145          #endif // DEBUG          #endif // DEBUG_RIFF
146          this->pFile   = pFile;          this->pFile   = pFile;
147          ullStartPos   = StartPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);          ullStartPos   = StartPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
148          pParent       = Parent;          pParent       = Parent;
# Line 115  namespace RIFF { Line 171  namespace RIFF {
171      }      }
172    
173      void Chunk::ReadHeader(file_offset_t filePos) {      void Chunk::ReadHeader(file_offset_t filePos) {
174          #if DEBUG          #if DEBUG_RIFF
175          std::cout << "Chunk::Readheader(" << filePos << ") ";          std::cout << "Chunk::Readheader(" << filePos << ") ";
176          #endif // DEBUG          #endif // DEBUG_RIFF
177          ChunkID = 0;          ChunkID = 0;
178          ullNewChunkSize = ullCurrentChunkSize = 0;          ullNewChunkSize = ullCurrentChunkSize = 0;
179          #if POSIX          #if POSIX
# Line 153  namespace RIFF { Line 209  namespace RIFF {
209                  else                  else
210                      swapBytes_64(&ullCurrentChunkSize);                      swapBytes_64(&ullCurrentChunkSize);
211              }              }
212              #if DEBUG              #if DEBUG_RIFF
213              std::cout << "ckID=" << convertToString(ChunkID) << " ";              std::cout << "ckID=" << convertToString(ChunkID) << " ";
214              std::cout << "ckSize=" << ullCurrentChunkSize << " ";              std::cout << "ckSize=" << ullCurrentChunkSize << " ";
215              std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl;              std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl;
216              #endif // DEBUG              #endif // DEBUG_RIFF
217              ullNewChunkSize = ullCurrentChunkSize;              ullNewChunkSize = ullCurrentChunkSize;
218          }          }
219      }      }
# Line 222  namespace RIFF { Line 278  namespace RIFF {
278       *                  data       *                  data
279       */       */
280      file_offset_t Chunk::SetPos(file_offset_t Where, stream_whence_t Whence) {      file_offset_t Chunk::SetPos(file_offset_t Where, stream_whence_t Whence) {
281          #if DEBUG          #if DEBUG_RIFF
282          std::cout << "Chunk::SetPos(file_offset_t,stream_whence_t)" << std::endl;          std::cout << "Chunk::SetPos(file_offset_t,stream_whence_t)" << std::endl;
283          #endif // DEBUG          #endif // DEBUG_RIFF
284          switch (Whence) {          switch (Whence) {
285              case stream_curpos:              case stream_curpos:
286                  ullPos += Where;                  ullPos += Where;
# Line 254  namespace RIFF { Line 310  namespace RIFF {
310       *  @returns  number of bytes left to read       *  @returns  number of bytes left to read
311       */       */
312      file_offset_t Chunk::RemainingBytes() const {      file_offset_t Chunk::RemainingBytes() const {
313          #if DEBUG          #if DEBUG_RIFF
314          std::cout << "Chunk::Remainingbytes()=" << ullCurrentChunkSize - ullPos << std::endl;          std::cout << "Chunk::Remainingbytes()=" << ullCurrentChunkSize - ullPos << std::endl;
315          #endif // DEBUG          #endif // DEBUG_RIFF
316          return (ullCurrentChunkSize > ullPos) ? ullCurrentChunkSize - ullPos : 0;          return (ullCurrentChunkSize > ullPos) ? ullCurrentChunkSize - ullPos : 0;
317      }      }
318    
# Line 285  namespace RIFF { Line 341  namespace RIFF {
341       *    possible without SetPos()       *    possible without SetPos()
342       */       */
343      stream_state_t Chunk::GetState() const {      stream_state_t Chunk::GetState() const {
344          #if DEBUG          #if DEBUG_RIFF
345          std::cout << "Chunk::GetState()" << std::endl;          std::cout << "Chunk::GetState()" << std::endl;
346          #endif // DEBUG          #endif // DEBUG_RIFF
347          #if POSIX          #if POSIX
348          if (pFile->hFileRead == 0) return stream_closed;          if (pFile->hFileRead == 0) return stream_closed;
349          #elif defined (WIN32)          #elif defined (WIN32)
# Line 313  namespace RIFF { Line 369  namespace RIFF {
369       *  @param WordCount  number of data words to read       *  @param WordCount  number of data words to read
370       *  @param WordSize   size of each data word to read       *  @param WordSize   size of each data word to read
371       *  @returns          number of successfully read data words or 0 if end       *  @returns          number of successfully read data words or 0 if end
372       *                    of file reached or error occured       *                    of file reached or error occurred
373       */       */
374      file_offset_t Chunk::Read(void* pData, file_offset_t WordCount, file_offset_t WordSize) {      file_offset_t Chunk::Read(void* pData, file_offset_t WordCount, file_offset_t WordSize) {
375          #if DEBUG          #if DEBUG_RIFF
376          std::cout << "Chunk::Read(void*,file_offset_t,file_offset_t)" << std::endl;          std::cout << "Chunk::Read(void*,file_offset_t,file_offset_t)" << std::endl;
377          #endif // DEBUG          #endif // DEBUG_RIFF
378          //if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet)          //if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet)
379          if (ullPos >= ullCurrentChunkSize) return 0;          if (ullPos >= ullCurrentChunkSize) return 0;
380          if (ullPos + WordCount * WordSize >= ullCurrentChunkSize) WordCount = (ullCurrentChunkSize - ullPos) / WordSize;          if (ullPos + WordCount * WordSize >= ullCurrentChunkSize) WordCount = (ullCurrentChunkSize - ullPos) / WordSize;
# Line 326  namespace RIFF { Line 382  namespace RIFF {
382          if (lseek(pFile->hFileRead, ullStartPos + ullPos, SEEK_SET) < 0) return 0;          if (lseek(pFile->hFileRead, ullStartPos + ullPos, SEEK_SET) < 0) return 0;
383          ssize_t readWords = read(pFile->hFileRead, pData, WordCount * WordSize);          ssize_t readWords = read(pFile->hFileRead, pData, WordCount * WordSize);
384          if (readWords < 1) {          if (readWords < 1) {
385              #if DEBUG              #if DEBUG_RIFF
386              std::cerr << "POSIX read() failed: " << strerror(errno) << std::endl << std::flush;              std::cerr << "POSIX read() failed: " << strerror(errno) << std::endl << std::flush;
387              #endif // DEBUG              #endif // DEBUG_RIFF
388              return 0;              return 0;
389          }          }
390          readWords /= WordSize;          readWords /= WordSize;
# Line 382  namespace RIFF { Line 438  namespace RIFF {
438       *  @param WordSize   size of each data word to write       *  @param WordSize   size of each data word to write
439       *  @returns          number of successfully written data words       *  @returns          number of successfully written data words
440       *  @throws RIFF::Exception  if write operation would exceed current       *  @throws RIFF::Exception  if write operation would exceed current
441       *                           chunk size or any IO error occured       *                           chunk size or any IO error occurred
442       *  @see Resize()       *  @see Resize()
443       */       */
444      file_offset_t Chunk::Write(void* pData, file_offset_t WordCount, file_offset_t WordSize) {      file_offset_t Chunk::Write(void* pData, file_offset_t WordCount, file_offset_t WordSize) {
# Line 455  namespace RIFF { Line 511  namespace RIFF {
511       * @param pData             destination buffer       * @param pData             destination buffer
512       * @param WordCount         number of 8 Bit signed integers to read       * @param WordCount         number of 8 Bit signed integers to read
513       * @returns                 number of read integers       * @returns                 number of read integers
514       * @throws RIFF::Exception  if an error occured or less than       * @throws RIFF::Exception  if an error occurred or less than
515       *                          \a WordCount integers could be read!       *                          \a WordCount integers could be read!
516       */       */
517      file_offset_t Chunk::ReadInt8(int8_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::ReadInt8(int8_t* pData, file_offset_t WordCount) {
518          #if DEBUG          #if DEBUG_RIFF
519          std::cout << "Chunk::ReadInt8(int8_t*,file_offset_t)" << std::endl;          std::cout << "Chunk::ReadInt8(int8_t*,file_offset_t)" << std::endl;
520          #endif // DEBUG          #endif // DEBUG_RIFF
521          return ReadSceptical(pData, WordCount, 1);          return ReadSceptical(pData, WordCount, 1);
522      }      }
523    
# Line 476  namespace RIFF { Line 532  namespace RIFF {
532       * @param pData             source buffer (containing the data)       * @param pData             source buffer (containing the data)
533       * @param WordCount         number of 8 Bit signed integers to write       * @param WordCount         number of 8 Bit signed integers to write
534       * @returns                 number of written integers       * @returns                 number of written integers
535       * @throws RIFF::Exception  if an IO error occured       * @throws RIFF::Exception  if an IO error occurred
536       * @see Resize()       * @see Resize()
537       */       */
538      file_offset_t Chunk::WriteInt8(int8_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::WriteInt8(int8_t* pData, file_offset_t WordCount) {
# Line 492  namespace RIFF { Line 548  namespace RIFF {
548       * @param pData             destination buffer       * @param pData             destination buffer
549       * @param WordCount         number of 8 Bit unsigned integers to read       * @param WordCount         number of 8 Bit unsigned integers to read
550       * @returns                 number of read integers       * @returns                 number of read integers
551       * @throws RIFF::Exception  if an error occured or less than       * @throws RIFF::Exception  if an error occurred or less than
552       *                          \a WordCount integers could be read!       *                          \a WordCount integers could be read!
553       */       */
554      file_offset_t Chunk::ReadUint8(uint8_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::ReadUint8(uint8_t* pData, file_offset_t WordCount) {
555          #if DEBUG          #if DEBUG_RIFF
556          std::cout << "Chunk::ReadUint8(uint8_t*,file_offset_t)" << std::endl;          std::cout << "Chunk::ReadUint8(uint8_t*,file_offset_t)" << std::endl;
557          #endif // DEBUG          #endif // DEBUG_RIFF
558          return ReadSceptical(pData, WordCount, 1);          return ReadSceptical(pData, WordCount, 1);
559      }      }
560    
# Line 513  namespace RIFF { Line 569  namespace RIFF {
569       * @param pData             source buffer (containing the data)       * @param pData             source buffer (containing the data)
570       * @param WordCount         number of 8 Bit unsigned integers to write       * @param WordCount         number of 8 Bit unsigned integers to write
571       * @returns                 number of written integers       * @returns                 number of written integers
572       * @throws RIFF::Exception  if an IO error occured       * @throws RIFF::Exception  if an IO error occurred
573       * @see Resize()       * @see Resize()
574       */       */
575      file_offset_t Chunk::WriteUint8(uint8_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::WriteUint8(uint8_t* pData, file_offset_t WordCount) {
# Line 529  namespace RIFF { Line 585  namespace RIFF {
585       * @param pData             destination buffer       * @param pData             destination buffer
586       * @param WordCount         number of 16 Bit signed integers to read       * @param WordCount         number of 16 Bit signed integers to read
587       * @returns                 number of read integers       * @returns                 number of read integers
588       * @throws RIFF::Exception  if an error occured or less than       * @throws RIFF::Exception  if an error occurred or less than
589       *                          \a WordCount integers could be read!       *                          \a WordCount integers could be read!
590       */       */
591      file_offset_t Chunk::ReadInt16(int16_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::ReadInt16(int16_t* pData, file_offset_t WordCount) {
592          #if DEBUG          #if DEBUG_RIFF
593          std::cout << "Chunk::ReadInt16(int16_t*,file_offset_t)" << std::endl;          std::cout << "Chunk::ReadInt16(int16_t*,file_offset_t)" << std::endl;
594          #endif // DEBUG          #endif // DEBUG_RIFF
595          return ReadSceptical(pData, WordCount, 2);          return ReadSceptical(pData, WordCount, 2);
596      }      }
597    
# Line 550  namespace RIFF { Line 606  namespace RIFF {
606       * @param pData             source buffer (containing the data)       * @param pData             source buffer (containing the data)
607       * @param WordCount         number of 16 Bit signed integers to write       * @param WordCount         number of 16 Bit signed integers to write
608       * @returns                 number of written integers       * @returns                 number of written integers
609       * @throws RIFF::Exception  if an IO error occured       * @throws RIFF::Exception  if an IO error occurred
610       * @see Resize()       * @see Resize()
611       */       */
612      file_offset_t Chunk::WriteInt16(int16_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::WriteInt16(int16_t* pData, file_offset_t WordCount) {
# Line 566  namespace RIFF { Line 622  namespace RIFF {
622       * @param pData             destination buffer       * @param pData             destination buffer
623       * @param WordCount         number of 8 Bit unsigned integers to read       * @param WordCount         number of 8 Bit unsigned integers to read
624       * @returns                 number of read integers       * @returns                 number of read integers
625       * @throws RIFF::Exception  if an error occured or less than       * @throws RIFF::Exception  if an error occurred or less than
626       *                          \a WordCount integers could be read!       *                          \a WordCount integers could be read!
627       */       */
628      file_offset_t Chunk::ReadUint16(uint16_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::ReadUint16(uint16_t* pData, file_offset_t WordCount) {
629          #if DEBUG          #if DEBUG_RIFF
630          std::cout << "Chunk::ReadUint16(uint16_t*,file_offset_t)" << std::endl;          std::cout << "Chunk::ReadUint16(uint16_t*,file_offset_t)" << std::endl;
631          #endif // DEBUG          #endif // DEBUG_RIFF
632          return ReadSceptical(pData, WordCount, 2);          return ReadSceptical(pData, WordCount, 2);
633      }      }
634    
# Line 587  namespace RIFF { Line 643  namespace RIFF {
643       * @param pData             source buffer (containing the data)       * @param pData             source buffer (containing the data)
644       * @param WordCount         number of 16 Bit unsigned integers to write       * @param WordCount         number of 16 Bit unsigned integers to write
645       * @returns                 number of written integers       * @returns                 number of written integers
646       * @throws RIFF::Exception  if an IO error occured       * @throws RIFF::Exception  if an IO error occurred
647       * @see Resize()       * @see Resize()
648       */       */
649      file_offset_t Chunk::WriteUint16(uint16_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::WriteUint16(uint16_t* pData, file_offset_t WordCount) {
# Line 603  namespace RIFF { Line 659  namespace RIFF {
659       * @param pData             destination buffer       * @param pData             destination buffer
660       * @param WordCount         number of 32 Bit signed integers to read       * @param WordCount         number of 32 Bit signed integers to read
661       * @returns                 number of read integers       * @returns                 number of read integers
662       * @throws RIFF::Exception  if an error occured or less than       * @throws RIFF::Exception  if an error occurred or less than
663       *                          \a WordCount integers could be read!       *                          \a WordCount integers could be read!
664       */       */
665      file_offset_t Chunk::ReadInt32(int32_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::ReadInt32(int32_t* pData, file_offset_t WordCount) {
666          #if DEBUG          #if DEBUG_RIFF
667          std::cout << "Chunk::ReadInt32(int32_t*,file_offset_t)" << std::endl;          std::cout << "Chunk::ReadInt32(int32_t*,file_offset_t)" << std::endl;
668          #endif // DEBUG          #endif // DEBUG_RIFF
669          return ReadSceptical(pData, WordCount, 4);          return ReadSceptical(pData, WordCount, 4);
670      }      }
671    
# Line 624  namespace RIFF { Line 680  namespace RIFF {
680       * @param pData             source buffer (containing the data)       * @param pData             source buffer (containing the data)
681       * @param WordCount         number of 32 Bit signed integers to write       * @param WordCount         number of 32 Bit signed integers to write
682       * @returns                 number of written integers       * @returns                 number of written integers
683       * @throws RIFF::Exception  if an IO error occured       * @throws RIFF::Exception  if an IO error occurred
684       * @see Resize()       * @see Resize()
685       */       */
686      file_offset_t Chunk::WriteInt32(int32_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::WriteInt32(int32_t* pData, file_offset_t WordCount) {
# Line 640  namespace RIFF { Line 696  namespace RIFF {
696       * @param pData             destination buffer       * @param pData             destination buffer
697       * @param WordCount         number of 32 Bit unsigned integers to read       * @param WordCount         number of 32 Bit unsigned integers to read
698       * @returns                 number of read integers       * @returns                 number of read integers
699       * @throws RIFF::Exception  if an error occured or less than       * @throws RIFF::Exception  if an error occurred or less than
700       *                          \a WordCount integers could be read!       *                          \a WordCount integers could be read!
701       */       */
702      file_offset_t Chunk::ReadUint32(uint32_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::ReadUint32(uint32_t* pData, file_offset_t WordCount) {
703          #if DEBUG          #if DEBUG_RIFF
704          std::cout << "Chunk::ReadUint32(uint32_t*,file_offset_t)" << std::endl;          std::cout << "Chunk::ReadUint32(uint32_t*,file_offset_t)" << std::endl;
705          #endif // DEBUG          #endif // DEBUG_RIFF
706          return ReadSceptical(pData, WordCount, 4);          return ReadSceptical(pData, WordCount, 4);
707      }      }
708    
# Line 657  namespace RIFF { Line 713  namespace RIFF {
713       *       *
714       * @param s                 destination string       * @param s                 destination string
715       * @param size              number of characters to read       * @param size              number of characters to read
716       * @throws RIFF::Exception  if an error occured or less than       * @throws RIFF::Exception  if an error occurred or less than
717       *                          \a size characters could be read!       *                          \a size characters could be read!
718       */       */
719      void Chunk::ReadString(String& s, int size) {      void Chunk::ReadString(String& s, int size) {
# Line 678  namespace RIFF { Line 734  namespace RIFF {
734       * @param pData             source buffer (containing the data)       * @param pData             source buffer (containing the data)
735       * @param WordCount         number of 32 Bit unsigned integers to write       * @param WordCount         number of 32 Bit unsigned integers to write
736       * @returns                 number of written integers       * @returns                 number of written integers
737       * @throws RIFF::Exception  if an IO error occured       * @throws RIFF::Exception  if an IO error occurred
738       * @see Resize()       * @see Resize()
739       */       */
740      file_offset_t Chunk::WriteUint32(uint32_t* pData, file_offset_t WordCount) {      file_offset_t Chunk::WriteUint32(uint32_t* pData, file_offset_t WordCount) {
# Line 690  namespace RIFF { Line 746  namespace RIFF {
746       * the chunk.       * the chunk.
747       *       *
748       * @returns                 read integer word       * @returns                 read integer word
749       * @throws RIFF::Exception  if an error occured       * @throws RIFF::Exception  if an error occurred
750       */       */
751      int8_t Chunk::ReadInt8() {      int8_t Chunk::ReadInt8() {
752          #if DEBUG          #if DEBUG_RIFF
753          std::cout << "Chunk::ReadInt8()" << std::endl;          std::cout << "Chunk::ReadInt8()" << std::endl;
754          #endif // DEBUG          #endif // DEBUG_RIFF
755          int8_t word;          int8_t word;
756          ReadSceptical(&word,1,1);          ReadSceptical(&word,1,1);
757          return word;          return word;
# Line 706  namespace RIFF { Line 762  namespace RIFF {
762       * within the chunk.       * within the chunk.
763       *       *
764       * @returns                 read integer word       * @returns                 read integer word
765       * @throws RIFF::Exception  if an error occured       * @throws RIFF::Exception  if an error occurred
766       */       */
767      uint8_t Chunk::ReadUint8() {      uint8_t Chunk::ReadUint8() {
768          #if DEBUG          #if DEBUG_RIFF
769          std::cout << "Chunk::ReadUint8()" << std::endl;          std::cout << "Chunk::ReadUint8()" << std::endl;
770          #endif // DEBUG          #endif // DEBUG_RIFF
771          uint8_t word;          uint8_t word;
772          ReadSceptical(&word,1,1);          ReadSceptical(&word,1,1);
773          return word;          return word;
# Line 723  namespace RIFF { Line 779  namespace RIFF {
779       * needed.       * needed.
780       *       *
781       * @returns                 read integer word       * @returns                 read integer word
782       * @throws RIFF::Exception  if an error occured       * @throws RIFF::Exception  if an error occurred
783       */       */
784      int16_t Chunk::ReadInt16() {      int16_t Chunk::ReadInt16() {
785          #if DEBUG          #if DEBUG_RIFF
786          std::cout << "Chunk::ReadInt16()" << std::endl;          std::cout << "Chunk::ReadInt16()" << std::endl;
787          #endif // DEBUG          #endif // DEBUG_RIFF
788          int16_t word;          int16_t word;
789          ReadSceptical(&word,1,2);          ReadSceptical(&word,1,2);
790          return word;          return word;
# Line 740  namespace RIFF { Line 796  namespace RIFF {
796       * needed.       * needed.
797       *       *
798       * @returns                 read integer word       * @returns                 read integer word
799       * @throws RIFF::Exception  if an error occured       * @throws RIFF::Exception  if an error occurred
800       */       */
801      uint16_t Chunk::ReadUint16() {      uint16_t Chunk::ReadUint16() {
802          #if DEBUG          #if DEBUG_RIFF
803          std::cout << "Chunk::ReadUint16()" << std::endl;          std::cout << "Chunk::ReadUint16()" << std::endl;
804          #endif // DEBUG          #endif // DEBUG_RIFF
805          uint16_t word;          uint16_t word;
806          ReadSceptical(&word,1,2);          ReadSceptical(&word,1,2);
807          return word;          return word;
# Line 757  namespace RIFF { Line 813  namespace RIFF {
813       * needed.       * needed.
814       *       *
815       * @returns                 read integer word       * @returns                 read integer word
816       * @throws RIFF::Exception  if an error occured       * @throws RIFF::Exception  if an error occurred
817       */       */
818      int32_t Chunk::ReadInt32() {      int32_t Chunk::ReadInt32() {
819          #if DEBUG          #if DEBUG_RIFF
820          std::cout << "Chunk::ReadInt32()" << std::endl;          std::cout << "Chunk::ReadInt32()" << std::endl;
821          #endif // DEBUG          #endif // DEBUG_RIFF
822          int32_t word;          int32_t word;
823          ReadSceptical(&word,1,4);          ReadSceptical(&word,1,4);
824          return word;          return word;
# Line 774  namespace RIFF { Line 830  namespace RIFF {
830       * needed.       * needed.
831       *       *
832       * @returns                 read integer word       * @returns                 read integer word
833       * @throws RIFF::Exception  if an error occured       * @throws RIFF::Exception  if an error occurred
834       */       */
835      uint32_t Chunk::ReadUint32() {      uint32_t Chunk::ReadUint32() {
836          #if DEBUG          #if DEBUG_RIFF
837          std::cout << "Chunk::ReadUint32()" << std::endl;          std::cout << "Chunk::ReadUint32()" << std::endl;
838          #endif // DEBUG          #endif // DEBUG_RIFF
839          uint32_t word;          uint32_t word;
840          ReadSceptical(&word,1,4);          ReadSceptical(&word,1,4);
841          return word;          return word;
# Line 874  namespace RIFF { Line 930  namespace RIFF {
930       * boundary!       * boundary!
931       *       *
932       * @param NewSize - new chunk body size in bytes (must be greater than zero)       * @param NewSize - new chunk body size in bytes (must be greater than zero)
933       * @throws RIFF::Exception  if \a NewSize is less than 1 or Unrealistic large       * @throws RIFF::Exception  if \a NewSize is less than 1 or unrealistic large
934       * @see File::Save()       * @see File::Save()
935       */       */
936      void Chunk::Resize(file_offset_t NewSize) {      void Chunk::Resize(file_offset_t NewSize) {
# Line 941  namespace RIFF { Line 997  namespace RIFF {
997              int iBytesMoved = 1;              int iBytesMoved = 1;
998              #endif              #endif
999              for (file_offset_t ullOffset = 0; ullToMove > 0 && iBytesMoved > 0; ullOffset += iBytesMoved, ullToMove -= iBytesMoved) {              for (file_offset_t ullOffset = 0; ullToMove > 0 && iBytesMoved > 0; ullOffset += iBytesMoved, ullToMove -= iBytesMoved) {
1000                  iBytesMoved = (ullToMove < 4096) ? ullToMove : 4096;                  iBytesMoved = (ullToMove < 4096) ? int(ullToMove) : 4096;
1001                  #if POSIX                  #if POSIX
1002                  lseek(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET);                  lseek(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET);
1003                  iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved);                  iBytesMoved = (int) read(pFile->hFileRead, pCopyBuffer, (size_t) iBytesMoved);
1004                  lseek(pFile->hFileWrite, ullWritePos + ullOffset, SEEK_SET);                  lseek(pFile->hFileWrite, ullWritePos + ullOffset, SEEK_SET);
1005                  iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved);                  iBytesMoved = (int) write(pFile->hFileWrite, pCopyBuffer, (size_t) iBytesMoved);
1006                  #elif defined(WIN32)                  #elif defined(WIN32)
1007                  LARGE_INTEGER liFilePos;                  LARGE_INTEGER liFilePos;
1008                  liFilePos.QuadPart = ullStartPos + ullCurrentDataOffset + ullOffset;                  liFilePos.QuadPart = ullStartPos + ullCurrentDataOffset + ullOffset;
# Line 970  namespace RIFF { Line 1026  namespace RIFF {
1026          ullCurrentChunkSize = ullNewChunkSize;          ullCurrentChunkSize = ullNewChunkSize;
1027          WriteHeader(ullOriginalPos);          WriteHeader(ullOriginalPos);
1028    
1029          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1030                __notify_progress(pProgress, 1.0); // notify done
1031    
1032          // update chunk's position pointers          // update chunk's position pointers
1033          ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);          ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
# Line 1008  namespace RIFF { Line 1065  namespace RIFF {
1065  // *  // *
1066    
1067      List::List(File* pFile) : Chunk(pFile) {      List::List(File* pFile) : Chunk(pFile) {
1068          #if DEBUG          #if DEBUG_RIFF
1069          std::cout << "List::List(File* pFile)" << std::endl;          std::cout << "List::List(File* pFile)" << std::endl;
1070          #endif // DEBUG          #endif // DEBUG_RIFF
1071          pSubChunks    = NULL;          pSubChunks    = NULL;
1072          pSubChunksMap = NULL;          pSubChunksMap = NULL;
1073      }      }
1074    
1075      List::List(File* pFile, file_offset_t StartPos, List* Parent)      List::List(File* pFile, file_offset_t StartPos, List* Parent)
1076        : Chunk(pFile, StartPos, Parent) {        : Chunk(pFile, StartPos, Parent) {
1077          #if DEBUG          #if DEBUG_RIFF
1078          std::cout << "List::List(File*,file_offset_t,List*)" << std::endl;          std::cout << "List::List(File*,file_offset_t,List*)" << std::endl;
1079          #endif // DEBUG          #endif // DEBUG_RIFF
1080          pSubChunks    = NULL;          pSubChunks    = NULL;
1081          pSubChunksMap = NULL;          pSubChunksMap = NULL;
1082          ReadHeader(StartPos);          ReadHeader(StartPos);
# Line 1034  namespace RIFF { Line 1091  namespace RIFF {
1091      }      }
1092    
1093      List::~List() {      List::~List() {
1094          #if DEBUG          #if DEBUG_RIFF
1095          std::cout << "List::~List()" << std::endl;          std::cout << "List::~List()" << std::endl;
1096          #endif // DEBUG          #endif // DEBUG_RIFF
1097          DeleteChunkList();          DeleteChunkList();
1098      }      }
1099    
# Line 1069  namespace RIFF { Line 1126  namespace RIFF {
1126       *                   that ID       *                   that ID
1127       */       */
1128      Chunk* List::GetSubChunk(uint32_t ChunkID) {      Chunk* List::GetSubChunk(uint32_t ChunkID) {
1129          #if DEBUG          #if DEBUG_RIFF
1130          std::cout << "List::GetSubChunk(uint32_t)" << std::endl;          std::cout << "List::GetSubChunk(uint32_t)" << std::endl;
1131          #endif // DEBUG          #endif // DEBUG_RIFF
1132          if (!pSubChunksMap) LoadSubChunks();          if (!pSubChunksMap) LoadSubChunks();
1133          return (*pSubChunksMap)[ChunkID];          return (*pSubChunksMap)[ChunkID];
1134      }      }
# Line 1079  namespace RIFF { Line 1136  namespace RIFF {
1136      /**      /**
1137       *  Returns sublist chunk with list type <i>\a ListType</i> within this       *  Returns sublist chunk with list type <i>\a ListType</i> within this
1138       *  chunk list. Use this method if you expect only one sublist chunk of       *  chunk list. Use this method if you expect only one sublist chunk of
1139       *  that type in the list. It there are more than one, it's undetermined       *  that type in the list. If there are more than one, it's undetermined
1140       *  which one of them will be returned! If there are no sublists with       *  which one of them will be returned! If there are no sublists with
1141       *  that desired list type, NULL will be returned.       *  that desired list type, NULL will be returned.
1142       *       *
# Line 1088  namespace RIFF { Line 1145  namespace RIFF {
1145       *                    that type       *                    that type
1146       */       */
1147      List* List::GetSubList(uint32_t ListType) {      List* List::GetSubList(uint32_t ListType) {
1148          #if DEBUG          #if DEBUG_RIFF
1149          std::cout << "List::GetSubList(uint32_t)" << std::endl;          std::cout << "List::GetSubList(uint32_t)" << std::endl;
1150          #endif // DEBUG          #endif // DEBUG_RIFF
1151          if (!pSubChunks) LoadSubChunks();          if (!pSubChunks) LoadSubChunks();
1152          ChunkList::iterator iter = pSubChunks->begin();          ChunkList::iterator iter = pSubChunks->begin();
1153          ChunkList::iterator end  = pSubChunks->end();          ChunkList::iterator end  = pSubChunks->end();
# Line 1105  namespace RIFF { Line 1162  namespace RIFF {
1162      }      }
1163    
1164      /**      /**
1165       *  Returns the first subchunk within the list. You have to call this       *  Returns the first subchunk within the list (which may be an ordinary
1166         *  chunk as well as a list chunk). You have to call this
1167       *  method before you can call GetNextSubChunk(). Recall it when you want       *  method before you can call GetNextSubChunk(). Recall it when you want
1168       *  to start from the beginning of the list again.       *  to start from the beginning of the list again.
1169       *       *
# Line 1113  namespace RIFF { Line 1171  namespace RIFF {
1171       *            otherwise       *            otherwise
1172       */       */
1173      Chunk* List::GetFirstSubChunk() {      Chunk* List::GetFirstSubChunk() {
1174          #if DEBUG          #if DEBUG_RIFF
1175          std::cout << "List::GetFirstSubChunk()" << std::endl;          std::cout << "List::GetFirstSubChunk()" << std::endl;
1176          #endif // DEBUG          #endif // DEBUG_RIFF
1177          if (!pSubChunks) LoadSubChunks();          if (!pSubChunks) LoadSubChunks();
1178          ChunksIterator = pSubChunks->begin();          ChunksIterator = pSubChunks->begin();
1179          return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;          return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
1180      }      }
1181    
1182      /**      /**
1183       *  Returns the next subchunk within the list. You have to call       *  Returns the next subchunk within the list (which may be an ordinary
1184         *  chunk as well as a list chunk). You have to call
1185       *  GetFirstSubChunk() before you can use this method!       *  GetFirstSubChunk() before you can use this method!
1186       *       *
1187       *  @returns  pointer to the next subchunk within the list or NULL if       *  @returns  pointer to the next subchunk within the list or NULL if
1188       *            end of list is reached       *            end of list is reached
1189       */       */
1190      Chunk* List::GetNextSubChunk() {      Chunk* List::GetNextSubChunk() {
1191          #if DEBUG          #if DEBUG_RIFF
1192          std::cout << "List::GetNextSubChunk()" << std::endl;          std::cout << "List::GetNextSubChunk()" << std::endl;
1193          #endif // DEBUG          #endif // DEBUG_RIFF
1194          if (!pSubChunks) return NULL;          if (!pSubChunks) return NULL;
1195          ChunksIterator++;          ChunksIterator++;
1196          return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;          return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
# Line 1147  namespace RIFF { Line 1206  namespace RIFF {
1206       *            otherwise       *            otherwise
1207       */       */
1208      List* List::GetFirstSubList() {      List* List::GetFirstSubList() {
1209          #if DEBUG          #if DEBUG_RIFF
1210          std::cout << "List::GetFirstSubList()" << std::endl;          std::cout << "List::GetFirstSubList()" << std::endl;
1211          #endif // DEBUG          #endif // DEBUG_RIFF
1212          if (!pSubChunks) LoadSubChunks();          if (!pSubChunks) LoadSubChunks();
1213          ListIterator            = pSubChunks->begin();          ListIterator            = pSubChunks->begin();
1214          ChunkList::iterator end = pSubChunks->end();          ChunkList::iterator end = pSubChunks->end();
# Line 1169  namespace RIFF { Line 1228  namespace RIFF {
1228       *            end of list is reached       *            end of list is reached
1229       */       */
1230      List* List::GetNextSubList() {      List* List::GetNextSubList() {
1231          #if DEBUG          #if DEBUG_RIFF
1232          std::cout << "List::GetNextSubList()" << std::endl;          std::cout << "List::GetNextSubList()" << std::endl;
1233          #endif // DEBUG          #endif // DEBUG_RIFF
1234          if (!pSubChunks) return NULL;          if (!pSubChunks) return NULL;
1235          if (ListIterator == pSubChunks->end()) return NULL;          if (ListIterator == pSubChunks->end()) return NULL;
1236          ListIterator++;          ListIterator++;
# Line 1184  namespace RIFF { Line 1243  namespace RIFF {
1243      }      }
1244    
1245      /**      /**
1246       *  Returns number of subchunks within the list.       *  Returns number of subchunks within the list (including list chunks).
1247       */       */
1248      unsigned int List::CountSubChunks() {      size_t List::CountSubChunks() {
1249          if (!pSubChunks) LoadSubChunks();          if (!pSubChunks) LoadSubChunks();
1250          return pSubChunks->size();          return pSubChunks->size();
1251      }      }
# Line 1195  namespace RIFF { Line 1254  namespace RIFF {
1254       *  Returns number of subchunks within the list with chunk ID       *  Returns number of subchunks within the list with chunk ID
1255       *  <i>\a ChunkId</i>.       *  <i>\a ChunkId</i>.
1256       */       */
1257      unsigned int List::CountSubChunks(uint32_t ChunkID) {      size_t List::CountSubChunks(uint32_t ChunkID) {
1258          unsigned int result = 0;          size_t result = 0;
1259          if (!pSubChunks) LoadSubChunks();          if (!pSubChunks) LoadSubChunks();
1260          ChunkList::iterator iter = pSubChunks->begin();          ChunkList::iterator iter = pSubChunks->begin();
1261          ChunkList::iterator end  = pSubChunks->end();          ChunkList::iterator end  = pSubChunks->end();
# Line 1212  namespace RIFF { Line 1271  namespace RIFF {
1271      /**      /**
1272       *  Returns number of sublists within the list.       *  Returns number of sublists within the list.
1273       */       */
1274      unsigned int List::CountSubLists() {      size_t List::CountSubLists() {
1275          return CountSubChunks(CHUNK_ID_LIST);          return CountSubChunks(CHUNK_ID_LIST);
1276      }      }
1277    
# Line 1220  namespace RIFF { Line 1279  namespace RIFF {
1279       *  Returns number of sublists within the list with list type       *  Returns number of sublists within the list with list type
1280       *  <i>\a ListType</i>       *  <i>\a ListType</i>
1281       */       */
1282      unsigned int List::CountSubLists(uint32_t ListType) {      size_t List::CountSubLists(uint32_t ListType) {
1283          unsigned int result = 0;          size_t result = 0;
1284          if (!pSubChunks) LoadSubChunks();          if (!pSubChunks) LoadSubChunks();
1285          ChunkList::iterator iter = pSubChunks->begin();          ChunkList::iterator iter = pSubChunks->begin();
1286          ChunkList::iterator end  = pSubChunks->end();          ChunkList::iterator end  = pSubChunks->end();
# Line 1373  namespace RIFF { Line 1432  namespace RIFF {
1432      }      }
1433    
1434      void List::ReadHeader(file_offset_t filePos) {      void List::ReadHeader(file_offset_t filePos) {
1435          #if DEBUG          #if DEBUG_RIFF
1436          std::cout << "List::Readheader(file_offset_t) ";          std::cout << "List::Readheader(file_offset_t) ";
1437          #endif // DEBUG          #endif // DEBUG_RIFF
1438          Chunk::ReadHeader(filePos);          Chunk::ReadHeader(filePos);
1439          if (ullCurrentChunkSize < 4) return;          if (ullCurrentChunkSize < 4) return;
1440          ullNewChunkSize = ullCurrentChunkSize -= 4;          ullNewChunkSize = ullCurrentChunkSize -= 4;
# Line 1392  namespace RIFF { Line 1451  namespace RIFF {
1451          fseeko(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);          fseeko(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);
1452          fread(&ListType, 4, 1, pFile->hFileRead);          fread(&ListType, 4, 1, pFile->hFileRead);
1453          #endif // POSIX          #endif // POSIX
1454          #if DEBUG          #if DEBUG_RIFF
1455          std::cout << "listType=" << convertToString(ListType) << std::endl;          std::cout << "listType=" << convertToString(ListType) << std::endl;
1456          #endif // DEBUG          #endif // DEBUG_RIFF
1457          if (!pFile->bEndianNative) {          if (!pFile->bEndianNative) {
1458              //swapBytes_32(&ListType);              //swapBytes_32(&ListType);
1459          }          }
# Line 1421  namespace RIFF { Line 1480  namespace RIFF {
1480      }      }
1481    
1482      void List::LoadSubChunks(progress_t* pProgress) {      void List::LoadSubChunks(progress_t* pProgress) {
1483          #if DEBUG          #if DEBUG_RIFF
1484          std::cout << "List::LoadSubChunks()";          std::cout << "List::LoadSubChunks()";
1485          #endif // DEBUG          #endif // DEBUG_RIFF
1486          if (!pSubChunks) {          if (!pSubChunks) {
1487              pSubChunks    = new ChunkList();              pSubChunks    = new ChunkList();
1488              pSubChunksMap = new ChunkMap();              pSubChunksMap = new ChunkMap();
# Line 1438  namespace RIFF { Line 1497  namespace RIFF {
1497                  Chunk* ck;                  Chunk* ck;
1498                  uint32_t ckid;                  uint32_t ckid;
1499                  Read(&ckid, 4, 1);                  Read(&ckid, 4, 1);
1500                  #if DEBUG                  #if DEBUG_RIFF
1501                  std::cout << " ckid=" << convertToString(ckid) << std::endl;                  std::cout << " ckid=" << convertToString(ckid) << std::endl;
1502                  #endif // DEBUG                  #endif // DEBUG_RIFF
1503                  if (ckid == CHUNK_ID_LIST) {                  if (ckid == CHUNK_ID_LIST) {
1504                      ck = new RIFF::List(pFile, ullStartPos + ullPos - 4, this);                      ck = new RIFF::List(pFile, ullStartPos + ullPos - 4, this);
1505                      SetPos(ck->GetSize() + LIST_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos);                      SetPos(ck->GetSize() + LIST_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos);
# Line 1455  namespace RIFF { Line 1514  namespace RIFF {
1514              }              }
1515              SetPos(ullOriginalPos); // restore position before this call              SetPos(ullOriginalPos); // restore position before this call
1516          }          }
1517          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1518                __notify_progress(pProgress, 1.0); // notify done
1519      }      }
1520    
1521      void List::LoadSubChunksRecursively(progress_t* pProgress) {      void List::LoadSubChunksRecursively(progress_t* pProgress) {
1522          const int n = CountSubLists();          const int n = (int) CountSubLists();
1523          int i = 0;          int i = 0;
1524          for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) {          for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) {
1525              // divide local progress into subprogress              if (pProgress) {
1526              progress_t subprogress;                  // divide local progress into subprogress
1527              __divide_progress(pProgress, &subprogress, n, i);                  progress_t subprogress;
1528              // do the actual work                  __divide_progress(pProgress, &subprogress, n, i);
1529              pList->LoadSubChunksRecursively(&subprogress);                  // do the actual work
1530                    pList->LoadSubChunksRecursively(&subprogress);
1531                } else
1532                    pList->LoadSubChunksRecursively(NULL);
1533          }          }
1534          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1535                __notify_progress(pProgress, 1.0); // notify done
1536      }      }
1537    
1538      /** @brief Write list chunk persistently e.g. to disk.      /** @brief Write list chunk persistently e.g. to disk.
# Line 1495  namespace RIFF { Line 1559  namespace RIFF {
1559    
1560          // write all subchunks (including sub list chunks) recursively          // write all subchunks (including sub list chunks) recursively
1561          if (pSubChunks) {          if (pSubChunks) {
1562              int i = 0;              size_t i = 0;
1563              const int n = pSubChunks->size();              const size_t n = pSubChunks->size();
1564              for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) {              for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) {
1565                  // divide local progress into subprogress for loading current Instrument                  if (pProgress) {
1566                  progress_t subprogress;                      // divide local progress into subprogress for loading current Instrument
1567                  __divide_progress(pProgress, &subprogress, n, i);                      progress_t subprogress;
1568                  // do the actual work                      __divide_progress(pProgress, &subprogress, n, i);
1569                  ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress);                      // do the actual work
1570                        ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress);
1571                    } else
1572                        ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, NULL);
1573              }              }
1574          }          }
1575    
# Line 1513  namespace RIFF { Line 1580  namespace RIFF {
1580          // offset of this list chunk in new written file may have changed          // offset of this list chunk in new written file may have changed
1581          ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize);          ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize);
1582    
1583           __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1584                __notify_progress(pProgress, 1.0); // notify done
1585    
1586          return ullWritePos;          return ullWritePos;
1587      }      }
# Line 1574  namespace RIFF { Line 1642  namespace RIFF {
1642       * Loads an existing RIFF file with all its chunks.       * Loads an existing RIFF file with all its chunks.
1643       *       *
1644       * @param path - path and file name of the RIFF file to open       * @param path - path and file name of the RIFF file to open
1645       * @throws RIFF::Exception if error occured while trying to load the       * @throws RIFF::Exception if error occurred while trying to load the
1646       *                         given RIFF file       *                         given RIFF file
1647       */       */
1648      File::File(const String& path)      File::File(const String& path)
1649          : List(this), Filename(path), bIsNewFile(false), Layout(layout_standard),          : List(this), Filename(path), bIsNewFile(false), Layout(layout_standard),
1650            FileOffsetPreference(offset_size_auto)            FileOffsetPreference(offset_size_auto)
1651      {      {
1652          #if DEBUG          #if DEBUG_RIFF
1653          std::cout << "File::File("<<path<<")" << std::endl;          std::cout << "File::File("<<path<<")" << std::endl;
1654          #endif // DEBUG          #endif // DEBUG_RIFF
1655          bEndianNative = true;          bEndianNative = true;
1656          FileOffsetSize = 4;          FileOffsetSize = 4;
1657          try {          try {
# Line 1621  namespace RIFF { Line 1689  namespace RIFF {
1689       * @param Endian - whether the file uses little endian or big endian layout       * @param Endian - whether the file uses little endian or big endian layout
1690       * @param layout - general file structure type       * @param layout - general file structure type
1691       * @param fileOffsetSize - (optional) preference how to deal with large files       * @param fileOffsetSize - (optional) preference how to deal with large files
1692       * @throws RIFF::Exception if error occured while trying to load the       * @throws RIFF::Exception if error occurred while trying to load the
1693       *                         given RIFF-alike file       *                         given RIFF-alike file
1694       */       */
1695      File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize)      File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize)
# Line 1648  namespace RIFF { Line 1716  namespace RIFF {
1716       * @param path - path and file name of the RIFF file or RIFF-alike file to       * @param path - path and file name of the RIFF file or RIFF-alike file to
1717       *               be opened       *               be opened
1718       * @param FileType - (optional) expected chunk ID of first chunk in file       * @param FileType - (optional) expected chunk ID of first chunk in file
1719       * @throws RIFF::Exception if error occured while trying to load the       * @throws RIFF::Exception if error occurred while trying to load the
1720       *                         given RIFF file or RIFF-alike file       *                         given RIFF file or RIFF-alike file
1721       */       */
1722      void File::__openExistingFile(const String& path, uint32_t* FileType) {      void File::__openExistingFile(const String& path, uint32_t* FileType) {
# Line 1810  namespace RIFF { Line 1878  namespace RIFF {
1878                      #if POSIX                      #if POSIX
1879                      if (hFileRead)  close(hFileRead);                      if (hFileRead)  close(hFileRead);
1880                      if (hFileWrite) close(hFileWrite);                      if (hFileWrite) close(hFileWrite);
1881                        hFileRead = hFileWrite = 0;
1882                      #elif defined(WIN32)                      #elif defined(WIN32)
1883                      if (hFileRead  != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);                      if (hFileRead  != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1884                      if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite);                      if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite);
1885                        hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1886                      #else                      #else
1887                      if (hFileRead)  fclose(hFileRead);                      if (hFileRead)  fclose(hFileRead);
1888                      if (hFileWrite) fclose(hFileWrite);                      if (hFileWrite) fclose(hFileWrite);
1889                        hFileRead = hFileWrite = NULL;
1890                      #endif                      #endif
                     hFileRead = hFileWrite = 0;  
1891                      break;                      break;
1892                  default:                  default:
1893                      throw Exception("Unknown file access mode");                      throw Exception("Unknown file access mode");
# Line 1852  namespace RIFF { Line 1922  namespace RIFF {
1922       *       *
1923       * @param pProgress - optional: callback function for progress notification       * @param pProgress - optional: callback function for progress notification
1924       * @throws RIFF::Exception if there is an empty chunk or empty list       * @throws RIFF::Exception if there is an empty chunk or empty list
1925       *                         chunk or any kind of IO error occured       *                         chunk or any kind of IO error occurred
1926       */       */
1927      void File::Save(progress_t* pProgress) {      void File::Save(progress_t* pProgress) {
1928          //TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files)          //TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files)
# Line 1860  namespace RIFF { Line 1930  namespace RIFF {
1930              throw Exception("Saving a RIFF file with layout_flat is not implemented yet");              throw Exception("Saving a RIFF file with layout_flat is not implemented yet");
1931    
1932          // make sure the RIFF tree is built (from the original file)          // make sure the RIFF tree is built (from the original file)
1933          {          if (pProgress) {
1934              // divide progress into subprogress              // divide progress into subprogress
1935              progress_t subprogress;              progress_t subprogress;
1936              __divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress              __divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress
# Line 1868  namespace RIFF { Line 1938  namespace RIFF {
1938              LoadSubChunksRecursively(&subprogress);              LoadSubChunksRecursively(&subprogress);
1939              // notify subprogress done              // notify subprogress done
1940              __notify_progress(&subprogress, 1.f);              __notify_progress(&subprogress, 1.f);
1941          }          } else
1942                LoadSubChunksRecursively(NULL);
1943    
1944          // reopen file in write mode          // reopen file in write mode
1945          SetMode(stream_mode_read_write);          SetMode(stream_mode_read_write);
# Line 1898  namespace RIFF { Line 1969  namespace RIFF {
1969    
1970              // divide progress into subprogress              // divide progress into subprogress
1971              progress_t subprogress;              progress_t subprogress;
1972              __divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress              if (pProgress)
1973                    __divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress
1974    
1975              // ... we enlarge this file first ...              // ... we enlarge this file first ...
1976              ResizeFile(newFileSize);              ResizeFile(newFileSize);
# Line 1908  namespace RIFF { Line 1980  namespace RIFF {
1980              #if defined(WIN32)              #if defined(WIN32)
1981              DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured              DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured
1982              #else              #else
1983              int iBytesMoved = 1;              ssize_t iBytesMoved = 1;
1984              #endif              #endif
1985              for (file_offset_t ullPos = workingFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) {              for (file_offset_t ullPos = workingFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) {
1986                  iBytesMoved = (ullPos < 4096) ? ullPos : 4096;                  iBytesMoved = (ullPos < 4096) ? ullPos : 4096;
# Line 1932  namespace RIFF { Line 2004  namespace RIFF {
2004                  fseeko(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET);                  fseeko(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET);
2005                  iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite);                  iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite);
2006                  #endif                  #endif
2007                  if (!(iNotif % 8) && iBytesMoved > 0)                  if (pProgress && !(iNotif % 8) && iBytesMoved > 0)
2008                      __notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize));                      __notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize));
2009              }              }
2010              delete[] pCopyBuffer;              delete[] pCopyBuffer;
2011              if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it");              if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it");
2012    
2013              __notify_progress(&subprogress, 1.f); // notify subprogress done              if (pProgress)
2014                    __notify_progress(&subprogress, 1.f); // notify subprogress done
2015          }          }
2016    
2017          // rebuild / rewrite complete RIFF tree ...          // rebuild / rewrite complete RIFF tree ...
2018    
2019          // divide progress into subprogress          // divide progress into subprogress
2020          progress_t subprogress;          progress_t subprogress;
2021          __divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress          if (pProgress)
2022                __divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress
2023          // do the actual work          // do the actual work
2024          const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, &subprogress);          const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, pProgress ? &subprogress : NULL);
2025          const file_offset_t finalActualSize = __GetFileSize(hFileWrite);          const file_offset_t finalActualSize = __GetFileSize(hFileWrite);
2026          // notify subprogress done          // notify subprogress done
2027          __notify_progress(&subprogress, 1.f);          if (pProgress)
2028                __notify_progress(&subprogress, 1.f);
2029    
2030          // resize file to the final size          // resize file to the final size
2031          if (finalSize < finalActualSize) ResizeFile(finalSize);          if (finalSize < finalActualSize) ResizeFile(finalSize);
2032    
2033          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
2034                __notify_progress(pProgress, 1.0); // notify done
2035      }      }
2036    
2037      /** @brief Save changes to another file.      /** @brief Save changes to another file.
# Line 1980  namespace RIFF { Line 2056  namespace RIFF {
2056              throw Exception("Saving a RIFF file with layout_flat is not implemented yet");              throw Exception("Saving a RIFF file with layout_flat is not implemented yet");
2057    
2058          // make sure the RIFF tree is built (from the original file)          // make sure the RIFF tree is built (from the original file)
2059          {          if (pProgress) {
2060              // divide progress into subprogress              // divide progress into subprogress
2061              progress_t subprogress;              progress_t subprogress;
2062              __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress              __divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress
# Line 1988  namespace RIFF { Line 2064  namespace RIFF {
2064              LoadSubChunksRecursively(&subprogress);              LoadSubChunksRecursively(&subprogress);
2065              // notify subprogress done              // notify subprogress done
2066              __notify_progress(&subprogress, 1.f);              __notify_progress(&subprogress, 1.f);
2067          }          } else
2068                LoadSubChunksRecursively(NULL);
2069    
2070          if (!bIsNewFile) SetMode(stream_mode_read);          if (!bIsNewFile) SetMode(stream_mode_read);
2071          // open the other (new) file for writing and truncate it to zero size          // open the other (new) file for writing and truncate it to zero size
# Line 2027  namespace RIFF { Line 2104  namespace RIFF {
2104    
2105          // write complete RIFF tree to the other (new) file          // write complete RIFF tree to the other (new) file
2106          file_offset_t ullTotalSize;          file_offset_t ullTotalSize;
2107          {          if (pProgress) {
2108              // divide progress into subprogress              // divide progress into subprogress
2109              progress_t subprogress;              progress_t subprogress;
2110              __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress              __divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress
# Line 2035  namespace RIFF { Line 2112  namespace RIFF {
2112              ullTotalSize = WriteChunk(0, 0, &subprogress);              ullTotalSize = WriteChunk(0, 0, &subprogress);
2113              // notify subprogress done              // notify subprogress done
2114              __notify_progress(&subprogress, 1.f);              __notify_progress(&subprogress, 1.f);
2115          }          } else
2116                ullTotalSize = WriteChunk(0, 0, NULL);
2117    
2118          file_offset_t ullActualSize = __GetFileSize(hFileWrite);          file_offset_t ullActualSize = __GetFileSize(hFileWrite);
2119    
2120          // resize file to the final size (if the file was originally larger)          // resize file to the final size (if the file was originally larger)
# Line 2056  namespace RIFF { Line 2135  namespace RIFF {
2135          Mode = (stream_mode_t) -1;       // Just set it to an undefined mode ...          Mode = (stream_mode_t) -1;       // Just set it to an undefined mode ...
2136          SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles.          SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles.
2137    
2138          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
2139                __notify_progress(pProgress, 1.0); // notify done
2140      }      }
2141    
2142      void File::ResizeFile(file_offset_t ullNewSize) {      void File::ResizeFile(file_offset_t ullNewSize) {
# Line 2077  namespace RIFF { Line 2157  namespace RIFF {
2157      }      }
2158    
2159      File::~File() {      File::~File() {
2160          #if DEBUG          #if DEBUG_RIFF
2161          std::cout << "File::~File()" << std::endl;          std::cout << "File::~File()" << std::endl;
2162          #endif // DEBUG          #endif // DEBUG_RIFF
2163          Cleanup();          Cleanup();
2164      }      }
2165    
# Line 2249  namespace RIFF { Line 2329  namespace RIFF {
2329  // *************** Exception ***************  // *************** Exception ***************
2330  // *  // *
2331    
2332        Exception::Exception() {
2333        }
2334    
2335        Exception::Exception(String format, ...) {
2336            va_list arg;
2337            va_start(arg, format);
2338            Message = assemble(format, arg);
2339            va_end(arg);
2340        }
2341    
2342        Exception::Exception(String format, va_list arg) {
2343            Message = assemble(format, arg);
2344        }
2345    
2346      void Exception::PrintMessage() {      void Exception::PrintMessage() {
2347          std::cout << "RIFF::Exception: " << Message << std::endl;          std::cout << "RIFF::Exception: " << Message << std::endl;
2348      }      }
2349    
2350        String Exception::assemble(String format, va_list arg) {
2351            char* buf = NULL;
2352            vasprintf(&buf, format.c_str(), arg);
2353            String s = buf;
2354            free(buf);
2355            return s;
2356        }
2357    
2358    
2359  // *************** functions ***************  // *************** functions ***************
2360  // *  // *

Legend:
Removed from v.2915  
changed lines
  Added in v.3488

  ViewVC Help
Powered by ViewVC