/[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 3053 by schoenebeck, Wed Dec 14 18:55:08 2016 UTC revision 3901 by schoenebeck, Wed May 12 17:35:38 2021 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-2021 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 838  namespace RIFF { Line 894  namespace RIFF {
894              uint8_t* pNewBuffer = new uint8_t[ullNewChunkSize];              uint8_t* pNewBuffer = new uint8_t[ullNewChunkSize];
895              if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(ullNewChunkSize) + " bytes");              if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(ullNewChunkSize) + " bytes");
896              memset(pNewBuffer, 0 , ullNewChunkSize);              memset(pNewBuffer, 0 , ullNewChunkSize);
897              memcpy(pNewBuffer, pChunkData, ullChunkDataSize);              if (pChunkData) {
898              delete[] pChunkData;                  memcpy(pNewBuffer, pChunkData, ullChunkDataSize);
899                    delete[] pChunkData;
900                }
901              pChunkData       = pNewBuffer;              pChunkData       = pNewBuffer;
902              ullChunkDataSize = ullNewChunkSize;              ullChunkDataSize = ullNewChunkSize;
903          }          }
# Line 970  namespace RIFF { Line 1028  namespace RIFF {
1028          ullCurrentChunkSize = ullNewChunkSize;          ullCurrentChunkSize = ullNewChunkSize;
1029          WriteHeader(ullOriginalPos);          WriteHeader(ullOriginalPos);
1030    
1031          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1032                __notify_progress(pProgress, 1.0); // notify done
1033    
1034          // update chunk's position pointers          // update chunk's position pointers
1035          ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);          ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize);
# Line 1008  namespace RIFF { Line 1067  namespace RIFF {
1067  // *  // *
1068    
1069      List::List(File* pFile) : Chunk(pFile) {      List::List(File* pFile) : Chunk(pFile) {
1070          #if DEBUG          #if DEBUG_RIFF
1071          std::cout << "List::List(File* pFile)" << std::endl;          std::cout << "List::List(File* pFile)" << std::endl;
1072          #endif // DEBUG          #endif // DEBUG_RIFF
1073          pSubChunks    = NULL;          pSubChunks    = NULL;
1074          pSubChunksMap = NULL;          pSubChunksMap = NULL;
1075      }      }
1076    
1077      List::List(File* pFile, file_offset_t StartPos, List* Parent)      List::List(File* pFile, file_offset_t StartPos, List* Parent)
1078        : Chunk(pFile, StartPos, Parent) {        : Chunk(pFile, StartPos, Parent) {
1079          #if DEBUG          #if DEBUG_RIFF
1080          std::cout << "List::List(File*,file_offset_t,List*)" << std::endl;          std::cout << "List::List(File*,file_offset_t,List*)" << std::endl;
1081          #endif // DEBUG          #endif // DEBUG_RIFF
1082          pSubChunks    = NULL;          pSubChunks    = NULL;
1083          pSubChunksMap = NULL;          pSubChunksMap = NULL;
1084          ReadHeader(StartPos);          ReadHeader(StartPos);
# Line 1034  namespace RIFF { Line 1093  namespace RIFF {
1093      }      }
1094    
1095      List::~List() {      List::~List() {
1096          #if DEBUG          #if DEBUG_RIFF
1097          std::cout << "List::~List()" << std::endl;          std::cout << "List::~List()" << std::endl;
1098          #endif // DEBUG          #endif // DEBUG_RIFF
1099          DeleteChunkList();          DeleteChunkList();
1100      }      }
1101    
# Line 1069  namespace RIFF { Line 1128  namespace RIFF {
1128       *                   that ID       *                   that ID
1129       */       */
1130      Chunk* List::GetSubChunk(uint32_t ChunkID) {      Chunk* List::GetSubChunk(uint32_t ChunkID) {
1131          #if DEBUG          #if DEBUG_RIFF
1132          std::cout << "List::GetSubChunk(uint32_t)" << std::endl;          std::cout << "List::GetSubChunk(uint32_t)" << std::endl;
1133          #endif // DEBUG          #endif // DEBUG_RIFF
1134          if (!pSubChunksMap) LoadSubChunks();          if (!pSubChunksMap) LoadSubChunks();
1135          return (*pSubChunksMap)[ChunkID];          return (*pSubChunksMap)[ChunkID];
1136      }      }
# Line 1088  namespace RIFF { Line 1147  namespace RIFF {
1147       *                    that type       *                    that type
1148       */       */
1149      List* List::GetSubList(uint32_t ListType) {      List* List::GetSubList(uint32_t ListType) {
1150          #if DEBUG          #if DEBUG_RIFF
1151          std::cout << "List::GetSubList(uint32_t)" << std::endl;          std::cout << "List::GetSubList(uint32_t)" << std::endl;
1152          #endif // DEBUG          #endif // DEBUG_RIFF
1153          if (!pSubChunks) LoadSubChunks();          if (!pSubChunks) LoadSubChunks();
1154          ChunkList::iterator iter = pSubChunks->begin();          ChunkList::iterator iter = pSubChunks->begin();
1155          ChunkList::iterator end  = pSubChunks->end();          ChunkList::iterator end  = pSubChunks->end();
# Line 1114  namespace RIFF { Line 1173  namespace RIFF {
1173       *            otherwise       *            otherwise
1174       */       */
1175      Chunk* List::GetFirstSubChunk() {      Chunk* List::GetFirstSubChunk() {
1176          #if DEBUG          #if DEBUG_RIFF
1177          std::cout << "List::GetFirstSubChunk()" << std::endl;          std::cout << "List::GetFirstSubChunk()" << std::endl;
1178          #endif // DEBUG          #endif // DEBUG_RIFF
1179          if (!pSubChunks) LoadSubChunks();          if (!pSubChunks) LoadSubChunks();
1180          ChunksIterator = pSubChunks->begin();          ChunksIterator = pSubChunks->begin();
1181          return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;          return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
# Line 1131  namespace RIFF { Line 1190  namespace RIFF {
1190       *            end of list is reached       *            end of list is reached
1191       */       */
1192      Chunk* List::GetNextSubChunk() {      Chunk* List::GetNextSubChunk() {
1193          #if DEBUG          #if DEBUG_RIFF
1194          std::cout << "List::GetNextSubChunk()" << std::endl;          std::cout << "List::GetNextSubChunk()" << std::endl;
1195          #endif // DEBUG          #endif // DEBUG_RIFF
1196          if (!pSubChunks) return NULL;          if (!pSubChunks) return NULL;
1197          ChunksIterator++;          ChunksIterator++;
1198          return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;          return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
# Line 1149  namespace RIFF { Line 1208  namespace RIFF {
1208       *            otherwise       *            otherwise
1209       */       */
1210      List* List::GetFirstSubList() {      List* List::GetFirstSubList() {
1211          #if DEBUG          #if DEBUG_RIFF
1212          std::cout << "List::GetFirstSubList()" << std::endl;          std::cout << "List::GetFirstSubList()" << std::endl;
1213          #endif // DEBUG          #endif // DEBUG_RIFF
1214          if (!pSubChunks) LoadSubChunks();          if (!pSubChunks) LoadSubChunks();
1215          ListIterator            = pSubChunks->begin();          ListIterator            = pSubChunks->begin();
1216          ChunkList::iterator end = pSubChunks->end();          ChunkList::iterator end = pSubChunks->end();
# Line 1171  namespace RIFF { Line 1230  namespace RIFF {
1230       *            end of list is reached       *            end of list is reached
1231       */       */
1232      List* List::GetNextSubList() {      List* List::GetNextSubList() {
1233          #if DEBUG          #if DEBUG_RIFF
1234          std::cout << "List::GetNextSubList()" << std::endl;          std::cout << "List::GetNextSubList()" << std::endl;
1235          #endif // DEBUG          #endif // DEBUG_RIFF
1236          if (!pSubChunks) return NULL;          if (!pSubChunks) return NULL;
1237          if (ListIterator == pSubChunks->end()) return NULL;          if (ListIterator == pSubChunks->end()) return NULL;
1238          ListIterator++;          ListIterator++;
# Line 1375  namespace RIFF { Line 1434  namespace RIFF {
1434      }      }
1435    
1436      void List::ReadHeader(file_offset_t filePos) {      void List::ReadHeader(file_offset_t filePos) {
1437          #if DEBUG          #if DEBUG_RIFF
1438          std::cout << "List::Readheader(file_offset_t) ";          std::cout << "List::Readheader(file_offset_t) ";
1439          #endif // DEBUG          #endif // DEBUG_RIFF
1440          Chunk::ReadHeader(filePos);          Chunk::ReadHeader(filePos);
1441          if (ullCurrentChunkSize < 4) return;          if (ullCurrentChunkSize < 4) return;
1442          ullNewChunkSize = ullCurrentChunkSize -= 4;          ullNewChunkSize = ullCurrentChunkSize -= 4;
# Line 1394  namespace RIFF { Line 1453  namespace RIFF {
1453          fseeko(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);          fseeko(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET);
1454          fread(&ListType, 4, 1, pFile->hFileRead);          fread(&ListType, 4, 1, pFile->hFileRead);
1455          #endif // POSIX          #endif // POSIX
1456          #if DEBUG          #if DEBUG_RIFF
1457          std::cout << "listType=" << convertToString(ListType) << std::endl;          std::cout << "listType=" << convertToString(ListType) << std::endl;
1458          #endif // DEBUG          #endif // DEBUG_RIFF
1459          if (!pFile->bEndianNative) {          if (!pFile->bEndianNative) {
1460              //swapBytes_32(&ListType);              //swapBytes_32(&ListType);
1461          }          }
# Line 1423  namespace RIFF { Line 1482  namespace RIFF {
1482      }      }
1483    
1484      void List::LoadSubChunks(progress_t* pProgress) {      void List::LoadSubChunks(progress_t* pProgress) {
1485          #if DEBUG          #if DEBUG_RIFF
1486          std::cout << "List::LoadSubChunks()";          std::cout << "List::LoadSubChunks()";
1487          #endif // DEBUG          #endif // DEBUG_RIFF
1488          if (!pSubChunks) {          if (!pSubChunks) {
1489              pSubChunks    = new ChunkList();              pSubChunks    = new ChunkList();
1490              pSubChunksMap = new ChunkMap();              pSubChunksMap = new ChunkMap();
# Line 1440  namespace RIFF { Line 1499  namespace RIFF {
1499                  Chunk* ck;                  Chunk* ck;
1500                  uint32_t ckid;                  uint32_t ckid;
1501                  Read(&ckid, 4, 1);                  Read(&ckid, 4, 1);
1502                  #if DEBUG                  #if DEBUG_RIFF
1503                  std::cout << " ckid=" << convertToString(ckid) << std::endl;                  std::cout << " ckid=" << convertToString(ckid) << std::endl;
1504                  #endif // DEBUG                  #endif // DEBUG_RIFF
1505                  if (ckid == CHUNK_ID_LIST) {                  if (ckid == CHUNK_ID_LIST) {
1506                      ck = new RIFF::List(pFile, ullStartPos + ullPos - 4, this);                      ck = new RIFF::List(pFile, ullStartPos + ullPos - 4, this);
1507                      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 1516  namespace RIFF {
1516              }              }
1517              SetPos(ullOriginalPos); // restore position before this call              SetPos(ullOriginalPos); // restore position before this call
1518          }          }
1519          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1520                __notify_progress(pProgress, 1.0); // notify done
1521      }      }
1522    
1523      void List::LoadSubChunksRecursively(progress_t* pProgress) {      void List::LoadSubChunksRecursively(progress_t* pProgress) {
1524          const int n = (int) CountSubLists();          const int n = (int) CountSubLists();
1525          int i = 0;          int i = 0;
1526          for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) {          for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) {
1527              // divide local progress into subprogress              if (pProgress) {
1528              progress_t subprogress;                  // divide local progress into subprogress
1529              __divide_progress(pProgress, &subprogress, n, i);                  progress_t subprogress;
1530              // do the actual work                  __divide_progress(pProgress, &subprogress, n, i);
1531              pList->LoadSubChunksRecursively(&subprogress);                  // do the actual work
1532                    pList->LoadSubChunksRecursively(&subprogress);
1533                } else
1534                    pList->LoadSubChunksRecursively(NULL);
1535          }          }
1536          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1537                __notify_progress(pProgress, 1.0); // notify done
1538      }      }
1539    
1540      /** @brief Write list chunk persistently e.g. to disk.      /** @brief Write list chunk persistently e.g. to disk.
# Line 1500  namespace RIFF { Line 1564  namespace RIFF {
1564              size_t i = 0;              size_t i = 0;
1565              const size_t n = pSubChunks->size();              const size_t n = pSubChunks->size();
1566              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) {
1567                  // divide local progress into subprogress for loading current Instrument                  if (pProgress) {
1568                  progress_t subprogress;                      // divide local progress into subprogress for loading current Instrument
1569                  __divide_progress(pProgress, &subprogress, n, i);                      progress_t subprogress;
1570                  // do the actual work                      __divide_progress(pProgress, &subprogress, n, i);
1571                  ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress);                      // do the actual work
1572                        ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress);
1573                    } else
1574                        ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, NULL);
1575              }              }
1576          }          }
1577    
# Line 1515  namespace RIFF { Line 1582  namespace RIFF {
1582          // offset of this list chunk in new written file may have changed          // offset of this list chunk in new written file may have changed
1583          ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize);          ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize);
1584    
1585           __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
1586                __notify_progress(pProgress, 1.0); // notify done
1587    
1588          return ullWritePos;          return ullWritePos;
1589      }      }
# Line 1583  namespace RIFF { Line 1651  namespace RIFF {
1651          : List(this), Filename(path), bIsNewFile(false), Layout(layout_standard),          : List(this), Filename(path), bIsNewFile(false), Layout(layout_standard),
1652            FileOffsetPreference(offset_size_auto)            FileOffsetPreference(offset_size_auto)
1653      {      {
1654          #if DEBUG          #if DEBUG_RIFF
1655          std::cout << "File::File("<<path<<")" << std::endl;          std::cout << "File::File("<<path<<")" << std::endl;
1656          #endif // DEBUG          #endif // DEBUG_RIFF
1657          bEndianNative = true;          bEndianNative = true;
1658          FileOffsetSize = 4;          FileOffsetSize = 4;
1659          try {          try {
# Line 1812  namespace RIFF { Line 1880  namespace RIFF {
1880                      #if POSIX                      #if POSIX
1881                      if (hFileRead)  close(hFileRead);                      if (hFileRead)  close(hFileRead);
1882                      if (hFileWrite) close(hFileWrite);                      if (hFileWrite) close(hFileWrite);
1883                        hFileRead = hFileWrite = 0;
1884                      #elif defined(WIN32)                      #elif defined(WIN32)
1885                      if (hFileRead  != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);                      if (hFileRead  != INVALID_HANDLE_VALUE) CloseHandle(hFileRead);
1886                      if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite);                      if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite);
1887                        hFileRead = hFileWrite = INVALID_HANDLE_VALUE;
1888                      #else                      #else
1889                      if (hFileRead)  fclose(hFileRead);                      if (hFileRead)  fclose(hFileRead);
1890                      if (hFileWrite) fclose(hFileWrite);                      if (hFileWrite) fclose(hFileWrite);
1891                        hFileRead = hFileWrite = NULL;
1892                      #endif                      #endif
                     hFileRead = hFileWrite = 0;  
1893                      break;                      break;
1894                  default:                  default:
1895                      throw Exception("Unknown file access mode");                      throw Exception("Unknown file access mode");
# Line 1862  namespace RIFF { Line 1932  namespace RIFF {
1932              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");
1933    
1934          // make sure the RIFF tree is built (from the original file)          // make sure the RIFF tree is built (from the original file)
1935          {          if (pProgress) {
1936              // divide progress into subprogress              // divide progress into subprogress
1937              progress_t subprogress;              progress_t subprogress;
1938              __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 1940  namespace RIFF {
1940              LoadSubChunksRecursively(&subprogress);              LoadSubChunksRecursively(&subprogress);
1941              // notify subprogress done              // notify subprogress done
1942              __notify_progress(&subprogress, 1.f);              __notify_progress(&subprogress, 1.f);
1943          }          } else
1944                LoadSubChunksRecursively(NULL);
1945    
1946          // reopen file in write mode          // reopen file in write mode
1947          SetMode(stream_mode_read_write);          SetMode(stream_mode_read_write);
# Line 1900  namespace RIFF { Line 1971  namespace RIFF {
1971    
1972              // divide progress into subprogress              // divide progress into subprogress
1973              progress_t subprogress;              progress_t subprogress;
1974              __divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress              if (pProgress)
1975                    __divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress
1976    
1977              // ... we enlarge this file first ...              // ... we enlarge this file first ...
1978              ResizeFile(newFileSize);              ResizeFile(newFileSize);
# Line 1934  namespace RIFF { Line 2006  namespace RIFF {
2006                  fseeko(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET);                  fseeko(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET);
2007                  iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite);                  iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite);
2008                  #endif                  #endif
2009                  if (!(iNotif % 8) && iBytesMoved > 0)                  if (pProgress && !(iNotif % 8) && iBytesMoved > 0)
2010                      __notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize));                      __notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize));
2011              }              }
2012              delete[] pCopyBuffer;              delete[] pCopyBuffer;
2013              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");
2014    
2015              __notify_progress(&subprogress, 1.f); // notify subprogress done              if (pProgress)
2016                    __notify_progress(&subprogress, 1.f); // notify subprogress done
2017          }          }
2018    
2019          // rebuild / rewrite complete RIFF tree ...          // rebuild / rewrite complete RIFF tree ...
2020    
2021          // divide progress into subprogress          // divide progress into subprogress
2022          progress_t subprogress;          progress_t subprogress;
2023          __divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress          if (pProgress)
2024                __divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress
2025          // do the actual work          // do the actual work
2026          const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, &subprogress);          const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, pProgress ? &subprogress : NULL);
2027          const file_offset_t finalActualSize = __GetFileSize(hFileWrite);          const file_offset_t finalActualSize = __GetFileSize(hFileWrite);
2028          // notify subprogress done          // notify subprogress done
2029          __notify_progress(&subprogress, 1.f);          if (pProgress)
2030                __notify_progress(&subprogress, 1.f);
2031    
2032          // resize file to the final size          // resize file to the final size
2033          if (finalSize < finalActualSize) ResizeFile(finalSize);          if (finalSize < finalActualSize) ResizeFile(finalSize);
2034    
2035          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
2036                __notify_progress(pProgress, 1.0); // notify done
2037      }      }
2038    
2039      /** @brief Save changes to another file.      /** @brief Save changes to another file.
# Line 1982  namespace RIFF { Line 2058  namespace RIFF {
2058              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");
2059    
2060          // make sure the RIFF tree is built (from the original file)          // make sure the RIFF tree is built (from the original file)
2061          {          if (pProgress) {
2062              // divide progress into subprogress              // divide progress into subprogress
2063              progress_t subprogress;              progress_t subprogress;
2064              __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 2066  namespace RIFF {
2066              LoadSubChunksRecursively(&subprogress);              LoadSubChunksRecursively(&subprogress);
2067              // notify subprogress done              // notify subprogress done
2068              __notify_progress(&subprogress, 1.f);              __notify_progress(&subprogress, 1.f);
2069          }          } else
2070                LoadSubChunksRecursively(NULL);
2071    
2072          if (!bIsNewFile) SetMode(stream_mode_read);          if (!bIsNewFile) SetMode(stream_mode_read);
2073          // 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 2106  namespace RIFF {
2106    
2107          // write complete RIFF tree to the other (new) file          // write complete RIFF tree to the other (new) file
2108          file_offset_t ullTotalSize;          file_offset_t ullTotalSize;
2109          {          if (pProgress) {
2110              // divide progress into subprogress              // divide progress into subprogress
2111              progress_t subprogress;              progress_t subprogress;
2112              __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 2114  namespace RIFF {
2114              ullTotalSize = WriteChunk(0, 0, &subprogress);              ullTotalSize = WriteChunk(0, 0, &subprogress);
2115              // notify subprogress done              // notify subprogress done
2116              __notify_progress(&subprogress, 1.f);              __notify_progress(&subprogress, 1.f);
2117          }          } else
2118                ullTotalSize = WriteChunk(0, 0, NULL);
2119    
2120          file_offset_t ullActualSize = __GetFileSize(hFileWrite);          file_offset_t ullActualSize = __GetFileSize(hFileWrite);
2121    
2122          // 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 2137  namespace RIFF {
2137          Mode = (stream_mode_t) -1;       // Just set it to an undefined mode ...          Mode = (stream_mode_t) -1;       // Just set it to an undefined mode ...
2138          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.
2139    
2140          __notify_progress(pProgress, 1.0); // notify done          if (pProgress)
2141                __notify_progress(pProgress, 1.0); // notify done
2142      }      }
2143    
2144      void File::ResizeFile(file_offset_t ullNewSize) {      void File::ResizeFile(file_offset_t ullNewSize) {
# Line 2079  namespace RIFF { Line 2159  namespace RIFF {
2159      }      }
2160    
2161      File::~File() {      File::~File() {
2162          #if DEBUG          #if DEBUG_RIFF
2163          std::cout << "File::~File()" << std::endl;          std::cout << "File::~File()" << std::endl;
2164          #endif // DEBUG          #endif // DEBUG_RIFF
2165          Cleanup();          Cleanup();
2166      }      }
2167    
# Line 2251  namespace RIFF { Line 2331  namespace RIFF {
2331  // *************** Exception ***************  // *************** Exception ***************
2332  // *  // *
2333    
2334        Exception::Exception() {
2335        }
2336    
2337        Exception::Exception(String format, ...) {
2338            va_list arg;
2339            va_start(arg, format);
2340            Message = assemble(format, arg);
2341            va_end(arg);
2342        }
2343    
2344        Exception::Exception(String format, va_list arg) {
2345            Message = assemble(format, arg);
2346        }
2347    
2348      void Exception::PrintMessage() {      void Exception::PrintMessage() {
2349          std::cout << "RIFF::Exception: " << Message << std::endl;          std::cout << "RIFF::Exception: " << Message << std::endl;
2350      }      }
2351    
2352        String Exception::assemble(String format, va_list arg) {
2353            char* buf = NULL;
2354            vasprintf(&buf, format.c_str(), arg);
2355            String s = buf;
2356            free(buf);
2357            return s;
2358        }
2359    
2360    
2361  // *************** functions ***************  // *************** functions ***************
2362  // *  // *

Legend:
Removed from v.3053  
changed lines
  Added in v.3901

  ViewVC Help
Powered by ViewVC