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-2009 by Christian Schoenebeck * |
* Copyright (C) 2003-2016 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 * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
|
|
24 |
#include <algorithm> |
#include <algorithm> |
25 |
|
#include <set> |
26 |
#include <string.h> |
#include <string.h> |
27 |
|
|
28 |
#include "RIFF.h" |
#include "RIFF.h" |
29 |
|
|
30 |
#include "helper.h" |
#include "helper.h" |
31 |
|
|
32 |
|
#if POSIX |
33 |
|
# include <errno.h> |
34 |
|
#endif |
35 |
|
|
36 |
namespace RIFF { |
namespace RIFF { |
37 |
|
|
38 |
// *************** Internal functions ************** |
// *************** Internal functions ************** |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
// *************** progress_t *************** |
58 |
|
// * |
59 |
|
|
60 |
|
progress_t::progress_t() { |
61 |
|
callback = NULL; |
62 |
|
custom = NULL; |
63 |
|
__range_min = 0.0f; |
64 |
|
__range_max = 1.0f; |
65 |
|
} |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
// *************** Chunk ************** |
// *************** Chunk ************** |
70 |
// * |
// * |
71 |
|
|
73 |
#if DEBUG |
#if DEBUG |
74 |
std::cout << "Chunk::Chunk(File* pFile)" << std::endl; |
std::cout << "Chunk::Chunk(File* pFile)" << std::endl; |
75 |
#endif // DEBUG |
#endif // DEBUG |
76 |
ulPos = 0; |
ullPos = 0; |
77 |
pParent = NULL; |
pParent = NULL; |
78 |
pChunkData = NULL; |
pChunkData = NULL; |
79 |
ulChunkDataSize = 0; |
ullCurrentChunkSize = 0; |
80 |
|
ullNewChunkSize = 0; |
81 |
|
ullChunkDataSize = 0; |
82 |
ChunkID = CHUNK_ID_RIFF; |
ChunkID = CHUNK_ID_RIFF; |
83 |
this->pFile = pFile; |
this->pFile = pFile; |
84 |
} |
} |
85 |
|
|
86 |
Chunk::Chunk(File* pFile, unsigned long StartPos, List* Parent) { |
Chunk::Chunk(File* pFile, file_offset_t StartPos, List* Parent) { |
87 |
#if DEBUG |
#if DEBUG |
88 |
std::cout << "Chunk::Chunk(File*,ulong,bool,List*),StartPos=" << StartPos << std::endl; |
std::cout << "Chunk::Chunk(File*,file_offset_t,List*),StartPos=" << StartPos << std::endl; |
89 |
#endif // DEBUG |
#endif // DEBUG |
90 |
this->pFile = pFile; |
this->pFile = pFile; |
91 |
ulStartPos = StartPos + CHUNK_HEADER_SIZE; |
ullStartPos = StartPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
92 |
pParent = Parent; |
pParent = Parent; |
93 |
ulPos = 0; |
ullPos = 0; |
94 |
pChunkData = NULL; |
pChunkData = NULL; |
95 |
ulChunkDataSize = 0; |
ullCurrentChunkSize = 0; |
96 |
|
ullNewChunkSize = 0; |
97 |
|
ullChunkDataSize = 0; |
98 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
99 |
} |
} |
100 |
|
|
101 |
Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, uint uiBodySize) { |
Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize) { |
102 |
this->pFile = pFile; |
this->pFile = pFile; |
103 |
ulStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk |
ullStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk |
104 |
this->pParent = pParent; |
this->pParent = pParent; |
105 |
ulPos = 0; |
ullPos = 0; |
106 |
pChunkData = NULL; |
pChunkData = NULL; |
|
ulChunkDataSize = 0; |
|
107 |
ChunkID = uiChunkID; |
ChunkID = uiChunkID; |
108 |
CurrentChunkSize = 0; |
ullChunkDataSize = 0; |
109 |
NewChunkSize = uiBodySize; |
ullCurrentChunkSize = 0; |
110 |
|
ullNewChunkSize = ullBodySize; |
111 |
} |
} |
112 |
|
|
113 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
|
pFile->UnlogResized(this); |
|
114 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
115 |
} |
} |
116 |
|
|
117 |
void Chunk::ReadHeader(unsigned long fPos) { |
void Chunk::ReadHeader(file_offset_t filePos) { |
118 |
#if DEBUG |
#if DEBUG |
119 |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
std::cout << "Chunk::Readheader(" << filePos << ") "; |
120 |
#endif // DEBUG |
#endif // DEBUG |
121 |
|
ChunkID = 0; |
122 |
|
ullNewChunkSize = ullCurrentChunkSize = 0; |
123 |
#if POSIX |
#if POSIX |
124 |
if (lseek(pFile->hFileRead, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileRead, filePos, SEEK_SET) != -1) { |
125 |
read(pFile->hFileRead, &ChunkID, 4); |
read(pFile->hFileRead, &ChunkID, 4); |
126 |
read(pFile->hFileRead, &CurrentChunkSize, 4); |
read(pFile->hFileRead, &ullCurrentChunkSize, pFile->FileOffsetSize); |
127 |
#elif defined(WIN32) |
#elif defined(WIN32) |
128 |
if (SetFilePointer(pFile->hFileRead, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
LARGE_INTEGER liFilePos; |
129 |
|
liFilePos.QuadPart = filePos; |
130 |
|
if (SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
131 |
DWORD dwBytesRead; |
DWORD dwBytesRead; |
132 |
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
133 |
ReadFile(pFile->hFileRead, &CurrentChunkSize, 4, &dwBytesRead, NULL); |
ReadFile(pFile->hFileRead, &ullCurrentChunkSize, pFile->FileOffsetSize, &dwBytesRead, NULL); |
134 |
#else |
#else |
135 |
if (!fseek(pFile->hFileRead, fPos, SEEK_SET)) { |
if (!fseeko(pFile->hFileRead, filePos, SEEK_SET)) { |
136 |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
137 |
fread(&CurrentChunkSize, 4, 1, pFile->hFileRead); |
fread(&ullCurrentChunkSize, pFile->FileOffsetSize, 1, pFile->hFileRead); |
138 |
#endif // POSIX |
#endif // POSIX |
139 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
140 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
148 |
#endif // WORDS_BIGENDIAN |
#endif // WORDS_BIGENDIAN |
149 |
if (!pFile->bEndianNative) { |
if (!pFile->bEndianNative) { |
150 |
//swapBytes_32(&ChunkID); |
//swapBytes_32(&ChunkID); |
151 |
swapBytes_32(&CurrentChunkSize); |
if (pFile->FileOffsetSize == 4) |
152 |
|
swapBytes_32(&ullCurrentChunkSize); |
153 |
|
else |
154 |
|
swapBytes_64(&ullCurrentChunkSize); |
155 |
} |
} |
156 |
#if DEBUG |
#if DEBUG |
157 |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
158 |
std::cout << "ckSize=" << ChunkSize << " "; |
std::cout << "ckSize=" << ullCurrentChunkSize << " "; |
159 |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; |
160 |
#endif // DEBUG |
#endif // DEBUG |
161 |
NewChunkSize = CurrentChunkSize; |
ullNewChunkSize = ullCurrentChunkSize; |
162 |
} |
} |
163 |
} |
} |
164 |
|
|
165 |
void Chunk::WriteHeader(unsigned long fPos) { |
void Chunk::WriteHeader(file_offset_t filePos) { |
166 |
uint32_t uiNewChunkID = ChunkID; |
uint32_t uiNewChunkID = ChunkID; |
167 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
168 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
172 |
#endif // WORDS_BIGENDIAN |
#endif // WORDS_BIGENDIAN |
173 |
} |
} |
174 |
|
|
175 |
uint32_t uiNewChunkSize = NewChunkSize; |
uint64_t ullNewChunkSize = this->ullNewChunkSize; |
176 |
if (!pFile->bEndianNative) { |
if (!pFile->bEndianNative) { |
177 |
swapBytes_32(&uiNewChunkSize); |
if (pFile->FileOffsetSize == 4) |
178 |
|
swapBytes_32(&ullNewChunkSize); |
179 |
|
else |
180 |
|
swapBytes_64(&ullNewChunkSize); |
181 |
} |
} |
182 |
|
|
183 |
#if POSIX |
#if POSIX |
184 |
if (lseek(pFile->hFileWrite, fPos, SEEK_SET) != -1) { |
if (lseek(pFile->hFileWrite, filePos, SEEK_SET) != -1) { |
185 |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
186 |
write(pFile->hFileWrite, &uiNewChunkSize, 4); |
write(pFile->hFileWrite, &ullNewChunkSize, pFile->FileOffsetSize); |
187 |
} |
} |
188 |
#elif defined(WIN32) |
#elif defined(WIN32) |
189 |
if (SetFilePointer(pFile->hFileWrite, fPos, NULL/*32 bit*/, FILE_BEGIN) != INVALID_SET_FILE_POINTER) { |
LARGE_INTEGER liFilePos; |
190 |
|
liFilePos.QuadPart = filePos; |
191 |
|
if (SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
192 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
193 |
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
194 |
WriteFile(pFile->hFileWrite, &uiNewChunkSize, 4, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &ullNewChunkSize, pFile->FileOffsetSize, &dwBytesWritten, NULL); |
195 |
} |
} |
196 |
#else |
#else |
197 |
if (!fseek(pFile->hFileWrite, fPos, SEEK_SET)) { |
if (!fseeko(pFile->hFileWrite, filePos, SEEK_SET)) { |
198 |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
199 |
fwrite(&uiNewChunkSize, 4, 1, pFile->hFileWrite); |
fwrite(&ullNewChunkSize, pFile->FileOffsetSize, 1, pFile->hFileWrite); |
200 |
} |
} |
201 |
#endif // POSIX |
#endif // POSIX |
202 |
} |
} |
205 |
* Returns the String representation of the chunk's ID (e.g. "RIFF", |
* Returns the String representation of the chunk's ID (e.g. "RIFF", |
206 |
* "LIST"). |
* "LIST"). |
207 |
*/ |
*/ |
208 |
String Chunk::GetChunkIDString() { |
String Chunk::GetChunkIDString() const { |
209 |
return convertToString(ChunkID); |
return convertToString(ChunkID); |
210 |
} |
} |
211 |
|
|
221 |
* if omitted \a Where relates to beginning of the chunk |
* if omitted \a Where relates to beginning of the chunk |
222 |
* data |
* data |
223 |
*/ |
*/ |
224 |
unsigned long Chunk::SetPos(unsigned long Where, stream_whence_t Whence) { |
file_offset_t Chunk::SetPos(file_offset_t Where, stream_whence_t Whence) { |
225 |
#if DEBUG |
#if DEBUG |
226 |
std::cout << "Chunk::SetPos(ulong)" << std::endl; |
std::cout << "Chunk::SetPos(file_offset_t,stream_whence_t)" << std::endl; |
227 |
#endif // DEBUG |
#endif // DEBUG |
228 |
switch (Whence) { |
switch (Whence) { |
229 |
case stream_curpos: |
case stream_curpos: |
230 |
ulPos += Where; |
ullPos += Where; |
231 |
break; |
break; |
232 |
case stream_end: |
case stream_end: |
233 |
ulPos = CurrentChunkSize - 1 - Where; |
ullPos = ullCurrentChunkSize - 1 - Where; |
234 |
break; |
break; |
235 |
case stream_backward: |
case stream_backward: |
236 |
ulPos -= Where; |
ullPos -= Where; |
237 |
break; |
break; |
238 |
case stream_start: default: |
case stream_start: default: |
239 |
ulPos = Where; |
ullPos = Where; |
240 |
break; |
break; |
241 |
} |
} |
242 |
if (ulPos > CurrentChunkSize) ulPos = CurrentChunkSize; |
if (ullPos > ullCurrentChunkSize) ullPos = ullCurrentChunkSize; |
243 |
return ulPos; |
return ullPos; |
244 |
} |
} |
245 |
|
|
246 |
/** |
/** |
253 |
* |
* |
254 |
* @returns number of bytes left to read |
* @returns number of bytes left to read |
255 |
*/ |
*/ |
256 |
unsigned long Chunk::RemainingBytes() { |
file_offset_t Chunk::RemainingBytes() const { |
257 |
#if DEBUG |
#if DEBUG |
258 |
std::cout << "Chunk::Remainingbytes()=" << CurrentChunkSize - ulPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << ullCurrentChunkSize - ullPos << std::endl; |
259 |
#endif // DEBUG |
#endif // DEBUG |
260 |
return CurrentChunkSize - ulPos; |
return (ullCurrentChunkSize > ullPos) ? ullCurrentChunkSize - ullPos : 0; |
261 |
|
} |
262 |
|
|
263 |
|
/** |
264 |
|
* Returns the actual total size in bytes (including header) of this Chunk |
265 |
|
* if being stored to a file. |
266 |
|
* |
267 |
|
* @param fileOffsetSize - RIFF file offset size (in bytes) assumed when |
268 |
|
* being saved to a file |
269 |
|
*/ |
270 |
|
file_offset_t Chunk::RequiredPhysicalSize(int fileOffsetSize) { |
271 |
|
return CHUNK_HEADER_SIZE(fileOffsetSize) + // RIFF chunk header |
272 |
|
ullNewChunkSize + // chunks's actual data body |
273 |
|
ullNewChunkSize % 2; // optional pad byte |
274 |
} |
} |
275 |
|
|
276 |
/** |
/** |
284 |
* already reached the end of the chunk data, no more reading |
* already reached the end of the chunk data, no more reading |
285 |
* possible without SetPos() |
* possible without SetPos() |
286 |
*/ |
*/ |
287 |
stream_state_t Chunk::GetState() { |
stream_state_t Chunk::GetState() const { |
288 |
#if DEBUG |
#if DEBUG |
289 |
std::cout << "Chunk::GetState()" << std::endl; |
std::cout << "Chunk::GetState()" << std::endl; |
290 |
#endif // DEBUG |
#endif // DEBUG |
291 |
#if POSIX |
#if POSIX |
292 |
if (pFile->hFileRead == 0) return stream_closed; |
if (pFile->hFileRead == 0) return stream_closed; |
293 |
#elif defined (WIN32) |
#elif defined (WIN32) |
296 |
#else |
#else |
297 |
if (pFile->hFileRead == NULL) return stream_closed; |
if (pFile->hFileRead == NULL) return stream_closed; |
298 |
#endif // POSIX |
#endif // POSIX |
299 |
if (ulPos < CurrentChunkSize) return stream_ready; |
if (ullPos < ullCurrentChunkSize) return stream_ready; |
300 |
else return stream_end_reached; |
else return stream_end_reached; |
301 |
} |
} |
302 |
|
|
303 |
/** |
/** |
313 |
* @param WordCount number of data words to read |
* @param WordCount number of data words to read |
314 |
* @param WordSize size of each data word to read |
* @param WordSize size of each data word to read |
315 |
* @returns number of successfully read data words or 0 if end |
* @returns number of successfully read data words or 0 if end |
316 |
* of file reached or error occured |
* of file reached or error occurred |
317 |
*/ |
*/ |
318 |
unsigned long Chunk::Read(void* pData, unsigned long WordCount, unsigned long WordSize) { |
file_offset_t Chunk::Read(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
319 |
#if DEBUG |
#if DEBUG |
320 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,file_offset_t,file_offset_t)" << std::endl; |
321 |
#endif // DEBUG |
#endif // DEBUG |
322 |
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) |
323 |
if (ulPos >= CurrentChunkSize) return 0; |
if (ullPos >= ullCurrentChunkSize) return 0; |
324 |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
if (ullPos + WordCount * WordSize >= ullCurrentChunkSize) WordCount = (ullCurrentChunkSize - ullPos) / WordSize; |
325 |
#if POSIX |
#if POSIX |
326 |
if (lseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
if (lseek(pFile->hFileRead, ullStartPos + ullPos, SEEK_SET) < 0) return 0; |
327 |
unsigned long readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
ssize_t readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
328 |
if (readWords < 1) return 0; |
if (readWords < 1) { |
329 |
|
#if DEBUG |
330 |
|
std::cerr << "POSIX read() failed: " << strerror(errno) << std::endl << std::flush; |
331 |
|
#endif // DEBUG |
332 |
|
return 0; |
333 |
|
} |
334 |
readWords /= WordSize; |
readWords /= WordSize; |
335 |
#elif defined(WIN32) |
#elif defined(WIN32) |
336 |
if (SetFilePointer(pFile->hFileRead, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return 0; |
LARGE_INTEGER liFilePos; |
337 |
|
liFilePos.QuadPart = ullStartPos + ullPos; |
338 |
|
if (!SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) |
339 |
|
return 0; |
340 |
DWORD readWords; |
DWORD readWords; |
341 |
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); |
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); //FIXME: does not work for reading buffers larger than 2GB (even though this should rarely be the case in practice) |
342 |
if (readWords < 1) return 0; |
if (readWords < 1) return 0; |
343 |
readWords /= WordSize; |
readWords /= WordSize; |
344 |
#else // standard C functions |
#else // standard C functions |
345 |
if (fseek(pFile->hFileRead, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseeko(pFile->hFileRead, ullStartPos + ullPos, SEEK_SET)) return 0; |
346 |
unsigned long readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
file_offset_t readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
347 |
#endif // POSIX |
#endif // POSIX |
348 |
if (!pFile->bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
349 |
switch (WordSize) { |
switch (WordSize) { |
350 |
case 2: |
case 2: |
351 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
352 |
swapBytes_16((uint16_t*) pData + iWord); |
swapBytes_16((uint16_t*) pData + iWord); |
353 |
break; |
break; |
354 |
case 4: |
case 4: |
355 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
356 |
swapBytes_32((uint32_t*) pData + iWord); |
swapBytes_32((uint32_t*) pData + iWord); |
357 |
break; |
break; |
358 |
|
case 8: |
359 |
|
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
360 |
|
swapBytes_64((uint64_t*) pData + iWord); |
361 |
|
break; |
362 |
default: |
default: |
363 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
364 |
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
365 |
break; |
break; |
366 |
} |
} |
382 |
* @param WordSize size of each data word to write |
* @param WordSize size of each data word to write |
383 |
* @returns number of successfully written data words |
* @returns number of successfully written data words |
384 |
* @throws RIFF::Exception if write operation would exceed current |
* @throws RIFF::Exception if write operation would exceed current |
385 |
* chunk size or any IO error occured |
* chunk size or any IO error occurred |
386 |
* @see Resize() |
* @see Resize() |
387 |
*/ |
*/ |
388 |
unsigned long Chunk::Write(void* pData, unsigned long WordCount, unsigned long WordSize) { |
file_offset_t Chunk::Write(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
389 |
if (pFile->Mode != stream_mode_read_write) |
if (pFile->Mode != stream_mode_read_write) |
390 |
throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first"); |
throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first"); |
391 |
if (ulPos >= CurrentChunkSize || ulPos + WordCount * WordSize > CurrentChunkSize) |
if (ullPos >= ullCurrentChunkSize || ullPos + WordCount * WordSize > ullCurrentChunkSize) |
392 |
throw Exception("End of chunk reached while trying to write data"); |
throw Exception("End of chunk reached while trying to write data"); |
393 |
if (!pFile->bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
394 |
switch (WordSize) { |
switch (WordSize) { |
395 |
case 2: |
case 2: |
396 |
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
397 |
swapBytes_16((uint16_t*) pData + iWord); |
swapBytes_16((uint16_t*) pData + iWord); |
398 |
break; |
break; |
399 |
case 4: |
case 4: |
400 |
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
401 |
swapBytes_32((uint32_t*) pData + iWord); |
swapBytes_32((uint32_t*) pData + iWord); |
402 |
break; |
break; |
403 |
|
case 8: |
404 |
|
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
405 |
|
swapBytes_64((uint64_t*) pData + iWord); |
406 |
|
break; |
407 |
default: |
default: |
408 |
for (unsigned long iWord = 0; iWord < WordCount; iWord++) |
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
409 |
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
410 |
break; |
break; |
411 |
} |
} |
412 |
} |
} |
413 |
#if POSIX |
#if POSIX |
414 |
if (lseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET) < 0) { |
if (lseek(pFile->hFileWrite, ullStartPos + ullPos, SEEK_SET) < 0) { |
415 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
throw Exception("Could not seek to position " + ToString(ullPos) + |
416 |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
" in chunk (" + ToString(ullStartPos + ullPos) + " in file)"); |
417 |
} |
} |
418 |
unsigned long writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
ssize_t writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
419 |
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
420 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
421 |
#elif defined(WIN32) |
#elif defined(WIN32) |
422 |
if (SetFilePointer(pFile->hFileWrite, ulStartPos + ulPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) { |
LARGE_INTEGER liFilePos; |
423 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
liFilePos.QuadPart = ullStartPos + ullPos; |
424 |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
if (!SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
425 |
|
throw Exception("Could not seek to position " + ToString(ullPos) + |
426 |
|
" in chunk (" + ToString(ullStartPos + ullPos) + " in file)"); |
427 |
} |
} |
428 |
DWORD writtenWords; |
DWORD writtenWords; |
429 |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); //FIXME: does not work for writing buffers larger than 2GB (even though this should rarely be the case in practice) |
430 |
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
431 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
432 |
#else // standard C functions |
#else // standard C functions |
433 |
if (fseek(pFile->hFileWrite, ulStartPos + ulPos, SEEK_SET)) { |
if (fseeko(pFile->hFileWrite, ullStartPos + ullPos, SEEK_SET)) { |
434 |
throw Exception("Could not seek to position " + ToString(ulPos) + |
throw Exception("Could not seek to position " + ToString(ullPos) + |
435 |
" in chunk (" + ToString(ulStartPos + ulPos) + " in file)"); |
" in chunk (" + ToString(ullStartPos + ullPos) + " in file)"); |
436 |
} |
} |
437 |
unsigned long writtenWords = fwrite(pData, WordSize, WordCount, pFile->hFileWrite); |
file_offset_t writtenWords = fwrite(pData, WordSize, WordCount, pFile->hFileWrite); |
438 |
#endif // POSIX |
#endif // POSIX |
439 |
SetPos(writtenWords * WordSize, stream_curpos); |
SetPos(writtenWords * WordSize, stream_curpos); |
440 |
return writtenWords; |
return writtenWords; |
441 |
} |
} |
442 |
|
|
443 |
/** Just an internal wrapper for the main <i>Read()</i> method with additional Exception throwing on errors. */ |
/** Just an internal wrapper for the main <i>Read()</i> method with additional Exception throwing on errors. */ |
444 |
unsigned long Chunk::ReadSceptical(void* pData, unsigned long WordCount, unsigned long WordSize) { |
file_offset_t Chunk::ReadSceptical(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
445 |
unsigned long readWords = Read(pData, WordCount, WordSize); |
file_offset_t readWords = Read(pData, WordCount, WordSize); |
446 |
if (readWords != WordCount) throw RIFF::Exception("End of chunk data reached."); |
if (readWords != WordCount) throw RIFF::Exception("End of chunk data reached."); |
447 |
return readWords; |
return readWords; |
448 |
} |
} |
455 |
* @param pData destination buffer |
* @param pData destination buffer |
456 |
* @param WordCount number of 8 Bit signed integers to read |
* @param WordCount number of 8 Bit signed integers to read |
457 |
* @returns number of read integers |
* @returns number of read integers |
458 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
459 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
460 |
*/ |
*/ |
461 |
unsigned long Chunk::ReadInt8(int8_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadInt8(int8_t* pData, file_offset_t WordCount) { |
462 |
#if DEBUG |
#if DEBUG |
463 |
std::cout << "Chunk::ReadInt8(int8_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadInt8(int8_t*,file_offset_t)" << std::endl; |
464 |
#endif // DEBUG |
#endif // DEBUG |
465 |
return ReadSceptical(pData, WordCount, 1); |
return ReadSceptical(pData, WordCount, 1); |
466 |
} |
} |
467 |
|
|
476 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
477 |
* @param WordCount number of 8 Bit signed integers to write |
* @param WordCount number of 8 Bit signed integers to write |
478 |
* @returns number of written integers |
* @returns number of written integers |
479 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
480 |
* @see Resize() |
* @see Resize() |
481 |
*/ |
*/ |
482 |
unsigned long Chunk::WriteInt8(int8_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteInt8(int8_t* pData, file_offset_t WordCount) { |
483 |
return Write(pData, WordCount, 1); |
return Write(pData, WordCount, 1); |
484 |
} |
} |
485 |
|
|
492 |
* @param pData destination buffer |
* @param pData destination buffer |
493 |
* @param WordCount number of 8 Bit unsigned integers to read |
* @param WordCount number of 8 Bit unsigned integers to read |
494 |
* @returns number of read integers |
* @returns number of read integers |
495 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
496 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
497 |
*/ |
*/ |
498 |
unsigned long Chunk::ReadUint8(uint8_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadUint8(uint8_t* pData, file_offset_t WordCount) { |
499 |
#if DEBUG |
#if DEBUG |
500 |
std::cout << "Chunk::ReadUint8(uint8_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadUint8(uint8_t*,file_offset_t)" << std::endl; |
501 |
#endif // DEBUG |
#endif // DEBUG |
502 |
return ReadSceptical(pData, WordCount, 1); |
return ReadSceptical(pData, WordCount, 1); |
503 |
} |
} |
504 |
|
|
513 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
514 |
* @param WordCount number of 8 Bit unsigned integers to write |
* @param WordCount number of 8 Bit unsigned integers to write |
515 |
* @returns number of written integers |
* @returns number of written integers |
516 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
517 |
* @see Resize() |
* @see Resize() |
518 |
*/ |
*/ |
519 |
unsigned long Chunk::WriteUint8(uint8_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteUint8(uint8_t* pData, file_offset_t WordCount) { |
520 |
return Write(pData, WordCount, 1); |
return Write(pData, WordCount, 1); |
521 |
} |
} |
522 |
|
|
529 |
* @param pData destination buffer |
* @param pData destination buffer |
530 |
* @param WordCount number of 16 Bit signed integers to read |
* @param WordCount number of 16 Bit signed integers to read |
531 |
* @returns number of read integers |
* @returns number of read integers |
532 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
533 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
534 |
*/ |
*/ |
535 |
unsigned long Chunk::ReadInt16(int16_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadInt16(int16_t* pData, file_offset_t WordCount) { |
536 |
#if DEBUG |
#if DEBUG |
537 |
std::cout << "Chunk::ReadInt16(int16_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadInt16(int16_t*,file_offset_t)" << std::endl; |
538 |
#endif // DEBUG |
#endif // DEBUG |
539 |
return ReadSceptical(pData, WordCount, 2); |
return ReadSceptical(pData, WordCount, 2); |
540 |
} |
} |
541 |
|
|
550 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
551 |
* @param WordCount number of 16 Bit signed integers to write |
* @param WordCount number of 16 Bit signed integers to write |
552 |
* @returns number of written integers |
* @returns number of written integers |
553 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
554 |
* @see Resize() |
* @see Resize() |
555 |
*/ |
*/ |
556 |
unsigned long Chunk::WriteInt16(int16_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteInt16(int16_t* pData, file_offset_t WordCount) { |
557 |
return Write(pData, WordCount, 2); |
return Write(pData, WordCount, 2); |
558 |
} |
} |
559 |
|
|
566 |
* @param pData destination buffer |
* @param pData destination buffer |
567 |
* @param WordCount number of 8 Bit unsigned integers to read |
* @param WordCount number of 8 Bit unsigned integers to read |
568 |
* @returns number of read integers |
* @returns number of read integers |
569 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
570 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
571 |
*/ |
*/ |
572 |
unsigned long Chunk::ReadUint16(uint16_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadUint16(uint16_t* pData, file_offset_t WordCount) { |
573 |
#if DEBUG |
#if DEBUG |
574 |
std::cout << "Chunk::ReadUint16(uint16_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadUint16(uint16_t*,file_offset_t)" << std::endl; |
575 |
#endif // DEBUG |
#endif // DEBUG |
576 |
return ReadSceptical(pData, WordCount, 2); |
return ReadSceptical(pData, WordCount, 2); |
577 |
} |
} |
578 |
|
|
587 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
588 |
* @param WordCount number of 16 Bit unsigned integers to write |
* @param WordCount number of 16 Bit unsigned integers to write |
589 |
* @returns number of written integers |
* @returns number of written integers |
590 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
591 |
* @see Resize() |
* @see Resize() |
592 |
*/ |
*/ |
593 |
unsigned long Chunk::WriteUint16(uint16_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteUint16(uint16_t* pData, file_offset_t WordCount) { |
594 |
return Write(pData, WordCount, 2); |
return Write(pData, WordCount, 2); |
595 |
} |
} |
596 |
|
|
603 |
* @param pData destination buffer |
* @param pData destination buffer |
604 |
* @param WordCount number of 32 Bit signed integers to read |
* @param WordCount number of 32 Bit signed integers to read |
605 |
* @returns number of read integers |
* @returns number of read integers |
606 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
607 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
608 |
*/ |
*/ |
609 |
unsigned long Chunk::ReadInt32(int32_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadInt32(int32_t* pData, file_offset_t WordCount) { |
610 |
#if DEBUG |
#if DEBUG |
611 |
std::cout << "Chunk::ReadInt32(int32_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadInt32(int32_t*,file_offset_t)" << std::endl; |
612 |
#endif // DEBUG |
#endif // DEBUG |
613 |
return ReadSceptical(pData, WordCount, 4); |
return ReadSceptical(pData, WordCount, 4); |
614 |
} |
} |
615 |
|
|
624 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
625 |
* @param WordCount number of 32 Bit signed integers to write |
* @param WordCount number of 32 Bit signed integers to write |
626 |
* @returns number of written integers |
* @returns number of written integers |
627 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
628 |
* @see Resize() |
* @see Resize() |
629 |
*/ |
*/ |
630 |
unsigned long Chunk::WriteInt32(int32_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteInt32(int32_t* pData, file_offset_t WordCount) { |
631 |
return Write(pData, WordCount, 4); |
return Write(pData, WordCount, 4); |
632 |
} |
} |
633 |
|
|
640 |
* @param pData destination buffer |
* @param pData destination buffer |
641 |
* @param WordCount number of 32 Bit unsigned integers to read |
* @param WordCount number of 32 Bit unsigned integers to read |
642 |
* @returns number of read integers |
* @returns number of read integers |
643 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
644 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
645 |
*/ |
*/ |
646 |
unsigned long Chunk::ReadUint32(uint32_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadUint32(uint32_t* pData, file_offset_t WordCount) { |
647 |
#if DEBUG |
#if DEBUG |
648 |
std::cout << "Chunk::ReadUint32(uint32_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadUint32(uint32_t*,file_offset_t)" << std::endl; |
649 |
#endif // DEBUG |
#endif // DEBUG |
650 |
return ReadSceptical(pData, WordCount, 4); |
return ReadSceptical(pData, WordCount, 4); |
651 |
} |
} |
652 |
|
|
653 |
/** |
/** |
654 |
|
* Reads a null-padded string of size characters and copies it |
655 |
|
* into the string \a s. The position within the chunk will |
656 |
|
* automatically be incremented. |
657 |
|
* |
658 |
|
* @param s destination string |
659 |
|
* @param size number of characters to read |
660 |
|
* @throws RIFF::Exception if an error occurred or less than |
661 |
|
* \a size characters could be read! |
662 |
|
*/ |
663 |
|
void Chunk::ReadString(String& s, int size) { |
664 |
|
char* buf = new char[size]; |
665 |
|
ReadSceptical(buf, 1, size); |
666 |
|
s.assign(buf, std::find(buf, buf + size, '\0')); |
667 |
|
delete[] buf; |
668 |
|
} |
669 |
|
|
670 |
|
/** |
671 |
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
672 |
* buffer pointed by \a pData to the chunk's body, directly to the |
* buffer pointed by \a pData to the chunk's body, directly to the |
673 |
* actual "physical" file. The position within the chunk will |
* actual "physical" file. The position within the chunk will |
678 |
* @param pData source buffer (containing the data) |
* @param pData source buffer (containing the data) |
679 |
* @param WordCount number of 32 Bit unsigned integers to write |
* @param WordCount number of 32 Bit unsigned integers to write |
680 |
* @returns number of written integers |
* @returns number of written integers |
681 |
* @throws RIFF::Exception if an IO error occured |
* @throws RIFF::Exception if an IO error occurred |
682 |
* @see Resize() |
* @see Resize() |
683 |
*/ |
*/ |
684 |
unsigned long Chunk::WriteUint32(uint32_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::WriteUint32(uint32_t* pData, file_offset_t WordCount) { |
685 |
return Write(pData, WordCount, 4); |
return Write(pData, WordCount, 4); |
686 |
} |
} |
687 |
|
|
690 |
* the chunk. |
* the chunk. |
691 |
* |
* |
692 |
* @returns read integer word |
* @returns read integer word |
693 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
694 |
*/ |
*/ |
695 |
int8_t Chunk::ReadInt8() { |
int8_t Chunk::ReadInt8() { |
696 |
#if DEBUG |
#if DEBUG |
697 |
std::cout << "Chunk::ReadInt8()" << std::endl; |
std::cout << "Chunk::ReadInt8()" << std::endl; |
698 |
#endif // DEBUG |
#endif // DEBUG |
699 |
int8_t word; |
int8_t word; |
700 |
ReadSceptical(&word,1,1); |
ReadSceptical(&word,1,1); |
701 |
return word; |
return word; |
706 |
* within the chunk. |
* within the chunk. |
707 |
* |
* |
708 |
* @returns read integer word |
* @returns read integer word |
709 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
710 |
*/ |
*/ |
711 |
uint8_t Chunk::ReadUint8() { |
uint8_t Chunk::ReadUint8() { |
712 |
#if DEBUG |
#if DEBUG |
713 |
std::cout << "Chunk::ReadUint8()" << std::endl; |
std::cout << "Chunk::ReadUint8()" << std::endl; |
714 |
#endif // DEBUG |
#endif // DEBUG |
715 |
uint8_t word; |
uint8_t word; |
716 |
ReadSceptical(&word,1,1); |
ReadSceptical(&word,1,1); |
717 |
return word; |
return word; |
723 |
* needed. |
* needed. |
724 |
* |
* |
725 |
* @returns read integer word |
* @returns read integer word |
726 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
727 |
*/ |
*/ |
728 |
int16_t Chunk::ReadInt16() { |
int16_t Chunk::ReadInt16() { |
729 |
#if DEBUG |
#if DEBUG |
730 |
std::cout << "Chunk::ReadInt16()" << std::endl; |
std::cout << "Chunk::ReadInt16()" << std::endl; |
731 |
#endif // DEBUG |
#endif // DEBUG |
732 |
int16_t word; |
int16_t word; |
733 |
ReadSceptical(&word,1,2); |
ReadSceptical(&word,1,2); |
734 |
return word; |
return word; |
740 |
* needed. |
* needed. |
741 |
* |
* |
742 |
* @returns read integer word |
* @returns read integer word |
743 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
744 |
*/ |
*/ |
745 |
uint16_t Chunk::ReadUint16() { |
uint16_t Chunk::ReadUint16() { |
746 |
#if DEBUG |
#if DEBUG |
747 |
std::cout << "Chunk::ReadUint16()" << std::endl; |
std::cout << "Chunk::ReadUint16()" << std::endl; |
748 |
#endif // DEBUG |
#endif // DEBUG |
749 |
uint16_t word; |
uint16_t word; |
750 |
ReadSceptical(&word,1,2); |
ReadSceptical(&word,1,2); |
751 |
return word; |
return word; |
757 |
* needed. |
* needed. |
758 |
* |
* |
759 |
* @returns read integer word |
* @returns read integer word |
760 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
761 |
*/ |
*/ |
762 |
int32_t Chunk::ReadInt32() { |
int32_t Chunk::ReadInt32() { |
763 |
#if DEBUG |
#if DEBUG |
764 |
std::cout << "Chunk::ReadInt32()" << std::endl; |
std::cout << "Chunk::ReadInt32()" << std::endl; |
765 |
#endif // DEBUG |
#endif // DEBUG |
766 |
int32_t word; |
int32_t word; |
767 |
ReadSceptical(&word,1,4); |
ReadSceptical(&word,1,4); |
768 |
return word; |
return word; |
774 |
* needed. |
* needed. |
775 |
* |
* |
776 |
* @returns read integer word |
* @returns read integer word |
777 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
778 |
*/ |
*/ |
779 |
uint32_t Chunk::ReadUint32() { |
uint32_t Chunk::ReadUint32() { |
780 |
#if DEBUG |
#if DEBUG |
781 |
std::cout << "Chunk::ReadUint32()" << std::endl; |
std::cout << "Chunk::ReadUint32()" << std::endl; |
782 |
#endif // DEBUG |
#endif // DEBUG |
783 |
uint32_t word; |
uint32_t word; |
784 |
ReadSceptical(&word,1,4); |
ReadSceptical(&word,1,4); |
785 |
return word; |
return word; |
807 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
808 |
*/ |
*/ |
809 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
810 |
if (!pChunkData && pFile->Filename != "" && ulStartPos != 0) { |
if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) { |
811 |
#if POSIX |
#if POSIX |
812 |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ullStartPos, SEEK_SET) == -1) return NULL; |
813 |
#elif defined(WIN32) |
#elif defined(WIN32) |
814 |
if (SetFilePointer(pFile->hFileRead, ulStartPos, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER) return NULL; |
LARGE_INTEGER liFilePos; |
815 |
|
liFilePos.QuadPart = ullStartPos; |
816 |
|
if (!SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) return NULL; |
817 |
#else |
#else |
818 |
if (fseek(pFile->hFileRead, ulStartPos, SEEK_SET)) return NULL; |
if (fseeko(pFile->hFileRead, ullStartPos, SEEK_SET)) return NULL; |
819 |
#endif // POSIX |
#endif // POSIX |
820 |
unsigned long ulBufferSize = (CurrentChunkSize > NewChunkSize) ? CurrentChunkSize : NewChunkSize; |
file_offset_t ullBufferSize = (ullCurrentChunkSize > ullNewChunkSize) ? ullCurrentChunkSize : ullNewChunkSize; |
821 |
pChunkData = new uint8_t[ulBufferSize]; |
pChunkData = new uint8_t[ullBufferSize]; |
822 |
if (!pChunkData) return NULL; |
if (!pChunkData) return NULL; |
823 |
memset(pChunkData, 0, ulBufferSize); |
memset(pChunkData, 0, ullBufferSize); |
824 |
#if POSIX |
#if POSIX |
825 |
unsigned long readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
file_offset_t readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
826 |
#elif defined(WIN32) |
#elif defined(WIN32) |
827 |
DWORD readWords; |
DWORD readWords; |
828 |
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); |
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); //FIXME: won't load chunks larger than 2GB ! |
829 |
#else |
#else |
830 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
file_offset_t readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
831 |
#endif // POSIX |
#endif // POSIX |
832 |
if (readWords != GetSize()) { |
if (readWords != GetSize()) { |
833 |
delete[] pChunkData; |
delete[] pChunkData; |
834 |
return (pChunkData = NULL); |
return (pChunkData = NULL); |
835 |
} |
} |
836 |
ulChunkDataSize = ulBufferSize; |
ullChunkDataSize = ullBufferSize; |
837 |
} else if (NewChunkSize > ulChunkDataSize) { |
} else if (ullNewChunkSize > ullChunkDataSize) { |
838 |
uint8_t* pNewBuffer = new uint8_t[NewChunkSize]; |
uint8_t* pNewBuffer = new uint8_t[ullNewChunkSize]; |
839 |
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(NewChunkSize) + " bytes"); |
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(ullNewChunkSize) + " bytes"); |
840 |
memset(pNewBuffer, 0 , NewChunkSize); |
memset(pNewBuffer, 0 , ullNewChunkSize); |
841 |
memcpy(pNewBuffer, pChunkData, ulChunkDataSize); |
memcpy(pNewBuffer, pChunkData, ullChunkDataSize); |
842 |
delete[] pChunkData; |
delete[] pChunkData; |
843 |
pChunkData = pNewBuffer; |
pChunkData = pNewBuffer; |
844 |
ulChunkDataSize = NewChunkSize; |
ullChunkDataSize = ullNewChunkSize; |
845 |
} |
} |
846 |
return pChunkData; |
return pChunkData; |
847 |
} |
} |
873 |
* calling File::Save() as this might exceed the current chunk's body |
* calling File::Save() as this might exceed the current chunk's body |
874 |
* boundary! |
* boundary! |
875 |
* |
* |
876 |
* @param iNewSize - new chunk body size in bytes (must be greater than zero) |
* @param NewSize - new chunk body size in bytes (must be greater than zero) |
877 |
* @throws RIFF::Exception if \a iNewSize is less than 1 |
* @throws RIFF::Exception if \a NewSize is less than 1 or unrealistic large |
878 |
* @see File::Save() |
* @see File::Save() |
879 |
*/ |
*/ |
880 |
void Chunk::Resize(int iNewSize) { |
void Chunk::Resize(file_offset_t NewSize) { |
881 |
if (iNewSize <= 0) |
if (NewSize == 0) |
882 |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this)); |
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this)); |
883 |
if (NewChunkSize == iNewSize) return; |
if ((NewSize >> 48) != 0) |
884 |
NewChunkSize = iNewSize; |
throw Exception("Unrealistic high chunk size detected: " + __resolveChunkPath(this)); |
885 |
pFile->LogAsResized(this); |
if (ullNewChunkSize == NewSize) return; |
886 |
|
ullNewChunkSize = NewSize; |
887 |
} |
} |
888 |
|
|
889 |
/** @brief Write chunk persistently e.g. to disk. |
/** @brief Write chunk persistently e.g. to disk. |
890 |
* |
* |
891 |
* Stores the chunk persistently to its actual "physical" file. |
* Stores the chunk persistently to its actual "physical" file. |
892 |
* |
* |
893 |
* @param ulWritePos - position within the "physical" file where this |
* @param ullWritePos - position within the "physical" file where this |
894 |
* chunk should be written to |
* chunk should be written to |
895 |
* @param ulCurrentDataOffset - offset of current (old) data within |
* @param ullCurrentDataOffset - offset of current (old) data within |
896 |
* the file |
* the file |
897 |
|
* @param pProgress - optional: callback function for progress notification |
898 |
* @returns new write position in the "physical" file, that is |
* @returns new write position in the "physical" file, that is |
899 |
* \a ulWritePos incremented by this chunk's new size |
* \a ullWritePos incremented by this chunk's new size |
900 |
* (including its header size of course) |
* (including its header size of course) |
901 |
*/ |
*/ |
902 |
unsigned long Chunk::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
file_offset_t Chunk::WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress) { |
903 |
const unsigned long ulOriginalPos = ulWritePos; |
const file_offset_t ullOriginalPos = ullWritePos; |
904 |
ulWritePos += CHUNK_HEADER_SIZE; |
ullWritePos += CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
905 |
|
|
906 |
if (pFile->Mode != stream_mode_read_write) |
if (pFile->Mode != stream_mode_read_write) |
907 |
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
912 |
LoadChunkData(); |
LoadChunkData(); |
913 |
// write chunk data from RAM persistently to the file |
// write chunk data from RAM persistently to the file |
914 |
#if POSIX |
#if POSIX |
915 |
lseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
lseek(pFile->hFileWrite, ullWritePos, SEEK_SET); |
916 |
if (write(pFile->hFileWrite, pChunkData, NewChunkSize) != NewChunkSize) { |
if (write(pFile->hFileWrite, pChunkData, ullNewChunkSize) != ullNewChunkSize) { |
917 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
918 |
} |
} |
919 |
#elif defined(WIN32) |
#elif defined(WIN32) |
920 |
SetFilePointer(pFile->hFileWrite, ulWritePos, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
921 |
|
liFilePos.QuadPart = ullWritePos; |
922 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
923 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
924 |
WriteFile(pFile->hFileWrite, pChunkData, NewChunkSize, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, pChunkData, ullNewChunkSize, &dwBytesWritten, NULL); //FIXME: won't save chunks larger than 2GB ! |
925 |
if (dwBytesWritten != NewChunkSize) { |
if (dwBytesWritten != ullNewChunkSize) { |
926 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
927 |
} |
} |
928 |
#else |
#else |
929 |
fseek(pFile->hFileWrite, ulWritePos, SEEK_SET); |
fseeko(pFile->hFileWrite, ullWritePos, SEEK_SET); |
930 |
if (fwrite(pChunkData, 1, NewChunkSize, pFile->hFileWrite) != NewChunkSize) { |
if (fwrite(pChunkData, 1, ullNewChunkSize, pFile->hFileWrite) != ullNewChunkSize) { |
931 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
932 |
} |
} |
933 |
#endif // POSIX |
#endif // POSIX |
934 |
} else { |
} else { |
935 |
// move chunk data from the end of the file to the appropriate position |
// move chunk data from the end of the file to the appropriate position |
936 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
937 |
unsigned long ulToMove = (NewChunkSize < CurrentChunkSize) ? NewChunkSize : CurrentChunkSize; |
file_offset_t ullToMove = (ullNewChunkSize < ullCurrentChunkSize) ? ullNewChunkSize : ullCurrentChunkSize; |
938 |
#if defined(WIN32) |
#if defined(WIN32) |
939 |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
940 |
#else |
#else |
941 |
int iBytesMoved = 1; |
int iBytesMoved = 1; |
942 |
#endif |
#endif |
943 |
for (unsigned long ulOffset = 0; iBytesMoved > 0; ulOffset += iBytesMoved, ulToMove -= iBytesMoved) { |
for (file_offset_t ullOffset = 0; ullToMove > 0 && iBytesMoved > 0; ullOffset += iBytesMoved, ullToMove -= iBytesMoved) { |
944 |
iBytesMoved = (ulToMove < 4096) ? ulToMove : 4096; |
iBytesMoved = (ullToMove < 4096) ? int(ullToMove) : 4096; |
945 |
#if POSIX |
#if POSIX |
946 |
lseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
lseek(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
947 |
iBytesMoved = read(pFile->hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = (int) read(pFile->hFileRead, pCopyBuffer, (size_t) iBytesMoved); |
948 |
lseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
lseek(pFile->hFileWrite, ullWritePos + ullOffset, SEEK_SET); |
949 |
iBytesMoved = write(pFile->hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = (int) write(pFile->hFileWrite, pCopyBuffer, (size_t) iBytesMoved); |
950 |
#elif defined(WIN32) |
#elif defined(WIN32) |
951 |
SetFilePointer(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
952 |
|
liFilePos.QuadPart = ullStartPos + ullCurrentDataOffset + ullOffset; |
953 |
|
SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
954 |
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
955 |
SetFilePointer(pFile->hFileWrite, ulWritePos + ulOffset, NULL/*32 bit*/, FILE_BEGIN); |
liFilePos.QuadPart = ullWritePos + ullOffset; |
956 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
957 |
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
958 |
#else |
#else |
959 |
fseek(pFile->hFileRead, ulStartPos + ulCurrentDataOffset + ulOffset, SEEK_SET); |
fseeko(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
960 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
961 |
fseek(pFile->hFileWrite, ulWritePos + ulOffset, SEEK_SET); |
fseeko(pFile->hFileWrite, ullWritePos + ullOffset, SEEK_SET); |
962 |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, pFile->hFileWrite); |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, pFile->hFileWrite); |
963 |
#endif |
#endif |
964 |
} |
} |
967 |
} |
} |
968 |
|
|
969 |
// update this chunk's header |
// update this chunk's header |
970 |
CurrentChunkSize = NewChunkSize; |
ullCurrentChunkSize = ullNewChunkSize; |
971 |
WriteHeader(ulOriginalPos); |
WriteHeader(ullOriginalPos); |
972 |
|
|
973 |
|
__notify_progress(pProgress, 1.0); // notify done |
974 |
|
|
975 |
// update chunk's position pointers |
// update chunk's position pointers |
976 |
ulStartPos = ulOriginalPos + CHUNK_HEADER_SIZE; |
ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
977 |
ulPos = 0; |
ullPos = 0; |
978 |
|
|
979 |
// add pad byte if needed |
// add pad byte if needed |
980 |
if ((ulStartPos + NewChunkSize) % 2 != 0) { |
if ((ullStartPos + ullNewChunkSize) % 2 != 0) { |
981 |
const char cPadByte = 0; |
const char cPadByte = 0; |
982 |
#if POSIX |
#if POSIX |
983 |
lseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
lseek(pFile->hFileWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
984 |
write(pFile->hFileWrite, &cPadByte, 1); |
write(pFile->hFileWrite, &cPadByte, 1); |
985 |
#elif defined(WIN32) |
#elif defined(WIN32) |
986 |
SetFilePointer(pFile->hFileWrite, ulStartPos + NewChunkSize, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
987 |
|
liFilePos.QuadPart = ullStartPos + ullNewChunkSize; |
988 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
989 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
990 |
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
991 |
#else |
#else |
992 |
fseek(pFile->hFileWrite, ulStartPos + NewChunkSize, SEEK_SET); |
fseeko(pFile->hFileWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
993 |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
994 |
#endif |
#endif |
995 |
return ulStartPos + NewChunkSize + 1; |
return ullStartPos + ullNewChunkSize + 1; |
996 |
} |
} |
997 |
|
|
998 |
return ulStartPos + NewChunkSize; |
return ullStartPos + ullNewChunkSize; |
999 |
} |
} |
1000 |
|
|
1001 |
void Chunk::__resetPos() { |
void Chunk::__resetPos() { |
1002 |
ulPos = 0; |
ullPos = 0; |
1003 |
} |
} |
1004 |
|
|
1005 |
|
|
1008 |
// * |
// * |
1009 |
|
|
1010 |
List::List(File* pFile) : Chunk(pFile) { |
List::List(File* pFile) : Chunk(pFile) { |
1011 |
#if DEBUG |
#if DEBUG |
1012 |
std::cout << "List::List(File* pFile)" << std::endl; |
std::cout << "List::List(File* pFile)" << std::endl; |
1013 |
#endif // DEBUG |
#endif // DEBUG |
1014 |
pSubChunks = NULL; |
pSubChunks = NULL; |
1015 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
1016 |
} |
} |
1017 |
|
|
1018 |
List::List(File* pFile, unsigned long StartPos, List* Parent) |
List::List(File* pFile, file_offset_t StartPos, List* Parent) |
1019 |
: Chunk(pFile, StartPos, Parent) { |
: Chunk(pFile, StartPos, Parent) { |
1020 |
#if DEBUG |
#if DEBUG |
1021 |
std::cout << "List::List(File*,ulong,bool,List*)" << std::endl; |
std::cout << "List::List(File*,file_offset_t,List*)" << std::endl; |
1022 |
#endif // DEBUG |
#endif // DEBUG |
1023 |
pSubChunks = NULL; |
pSubChunks = NULL; |
1024 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
1025 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
1026 |
ulStartPos = StartPos + LIST_HEADER_SIZE; |
ullStartPos = StartPos + LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1027 |
} |
} |
1028 |
|
|
1029 |
List::List(File* pFile, List* pParent, uint32_t uiListID) |
List::List(File* pFile, List* pParent, uint32_t uiListID) |
1034 |
} |
} |
1035 |
|
|
1036 |
List::~List() { |
List::~List() { |
1037 |
#if DEBUG |
#if DEBUG |
1038 |
std::cout << "List::~List()" << std::endl; |
std::cout << "List::~List()" << std::endl; |
1039 |
#endif // DEBUG |
#endif // DEBUG |
1040 |
|
DeleteChunkList(); |
1041 |
|
} |
1042 |
|
|
1043 |
|
void List::DeleteChunkList() { |
1044 |
if (pSubChunks) { |
if (pSubChunks) { |
1045 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1046 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1049 |
iter++; |
iter++; |
1050 |
} |
} |
1051 |
delete pSubChunks; |
delete pSubChunks; |
1052 |
|
pSubChunks = NULL; |
1053 |
|
} |
1054 |
|
if (pSubChunksMap) { |
1055 |
|
delete pSubChunksMap; |
1056 |
|
pSubChunksMap = NULL; |
1057 |
} |
} |
|
if (pSubChunksMap) delete pSubChunksMap; |
|
1058 |
} |
} |
1059 |
|
|
1060 |
/** |
/** |
1069 |
* that ID |
* that ID |
1070 |
*/ |
*/ |
1071 |
Chunk* List::GetSubChunk(uint32_t ChunkID) { |
Chunk* List::GetSubChunk(uint32_t ChunkID) { |
1072 |
#if DEBUG |
#if DEBUG |
1073 |
std::cout << "List::GetSubChunk(uint32_t)" << std::endl; |
std::cout << "List::GetSubChunk(uint32_t)" << std::endl; |
1074 |
#endif // DEBUG |
#endif // DEBUG |
1075 |
if (!pSubChunksMap) LoadSubChunks(); |
if (!pSubChunksMap) LoadSubChunks(); |
1076 |
return (*pSubChunksMap)[ChunkID]; |
return (*pSubChunksMap)[ChunkID]; |
1077 |
} |
} |
1079 |
/** |
/** |
1080 |
* Returns sublist chunk with list type <i>\a ListType</i> within this |
* Returns sublist chunk with list type <i>\a ListType</i> within this |
1081 |
* chunk list. Use this method if you expect only one sublist chunk of |
* chunk list. Use this method if you expect only one sublist chunk of |
1082 |
* that type in the list. It there are more than one, it's undetermined |
* that type in the list. If there are more than one, it's undetermined |
1083 |
* which one of them will be returned! If there are no sublists with |
* which one of them will be returned! If there are no sublists with |
1084 |
* that desired list type, NULL will be returned. |
* that desired list type, NULL will be returned. |
1085 |
* |
* |
1105 |
} |
} |
1106 |
|
|
1107 |
/** |
/** |
1108 |
* Returns the first subchunk within the list. You have to call this |
* Returns the first subchunk within the list (which may be an ordinary |
1109 |
|
* chunk as well as a list chunk). You have to call this |
1110 |
* method before you can call GetNextSubChunk(). Recall it when you want |
* method before you can call GetNextSubChunk(). Recall it when you want |
1111 |
* to start from the beginning of the list again. |
* to start from the beginning of the list again. |
1112 |
* |
* |
1123 |
} |
} |
1124 |
|
|
1125 |
/** |
/** |
1126 |
* Returns the next subchunk within the list. You have to call |
* Returns the next subchunk within the list (which may be an ordinary |
1127 |
|
* chunk as well as a list chunk). You have to call |
1128 |
* GetFirstSubChunk() before you can use this method! |
* GetFirstSubChunk() before you can use this method! |
1129 |
* |
* |
1130 |
* @returns pointer to the next subchunk within the list or NULL if |
* @returns pointer to the next subchunk within the list or NULL if |
1186 |
} |
} |
1187 |
|
|
1188 |
/** |
/** |
1189 |
* Returns number of subchunks within the list. |
* Returns number of subchunks within the list (including list chunks). |
1190 |
*/ |
*/ |
1191 |
unsigned int List::CountSubChunks() { |
size_t List::CountSubChunks() { |
1192 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1193 |
return pSubChunks->size(); |
return pSubChunks->size(); |
1194 |
} |
} |
1197 |
* Returns number of subchunks within the list with chunk ID |
* Returns number of subchunks within the list with chunk ID |
1198 |
* <i>\a ChunkId</i>. |
* <i>\a ChunkId</i>. |
1199 |
*/ |
*/ |
1200 |
unsigned int List::CountSubChunks(uint32_t ChunkID) { |
size_t List::CountSubChunks(uint32_t ChunkID) { |
1201 |
unsigned int result = 0; |
size_t result = 0; |
1202 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1203 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1204 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1214 |
/** |
/** |
1215 |
* Returns number of sublists within the list. |
* Returns number of sublists within the list. |
1216 |
*/ |
*/ |
1217 |
unsigned int List::CountSubLists() { |
size_t List::CountSubLists() { |
1218 |
return CountSubChunks(CHUNK_ID_LIST); |
return CountSubChunks(CHUNK_ID_LIST); |
1219 |
} |
} |
1220 |
|
|
1222 |
* Returns number of sublists within the list with list type |
* Returns number of sublists within the list with list type |
1223 |
* <i>\a ListType</i> |
* <i>\a ListType</i> |
1224 |
*/ |
*/ |
1225 |
unsigned int List::CountSubLists(uint32_t ListType) { |
size_t List::CountSubLists(uint32_t ListType) { |
1226 |
unsigned int result = 0; |
size_t result = 0; |
1227 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1228 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1229 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1240 |
/** @brief Creates a new sub chunk. |
/** @brief Creates a new sub chunk. |
1241 |
* |
* |
1242 |
* Creates and adds a new sub chunk to this list chunk. Note that the |
* Creates and adds a new sub chunk to this list chunk. Note that the |
1243 |
* chunk's body size given by \a uiBodySize must be greater than zero. |
* chunk's body size given by \a ullBodySize must be greater than zero. |
1244 |
* You have to call File::Save() to make this change persistent to the |
* You have to call File::Save() to make this change persistent to the |
1245 |
* actual file and <b>before</b> performing any data write operations |
* actual file and <b>before</b> performing any data write operations |
1246 |
* on the new chunk! |
* on the new chunk! |
1247 |
* |
* |
1248 |
* @param uiChunkID - chunk ID of the new chunk |
* @param uiChunkID - chunk ID of the new chunk |
1249 |
* @param uiBodySize - size of the new chunk's body, that is its actual |
* @param ullBodySize - size of the new chunk's body, that is its actual |
1250 |
* data size (without header) |
* data size (without header) |
1251 |
* @throws RIFF::Exception if \a uiBodySize equals zero |
* @throws RIFF::Exception if \a ullBodySize equals zero |
1252 |
*/ |
*/ |
1253 |
Chunk* List::AddSubChunk(uint32_t uiChunkID, uint uiBodySize) { |
Chunk* List::AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize) { |
1254 |
if (uiBodySize == 0) throw Exception("Chunk body size must be at least 1 byte"); |
if (ullBodySize == 0) throw Exception("Chunk body size must be at least 1 byte"); |
1255 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1256 |
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0); |
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0); |
1257 |
pSubChunks->push_back(pNewChunk); |
pSubChunks->push_back(pNewChunk); |
1258 |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1259 |
pNewChunk->Resize(uiBodySize); |
pNewChunk->Resize(ullBodySize); |
1260 |
NewChunkSize += CHUNK_HEADER_SIZE; |
ullNewChunkSize += CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
|
pFile->LogAsResized(this); |
|
1261 |
return pNewChunk; |
return pNewChunk; |
1262 |
} |
} |
1263 |
|
|
1264 |
/** @brief Moves a sub chunk. |
/** @brief Moves a sub chunk witin this list. |
1265 |
* |
* |
1266 |
* Moves a sub chunk from one position in a list to another |
* Moves a sub chunk from one position in this list to another |
1267 |
* position in the same list. The pSrc chunk is placed before the |
* position in the same list. The pSrc chunk is placed before the |
1268 |
* pDst chunk. |
* pDst chunk. |
1269 |
* |
* |
1279 |
pSubChunks->insert(iter, pSrc); |
pSubChunks->insert(iter, pSrc); |
1280 |
} |
} |
1281 |
|
|
1282 |
|
/** @brief Moves a sub chunk from this list to another list. |
1283 |
|
* |
1284 |
|
* Moves a sub chunk from this list list to the end of another |
1285 |
|
* list. |
1286 |
|
* |
1287 |
|
* @param pSrc - sub chunk to be moved |
1288 |
|
* @param pDst - destination list where the chunk shall be moved to |
1289 |
|
*/ |
1290 |
|
void List::MoveSubChunk(Chunk* pSrc, List* pNewParent) { |
1291 |
|
if (pNewParent == this || !pNewParent) return; |
1292 |
|
if (!pSubChunks) LoadSubChunks(); |
1293 |
|
if (!pNewParent->pSubChunks) pNewParent->LoadSubChunks(); |
1294 |
|
pSubChunks->remove(pSrc); |
1295 |
|
pNewParent->pSubChunks->push_back(pSrc); |
1296 |
|
// update chunk id map of this List |
1297 |
|
if ((*pSubChunksMap)[pSrc->GetChunkID()] == pSrc) { |
1298 |
|
pSubChunksMap->erase(pSrc->GetChunkID()); |
1299 |
|
// try to find another chunk of the same chunk ID |
1300 |
|
ChunkList::iterator iter = pSubChunks->begin(); |
1301 |
|
ChunkList::iterator end = pSubChunks->end(); |
1302 |
|
for (; iter != end; ++iter) { |
1303 |
|
if ((*iter)->GetChunkID() == pSrc->GetChunkID()) { |
1304 |
|
(*pSubChunksMap)[pSrc->GetChunkID()] = *iter; |
1305 |
|
break; // we're done, stop search |
1306 |
|
} |
1307 |
|
} |
1308 |
|
} |
1309 |
|
// update chunk id map of other list |
1310 |
|
if (!(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()]) |
1311 |
|
(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()] = pSrc; |
1312 |
|
} |
1313 |
|
|
1314 |
/** @brief Creates a new list sub chunk. |
/** @brief Creates a new list sub chunk. |
1315 |
* |
* |
1316 |
* Creates and adds a new list sub chunk to this list chunk. Note that |
* Creates and adds a new list sub chunk to this list chunk. Note that |
1325 |
List* pNewListChunk = new List(pFile, this, uiListType); |
List* pNewListChunk = new List(pFile, this, uiListType); |
1326 |
pSubChunks->push_back(pNewListChunk); |
pSubChunks->push_back(pNewListChunk); |
1327 |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1328 |
NewChunkSize += LIST_HEADER_SIZE; |
ullNewChunkSize += LIST_HEADER_SIZE(pFile->FileOffsetSize); |
|
pFile->LogAsResized(this); |
|
1329 |
return pNewListChunk; |
return pNewListChunk; |
1330 |
} |
} |
1331 |
|
|
1357 |
delete pSubChunk; |
delete pSubChunk; |
1358 |
} |
} |
1359 |
|
|
1360 |
void List::ReadHeader(unsigned long fPos) { |
/** |
1361 |
#if DEBUG |
* Returns the actual total size in bytes (including List chunk header and |
1362 |
std::cout << "List::Readheader(ulong) "; |
* all subchunks) of this List Chunk if being stored to a file. |
1363 |
#endif // DEBUG |
* |
1364 |
Chunk::ReadHeader(fPos); |
* @param fileOffsetSize - RIFF file offset size (in bytes) assumed when |
1365 |
NewChunkSize = CurrentChunkSize -= 4; |
* being saved to a file |
1366 |
|
*/ |
1367 |
|
file_offset_t List::RequiredPhysicalSize(int fileOffsetSize) { |
1368 |
|
if (!pSubChunks) LoadSubChunks(); |
1369 |
|
file_offset_t size = LIST_HEADER_SIZE(fileOffsetSize); |
1370 |
|
ChunkList::iterator iter = pSubChunks->begin(); |
1371 |
|
ChunkList::iterator end = pSubChunks->end(); |
1372 |
|
for (; iter != end; ++iter) |
1373 |
|
size += (*iter)->RequiredPhysicalSize(fileOffsetSize); |
1374 |
|
return size; |
1375 |
|
} |
1376 |
|
|
1377 |
|
void List::ReadHeader(file_offset_t filePos) { |
1378 |
|
#if DEBUG |
1379 |
|
std::cout << "List::Readheader(file_offset_t) "; |
1380 |
|
#endif // DEBUG |
1381 |
|
Chunk::ReadHeader(filePos); |
1382 |
|
if (ullCurrentChunkSize < 4) return; |
1383 |
|
ullNewChunkSize = ullCurrentChunkSize -= 4; |
1384 |
#if POSIX |
#if POSIX |
1385 |
lseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1386 |
read(pFile->hFileRead, &ListType, 4); |
read(pFile->hFileRead, &ListType, 4); |
1387 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1388 |
SetFilePointer(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
1389 |
|
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1390 |
|
SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1391 |
DWORD dwBytesRead; |
DWORD dwBytesRead; |
1392 |
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
1393 |
#else |
#else |
1394 |
fseek(pFile->hFileRead, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseeko(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1395 |
fread(&ListType, 4, 1, pFile->hFileRead); |
fread(&ListType, 4, 1, pFile->hFileRead); |
1396 |
#endif // POSIX |
#endif // POSIX |
1397 |
#if DEBUG |
#if DEBUG |
1398 |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
1399 |
#endif // DEBUG |
#endif // DEBUG |
1400 |
if (!pFile->bEndianNative) { |
if (!pFile->bEndianNative) { |
1401 |
//swapBytes_32(&ListType); |
//swapBytes_32(&ListType); |
1402 |
} |
} |
1403 |
} |
} |
1404 |
|
|
1405 |
void List::WriteHeader(unsigned long fPos) { |
void List::WriteHeader(file_offset_t filePos) { |
1406 |
// the four list type bytes officially belong the chunk's body in the RIFF format |
// the four list type bytes officially belong the chunk's body in the RIFF format |
1407 |
NewChunkSize += 4; |
ullNewChunkSize += 4; |
1408 |
Chunk::WriteHeader(fPos); |
Chunk::WriteHeader(filePos); |
1409 |
NewChunkSize -= 4; // just revert the +4 incrementation |
ullNewChunkSize -= 4; // just revert the +4 incrementation |
1410 |
#if POSIX |
#if POSIX |
1411 |
lseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(pFile->hFileWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1412 |
write(pFile->hFileWrite, &ListType, 4); |
write(pFile->hFileWrite, &ListType, 4); |
1413 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1414 |
SetFilePointer(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
1415 |
|
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1416 |
|
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1417 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
1418 |
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
1419 |
#else |
#else |
1420 |
fseek(pFile->hFileWrite, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseeko(pFile->hFileWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1421 |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
1422 |
#endif // POSIX |
#endif // POSIX |
1423 |
} |
} |
1424 |
|
|
1425 |
void List::LoadSubChunks() { |
void List::LoadSubChunks(progress_t* pProgress) { |
1426 |
#if DEBUG |
#if DEBUG |
1427 |
std::cout << "List::LoadSubChunks()"; |
std::cout << "List::LoadSubChunks()"; |
1428 |
#endif // DEBUG |
#endif // DEBUG |
1429 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1430 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1431 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1434 |
#else |
#else |
1435 |
if (!pFile->hFileRead) return; |
if (!pFile->hFileRead) return; |
1436 |
#endif |
#endif |
1437 |
unsigned long uiOriginalPos = GetPos(); |
file_offset_t ullOriginalPos = GetPos(); |
1438 |
SetPos(0); // jump to beginning of list chunk body |
SetPos(0); // jump to beginning of list chunk body |
1439 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
while (RemainingBytes() >= CHUNK_HEADER_SIZE(pFile->FileOffsetSize)) { |
1440 |
Chunk* ck; |
Chunk* ck; |
1441 |
uint32_t ckid; |
uint32_t ckid; |
1442 |
Read(&ckid, 4, 1); |
Read(&ckid, 4, 1); |
1443 |
#if DEBUG |
#if DEBUG |
1444 |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
1445 |
#endif // DEBUG |
#endif // DEBUG |
1446 |
if (ckid == CHUNK_ID_LIST) { |
if (ckid == CHUNK_ID_LIST) { |
1447 |
ck = new RIFF::List(pFile, ulStartPos + ulPos - 4, this); |
ck = new RIFF::List(pFile, ullStartPos + ullPos - 4, this); |
1448 |
SetPos(ck->GetSize() + LIST_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + LIST_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos); |
1449 |
} |
} |
1450 |
else { // simple chunk |
else { // simple chunk |
1451 |
ck = new RIFF::Chunk(pFile, ulStartPos + ulPos - 4, this); |
ck = new RIFF::Chunk(pFile, ullStartPos + ullPos - 4, this); |
1452 |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos); |
1453 |
} |
} |
1454 |
pSubChunks->push_back(ck); |
pSubChunks->push_back(ck); |
1455 |
(*pSubChunksMap)[ckid] = ck; |
(*pSubChunksMap)[ckid] = ck; |
1456 |
if (GetPos() % 2 != 0) SetPos(1, RIFF::stream_curpos); // jump over pad byte |
if (GetPos() % 2 != 0) SetPos(1, RIFF::stream_curpos); // jump over pad byte |
1457 |
} |
} |
1458 |
SetPos(uiOriginalPos); // restore position before this call |
SetPos(ullOriginalPos); // restore position before this call |
1459 |
} |
} |
1460 |
|
__notify_progress(pProgress, 1.0); // notify done |
1461 |
} |
} |
1462 |
|
|
1463 |
void List::LoadSubChunksRecursively() { |
void List::LoadSubChunksRecursively(progress_t* pProgress) { |
1464 |
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList()) |
const int n = (int) CountSubLists(); |
1465 |
pList->LoadSubChunksRecursively(); |
int i = 0; |
1466 |
|
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) { |
1467 |
|
// divide local progress into subprogress |
1468 |
|
progress_t subprogress; |
1469 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1470 |
|
// do the actual work |
1471 |
|
pList->LoadSubChunksRecursively(&subprogress); |
1472 |
|
} |
1473 |
|
__notify_progress(pProgress, 1.0); // notify done |
1474 |
} |
} |
1475 |
|
|
1476 |
/** @brief Write list chunk persistently e.g. to disk. |
/** @brief Write list chunk persistently e.g. to disk. |
1479 |
* subchunks (including sub list chunks) will be stored recursively as |
* subchunks (including sub list chunks) will be stored recursively as |
1480 |
* well. |
* well. |
1481 |
* |
* |
1482 |
* @param ulWritePos - position within the "physical" file where this |
* @param ullWritePos - position within the "physical" file where this |
1483 |
* list chunk should be written to |
* list chunk should be written to |
1484 |
* @param ulCurrentDataOffset - offset of current (old) data within |
* @param ullCurrentDataOffset - offset of current (old) data within |
1485 |
* the file |
* the file |
1486 |
|
* @param pProgress - optional: callback function for progress notification |
1487 |
* @returns new write position in the "physical" file, that is |
* @returns new write position in the "physical" file, that is |
1488 |
* \a ulWritePos incremented by this list chunk's new size |
* \a ullWritePos incremented by this list chunk's new size |
1489 |
* (including its header size of course) |
* (including its header size of course) |
1490 |
*/ |
*/ |
1491 |
unsigned long List::WriteChunk(unsigned long ulWritePos, unsigned long ulCurrentDataOffset) { |
file_offset_t List::WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress) { |
1492 |
const unsigned long ulOriginalPos = ulWritePos; |
const file_offset_t ullOriginalPos = ullWritePos; |
1493 |
ulWritePos += LIST_HEADER_SIZE; |
ullWritePos += LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1494 |
|
|
1495 |
if (pFile->Mode != stream_mode_read_write) |
if (pFile->Mode != stream_mode_read_write) |
1496 |
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
1497 |
|
|
1498 |
// write all subchunks (including sub list chunks) recursively |
// write all subchunks (including sub list chunks) recursively |
1499 |
if (pSubChunks) { |
if (pSubChunks) { |
1500 |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
size_t i = 0; |
1501 |
ulWritePos = (*iter)->WriteChunk(ulWritePos, ulCurrentDataOffset); |
const size_t n = pSubChunks->size(); |
1502 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) { |
1503 |
|
// divide local progress into subprogress for loading current Instrument |
1504 |
|
progress_t subprogress; |
1505 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1506 |
|
// do the actual work |
1507 |
|
ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress); |
1508 |
} |
} |
1509 |
} |
} |
1510 |
|
|
1511 |
// update this list chunk's header |
// update this list chunk's header |
1512 |
CurrentChunkSize = NewChunkSize = ulWritePos - ulOriginalPos - LIST_HEADER_SIZE; |
ullCurrentChunkSize = ullNewChunkSize = ullWritePos - ullOriginalPos - LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1513 |
WriteHeader(ulOriginalPos); |
WriteHeader(ullOriginalPos); |
1514 |
|
|
1515 |
// offset of this list chunk in new written file may have changed |
// offset of this list chunk in new written file may have changed |
1516 |
ulStartPos = ulOriginalPos + LIST_HEADER_SIZE; |
ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1517 |
|
|
1518 |
return ulWritePos; |
__notify_progress(pProgress, 1.0); // notify done |
1519 |
|
|
1520 |
|
return ullWritePos; |
1521 |
} |
} |
1522 |
|
|
1523 |
void List::__resetPos() { |
void List::__resetPos() { |
1532 |
/** |
/** |
1533 |
* Returns string representation of the lists's id |
* Returns string representation of the lists's id |
1534 |
*/ |
*/ |
1535 |
String List::GetListTypeString() { |
String List::GetListTypeString() const { |
1536 |
return convertToString(ListType); |
return convertToString(ListType); |
1537 |
} |
} |
1538 |
|
|
1555 |
* @param FileType - four-byte identifier of the RIFF file type |
* @param FileType - four-byte identifier of the RIFF file type |
1556 |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1557 |
*/ |
*/ |
1558 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) |
1559 |
|
: List(this), bIsNewFile(true), Layout(layout_standard), |
1560 |
|
FileOffsetPreference(offset_size_auto) |
1561 |
|
{ |
1562 |
#if defined(WIN32) |
#if defined(WIN32) |
1563 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1564 |
#else |
#else |
1566 |
#endif |
#endif |
1567 |
Mode = stream_mode_closed; |
Mode = stream_mode_closed; |
1568 |
bEndianNative = true; |
bEndianNative = true; |
|
ulStartPos = RIFF_HEADER_SIZE; |
|
1569 |
ListType = FileType; |
ListType = FileType; |
1570 |
|
FileOffsetSize = 4; |
1571 |
|
ullStartPos = RIFF_HEADER_SIZE(FileOffsetSize); |
1572 |
} |
} |
1573 |
|
|
1574 |
/** @brief Load existing RIFF file. |
/** @brief Load existing RIFF file. |
1576 |
* Loads an existing RIFF file with all its chunks. |
* Loads an existing RIFF file with all its chunks. |
1577 |
* |
* |
1578 |
* @param path - path and file name of the RIFF file to open |
* @param path - path and file name of the RIFF file to open |
1579 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occurred while trying to load the |
1580 |
* given RIFF file |
* given RIFF file |
1581 |
*/ |
*/ |
1582 |
File::File(const String& path) : List(this), Filename(path) { |
File::File(const String& path) |
1583 |
#if DEBUG |
: List(this), Filename(path), bIsNewFile(false), Layout(layout_standard), |
1584 |
std::cout << "File::File("<<path<<")" << std::endl; |
FileOffsetPreference(offset_size_auto) |
1585 |
#endif // DEBUG |
{ |
1586 |
|
#if DEBUG |
1587 |
|
std::cout << "File::File("<<path<<")" << std::endl; |
1588 |
|
#endif // DEBUG |
1589 |
bEndianNative = true; |
bEndianNative = true; |
1590 |
|
FileOffsetSize = 4; |
1591 |
|
try { |
1592 |
|
__openExistingFile(path); |
1593 |
|
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1594 |
|
throw RIFF::Exception("Not a RIFF file"); |
1595 |
|
} |
1596 |
|
} |
1597 |
|
catch (...) { |
1598 |
|
Cleanup(); |
1599 |
|
throw; |
1600 |
|
} |
1601 |
|
} |
1602 |
|
|
1603 |
|
/** @brief Load existing RIFF-like file. |
1604 |
|
* |
1605 |
|
* Loads an existing file, which is not a "real" RIFF file, but similar to |
1606 |
|
* an ordinary RIFF file. |
1607 |
|
* |
1608 |
|
* A "real" RIFF file contains at top level a List chunk either with chunk |
1609 |
|
* ID "RIFF" or "RIFX". The simple constructor above expects this to be |
1610 |
|
* case, and if it finds the toplevel List chunk to have another chunk ID |
1611 |
|
* than one of those two expected ones, it would throw an Exception and |
1612 |
|
* would refuse to load the file accordingly. |
1613 |
|
* |
1614 |
|
* Since there are however a lot of file formats which use the same simple |
1615 |
|
* principles of the RIFF format, with another toplevel List chunk ID |
1616 |
|
* though, you can use this alternative constructor here to be able to load |
1617 |
|
* and handle those files in the same way as you would do with "real" RIFF |
1618 |
|
* files. |
1619 |
|
* |
1620 |
|
* @param path - path and file name of the RIFF-alike file to be opened |
1621 |
|
* @param FileType - expected toplevel List chunk ID (this is the very |
1622 |
|
* first chunk found in the file) |
1623 |
|
* @param Endian - whether the file uses little endian or big endian layout |
1624 |
|
* @param layout - general file structure type |
1625 |
|
* @param fileOffsetSize - (optional) preference how to deal with large files |
1626 |
|
* @throws RIFF::Exception if error occurred while trying to load the |
1627 |
|
* given RIFF-alike file |
1628 |
|
*/ |
1629 |
|
File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize) |
1630 |
|
: List(this), Filename(path), bIsNewFile(false), Layout(layout), |
1631 |
|
FileOffsetPreference(fileOffsetSize) |
1632 |
|
{ |
1633 |
|
SetByteOrder(Endian); |
1634 |
|
if (fileOffsetSize < offset_size_auto || fileOffsetSize > offset_size_64bit) |
1635 |
|
throw Exception("Invalid RIFF::offset_size_t"); |
1636 |
|
FileOffsetSize = 4; |
1637 |
|
try { |
1638 |
|
__openExistingFile(path, &FileType); |
1639 |
|
} |
1640 |
|
catch (...) { |
1641 |
|
Cleanup(); |
1642 |
|
throw; |
1643 |
|
} |
1644 |
|
} |
1645 |
|
|
1646 |
|
/** |
1647 |
|
* Opens an already existing RIFF file or RIFF-alike file. This method |
1648 |
|
* shall only be called once (in a File class constructor). |
1649 |
|
* |
1650 |
|
* @param path - path and file name of the RIFF file or RIFF-alike file to |
1651 |
|
* be opened |
1652 |
|
* @param FileType - (optional) expected chunk ID of first chunk in file |
1653 |
|
* @throws RIFF::Exception if error occurred while trying to load the |
1654 |
|
* given RIFF file or RIFF-alike file |
1655 |
|
*/ |
1656 |
|
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
1657 |
#if POSIX |
#if POSIX |
1658 |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1659 |
if (hFileRead <= 0) { |
if (hFileRead == -1) { |
1660 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1661 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
String sError = strerror(errno); |
1662 |
|
throw RIFF::Exception("Can't open \"" + path + "\": " + sError); |
1663 |
} |
} |
1664 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1665 |
hFileRead = hFileWrite = CreateFile( |
hFileRead = hFileWrite = CreateFile( |
1678 |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1679 |
#endif // POSIX |
#endif // POSIX |
1680 |
Mode = stream_mode_read; |
Mode = stream_mode_read; |
1681 |
ulStartPos = RIFF_HEADER_SIZE; |
|
1682 |
ReadHeader(0); |
// determine RIFF file offset size to be used (in RIFF chunk headers) |
1683 |
if (ChunkID != CHUNK_ID_RIFF) { |
// according to the current file offset preference |
1684 |
throw RIFF::Exception("Not a RIFF file"); |
FileOffsetSize = FileOffsetSizeFor(GetCurrentFileSize()); |
1685 |
|
|
1686 |
|
switch (Layout) { |
1687 |
|
case layout_standard: // this is a normal RIFF file |
1688 |
|
ullStartPos = RIFF_HEADER_SIZE(FileOffsetSize); |
1689 |
|
ReadHeader(0); |
1690 |
|
if (FileType && ChunkID != *FileType) |
1691 |
|
throw RIFF::Exception("Invalid file container ID"); |
1692 |
|
break; |
1693 |
|
case layout_flat: // non-standard RIFF-alike file |
1694 |
|
ullStartPos = 0; |
1695 |
|
ullNewChunkSize = ullCurrentChunkSize = GetCurrentFileSize(); |
1696 |
|
if (FileType) { |
1697 |
|
uint32_t ckid; |
1698 |
|
if (Read(&ckid, 4, 1) != 4) { |
1699 |
|
throw RIFF::Exception("Invalid file header ID (premature end of header)"); |
1700 |
|
} else if (ckid != *FileType) { |
1701 |
|
String s = " (expected '" + convertToString(*FileType) + "' but got '" + convertToString(ckid) + "')"; |
1702 |
|
throw RIFF::Exception("Invalid file header ID" + s); |
1703 |
|
} |
1704 |
|
SetPos(0); // reset to first byte of file |
1705 |
|
} |
1706 |
|
LoadSubChunks(); |
1707 |
|
break; |
1708 |
} |
} |
1709 |
} |
} |
1710 |
|
|
1711 |
String File::GetFileName() { |
String File::GetFileName() const { |
1712 |
return Filename; |
return Filename; |
1713 |
} |
} |
1714 |
|
|
1715 |
|
void File::SetFileName(const String& path) { |
1716 |
|
Filename = path; |
1717 |
|
} |
1718 |
|
|
1719 |
stream_mode_t File::GetMode() { |
stream_mode_t File::GetMode() const { |
1720 |
return Mode; |
return Mode; |
1721 |
} |
} |
1722 |
|
|
1723 |
|
layout_t File::GetLayout() const { |
1724 |
|
return Layout; |
1725 |
|
} |
1726 |
|
|
1727 |
/** @brief Change file access mode. |
/** @brief Change file access mode. |
1728 |
* |
* |
1729 |
* Changes files access mode either to read-only mode or to read/write |
* Changes files access mode either to read-only mode or to read/write |
1741 |
#if POSIX |
#if POSIX |
1742 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1743 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1744 |
if (hFileRead < 0) { |
if (hFileRead == -1) { |
1745 |
hFileRead = hFileWrite = 0; |
hFileRead = hFileWrite = 0; |
1746 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
String sError = strerror(errno); |
1747 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode: " + sError); |
1748 |
} |
} |
1749 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1750 |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1771 |
#if POSIX |
#if POSIX |
1772 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1773 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
1774 |
if (hFileRead < 0) { |
if (hFileRead == -1) { |
1775 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
1776 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
String sError = strerror(errno); |
1777 |
|
throw Exception("Could not open file \"" + Filename + "\" in read+write mode: " + sError); |
1778 |
} |
} |
1779 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1780 |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
1850 |
/** @brief Save changes to same file. |
/** @brief Save changes to same file. |
1851 |
* |
* |
1852 |
* Make all changes of all chunks persistent by writing them to the |
* Make all changes of all chunks persistent by writing them to the |
1853 |
* actual (same) file. The file might temporarily grow to a higher size |
* actual (same) file. |
|
* than it will have at the end of the saving process, in case chunks |
|
|
* were grown. |
|
1854 |
* |
* |
1855 |
|
* @param pProgress - optional: callback function for progress notification |
1856 |
* @throws RIFF::Exception if there is an empty chunk or empty list |
* @throws RIFF::Exception if there is an empty chunk or empty list |
1857 |
* chunk or any kind of IO error occured |
* chunk or any kind of IO error occurred |
1858 |
*/ |
*/ |
1859 |
void File::Save() { |
void File::Save(progress_t* pProgress) { |
1860 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1861 |
|
if (Layout == layout_flat) |
1862 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1863 |
|
|
1864 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1865 |
LoadSubChunksRecursively(); |
{ |
1866 |
|
// divide progress into subprogress |
1867 |
|
progress_t subprogress; |
1868 |
|
__divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress |
1869 |
|
// do the actual work |
1870 |
|
LoadSubChunksRecursively(&subprogress); |
1871 |
|
// notify subprogress done |
1872 |
|
__notify_progress(&subprogress, 1.f); |
1873 |
|
} |
1874 |
|
|
1875 |
// reopen file in write mode |
// reopen file in write mode |
1876 |
SetMode(stream_mode_read_write); |
SetMode(stream_mode_read_write); |
1877 |
|
|
1878 |
|
// get the current file size as it is now still physically stored on disk |
1879 |
|
const file_offset_t workingFileSize = GetCurrentFileSize(); |
1880 |
|
|
1881 |
|
// get the overall file size required to save this file |
1882 |
|
const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference); |
1883 |
|
|
1884 |
|
// determine whether this file will yield in a large file (>=4GB) and |
1885 |
|
// the RIFF file offset size to be used accordingly for all chunks |
1886 |
|
FileOffsetSize = FileOffsetSizeFor(newFileSize); |
1887 |
|
|
1888 |
// to be able to save the whole file without loading everything into |
// to be able to save the whole file without loading everything into |
1889 |
// RAM and without having to store the data in a temporary file, we |
// RAM and without having to store the data in a temporary file, we |
1890 |
// enlarge the file with the sum of all _positive_ chunk size |
// enlarge the file with the overall positive file size change, |
1891 |
// changes, move current data towards the end of the file with the |
// then move current data towards the end of the file by the calculated |
1892 |
// calculated sum and finally update / rewrite the file by copying |
// positive file size difference and finally update / rewrite the file |
1893 |
// the old data back to the right position at the beginning of the file |
// by copying the old data back to the right position at the beginning |
1894 |
|
// of the file |
|
// first we sum up all positive chunk size changes (and skip all negative ones) |
|
|
unsigned long ulPositiveSizeDiff = 0; |
|
|
for (std::set<Chunk*>::const_iterator iter = ResizedChunks.begin(), end = ResizedChunks.end(); iter != end; ++iter) { |
|
|
if ((*iter)->GetNewSize() == 0) { |
|
|
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(*iter)); |
|
|
} |
|
|
unsigned long newSizePadded = (*iter)->GetNewSize() + (*iter)->GetNewSize() % 2; |
|
|
unsigned long oldSizePadded = (*iter)->GetSize() + (*iter)->GetSize() % 2; |
|
|
if (newSizePadded > oldSizePadded) ulPositiveSizeDiff += newSizePadded - oldSizePadded; |
|
|
} |
|
|
|
|
|
unsigned long ulWorkingFileSize = GetFileSize(); |
|
1895 |
|
|
1896 |
// if there are positive size changes... |
// if there are positive size changes... |
1897 |
if (ulPositiveSizeDiff > 0) { |
file_offset_t positiveSizeDiff = 0; |
1898 |
|
if (newFileSize > workingFileSize) { |
1899 |
|
positiveSizeDiff = newFileSize - workingFileSize; |
1900 |
|
|
1901 |
|
// divide progress into subprogress |
1902 |
|
progress_t subprogress; |
1903 |
|
__divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress |
1904 |
|
|
1905 |
// ... we enlarge this file first ... |
// ... we enlarge this file first ... |
1906 |
ulWorkingFileSize += ulPositiveSizeDiff; |
ResizeFile(newFileSize); |
1907 |
ResizeFile(ulWorkingFileSize); |
|
1908 |
// ... and move current data by the same amount towards end of file. |
// ... and move current data by the same amount towards end of file. |
1909 |
int8_t* pCopyBuffer = new int8_t[4096]; |
int8_t* pCopyBuffer = new int8_t[4096]; |
|
const unsigned long ulFileSize = GetSize() + RIFF_HEADER_SIZE; |
|
1910 |
#if defined(WIN32) |
#if defined(WIN32) |
1911 |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1912 |
#else |
#else |
1913 |
int iBytesMoved = 1; |
ssize_t iBytesMoved = 1; |
1914 |
#endif |
#endif |
1915 |
for (unsigned long ulPos = ulFileSize; iBytesMoved > 0; ) { |
for (file_offset_t ullPos = workingFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) { |
1916 |
iBytesMoved = (ulPos < 4096) ? ulPos : 4096; |
iBytesMoved = (ullPos < 4096) ? ullPos : 4096; |
1917 |
ulPos -= iBytesMoved; |
ullPos -= iBytesMoved; |
1918 |
#if POSIX |
#if POSIX |
1919 |
lseek(hFileRead, ulPos, SEEK_SET); |
lseek(hFileRead, ullPos, SEEK_SET); |
1920 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
1921 |
lseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
lseek(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET); |
1922 |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
1923 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1924 |
SetFilePointer(hFileRead, ulPos, NULL/*32 bit*/, FILE_BEGIN); |
LARGE_INTEGER liFilePos; |
1925 |
|
liFilePos.QuadPart = ullPos; |
1926 |
|
SetFilePointerEx(hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1927 |
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1928 |
SetFilePointer(hFileWrite, ulPos + ulPositiveSizeDiff, NULL/*32 bit*/, FILE_BEGIN); |
liFilePos.QuadPart = ullPos + positiveSizeDiff; |
1929 |
|
SetFilePointerEx(hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1930 |
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1931 |
#else |
#else |
1932 |
fseek(hFileRead, ulPos, SEEK_SET); |
fseeko(hFileRead, ullPos, SEEK_SET); |
1933 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
1934 |
fseek(hFileWrite, ulPos + ulPositiveSizeDiff, SEEK_SET); |
fseeko(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET); |
1935 |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
1936 |
#endif |
#endif |
1937 |
|
if (!(iNotif % 8) && iBytesMoved > 0) |
1938 |
|
__notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize)); |
1939 |
} |
} |
1940 |
delete[] pCopyBuffer; |
delete[] pCopyBuffer; |
1941 |
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"); |
1942 |
|
|
1943 |
|
__notify_progress(&subprogress, 1.f); // notify subprogress done |
1944 |
} |
} |
1945 |
|
|
1946 |
// rebuild / rewrite complete RIFF tree |
// rebuild / rewrite complete RIFF tree ... |
1947 |
unsigned long ulTotalSize = WriteChunk(0, ulPositiveSizeDiff); |
|
1948 |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
// divide progress into subprogress |
1949 |
|
progress_t subprogress; |
1950 |
|
__divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress |
1951 |
|
// do the actual work |
1952 |
|
const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, &subprogress); |
1953 |
|
const file_offset_t finalActualSize = __GetFileSize(hFileWrite); |
1954 |
|
// notify subprogress done |
1955 |
|
__notify_progress(&subprogress, 1.f); |
1956 |
|
|
1957 |
// resize file to the final size |
// resize file to the final size |
1958 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (finalSize < finalActualSize) ResizeFile(finalSize); |
1959 |
|
|
1960 |
// forget all resized chunks |
__notify_progress(pProgress, 1.0); // notify done |
|
ResizedChunks.clear(); |
|
1961 |
} |
} |
1962 |
|
|
1963 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
1972 |
* the new file (given by \a path) afterwards. |
* the new file (given by \a path) afterwards. |
1973 |
* |
* |
1974 |
* @param path - path and file name where everything should be written to |
* @param path - path and file name where everything should be written to |
1975 |
|
* @param pProgress - optional: callback function for progress notification |
1976 |
*/ |
*/ |
1977 |
void File::Save(const String& path) { |
void File::Save(const String& path, progress_t* pProgress) { |
1978 |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
1979 |
|
|
1980 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1981 |
|
if (Layout == layout_flat) |
1982 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1983 |
|
|
1984 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1985 |
LoadSubChunksRecursively(); |
{ |
1986 |
|
// divide progress into subprogress |
1987 |
|
progress_t subprogress; |
1988 |
|
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress |
1989 |
|
// do the actual work |
1990 |
|
LoadSubChunksRecursively(&subprogress); |
1991 |
|
// notify subprogress done |
1992 |
|
__notify_progress(&subprogress, 1.f); |
1993 |
|
} |
1994 |
|
|
1995 |
if (Filename.length() > 0) SetMode(stream_mode_read); |
if (!bIsNewFile) SetMode(stream_mode_read); |
1996 |
// 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 |
1997 |
#if POSIX |
#if POSIX |
1998 |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
1999 |
if (hFileWrite < 0) { |
if (hFileWrite == -1) { |
2000 |
hFileWrite = hFileRead; |
hFileWrite = hFileRead; |
2001 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
String sError = strerror(errno); |
2002 |
|
throw Exception("Could not open file \"" + path + "\" for writing: " + sError); |
2003 |
} |
} |
2004 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2005 |
hFileWrite = CreateFile( |
hFileWrite = CreateFile( |
2020 |
#endif // POSIX |
#endif // POSIX |
2021 |
Mode = stream_mode_read_write; |
Mode = stream_mode_read_write; |
2022 |
|
|
2023 |
|
// get the overall file size required to save this file |
2024 |
|
const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference); |
2025 |
|
|
2026 |
|
// determine whether this file will yield in a large file (>=4GB) and |
2027 |
|
// the RIFF file offset size to be used accordingly for all chunks |
2028 |
|
FileOffsetSize = FileOffsetSizeFor(newFileSize); |
2029 |
|
|
2030 |
// write complete RIFF tree to the other (new) file |
// write complete RIFF tree to the other (new) file |
2031 |
unsigned long ulTotalSize = WriteChunk(0, 0); |
file_offset_t ullTotalSize; |
2032 |
unsigned long ulActualSize = __GetFileSize(hFileWrite); |
{ |
2033 |
|
// divide progress into subprogress |
2034 |
|
progress_t subprogress; |
2035 |
|
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress |
2036 |
|
// do the actual work |
2037 |
|
ullTotalSize = WriteChunk(0, 0, &subprogress); |
2038 |
|
// notify subprogress done |
2039 |
|
__notify_progress(&subprogress, 1.f); |
2040 |
|
} |
2041 |
|
file_offset_t ullActualSize = __GetFileSize(hFileWrite); |
2042 |
|
|
2043 |
// resize file to the final size (if the file was originally larger) |
// resize file to the final size (if the file was originally larger) |
2044 |
if (ulTotalSize < ulActualSize) ResizeFile(ulTotalSize); |
if (ullActualSize > ullTotalSize) ResizeFile(ullTotalSize); |
|
|
|
|
// forget all resized chunks |
|
|
ResizedChunks.clear(); |
|
2045 |
|
|
2046 |
#if POSIX |
#if POSIX |
2047 |
if (hFileWrite) close(hFileWrite); |
if (hFileWrite) close(hFileWrite); |
2054 |
|
|
2055 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
2056 |
Filename = path; |
Filename = path; |
2057 |
|
bIsNewFile = false; |
2058 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
2059 |
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. |
2060 |
|
|
2061 |
|
__notify_progress(pProgress, 1.0); // notify done |
2062 |
} |
} |
2063 |
|
|
2064 |
void File::ResizeFile(unsigned long ulNewSize) { |
void File::ResizeFile(file_offset_t ullNewSize) { |
2065 |
#if POSIX |
#if POSIX |
2066 |
if (ftruncate(hFileWrite, ulNewSize) < 0) |
if (ftruncate(hFileWrite, ullNewSize) < 0) |
2067 |
throw Exception("Could not resize file \"" + Filename + "\""); |
throw Exception("Could not resize file \"" + Filename + "\""); |
2068 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2069 |
|
LARGE_INTEGER liFilePos; |
2070 |
|
liFilePos.QuadPart = ullNewSize; |
2071 |
if ( |
if ( |
2072 |
SetFilePointer(hFileWrite, ulNewSize, NULL/*32 bit*/, FILE_BEGIN) == INVALID_SET_FILE_POINTER || |
!SetFilePointerEx(hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN) || |
2073 |
!SetEndOfFile(hFileWrite) |
!SetEndOfFile(hFileWrite) |
2074 |
) throw Exception("Could not resize file \"" + Filename + "\""); |
) throw Exception("Could not resize file \"" + Filename + "\""); |
2075 |
#else |
#else |
2079 |
} |
} |
2080 |
|
|
2081 |
File::~File() { |
File::~File() { |
2082 |
#if DEBUG |
#if DEBUG |
2083 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
2084 |
#endif // DEBUG |
#endif // DEBUG |
2085 |
|
Cleanup(); |
2086 |
|
} |
2087 |
|
|
2088 |
|
/** |
2089 |
|
* Returns @c true if this file has been created new from scratch and |
2090 |
|
* has not been stored to disk yet. |
2091 |
|
*/ |
2092 |
|
bool File::IsNew() const { |
2093 |
|
return bIsNewFile; |
2094 |
|
} |
2095 |
|
|
2096 |
|
void File::Cleanup() { |
2097 |
#if POSIX |
#if POSIX |
2098 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
2099 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2101 |
#else |
#else |
2102 |
if (hFileRead) fclose(hFileRead); |
if (hFileRead) fclose(hFileRead); |
2103 |
#endif // POSIX |
#endif // POSIX |
2104 |
|
DeleteChunkList(); |
2105 |
|
pFile = NULL; |
2106 |
|
} |
2107 |
|
|
2108 |
|
/** |
2109 |
|
* Returns the current size of this file (in bytes) as it is currently |
2110 |
|
* yet stored on disk. If this file does not yet exist on disk (i.e. when |
2111 |
|
* this RIFF File has just been created from scratch and Save() has not |
2112 |
|
* been called yet) then this method returns 0. |
2113 |
|
*/ |
2114 |
|
file_offset_t File::GetCurrentFileSize() const { |
2115 |
|
file_offset_t size = 0; |
2116 |
|
try { |
2117 |
|
size = __GetFileSize(hFileRead); |
2118 |
|
} catch (...) { |
2119 |
|
size = 0; |
2120 |
|
} |
2121 |
|
return size; |
2122 |
|
} |
2123 |
|
|
2124 |
|
/** |
2125 |
|
* Returns the required size (in bytes) for this RIFF File to be saved to |
2126 |
|
* disk. The precise size of the final file on disk depends on the RIFF |
2127 |
|
* file offset size actually used internally in all headers of the RIFF |
2128 |
|
* chunks. By default libgig handles the required file offset size |
2129 |
|
* automatically for you; that means it is using 32 bit offsets for files |
2130 |
|
* smaller than 4 GB and 64 bit offsets for files equal or larger than |
2131 |
|
* 4 GB. You may however also override this default behavior by passing the |
2132 |
|
* respective option to the RIFF File constructor to force one particular |
2133 |
|
* offset size. In the latter case this method will return the file size |
2134 |
|
* for the requested forced file offset size that will be used when calling |
2135 |
|
* Save() later on. |
2136 |
|
* |
2137 |
|
* You may also use the overridden method below to get the file size for |
2138 |
|
* an arbitrary other file offset size instead. |
2139 |
|
* |
2140 |
|
* @see offset_size_t |
2141 |
|
* @see GetFileOffsetSize() |
2142 |
|
*/ |
2143 |
|
file_offset_t File::GetRequiredFileSize() { |
2144 |
|
return GetRequiredFileSize(FileOffsetPreference); |
2145 |
} |
} |
2146 |
|
|
2147 |
void File::LogAsResized(Chunk* pResizedChunk) { |
/** |
2148 |
ResizedChunks.insert(pResizedChunk); |
* Returns the rquired size (in bytes) for this RIFF file to be saved to |
2149 |
|
* disk, assuming the passed @a fileOffsestSize would be used for the |
2150 |
|
* Save() operation. |
2151 |
|
* |
2152 |
|
* This overridden method essentialy behaves like the above method, with |
2153 |
|
* the difference that you must provide a specific RIFF @a fileOffsetSize |
2154 |
|
* for calculating the theoretical final file size. |
2155 |
|
* |
2156 |
|
* @see GetFileOffsetSize() |
2157 |
|
*/ |
2158 |
|
file_offset_t File::GetRequiredFileSize(offset_size_t fileOffsetSize) { |
2159 |
|
switch (fileOffsetSize) { |
2160 |
|
case offset_size_auto: { |
2161 |
|
file_offset_t fileSize = GetRequiredFileSize(offset_size_32bit); |
2162 |
|
if (fileSize >> 32) |
2163 |
|
return GetRequiredFileSize(offset_size_64bit); |
2164 |
|
else |
2165 |
|
return fileSize; |
2166 |
|
} |
2167 |
|
case offset_size_32bit: break; |
2168 |
|
case offset_size_64bit: break; |
2169 |
|
default: throw Exception("Internal error: Invalid RIFF::offset_size_t"); |
2170 |
|
} |
2171 |
|
return RequiredPhysicalSize(FileOffsetSize); |
2172 |
} |
} |
2173 |
|
|
2174 |
void File::UnlogResized(Chunk* pResizedChunk) { |
int File::FileOffsetSizeFor(file_offset_t fileSize) const { |
2175 |
ResizedChunks.erase(pResizedChunk); |
switch (FileOffsetPreference) { |
2176 |
|
case offset_size_auto: |
2177 |
|
return (fileSize >> 32) ? 8 : 4; |
2178 |
|
case offset_size_32bit: |
2179 |
|
return 4; |
2180 |
|
case offset_size_64bit: |
2181 |
|
return 8; |
2182 |
|
default: |
2183 |
|
throw Exception("Internal error: Invalid RIFF::offset_size_t"); |
2184 |
|
} |
2185 |
} |
} |
2186 |
|
|
2187 |
unsigned long File::GetFileSize() { |
/** |
2188 |
return __GetFileSize(hFileRead); |
* Returns the current size (in bytes) of file offsets stored in the |
2189 |
|
* headers of all chunks of this file. |
2190 |
|
* |
2191 |
|
* Most RIFF files are using 32 bit file offsets internally, which limits |
2192 |
|
* them to a maximum file size of less than 4 GB though. In contrast to the |
2193 |
|
* common standard, this RIFF File class implementation supports handling of |
2194 |
|
* RIFF files equal or larger than 4 GB. In such cases 64 bit file offsets |
2195 |
|
* have to be used in all headers of all RIFF Chunks when being stored to a |
2196 |
|
* physical file. libgig by default automatically selects the correct file |
2197 |
|
* offset size for you. You may however also force one particular file |
2198 |
|
* offset size by supplying the respective option to the RIFF::File |
2199 |
|
* constructor. |
2200 |
|
* |
2201 |
|
* This method can be used to check which RIFF file offset size is currently |
2202 |
|
* being used for this RIFF File. |
2203 |
|
* |
2204 |
|
* @returns current RIFF file offset size used (in bytes) |
2205 |
|
* @see offset_size_t |
2206 |
|
*/ |
2207 |
|
int File::GetFileOffsetSize() const { |
2208 |
|
return FileOffsetSize; |
2209 |
|
} |
2210 |
|
|
2211 |
|
/** |
2212 |
|
* Returns the required size (in bytes) of file offsets stored in the |
2213 |
|
* headers of all chunks of this file if the current RIFF tree would be |
2214 |
|
* saved to disk by calling Save(). |
2215 |
|
* |
2216 |
|
* See GetFileOffsetSize() for mor details about RIFF file offsets. |
2217 |
|
* |
2218 |
|
* @returns RIFF file offset size required (in bytes) if being saved |
2219 |
|
* @see offset_size_t |
2220 |
|
*/ |
2221 |
|
int File::GetRequiredFileOffsetSize() { |
2222 |
|
return FileOffsetSizeFor(GetCurrentFileSize()); |
2223 |
} |
} |
2224 |
|
|
2225 |
#if POSIX |
#if POSIX |
2226 |
unsigned long File::__GetFileSize(int hFile) { |
file_offset_t File::__GetFileSize(int hFile) const { |
2227 |
struct stat filestat; |
struct stat filestat; |
2228 |
fstat(hFile, &filestat); |
if (fstat(hFile, &filestat) == -1) |
2229 |
long size = filestat.st_size; |
throw Exception("POSIX FS error: could not determine file size"); |
2230 |
return size; |
return filestat.st_size; |
2231 |
} |
} |
2232 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2233 |
unsigned long File::__GetFileSize(HANDLE hFile) { |
file_offset_t File::__GetFileSize(HANDLE hFile) const { |
2234 |
DWORD dwSize = ::GetFileSize(hFile, NULL /*32bit*/); |
LARGE_INTEGER size; |
2235 |
if (dwSize == INVALID_FILE_SIZE) |
if (!GetFileSizeEx(hFile, &size)) |
2236 |
throw Exception("Windows FS error: could not determine file size"); |
throw Exception("Windows FS error: could not determine file size"); |
2237 |
return dwSize; |
return size.QuadPart; |
2238 |
} |
} |
2239 |
#else // standard C functions |
#else // standard C functions |
2240 |
unsigned long File::__GetFileSize(FILE* hFile) { |
file_offset_t File::__GetFileSize(FILE* hFile) const { |
2241 |
long curpos = ftell(hFile); |
off_t curpos = ftello(hFile); |
2242 |
fseek(hFile, 0, SEEK_END); |
if (fseeko(hFile, 0, SEEK_END) == -1) |
2243 |
long size = ftell(hFile); |
throw Exception("FS error: could not determine file size"); |
2244 |
fseek(hFile, curpos, SEEK_SET); |
off_t size = ftello(hFile); |
2245 |
|
fseeko(hFile, curpos, SEEK_SET); |
2246 |
return size; |
return size; |
2247 |
} |
} |
2248 |
#endif |
#endif |