/[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 3048 by schoenebeck, Fri Nov 25 18:34:45 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 316  namespace RIFF { Line 372  namespace RIFF {
372       *                    of file reached or error occurred       *                    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 459  namespace RIFF { Line 515  namespace RIFF {
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 496  namespace RIFF { Line 552  namespace RIFF {
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 533  namespace RIFF { Line 589  namespace RIFF {
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 570  namespace RIFF { Line 626  namespace RIFF {
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 607  namespace RIFF { Line 663  namespace RIFF {
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 644  namespace RIFF { Line 700  namespace RIFF {
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 693  namespace RIFF { Line 749  namespace RIFF {
749       * @throws RIFF::Exception  if an error occurred       * @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 709  namespace RIFF { Line 765  namespace RIFF {
765       * @throws RIFF::Exception  if an error occurred       * @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 726  namespace RIFF { Line 782  namespace RIFF {
782       * @throws RIFF::Exception  if an error occurred       * @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 743  namespace RIFF { Line 799  namespace RIFF {
799       * @throws RIFF::Exception  if an error occurred       * @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 760  namespace RIFF { Line 816  namespace RIFF {
816       * @throws RIFF::Exception  if an error occurred       * @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 777  namespace RIFF { Line 833  namespace RIFF {
833       * @throws RIFF::Exception  if an error occurred       * @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 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 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 1114  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;
# Line 1131  namespace RIFF { Line 1188  namespace RIFF {
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 1149  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 1171  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 1375  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 1394  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 1423  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 1440  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 1457  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 1500  namespace RIFF { Line 1562  namespace RIFF {
1562              size_t i = 0;              size_t i = 0;
1563              const size_t 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 1515  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 1583  namespace RIFF { Line 1649  namespace RIFF {
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 1812  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 1862  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 1870  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 1900  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 1934  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 1982  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 1990  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 2029  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 2037  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 2058  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 2079  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 2251  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.3048  
changed lines
  Added in v.3488

  ViewVC Help
Powered by ViewVC