00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <algorithm>
00025 #include <string.h>
00026
00027 #include "RIFF.h"
00028
00029 #include "helper.h"
00030
00031 namespace RIFF {
00032
00033
00034
00035
00037 static String __resolveChunkPath(Chunk* pCk) {
00038 String sPath;
00039 for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) {
00040 if (pChunk->GetChunkID() == CHUNK_ID_LIST) {
00041 List* pList = (List*) pChunk;
00042 sPath = "->'" + pList->GetListTypeString() + "'" + sPath;
00043 } else {
00044 sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath;
00045 }
00046 }
00047 return sPath;
00048 }
00049
00050
00051
00052
00053
00054
00055 Chunk::Chunk(File* pFile) {
00056 #if DEBUG
00057 std::cout << "Chunk::Chunk(File* pFile)" << std::endl;
00058 #endif // DEBUG
00059 ulPos = 0;
00060 pParent = NULL;
00061 pChunkData = NULL;
00062 ulChunkDataSize = 0;
00063 ChunkID = CHUNK_ID_RIFF;
00064 this->pFile = pFile;
00065 }
00066
00067 Chunk::Chunk(File* pFile, unsigned long StartPos, List* Parent) {
00068 #if DEBUG
00069 std::cout << "Chunk::Chunk(File*,ulong,bool,List*),StartPos=" << StartPos << std::endl;
00070 #endif // DEBUG
00071 this->pFile = pFile;
00072 ulStartPos = StartPos + CHUNK_HEADER_SIZE;
00073 pParent = Parent;
00074 ulPos = 0;
00075 pChunkData = NULL;
00076 ulChunkDataSize = 0;
00077 ReadHeader(StartPos);
00078 }
00079
00080 Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize) {
00081 this->pFile = pFile;
00082 ulStartPos = 0;
00083 this->pParent = pParent;
00084 ulPos = 0;
00085 pChunkData = NULL;
00086 ulChunkDataSize = 0;
00087 ChunkID = uiChunkID;
00088 CurrentChunkSize = 0;
00089 NewChunkSize = uiBodySize;
00090 }
00091
00092 Chunk::~Chunk() {
00093 pFile->UnlogResized(this);
00094 if (pChunkData) delete[] pChunkData;
00095 }
00096
00097 void Chunk::ReadHeader(unsigned long fPos) {
00098 #if DEBUG
00099 std::cout << "Chunk::Readheader(" << fPos << ") ";
00100 #endif // DEBUG
00101 #if POSIX
00102 if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) {
00103 read(pFile->hFileRead, &ChunkID, 4);
00104 read(pFile->hFileRead, &CurrentChunkSize, 4);
00105 #elif defined(WIN32)
00106 if (SetFilePointer(pFile->hFileRead, fPos, NULL, FILE_BEGIN) != INVALID_SET_FILE_POINTER) {
00107 DWORD dwBytesRead;
00108 ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL);
00109 ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL);
00110 #else
00111 if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) {
00112 fread(&ChunkID, 4, 1, pFile->hFileRead);
00113 fread(&CurrentChunkSize, 4, 1, pFile->hFileRead);
00114 #endif // POSIX
00115 #if WORDS_BIGENDIAN
00116 if (ChunkID == CHUNK_ID_RIFF) {
00117 pFile->bEndianNative = false;
00118 }
00119 #else // little endian
00120 if (ChunkID == CHUNK_ID_RIFX) {
00121 pFile->bEndianNative = false;
00122 ChunkID = CHUNK_ID_RIFF;
00123 }
00124 #endif // WORDS_BIGENDIAN
00125 if (!pFile->bEndianNative) {
00126
00127 swapBytes_32(&CurrentChunkSize);
00128 }
00129 #if DEBUG
00130 std::cout << "ckID=" << convertToString(ChunkID) << " ";
00131 std::cout << "ckSize=" << ChunkSize << " ";
00132 std::cout << "bEndianNative=" << bEndianNative << std::endl;
00133 #endif // DEBUG
00134 NewChunkSize = CurrentChunkSize;
00135 }
00136 }
00137
00138 void Chunk::WriteHeader(unsigned long fPos) {
00139 uint32_t uiNewChunkID = ChunkID;
00140 if (ChunkID == CHUNK_ID_RIFF) {
00141 #if WORDS_BIGENDIAN
00142 if (pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX;
00143 #else // little endian
00144 if (!pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX;
00145 #endif // WORDS_BIGENDIAN
00146 }
00147
00148 uint32_t uiNewChunkSize = NewChunkSize;
00149 if (!pFile->bEndianNative) {
00150 swapBytes_32(&uiNewChunkSize);
00151 }
00152
00153 #if POSIX
00154 if (lseek(pFile->hFileWrite, fPos, SEEK_SET) != -1) {
00155 write(pFile->hFileWrite, &uiNewChunkID, 4);
00156 write(pFile->hFileWrite, &uiNewChunkSize, 4);
00157 }
00158 #elif defined(WIN32)
00159 if (SetFilePointer(pFile->hFileWrite, fPos, NULL, FILE_BEGIN) != INVALID_SET_FILE_POINTER) {
00160 DWORD dwBytesWritten;
00161 WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL);
00162 WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL);
00163 }
00164 #else
00165 if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) {
00166 fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite);
00167 fwrite(&uiNewChunkSize, 4, 1, pFile->hFileWrite);
00168 }
00169 #endif // POSIX
00170 }
00171
00176 String Chunk::GetChunkIDString() {
00177 return convertToString(ChunkID);
00178 }
00179
00192 unsigned long Chunk::SetPos(unsigned long Where, stream_whence_t Whence) {
00193 #if DEBUG
00194 std::cout << "Chunk::SetPos(ulong)" << std::endl;
00195 #endif // DEBUG
00196 switch (Whence) {
00197 case stream_curpos:
00198 ulPos += Where;
00199 break;
00200 case stream_end:
00201 ulPos = CurrentChunkSize - 1 - Where;
00202 break;
00203 case stream_backward:
00204 ulPos -= Where;
00205 break;
00206 case stream_start: default:
00207 ulPos = Where;
00208 break;
00209 }
00210 if (ulPos > CurrentChunkSize) ulPos = CurrentChunkSize;
00211 return ulPos;
00212 }
00213
00224 unsigned long Chunk::RemainingBytes() {
00225 #if DEBUG
00226 std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl;
00227 #endif // DEBUG
00228 return CurrentChunkSize - ulPos;
00229 }
00230
00242 stream_state_t Chunk::GetState() {
00243 #if DEBUG
00244 std::cout << "Chunk::GetState()" << std::endl;
00245 #endif // DEBUG
00246 #if POSIX
00247 if (pFile->hFileRead == 0) return stream_closed;
00248 #elif defined (WIN32)
00249 if (pFile->hFileRead == INVALID_HANDLE_VALUE)
00250 return stream_closed;
00251 #else
00252 if (pFile->hFileRead == NULL) return stream_closed;
00253 #endif // POSIX
00254 if (ulPos < CurrentChunkSize) return stream_ready;
00255 else return stream_end_reached;
00256 }
00257
00273 unsigned long Chunk::Read(void* pData, unsigned long WordCount, unsigned long WordSize) {
00274 #if DEBUG
00275 std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl;
00276 #endif // DEBUG
00277 if (ulPos >= CurrentChunkSize) return 0;
00278 if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize;
00279 #if POSIX
00280 if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0;
00281 unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize);
00282 if (readWords < 1) return 0;
00283 readWords /= WordSize;
00284 #elif defined(WIN32)
00285 if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0;
00286 DWORD readWords;
00287 ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL);
00288 if (readWords < 1) return 0;
00289 readWords /= WordSize;
00290 #else // standard C functions
00291 if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0;
00292 unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead);
00293 #endif // POSIX
00294 if (!pFile->bEndianNative && WordSize != 1) {
00295 switch (WordSize) {
00296 case 2:
00297 for (unsigned long iWord = 0; iWord < readWords; iWord++)
00298 swapBytes_16((uint16_t*) pData + iWord);
00299 break;
00300 case 4:
00301 for (unsigned long iWord = 0; iWord < readWords; iWord++)
00302 swapBytes_32((uint32_t*) pData + iWord);
00303 break;
00304 default:
00305 for (unsigned long iWord = 0; iWord < readWords; iWord++)
00306 swapBytes((uint8_t*) pData + iWord * WordSize, WordSize);
00307 break;
00308 }
00309 }
00310 SetPos(readWords * WordSize, stream_curpos);
00311 return readWords;
00312 }
00313
00330 unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) {
00331 if (pFile->Mode != stream_mode_read_write)
00332 throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first");
00333 if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize > CurrentChunkSize)
00334 throw Exception("End of chunk reached while trying to write data");
00335 if (!pFile->bEndianNative && WordSize != 1) {
00336 switch (WordSize) {
00337 case 2:
00338 for (unsigned long iWord = 0; iWord < WordCount; iWord++)
00339 swapBytes_16((uint16_t*) pData + iWord);
00340 break;
00341 case 4:
00342 for (unsigned long iWord = 0; iWord < WordCount; iWord++)
00343 swapBytes_32((uint32_t*) pData + iWord);
00344 break;
00345 default:
00346 for (unsigned long iWord = 0; iWord < WordCount; iWord++)
00347 swapBytes((uint8_t*) pData + iWord * WordSize, WordSize);
00348 break;
00349 }
00350 }
00351 #if POSIX
00352 if (lseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET) < 0) {
00353 throw Exception("Could not seek to position " + ToString(ulPos) +
00354 " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");
00355 }
00356 unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize);
00357 if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data");
00358 writtenWords /= WordSize;
00359 #elif defined(WIN32)
00360 if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
00361 throw Exception("Could not seek to position " + ToString(ulPos) +
00362 " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");
00363 }
00364 DWORD writtenWords;
00365 WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL);
00366 if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data");
00367 writtenWords /= WordSize;
00368 #else // standard C functions
00369 if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) {
00370 throw Exception("Could not seek to position " + ToString(ulPos) +
00371 " in chunk (" + ToString(ulStartPos + ulPos) + " in file)");
00372 }
00373 unsigned long writtenWords = fwrite(pData, WordSize, WordCount, pFile->hFileWrite);
00374 #endif // POSIX
00375 SetPos(writtenWords * WordSize, stream_curpos);
00376 return writtenWords;
00377 }
00378
00380 unsigned long Chunk::ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize) {
00381 unsigned long readWords = Read(pData, WordCount, WordSize);
00382 if (readWords != WordCount) throw RIFF::Exception("End of chunk data reached.");
00383 return readWords;
00384 }
00385
00397 unsigned long Chunk::ReadInt8(int8_t* pData, unsigned long WordCount) {
00398 #if DEBUG
00399 std::cout << "Chunk::ReadInt8(int8_t*,ulong)" << std::endl;
00400 #endif // DEBUG
00401 return ReadSceptical(pData, WordCount, 1);
00402 }
00403
00418 unsigned long Chunk::WriteInt8(int8_t* pData, unsigned long WordCount) {
00419 return Write(pData, WordCount, 1);
00420 }
00421
00434 unsigned long Chunk::ReadUint8(uint8_t* pData, unsigned long WordCount) {
00435 #if DEBUG
00436 std::cout << "Chunk::ReadUint8(uint8_t*,ulong)" << std::endl;
00437 #endif // DEBUG
00438 return ReadSceptical(pData, WordCount, 1);
00439 }
00440
00455 unsigned long Chunk::WriteUint8(uint8_t* pData, unsigned long WordCount) {
00456 return Write(pData, WordCount, 1);
00457 }
00458
00471 unsigned long Chunk::ReadInt16(int16_t* pData, unsigned long WordCount) {
00472 #if DEBUG
00473 std::cout << "Chunk::ReadInt16(int16_t*,ulong)" << std::endl;
00474 #endif // DEBUG
00475 return ReadSceptical(pData, WordCount, 2);
00476 }
00477
00492 unsigned long Chunk::WriteInt16(int16_t* pData, unsigned long WordCount) {
00493 return Write(pData, WordCount, 2);
00494 }
00495
00508 unsigned long Chunk::ReadUint16(uint16_t* pData, unsigned long WordCount) {
00509 #if DEBUG
00510 std::cout << "Chunk::ReadUint16(uint16_t*,ulong)" << std::endl;
00511 #endif // DEBUG
00512 return ReadSceptical(pData, WordCount, 2);
00513 }
00514
00529 unsigned long Chunk::WriteUint16(uint16_t* pData, unsigned long WordCount) {
00530 return Write(pData, WordCount, 2);
00531 }
00532
00545 unsigned long Chunk::ReadInt32(int32_t* pData, unsigned long WordCount) {
00546 #if DEBUG
00547 std::cout << "Chunk::ReadInt32(int32_t*,ulong)" << std::endl;
00548 #endif // DEBUG
00549 return ReadSceptical(pData, WordCount, 4);
00550 }
00551
00566 unsigned long Chunk::WriteInt32(int32_t* pData, unsigned long WordCount) {
00567 return Write(pData, WordCount, 4);
00568 }
00569
00582 unsigned long Chunk::ReadUint32(uint32_t* pData, unsigned long WordCount) {
00583 #if DEBUG
00584 std::cout << "Chunk::ReadUint32(uint32_t*,ulong)" << std::endl;
00585 #endif // DEBUG
00586 return ReadSceptical(pData, WordCount, 4);
00587 }
00588
00603 unsigned long Chunk::WriteUint32(uint32_t* pData, unsigned long WordCount) {
00604 return Write(pData, WordCount, 4);
00605 }
00606
00614 int8_t Chunk::ReadInt8() {
00615 #if DEBUG
00616 std::cout << "Chunk::ReadInt8()" << std::endl;
00617 #endif // DEBUG
00618 int8_t word;
00619 ReadSceptical(&word,1,1);
00620 return word;
00621 }
00622
00630 uint8_t Chunk::ReadUint8() {
00631 #if DEBUG
00632 std::cout << "Chunk::ReadUint8()" << std::endl;
00633 #endif // DEBUG
00634 uint8_t word;
00635 ReadSceptical(&word,1,1);
00636 return word;
00637 }
00638
00647 int16_t Chunk::ReadInt16() {
00648 #if DEBUG
00649 std::cout << "Chunk::ReadInt16()" << std::endl;
00650 #endif // DEBUG
00651 int16_t word;
00652 ReadSceptical(&word,1,2);
00653 return word;
00654 }
00655
00664 uint16_t Chunk::ReadUint16() {
00665 #if DEBUG
00666 std::cout << "Chunk::ReadUint16()" << std::endl;
00667 #endif // DEBUG
00668 uint16_t word;
00669 ReadSceptical(&word,1,2);
00670 return word;
00671 }
00672
00681 int32_t Chunk::ReadInt32() {
00682 #if DEBUG
00683 std::cout << "Chunk::ReadInt32()" << std::endl;
00684 #endif // DEBUG
00685 int32_t word;
00686 ReadSceptical(&word,1,4);
00687 return word;
00688 }
00689
00698 uint32_t Chunk::ReadUint32() {
00699 #if DEBUG
00700 std::cout << "Chunk::ReadUint32()" << std::endl;
00701 #endif // DEBUG
00702 uint32_t word;
00703 ReadSceptical(&word,1,4);
00704 return word;
00705 }
00706
00728 void* Chunk::LoadChunkData() {
00729 if (!pChunkData && pFile->Filename != "") {
00730 #if POSIX
00731 if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL;
00732 #elif defined(WIN32)
00733 if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL;
00734 #else
00735 if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL;
00736 #endif // POSIX
00737 unsigned long ulBufferSize = (CurrentChunkSize > NewChunkSize) ? CurrentChunkSize : NewChunkSize;
00738 pChunkData = new uint8_t[ulBufferSize];
00739 if (!pChunkData) return NULL;
00740 memset(pChunkData, 0, ulBufferSize);
00741 #if POSIX
00742 unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize());
00743 #elif defined(WIN32)
00744 DWORD readWords;
00745 ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL);
00746 #else
00747 unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead);
00748 #endif // POSIX
00749 if (readWords != GetSize()) {
00750 delete[] pChunkData;
00751 return (pChunkData = NULL);
00752 }
00753 ulChunkDataSize = ulBufferSize;
00754 } else if (NewChunkSize > ulChunkDataSize) {
00755 uint8_t* pNewBuffer = new uint8_t[NewChunkSize];
00756 if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(NewChunkSize) + " bytes");
00757 memset(pNewBuffer, 0 , NewChunkSize);
00758 memcpy(pNewBuffer, pChunkData, ulChunkDataSize);
00759 delete[] pChunkData;
00760 pChunkData = pNewBuffer;
00761 ulChunkDataSize = NewChunkSize;
00762 }
00763 return pChunkData;
00764 }
00765
00772 void Chunk::ReleaseChunkData() {
00773 if (pChunkData) {
00774 delete[] pChunkData;
00775 pChunkData = NULL;
00776 }
00777 }
00778
00797 void Chunk::Resize(int iNewSize) {
00798 if (iNewSize <= 0)
00799 throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this));
00800 if (NewChunkSize == iNewSize) return;
00801 NewChunkSize = iNewSize;
00802 pFile->LogAsResized(this);
00803 }
00804
00817 unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) {
00818 const unsigned long ulOriginalPos = ulWritePos;
00819 ulWritePos += CHUNK_HEADER_SIZE;
00820
00821 if (pFile->Mode != stream_mode_read_write)
00822 throw Exception("Cannot write list chunk, file has to be opened in read+write mode");
00823
00824
00825 if (pChunkData) {
00826
00827 LoadChunkData();
00828
00829 #if POSIX
00830 lseek(pFile->hFileWrite, ulWritePos, SEEK_SET);
00831 if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) {
00832 throw Exception("Writing Chunk data (from RAM) failed");
00833 }
00834 #elif defined(WIN32)
00835 SetFilePointer(pFile->hFileWrite, ulWritePos, NULL, FILE_BEGIN);
00836 DWORD dwBytesWritten;
00837 WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL);
00838 if (dwBytesWritten != NewChunkSize) {
00839 throw Exception("Writing Chunk data (from RAM) failed");
00840 }
00841 #else
00842 fseek(pFile->hFileWrite, ulWritePos, SEEK_SET);
00843 if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) {
00844 throw Exception("Writing Chunk data (from RAM) failed");
00845 }
00846 #endif // POSIX
00847 } else {
00848
00849 int8_t* pCopyBuffer = new int8_t[4096];
00850 unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize;
00851 #if defined(WIN32)
00852 DWORD iBytesMoved = 1;
00853 #else
00854 int iBytesMoved = 1;
00855 #endif
00856 for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) {
00857 iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096;
00858 #if POSIX
00859 lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);
00860 iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved);
00861 lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET);
00862 iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved);
00863 #elif defined(WIN32)
00864 SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL, FILE_BEGIN);
00865 ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
00866 SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL, FILE_BEGIN);
00867 WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL);
00868 #else
00869 fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET);
00870 iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead);
00871 fseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET);
00872 iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, pFile->hFileWrite);
00873 #endif
00874 }
00875 delete[] pCopyBuffer;
00876 if (iBytesMoved < 0) throw Exception("Writing Chunk data (from file) failed");
00877 }
00878
00879
00880 CurrentChunkSize = NewChunkSize;
00881 WriteHeader(ulOriginalPos);
00882
00883
00884 ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE;
00885 ulPos = 0;
00886
00887
00888 if ((ulStartPos + NewChunkSize) % 2 != 0) {
00889 const char cPadByte = 0;
00890 #if POSIX
00891 lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);
00892 write(pFile->hFileWrite, &cPadByte, 1);
00893 #elif defined(WIN32)
00894 SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL, FILE_BEGIN);
00895 DWORD dwBytesWritten;
00896 WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL);
00897 #else
00898 fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET);
00899 fwrite(&cPadByte, 1, 1, pFile->hFileWrite);
00900 #endif
00901 return ulStartPos + NewChunkSize + 1;
00902 }
00903
00904 return ulStartPos + NewChunkSize;
00905 }
00906
00907 void Chunk::__resetPos() {
00908 ulPos = 0;
00909 }
00910
00911
00912
00913
00914
00915
00916 List::List(File* pFile) : Chunk(pFile) {
00917 #if DEBUG
00918 std::cout << "List::List(File* pFile)" << std::endl;
00919 #endif // DEBUG
00920 pSubChunks = NULL;
00921 pSubChunksMap = NULL;
00922 }
00923
00924 List::List(File* pFile, unsigned long StartPos, List* Parent)
00925 : Chunk(pFile, StartPos, Parent) {
00926 #if DEBUG
00927 std::cout << "List::List(File*,ulong,bool,List*)" << std::endl;
00928 #endif // DEBUG
00929 pSubChunks = NULL;
00930 pSubChunksMap = NULL;
00931 ReadHeader(StartPos);
00932 ulStartPos = StartPos + LIST_HEADER_SIZE;
00933 }
00934
00935 List::List(File* pFile, List* pParent, uint32_t uiListID)
00936 : Chunk(pFile, pParent, CHUNK_ID_LIST, 0) {
00937 pSubChunks = NULL;
00938 pSubChunksMap = NULL;
00939 ListType = uiListID;
00940 }
00941
00942 List::~List() {
00943 #if DEBUG
00944 std::cout << "List::~List()" << std::endl;
00945 #endif // DEBUG
00946 if (pSubChunks) {
00947 ChunkList::iterator iter = pSubChunks->begin();
00948 ChunkList::iterator end = pSubChunks->end();
00949 while (iter != end) {
00950 delete *iter;
00951 iter++;
00952 }
00953 delete pSubChunks;
00954 }
00955 if (pSubChunksMap) delete pSubChunksMap;
00956 }
00957
00969 Chunk* List::GetSubChunk(uint32_t ChunkID) {
00970 #if DEBUG
00971 std::cout << "List::GetSubChunk(uint32_t)" << std::endl;
00972 #endif // DEBUG
00973 if (!pSubChunksMap) LoadSubChunks();
00974 return (*pSubChunksMap)[ChunkID];
00975 }
00976
00988 List* List::GetSubList(uint32_t ListType) {
00989 #if DEBUG
00990 std::cout << "List::GetSubList(uint32_t)" << std::endl;
00991 #endif // DEBUG
00992 if (!pSubChunks) LoadSubChunks();
00993 ChunkList::iterator iter = pSubChunks->begin();
00994 ChunkList::iterator end = pSubChunks->end();
00995 while (iter != end) {
00996 if ((*iter)->GetChunkID() == CHUNK_ID_LIST) {
00997 List* l = (List*) *iter;
00998 if (l->GetListType() == ListType) return l;
00999 }
01000 iter++;
01001 }
01002 return NULL;
01003 }
01004
01013 Chunk* List::GetFirstSubChunk() {
01014 #if DEBUG
01015 std::cout << "List::GetFirstSubChunk()" << std::endl;
01016 #endif // DEBUG
01017 if (!pSubChunks) LoadSubChunks();
01018 ChunksIterator = pSubChunks->begin();
01019 return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
01020 }
01021
01029 Chunk* List::GetNextSubChunk() {
01030 #if DEBUG
01031 std::cout << "List::GetNextSubChunk()" << std::endl;
01032 #endif // DEBUG
01033 if (!pSubChunks) return NULL;
01034 ChunksIterator++;
01035 return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL;
01036 }
01037
01047 List* List::GetFirstSubList() {
01048 #if DEBUG
01049 std::cout << "List::GetFirstSubList()" << std::endl;
01050 #endif // DEBUG
01051 if (!pSubChunks) LoadSubChunks();
01052 ListIterator = pSubChunks->begin();
01053 ChunkList::iterator end = pSubChunks->end();
01054 while (ListIterator != end) {
01055 if ((*ListIterator)->GetChunkID() == CHUNK_ID_LIST) return (List*) *ListIterator;
01056 ListIterator++;
01057 }
01058 return NULL;
01059 }
01060
01069 List* List::GetNextSubList() {
01070 #if DEBUG
01071 std::cout << "List::GetNextSubList()" << std::endl;
01072 #endif // DEBUG
01073 if (!pSubChunks) return NULL;
01074 if (ListIterator == pSubChunks->end()) return NULL;
01075 ListIterator++;
01076 ChunkList::iterator end = pSubChunks->end();
01077 while (ListIterator != end) {
01078 if ((*ListIterator)->GetChunkID() == CHUNK_ID_LIST) return (List*) *ListIterator;
01079 ListIterator++;
01080 }
01081 return NULL;
01082 }
01083
01087 unsigned int List::CountSubChunks() {
01088 if (!pSubChunks) LoadSubChunks();
01089 return pSubChunks->size();
01090 }
01091
01096 unsigned int List::CountSubChunks(uint32_t ChunkID) {
01097 unsigned int result = 0;
01098 if (!pSubChunks) LoadSubChunks();
01099 ChunkList::iterator iter = pSubChunks->begin();
01100 ChunkList::iterator end = pSubChunks->end();
01101 while (iter != end) {
01102 if ((*iter)->GetChunkID() == ChunkID) {
01103 result++;
01104 }
01105 iter++;
01106 }
01107 return result;
01108 }
01109
01113 unsigned int List::CountSubLists() {
01114 return CountSubChunks(CHUNK_ID_LIST);
01115 }
01116
01121 unsigned int List::CountSubLists(uint32_t ListType) {
01122 unsigned int result = 0;
01123 if (!pSubChunks) LoadSubChunks();
01124 ChunkList::iterator iter = pSubChunks->begin();
01125 ChunkList::iterator end =