1 |
/*************************************************************************** |
/*************************************************************************** |
2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file loader library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Christian Schoenebeck * |
* Copyright (C) 2003-2021 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
9 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
21 |
* MA 02111-1307 USA * |
* MA 02111-1307 USA * |
22 |
***************************************************************************/ |
***************************************************************************/ |
23 |
#if 1 |
|
24 |
|
#include <algorithm> |
25 |
|
#include <set> |
26 |
|
#include <string.h> |
27 |
|
|
28 |
#include "RIFF.h" |
#include "RIFF.h" |
29 |
|
|
30 |
|
#include "helper.h" |
31 |
|
|
32 |
|
#if POSIX |
33 |
|
# include <errno.h> |
34 |
|
#endif |
35 |
|
|
36 |
namespace RIFF { |
namespace RIFF { |
37 |
|
|
38 |
|
// *************** Internal functions ************** |
39 |
|
// * |
40 |
|
|
41 |
|
/// Returns a human readable path of the given chunk. |
42 |
|
static String __resolveChunkPath(Chunk* pCk) { |
43 |
|
String sPath; |
44 |
|
for (Chunk* pChunk = pCk; pChunk; pChunk = pChunk->GetParent()) { |
45 |
|
if (pChunk->GetChunkID() == CHUNK_ID_LIST) { |
46 |
|
List* pList = (List*) pChunk; |
47 |
|
sPath = "->'" + pList->GetListTypeString() + "'" + sPath; |
48 |
|
} else { |
49 |
|
sPath = "->'" + pChunk->GetChunkIDString() + "'" + sPath; |
50 |
|
} |
51 |
|
} |
52 |
|
return sPath; |
53 |
|
} |
54 |
|
|
55 |
|
inline static bool _isValidHandle(File::Handle handle) { |
56 |
|
#if defined(WIN32) |
57 |
|
return handle != INVALID_HANDLE_VALUE; |
58 |
|
#else |
59 |
|
return handle; |
60 |
|
#endif |
61 |
|
} |
62 |
|
|
63 |
|
inline static void _close(File::Handle handle) { |
64 |
|
if (!_isValidHandle(handle)) return; |
65 |
|
#if POSIX |
66 |
|
close(handle); |
67 |
|
#elif defined(WIN32) |
68 |
|
CloseHandle(handle); |
69 |
|
#else |
70 |
|
fclose(handle); |
71 |
|
#endif |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
// *************** progress_t *************** |
77 |
|
// * |
78 |
|
|
79 |
|
progress_t::progress_t() { |
80 |
|
callback = NULL; |
81 |
|
custom = NULL; |
82 |
|
__range_min = 0.0f; |
83 |
|
__range_max = 1.0f; |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
|
* Divides this progress task into the requested amount of equal weighted |
88 |
|
* sub-progress tasks and returns a vector with those subprogress tasks. |
89 |
|
* |
90 |
|
* @param iSubtasks - total amount sub tasks this task should be subdivided |
91 |
|
* @returns subtasks |
92 |
|
*/ |
93 |
|
std::vector<progress_t> progress_t::subdivide(int iSubtasks) { |
94 |
|
std::vector<progress_t> v; |
95 |
|
for (int i = 0; i < iSubtasks; ++i) { |
96 |
|
progress_t p; |
97 |
|
__divide_progress(this, &p, iSubtasks, i); |
98 |
|
v.push_back(p); |
99 |
|
} |
100 |
|
return v; |
101 |
|
} |
102 |
|
|
103 |
|
/** |
104 |
|
* Divides this progress task into the requested amount of sub-progress |
105 |
|
* tasks, where each one of those new sub-progress tasks is created with its |
106 |
|
* requested individual weight / portion, and finally returns a vector |
107 |
|
* with those new subprogress tasks. |
108 |
|
* |
109 |
|
* The amount of subprogresses to be created is determined by this method |
110 |
|
* by calling @c vSubTaskPortions.size() . |
111 |
|
* |
112 |
|
* Example: consider you wanted to create 3 subprogresses where the 1st |
113 |
|
* subtask should be assigned 10% of the new 3 subprogresses' overall |
114 |
|
* progress, the 2nd subtask should be assigned 50% of the new 3 |
115 |
|
* subprogresses' overall progress, and the 3rd subtask should be assigned |
116 |
|
* 40%, then you might call this method like this: |
117 |
|
* @code |
118 |
|
* std::vector<progress_t> subprogresses = progress.subdivide({0.1, 0.5, 0.4}); |
119 |
|
* @endcode |
120 |
|
* |
121 |
|
* @param vSubTaskPortions - amount and individual weight of subtasks to be |
122 |
|
* created |
123 |
|
* @returns subtasks |
124 |
|
*/ |
125 |
|
std::vector<progress_t> progress_t::subdivide(std::vector<float> vSubTaskPortions) { |
126 |
|
float fTotal = 0.f; // usually 1.0, but we sum the portions up below to be sure |
127 |
|
for (int i = 0; i < vSubTaskPortions.size(); ++i) |
128 |
|
fTotal += vSubTaskPortions[i]; |
129 |
|
|
130 |
|
float fLow = 0.f, fHigh = 0.f; |
131 |
|
std::vector<progress_t> v; |
132 |
|
for (int i = 0; i < vSubTaskPortions.size(); ++i) { |
133 |
|
fLow = fHigh; |
134 |
|
fHigh = vSubTaskPortions[i]; |
135 |
|
progress_t p; |
136 |
|
__divide_progress(this, &p, fTotal, fLow, fHigh); |
137 |
|
v.push_back(p); |
138 |
|
} |
139 |
|
return v; |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
// *************** Chunk ************** |
// *************** Chunk ************** |
145 |
// * |
// * |
146 |
|
|
147 |
Chunk::Chunk() { |
Chunk::Chunk(File* pFile) { |
148 |
#if DEBUG |
#if DEBUG_RIFF |
149 |
std::cout << "Chunk::Chunk()" << std::endl; |
std::cout << "Chunk::Chunk(File* pFile)" << std::endl; |
150 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
151 |
ulPos = 0; |
chunkPos.ullPos = 0; |
152 |
pParent = NULL; |
pParent = NULL; |
153 |
pChunkData = NULL; |
pChunkData = NULL; |
154 |
|
ullCurrentChunkSize = 0; |
155 |
|
ullNewChunkSize = 0; |
156 |
|
ullChunkDataSize = 0; |
157 |
|
ChunkID = CHUNK_ID_RIFF; |
158 |
|
this->pFile = pFile; |
159 |
} |
} |
160 |
|
|
161 |
#if POSIX |
Chunk::Chunk(File* pFile, file_offset_t StartPos, List* Parent) { |
162 |
Chunk::Chunk(int hFile, unsigned long StartPos, bool EndianNative, List* Parent) { |
#if DEBUG_RIFF |
163 |
#else |
std::cout << "Chunk::Chunk(File*,file_offset_t,List*),StartPos=" << StartPos << std::endl; |
164 |
Chunk::Chunk(FILE* hFile, unsigned long StartPos, bool EndianNative, List* Parent) { |
#endif // DEBUG_RIFF |
165 |
#endif // POSIX |
this->pFile = pFile; |
166 |
#if DEBUG |
ullStartPos = StartPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
|
std::cout << "Chunk::Chunk(FILE,ulong,bool,List*),StartPos=" << StartPos << std::endl; |
|
|
#endif // DEBUG |
|
|
Chunk::hFile = hFile; |
|
|
ulStartPos = StartPos + CHUNK_HEADER_SIZE; |
|
|
bEndianNative = EndianNative; |
|
167 |
pParent = Parent; |
pParent = Parent; |
168 |
ulPos = 0; |
chunkPos.ullPos = 0; |
169 |
pChunkData = NULL; |
pChunkData = NULL; |
170 |
|
ullCurrentChunkSize = 0; |
171 |
|
ullNewChunkSize = 0; |
172 |
|
ullChunkDataSize = 0; |
173 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
174 |
} |
} |
175 |
|
|
176 |
|
Chunk::Chunk(File* pFile, List* pParent, uint32_t uiChunkID, file_offset_t ullBodySize) { |
177 |
|
this->pFile = pFile; |
178 |
|
ullStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk |
179 |
|
this->pParent = pParent; |
180 |
|
chunkPos.ullPos = 0; |
181 |
|
pChunkData = NULL; |
182 |
|
ChunkID = uiChunkID; |
183 |
|
ullChunkDataSize = 0; |
184 |
|
ullCurrentChunkSize = 0; |
185 |
|
ullNewChunkSize = ullBodySize; |
186 |
|
} |
187 |
|
|
188 |
Chunk::~Chunk() { |
Chunk::~Chunk() { |
189 |
if (pChunkData) delete[] pChunkData; |
if (pChunkData) delete[] pChunkData; |
190 |
} |
} |
191 |
|
|
192 |
void Chunk::ReadHeader(unsigned long fPos) { |
void Chunk::ReadHeader(file_offset_t filePos) { |
193 |
#if DEBUG |
#if DEBUG_RIFF |
194 |
std::cout << "Chunk::Readheader(" << fPos << ") "; |
std::cout << "Chunk::Readheader(" << filePos << ") "; |
195 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
196 |
|
ChunkID = 0; |
197 |
|
ullNewChunkSize = ullCurrentChunkSize = 0; |
198 |
|
|
199 |
|
const File::Handle hRead = pFile->FileHandle(); |
200 |
|
|
201 |
#if POSIX |
#if POSIX |
202 |
if (lseek(hFile, fPos, SEEK_SET) != -1) { |
if (lseek(hRead, filePos, SEEK_SET) != -1) { |
203 |
read(hFile, &ChunkID, 4); |
read(hRead, &ChunkID, 4); |
204 |
read(hFile, &ChunkSize, 4); |
read(hRead, &ullCurrentChunkSize, pFile->FileOffsetSize); |
205 |
|
#elif defined(WIN32) |
206 |
|
LARGE_INTEGER liFilePos; |
207 |
|
liFilePos.QuadPart = filePos; |
208 |
|
if (SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
209 |
|
DWORD dwBytesRead; |
210 |
|
ReadFile(hRead, &ChunkID, 4, &dwBytesRead, NULL); |
211 |
|
ReadFile(hRead, &ullCurrentChunkSize, pFile->FileOffsetSize, &dwBytesRead, NULL); |
212 |
#else |
#else |
213 |
if (!fseek(hFile, fPos, SEEK_SET)) { |
if (!fseeko(hRead, filePos, SEEK_SET)) { |
214 |
fread(&ChunkID, 4, 1, hFile); |
fread(&ChunkID, 4, 1, hRead); |
215 |
fread(&ChunkSize, 4, 1, hFile); |
fread(&ullCurrentChunkSize, pFile->FileOffsetSize, 1, hRead); |
216 |
#endif // POSIX |
#endif // POSIX |
217 |
#if WORDS_BIGENDIAN |
#if WORDS_BIGENDIAN |
218 |
if (ChunkID == CHUNK_ID_RIFF) { |
if (ChunkID == CHUNK_ID_RIFF) { |
219 |
bEndianNative = false; |
pFile->bEndianNative = false; |
220 |
} |
} |
221 |
#else // little endian |
#else // little endian |
222 |
if (ChunkID == CHUNK_ID_RIFX) { |
if (ChunkID == CHUNK_ID_RIFX) { |
223 |
bEndianNative = false; |
pFile->bEndianNative = false; |
224 |
ChunkID = CHUNK_ID_RIFF; |
ChunkID = CHUNK_ID_RIFF; |
225 |
} |
} |
226 |
#endif // WORDS_BIGENDIAN |
#endif // WORDS_BIGENDIAN |
227 |
if (!bEndianNative) { |
if (!pFile->bEndianNative) { |
228 |
//swapBytes_32(&ChunkID); |
//swapBytes_32(&ChunkID); |
229 |
swapBytes_32(&ChunkSize); |
if (pFile->FileOffsetSize == 4) |
230 |
|
swapBytes_32(&ullCurrentChunkSize); |
231 |
|
else |
232 |
|
swapBytes_64(&ullCurrentChunkSize); |
233 |
} |
} |
234 |
#if DEBUG |
#if DEBUG_RIFF |
235 |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
std::cout << "ckID=" << convertToString(ChunkID) << " "; |
236 |
std::cout << "ckSize=" << ChunkSize << " "; |
std::cout << "ckSize=" << ullCurrentChunkSize << " "; |
237 |
std::cout << "bEndianNative=" << bEndianNative << std::endl; |
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; |
238 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
239 |
|
ullNewChunkSize = ullCurrentChunkSize; |
240 |
|
} |
241 |
|
} |
242 |
|
|
243 |
|
void Chunk::WriteHeader(file_offset_t filePos) { |
244 |
|
uint32_t uiNewChunkID = ChunkID; |
245 |
|
if (ChunkID == CHUNK_ID_RIFF) { |
246 |
|
#if WORDS_BIGENDIAN |
247 |
|
if (pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX; |
248 |
|
#else // little endian |
249 |
|
if (!pFile->bEndianNative) uiNewChunkID = CHUNK_ID_RIFX; |
250 |
|
#endif // WORDS_BIGENDIAN |
251 |
|
} |
252 |
|
|
253 |
|
uint64_t ullNewChunkSize = this->ullNewChunkSize; |
254 |
|
if (!pFile->bEndianNative) { |
255 |
|
if (pFile->FileOffsetSize == 4) |
256 |
|
swapBytes_32(&ullNewChunkSize); |
257 |
|
else |
258 |
|
swapBytes_64(&ullNewChunkSize); |
259 |
} |
} |
260 |
|
|
261 |
|
const File::Handle hWrite = pFile->FileWriteHandle(); |
262 |
|
|
263 |
|
#if POSIX |
264 |
|
if (lseek(hWrite, filePos, SEEK_SET) != -1) { |
265 |
|
write(hWrite, &uiNewChunkID, 4); |
266 |
|
write(hWrite, &ullNewChunkSize, pFile->FileOffsetSize); |
267 |
|
} |
268 |
|
#elif defined(WIN32) |
269 |
|
LARGE_INTEGER liFilePos; |
270 |
|
liFilePos.QuadPart = filePos; |
271 |
|
if (SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
272 |
|
DWORD dwBytesWritten; |
273 |
|
WriteFile(hWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
274 |
|
WriteFile(hWrite, &ullNewChunkSize, pFile->FileOffsetSize, &dwBytesWritten, NULL); |
275 |
|
} |
276 |
|
#else |
277 |
|
if (!fseeko(hWrite, filePos, SEEK_SET)) { |
278 |
|
fwrite(&uiNewChunkID, 4, 1, hWrite); |
279 |
|
fwrite(&ullNewChunkSize, pFile->FileOffsetSize, 1, hWrite); |
280 |
|
} |
281 |
|
#endif // POSIX |
282 |
} |
} |
283 |
|
|
284 |
/** |
/** |
285 |
* Returns the String representation of the chunk's ID (e.g. "RIFF", |
* Returns the String representation of the chunk's ID (e.g. "RIFF", |
286 |
* "LIST"). |
* "LIST"). |
287 |
*/ |
*/ |
288 |
String Chunk::GetChunkIDString() { |
String Chunk::GetChunkIDString() const { |
289 |
return convertToString(ChunkID); |
return convertToString(ChunkID); |
290 |
} |
} |
291 |
|
|
292 |
/** |
/** |
293 |
|
* This is an internal-only method which must not be used by any application |
294 |
|
* and might change at any time. |
295 |
|
* |
296 |
|
* Returns a reference (memory location) of the chunk's current file |
297 |
|
* (read/write) position variable which depends on the current value of |
298 |
|
* File::IsIOPerThread(). |
299 |
|
*/ |
300 |
|
file_offset_t& Chunk::GetPosUnsafeRef() { |
301 |
|
if (!pFile->IsIOPerThread()) return chunkPos.ullPos; |
302 |
|
const std::thread::id tid = std::this_thread::get_id(); |
303 |
|
return chunkPos.byThread[tid]; |
304 |
|
} |
305 |
|
|
306 |
|
/** |
307 |
|
* Current read/write position within the chunk data body (starting with 0). |
308 |
|
* |
309 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
310 |
|
*/ |
311 |
|
file_offset_t Chunk::GetPos() const { |
312 |
|
if (!pFile->IsIOPerThread()) return chunkPos.ullPos; |
313 |
|
const std::thread::id tid = std::this_thread::get_id(); |
314 |
|
std::lock_guard<std::mutex> lock(chunkPos.mutex); |
315 |
|
return chunkPos.byThread[tid]; |
316 |
|
} |
317 |
|
|
318 |
|
/** |
319 |
|
* Current, actual offset in file of current chunk data body read/write |
320 |
|
* position. |
321 |
|
* |
322 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
323 |
|
*/ |
324 |
|
file_offset_t Chunk::GetFilePos() const { |
325 |
|
return ullStartPos + GetPos(); |
326 |
|
} |
327 |
|
|
328 |
|
/** |
329 |
* Sets the position within the chunk body, thus within the data portion |
* Sets the position within the chunk body, thus within the data portion |
330 |
* of the chunk (in bytes). |
* of the chunk (in bytes). |
331 |
* |
* |
332 |
|
* <b>Caution:</b> the position will be reset to zero whenever |
333 |
|
* File::Save() was called. |
334 |
|
* |
335 |
* @param Where - position offset (in bytes) |
* @param Where - position offset (in bytes) |
336 |
* @param Whence - optional: defines to what <i>\a Where</i> relates to, |
* @param Whence - optional: defines to what <i>\a Where</i> relates to, |
337 |
* if omitted \a Where relates to beginning of the chunk |
* if omitted \a Where relates to beginning of the chunk |
338 |
* data |
* data |
339 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
340 |
*/ |
*/ |
341 |
unsigned long Chunk::SetPos(unsigned long Where, stream_whence_t Whence) { |
file_offset_t Chunk::SetPos(file_offset_t Where, stream_whence_t Whence) { |
342 |
#if DEBUG |
#if DEBUG_RIFF |
343 |
std::cout << "Chunk::SetPos(ulong)" << std::endl; |
std::cout << "Chunk::SetPos(file_offset_t,stream_whence_t)" << std::endl; |
344 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
345 |
|
std::lock_guard<std::mutex> lock(chunkPos.mutex); |
346 |
|
file_offset_t& pos = GetPosUnsafeRef(); |
347 |
switch (Whence) { |
switch (Whence) { |
348 |
case stream_curpos: |
case stream_curpos: |
349 |
ulPos += Where; |
pos += Where; |
350 |
break; |
break; |
351 |
case stream_end: |
case stream_end: |
352 |
ulPos = ChunkSize - 1 - Where; |
pos = ullCurrentChunkSize - 1 - Where; |
353 |
break; |
break; |
354 |
case stream_backward: |
case stream_backward: |
355 |
ulPos -= Where; |
pos -= Where; |
356 |
break; |
break; |
357 |
case stream_start: default: |
case stream_start: default: |
358 |
ulPos = Where; |
pos = Where; |
359 |
break; |
break; |
360 |
} |
} |
361 |
if (ulPos > ChunkSize) ulPos = ChunkSize; |
if (pos > ullCurrentChunkSize) pos = ullCurrentChunkSize; |
362 |
return ulPos; |
return pos; |
363 |
} |
} |
364 |
|
|
365 |
/** |
/** |
371 |
* of the chunk data. |
* of the chunk data. |
372 |
* |
* |
373 |
* @returns number of bytes left to read |
* @returns number of bytes left to read |
374 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
375 |
*/ |
*/ |
376 |
unsigned long Chunk::RemainingBytes() { |
file_offset_t Chunk::RemainingBytes() const { |
377 |
#if DEBUG |
#if DEBUG_RIFF |
378 |
std::cout << "Chunk::Remainingbytes()=" << ChunkSize - ulPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << ullCurrentChunkSize - ullPos << std::endl; |
379 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
380 |
return ChunkSize - ulPos; |
const file_offset_t pos = GetPos(); |
381 |
|
return (ullCurrentChunkSize > pos) ? ullCurrentChunkSize - pos : 0; |
382 |
|
} |
383 |
|
|
384 |
|
/** |
385 |
|
* Returns the actual total size in bytes (including header) of this Chunk |
386 |
|
* if being stored to a file. |
387 |
|
* |
388 |
|
* @param fileOffsetSize - RIFF file offset size (in bytes) assumed when |
389 |
|
* being saved to a file |
390 |
|
*/ |
391 |
|
file_offset_t Chunk::RequiredPhysicalSize(int fileOffsetSize) { |
392 |
|
return CHUNK_HEADER_SIZE(fileOffsetSize) + // RIFF chunk header |
393 |
|
ullNewChunkSize + // chunks's actual data body |
394 |
|
ullNewChunkSize % 2; // optional pad byte |
395 |
} |
} |
396 |
|
|
397 |
/** |
/** |
402 |
* - RIFF::stream_closed : |
* - RIFF::stream_closed : |
403 |
* the data stream was closed somehow, no more reading possible |
* the data stream was closed somehow, no more reading possible |
404 |
* - RIFF::stream_end_reached : |
* - RIFF::stream_end_reached : |
405 |
* alreaady reached the end of the chunk data, no more reading |
* already reached the end of the chunk data, no more reading |
406 |
* possible without SetPos() |
* possible without SetPos() |
407 |
|
* |
408 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
409 |
*/ |
*/ |
410 |
stream_state_t Chunk::GetState() { |
stream_state_t Chunk::GetState() const { |
411 |
#if DEBUG |
#if DEBUG_RIFF |
412 |
std::cout << "Chunk::GetState()" << std::endl; |
std::cout << "Chunk::GetState()" << std::endl; |
413 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
414 |
#if POSIX |
|
415 |
if (hFile == 0) return stream_closed; |
const File::Handle hRead = pFile->FileHandle(); |
416 |
#else |
|
417 |
if (hFile == NULL) return stream_closed; |
if (!_isValidHandle(hRead)) |
418 |
#endif // POSIX |
return stream_closed; |
419 |
if (ulPos < ChunkSize) return stream_ready; |
|
420 |
else return stream_end_reached; |
const file_offset_t pos = GetPos(); |
421 |
|
if (pos < ullCurrentChunkSize) return stream_ready; |
422 |
|
else return stream_end_reached; |
423 |
} |
} |
424 |
|
|
425 |
/** |
/** |
435 |
* @param WordCount number of data words to read |
* @param WordCount number of data words to read |
436 |
* @param WordSize size of each data word to read |
* @param WordSize size of each data word to read |
437 |
* @returns number of successfully read data words or 0 if end |
* @returns number of successfully read data words or 0 if end |
438 |
* of file reached or error occured |
* of file reached or error occurred |
439 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
440 |
*/ |
*/ |
441 |
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) { |
442 |
#if DEBUG |
#if DEBUG_RIFF |
443 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,file_offset_t,file_offset_t)" << std::endl; |
444 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
445 |
if (ulPos >= ChunkSize) return 0; |
//if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
446 |
if (ulPos + WordCount * WordSize >= ChunkSize) WordCount = (ChunkSize - ulPos) / WordSize; |
const file_offset_t pos = GetPos(); |
447 |
|
if (pos >= ullCurrentChunkSize) return 0; |
448 |
|
if (pos + WordCount * WordSize >= ullCurrentChunkSize) |
449 |
|
WordCount = (ullCurrentChunkSize - pos) / WordSize; |
450 |
|
|
451 |
|
const File::Handle hRead = pFile->FileHandle(); |
452 |
|
|
453 |
#if POSIX |
#if POSIX |
454 |
if (lseek(hFile, ulStartPos + ulPos, SEEK_SET) < 0) return 0; |
if (lseek(hRead, ullStartPos + pos, SEEK_SET) < 0) return 0; |
455 |
unsigned long readWords = read(hFile, pData, WordCount * WordSize); |
ssize_t readWords = read(hRead, pData, WordCount * WordSize); |
456 |
|
if (readWords < 1) { |
457 |
|
#if DEBUG_RIFF |
458 |
|
std::cerr << "POSIX read() failed: " << strerror(errno) << std::endl << std::flush; |
459 |
|
#endif // DEBUG_RIFF |
460 |
|
return 0; |
461 |
|
} |
462 |
|
readWords /= WordSize; |
463 |
|
#elif defined(WIN32) |
464 |
|
LARGE_INTEGER liFilePos; |
465 |
|
liFilePos.QuadPart = ullStartPos + pos; |
466 |
|
if (!SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) |
467 |
|
return 0; |
468 |
|
DWORD readWords; |
469 |
|
ReadFile(hRead, 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) |
470 |
if (readWords < 1) return 0; |
if (readWords < 1) return 0; |
471 |
readWords /= WordSize; |
readWords /= WordSize; |
472 |
#else // standard C functions |
#else // standard C functions |
473 |
if (fseek(hFile, ulStartPos + ulPos, SEEK_SET)) return 0; |
if (fseeko(hRead, ullStartPos + pos, SEEK_SET)) return 0; |
474 |
unsigned long readWords = fread(pData, WordSize, WordCount, hFile); |
file_offset_t readWords = fread(pData, WordSize, WordCount, hRead); |
475 |
#endif // POSIX |
#endif // POSIX |
476 |
if (!bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
477 |
switch (WordSize) { |
switch (WordSize) { |
478 |
case 2: |
case 2: |
479 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
480 |
swapBytes_16((uint16_t*) pData + iWord); |
swapBytes_16((uint16_t*) pData + iWord); |
481 |
break; |
break; |
482 |
case 4: |
case 4: |
483 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
484 |
swapBytes_32((uint32_t*) pData + iWord); |
swapBytes_32((uint32_t*) pData + iWord); |
485 |
break; |
break; |
486 |
|
case 8: |
487 |
|
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
488 |
|
swapBytes_64((uint64_t*) pData + iWord); |
489 |
|
break; |
490 |
default: |
default: |
491 |
for (unsigned long iWord = 0; iWord < readWords; iWord++) |
for (file_offset_t iWord = 0; iWord < readWords; iWord++) |
492 |
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
493 |
break; |
break; |
494 |
} |
} |
497 |
return readWords; |
return readWords; |
498 |
} |
} |
499 |
|
|
500 |
|
/** |
501 |
|
* Writes \a WordCount number of data words with given \a WordSize from |
502 |
|
* the buffer pointed by \a pData. Be sure to provide the correct |
503 |
|
* \a WordSize, as this will be important and taken into account for |
504 |
|
* eventual endian correction (swapping of bytes due to different |
505 |
|
* native byte order of a system). The position within the chunk will |
506 |
|
* automatically be incremented. |
507 |
|
* |
508 |
|
* @param pData source buffer (containing the data) |
509 |
|
* @param WordCount number of data words to write |
510 |
|
* @param WordSize size of each data word to write |
511 |
|
* @returns number of successfully written data words |
512 |
|
* @throws RIFF::Exception if write operation would exceed current |
513 |
|
* chunk size or any IO error occurred |
514 |
|
* @see Resize() |
515 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
516 |
|
*/ |
517 |
|
file_offset_t Chunk::Write(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
518 |
|
const File::HandlePair io = pFile->FileHandlePair(); |
519 |
|
if (io.Mode != stream_mode_read_write) |
520 |
|
throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first"); |
521 |
|
const file_offset_t pos = GetPos(); |
522 |
|
if (pos >= ullCurrentChunkSize || pos + WordCount * WordSize > ullCurrentChunkSize) |
523 |
|
throw Exception("End of chunk reached while trying to write data"); |
524 |
|
if (!pFile->bEndianNative && WordSize != 1) { |
525 |
|
switch (WordSize) { |
526 |
|
case 2: |
527 |
|
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
528 |
|
swapBytes_16((uint16_t*) pData + iWord); |
529 |
|
break; |
530 |
|
case 4: |
531 |
|
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
532 |
|
swapBytes_32((uint32_t*) pData + iWord); |
533 |
|
break; |
534 |
|
case 8: |
535 |
|
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
536 |
|
swapBytes_64((uint64_t*) pData + iWord); |
537 |
|
break; |
538 |
|
default: |
539 |
|
for (file_offset_t iWord = 0; iWord < WordCount; iWord++) |
540 |
|
swapBytes((uint8_t*) pData + iWord * WordSize, WordSize); |
541 |
|
break; |
542 |
|
} |
543 |
|
} |
544 |
|
#if POSIX |
545 |
|
if (lseek(io.hWrite, ullStartPos + pos, SEEK_SET) < 0) { |
546 |
|
throw Exception("Could not seek to position " + ToString(pos) + |
547 |
|
" in chunk (" + ToString(ullStartPos + pos) + " in file)"); |
548 |
|
} |
549 |
|
ssize_t writtenWords = write(io.hWrite, pData, WordCount * WordSize); |
550 |
|
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
551 |
|
writtenWords /= WordSize; |
552 |
|
#elif defined(WIN32) |
553 |
|
LARGE_INTEGER liFilePos; |
554 |
|
liFilePos.QuadPart = ullStartPos + pos; |
555 |
|
if (!SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
556 |
|
throw Exception("Could not seek to position " + ToString(pos) + |
557 |
|
" in chunk (" + ToString(ullStartPos + pos) + " in file)"); |
558 |
|
} |
559 |
|
DWORD writtenWords; |
560 |
|
WriteFile(io.hWrite, 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) |
561 |
|
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
562 |
|
writtenWords /= WordSize; |
563 |
|
#else // standard C functions |
564 |
|
if (fseeko(io.hWrite, ullStartPos + pos, SEEK_SET)) { |
565 |
|
throw Exception("Could not seek to position " + ToString(pos) + |
566 |
|
" in chunk (" + ToString(ullStartPos + pos) + " in file)"); |
567 |
|
} |
568 |
|
file_offset_t writtenWords = fwrite(pData, WordSize, WordCount, io.hWrite); |
569 |
|
#endif // POSIX |
570 |
|
SetPos(writtenWords * WordSize, stream_curpos); |
571 |
|
return writtenWords; |
572 |
|
} |
573 |
|
|
574 |
/** 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. */ |
575 |
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) { |
576 |
unsigned long readWords = Read(pData, WordCount, WordSize); |
file_offset_t readWords = Read(pData, WordCount, WordSize); |
577 |
if (readWords != WordCount) throw RIFF::Exception("End of chunk data reached."); |
if (readWords != WordCount) throw RIFF::Exception("End of chunk data reached."); |
578 |
return readWords; |
return readWords; |
579 |
} |
} |
586 |
* @param pData destination buffer |
* @param pData destination buffer |
587 |
* @param WordCount number of 8 Bit signed integers to read |
* @param WordCount number of 8 Bit signed integers to read |
588 |
* @returns number of read integers |
* @returns number of read integers |
589 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
590 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
591 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
592 |
*/ |
*/ |
593 |
unsigned long Chunk::ReadInt8(int8_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadInt8(int8_t* pData, file_offset_t WordCount) { |
594 |
#if DEBUG |
#if DEBUG_RIFF |
595 |
std::cout << "Chunk::ReadInt8(int8_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadInt8(int8_t*,file_offset_t)" << std::endl; |
596 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
597 |
return ReadSceptical(pData, WordCount, 1); |
return ReadSceptical(pData, WordCount, 1); |
598 |
} |
} |
599 |
|
|
600 |
/** |
/** |
601 |
|
* Writes \a WordCount number of 8 Bit signed integer words from the |
602 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
603 |
|
* actual "physical" file. The position within the chunk will |
604 |
|
* automatically be incremented. Note: you cannot write beyond the |
605 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
606 |
|
* before. |
607 |
|
* |
608 |
|
* @param pData source buffer (containing the data) |
609 |
|
* @param WordCount number of 8 Bit signed integers to write |
610 |
|
* @returns number of written integers |
611 |
|
* @throws RIFF::Exception if an IO error occurred |
612 |
|
* @see Resize() |
613 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
614 |
|
*/ |
615 |
|
file_offset_t Chunk::WriteInt8(int8_t* pData, file_offset_t WordCount) { |
616 |
|
return Write(pData, WordCount, 1); |
617 |
|
} |
618 |
|
|
619 |
|
/** |
620 |
* Reads \a WordCount number of 8 Bit unsigned integer words and copies |
* Reads \a WordCount number of 8 Bit unsigned integer words and copies |
621 |
* it into the buffer pointed by \a pData. The buffer has to be |
* it into the buffer pointed by \a pData. The buffer has to be |
622 |
* allocated. The position within the chunk will automatically be |
* allocated. The position within the chunk will automatically be |
625 |
* @param pData destination buffer |
* @param pData destination buffer |
626 |
* @param WordCount number of 8 Bit unsigned integers to read |
* @param WordCount number of 8 Bit unsigned integers to read |
627 |
* @returns number of read integers |
* @returns number of read integers |
628 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
629 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
630 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
631 |
*/ |
*/ |
632 |
unsigned long Chunk::ReadUint8(uint8_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadUint8(uint8_t* pData, file_offset_t WordCount) { |
633 |
#if DEBUG |
#if DEBUG_RIFF |
634 |
std::cout << "Chunk::ReadUint8(uint8_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadUint8(uint8_t*,file_offset_t)" << std::endl; |
635 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
636 |
return ReadSceptical(pData, WordCount, 1); |
return ReadSceptical(pData, WordCount, 1); |
637 |
} |
} |
638 |
|
|
639 |
/** |
/** |
640 |
|
* Writes \a WordCount number of 8 Bit unsigned integer words from the |
641 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
642 |
|
* actual "physical" file. The position within the chunk will |
643 |
|
* automatically be incremented. Note: you cannot write beyond the |
644 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
645 |
|
* before. |
646 |
|
* |
647 |
|
* @param pData source buffer (containing the data) |
648 |
|
* @param WordCount number of 8 Bit unsigned integers to write |
649 |
|
* @returns number of written integers |
650 |
|
* @throws RIFF::Exception if an IO error occurred |
651 |
|
* @see Resize() |
652 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
653 |
|
*/ |
654 |
|
file_offset_t Chunk::WriteUint8(uint8_t* pData, file_offset_t WordCount) { |
655 |
|
return Write(pData, WordCount, 1); |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
* Reads \a WordCount number of 16 Bit signed integer words and copies |
* Reads \a WordCount number of 16 Bit signed integer words and copies |
660 |
* it into the buffer pointed by \a pData. The buffer has to be |
* it into the buffer pointed by \a pData. The buffer has to be |
661 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
664 |
* @param pData destination buffer |
* @param pData destination buffer |
665 |
* @param WordCount number of 16 Bit signed integers to read |
* @param WordCount number of 16 Bit signed integers to read |
666 |
* @returns number of read integers |
* @returns number of read integers |
667 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
668 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
669 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
670 |
*/ |
*/ |
671 |
unsigned long Chunk::ReadInt16(int16_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadInt16(int16_t* pData, file_offset_t WordCount) { |
672 |
#if DEBUG |
#if DEBUG_RIFF |
673 |
std::cout << "Chunk::ReadInt16(int16_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadInt16(int16_t*,file_offset_t)" << std::endl; |
674 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
675 |
return ReadSceptical(pData, WordCount, 2); |
return ReadSceptical(pData, WordCount, 2); |
676 |
} |
} |
677 |
|
|
678 |
/** |
/** |
679 |
|
* Writes \a WordCount number of 16 Bit signed integer words from the |
680 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
681 |
|
* actual "physical" file. The position within the chunk will |
682 |
|
* automatically be incremented. Note: you cannot write beyond the |
683 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
684 |
|
* before. |
685 |
|
* |
686 |
|
* @param pData source buffer (containing the data) |
687 |
|
* @param WordCount number of 16 Bit signed integers to write |
688 |
|
* @returns number of written integers |
689 |
|
* @throws RIFF::Exception if an IO error occurred |
690 |
|
* @see Resize() |
691 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
692 |
|
*/ |
693 |
|
file_offset_t Chunk::WriteInt16(int16_t* pData, file_offset_t WordCount) { |
694 |
|
return Write(pData, WordCount, 2); |
695 |
|
} |
696 |
|
|
697 |
|
/** |
698 |
* Reads \a WordCount number of 16 Bit unsigned integer words and copies |
* Reads \a WordCount number of 16 Bit unsigned integer words and copies |
699 |
* it into the buffer pointed by \a pData. The buffer has to be |
* it into the buffer pointed by \a pData. The buffer has to be |
700 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
703 |
* @param pData destination buffer |
* @param pData destination buffer |
704 |
* @param WordCount number of 8 Bit unsigned integers to read |
* @param WordCount number of 8 Bit unsigned integers to read |
705 |
* @returns number of read integers |
* @returns number of read integers |
706 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
707 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
708 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
709 |
*/ |
*/ |
710 |
unsigned long Chunk::ReadUint16(uint16_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadUint16(uint16_t* pData, file_offset_t WordCount) { |
711 |
#if DEBUG |
#if DEBUG_RIFF |
712 |
std::cout << "Chunk::ReadUint16(uint16_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadUint16(uint16_t*,file_offset_t)" << std::endl; |
713 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
714 |
return ReadSceptical(pData, WordCount, 2); |
return ReadSceptical(pData, WordCount, 2); |
715 |
} |
} |
716 |
|
|
717 |
/** |
/** |
718 |
|
* Writes \a WordCount number of 16 Bit unsigned integer words from the |
719 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
720 |
|
* actual "physical" file. The position within the chunk will |
721 |
|
* automatically be incremented. Note: you cannot write beyond the |
722 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
723 |
|
* before. |
724 |
|
* |
725 |
|
* @param pData source buffer (containing the data) |
726 |
|
* @param WordCount number of 16 Bit unsigned integers to write |
727 |
|
* @returns number of written integers |
728 |
|
* @throws RIFF::Exception if an IO error occurred |
729 |
|
* @see Resize() |
730 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
731 |
|
*/ |
732 |
|
file_offset_t Chunk::WriteUint16(uint16_t* pData, file_offset_t WordCount) { |
733 |
|
return Write(pData, WordCount, 2); |
734 |
|
} |
735 |
|
|
736 |
|
/** |
737 |
* Reads \a WordCount number of 32 Bit signed integer words and copies |
* Reads \a WordCount number of 32 Bit signed integer words and copies |
738 |
* it into the buffer pointed by \a pData. The buffer has to be |
* it into the buffer pointed by \a pData. The buffer has to be |
739 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
742 |
* @param pData destination buffer |
* @param pData destination buffer |
743 |
* @param WordCount number of 32 Bit signed integers to read |
* @param WordCount number of 32 Bit signed integers to read |
744 |
* @returns number of read integers |
* @returns number of read integers |
745 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
746 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
747 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
748 |
*/ |
*/ |
749 |
unsigned long Chunk::ReadInt32(int32_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadInt32(int32_t* pData, file_offset_t WordCount) { |
750 |
#if DEBUG |
#if DEBUG_RIFF |
751 |
std::cout << "Chunk::ReadInt32(int32_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadInt32(int32_t*,file_offset_t)" << std::endl; |
752 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
753 |
return ReadSceptical(pData, WordCount, 4); |
return ReadSceptical(pData, WordCount, 4); |
754 |
} |
} |
755 |
|
|
756 |
/** |
/** |
757 |
|
* Writes \a WordCount number of 32 Bit signed integer words from the |
758 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
759 |
|
* actual "physical" file. The position within the chunk will |
760 |
|
* automatically be incremented. Note: you cannot write beyond the |
761 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
762 |
|
* before. |
763 |
|
* |
764 |
|
* @param pData source buffer (containing the data) |
765 |
|
* @param WordCount number of 32 Bit signed integers to write |
766 |
|
* @returns number of written integers |
767 |
|
* @throws RIFF::Exception if an IO error occurred |
768 |
|
* @see Resize() |
769 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
770 |
|
*/ |
771 |
|
file_offset_t Chunk::WriteInt32(int32_t* pData, file_offset_t WordCount) { |
772 |
|
return Write(pData, WordCount, 4); |
773 |
|
} |
774 |
|
|
775 |
|
/** |
776 |
* Reads \a WordCount number of 32 Bit unsigned integer words and copies |
* Reads \a WordCount number of 32 Bit unsigned integer words and copies |
777 |
* it into the buffer pointed by \a pData. The buffer has to be |
* it into the buffer pointed by \a pData. The buffer has to be |
778 |
* allocated. Endian correction will automatically be done if needed. |
* allocated. Endian correction will automatically be done if needed. |
781 |
* @param pData destination buffer |
* @param pData destination buffer |
782 |
* @param WordCount number of 32 Bit unsigned integers to read |
* @param WordCount number of 32 Bit unsigned integers to read |
783 |
* @returns number of read integers |
* @returns number of read integers |
784 |
* @throws RIFF::Exception if an error occured or less than |
* @throws RIFF::Exception if an error occurred or less than |
785 |
* \a WordCount integers could be read! |
* \a WordCount integers could be read! |
786 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
787 |
*/ |
*/ |
788 |
unsigned long Chunk::ReadUint32(uint32_t* pData, unsigned long WordCount) { |
file_offset_t Chunk::ReadUint32(uint32_t* pData, file_offset_t WordCount) { |
789 |
#if DEBUG |
#if DEBUG_RIFF |
790 |
std::cout << "Chunk::ReadUint32(uint32_t*,ulong)" << std::endl; |
std::cout << "Chunk::ReadUint32(uint32_t*,file_offset_t)" << std::endl; |
791 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
792 |
return ReadSceptical(pData, WordCount, 4); |
return ReadSceptical(pData, WordCount, 4); |
793 |
} |
} |
794 |
|
|
795 |
/** |
/** |
796 |
|
* Reads a null-padded string of size characters and copies it |
797 |
|
* into the string \a s. The position within the chunk will |
798 |
|
* automatically be incremented. |
799 |
|
* |
800 |
|
* @param s destination string |
801 |
|
* @param size number of characters to read |
802 |
|
* @throws RIFF::Exception if an error occurred or less than |
803 |
|
* \a size characters could be read! |
804 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
805 |
|
*/ |
806 |
|
void Chunk::ReadString(String& s, int size) { |
807 |
|
char* buf = new char[size]; |
808 |
|
ReadSceptical(buf, 1, size); |
809 |
|
s.assign(buf, std::find(buf, buf + size, '\0')); |
810 |
|
delete[] buf; |
811 |
|
} |
812 |
|
|
813 |
|
/** |
814 |
|
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
815 |
|
* buffer pointed by \a pData to the chunk's body, directly to the |
816 |
|
* actual "physical" file. The position within the chunk will |
817 |
|
* automatically be incremented. Note: you cannot write beyond the |
818 |
|
* boundaries of the chunk, to append data to the chunk call Resize() |
819 |
|
* before. |
820 |
|
* |
821 |
|
* @param pData source buffer (containing the data) |
822 |
|
* @param WordCount number of 32 Bit unsigned integers to write |
823 |
|
* @returns number of written integers |
824 |
|
* @throws RIFF::Exception if an IO error occurred |
825 |
|
* @see Resize() |
826 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
827 |
|
*/ |
828 |
|
file_offset_t Chunk::WriteUint32(uint32_t* pData, file_offset_t WordCount) { |
829 |
|
return Write(pData, WordCount, 4); |
830 |
|
} |
831 |
|
|
832 |
|
/** |
833 |
* Reads one 8 Bit signed integer word and increments the position within |
* Reads one 8 Bit signed integer word and increments the position within |
834 |
* the chunk. |
* the chunk. |
835 |
* |
* |
836 |
* @returns read integer word |
* @returns read integer word |
837 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
838 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
839 |
*/ |
*/ |
840 |
int8_t Chunk::ReadInt8() { |
int8_t Chunk::ReadInt8() { |
841 |
#if DEBUG |
#if DEBUG_RIFF |
842 |
std::cout << "Chunk::ReadInt8()" << std::endl; |
std::cout << "Chunk::ReadInt8()" << std::endl; |
843 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
844 |
int8_t word; |
int8_t word; |
845 |
ReadSceptical(&word,1,1); |
ReadSceptical(&word,1,1); |
846 |
return word; |
return word; |
851 |
* within the chunk. |
* within the chunk. |
852 |
* |
* |
853 |
* @returns read integer word |
* @returns read integer word |
854 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
855 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
856 |
*/ |
*/ |
857 |
uint8_t Chunk::ReadUint8() { |
uint8_t Chunk::ReadUint8() { |
858 |
#if DEBUG |
#if DEBUG_RIFF |
859 |
std::cout << "Chunk::ReadUint8()" << std::endl; |
std::cout << "Chunk::ReadUint8()" << std::endl; |
860 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
861 |
uint8_t word; |
uint8_t word; |
862 |
ReadSceptical(&word,1,1); |
ReadSceptical(&word,1,1); |
863 |
return word; |
return word; |
869 |
* needed. |
* needed. |
870 |
* |
* |
871 |
* @returns read integer word |
* @returns read integer word |
872 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
873 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
874 |
*/ |
*/ |
875 |
int16_t Chunk::ReadInt16() { |
int16_t Chunk::ReadInt16() { |
876 |
#if DEBUG |
#if DEBUG_RIFF |
877 |
std::cout << "Chunk::ReadInt16()" << std::endl; |
std::cout << "Chunk::ReadInt16()" << std::endl; |
878 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
879 |
int16_t word; |
int16_t word; |
880 |
ReadSceptical(&word,1,2); |
ReadSceptical(&word,1,2); |
881 |
return word; |
return word; |
887 |
* needed. |
* needed. |
888 |
* |
* |
889 |
* @returns read integer word |
* @returns read integer word |
890 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
891 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
892 |
*/ |
*/ |
893 |
uint16_t Chunk::ReadUint16() { |
uint16_t Chunk::ReadUint16() { |
894 |
#if DEBUG |
#if DEBUG_RIFF |
895 |
std::cout << "Chunk::ReadUint16()" << std::endl; |
std::cout << "Chunk::ReadUint16()" << std::endl; |
896 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
897 |
uint16_t word; |
uint16_t word; |
898 |
ReadSceptical(&word,1,2); |
ReadSceptical(&word,1,2); |
899 |
return word; |
return word; |
905 |
* needed. |
* needed. |
906 |
* |
* |
907 |
* @returns read integer word |
* @returns read integer word |
908 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
909 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
910 |
*/ |
*/ |
911 |
int32_t Chunk::ReadInt32() { |
int32_t Chunk::ReadInt32() { |
912 |
#if DEBUG |
#if DEBUG_RIFF |
913 |
std::cout << "Chunk::ReadInt32()" << std::endl; |
std::cout << "Chunk::ReadInt32()" << std::endl; |
914 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
915 |
int32_t word; |
int32_t word; |
916 |
ReadSceptical(&word,1,4); |
ReadSceptical(&word,1,4); |
917 |
return word; |
return word; |
923 |
* needed. |
* needed. |
924 |
* |
* |
925 |
* @returns read integer word |
* @returns read integer word |
926 |
* @throws RIFF::Exception if an error occured |
* @throws RIFF::Exception if an error occurred |
927 |
|
* @see File::IsIOPerThread() for multi-threaded streamings |
928 |
*/ |
*/ |
929 |
uint32_t Chunk::ReadUint32() { |
uint32_t Chunk::ReadUint32() { |
930 |
#if DEBUG |
#if DEBUG_RIFF |
931 |
std::cout << "Chunk::ReadUint32()" << std::endl; |
std::cout << "Chunk::ReadUint32()" << std::endl; |
932 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
933 |
uint32_t word; |
uint32_t word; |
934 |
ReadSceptical(&word,1,4); |
ReadSceptical(&word,1,4); |
935 |
return word; |
return word; |
936 |
} |
} |
937 |
|
|
938 |
|
/** @brief Load chunk body into RAM. |
939 |
|
* |
940 |
|
* Loads the whole chunk body into memory. You can modify the data in |
941 |
|
* RAM and save the data by calling File::Save() afterwards. |
942 |
|
* |
943 |
|
* <b>Caution:</b> the buffer pointer will be invalidated once |
944 |
|
* File::Save() was called. You have to call LoadChunkData() again to |
945 |
|
* get a new, valid pointer whenever File::Save() was called. |
946 |
|
* |
947 |
|
* You can call LoadChunkData() again if you previously scheduled to |
948 |
|
* enlarge this chunk with a Resize() call. In that case the buffer will |
949 |
|
* be enlarged to the new, scheduled chunk size and you can already |
950 |
|
* place the new chunk data to the buffer and finally call File::Save() |
951 |
|
* to enlarge the chunk physically and write the new data in one rush. |
952 |
|
* This approach is definitely recommended if you have to enlarge and |
953 |
|
* write new data to a lot of chunks. |
954 |
|
* |
955 |
|
* @returns a pointer to the data in RAM on success, NULL otherwise |
956 |
|
* @throws Exception if data buffer could not be enlarged |
957 |
|
* @see ReleaseChunkData() |
958 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
959 |
|
*/ |
960 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
961 |
if (!pChunkData) { |
if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) { |
962 |
|
File::Handle hRead = pFile->FileHandle(); |
963 |
#if POSIX |
#if POSIX |
964 |
if (lseek(hFile, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(hRead, ullStartPos, SEEK_SET) == -1) return NULL; |
965 |
pChunkData = new uint8_t[GetSize()]; |
#elif defined(WIN32) |
966 |
if (!pChunkData) return NULL; |
LARGE_INTEGER liFilePos; |
967 |
unsigned long readWords = read(hFile, pChunkData, GetSize()); |
liFilePos.QuadPart = ullStartPos; |
968 |
|
if (!SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) return NULL; |
969 |
#else |
#else |
970 |
if (fseek(hFile, ulStartPos, SEEK_SET)) return NULL; |
if (fseeko(hRead, ullStartPos, SEEK_SET)) return NULL; |
971 |
pChunkData = new uint8_t[GetSize()]; |
#endif // POSIX |
972 |
|
file_offset_t ullBufferSize = (ullCurrentChunkSize > ullNewChunkSize) ? ullCurrentChunkSize : ullNewChunkSize; |
973 |
|
pChunkData = new uint8_t[ullBufferSize]; |
974 |
if (!pChunkData) return NULL; |
if (!pChunkData) return NULL; |
975 |
unsigned long readWords = fread(pChunkData, 1, GetSize(), hFile); |
memset(pChunkData, 0, ullBufferSize); |
976 |
|
#if POSIX |
977 |
|
file_offset_t readWords = read(hRead, pChunkData, GetSize()); |
978 |
|
#elif defined(WIN32) |
979 |
|
DWORD readWords; |
980 |
|
ReadFile(hRead, pChunkData, GetSize(), &readWords, NULL); //FIXME: won't load chunks larger than 2GB ! |
981 |
|
#else |
982 |
|
file_offset_t readWords = fread(pChunkData, 1, GetSize(), hRead); |
983 |
#endif // POSIX |
#endif // POSIX |
984 |
if (readWords != GetSize()) { |
if (readWords != GetSize()) { |
985 |
delete[] pChunkData; |
delete[] pChunkData; |
986 |
return (pChunkData = NULL); |
return (pChunkData = NULL); |
987 |
} |
} |
988 |
|
ullChunkDataSize = ullBufferSize; |
989 |
|
} else if (ullNewChunkSize > ullChunkDataSize) { |
990 |
|
uint8_t* pNewBuffer = new uint8_t[ullNewChunkSize]; |
991 |
|
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(ullNewChunkSize) + " bytes"); |
992 |
|
memset(pNewBuffer, 0 , ullNewChunkSize); |
993 |
|
if (pChunkData) { |
994 |
|
memcpy(pNewBuffer, pChunkData, ullChunkDataSize); |
995 |
|
delete[] pChunkData; |
996 |
|
} |
997 |
|
pChunkData = pNewBuffer; |
998 |
|
ullChunkDataSize = ullNewChunkSize; |
999 |
} |
} |
1000 |
return pChunkData; |
return pChunkData; |
1001 |
} |
} |
1002 |
|
|
1003 |
|
/** @brief Free loaded chunk body from RAM. |
1004 |
|
* |
1005 |
|
* Frees loaded chunk body data from memory (RAM). You should call |
1006 |
|
* File::Save() before calling this method if you modified the data to |
1007 |
|
* make the changes persistent. |
1008 |
|
*/ |
1009 |
void Chunk::ReleaseChunkData() { |
void Chunk::ReleaseChunkData() { |
1010 |
if (pChunkData) { |
if (pChunkData) { |
1011 |
delete[] pChunkData; |
delete[] pChunkData; |
1013 |
} |
} |
1014 |
} |
} |
1015 |
|
|
1016 |
|
/** @brief Resize chunk. |
1017 |
|
* |
1018 |
|
* Resizes this chunk's body, that is the actual size of data possible |
1019 |
|
* to be written to this chunk. This call will return immediately and |
1020 |
|
* just schedule the resize operation. You should call File::Save() to |
1021 |
|
* actually perform the resize operation(s) "physically" to the file. |
1022 |
|
* As this can take a while on large files, it is recommended to call |
1023 |
|
* Resize() first on all chunks which have to be resized and finally to |
1024 |
|
* call File::Save() to perform all those resize operations in one rush. |
1025 |
|
* |
1026 |
|
* <b>Caution:</b> You cannot directly write to enlarged chunks before |
1027 |
|
* calling File::Save() as this might exceed the current chunk's body |
1028 |
|
* boundary! |
1029 |
|
* |
1030 |
|
* @param NewSize - new chunk body size in bytes (must be greater than zero) |
1031 |
|
* @throws RIFF::Exception if \a NewSize is less than 1 or unrealistic large |
1032 |
|
* @see File::Save() |
1033 |
|
*/ |
1034 |
|
void Chunk::Resize(file_offset_t NewSize) { |
1035 |
|
if (NewSize == 0) |
1036 |
|
throw Exception("There is at least one empty chunk (zero size): " + __resolveChunkPath(this)); |
1037 |
|
if ((NewSize >> 48) != 0) |
1038 |
|
throw Exception("Unrealistic high chunk size detected: " + __resolveChunkPath(this)); |
1039 |
|
if (ullNewChunkSize == NewSize) return; |
1040 |
|
ullNewChunkSize = NewSize; |
1041 |
|
} |
1042 |
|
|
1043 |
|
/** @brief Write chunk persistently e.g. to disk. |
1044 |
|
* |
1045 |
|
* Stores the chunk persistently to its actual "physical" file. |
1046 |
|
* |
1047 |
|
* @param ullWritePos - position within the "physical" file where this |
1048 |
|
* chunk should be written to |
1049 |
|
* @param ullCurrentDataOffset - offset of current (old) data within |
1050 |
|
* the file |
1051 |
|
* @param pProgress - optional: callback function for progress notification |
1052 |
|
* @returns new write position in the "physical" file, that is |
1053 |
|
* \a ullWritePos incremented by this chunk's new size |
1054 |
|
* (including its header size of course) |
1055 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
1056 |
|
*/ |
1057 |
|
file_offset_t Chunk::WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress) { |
1058 |
|
const file_offset_t ullOriginalPos = ullWritePos; |
1059 |
|
ullWritePos += CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1060 |
|
|
1061 |
|
const File::HandlePair io = pFile->FileHandlePair(); |
1062 |
|
|
1063 |
|
if (io.Mode != stream_mode_read_write) |
1064 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
1065 |
|
|
1066 |
|
// if the whole chunk body was loaded into RAM |
1067 |
|
if (pChunkData) { |
1068 |
|
// make sure chunk data buffer in RAM is at least as large as the new chunk size |
1069 |
|
LoadChunkData(); |
1070 |
|
// write chunk data from RAM persistently to the file |
1071 |
|
#if POSIX |
1072 |
|
lseek(io.hWrite, ullWritePos, SEEK_SET); |
1073 |
|
if (write(io.hWrite, pChunkData, ullNewChunkSize) != ullNewChunkSize) { |
1074 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
1075 |
|
} |
1076 |
|
#elif defined(WIN32) |
1077 |
|
LARGE_INTEGER liFilePos; |
1078 |
|
liFilePos.QuadPart = ullWritePos; |
1079 |
|
SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1080 |
|
DWORD dwBytesWritten; |
1081 |
|
WriteFile(io.hWrite, pChunkData, ullNewChunkSize, &dwBytesWritten, NULL); //FIXME: won't save chunks larger than 2GB ! |
1082 |
|
if (dwBytesWritten != ullNewChunkSize) { |
1083 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
1084 |
|
} |
1085 |
|
#else |
1086 |
|
fseeko(io.hWrite, ullWritePos, SEEK_SET); |
1087 |
|
if (fwrite(pChunkData, 1, ullNewChunkSize, io.hWrite) != ullNewChunkSize) { |
1088 |
|
throw Exception("Writing Chunk data (from RAM) failed"); |
1089 |
|
} |
1090 |
|
#endif // POSIX |
1091 |
|
} else { |
1092 |
|
// move chunk data from the end of the file to the appropriate position |
1093 |
|
int8_t* pCopyBuffer = new int8_t[4096]; |
1094 |
|
file_offset_t ullToMove = (ullNewChunkSize < ullCurrentChunkSize) ? ullNewChunkSize : ullCurrentChunkSize; |
1095 |
|
#if defined(WIN32) |
1096 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
1097 |
|
#else |
1098 |
|
int iBytesMoved = 1; |
1099 |
|
#endif |
1100 |
|
for (file_offset_t ullOffset = 0; ullToMove > 0 && iBytesMoved > 0; ullOffset += iBytesMoved, ullToMove -= iBytesMoved) { |
1101 |
|
iBytesMoved = (ullToMove < 4096) ? int(ullToMove) : 4096; |
1102 |
|
#if POSIX |
1103 |
|
lseek(io.hRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
1104 |
|
iBytesMoved = (int) read(io.hRead, pCopyBuffer, (size_t) iBytesMoved); |
1105 |
|
lseek(io.hWrite, ullWritePos + ullOffset, SEEK_SET); |
1106 |
|
iBytesMoved = (int) write(io.hWrite, pCopyBuffer, (size_t) iBytesMoved); |
1107 |
|
#elif defined(WIN32) |
1108 |
|
LARGE_INTEGER liFilePos; |
1109 |
|
liFilePos.QuadPart = ullStartPos + ullCurrentDataOffset + ullOffset; |
1110 |
|
SetFilePointerEx(io.hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1111 |
|
ReadFile(io.hRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1112 |
|
liFilePos.QuadPart = ullWritePos + ullOffset; |
1113 |
|
SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1114 |
|
WriteFile(io.hWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1115 |
|
#else |
1116 |
|
fseeko(io.hRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
1117 |
|
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, io.hRead); |
1118 |
|
fseeko(io.hWrite, ullWritePos + ullOffset, SEEK_SET); |
1119 |
|
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, io.hWrite); |
1120 |
|
#endif |
1121 |
|
} |
1122 |
|
delete[] pCopyBuffer; |
1123 |
|
if (iBytesMoved < 0) throw Exception("Writing Chunk data (from file) failed"); |
1124 |
|
} |
1125 |
|
|
1126 |
|
// update this chunk's header |
1127 |
|
ullCurrentChunkSize = ullNewChunkSize; |
1128 |
|
WriteHeader(ullOriginalPos); |
1129 |
|
|
1130 |
|
if (pProgress) |
1131 |
|
__notify_progress(pProgress, 1.0); // notify done |
1132 |
|
|
1133 |
|
// update chunk's position pointers |
1134 |
|
ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1135 |
|
Chunk::__resetPos(); |
1136 |
|
|
1137 |
|
// add pad byte if needed |
1138 |
|
if ((ullStartPos + ullNewChunkSize) % 2 != 0) { |
1139 |
|
const char cPadByte = 0; |
1140 |
|
#if POSIX |
1141 |
|
lseek(io.hWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
1142 |
|
write(io.hWrite, &cPadByte, 1); |
1143 |
|
#elif defined(WIN32) |
1144 |
|
LARGE_INTEGER liFilePos; |
1145 |
|
liFilePos.QuadPart = ullStartPos + ullNewChunkSize; |
1146 |
|
SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1147 |
|
DWORD dwBytesWritten; |
1148 |
|
WriteFile(io.hWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
1149 |
|
#else |
1150 |
|
fseeko(io.hWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
1151 |
|
fwrite(&cPadByte, 1, 1, io.hWrite); |
1152 |
|
#endif |
1153 |
|
return ullStartPos + ullNewChunkSize + 1; |
1154 |
|
} |
1155 |
|
|
1156 |
|
return ullStartPos + ullNewChunkSize; |
1157 |
|
} |
1158 |
|
|
1159 |
|
void Chunk::__resetPos() { |
1160 |
|
std::lock_guard<std::mutex> lock(chunkPos.mutex); |
1161 |
|
chunkPos.ullPos = 0; |
1162 |
|
chunkPos.byThread.clear(); |
1163 |
|
} |
1164 |
|
|
1165 |
|
|
1166 |
|
|
1167 |
// *************** List *************** |
// *************** List *************** |
1168 |
// * |
// * |
1169 |
|
|
1170 |
List::List() : Chunk() { |
List::List(File* pFile) : Chunk(pFile) { |
1171 |
#if DEBUG |
#if DEBUG_RIFF |
1172 |
std::cout << "List::List()" << std::endl; |
std::cout << "List::List(File* pFile)" << std::endl; |
1173 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1174 |
pSubChunks = NULL; |
pSubChunks = NULL; |
1175 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
1176 |
} |
} |
1177 |
|
|
1178 |
#if POSIX |
List::List(File* pFile, file_offset_t StartPos, List* Parent) |
1179 |
List::List(int hFile, unsigned long StartPos, bool EndianNative, List* Parent) |
: Chunk(pFile, StartPos, Parent) { |
1180 |
#else |
#if DEBUG_RIFF |
1181 |
List::List(FILE* hFile, unsigned long StartPos, bool EndianNative, List* Parent) |
std::cout << "List::List(File*,file_offset_t,List*)" << std::endl; |
1182 |
#endif // POSIX |
#endif // DEBUG_RIFF |
|
: Chunk(hFile, StartPos, EndianNative, Parent) { |
|
|
#if DEBUG |
|
|
std::cout << "List::List(FILE*,ulong,bool,List*)" << std::endl; |
|
|
#endif // DEBUG |
|
1183 |
pSubChunks = NULL; |
pSubChunks = NULL; |
1184 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
1185 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
1186 |
ulStartPos = StartPos + LIST_HEADER_SIZE; |
ullStartPos = StartPos + LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1187 |
|
} |
1188 |
|
|
1189 |
|
List::List(File* pFile, List* pParent, uint32_t uiListID) |
1190 |
|
: Chunk(pFile, pParent, CHUNK_ID_LIST, 0) { |
1191 |
|
pSubChunks = NULL; |
1192 |
|
pSubChunksMap = NULL; |
1193 |
|
ListType = uiListID; |
1194 |
} |
} |
1195 |
|
|
1196 |
List::~List() { |
List::~List() { |
1197 |
#if DEBUG |
#if DEBUG_RIFF |
1198 |
std::cout << "List::~List()" << std::endl; |
std::cout << "List::~List()" << std::endl; |
1199 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1200 |
|
DeleteChunkList(); |
1201 |
|
} |
1202 |
|
|
1203 |
|
void List::DeleteChunkList() { |
1204 |
if (pSubChunks) { |
if (pSubChunks) { |
1205 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1206 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1209 |
iter++; |
iter++; |
1210 |
} |
} |
1211 |
delete pSubChunks; |
delete pSubChunks; |
1212 |
|
pSubChunks = NULL; |
1213 |
|
} |
1214 |
|
if (pSubChunksMap) { |
1215 |
|
delete pSubChunksMap; |
1216 |
|
pSubChunksMap = NULL; |
1217 |
} |
} |
|
if (pSubChunksMap) delete pSubChunksMap; |
|
1218 |
} |
} |
1219 |
|
|
1220 |
/** |
/** |
1229 |
* that ID |
* that ID |
1230 |
*/ |
*/ |
1231 |
Chunk* List::GetSubChunk(uint32_t ChunkID) { |
Chunk* List::GetSubChunk(uint32_t ChunkID) { |
1232 |
#if DEBUG |
#if DEBUG_RIFF |
1233 |
std::cout << "List::GetSubChunk(uint32_t)" << std::endl; |
std::cout << "List::GetSubChunk(uint32_t)" << std::endl; |
1234 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1235 |
if (!pSubChunksMap) LoadSubChunks(); |
if (!pSubChunksMap) LoadSubChunks(); |
1236 |
return (*pSubChunksMap)[ChunkID]; |
return (*pSubChunksMap)[ChunkID]; |
1237 |
} |
} |
1239 |
/** |
/** |
1240 |
* Returns sublist chunk with list type <i>\a ListType</i> within this |
* Returns sublist chunk with list type <i>\a ListType</i> within this |
1241 |
* 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 |
1242 |
* 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 |
1243 |
* 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 |
1244 |
* that desired list type, NULL will be returned. |
* that desired list type, NULL will be returned. |
1245 |
* |
* |
1248 |
* that type |
* that type |
1249 |
*/ |
*/ |
1250 |
List* List::GetSubList(uint32_t ListType) { |
List* List::GetSubList(uint32_t ListType) { |
1251 |
#if DEBUG |
#if DEBUG_RIFF |
1252 |
std::cout << "List::GetSubList(uint32_t)" << std::endl; |
std::cout << "List::GetSubList(uint32_t)" << std::endl; |
1253 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1254 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1255 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1256 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1265 |
} |
} |
1266 |
|
|
1267 |
/** |
/** |
1268 |
* Returns the first subchunk within the list. You have to call this |
* Returns the first subchunk within the list (which may be an ordinary |
1269 |
|
* chunk as well as a list chunk). You have to call this |
1270 |
* method before you can call GetNextSubChunk(). Recall it when you want |
* method before you can call GetNextSubChunk(). Recall it when you want |
1271 |
* to start from the beginning of the list again. |
* to start from the beginning of the list again. |
1272 |
* |
* |
1274 |
* otherwise |
* otherwise |
1275 |
*/ |
*/ |
1276 |
Chunk* List::GetFirstSubChunk() { |
Chunk* List::GetFirstSubChunk() { |
1277 |
#if DEBUG |
#if DEBUG_RIFF |
1278 |
std::cout << "List::GetFirstSubChunk()" << std::endl; |
std::cout << "List::GetFirstSubChunk()" << std::endl; |
1279 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1280 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1281 |
ChunksIterator = pSubChunks->begin(); |
ChunksIterator = pSubChunks->begin(); |
1282 |
return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL; |
return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL; |
1283 |
} |
} |
1284 |
|
|
1285 |
/** |
/** |
1286 |
* Returns the next subchunk within the list. You have to call |
* Returns the next subchunk within the list (which may be an ordinary |
1287 |
|
* chunk as well as a list chunk). You have to call |
1288 |
* GetFirstSubChunk() before you can use this method! |
* GetFirstSubChunk() before you can use this method! |
1289 |
* |
* |
1290 |
* @returns pointer to the next subchunk within the list or NULL if |
* @returns pointer to the next subchunk within the list or NULL if |
1291 |
* end of list is reached |
* end of list is reached |
1292 |
*/ |
*/ |
1293 |
Chunk* List::GetNextSubChunk() { |
Chunk* List::GetNextSubChunk() { |
1294 |
#if DEBUG |
#if DEBUG_RIFF |
1295 |
std::cout << "List::GetNextSubChunk()" << std::endl; |
std::cout << "List::GetNextSubChunk()" << std::endl; |
1296 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1297 |
if (!pSubChunks) return NULL; |
if (!pSubChunks) return NULL; |
1298 |
ChunksIterator++; |
ChunksIterator++; |
1299 |
return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL; |
return (ChunksIterator != pSubChunks->end()) ? *ChunksIterator : NULL; |
1309 |
* otherwise |
* otherwise |
1310 |
*/ |
*/ |
1311 |
List* List::GetFirstSubList() { |
List* List::GetFirstSubList() { |
1312 |
#if DEBUG |
#if DEBUG_RIFF |
1313 |
std::cout << "List::GetFirstSubList()" << std::endl; |
std::cout << "List::GetFirstSubList()" << std::endl; |
1314 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1315 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1316 |
ListIterator = pSubChunks->begin(); |
ListIterator = pSubChunks->begin(); |
1317 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1331 |
* end of list is reached |
* end of list is reached |
1332 |
*/ |
*/ |
1333 |
List* List::GetNextSubList() { |
List* List::GetNextSubList() { |
1334 |
#if DEBUG |
#if DEBUG_RIFF |
1335 |
std::cout << "List::GetNextSubList()" << std::endl; |
std::cout << "List::GetNextSubList()" << std::endl; |
1336 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1337 |
if (!pSubChunks) return NULL; |
if (!pSubChunks) return NULL; |
1338 |
if (ListIterator == pSubChunks->end()) return NULL; |
if (ListIterator == pSubChunks->end()) return NULL; |
1339 |
ListIterator++; |
ListIterator++; |
1346 |
} |
} |
1347 |
|
|
1348 |
/** |
/** |
1349 |
* Returns number subchunks within the list. |
* Returns number of subchunks within the list (including list chunks). |
1350 |
*/ |
*/ |
1351 |
unsigned int List::CountSubChunks() { |
size_t List::CountSubChunks() { |
1352 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1353 |
return pSubChunks->size(); |
return pSubChunks->size(); |
1354 |
} |
} |
1357 |
* Returns number of subchunks within the list with chunk ID |
* Returns number of subchunks within the list with chunk ID |
1358 |
* <i>\a ChunkId</i>. |
* <i>\a ChunkId</i>. |
1359 |
*/ |
*/ |
1360 |
unsigned int List::CountSubChunks(uint32_t ChunkID) { |
size_t List::CountSubChunks(uint32_t ChunkID) { |
1361 |
unsigned int result = 0; |
size_t result = 0; |
1362 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1363 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1364 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1374 |
/** |
/** |
1375 |
* Returns number of sublists within the list. |
* Returns number of sublists within the list. |
1376 |
*/ |
*/ |
1377 |
unsigned int List::CountSubLists() { |
size_t List::CountSubLists() { |
1378 |
return CountSubChunks(CHUNK_ID_LIST); |
return CountSubChunks(CHUNK_ID_LIST); |
1379 |
} |
} |
1380 |
|
|
1382 |
* Returns number of sublists within the list with list type |
* Returns number of sublists within the list with list type |
1383 |
* <i>\a ListType</i> |
* <i>\a ListType</i> |
1384 |
*/ |
*/ |
1385 |
unsigned int List::CountSubLists(uint32_t ListType) { |
size_t List::CountSubLists(uint32_t ListType) { |
1386 |
unsigned int result = 0; |
size_t result = 0; |
1387 |
if (!pSubChunks) LoadSubChunks(); |
if (!pSubChunks) LoadSubChunks(); |
1388 |
ChunkList::iterator iter = pSubChunks->begin(); |
ChunkList::iterator iter = pSubChunks->begin(); |
1389 |
ChunkList::iterator end = pSubChunks->end(); |
ChunkList::iterator end = pSubChunks->end(); |
1397 |
return result; |
return result; |
1398 |
} |
} |
1399 |
|
|
1400 |
void List::ReadHeader(unsigned long fPos) { |
/** @brief Creates a new sub chunk. |
1401 |
#if DEBUG |
* |
1402 |
std::cout << "List::Readheader(ulong) "; |
* Creates and adds a new sub chunk to this list chunk. Note that the |
1403 |
#endif // DEBUG |
* chunk's body size given by \a ullBodySize must be greater than zero. |
1404 |
Chunk::ReadHeader(fPos); |
* You have to call File::Save() to make this change persistent to the |
1405 |
ChunkSize -= 4; |
* actual file and <b>before</b> performing any data write operations |
1406 |
|
* on the new chunk! |
1407 |
|
* |
1408 |
|
* @param uiChunkID - chunk ID of the new chunk |
1409 |
|
* @param ullBodySize - size of the new chunk's body, that is its actual |
1410 |
|
* data size (without header) |
1411 |
|
* @throws RIFF::Exception if \a ullBodySize equals zero |
1412 |
|
*/ |
1413 |
|
Chunk* List::AddSubChunk(uint32_t uiChunkID, file_offset_t ullBodySize) { |
1414 |
|
if (ullBodySize == 0) throw Exception("Chunk body size must be at least 1 byte"); |
1415 |
|
if (!pSubChunks) LoadSubChunks(); |
1416 |
|
Chunk* pNewChunk = new Chunk(pFile, this, uiChunkID, 0); |
1417 |
|
pSubChunks->push_back(pNewChunk); |
1418 |
|
(*pSubChunksMap)[uiChunkID] = pNewChunk; |
1419 |
|
pNewChunk->Resize(ullBodySize); |
1420 |
|
ullNewChunkSize += CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1421 |
|
return pNewChunk; |
1422 |
|
} |
1423 |
|
|
1424 |
|
/** @brief Moves a sub chunk witin this list. |
1425 |
|
* |
1426 |
|
* Moves a sub chunk from one position in this list to another |
1427 |
|
* position in the same list. The pSrc chunk is placed before the |
1428 |
|
* pDst chunk. |
1429 |
|
* |
1430 |
|
* @param pSrc - sub chunk to be moved |
1431 |
|
* @param pDst - the position to move to. pSrc will be placed |
1432 |
|
* before pDst. If pDst is 0, pSrc will be placed |
1433 |
|
* last in list. |
1434 |
|
*/ |
1435 |
|
void List::MoveSubChunk(Chunk* pSrc, Chunk* pDst) { |
1436 |
|
if (!pSubChunks) LoadSubChunks(); |
1437 |
|
pSubChunks->remove(pSrc); |
1438 |
|
ChunkList::iterator iter = find(pSubChunks->begin(), pSubChunks->end(), pDst); |
1439 |
|
pSubChunks->insert(iter, pSrc); |
1440 |
|
} |
1441 |
|
|
1442 |
|
/** @brief Moves a sub chunk from this list to another list. |
1443 |
|
* |
1444 |
|
* Moves a sub chunk from this list list to the end of another |
1445 |
|
* list. |
1446 |
|
* |
1447 |
|
* @param pSrc - sub chunk to be moved |
1448 |
|
* @param pDst - destination list where the chunk shall be moved to |
1449 |
|
*/ |
1450 |
|
void List::MoveSubChunk(Chunk* pSrc, List* pNewParent) { |
1451 |
|
if (pNewParent == this || !pNewParent) return; |
1452 |
|
if (!pSubChunks) LoadSubChunks(); |
1453 |
|
if (!pNewParent->pSubChunks) pNewParent->LoadSubChunks(); |
1454 |
|
pSubChunks->remove(pSrc); |
1455 |
|
pNewParent->pSubChunks->push_back(pSrc); |
1456 |
|
// update chunk id map of this List |
1457 |
|
if ((*pSubChunksMap)[pSrc->GetChunkID()] == pSrc) { |
1458 |
|
pSubChunksMap->erase(pSrc->GetChunkID()); |
1459 |
|
// try to find another chunk of the same chunk ID |
1460 |
|
ChunkList::iterator iter = pSubChunks->begin(); |
1461 |
|
ChunkList::iterator end = pSubChunks->end(); |
1462 |
|
for (; iter != end; ++iter) { |
1463 |
|
if ((*iter)->GetChunkID() == pSrc->GetChunkID()) { |
1464 |
|
(*pSubChunksMap)[pSrc->GetChunkID()] = *iter; |
1465 |
|
break; // we're done, stop search |
1466 |
|
} |
1467 |
|
} |
1468 |
|
} |
1469 |
|
// update chunk id map of other list |
1470 |
|
if (!(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()]) |
1471 |
|
(*pNewParent->pSubChunksMap)[pSrc->GetChunkID()] = pSrc; |
1472 |
|
} |
1473 |
|
|
1474 |
|
/** @brief Creates a new list sub chunk. |
1475 |
|
* |
1476 |
|
* Creates and adds a new list sub chunk to this list chunk. Note that |
1477 |
|
* you have to add sub chunks / sub list chunks to the new created chunk |
1478 |
|
* <b>before</b> trying to make this change persisten to the actual |
1479 |
|
* file with File::Save()! |
1480 |
|
* |
1481 |
|
* @param uiListType - list ID of the new list chunk |
1482 |
|
*/ |
1483 |
|
List* List::AddSubList(uint32_t uiListType) { |
1484 |
|
if (!pSubChunks) LoadSubChunks(); |
1485 |
|
List* pNewListChunk = new List(pFile, this, uiListType); |
1486 |
|
pSubChunks->push_back(pNewListChunk); |
1487 |
|
(*pSubChunksMap)[CHUNK_ID_LIST] = pNewListChunk; |
1488 |
|
ullNewChunkSize += LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1489 |
|
return pNewListChunk; |
1490 |
|
} |
1491 |
|
|
1492 |
|
/** @brief Removes a sub chunk. |
1493 |
|
* |
1494 |
|
* Removes the sub chunk given by \a pSubChunk from this list and frees |
1495 |
|
* it completely from RAM. The given chunk can either be a normal sub |
1496 |
|
* chunk or a list sub chunk. In case the given chunk is a list chunk, |
1497 |
|
* all its subchunks (if any) will be removed recursively as well. You |
1498 |
|
* should call File::Save() to make this change persistent at any time. |
1499 |
|
* |
1500 |
|
* @param pSubChunk - sub chunk or sub list chunk to be removed |
1501 |
|
*/ |
1502 |
|
void List::DeleteSubChunk(Chunk* pSubChunk) { |
1503 |
|
if (!pSubChunks) LoadSubChunks(); |
1504 |
|
pSubChunks->remove(pSubChunk); |
1505 |
|
if ((*pSubChunksMap)[pSubChunk->GetChunkID()] == pSubChunk) { |
1506 |
|
pSubChunksMap->erase(pSubChunk->GetChunkID()); |
1507 |
|
// try to find another chunk of the same chunk ID |
1508 |
|
ChunkList::iterator iter = pSubChunks->begin(); |
1509 |
|
ChunkList::iterator end = pSubChunks->end(); |
1510 |
|
for (; iter != end; ++iter) { |
1511 |
|
if ((*iter)->GetChunkID() == pSubChunk->GetChunkID()) { |
1512 |
|
(*pSubChunksMap)[pSubChunk->GetChunkID()] = *iter; |
1513 |
|
break; // we're done, stop search |
1514 |
|
} |
1515 |
|
} |
1516 |
|
} |
1517 |
|
delete pSubChunk; |
1518 |
|
} |
1519 |
|
|
1520 |
|
/** |
1521 |
|
* Returns the actual total size in bytes (including List chunk header and |
1522 |
|
* all subchunks) of this List Chunk if being stored to a file. |
1523 |
|
* |
1524 |
|
* @param fileOffsetSize - RIFF file offset size (in bytes) assumed when |
1525 |
|
* being saved to a file |
1526 |
|
*/ |
1527 |
|
file_offset_t List::RequiredPhysicalSize(int fileOffsetSize) { |
1528 |
|
if (!pSubChunks) LoadSubChunks(); |
1529 |
|
file_offset_t size = LIST_HEADER_SIZE(fileOffsetSize); |
1530 |
|
ChunkList::iterator iter = pSubChunks->begin(); |
1531 |
|
ChunkList::iterator end = pSubChunks->end(); |
1532 |
|
for (; iter != end; ++iter) |
1533 |
|
size += (*iter)->RequiredPhysicalSize(fileOffsetSize); |
1534 |
|
return size; |
1535 |
|
} |
1536 |
|
|
1537 |
|
void List::ReadHeader(file_offset_t filePos) { |
1538 |
|
#if DEBUG_RIFF |
1539 |
|
std::cout << "List::Readheader(file_offset_t) "; |
1540 |
|
#endif // DEBUG_RIFF |
1541 |
|
Chunk::ReadHeader(filePos); |
1542 |
|
if (ullCurrentChunkSize < 4) return; |
1543 |
|
ullNewChunkSize = ullCurrentChunkSize -= 4; |
1544 |
|
|
1545 |
|
const File::Handle hRead = pFile->FileHandle(); |
1546 |
|
|
1547 |
#if POSIX |
#if POSIX |
1548 |
lseek(hFile, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
lseek(hRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1549 |
read(hFile, &ListType, 4); |
read(hRead, &ListType, 4); |
1550 |
|
#elif defined(WIN32) |
1551 |
|
LARGE_INTEGER liFilePos; |
1552 |
|
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1553 |
|
SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1554 |
|
DWORD dwBytesRead; |
1555 |
|
ReadFile(hRead, &ListType, 4, &dwBytesRead, NULL); |
1556 |
#else |
#else |
1557 |
fseek(hFile, fPos + CHUNK_HEADER_SIZE, SEEK_SET); |
fseeko(hRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1558 |
fread(&ListType, 4, 1, hFile); |
fread(&ListType, 4, 1, hRead); |
1559 |
#endif // POSIX |
#endif // POSIX |
1560 |
#if DEBUG |
#if DEBUG_RIFF |
1561 |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
std::cout << "listType=" << convertToString(ListType) << std::endl; |
1562 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1563 |
if (!bEndianNative) { |
if (!pFile->bEndianNative) { |
1564 |
//swapBytes_32(&ListType); |
//swapBytes_32(&ListType); |
1565 |
} |
} |
1566 |
} |
} |
1567 |
|
|
1568 |
void List::LoadSubChunks() { |
void List::WriteHeader(file_offset_t filePos) { |
1569 |
#if DEBUG |
// the four list type bytes officially belong the chunk's body in the RIFF format |
1570 |
std::cout << "List::LoadSubChunks()"; |
ullNewChunkSize += 4; |
1571 |
#endif // DEBUG |
Chunk::WriteHeader(filePos); |
1572 |
|
ullNewChunkSize -= 4; // just revert the +4 incrementation |
1573 |
|
|
1574 |
|
const File::Handle hWrite = pFile->FileWriteHandle(); |
1575 |
|
|
1576 |
|
#if POSIX |
1577 |
|
lseek(hWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1578 |
|
write(hWrite, &ListType, 4); |
1579 |
|
#elif defined(WIN32) |
1580 |
|
LARGE_INTEGER liFilePos; |
1581 |
|
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1582 |
|
SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1583 |
|
DWORD dwBytesWritten; |
1584 |
|
WriteFile(hWrite, &ListType, 4, &dwBytesWritten, NULL); |
1585 |
|
#else |
1586 |
|
fseeko(hWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1587 |
|
fwrite(&ListType, 4, 1, hWrite); |
1588 |
|
#endif // POSIX |
1589 |
|
} |
1590 |
|
|
1591 |
|
void List::LoadSubChunks(progress_t* pProgress) { |
1592 |
|
#if DEBUG_RIFF |
1593 |
|
std::cout << "List::LoadSubChunks()"; |
1594 |
|
#endif // DEBUG_RIFF |
1595 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1596 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1597 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1598 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE) { |
|
1599 |
|
const File::Handle hRead = pFile->FileHandle(); |
1600 |
|
if (!_isValidHandle(hRead)) return; |
1601 |
|
|
1602 |
|
const file_offset_t ullOriginalPos = GetPos(); |
1603 |
|
SetPos(0); // jump to beginning of list chunk body |
1604 |
|
while (RemainingBytes() >= CHUNK_HEADER_SIZE(pFile->FileOffsetSize)) { |
1605 |
Chunk* ck; |
Chunk* ck; |
1606 |
uint32_t ckid; |
uint32_t ckid; |
1607 |
Read(&ckid, 4, 1); |
// return value check is required here to prevent a potential |
1608 |
#if DEBUG |
// garbage data use of 'ckid' below in case Read() failed |
1609 |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
if (Read(&ckid, 4, 1) != 4) |
1610 |
#endif // DEBUG |
throw Exception("LoadSubChunks(): Failed reading RIFF chunk ID"); |
1611 |
|
#if DEBUG_RIFF |
1612 |
|
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
1613 |
|
#endif // DEBUG_RIFF |
1614 |
|
const file_offset_t pos = GetPos(); |
1615 |
if (ckid == CHUNK_ID_LIST) { |
if (ckid == CHUNK_ID_LIST) { |
1616 |
ck = new RIFF::List(hFile, ulStartPos + ulPos - 4, bEndianNative, this); |
ck = new RIFF::List(pFile, ullStartPos + pos - 4, this); |
1617 |
SetPos(ck->GetSize() + LIST_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + LIST_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos); |
1618 |
} |
} |
1619 |
else { // simple chunk |
else { // simple chunk |
1620 |
ck = new RIFF::Chunk(hFile, ulStartPos + ulPos - 4, bEndianNative, this); |
ck = new RIFF::Chunk(pFile, ullStartPos + pos - 4, this); |
1621 |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE - 4, RIFF::stream_curpos); |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE(pFile->FileOffsetSize) - 4, RIFF::stream_curpos); |
1622 |
} |
} |
1623 |
pSubChunks->push_back(ck); |
pSubChunks->push_back(ck); |
1624 |
(*pSubChunksMap)[ckid] = ck; |
(*pSubChunksMap)[ckid] = ck; |
1625 |
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 |
1626 |
} |
} |
1627 |
|
SetPos(ullOriginalPos); // restore position before this call |
1628 |
|
} |
1629 |
|
if (pProgress) |
1630 |
|
__notify_progress(pProgress, 1.0); // notify done |
1631 |
|
} |
1632 |
|
|
1633 |
|
void List::LoadSubChunksRecursively(progress_t* pProgress) { |
1634 |
|
const int n = (int) CountSubLists(); |
1635 |
|
int i = 0; |
1636 |
|
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) { |
1637 |
|
if (pProgress) { |
1638 |
|
// divide local progress into subprogress |
1639 |
|
progress_t subprogress; |
1640 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1641 |
|
// do the actual work |
1642 |
|
pList->LoadSubChunksRecursively(&subprogress); |
1643 |
|
} else |
1644 |
|
pList->LoadSubChunksRecursively(NULL); |
1645 |
|
} |
1646 |
|
if (pProgress) |
1647 |
|
__notify_progress(pProgress, 1.0); // notify done |
1648 |
|
} |
1649 |
|
|
1650 |
|
/** @brief Write list chunk persistently e.g. to disk. |
1651 |
|
* |
1652 |
|
* Stores the list chunk persistently to its actual "physical" file. All |
1653 |
|
* subchunks (including sub list chunks) will be stored recursively as |
1654 |
|
* well. |
1655 |
|
* |
1656 |
|
* @param ullWritePos - position within the "physical" file where this |
1657 |
|
* list chunk should be written to |
1658 |
|
* @param ullCurrentDataOffset - offset of current (old) data within |
1659 |
|
* the file |
1660 |
|
* @param pProgress - optional: callback function for progress notification |
1661 |
|
* @returns new write position in the "physical" file, that is |
1662 |
|
* \a ullWritePos incremented by this list chunk's new size |
1663 |
|
* (including its header size of course) |
1664 |
|
*/ |
1665 |
|
file_offset_t List::WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress) { |
1666 |
|
const file_offset_t ullOriginalPos = ullWritePos; |
1667 |
|
ullWritePos += LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1668 |
|
|
1669 |
|
if (pFile->GetMode() != stream_mode_read_write) |
1670 |
|
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
1671 |
|
|
1672 |
|
// write all subchunks (including sub list chunks) recursively |
1673 |
|
if (pSubChunks) { |
1674 |
|
size_t i = 0; |
1675 |
|
const size_t n = pSubChunks->size(); |
1676 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) { |
1677 |
|
if (pProgress) { |
1678 |
|
// divide local progress into subprogress for loading current Instrument |
1679 |
|
progress_t subprogress; |
1680 |
|
__divide_progress(pProgress, &subprogress, n, i); |
1681 |
|
// do the actual work |
1682 |
|
ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress); |
1683 |
|
} else |
1684 |
|
ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, NULL); |
1685 |
|
} |
1686 |
|
} |
1687 |
|
|
1688 |
|
// update this list chunk's header |
1689 |
|
ullCurrentChunkSize = ullNewChunkSize = ullWritePos - ullOriginalPos - LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1690 |
|
WriteHeader(ullOriginalPos); |
1691 |
|
|
1692 |
|
// offset of this list chunk in new written file may have changed |
1693 |
|
ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1694 |
|
|
1695 |
|
if (pProgress) |
1696 |
|
__notify_progress(pProgress, 1.0); // notify done |
1697 |
|
|
1698 |
|
return ullWritePos; |
1699 |
|
} |
1700 |
|
|
1701 |
|
void List::__resetPos() { |
1702 |
|
Chunk::__resetPos(); |
1703 |
|
if (pSubChunks) { |
1704 |
|
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter) { |
1705 |
|
(*iter)->__resetPos(); |
1706 |
|
} |
1707 |
} |
} |
1708 |
} |
} |
1709 |
|
|
1710 |
/** |
/** |
1711 |
* Returns string representation of the lists's id |
* Returns string representation of the lists's id |
1712 |
*/ |
*/ |
1713 |
String List::GetListTypeString() { |
String List::GetListTypeString() const { |
1714 |
return convertToString(ListType); |
return convertToString(ListType); |
1715 |
} |
} |
1716 |
|
|
1719 |
// *************** File *************** |
// *************** File *************** |
1720 |
// * |
// * |
1721 |
|
|
1722 |
File::File(const String& path) : List() { |
/** @brief Create new RIFF file. |
1723 |
#if DEBUG |
* |
1724 |
std::cout << "File::File("<<path<<")" << std::endl; |
* Use this constructor if you want to create a new RIFF file completely |
1725 |
#endif // DEBUG |
* "from scratch". Note: there must be no empty chunks or empty list |
1726 |
|
* chunks when trying to make the new RIFF file persistent with Save()! |
1727 |
|
* |
1728 |
|
* Note: by default, the RIFF file will be saved in native endian |
1729 |
|
* format; that is, as a RIFF file on little-endian machines and |
1730 |
|
* as a RIFX file on big-endian. To change this behaviour, call |
1731 |
|
* SetByteOrder() before calling Save(). |
1732 |
|
* |
1733 |
|
* @param FileType - four-byte identifier of the RIFF file type |
1734 |
|
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1735 |
|
*/ |
1736 |
|
File::File(uint32_t FileType) |
1737 |
|
: List(this), bIsNewFile(true), Layout(layout_standard), |
1738 |
|
FileOffsetPreference(offset_size_auto) |
1739 |
|
{ |
1740 |
|
io.isPerThread = false; |
1741 |
|
#if defined(WIN32) |
1742 |
|
io.hRead = io.hWrite = INVALID_HANDLE_VALUE; |
1743 |
|
#else |
1744 |
|
io.hRead = io.hWrite = 0; |
1745 |
|
#endif |
1746 |
|
io.Mode = stream_mode_closed; |
1747 |
|
bEndianNative = true; |
1748 |
|
ListType = FileType; |
1749 |
|
FileOffsetSize = 4; |
1750 |
|
ullStartPos = RIFF_HEADER_SIZE(FileOffsetSize); |
1751 |
|
} |
1752 |
|
|
1753 |
|
/** @brief Load existing RIFF file. |
1754 |
|
* |
1755 |
|
* Loads an existing RIFF file with all its chunks. |
1756 |
|
* |
1757 |
|
* @param path - path and file name of the RIFF file to open |
1758 |
|
* @throws RIFF::Exception if error occurred while trying to load the |
1759 |
|
* given RIFF file |
1760 |
|
*/ |
1761 |
|
File::File(const String& path) |
1762 |
|
: List(this), Filename(path), bIsNewFile(false), Layout(layout_standard), |
1763 |
|
FileOffsetPreference(offset_size_auto) |
1764 |
|
{ |
1765 |
|
#if DEBUG_RIFF |
1766 |
|
std::cout << "File::File("<<path<<")" << std::endl; |
1767 |
|
#endif // DEBUG_RIFF |
1768 |
bEndianNative = true; |
bEndianNative = true; |
1769 |
|
FileOffsetSize = 4; |
1770 |
|
try { |
1771 |
|
__openExistingFile(path); |
1772 |
|
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1773 |
|
throw RIFF::Exception("Not a RIFF file"); |
1774 |
|
} |
1775 |
|
} |
1776 |
|
catch (...) { |
1777 |
|
Cleanup(); |
1778 |
|
throw; |
1779 |
|
} |
1780 |
|
} |
1781 |
|
|
1782 |
|
/** @brief Load existing RIFF-like file. |
1783 |
|
* |
1784 |
|
* Loads an existing file, which is not a "real" RIFF file, but similar to |
1785 |
|
* an ordinary RIFF file. |
1786 |
|
* |
1787 |
|
* A "real" RIFF file contains at top level a List chunk either with chunk |
1788 |
|
* ID "RIFF" or "RIFX". The simple constructor above expects this to be |
1789 |
|
* case, and if it finds the toplevel List chunk to have another chunk ID |
1790 |
|
* than one of those two expected ones, it would throw an Exception and |
1791 |
|
* would refuse to load the file accordingly. |
1792 |
|
* |
1793 |
|
* Since there are however a lot of file formats which use the same simple |
1794 |
|
* principles of the RIFF format, with another toplevel List chunk ID |
1795 |
|
* though, you can use this alternative constructor here to be able to load |
1796 |
|
* and handle those files in the same way as you would do with "real" RIFF |
1797 |
|
* files. |
1798 |
|
* |
1799 |
|
* @param path - path and file name of the RIFF-alike file to be opened |
1800 |
|
* @param FileType - expected toplevel List chunk ID (this is the very |
1801 |
|
* first chunk found in the file) |
1802 |
|
* @param Endian - whether the file uses little endian or big endian layout |
1803 |
|
* @param layout - general file structure type |
1804 |
|
* @param fileOffsetSize - (optional) preference how to deal with large files |
1805 |
|
* @throws RIFF::Exception if error occurred while trying to load the |
1806 |
|
* given RIFF-alike file |
1807 |
|
*/ |
1808 |
|
File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout, offset_size_t fileOffsetSize) |
1809 |
|
: List(this), Filename(path), bIsNewFile(false), Layout(layout), |
1810 |
|
FileOffsetPreference(fileOffsetSize) |
1811 |
|
{ |
1812 |
|
SetByteOrder(Endian); |
1813 |
|
if (fileOffsetSize < offset_size_auto || fileOffsetSize > offset_size_64bit) |
1814 |
|
throw Exception("Invalid RIFF::offset_size_t"); |
1815 |
|
FileOffsetSize = 4; |
1816 |
|
try { |
1817 |
|
__openExistingFile(path, &FileType); |
1818 |
|
} |
1819 |
|
catch (...) { |
1820 |
|
Cleanup(); |
1821 |
|
throw; |
1822 |
|
} |
1823 |
|
} |
1824 |
|
|
1825 |
|
/** |
1826 |
|
* Opens an already existing RIFF file or RIFF-alike file. This method |
1827 |
|
* shall only be called once (in a File class constructor). |
1828 |
|
* |
1829 |
|
* @param path - path and file name of the RIFF file or RIFF-alike file to |
1830 |
|
* be opened |
1831 |
|
* @param FileType - (optional) expected chunk ID of first chunk in file |
1832 |
|
* @throws RIFF::Exception if error occurred while trying to load the |
1833 |
|
* given RIFF file or RIFF-alike file |
1834 |
|
*/ |
1835 |
|
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
1836 |
|
io.isPerThread = false; |
1837 |
#if POSIX |
#if POSIX |
1838 |
hFile = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
io.hRead = io.hWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1839 |
if (hFile <= 0) { |
if (io.hRead == -1) { |
1840 |
hFile = 0; |
io.hRead = io.hWrite = 0; |
1841 |
|
String sError = strerror(errno); |
1842 |
|
throw RIFF::Exception("Can't open \"" + path + "\": " + sError); |
1843 |
|
} |
1844 |
|
#elif defined(WIN32) |
1845 |
|
io.hRead = io.hWrite = CreateFile( |
1846 |
|
path.c_str(), GENERIC_READ, |
1847 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1848 |
|
NULL, OPEN_EXISTING, |
1849 |
|
FILE_ATTRIBUTE_NORMAL | |
1850 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1851 |
|
); |
1852 |
|
if (io.hRead == INVALID_HANDLE_VALUE) { |
1853 |
|
io.hRead = io.hWrite = INVALID_HANDLE_VALUE; |
1854 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1855 |
} |
} |
1856 |
#else |
#else |
1857 |
hFile = fopen(path.c_str(), "rb"); |
io.hRead = io.hWrite = fopen(path.c_str(), "rb"); |
1858 |
if (!hFile) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!io.hRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1859 |
#endif // POSIX |
#endif // POSIX |
1860 |
ulStartPos = RIFF_HEADER_SIZE; |
io.Mode = stream_mode_read; |
1861 |
ReadHeader(0); |
|
1862 |
if (ChunkID != CHUNK_ID_RIFF) { |
// determine RIFF file offset size to be used (in RIFF chunk headers) |
1863 |
throw RIFF::Exception("Not a RIFF file"); |
// according to the current file offset preference |
1864 |
|
FileOffsetSize = FileOffsetSizeFor(GetCurrentFileSize()); |
1865 |
|
|
1866 |
|
switch (Layout) { |
1867 |
|
case layout_standard: // this is a normal RIFF file |
1868 |
|
ullStartPos = RIFF_HEADER_SIZE(FileOffsetSize); |
1869 |
|
ReadHeader(0); |
1870 |
|
if (FileType && ChunkID != *FileType) |
1871 |
|
throw RIFF::Exception("Invalid file container ID"); |
1872 |
|
break; |
1873 |
|
case layout_flat: // non-standard RIFF-alike file |
1874 |
|
ullStartPos = 0; |
1875 |
|
ullNewChunkSize = ullCurrentChunkSize = GetCurrentFileSize(); |
1876 |
|
if (FileType) { |
1877 |
|
uint32_t ckid; |
1878 |
|
if (Read(&ckid, 4, 1) != 4) { |
1879 |
|
throw RIFF::Exception("Invalid file header ID (premature end of header)"); |
1880 |
|
} else if (ckid != *FileType) { |
1881 |
|
String s = " (expected '" + convertToString(*FileType) + "' but got '" + convertToString(ckid) + "')"; |
1882 |
|
throw RIFF::Exception("Invalid file header ID" + s); |
1883 |
|
} |
1884 |
|
SetPos(0); // reset to first byte of file |
1885 |
|
} |
1886 |
|
LoadSubChunks(); |
1887 |
|
break; |
1888 |
} |
} |
1889 |
} |
} |
1890 |
|
|
1891 |
File::~File() { |
String File::GetFileName() const { |
1892 |
#if DEBUG |
return Filename; |
1893 |
std::cout << "File::~File()" << std::endl; |
} |
1894 |
#endif // DEBUG |
|
1895 |
#if POSIX |
void File::SetFileName(const String& path) { |
1896 |
if (hFile) close(hFile); |
Filename = path; |
1897 |
|
} |
1898 |
|
|
1899 |
|
/** |
1900 |
|
* This is an internal-only method which must not be used by any application |
1901 |
|
* and might change at any time. |
1902 |
|
* |
1903 |
|
* Resolves and returns a reference (memory location) of the RIFF file's |
1904 |
|
* internal and OS dependent file I/O handles which are intended to be used |
1905 |
|
* by the calling thread. |
1906 |
|
*/ |
1907 |
|
File::HandlePair& File::FileHandlePairUnsafeRef() { |
1908 |
|
if (io.byThread.empty()) return io; |
1909 |
|
const std::thread::id tid = std::this_thread::get_id(); |
1910 |
|
const auto it = io.byThread.find(tid); |
1911 |
|
return (it != io.byThread.end()) ? |
1912 |
|
it->second : |
1913 |
|
io.byThread[tid] = { |
1914 |
|
#if defined(WIN32) |
1915 |
|
.hRead = INVALID_HANDLE_VALUE, |
1916 |
|
.hWrite = INVALID_HANDLE_VALUE, |
1917 |
|
#else |
1918 |
|
.hRead = 0, |
1919 |
|
.hWrite = 0, |
1920 |
|
#endif |
1921 |
|
.Mode = stream_mode_closed |
1922 |
|
}; |
1923 |
|
} |
1924 |
|
|
1925 |
|
/** |
1926 |
|
* Returns the OS dependent file I/O read and write handles intended to be |
1927 |
|
* used by the calling thread. |
1928 |
|
* |
1929 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
1930 |
|
*/ |
1931 |
|
File::HandlePair File::FileHandlePair() const { |
1932 |
|
std::lock_guard<std::mutex> lock(io.mutex); |
1933 |
|
if (io.byThread.empty()) return io; |
1934 |
|
const std::thread::id tid = std::this_thread::get_id(); |
1935 |
|
const auto it = io.byThread.find(tid); |
1936 |
|
return (it != io.byThread.end()) ? |
1937 |
|
it->second : |
1938 |
|
io.byThread[tid] = { |
1939 |
|
#if defined(WIN32) |
1940 |
|
.hRead = INVALID_HANDLE_VALUE, |
1941 |
|
.hWrite = INVALID_HANDLE_VALUE, |
1942 |
|
#else |
1943 |
|
.hRead = 0, |
1944 |
|
.hWrite = 0, |
1945 |
|
#endif |
1946 |
|
.Mode = stream_mode_closed |
1947 |
|
}; |
1948 |
|
} |
1949 |
|
|
1950 |
|
/** |
1951 |
|
* Returns the OS dependent file I/O read handle intended to be used by the |
1952 |
|
* calling thread. |
1953 |
|
* |
1954 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
1955 |
|
*/ |
1956 |
|
File::Handle File::FileHandle() const { |
1957 |
|
return FileHandlePair().hRead; |
1958 |
|
} |
1959 |
|
|
1960 |
|
/** |
1961 |
|
* Returns the OS dependent file I/O write handle intended to be used by the |
1962 |
|
* calling thread. |
1963 |
|
* |
1964 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
1965 |
|
*/ |
1966 |
|
File::Handle File::FileWriteHandle() const { |
1967 |
|
return FileHandlePair().hWrite; |
1968 |
|
} |
1969 |
|
|
1970 |
|
/** |
1971 |
|
* Returns the file I/O mode currently being available for the calling |
1972 |
|
* thread for this RIFF file (either ro, rw or closed). |
1973 |
|
* |
1974 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
1975 |
|
*/ |
1976 |
|
stream_mode_t File::GetMode() const { |
1977 |
|
return FileHandlePair().Mode; |
1978 |
|
} |
1979 |
|
|
1980 |
|
layout_t File::GetLayout() const { |
1981 |
|
return Layout; |
1982 |
|
} |
1983 |
|
|
1984 |
|
/** @brief Change file access mode. |
1985 |
|
* |
1986 |
|
* Changes files access mode either to read-only mode or to read/write |
1987 |
|
* mode. |
1988 |
|
* |
1989 |
|
* @param NewMode - new file access mode |
1990 |
|
* @returns true if mode was changed, false if current mode already |
1991 |
|
* equals new mode |
1992 |
|
* @throws RIFF::Exception if file could not be opened in requested file |
1993 |
|
* access mode or if passed access mode is unknown |
1994 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
1995 |
|
*/ |
1996 |
|
bool File::SetMode(stream_mode_t NewMode) { |
1997 |
|
bool bResetPos = false; |
1998 |
|
bool res = SetModeInternal(NewMode, &bResetPos); |
1999 |
|
// resetting position must be handled outside above's call to avoid any |
2000 |
|
// potential dead lock, as SetModeInternal() acquires a lock and |
2001 |
|
// __resetPos() acquires a lock by itself (not a theoretical issue!) |
2002 |
|
if (bResetPos) |
2003 |
|
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
2004 |
|
return res; |
2005 |
|
} |
2006 |
|
|
2007 |
|
bool File::SetModeInternal(stream_mode_t NewMode, bool* pResetPos) { |
2008 |
|
std::lock_guard<std::mutex> lock(io.mutex); |
2009 |
|
HandlePair& io = FileHandlePairUnsafeRef(); |
2010 |
|
if (NewMode != io.Mode) { |
2011 |
|
switch (NewMode) { |
2012 |
|
case stream_mode_read: |
2013 |
|
if (_isValidHandle(io.hRead)) _close(io.hRead); |
2014 |
|
#if POSIX |
2015 |
|
io.hRead = io.hWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
2016 |
|
if (io.hRead == -1) { |
2017 |
|
io.hRead = io.hWrite = 0; |
2018 |
|
String sError = strerror(errno); |
2019 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode: " + sError); |
2020 |
|
} |
2021 |
|
#elif defined(WIN32) |
2022 |
|
io.hRead = io.hWrite = CreateFile( |
2023 |
|
Filename.c_str(), GENERIC_READ, |
2024 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
2025 |
|
NULL, OPEN_EXISTING, |
2026 |
|
FILE_ATTRIBUTE_NORMAL | |
2027 |
|
FILE_FLAG_RANDOM_ACCESS, |
2028 |
|
NULL |
2029 |
|
); |
2030 |
|
if (io.hRead == INVALID_HANDLE_VALUE) { |
2031 |
|
io.hRead = io.hWrite = INVALID_HANDLE_VALUE; |
2032 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
2033 |
|
} |
2034 |
|
#else |
2035 |
|
io.hRead = io.hWrite = fopen(Filename.c_str(), "rb"); |
2036 |
|
if (!io.hRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
2037 |
|
#endif |
2038 |
|
*pResetPos = true; |
2039 |
|
break; |
2040 |
|
case stream_mode_read_write: |
2041 |
|
if (_isValidHandle(io.hRead)) _close(io.hRead); |
2042 |
|
#if POSIX |
2043 |
|
io.hRead = io.hWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
2044 |
|
if (io.hRead == -1) { |
2045 |
|
io.hRead = io.hWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
2046 |
|
String sError = strerror(errno); |
2047 |
|
throw Exception("Could not open file \"" + Filename + "\" in read+write mode: " + sError); |
2048 |
|
} |
2049 |
|
#elif defined(WIN32) |
2050 |
|
io.hRead = io.hWrite = CreateFile( |
2051 |
|
Filename.c_str(), |
2052 |
|
GENERIC_READ | GENERIC_WRITE, |
2053 |
|
FILE_SHARE_READ, |
2054 |
|
NULL, OPEN_ALWAYS, |
2055 |
|
FILE_ATTRIBUTE_NORMAL | |
2056 |
|
FILE_FLAG_RANDOM_ACCESS, |
2057 |
|
NULL |
2058 |
|
); |
2059 |
|
if (io.hRead == INVALID_HANDLE_VALUE) { |
2060 |
|
io.hRead = io.hWrite = CreateFile( |
2061 |
|
Filename.c_str(), GENERIC_READ, |
2062 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
2063 |
|
NULL, OPEN_EXISTING, |
2064 |
|
FILE_ATTRIBUTE_NORMAL | |
2065 |
|
FILE_FLAG_RANDOM_ACCESS, |
2066 |
|
NULL |
2067 |
|
); |
2068 |
|
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
2069 |
|
} |
2070 |
|
#else |
2071 |
|
io.hRead = io.hWrite = fopen(Filename.c_str(), "r+b"); |
2072 |
|
if (!io.hRead) { |
2073 |
|
io.hRead = io.hWrite = fopen(Filename.c_str(), "rb"); |
2074 |
|
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
2075 |
|
} |
2076 |
|
#endif |
2077 |
|
*pResetPos = true; |
2078 |
|
break; |
2079 |
|
case stream_mode_closed: |
2080 |
|
if (_isValidHandle(io.hRead)) _close(io.hRead); |
2081 |
|
if (_isValidHandle(io.hWrite)) _close(io.hWrite); |
2082 |
|
#if POSIX |
2083 |
|
io.hRead = io.hWrite = 0; |
2084 |
|
#elif defined(WIN32) |
2085 |
|
io.hRead = io.hWrite = INVALID_HANDLE_VALUE; |
2086 |
|
#else |
2087 |
|
io.hRead = io.hWrite = NULL; |
2088 |
|
#endif |
2089 |
|
break; |
2090 |
|
default: |
2091 |
|
throw Exception("Unknown file access mode"); |
2092 |
|
} |
2093 |
|
io.Mode = NewMode; |
2094 |
|
return true; |
2095 |
|
} |
2096 |
|
return false; |
2097 |
|
} |
2098 |
|
|
2099 |
|
/** @brief Set the byte order to be used when saving. |
2100 |
|
* |
2101 |
|
* Set the byte order to be used in the file. A value of |
2102 |
|
* endian_little will create a RIFF file, endian_big a RIFX file |
2103 |
|
* and endian_native will create a RIFF file on little-endian |
2104 |
|
* machines and RIFX on big-endian machines. |
2105 |
|
* |
2106 |
|
* @param Endian - endianess to use when file is saved. |
2107 |
|
*/ |
2108 |
|
void File::SetByteOrder(endian_t Endian) { |
2109 |
|
#if WORDS_BIGENDIAN |
2110 |
|
bEndianNative = Endian != endian_little; |
2111 |
#else |
#else |
2112 |
if (hFile) fclose(hFile); |
bEndianNative = Endian != endian_big; |
2113 |
#endif // POSIX |
#endif |
2114 |
} |
} |
2115 |
|
|
2116 |
unsigned long File::GetFileSize() { |
/** @brief Save changes to same file. |
2117 |
|
* |
2118 |
|
* Make all changes of all chunks persistent by writing them to the |
2119 |
|
* actual (same) file. |
2120 |
|
* |
2121 |
|
* @param pProgress - optional: callback function for progress notification |
2122 |
|
* @throws RIFF::Exception if there is an empty chunk or empty list |
2123 |
|
* chunk or any kind of IO error occurred |
2124 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
2125 |
|
*/ |
2126 |
|
void File::Save(progress_t* pProgress) { |
2127 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
2128 |
|
if (Layout == layout_flat) |
2129 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
2130 |
|
|
2131 |
|
// make sure the RIFF tree is built (from the original file) |
2132 |
|
if (pProgress) { |
2133 |
|
// divide progress into subprogress |
2134 |
|
progress_t subprogress; |
2135 |
|
__divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress |
2136 |
|
// do the actual work |
2137 |
|
LoadSubChunksRecursively(&subprogress); |
2138 |
|
// notify subprogress done |
2139 |
|
__notify_progress(&subprogress, 1.f); |
2140 |
|
} else |
2141 |
|
LoadSubChunksRecursively(NULL); |
2142 |
|
|
2143 |
|
// reopen file in write mode |
2144 |
|
SetMode(stream_mode_read_write); |
2145 |
|
|
2146 |
|
// get the current file size as it is now still physically stored on disk |
2147 |
|
const file_offset_t workingFileSize = GetCurrentFileSize(); |
2148 |
|
|
2149 |
|
// get the overall file size required to save this file |
2150 |
|
const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference); |
2151 |
|
|
2152 |
|
// determine whether this file will yield in a large file (>=4GB) and |
2153 |
|
// the RIFF file offset size to be used accordingly for all chunks |
2154 |
|
FileOffsetSize = FileOffsetSizeFor(newFileSize); |
2155 |
|
|
2156 |
|
const HandlePair io = FileHandlePair(); |
2157 |
|
const Handle hRead = io.hRead; |
2158 |
|
const Handle hWrite = io.hWrite; |
2159 |
|
|
2160 |
|
// to be able to save the whole file without loading everything into |
2161 |
|
// RAM and without having to store the data in a temporary file, we |
2162 |
|
// enlarge the file with the overall positive file size change, |
2163 |
|
// then move current data towards the end of the file by the calculated |
2164 |
|
// positive file size difference and finally update / rewrite the file |
2165 |
|
// by copying the old data back to the right position at the beginning |
2166 |
|
// of the file |
2167 |
|
|
2168 |
|
// if there are positive size changes... |
2169 |
|
file_offset_t positiveSizeDiff = 0; |
2170 |
|
if (newFileSize > workingFileSize) { |
2171 |
|
positiveSizeDiff = newFileSize - workingFileSize; |
2172 |
|
|
2173 |
|
// divide progress into subprogress |
2174 |
|
progress_t subprogress; |
2175 |
|
if (pProgress) |
2176 |
|
__divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress |
2177 |
|
|
2178 |
|
// ... we enlarge this file first ... |
2179 |
|
ResizeFile(newFileSize); |
2180 |
|
|
2181 |
|
// ... and move current data by the same amount towards end of file. |
2182 |
|
int8_t* pCopyBuffer = new int8_t[4096]; |
2183 |
|
#if defined(WIN32) |
2184 |
|
DWORD iBytesMoved = 1; // we have to pass it via pointer to the Windows API, thus the correct size must be ensured |
2185 |
|
#else |
2186 |
|
ssize_t iBytesMoved = 1; |
2187 |
|
#endif |
2188 |
|
for (file_offset_t ullPos = workingFileSize, iNotif = 0; iBytesMoved > 0; ++iNotif) { |
2189 |
|
iBytesMoved = (ullPos < 4096) ? ullPos : 4096; |
2190 |
|
ullPos -= iBytesMoved; |
2191 |
|
#if POSIX |
2192 |
|
lseek(hRead, ullPos, SEEK_SET); |
2193 |
|
iBytesMoved = read(hRead, pCopyBuffer, iBytesMoved); |
2194 |
|
lseek(hWrite, ullPos + positiveSizeDiff, SEEK_SET); |
2195 |
|
iBytesMoved = write(hWrite, pCopyBuffer, iBytesMoved); |
2196 |
|
#elif defined(WIN32) |
2197 |
|
LARGE_INTEGER liFilePos; |
2198 |
|
liFilePos.QuadPart = ullPos; |
2199 |
|
SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
2200 |
|
ReadFile(hRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
2201 |
|
liFilePos.QuadPart = ullPos + positiveSizeDiff; |
2202 |
|
SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
2203 |
|
WriteFile(hWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
2204 |
|
#else |
2205 |
|
fseeko(hRead, ullPos, SEEK_SET); |
2206 |
|
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hRead); |
2207 |
|
fseeko(hWrite, ullPos + positiveSizeDiff, SEEK_SET); |
2208 |
|
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hWrite); |
2209 |
|
#endif |
2210 |
|
if (pProgress && !(iNotif % 8) && iBytesMoved > 0) |
2211 |
|
__notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize)); |
2212 |
|
} |
2213 |
|
delete[] pCopyBuffer; |
2214 |
|
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
2215 |
|
|
2216 |
|
if (pProgress) |
2217 |
|
__notify_progress(&subprogress, 1.f); // notify subprogress done |
2218 |
|
} |
2219 |
|
|
2220 |
|
// rebuild / rewrite complete RIFF tree ... |
2221 |
|
|
2222 |
|
// divide progress into subprogress |
2223 |
|
progress_t subprogress; |
2224 |
|
if (pProgress) |
2225 |
|
__divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress |
2226 |
|
// do the actual work |
2227 |
|
const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, pProgress ? &subprogress : NULL); |
2228 |
|
const file_offset_t finalActualSize = __GetFileSize(hWrite); |
2229 |
|
// notify subprogress done |
2230 |
|
if (pProgress) |
2231 |
|
__notify_progress(&subprogress, 1.f); |
2232 |
|
|
2233 |
|
// resize file to the final size |
2234 |
|
if (finalSize < finalActualSize) ResizeFile(finalSize); |
2235 |
|
|
2236 |
|
if (pProgress) |
2237 |
|
__notify_progress(pProgress, 1.0); // notify done |
2238 |
|
} |
2239 |
|
|
2240 |
|
/** @brief Save changes to another file. |
2241 |
|
* |
2242 |
|
* Make all changes of all chunks persistent by writing them to another |
2243 |
|
* file. <b>Caution:</b> this method is optimized for writing to |
2244 |
|
* <b>another</b> file, do not use it to save the changes to the same |
2245 |
|
* file! Use File::Save() in that case instead! Ignoring this might |
2246 |
|
* result in a corrupted file, especially in case chunks were resized! |
2247 |
|
* |
2248 |
|
* After calling this method, this File object will be associated with |
2249 |
|
* the new file (given by \a path) afterwards. |
2250 |
|
* |
2251 |
|
* @param path - path and file name where everything should be written to |
2252 |
|
* @param pProgress - optional: callback function for progress notification |
2253 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
2254 |
|
*/ |
2255 |
|
void File::Save(const String& path, progress_t* pProgress) { |
2256 |
|
//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 |
2257 |
|
|
2258 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
2259 |
|
if (Layout == layout_flat) |
2260 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
2261 |
|
|
2262 |
|
// make sure the RIFF tree is built (from the original file) |
2263 |
|
if (pProgress) { |
2264 |
|
// divide progress into subprogress |
2265 |
|
progress_t subprogress; |
2266 |
|
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress |
2267 |
|
// do the actual work |
2268 |
|
LoadSubChunksRecursively(&subprogress); |
2269 |
|
// notify subprogress done |
2270 |
|
__notify_progress(&subprogress, 1.f); |
2271 |
|
} else |
2272 |
|
LoadSubChunksRecursively(NULL); |
2273 |
|
|
2274 |
|
if (!bIsNewFile) SetMode(stream_mode_read); |
2275 |
|
|
2276 |
|
{ |
2277 |
|
std::lock_guard<std::mutex> lock(io.mutex); |
2278 |
|
HandlePair& io = FileHandlePairUnsafeRef(); |
2279 |
|
|
2280 |
|
// open the other (new) file for writing and truncate it to zero size |
2281 |
|
#if POSIX |
2282 |
|
io.hWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
2283 |
|
if (io.hWrite == -1) { |
2284 |
|
io.hWrite = io.hRead; |
2285 |
|
String sError = strerror(errno); |
2286 |
|
throw Exception("Could not open file \"" + path + "\" for writing: " + sError); |
2287 |
|
} |
2288 |
|
#elif defined(WIN32) |
2289 |
|
io.hWrite = CreateFile( |
2290 |
|
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
2291 |
|
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
2292 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
2293 |
|
); |
2294 |
|
if (io.hWrite == INVALID_HANDLE_VALUE) { |
2295 |
|
io.hWrite = io.hRead; |
2296 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
2297 |
|
} |
2298 |
|
#else |
2299 |
|
io.hWrite = fopen(path.c_str(), "w+b"); |
2300 |
|
if (!io.hWrite) { |
2301 |
|
io.hWrite = io.hRead; |
2302 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
2303 |
|
} |
2304 |
|
#endif // POSIX |
2305 |
|
io.Mode = stream_mode_read_write; |
2306 |
|
} |
2307 |
|
|
2308 |
|
// get the overall file size required to save this file |
2309 |
|
const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference); |
2310 |
|
|
2311 |
|
// determine whether this file will yield in a large file (>=4GB) and |
2312 |
|
// the RIFF file offset size to be used accordingly for all chunks |
2313 |
|
FileOffsetSize = FileOffsetSizeFor(newFileSize); |
2314 |
|
|
2315 |
|
// write complete RIFF tree to the other (new) file |
2316 |
|
file_offset_t ullTotalSize; |
2317 |
|
if (pProgress) { |
2318 |
|
// divide progress into subprogress |
2319 |
|
progress_t subprogress; |
2320 |
|
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress |
2321 |
|
// do the actual work |
2322 |
|
ullTotalSize = WriteChunk(0, 0, &subprogress); |
2323 |
|
// notify subprogress done |
2324 |
|
__notify_progress(&subprogress, 1.f); |
2325 |
|
} else |
2326 |
|
ullTotalSize = WriteChunk(0, 0, NULL); |
2327 |
|
|
2328 |
|
const file_offset_t ullActualSize = __GetFileSize(FileWriteHandle()); |
2329 |
|
|
2330 |
|
// resize file to the final size (if the file was originally larger) |
2331 |
|
if (ullActualSize > ullTotalSize) ResizeFile(ullTotalSize); |
2332 |
|
|
2333 |
|
{ |
2334 |
|
std::lock_guard<std::mutex> lock(io.mutex); |
2335 |
|
HandlePair& io = FileHandlePairUnsafeRef(); |
2336 |
|
|
2337 |
|
if (_isValidHandle(io.hWrite)) _close(io.hWrite); |
2338 |
|
io.hWrite = io.hRead; |
2339 |
|
|
2340 |
|
// associate new file with this File object from now on |
2341 |
|
Filename = path; |
2342 |
|
bIsNewFile = false; |
2343 |
|
io.Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
2344 |
|
} |
2345 |
|
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
2346 |
|
|
2347 |
|
if (pProgress) |
2348 |
|
__notify_progress(pProgress, 1.0); // notify done |
2349 |
|
} |
2350 |
|
|
2351 |
|
void File::ResizeFile(file_offset_t ullNewSize) { |
2352 |
|
const Handle hWrite = FileWriteHandle(); |
2353 |
#if POSIX |
#if POSIX |
2354 |
struct stat filestat; |
if (ftruncate(hWrite, ullNewSize) < 0) |
2355 |
fstat(hFile, &filestat); |
throw Exception("Could not resize file \"" + Filename + "\""); |
2356 |
long size = filestat.st_size; |
#elif defined(WIN32) |
2357 |
#else // standard C functions |
LARGE_INTEGER liFilePos; |
2358 |
long curpos = ftell(hFile); |
liFilePos.QuadPart = ullNewSize; |
2359 |
fseek(hFile, 0, SEEK_END); |
if ( |
2360 |
long size = ftell(hFile); |
!SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN) || |
2361 |
fseek(hFile, curpos, SEEK_SET); |
!SetEndOfFile(hWrite) |
2362 |
#endif // POSIX |
) throw Exception("Could not resize file \"" + Filename + "\""); |
2363 |
|
#else |
2364 |
|
# error Sorry, this version of libgig only supports POSIX and Windows systems yet. |
2365 |
|
# error Reason: portable implementation of RIFF::File::ResizeFile() is missing (yet)! |
2366 |
|
#endif |
2367 |
|
} |
2368 |
|
|
2369 |
|
File::~File() { |
2370 |
|
#if DEBUG_RIFF |
2371 |
|
std::cout << "File::~File()" << std::endl; |
2372 |
|
#endif // DEBUG_RIFF |
2373 |
|
Cleanup(); |
2374 |
|
} |
2375 |
|
|
2376 |
|
/** |
2377 |
|
* Returns @c true if this file has been created new from scratch and |
2378 |
|
* has not been stored to disk yet. |
2379 |
|
*/ |
2380 |
|
bool File::IsNew() const { |
2381 |
|
return bIsNewFile; |
2382 |
|
} |
2383 |
|
|
2384 |
|
void File::Cleanup() { |
2385 |
|
if (IsIOPerThread()) { |
2386 |
|
for (auto it = io.byThread.begin(); it != io.byThread.end(); ++it) { |
2387 |
|
_close(it->second.hRead); |
2388 |
|
} |
2389 |
|
} else { |
2390 |
|
_close(io.hRead); |
2391 |
|
} |
2392 |
|
DeleteChunkList(); |
2393 |
|
pFile = NULL; |
2394 |
|
} |
2395 |
|
|
2396 |
|
/** |
2397 |
|
* Returns the current size of this file (in bytes) as it is currently |
2398 |
|
* yet stored on disk. If this file does not yet exist on disk (i.e. when |
2399 |
|
* this RIFF File has just been created from scratch and Save() has not |
2400 |
|
* been called yet) then this method returns 0. |
2401 |
|
*/ |
2402 |
|
file_offset_t File::GetCurrentFileSize() const { |
2403 |
|
file_offset_t size = 0; |
2404 |
|
const Handle hRead = FileHandle(); |
2405 |
|
try { |
2406 |
|
size = __GetFileSize(hRead); |
2407 |
|
} catch (...) { |
2408 |
|
size = 0; |
2409 |
|
} |
2410 |
return size; |
return size; |
2411 |
} |
} |
2412 |
|
|
2413 |
|
/** |
2414 |
|
* Returns the required size (in bytes) for this RIFF File to be saved to |
2415 |
|
* disk. The precise size of the final file on disk depends on the RIFF |
2416 |
|
* file offset size actually used internally in all headers of the RIFF |
2417 |
|
* chunks. By default libgig handles the required file offset size |
2418 |
|
* automatically for you; that means it is using 32 bit offsets for files |
2419 |
|
* smaller than 4 GB and 64 bit offsets for files equal or larger than |
2420 |
|
* 4 GB. You may however also override this default behavior by passing the |
2421 |
|
* respective option to the RIFF File constructor to force one particular |
2422 |
|
* offset size. In the latter case this method will return the file size |
2423 |
|
* for the requested forced file offset size that will be used when calling |
2424 |
|
* Save() later on. |
2425 |
|
* |
2426 |
|
* You may also use the overridden method below to get the file size for |
2427 |
|
* an arbitrary other file offset size instead. |
2428 |
|
* |
2429 |
|
* @see offset_size_t |
2430 |
|
* @see GetFileOffsetSize() |
2431 |
|
*/ |
2432 |
|
file_offset_t File::GetRequiredFileSize() { |
2433 |
|
return GetRequiredFileSize(FileOffsetPreference); |
2434 |
|
} |
2435 |
|
|
2436 |
|
/** |
2437 |
|
* Returns the rquired size (in bytes) for this RIFF file to be saved to |
2438 |
|
* disk, assuming the passed @a fileOffsestSize would be used for the |
2439 |
|
* Save() operation. |
2440 |
|
* |
2441 |
|
* This overridden method essentialy behaves like the above method, with |
2442 |
|
* the difference that you must provide a specific RIFF @a fileOffsetSize |
2443 |
|
* for calculating the theoretical final file size. |
2444 |
|
* |
2445 |
|
* @see GetFileOffsetSize() |
2446 |
|
*/ |
2447 |
|
file_offset_t File::GetRequiredFileSize(offset_size_t fileOffsetSize) { |
2448 |
|
switch (fileOffsetSize) { |
2449 |
|
case offset_size_auto: { |
2450 |
|
file_offset_t fileSize = GetRequiredFileSize(offset_size_32bit); |
2451 |
|
if (fileSize >> 32) |
2452 |
|
return GetRequiredFileSize(offset_size_64bit); |
2453 |
|
else |
2454 |
|
return fileSize; |
2455 |
|
} |
2456 |
|
case offset_size_32bit: break; |
2457 |
|
case offset_size_64bit: break; |
2458 |
|
default: throw Exception("Internal error: Invalid RIFF::offset_size_t"); |
2459 |
|
} |
2460 |
|
return RequiredPhysicalSize(FileOffsetSize); |
2461 |
|
} |
2462 |
|
|
2463 |
|
int File::FileOffsetSizeFor(file_offset_t fileSize) const { |
2464 |
|
switch (FileOffsetPreference) { |
2465 |
|
case offset_size_auto: |
2466 |
|
return (fileSize >> 32) ? 8 : 4; |
2467 |
|
case offset_size_32bit: |
2468 |
|
return 4; |
2469 |
|
case offset_size_64bit: |
2470 |
|
return 8; |
2471 |
|
default: |
2472 |
|
throw Exception("Internal error: Invalid RIFF::offset_size_t"); |
2473 |
|
} |
2474 |
|
} |
2475 |
|
|
2476 |
|
/** |
2477 |
|
* Returns the current size (in bytes) of file offsets stored in the |
2478 |
|
* headers of all chunks of this file. |
2479 |
|
* |
2480 |
|
* Most RIFF files are using 32 bit file offsets internally, which limits |
2481 |
|
* them to a maximum file size of less than 4 GB though. In contrast to the |
2482 |
|
* common standard, this RIFF File class implementation supports handling of |
2483 |
|
* RIFF files equal or larger than 4 GB. In such cases 64 bit file offsets |
2484 |
|
* have to be used in all headers of all RIFF Chunks when being stored to a |
2485 |
|
* physical file. libgig by default automatically selects the correct file |
2486 |
|
* offset size for you. You may however also force one particular file |
2487 |
|
* offset size by supplying the respective option to the RIFF::File |
2488 |
|
* constructor. |
2489 |
|
* |
2490 |
|
* This method can be used to check which RIFF file offset size is currently |
2491 |
|
* being used for this RIFF File. |
2492 |
|
* |
2493 |
|
* @returns current RIFF file offset size used (in bytes) |
2494 |
|
* @see offset_size_t |
2495 |
|
*/ |
2496 |
|
int File::GetFileOffsetSize() const { |
2497 |
|
return FileOffsetSize; |
2498 |
|
} |
2499 |
|
|
2500 |
|
/** |
2501 |
|
* Returns the required size (in bytes) of file offsets stored in the |
2502 |
|
* headers of all chunks of this file if the current RIFF tree would be |
2503 |
|
* saved to disk by calling Save(). |
2504 |
|
* |
2505 |
|
* See GetFileOffsetSize() for mor details about RIFF file offsets. |
2506 |
|
* |
2507 |
|
* @returns RIFF file offset size required (in bytes) if being saved |
2508 |
|
* @see offset_size_t |
2509 |
|
*/ |
2510 |
|
int File::GetRequiredFileOffsetSize() { |
2511 |
|
return FileOffsetSizeFor(GetCurrentFileSize()); |
2512 |
|
} |
2513 |
|
|
2514 |
|
/** @brief Whether file streams are independent for each thread. |
2515 |
|
* |
2516 |
|
* All file I/O operations like reading from a RIFF chunk body (e.g. by |
2517 |
|
* calling Chunk::Read(), Chunk::ReadInt8()), writing to a RIFF chunk body |
2518 |
|
* (e.g. by calling Chunk::Write(), Chunk::WriteInt8()) or saving the |
2519 |
|
* current RIFF tree structure to some file (e.g. by calling Save()) |
2520 |
|
* operate on a file I/O stream state, i.e. there is a "current" file |
2521 |
|
* read/write position and reading/writing by a certain amount of bytes |
2522 |
|
* automatically advances that "current" file position. |
2523 |
|
* |
2524 |
|
* By default there is only one stream state for a RIFF::File object, which |
2525 |
|
* is not an issue as long as only one thread is using the RIFF::File |
2526 |
|
* object at a time (which might also be the case in a collaborative / |
2527 |
|
* coroutine multi-threaded scenario). |
2528 |
|
* |
2529 |
|
* If however a RIFF::File object is read/written @b simultaniously by |
2530 |
|
* multiple threads this can lead to undefined behaviour as the individual |
2531 |
|
* threads would concurrently alter the file stream position. For such a |
2532 |
|
* concurrent multithreaded file I/O scenario @c SetIOPerThread(true) might |
2533 |
|
* be enabled which causes each thread to automatically use its own file |
2534 |
|
* stream state. |
2535 |
|
* |
2536 |
|
* @returns true if each thread has its own file stream state |
2537 |
|
* (default: false) |
2538 |
|
* @see SetIOPerThread() |
2539 |
|
*/ |
2540 |
|
bool File::IsIOPerThread() const { |
2541 |
|
//NOTE: Not caring about atomicity here at all, for three reasons: |
2542 |
|
// 1. SetIOPerThread() is assumed to be called only once for the entire |
2543 |
|
// life time of a RIFF::File, usually very early at its lifetime, and |
2544 |
|
// hence a change to isPerThread should already safely be propagated |
2545 |
|
// before any other thread would actually read this boolean flag. |
2546 |
|
// 2. This method is called very frequently, and therefore any |
2547 |
|
// synchronization techique would hurt runtime efficiency. |
2548 |
|
// 3. Using even a mutex lock here might easily cause a deadlock due to |
2549 |
|
// other locks been taken in this .cpp file, i.e. at a higher call |
2550 |
|
// stack level (and this is the main reason why I removed it here). |
2551 |
|
return io.isPerThread; |
2552 |
|
} |
2553 |
|
|
2554 |
|
/** @brief Enable/disable file streams being independent for each thread. |
2555 |
|
* |
2556 |
|
* By enabling this feature (default off) each thread will automatically use |
2557 |
|
* its own file I/O stream state for allowing simultanious multi-threaded |
2558 |
|
* file read/write operations. |
2559 |
|
* |
2560 |
|
* @b NOTE: After having enabled this feature, the individual threads must |
2561 |
|
* at least once check GetState() and if their file I/O stream is yet closed |
2562 |
|
* they must call SetMode() (i.e. once) respectively to open their own file |
2563 |
|
* handles before being able to use any of the Read() or Write() methods. |
2564 |
|
* |
2565 |
|
* @param enable - @c true: one independent stream state per thread, |
2566 |
|
* @c false: only one stream in total shared by @b all threads |
2567 |
|
* @see IsIOPerThread() for more details about this feature |
2568 |
|
*/ |
2569 |
|
void File::SetIOPerThread(bool enable) { |
2570 |
|
std::lock_guard<std::mutex> lock(io.mutex); |
2571 |
|
if (!io.byThread.empty() == enable) return; |
2572 |
|
io.isPerThread = enable; |
2573 |
|
if (enable) { |
2574 |
|
const std::thread::id tid = std::this_thread::get_id(); |
2575 |
|
io.byThread[tid] = io; |
2576 |
|
} else { |
2577 |
|
// retain an arbitrary handle pair, close all other handle pairs |
2578 |
|
for (auto it = io.byThread.begin(); it != io.byThread.end(); ++it) { |
2579 |
|
if (it == io.byThread.begin()) { |
2580 |
|
io.hRead = it->second.hRead; |
2581 |
|
io.hWrite = it->second.hWrite; |
2582 |
|
} else { |
2583 |
|
_close(it->second.hRead); |
2584 |
|
_close(it->second.hWrite); |
2585 |
|
} |
2586 |
|
} |
2587 |
|
io.byThread.clear(); |
2588 |
|
} |
2589 |
|
} |
2590 |
|
|
2591 |
|
#if POSIX |
2592 |
|
file_offset_t File::__GetFileSize(int hFile) const { |
2593 |
|
struct stat filestat; |
2594 |
|
if (fstat(hFile, &filestat) == -1) |
2595 |
|
throw Exception("POSIX FS error: could not determine file size"); |
2596 |
|
return filestat.st_size; |
2597 |
|
} |
2598 |
|
#elif defined(WIN32) |
2599 |
|
file_offset_t File::__GetFileSize(HANDLE hFile) const { |
2600 |
|
LARGE_INTEGER size; |
2601 |
|
if (!GetFileSizeEx(hFile, &size)) |
2602 |
|
throw Exception("Windows FS error: could not determine file size"); |
2603 |
|
return size.QuadPart; |
2604 |
|
} |
2605 |
|
#else // standard C functions |
2606 |
|
file_offset_t File::__GetFileSize(FILE* hFile) const { |
2607 |
|
off_t curpos = ftello(hFile); |
2608 |
|
if (fseeko(hFile, 0, SEEK_END) == -1) |
2609 |
|
throw Exception("FS error: could not determine file size"); |
2610 |
|
off_t size = ftello(hFile); |
2611 |
|
fseeko(hFile, curpos, SEEK_SET); |
2612 |
|
return size; |
2613 |
|
} |
2614 |
|
#endif |
2615 |
|
|
2616 |
|
|
2617 |
// *************** Exception *************** |
// *************** Exception *************** |
2618 |
// * |
// * |
2619 |
|
|
2620 |
|
Exception::Exception() { |
2621 |
|
} |
2622 |
|
|
2623 |
|
Exception::Exception(String format, ...) { |
2624 |
|
va_list arg; |
2625 |
|
va_start(arg, format); |
2626 |
|
Message = assemble(format, arg); |
2627 |
|
va_end(arg); |
2628 |
|
} |
2629 |
|
|
2630 |
|
Exception::Exception(String format, va_list arg) { |
2631 |
|
Message = assemble(format, arg); |
2632 |
|
} |
2633 |
|
|
2634 |
void Exception::PrintMessage() { |
void Exception::PrintMessage() { |
2635 |
std::cout << "RIFF::Exception: " << Message << std::endl; |
std::cout << "RIFF::Exception: " << Message << std::endl; |
2636 |
} |
} |
2637 |
|
|
2638 |
|
String Exception::assemble(String format, va_list arg) { |
2639 |
|
char* buf = NULL; |
2640 |
|
vasprintf(&buf, format.c_str(), arg); |
2641 |
|
String s = buf; |
2642 |
|
free(buf); |
2643 |
|
return s; |
2644 |
|
} |
2645 |
|
|
2646 |
|
|
2647 |
|
// *************** functions *************** |
2648 |
|
// * |
2649 |
|
|
2650 |
|
/** |
2651 |
|
* Returns the name of this C++ library. This is usually "libgig" of |
2652 |
|
* course. This call is equivalent to DLS::libraryName() and |
2653 |
|
* gig::libraryName(). |
2654 |
|
*/ |
2655 |
|
String libraryName() { |
2656 |
|
return PACKAGE; |
2657 |
|
} |
2658 |
|
|
2659 |
|
/** |
2660 |
|
* Returns version of this C++ library. This call is equivalent to |
2661 |
|
* DLS::libraryVersion() and gig::libraryVersion(). |
2662 |
|
*/ |
2663 |
|
String libraryVersion() { |
2664 |
|
return VERSION; |
2665 |
|
} |
2666 |
|
|
2667 |
} // namespace RIFF |
} // namespace RIFF |
|
#endif |
|