2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2016 by Christian Schoenebeck * |
* Copyright (C) 2003-2021 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
52 |
return sPath; |
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 *************** |
// *************** progress_t *************** |
83 |
__range_max = 1.0f; |
__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(File* pFile) { |
Chunk::Chunk(File* pFile) { |
148 |
#if DEBUG |
#if DEBUG_RIFF |
149 |
std::cout << "Chunk::Chunk(File* pFile)" << std::endl; |
std::cout << "Chunk::Chunk(File* pFile)" << std::endl; |
150 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
151 |
ullPos = 0; |
chunkPos.ullPos = 0; |
152 |
pParent = NULL; |
pParent = NULL; |
153 |
pChunkData = NULL; |
pChunkData = NULL; |
154 |
ullCurrentChunkSize = 0; |
ullCurrentChunkSize = 0; |
159 |
} |
} |
160 |
|
|
161 |
Chunk::Chunk(File* pFile, file_offset_t StartPos, List* Parent) { |
Chunk::Chunk(File* pFile, file_offset_t StartPos, List* Parent) { |
162 |
#if DEBUG |
#if DEBUG_RIFF |
163 |
std::cout << "Chunk::Chunk(File*,file_offset_t,List*),StartPos=" << StartPos << std::endl; |
std::cout << "Chunk::Chunk(File*,file_offset_t,List*),StartPos=" << StartPos << std::endl; |
164 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
165 |
this->pFile = pFile; |
this->pFile = pFile; |
166 |
ullStartPos = StartPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
ullStartPos = StartPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
167 |
pParent = Parent; |
pParent = Parent; |
168 |
ullPos = 0; |
chunkPos.ullPos = 0; |
169 |
pChunkData = NULL; |
pChunkData = NULL; |
170 |
ullCurrentChunkSize = 0; |
ullCurrentChunkSize = 0; |
171 |
ullNewChunkSize = 0; |
ullNewChunkSize = 0; |
177 |
this->pFile = pFile; |
this->pFile = pFile; |
178 |
ullStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk |
ullStartPos = 0; // arbitrary usually, since it will be updated when we write the chunk |
179 |
this->pParent = pParent; |
this->pParent = pParent; |
180 |
ullPos = 0; |
chunkPos.ullPos = 0; |
181 |
pChunkData = NULL; |
pChunkData = NULL; |
182 |
ChunkID = uiChunkID; |
ChunkID = uiChunkID; |
183 |
ullChunkDataSize = 0; |
ullChunkDataSize = 0; |
190 |
} |
} |
191 |
|
|
192 |
void Chunk::ReadHeader(file_offset_t filePos) { |
void Chunk::ReadHeader(file_offset_t filePos) { |
193 |
#if DEBUG |
#if DEBUG_RIFF |
194 |
std::cout << "Chunk::Readheader(" << filePos << ") "; |
std::cout << "Chunk::Readheader(" << filePos << ") "; |
195 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
196 |
ChunkID = 0; |
ChunkID = 0; |
197 |
ullNewChunkSize = ullCurrentChunkSize = 0; |
ullNewChunkSize = ullCurrentChunkSize = 0; |
198 |
|
|
199 |
|
const File::Handle hRead = pFile->FileHandle(); |
200 |
|
|
201 |
#if POSIX |
#if POSIX |
202 |
if (lseek(pFile->hFileRead, filePos, SEEK_SET) != -1) { |
if (lseek(hRead, filePos, SEEK_SET) != -1) { |
203 |
read(pFile->hFileRead, &ChunkID, 4); |
read(hRead, &ChunkID, 4); |
204 |
read(pFile->hFileRead, &ullCurrentChunkSize, pFile->FileOffsetSize); |
read(hRead, &ullCurrentChunkSize, pFile->FileOffsetSize); |
205 |
#elif defined(WIN32) |
#elif defined(WIN32) |
206 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
207 |
liFilePos.QuadPart = filePos; |
liFilePos.QuadPart = filePos; |
208 |
if (SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
if (SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
209 |
DWORD dwBytesRead; |
DWORD dwBytesRead; |
210 |
ReadFile(pFile->hFileRead, &ChunkID, 4, &dwBytesRead, NULL); |
ReadFile(hRead, &ChunkID, 4, &dwBytesRead, NULL); |
211 |
ReadFile(pFile->hFileRead, &ullCurrentChunkSize, pFile->FileOffsetSize, &dwBytesRead, NULL); |
ReadFile(hRead, &ullCurrentChunkSize, pFile->FileOffsetSize, &dwBytesRead, NULL); |
212 |
#else |
#else |
213 |
if (!fseeko(pFile->hFileRead, filePos, SEEK_SET)) { |
if (!fseeko(hRead, filePos, SEEK_SET)) { |
214 |
fread(&ChunkID, 4, 1, pFile->hFileRead); |
fread(&ChunkID, 4, 1, hRead); |
215 |
fread(&ullCurrentChunkSize, pFile->FileOffsetSize, 1, pFile->hFileRead); |
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) { |
231 |
else |
else |
232 |
swapBytes_64(&ullCurrentChunkSize); |
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=" << ullCurrentChunkSize << " "; |
std::cout << "ckSize=" << ullCurrentChunkSize << " "; |
237 |
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; |
std::cout << "bEndianNative=" << pFile->bEndianNative << std::endl; |
238 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
239 |
ullNewChunkSize = ullCurrentChunkSize; |
ullNewChunkSize = ullCurrentChunkSize; |
240 |
} |
} |
241 |
} |
} |
258 |
swapBytes_64(&ullNewChunkSize); |
swapBytes_64(&ullNewChunkSize); |
259 |
} |
} |
260 |
|
|
261 |
|
const File::Handle hWrite = pFile->FileWriteHandle(); |
262 |
|
|
263 |
#if POSIX |
#if POSIX |
264 |
if (lseek(pFile->hFileWrite, filePos, SEEK_SET) != -1) { |
if (lseek(hWrite, filePos, SEEK_SET) != -1) { |
265 |
write(pFile->hFileWrite, &uiNewChunkID, 4); |
write(hWrite, &uiNewChunkID, 4); |
266 |
write(pFile->hFileWrite, &ullNewChunkSize, pFile->FileOffsetSize); |
write(hWrite, &ullNewChunkSize, pFile->FileOffsetSize); |
267 |
} |
} |
268 |
#elif defined(WIN32) |
#elif defined(WIN32) |
269 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
270 |
liFilePos.QuadPart = filePos; |
liFilePos.QuadPart = filePos; |
271 |
if (SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
if (SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
272 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
273 |
WriteFile(pFile->hFileWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
WriteFile(hWrite, &uiNewChunkID, 4, &dwBytesWritten, NULL); |
274 |
WriteFile(pFile->hFileWrite, &ullNewChunkSize, pFile->FileOffsetSize, &dwBytesWritten, NULL); |
WriteFile(hWrite, &ullNewChunkSize, pFile->FileOffsetSize, &dwBytesWritten, NULL); |
275 |
} |
} |
276 |
#else |
#else |
277 |
if (!fseeko(pFile->hFileWrite, filePos, SEEK_SET)) { |
if (!fseeko(hWrite, filePos, SEEK_SET)) { |
278 |
fwrite(&uiNewChunkID, 4, 1, pFile->hFileWrite); |
fwrite(&uiNewChunkID, 4, 1, hWrite); |
279 |
fwrite(&ullNewChunkSize, pFile->FileOffsetSize, 1, pFile->hFileWrite); |
fwrite(&ullNewChunkSize, pFile->FileOffsetSize, 1, hWrite); |
280 |
} |
} |
281 |
#endif // POSIX |
#endif // POSIX |
282 |
} |
} |
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 |
* |
* |
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 |
file_offset_t Chunk::SetPos(file_offset_t Where, stream_whence_t Whence) { |
file_offset_t Chunk::SetPos(file_offset_t Where, stream_whence_t Whence) { |
342 |
#if DEBUG |
#if DEBUG_RIFF |
343 |
std::cout << "Chunk::SetPos(file_offset_t,stream_whence_t)" << 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 |
ullPos += Where; |
pos += Where; |
350 |
break; |
break; |
351 |
case stream_end: |
case stream_end: |
352 |
ullPos = ullCurrentChunkSize - 1 - Where; |
pos = ullCurrentChunkSize - 1 - Where; |
353 |
break; |
break; |
354 |
case stream_backward: |
case stream_backward: |
355 |
ullPos -= Where; |
pos -= Where; |
356 |
break; |
break; |
357 |
case stream_start: default: |
case stream_start: default: |
358 |
ullPos = Where; |
pos = Where; |
359 |
break; |
break; |
360 |
} |
} |
361 |
if (ullPos > ullCurrentChunkSize) ullPos = ullCurrentChunkSize; |
if (pos > ullCurrentChunkSize) pos = ullCurrentChunkSize; |
362 |
return ullPos; |
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 |
file_offset_t Chunk::RemainingBytes() const { |
file_offset_t Chunk::RemainingBytes() const { |
377 |
#if DEBUG |
#if DEBUG_RIFF |
378 |
std::cout << "Chunk::Remainingbytes()=" << ullCurrentChunkSize - ullPos << std::endl; |
std::cout << "Chunk::Remainingbytes()=" << ullCurrentChunkSize - ullPos << std::endl; |
379 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
380 |
return (ullCurrentChunkSize > ullPos) ? ullCurrentChunkSize - ullPos : 0; |
const file_offset_t pos = GetPos(); |
381 |
|
return (ullCurrentChunkSize > pos) ? ullCurrentChunkSize - pos : 0; |
382 |
} |
} |
383 |
|
|
384 |
/** |
/** |
404 |
* - RIFF::stream_end_reached : |
* - RIFF::stream_end_reached : |
405 |
* already 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() const { |
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 (pFile->hFileRead == 0) return stream_closed; |
const File::Handle hRead = pFile->FileHandle(); |
416 |
#elif defined (WIN32) |
|
417 |
if (pFile->hFileRead == INVALID_HANDLE_VALUE) |
if (!_isValidHandle(hRead)) |
418 |
return stream_closed; |
return stream_closed; |
419 |
#else |
|
420 |
if (pFile->hFileRead == NULL) return stream_closed; |
const file_offset_t pos = GetPos(); |
421 |
#endif // POSIX |
if (pos < ullCurrentChunkSize) return stream_ready; |
|
if (ullPos < ullCurrentChunkSize) return stream_ready; |
|
422 |
else return stream_end_reached; |
else return stream_end_reached; |
423 |
} |
} |
424 |
|
|
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 occurred |
* of file reached or error occurred |
439 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
440 |
*/ |
*/ |
441 |
file_offset_t Chunk::Read(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
file_offset_t Chunk::Read(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
442 |
#if DEBUG |
#if DEBUG_RIFF |
443 |
std::cout << "Chunk::Read(void*,file_offset_t,file_offset_t)" << std::endl; |
std::cout << "Chunk::Read(void*,file_offset_t,file_offset_t)" << std::endl; |
444 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
445 |
//if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
//if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
446 |
if (ullPos >= ullCurrentChunkSize) return 0; |
const file_offset_t pos = GetPos(); |
447 |
if (ullPos + WordCount * WordSize >= ullCurrentChunkSize) WordCount = (ullCurrentChunkSize - ullPos) / WordSize; |
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(pFile->hFileRead, ullStartPos + ullPos, SEEK_SET) < 0) return 0; |
if (lseek(hRead, ullStartPos + pos, SEEK_SET) < 0) return 0; |
455 |
ssize_t readWords = read(pFile->hFileRead, pData, WordCount * WordSize); |
ssize_t readWords = read(hRead, pData, WordCount * WordSize); |
456 |
if (readWords < 1) { |
if (readWords < 1) { |
457 |
#if DEBUG |
#if DEBUG_RIFF |
458 |
std::cerr << "POSIX read() failed: " << strerror(errno) << std::endl << std::flush; |
std::cerr << "POSIX read() failed: " << strerror(errno) << std::endl << std::flush; |
459 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
460 |
return 0; |
return 0; |
461 |
} |
} |
462 |
readWords /= WordSize; |
readWords /= WordSize; |
463 |
#elif defined(WIN32) |
#elif defined(WIN32) |
464 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
465 |
liFilePos.QuadPart = ullStartPos + ullPos; |
liFilePos.QuadPart = ullStartPos + pos; |
466 |
if (!SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) |
if (!SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) |
467 |
return 0; |
return 0; |
468 |
DWORD readWords; |
DWORD readWords; |
469 |
ReadFile(pFile->hFileRead, pData, WordCount * WordSize, &readWords, NULL); //FIXME: does not work for reading buffers larger than 2GB (even though this should rarely be the case in practice) |
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 (fseeko(pFile->hFileRead, ullStartPos + ullPos, SEEK_SET)) return 0; |
if (fseeko(hRead, ullStartPos + pos, SEEK_SET)) return 0; |
474 |
file_offset_t readWords = fread(pData, WordSize, WordCount, pFile->hFileRead); |
file_offset_t readWords = fread(pData, WordSize, WordCount, hRead); |
475 |
#endif // POSIX |
#endif // POSIX |
476 |
if (!pFile->bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
477 |
switch (WordSize) { |
switch (WordSize) { |
512 |
* @throws RIFF::Exception if write operation would exceed current |
* @throws RIFF::Exception if write operation would exceed current |
513 |
* chunk size or any IO error occurred |
* chunk size or any IO error occurred |
514 |
* @see Resize() |
* @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) { |
file_offset_t Chunk::Write(void* pData, file_offset_t WordCount, file_offset_t WordSize) { |
518 |
if (pFile->Mode != stream_mode_read_write) |
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"); |
throw Exception("Cannot write data to chunk, file has to be opened in read+write mode first"); |
521 |
if (ullPos >= ullCurrentChunkSize || ullPos + WordCount * WordSize > ullCurrentChunkSize) |
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"); |
throw Exception("End of chunk reached while trying to write data"); |
524 |
if (!pFile->bEndianNative && WordSize != 1) { |
if (!pFile->bEndianNative && WordSize != 1) { |
525 |
switch (WordSize) { |
switch (WordSize) { |
542 |
} |
} |
543 |
} |
} |
544 |
#if POSIX |
#if POSIX |
545 |
if (lseek(pFile->hFileWrite, ullStartPos + ullPos, SEEK_SET) < 0) { |
if (lseek(io.hWrite, ullStartPos + pos, SEEK_SET) < 0) { |
546 |
throw Exception("Could not seek to position " + ToString(ullPos) + |
throw Exception("Could not seek to position " + ToString(pos) + |
547 |
" in chunk (" + ToString(ullStartPos + ullPos) + " in file)"); |
" in chunk (" + ToString(ullStartPos + pos) + " in file)"); |
548 |
} |
} |
549 |
ssize_t writtenWords = write(pFile->hFileWrite, pData, WordCount * WordSize); |
ssize_t writtenWords = write(io.hWrite, pData, WordCount * WordSize); |
550 |
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
if (writtenWords < 1) throw Exception("POSIX IO Error while trying to write chunk data"); |
551 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
552 |
#elif defined(WIN32) |
#elif defined(WIN32) |
553 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
554 |
liFilePos.QuadPart = ullStartPos + ullPos; |
liFilePos.QuadPart = ullStartPos + pos; |
555 |
if (!SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
if (!SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) { |
556 |
throw Exception("Could not seek to position " + ToString(ullPos) + |
throw Exception("Could not seek to position " + ToString(pos) + |
557 |
" in chunk (" + ToString(ullStartPos + ullPos) + " in file)"); |
" in chunk (" + ToString(ullStartPos + pos) + " in file)"); |
558 |
} |
} |
559 |
DWORD writtenWords; |
DWORD writtenWords; |
560 |
WriteFile(pFile->hFileWrite, pData, WordCount * WordSize, &writtenWords, NULL); //FIXME: does not work for writing buffers larger than 2GB (even though this should rarely be the case in practice) |
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"); |
if (writtenWords < 1) throw Exception("Windows IO Error while trying to write chunk data"); |
562 |
writtenWords /= WordSize; |
writtenWords /= WordSize; |
563 |
#else // standard C functions |
#else // standard C functions |
564 |
if (fseeko(pFile->hFileWrite, ullStartPos + ullPos, SEEK_SET)) { |
if (fseeko(io.hWrite, ullStartPos + pos, SEEK_SET)) { |
565 |
throw Exception("Could not seek to position " + ToString(ullPos) + |
throw Exception("Could not seek to position " + ToString(pos) + |
566 |
" in chunk (" + ToString(ullStartPos + ullPos) + " in file)"); |
" in chunk (" + ToString(ullStartPos + pos) + " in file)"); |
567 |
} |
} |
568 |
file_offset_t writtenWords = fwrite(pData, WordSize, WordCount, pFile->hFileWrite); |
file_offset_t writtenWords = fwrite(pData, WordSize, WordCount, io.hWrite); |
569 |
#endif // POSIX |
#endif // POSIX |
570 |
SetPos(writtenWords * WordSize, stream_curpos); |
SetPos(writtenWords * WordSize, stream_curpos); |
571 |
return writtenWords; |
return writtenWords; |
588 |
* @returns number of read integers |
* @returns number of read integers |
589 |
* @throws RIFF::Exception if an error occurred 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 |
file_offset_t Chunk::ReadInt8(int8_t* pData, file_offset_t 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*,file_offset_t)" << 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 |
|
|
610 |
* @returns number of written integers |
* @returns number of written integers |
611 |
* @throws RIFF::Exception if an IO error occurred |
* @throws RIFF::Exception if an IO error occurred |
612 |
* @see Resize() |
* @see Resize() |
613 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
614 |
*/ |
*/ |
615 |
file_offset_t Chunk::WriteInt8(int8_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteInt8(int8_t* pData, file_offset_t WordCount) { |
616 |
return Write(pData, WordCount, 1); |
return Write(pData, WordCount, 1); |
627 |
* @returns number of read integers |
* @returns number of read integers |
628 |
* @throws RIFF::Exception if an error occurred 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 |
file_offset_t Chunk::ReadUint8(uint8_t* pData, file_offset_t 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*,file_offset_t)" << 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 |
|
|
649 |
* @returns number of written integers |
* @returns number of written integers |
650 |
* @throws RIFF::Exception if an IO error occurred |
* @throws RIFF::Exception if an IO error occurred |
651 |
* @see Resize() |
* @see Resize() |
652 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
653 |
*/ |
*/ |
654 |
file_offset_t Chunk::WriteUint8(uint8_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteUint8(uint8_t* pData, file_offset_t WordCount) { |
655 |
return Write(pData, WordCount, 1); |
return Write(pData, WordCount, 1); |
666 |
* @returns number of read integers |
* @returns number of read integers |
667 |
* @throws RIFF::Exception if an error occurred 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 |
file_offset_t Chunk::ReadInt16(int16_t* pData, file_offset_t 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*,file_offset_t)" << 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 |
|
|
688 |
* @returns number of written integers |
* @returns number of written integers |
689 |
* @throws RIFF::Exception if an IO error occurred |
* @throws RIFF::Exception if an IO error occurred |
690 |
* @see Resize() |
* @see Resize() |
691 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
692 |
*/ |
*/ |
693 |
file_offset_t Chunk::WriteInt16(int16_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteInt16(int16_t* pData, file_offset_t WordCount) { |
694 |
return Write(pData, WordCount, 2); |
return Write(pData, WordCount, 2); |
705 |
* @returns number of read integers |
* @returns number of read integers |
706 |
* @throws RIFF::Exception if an error occurred 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 |
file_offset_t Chunk::ReadUint16(uint16_t* pData, file_offset_t 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*,file_offset_t)" << 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 |
|
|
727 |
* @returns number of written integers |
* @returns number of written integers |
728 |
* @throws RIFF::Exception if an IO error occurred |
* @throws RIFF::Exception if an IO error occurred |
729 |
* @see Resize() |
* @see Resize() |
730 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
731 |
*/ |
*/ |
732 |
file_offset_t Chunk::WriteUint16(uint16_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteUint16(uint16_t* pData, file_offset_t WordCount) { |
733 |
return Write(pData, WordCount, 2); |
return Write(pData, WordCount, 2); |
744 |
* @returns number of read integers |
* @returns number of read integers |
745 |
* @throws RIFF::Exception if an error occurred 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 |
file_offset_t Chunk::ReadInt32(int32_t* pData, file_offset_t 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*,file_offset_t)" << 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 |
|
|
766 |
* @returns number of written integers |
* @returns number of written integers |
767 |
* @throws RIFF::Exception if an IO error occurred |
* @throws RIFF::Exception if an IO error occurred |
768 |
* @see Resize() |
* @see Resize() |
769 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
770 |
*/ |
*/ |
771 |
file_offset_t Chunk::WriteInt32(int32_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteInt32(int32_t* pData, file_offset_t WordCount) { |
772 |
return Write(pData, WordCount, 4); |
return Write(pData, WordCount, 4); |
783 |
* @returns number of read integers |
* @returns number of read integers |
784 |
* @throws RIFF::Exception if an error occurred 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 |
file_offset_t Chunk::ReadUint32(uint32_t* pData, file_offset_t 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*,file_offset_t)" << 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 |
|
|
801 |
* @param size number of characters to read |
* @param size number of characters to read |
802 |
* @throws RIFF::Exception if an error occurred or less than |
* @throws RIFF::Exception if an error occurred or less than |
803 |
* \a size characters could be read! |
* \a size characters could be read! |
804 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
805 |
*/ |
*/ |
806 |
void Chunk::ReadString(String& s, int size) { |
void Chunk::ReadString(String& s, int size) { |
807 |
char* buf = new char[size]; |
char* buf = new char[size]; |
823 |
* @returns number of written integers |
* @returns number of written integers |
824 |
* @throws RIFF::Exception if an IO error occurred |
* @throws RIFF::Exception if an IO error occurred |
825 |
* @see Resize() |
* @see Resize() |
826 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
827 |
*/ |
*/ |
828 |
file_offset_t Chunk::WriteUint32(uint32_t* pData, file_offset_t WordCount) { |
file_offset_t Chunk::WriteUint32(uint32_t* pData, file_offset_t WordCount) { |
829 |
return Write(pData, WordCount, 4); |
return Write(pData, WordCount, 4); |
835 |
* |
* |
836 |
* @returns read integer word |
* @returns read integer word |
837 |
* @throws RIFF::Exception if an error occurred |
* @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; |
852 |
* |
* |
853 |
* @returns read integer word |
* @returns read integer word |
854 |
* @throws RIFF::Exception if an error occurred |
* @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; |
870 |
* |
* |
871 |
* @returns read integer word |
* @returns read integer word |
872 |
* @throws RIFF::Exception if an error occurred |
* @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; |
888 |
* |
* |
889 |
* @returns read integer word |
* @returns read integer word |
890 |
* @throws RIFF::Exception if an error occurred |
* @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; |
906 |
* |
* |
907 |
* @returns read integer word |
* @returns read integer word |
908 |
* @throws RIFF::Exception if an error occurred |
* @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; |
924 |
* |
* |
925 |
* @returns read integer word |
* @returns read integer word |
926 |
* @throws RIFF::Exception if an error occurred |
* @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; |
955 |
* @returns a pointer to the data in RAM on success, NULL otherwise |
* @returns a pointer to the data in RAM on success, NULL otherwise |
956 |
* @throws Exception if data buffer could not be enlarged |
* @throws Exception if data buffer could not be enlarged |
957 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
958 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
959 |
*/ |
*/ |
960 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
961 |
if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) { |
if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) { |
962 |
|
File::Handle hRead = pFile->FileHandle(); |
963 |
#if POSIX |
#if POSIX |
964 |
if (lseek(pFile->hFileRead, ullStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(hRead, ullStartPos, SEEK_SET) == -1) return NULL; |
965 |
#elif defined(WIN32) |
#elif defined(WIN32) |
966 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
967 |
liFilePos.QuadPart = ullStartPos; |
liFilePos.QuadPart = ullStartPos; |
968 |
if (!SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) return NULL; |
if (!SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN)) return NULL; |
969 |
#else |
#else |
970 |
if (fseeko(pFile->hFileRead, ullStartPos, SEEK_SET)) return NULL; |
if (fseeko(hRead, ullStartPos, SEEK_SET)) return NULL; |
971 |
#endif // POSIX |
#endif // POSIX |
972 |
file_offset_t ullBufferSize = (ullCurrentChunkSize > ullNewChunkSize) ? ullCurrentChunkSize : ullNewChunkSize; |
file_offset_t ullBufferSize = (ullCurrentChunkSize > ullNewChunkSize) ? ullCurrentChunkSize : ullNewChunkSize; |
973 |
pChunkData = new uint8_t[ullBufferSize]; |
pChunkData = new uint8_t[ullBufferSize]; |
974 |
if (!pChunkData) return NULL; |
if (!pChunkData) return NULL; |
975 |
memset(pChunkData, 0, ullBufferSize); |
memset(pChunkData, 0, ullBufferSize); |
976 |
#if POSIX |
#if POSIX |
977 |
file_offset_t readWords = read(pFile->hFileRead, pChunkData, GetSize()); |
file_offset_t readWords = read(hRead, pChunkData, GetSize()); |
978 |
#elif defined(WIN32) |
#elif defined(WIN32) |
979 |
DWORD readWords; |
DWORD readWords; |
980 |
ReadFile(pFile->hFileRead, pChunkData, GetSize(), &readWords, NULL); //FIXME: won't load chunks larger than 2GB ! |
ReadFile(hRead, pChunkData, GetSize(), &readWords, NULL); //FIXME: won't load chunks larger than 2GB ! |
981 |
#else |
#else |
982 |
file_offset_t readWords = fread(pChunkData, 1, GetSize(), pFile->hFileRead); |
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; |
990 |
uint8_t* pNewBuffer = new uint8_t[ullNewChunkSize]; |
uint8_t* pNewBuffer = new uint8_t[ullNewChunkSize]; |
991 |
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(ullNewChunkSize) + " bytes"); |
if (!pNewBuffer) throw Exception("Could not enlarge chunk data buffer to " + ToString(ullNewChunkSize) + " bytes"); |
992 |
memset(pNewBuffer, 0 , ullNewChunkSize); |
memset(pNewBuffer, 0 , ullNewChunkSize); |
993 |
memcpy(pNewBuffer, pChunkData, ullChunkDataSize); |
if (pChunkData) { |
994 |
delete[] pChunkData; |
memcpy(pNewBuffer, pChunkData, ullChunkDataSize); |
995 |
|
delete[] pChunkData; |
996 |
|
} |
997 |
pChunkData = pNewBuffer; |
pChunkData = pNewBuffer; |
998 |
ullChunkDataSize = ullNewChunkSize; |
ullChunkDataSize = ullNewChunkSize; |
999 |
} |
} |
1052 |
* @returns new write position in the "physical" file, that is |
* @returns new write position in the "physical" file, that is |
1053 |
* \a ullWritePos incremented by this chunk's new size |
* \a ullWritePos incremented by this chunk's new size |
1054 |
* (including its header size of course) |
* (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) { |
file_offset_t Chunk::WriteChunk(file_offset_t ullWritePos, file_offset_t ullCurrentDataOffset, progress_t* pProgress) { |
1058 |
const file_offset_t ullOriginalPos = ullWritePos; |
const file_offset_t ullOriginalPos = ullWritePos; |
1059 |
ullWritePos += CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
ullWritePos += CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1060 |
|
|
1061 |
if (pFile->Mode != stream_mode_read_write) |
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"); |
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 |
// if the whole chunk body was loaded into RAM |
1069 |
LoadChunkData(); |
LoadChunkData(); |
1070 |
// write chunk data from RAM persistently to the file |
// write chunk data from RAM persistently to the file |
1071 |
#if POSIX |
#if POSIX |
1072 |
lseek(pFile->hFileWrite, ullWritePos, SEEK_SET); |
lseek(io.hWrite, ullWritePos, SEEK_SET); |
1073 |
if (write(pFile->hFileWrite, pChunkData, ullNewChunkSize) != ullNewChunkSize) { |
if (write(io.hWrite, pChunkData, ullNewChunkSize) != ullNewChunkSize) { |
1074 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
1075 |
} |
} |
1076 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1077 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
1078 |
liFilePos.QuadPart = ullWritePos; |
liFilePos.QuadPart = ullWritePos; |
1079 |
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1080 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
1081 |
WriteFile(pFile->hFileWrite, pChunkData, ullNewChunkSize, &dwBytesWritten, NULL); //FIXME: won't save chunks larger than 2GB ! |
WriteFile(io.hWrite, pChunkData, ullNewChunkSize, &dwBytesWritten, NULL); //FIXME: won't save chunks larger than 2GB ! |
1082 |
if (dwBytesWritten != ullNewChunkSize) { |
if (dwBytesWritten != ullNewChunkSize) { |
1083 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
1084 |
} |
} |
1085 |
#else |
#else |
1086 |
fseeko(pFile->hFileWrite, ullWritePos, SEEK_SET); |
fseeko(io.hWrite, ullWritePos, SEEK_SET); |
1087 |
if (fwrite(pChunkData, 1, ullNewChunkSize, pFile->hFileWrite) != ullNewChunkSize) { |
if (fwrite(pChunkData, 1, ullNewChunkSize, io.hWrite) != ullNewChunkSize) { |
1088 |
throw Exception("Writing Chunk data (from RAM) failed"); |
throw Exception("Writing Chunk data (from RAM) failed"); |
1089 |
} |
} |
1090 |
#endif // POSIX |
#endif // POSIX |
1100 |
for (file_offset_t ullOffset = 0; ullToMove > 0 && iBytesMoved > 0; ullOffset += iBytesMoved, ullToMove -= iBytesMoved) { |
for (file_offset_t ullOffset = 0; ullToMove > 0 && iBytesMoved > 0; ullOffset += iBytesMoved, ullToMove -= iBytesMoved) { |
1101 |
iBytesMoved = (ullToMove < 4096) ? int(ullToMove) : 4096; |
iBytesMoved = (ullToMove < 4096) ? int(ullToMove) : 4096; |
1102 |
#if POSIX |
#if POSIX |
1103 |
lseek(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
lseek(io.hRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
1104 |
iBytesMoved = (int) read(pFile->hFileRead, pCopyBuffer, (size_t) iBytesMoved); |
iBytesMoved = (int) read(io.hRead, pCopyBuffer, (size_t) iBytesMoved); |
1105 |
lseek(pFile->hFileWrite, ullWritePos + ullOffset, SEEK_SET); |
lseek(io.hWrite, ullWritePos + ullOffset, SEEK_SET); |
1106 |
iBytesMoved = (int) write(pFile->hFileWrite, pCopyBuffer, (size_t) iBytesMoved); |
iBytesMoved = (int) write(io.hWrite, pCopyBuffer, (size_t) iBytesMoved); |
1107 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1108 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
1109 |
liFilePos.QuadPart = ullStartPos + ullCurrentDataOffset + ullOffset; |
liFilePos.QuadPart = ullStartPos + ullCurrentDataOffset + ullOffset; |
1110 |
SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
SetFilePointerEx(io.hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1111 |
ReadFile(pFile->hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
ReadFile(io.hRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1112 |
liFilePos.QuadPart = ullWritePos + ullOffset; |
liFilePos.QuadPart = ullWritePos + ullOffset; |
1113 |
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1114 |
WriteFile(pFile->hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
WriteFile(io.hWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
1115 |
#else |
#else |
1116 |
fseeko(pFile->hFileRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
fseeko(io.hRead, ullStartPos + ullCurrentDataOffset + ullOffset, SEEK_SET); |
1117 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, pFile->hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, io.hRead); |
1118 |
fseeko(pFile->hFileWrite, ullWritePos + ullOffset, SEEK_SET); |
fseeko(io.hWrite, ullWritePos + ullOffset, SEEK_SET); |
1119 |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, pFile->hFileWrite); |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, io.hWrite); |
1120 |
#endif |
#endif |
1121 |
} |
} |
1122 |
delete[] pCopyBuffer; |
delete[] pCopyBuffer; |
1127 |
ullCurrentChunkSize = ullNewChunkSize; |
ullCurrentChunkSize = ullNewChunkSize; |
1128 |
WriteHeader(ullOriginalPos); |
WriteHeader(ullOriginalPos); |
1129 |
|
|
1130 |
__notify_progress(pProgress, 1.0); // notify done |
if (pProgress) |
1131 |
|
__notify_progress(pProgress, 1.0); // notify done |
1132 |
|
|
1133 |
// update chunk's position pointers |
// update chunk's position pointers |
1134 |
ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
ullStartPos = ullOriginalPos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1135 |
ullPos = 0; |
Chunk::__resetPos(); |
1136 |
|
|
1137 |
// add pad byte if needed |
// add pad byte if needed |
1138 |
if ((ullStartPos + ullNewChunkSize) % 2 != 0) { |
if ((ullStartPos + ullNewChunkSize) % 2 != 0) { |
1139 |
const char cPadByte = 0; |
const char cPadByte = 0; |
1140 |
#if POSIX |
#if POSIX |
1141 |
lseek(pFile->hFileWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
lseek(io.hWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
1142 |
write(pFile->hFileWrite, &cPadByte, 1); |
write(io.hWrite, &cPadByte, 1); |
1143 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1144 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
1145 |
liFilePos.QuadPart = ullStartPos + ullNewChunkSize; |
liFilePos.QuadPart = ullStartPos + ullNewChunkSize; |
1146 |
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
SetFilePointerEx(io.hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1147 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
1148 |
WriteFile(pFile->hFileWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
WriteFile(io.hWrite, &cPadByte, 1, &dwBytesWritten, NULL); |
1149 |
#else |
#else |
1150 |
fseeko(pFile->hFileWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
fseeko(io.hWrite, ullStartPos + ullNewChunkSize, SEEK_SET); |
1151 |
fwrite(&cPadByte, 1, 1, pFile->hFileWrite); |
fwrite(&cPadByte, 1, 1, io.hWrite); |
1152 |
#endif |
#endif |
1153 |
return ullStartPos + ullNewChunkSize + 1; |
return ullStartPos + ullNewChunkSize + 1; |
1154 |
} |
} |
1157 |
} |
} |
1158 |
|
|
1159 |
void Chunk::__resetPos() { |
void Chunk::__resetPos() { |
1160 |
ullPos = 0; |
std::lock_guard<std::mutex> lock(chunkPos.mutex); |
1161 |
|
chunkPos.ullPos = 0; |
1162 |
|
chunkPos.byThread.clear(); |
1163 |
} |
} |
1164 |
|
|
1165 |
|
|
1168 |
// * |
// * |
1169 |
|
|
1170 |
List::List(File* pFile) : Chunk(pFile) { |
List::List(File* pFile) : Chunk(pFile) { |
1171 |
#if DEBUG |
#if DEBUG_RIFF |
1172 |
std::cout << "List::List(File* pFile)" << 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 |
List::List(File* pFile, file_offset_t StartPos, List* Parent) |
List::List(File* pFile, file_offset_t StartPos, List* Parent) |
1179 |
: Chunk(pFile, StartPos, Parent) { |
: Chunk(pFile, StartPos, Parent) { |
1180 |
#if DEBUG |
#if DEBUG_RIFF |
1181 |
std::cout << "List::List(File*,file_offset_t,List*)" << std::endl; |
std::cout << "List::List(File*,file_offset_t,List*)" << std::endl; |
1182 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1183 |
pSubChunks = NULL; |
pSubChunks = NULL; |
1184 |
pSubChunksMap = NULL; |
pSubChunksMap = NULL; |
1185 |
ReadHeader(StartPos); |
ReadHeader(StartPos); |
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(); |
DeleteChunkList(); |
1201 |
} |
} |
1202 |
|
|
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 |
} |
} |
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(); |
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; |
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++; |
1535 |
} |
} |
1536 |
|
|
1537 |
void List::ReadHeader(file_offset_t filePos) { |
void List::ReadHeader(file_offset_t filePos) { |
1538 |
#if DEBUG |
#if DEBUG_RIFF |
1539 |
std::cout << "List::Readheader(file_offset_t) "; |
std::cout << "List::Readheader(file_offset_t) "; |
1540 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1541 |
Chunk::ReadHeader(filePos); |
Chunk::ReadHeader(filePos); |
1542 |
if (ullCurrentChunkSize < 4) return; |
if (ullCurrentChunkSize < 4) return; |
1543 |
ullNewChunkSize = ullCurrentChunkSize -= 4; |
ullNewChunkSize = ullCurrentChunkSize -= 4; |
1544 |
|
|
1545 |
|
const File::Handle hRead = pFile->FileHandle(); |
1546 |
|
|
1547 |
#if POSIX |
#if POSIX |
1548 |
lseek(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
lseek(hRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1549 |
read(pFile->hFileRead, &ListType, 4); |
read(hRead, &ListType, 4); |
1550 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1551 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
1552 |
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1553 |
SetFilePointerEx(pFile->hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1554 |
DWORD dwBytesRead; |
DWORD dwBytesRead; |
1555 |
ReadFile(pFile->hFileRead, &ListType, 4, &dwBytesRead, NULL); |
ReadFile(hRead, &ListType, 4, &dwBytesRead, NULL); |
1556 |
#else |
#else |
1557 |
fseeko(pFile->hFileRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
fseeko(hRead, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1558 |
fread(&ListType, 4, 1, pFile->hFileRead); |
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 (!pFile->bEndianNative) { |
if (!pFile->bEndianNative) { |
1564 |
//swapBytes_32(&ListType); |
//swapBytes_32(&ListType); |
1565 |
} |
} |
1570 |
ullNewChunkSize += 4; |
ullNewChunkSize += 4; |
1571 |
Chunk::WriteHeader(filePos); |
Chunk::WriteHeader(filePos); |
1572 |
ullNewChunkSize -= 4; // just revert the +4 incrementation |
ullNewChunkSize -= 4; // just revert the +4 incrementation |
1573 |
|
|
1574 |
|
const File::Handle hWrite = pFile->FileWriteHandle(); |
1575 |
|
|
1576 |
#if POSIX |
#if POSIX |
1577 |
lseek(pFile->hFileWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
lseek(hWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1578 |
write(pFile->hFileWrite, &ListType, 4); |
write(hWrite, &ListType, 4); |
1579 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1580 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
1581 |
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
liFilePos.QuadPart = filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize); |
1582 |
SetFilePointerEx(pFile->hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
1583 |
DWORD dwBytesWritten; |
DWORD dwBytesWritten; |
1584 |
WriteFile(pFile->hFileWrite, &ListType, 4, &dwBytesWritten, NULL); |
WriteFile(hWrite, &ListType, 4, &dwBytesWritten, NULL); |
1585 |
#else |
#else |
1586 |
fseeko(pFile->hFileWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
fseeko(hWrite, filePos + CHUNK_HEADER_SIZE(pFile->FileOffsetSize), SEEK_SET); |
1587 |
fwrite(&ListType, 4, 1, pFile->hFileWrite); |
fwrite(&ListType, 4, 1, hWrite); |
1588 |
#endif // POSIX |
#endif // POSIX |
1589 |
} |
} |
1590 |
|
|
1591 |
void List::LoadSubChunks(progress_t* pProgress) { |
void List::LoadSubChunks(progress_t* pProgress) { |
1592 |
#if DEBUG |
#if DEBUG_RIFF |
1593 |
std::cout << "List::LoadSubChunks()"; |
std::cout << "List::LoadSubChunks()"; |
1594 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1595 |
if (!pSubChunks) { |
if (!pSubChunks) { |
1596 |
pSubChunks = new ChunkList(); |
pSubChunks = new ChunkList(); |
1597 |
pSubChunksMap = new ChunkMap(); |
pSubChunksMap = new ChunkMap(); |
1598 |
#if defined(WIN32) |
|
1599 |
if (pFile->hFileRead == INVALID_HANDLE_VALUE) return; |
const File::Handle hRead = pFile->FileHandle(); |
1600 |
#else |
if (!_isValidHandle(hRead)) return; |
1601 |
if (!pFile->hFileRead) return; |
|
1602 |
#endif |
const file_offset_t ullOriginalPos = GetPos(); |
|
file_offset_t ullOriginalPos = GetPos(); |
|
1603 |
SetPos(0); // jump to beginning of list chunk body |
SetPos(0); // jump to beginning of list chunk body |
1604 |
while (RemainingBytes() >= CHUNK_HEADER_SIZE(pFile->FileOffsetSize)) { |
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 |
|
if (Read(&ckid, 4, 1) != 4) |
1610 |
|
throw Exception("LoadSubChunks(): Failed reading RIFF chunk ID"); |
1611 |
|
#if DEBUG_RIFF |
1612 |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
std::cout << " ckid=" << convertToString(ckid) << std::endl; |
1613 |
#endif // DEBUG |
#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(pFile, ullStartPos + ullPos - 4, this); |
ck = new RIFF::List(pFile, ullStartPos + pos - 4, this); |
1617 |
SetPos(ck->GetSize() + LIST_HEADER_SIZE(pFile->FileOffsetSize) - 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(pFile, ullStartPos + ullPos - 4, this); |
ck = new RIFF::Chunk(pFile, ullStartPos + pos - 4, this); |
1621 |
SetPos(ck->GetSize() + CHUNK_HEADER_SIZE(pFile->FileOffsetSize) - 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); |
1626 |
} |
} |
1627 |
SetPos(ullOriginalPos); // restore position before this call |
SetPos(ullOriginalPos); // restore position before this call |
1628 |
} |
} |
1629 |
__notify_progress(pProgress, 1.0); // notify done |
if (pProgress) |
1630 |
|
__notify_progress(pProgress, 1.0); // notify done |
1631 |
} |
} |
1632 |
|
|
1633 |
void List::LoadSubChunksRecursively(progress_t* pProgress) { |
void List::LoadSubChunksRecursively(progress_t* pProgress) { |
1634 |
const int n = (int) CountSubLists(); |
const int n = (int) CountSubLists(); |
1635 |
int i = 0; |
int i = 0; |
1636 |
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) { |
for (List* pList = GetFirstSubList(); pList; pList = GetNextSubList(), ++i) { |
1637 |
// divide local progress into subprogress |
if (pProgress) { |
1638 |
progress_t subprogress; |
// divide local progress into subprogress |
1639 |
__divide_progress(pProgress, &subprogress, n, i); |
progress_t subprogress; |
1640 |
// do the actual work |
__divide_progress(pProgress, &subprogress, n, i); |
1641 |
pList->LoadSubChunksRecursively(&subprogress); |
// do the actual work |
1642 |
|
pList->LoadSubChunksRecursively(&subprogress); |
1643 |
|
} else |
1644 |
|
pList->LoadSubChunksRecursively(NULL); |
1645 |
} |
} |
1646 |
__notify_progress(pProgress, 1.0); // notify done |
if (pProgress) |
1647 |
|
__notify_progress(pProgress, 1.0); // notify done |
1648 |
} |
} |
1649 |
|
|
1650 |
/** @brief Write list chunk persistently e.g. to disk. |
/** @brief Write list chunk persistently e.g. to disk. |
1666 |
const file_offset_t ullOriginalPos = ullWritePos; |
const file_offset_t ullOriginalPos = ullWritePos; |
1667 |
ullWritePos += LIST_HEADER_SIZE(pFile->FileOffsetSize); |
ullWritePos += LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1668 |
|
|
1669 |
if (pFile->Mode != stream_mode_read_write) |
if (pFile->GetMode() != stream_mode_read_write) |
1670 |
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
throw Exception("Cannot write list chunk, file has to be opened in read+write mode"); |
1671 |
|
|
1672 |
// write all subchunks (including sub list chunks) recursively |
// write all subchunks (including sub list chunks) recursively |
1674 |
size_t i = 0; |
size_t i = 0; |
1675 |
const size_t n = pSubChunks->size(); |
const size_t n = pSubChunks->size(); |
1676 |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) { |
for (ChunkList::iterator iter = pSubChunks->begin(), end = pSubChunks->end(); iter != end; ++iter, ++i) { |
1677 |
// divide local progress into subprogress for loading current Instrument |
if (pProgress) { |
1678 |
progress_t subprogress; |
// divide local progress into subprogress for loading current Instrument |
1679 |
__divide_progress(pProgress, &subprogress, n, i); |
progress_t subprogress; |
1680 |
// do the actual work |
__divide_progress(pProgress, &subprogress, n, i); |
1681 |
ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress); |
// do the actual work |
1682 |
|
ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, &subprogress); |
1683 |
|
} else |
1684 |
|
ullWritePos = (*iter)->WriteChunk(ullWritePos, ullCurrentDataOffset, NULL); |
1685 |
} |
} |
1686 |
} |
} |
1687 |
|
|
1692 |
// offset of this list chunk in new written file may have changed |
// offset of this list chunk in new written file may have changed |
1693 |
ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize); |
ullStartPos = ullOriginalPos + LIST_HEADER_SIZE(pFile->FileOffsetSize); |
1694 |
|
|
1695 |
__notify_progress(pProgress, 1.0); // notify done |
if (pProgress) |
1696 |
|
__notify_progress(pProgress, 1.0); // notify done |
1697 |
|
|
1698 |
return ullWritePos; |
return ullWritePos; |
1699 |
} |
} |
1738 |
FileOffsetPreference(offset_size_auto) |
FileOffsetPreference(offset_size_auto) |
1739 |
{ |
{ |
1740 |
#if defined(WIN32) |
#if defined(WIN32) |
1741 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
io.hRead = io.hWrite = INVALID_HANDLE_VALUE; |
1742 |
#else |
#else |
1743 |
hFileRead = hFileWrite = 0; |
io.hRead = io.hWrite = 0; |
1744 |
#endif |
#endif |
1745 |
Mode = stream_mode_closed; |
io.Mode = stream_mode_closed; |
1746 |
bEndianNative = true; |
bEndianNative = true; |
1747 |
ListType = FileType; |
ListType = FileType; |
1748 |
FileOffsetSize = 4; |
FileOffsetSize = 4; |
1761 |
: List(this), Filename(path), bIsNewFile(false), Layout(layout_standard), |
: List(this), Filename(path), bIsNewFile(false), Layout(layout_standard), |
1762 |
FileOffsetPreference(offset_size_auto) |
FileOffsetPreference(offset_size_auto) |
1763 |
{ |
{ |
1764 |
#if DEBUG |
#if DEBUG_RIFF |
1765 |
std::cout << "File::File("<<path<<")" << std::endl; |
std::cout << "File::File("<<path<<")" << std::endl; |
1766 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
1767 |
bEndianNative = true; |
bEndianNative = true; |
1768 |
FileOffsetSize = 4; |
FileOffsetSize = 4; |
1769 |
try { |
try { |
1833 |
*/ |
*/ |
1834 |
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
1835 |
#if POSIX |
#if POSIX |
1836 |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
io.hRead = io.hWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1837 |
if (hFileRead == -1) { |
if (io.hRead == -1) { |
1838 |
hFileRead = hFileWrite = 0; |
io.hRead = io.hWrite = 0; |
1839 |
String sError = strerror(errno); |
String sError = strerror(errno); |
1840 |
throw RIFF::Exception("Can't open \"" + path + "\": " + sError); |
throw RIFF::Exception("Can't open \"" + path + "\": " + sError); |
1841 |
} |
} |
1842 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1843 |
hFileRead = hFileWrite = CreateFile( |
io.hRead = io.hWrite = CreateFile( |
1844 |
path.c_str(), GENERIC_READ, |
path.c_str(), GENERIC_READ, |
1845 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1846 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
1847 |
FILE_ATTRIBUTE_NORMAL | |
FILE_ATTRIBUTE_NORMAL | |
1848 |
FILE_FLAG_RANDOM_ACCESS, NULL |
FILE_FLAG_RANDOM_ACCESS, NULL |
1849 |
); |
); |
1850 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (io.hRead == INVALID_HANDLE_VALUE) { |
1851 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
io.hRead = io.hWrite = INVALID_HANDLE_VALUE; |
1852 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1853 |
} |
} |
1854 |
#else |
#else |
1855 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
io.hRead = io.hWrite = fopen(path.c_str(), "rb"); |
1856 |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
if (!io.hRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1857 |
#endif // POSIX |
#endif // POSIX |
1858 |
Mode = stream_mode_read; |
io.Mode = stream_mode_read; |
1859 |
|
|
1860 |
// determine RIFF file offset size to be used (in RIFF chunk headers) |
// determine RIFF file offset size to be used (in RIFF chunk headers) |
1861 |
// according to the current file offset preference |
// according to the current file offset preference |
1894 |
Filename = path; |
Filename = path; |
1895 |
} |
} |
1896 |
|
|
1897 |
|
/** |
1898 |
|
* This is an internal-only method which must not be used by any application |
1899 |
|
* and might change at any time. |
1900 |
|
* |
1901 |
|
* Resolves and returns a reference (memory location) of the RIFF file's |
1902 |
|
* internal and OS dependent file I/O handles which are intended to be used |
1903 |
|
* by the calling thread. |
1904 |
|
*/ |
1905 |
|
File::HandlePair& File::FileHandlePairUnsafeRef() { |
1906 |
|
if (io.byThread.empty()) return io; |
1907 |
|
const std::thread::id tid = std::this_thread::get_id(); |
1908 |
|
const auto it = io.byThread.find(tid); |
1909 |
|
return (it != io.byThread.end()) ? |
1910 |
|
it->second : |
1911 |
|
io.byThread[tid] = { |
1912 |
|
#if defined(WIN32) |
1913 |
|
.hRead = INVALID_HANDLE_VALUE, |
1914 |
|
.hWrite = INVALID_HANDLE_VALUE, |
1915 |
|
#else |
1916 |
|
.hRead = 0, |
1917 |
|
.hWrite = 0, |
1918 |
|
#endif |
1919 |
|
.Mode = stream_mode_closed |
1920 |
|
}; |
1921 |
|
} |
1922 |
|
|
1923 |
|
/** |
1924 |
|
* Returns the OS dependent file I/O read and write handles intended to be |
1925 |
|
* used by the calling thread. |
1926 |
|
* |
1927 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
1928 |
|
*/ |
1929 |
|
File::HandlePair File::FileHandlePair() const { |
1930 |
|
std::lock_guard<std::mutex> lock(io.mutex); |
1931 |
|
if (io.byThread.empty()) return io; |
1932 |
|
const std::thread::id tid = std::this_thread::get_id(); |
1933 |
|
const auto it = io.byThread.find(tid); |
1934 |
|
return (it != io.byThread.end()) ? |
1935 |
|
it->second : |
1936 |
|
io.byThread[tid] = { |
1937 |
|
#if defined(WIN32) |
1938 |
|
.hRead = INVALID_HANDLE_VALUE, |
1939 |
|
.hWrite = INVALID_HANDLE_VALUE, |
1940 |
|
#else |
1941 |
|
.hRead = 0, |
1942 |
|
.hWrite = 0, |
1943 |
|
#endif |
1944 |
|
.Mode = stream_mode_closed |
1945 |
|
}; |
1946 |
|
} |
1947 |
|
|
1948 |
|
/** |
1949 |
|
* Returns the OS dependent file I/O read handle intended to be used by the |
1950 |
|
* calling thread. |
1951 |
|
* |
1952 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
1953 |
|
*/ |
1954 |
|
File::Handle File::FileHandle() const { |
1955 |
|
return FileHandlePair().hRead; |
1956 |
|
} |
1957 |
|
|
1958 |
|
/** |
1959 |
|
* Returns the OS dependent file I/O write handle intended to be used by the |
1960 |
|
* calling thread. |
1961 |
|
* |
1962 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
1963 |
|
*/ |
1964 |
|
File::Handle File::FileWriteHandle() const { |
1965 |
|
return FileHandlePair().hWrite; |
1966 |
|
} |
1967 |
|
|
1968 |
|
/** |
1969 |
|
* Returns the file I/O mode currently being available for the calling |
1970 |
|
* thread for this RIFF file (either ro, rw or closed). |
1971 |
|
* |
1972 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
1973 |
|
*/ |
1974 |
stream_mode_t File::GetMode() const { |
stream_mode_t File::GetMode() const { |
1975 |
return Mode; |
return FileHandlePair().Mode; |
1976 |
} |
} |
1977 |
|
|
1978 |
layout_t File::GetLayout() const { |
layout_t File::GetLayout() const { |
1987 |
* @param NewMode - new file access mode |
* @param NewMode - new file access mode |
1988 |
* @returns true if mode was changed, false if current mode already |
* @returns true if mode was changed, false if current mode already |
1989 |
* equals new mode |
* equals new mode |
1990 |
* @throws RIFF::Exception if new file access mode is unknown |
* @throws RIFF::Exception if file could not be opened in requested file |
1991 |
|
* access mode or if passed access mode is unknown |
1992 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
1993 |
*/ |
*/ |
1994 |
bool File::SetMode(stream_mode_t NewMode) { |
bool File::SetMode(stream_mode_t NewMode) { |
1995 |
if (NewMode != Mode) { |
std::lock_guard<std::mutex> lock(io.mutex); |
1996 |
|
HandlePair& io = FileHandlePairUnsafeRef(); |
1997 |
|
if (NewMode != io.Mode) { |
1998 |
switch (NewMode) { |
switch (NewMode) { |
1999 |
case stream_mode_read: |
case stream_mode_read: |
2000 |
|
if (_isValidHandle(io.hRead)) _close(io.hRead); |
2001 |
#if POSIX |
#if POSIX |
2002 |
if (hFileRead) close(hFileRead); |
io.hRead = io.hWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
2003 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
if (io.hRead == -1) { |
2004 |
if (hFileRead == -1) { |
io.hRead = io.hWrite = 0; |
|
hFileRead = hFileWrite = 0; |
|
2005 |
String sError = strerror(errno); |
String sError = strerror(errno); |
2006 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode: " + sError); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode: " + sError); |
2007 |
} |
} |
2008 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2009 |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
io.hRead = io.hWrite = CreateFile( |
|
hFileRead = hFileWrite = CreateFile( |
|
2010 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
2011 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
2012 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
2014 |
FILE_FLAG_RANDOM_ACCESS, |
FILE_FLAG_RANDOM_ACCESS, |
2015 |
NULL |
NULL |
2016 |
); |
); |
2017 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (io.hRead == INVALID_HANDLE_VALUE) { |
2018 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
io.hRead = io.hWrite = INVALID_HANDLE_VALUE; |
2019 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
2020 |
} |
} |
2021 |
#else |
#else |
2022 |
if (hFileRead) fclose(hFileRead); |
io.hRead = io.hWrite = fopen(Filename.c_str(), "rb"); |
2023 |
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
if (!io.hRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
|
if (!hFileRead) throw Exception("Could not (re)open file \"" + Filename + "\" in read mode"); |
|
2024 |
#endif |
#endif |
2025 |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
2026 |
break; |
break; |
2027 |
case stream_mode_read_write: |
case stream_mode_read_write: |
2028 |
|
if (_isValidHandle(io.hRead)) _close(io.hRead); |
2029 |
#if POSIX |
#if POSIX |
2030 |
if (hFileRead) close(hFileRead); |
io.hRead = io.hWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
2031 |
hFileRead = hFileWrite = open(Filename.c_str(), O_RDWR | O_NONBLOCK); |
if (io.hRead == -1) { |
2032 |
if (hFileRead == -1) { |
io.hRead = io.hWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
|
hFileRead = hFileWrite = open(Filename.c_str(), O_RDONLY | O_NONBLOCK); |
|
2033 |
String sError = strerror(errno); |
String sError = strerror(errno); |
2034 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode: " + sError); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode: " + sError); |
2035 |
} |
} |
2036 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2037 |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
io.hRead = io.hWrite = CreateFile( |
|
hFileRead = hFileWrite = CreateFile( |
|
2038 |
Filename.c_str(), |
Filename.c_str(), |
2039 |
GENERIC_READ | GENERIC_WRITE, |
GENERIC_READ | GENERIC_WRITE, |
2040 |
FILE_SHARE_READ, |
FILE_SHARE_READ, |
2043 |
FILE_FLAG_RANDOM_ACCESS, |
FILE_FLAG_RANDOM_ACCESS, |
2044 |
NULL |
NULL |
2045 |
); |
); |
2046 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
if (io.hRead == INVALID_HANDLE_VALUE) { |
2047 |
hFileRead = hFileWrite = CreateFile( |
io.hRead = io.hWrite = CreateFile( |
2048 |
Filename.c_str(), GENERIC_READ, |
Filename.c_str(), GENERIC_READ, |
2049 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
2050 |
NULL, OPEN_EXISTING, |
NULL, OPEN_EXISTING, |
2055 |
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not (re)open file \"" + Filename + "\" in read+write mode"); |
2056 |
} |
} |
2057 |
#else |
#else |
2058 |
if (hFileRead) fclose(hFileRead); |
io.hRead = io.hWrite = fopen(Filename.c_str(), "r+b"); |
2059 |
hFileRead = hFileWrite = fopen(Filename.c_str(), "r+b"); |
if (!io.hRead) { |
2060 |
if (!hFileRead) { |
io.hRead = io.hWrite = fopen(Filename.c_str(), "rb"); |
|
hFileRead = hFileWrite = fopen(Filename.c_str(), "rb"); |
|
2061 |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
throw Exception("Could not open file \"" + Filename + "\" in read+write mode"); |
2062 |
} |
} |
2063 |
#endif |
#endif |
2064 |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
__resetPos(); // reset read/write position of ALL 'Chunk' objects |
2065 |
break; |
break; |
2066 |
case stream_mode_closed: |
case stream_mode_closed: |
2067 |
|
if (_isValidHandle(io.hRead)) _close(io.hRead); |
2068 |
|
if (_isValidHandle(io.hWrite)) _close(io.hWrite); |
2069 |
#if POSIX |
#if POSIX |
2070 |
if (hFileRead) close(hFileRead); |
io.hRead = io.hWrite = 0; |
|
if (hFileWrite) close(hFileWrite); |
|
2071 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2072 |
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
io.hRead = io.hWrite = INVALID_HANDLE_VALUE; |
|
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
|
2073 |
#else |
#else |
2074 |
if (hFileRead) fclose(hFileRead); |
io.hRead = io.hWrite = NULL; |
|
if (hFileWrite) fclose(hFileWrite); |
|
2075 |
#endif |
#endif |
|
hFileRead = hFileWrite = 0; |
|
2076 |
break; |
break; |
2077 |
default: |
default: |
2078 |
throw Exception("Unknown file access mode"); |
throw Exception("Unknown file access mode"); |
2079 |
} |
} |
2080 |
Mode = NewMode; |
io.Mode = NewMode; |
2081 |
return true; |
return true; |
2082 |
} |
} |
2083 |
return false; |
return false; |
2108 |
* @param pProgress - optional: callback function for progress notification |
* @param pProgress - optional: callback function for progress notification |
2109 |
* @throws RIFF::Exception if there is an empty chunk or empty list |
* @throws RIFF::Exception if there is an empty chunk or empty list |
2110 |
* chunk or any kind of IO error occurred |
* chunk or any kind of IO error occurred |
2111 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
2112 |
*/ |
*/ |
2113 |
void File::Save(progress_t* pProgress) { |
void File::Save(progress_t* pProgress) { |
2114 |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
2116 |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
2117 |
|
|
2118 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
2119 |
{ |
if (pProgress) { |
2120 |
// divide progress into subprogress |
// divide progress into subprogress |
2121 |
progress_t subprogress; |
progress_t subprogress; |
2122 |
__divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress |
__divide_progress(pProgress, &subprogress, 3.f, 0.f); // arbitrarily subdivided into 1/3 of total progress |
2124 |
LoadSubChunksRecursively(&subprogress); |
LoadSubChunksRecursively(&subprogress); |
2125 |
// notify subprogress done |
// notify subprogress done |
2126 |
__notify_progress(&subprogress, 1.f); |
__notify_progress(&subprogress, 1.f); |
2127 |
} |
} else |
2128 |
|
LoadSubChunksRecursively(NULL); |
2129 |
|
|
2130 |
// reopen file in write mode |
// reopen file in write mode |
2131 |
SetMode(stream_mode_read_write); |
SetMode(stream_mode_read_write); |
2140 |
// the RIFF file offset size to be used accordingly for all chunks |
// the RIFF file offset size to be used accordingly for all chunks |
2141 |
FileOffsetSize = FileOffsetSizeFor(newFileSize); |
FileOffsetSize = FileOffsetSizeFor(newFileSize); |
2142 |
|
|
2143 |
|
const HandlePair io = FileHandlePair(); |
2144 |
|
const Handle hRead = io.hRead; |
2145 |
|
const Handle hWrite = io.hWrite; |
2146 |
|
|
2147 |
// to be able to save the whole file without loading everything into |
// to be able to save the whole file without loading everything into |
2148 |
// RAM and without having to store the data in a temporary file, we |
// RAM and without having to store the data in a temporary file, we |
2149 |
// enlarge the file with the overall positive file size change, |
// enlarge the file with the overall positive file size change, |
2159 |
|
|
2160 |
// divide progress into subprogress |
// divide progress into subprogress |
2161 |
progress_t subprogress; |
progress_t subprogress; |
2162 |
__divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress |
if (pProgress) |
2163 |
|
__divide_progress(pProgress, &subprogress, 3.f, 1.f); // arbitrarily subdivided into 1/3 of total progress |
2164 |
|
|
2165 |
// ... we enlarge this file first ... |
// ... we enlarge this file first ... |
2166 |
ResizeFile(newFileSize); |
ResizeFile(newFileSize); |
2176 |
iBytesMoved = (ullPos < 4096) ? ullPos : 4096; |
iBytesMoved = (ullPos < 4096) ? ullPos : 4096; |
2177 |
ullPos -= iBytesMoved; |
ullPos -= iBytesMoved; |
2178 |
#if POSIX |
#if POSIX |
2179 |
lseek(hFileRead, ullPos, SEEK_SET); |
lseek(hRead, ullPos, SEEK_SET); |
2180 |
iBytesMoved = read(hFileRead, pCopyBuffer, iBytesMoved); |
iBytesMoved = read(hRead, pCopyBuffer, iBytesMoved); |
2181 |
lseek(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET); |
lseek(hWrite, ullPos + positiveSizeDiff, SEEK_SET); |
2182 |
iBytesMoved = write(hFileWrite, pCopyBuffer, iBytesMoved); |
iBytesMoved = write(hWrite, pCopyBuffer, iBytesMoved); |
2183 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2184 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
2185 |
liFilePos.QuadPart = ullPos; |
liFilePos.QuadPart = ullPos; |
2186 |
SetFilePointerEx(hFileRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
SetFilePointerEx(hRead, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
2187 |
ReadFile(hFileRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
ReadFile(hRead, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
2188 |
liFilePos.QuadPart = ullPos + positiveSizeDiff; |
liFilePos.QuadPart = ullPos + positiveSizeDiff; |
2189 |
SetFilePointerEx(hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN); |
2190 |
WriteFile(hFileWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
WriteFile(hWrite, pCopyBuffer, iBytesMoved, &iBytesMoved, NULL); |
2191 |
#else |
#else |
2192 |
fseeko(hFileRead, ullPos, SEEK_SET); |
fseeko(hRead, ullPos, SEEK_SET); |
2193 |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hFileRead); |
iBytesMoved = fread(pCopyBuffer, 1, iBytesMoved, hRead); |
2194 |
fseeko(hFileWrite, ullPos + positiveSizeDiff, SEEK_SET); |
fseeko(hWrite, ullPos + positiveSizeDiff, SEEK_SET); |
2195 |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hFileWrite); |
iBytesMoved = fwrite(pCopyBuffer, 1, iBytesMoved, hWrite); |
2196 |
#endif |
#endif |
2197 |
if (!(iNotif % 8) && iBytesMoved > 0) |
if (pProgress && !(iNotif % 8) && iBytesMoved > 0) |
2198 |
__notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize)); |
__notify_progress(&subprogress, float(workingFileSize - ullPos) / float(workingFileSize)); |
2199 |
} |
} |
2200 |
delete[] pCopyBuffer; |
delete[] pCopyBuffer; |
2201 |
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
if (iBytesMoved < 0) throw Exception("Could not modify file while trying to enlarge it"); |
2202 |
|
|
2203 |
__notify_progress(&subprogress, 1.f); // notify subprogress done |
if (pProgress) |
2204 |
|
__notify_progress(&subprogress, 1.f); // notify subprogress done |
2205 |
} |
} |
2206 |
|
|
2207 |
// rebuild / rewrite complete RIFF tree ... |
// rebuild / rewrite complete RIFF tree ... |
2208 |
|
|
2209 |
// divide progress into subprogress |
// divide progress into subprogress |
2210 |
progress_t subprogress; |
progress_t subprogress; |
2211 |
__divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress |
if (pProgress) |
2212 |
|
__divide_progress(pProgress, &subprogress, 3.f, 2.f); // arbitrarily subdivided into 1/3 of total progress |
2213 |
// do the actual work |
// do the actual work |
2214 |
const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, &subprogress); |
const file_offset_t finalSize = WriteChunk(0, positiveSizeDiff, pProgress ? &subprogress : NULL); |
2215 |
const file_offset_t finalActualSize = __GetFileSize(hFileWrite); |
const file_offset_t finalActualSize = __GetFileSize(hWrite); |
2216 |
// notify subprogress done |
// notify subprogress done |
2217 |
__notify_progress(&subprogress, 1.f); |
if (pProgress) |
2218 |
|
__notify_progress(&subprogress, 1.f); |
2219 |
|
|
2220 |
// resize file to the final size |
// resize file to the final size |
2221 |
if (finalSize < finalActualSize) ResizeFile(finalSize); |
if (finalSize < finalActualSize) ResizeFile(finalSize); |
2222 |
|
|
2223 |
__notify_progress(pProgress, 1.0); // notify done |
if (pProgress) |
2224 |
|
__notify_progress(pProgress, 1.0); // notify done |
2225 |
} |
} |
2226 |
|
|
2227 |
/** @brief Save changes to another file. |
/** @brief Save changes to another file. |
2237 |
* |
* |
2238 |
* @param path - path and file name where everything should be written to |
* @param path - path and file name where everything should be written to |
2239 |
* @param pProgress - optional: callback function for progress notification |
* @param pProgress - optional: callback function for progress notification |
2240 |
|
* @see File::IsIOPerThread() for multi-threaded streaming |
2241 |
*/ |
*/ |
2242 |
void File::Save(const String& path, progress_t* pProgress) { |
void File::Save(const String& path, progress_t* pProgress) { |
2243 |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
2247 |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
2248 |
|
|
2249 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
2250 |
{ |
if (pProgress) { |
2251 |
// divide progress into subprogress |
// divide progress into subprogress |
2252 |
progress_t subprogress; |
progress_t subprogress; |
2253 |
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress |
__divide_progress(pProgress, &subprogress, 2.f, 0.f); // arbitrarily subdivided into 1/2 of total progress |
2255 |
LoadSubChunksRecursively(&subprogress); |
LoadSubChunksRecursively(&subprogress); |
2256 |
// notify subprogress done |
// notify subprogress done |
2257 |
__notify_progress(&subprogress, 1.f); |
__notify_progress(&subprogress, 1.f); |
2258 |
} |
} else |
2259 |
|
LoadSubChunksRecursively(NULL); |
2260 |
|
|
2261 |
if (!bIsNewFile) SetMode(stream_mode_read); |
if (!bIsNewFile) SetMode(stream_mode_read); |
2262 |
// open the other (new) file for writing and truncate it to zero size |
|
2263 |
#if POSIX |
{ |
2264 |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
std::lock_guard<std::mutex> lock(io.mutex); |
2265 |
if (hFileWrite == -1) { |
HandlePair& io = FileHandlePairUnsafeRef(); |
2266 |
hFileWrite = hFileRead; |
|
2267 |
String sError = strerror(errno); |
// open the other (new) file for writing and truncate it to zero size |
2268 |
throw Exception("Could not open file \"" + path + "\" for writing: " + sError); |
#if POSIX |
2269 |
} |
io.hWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
2270 |
#elif defined(WIN32) |
if (io.hWrite == -1) { |
2271 |
hFileWrite = CreateFile( |
io.hWrite = io.hRead; |
2272 |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
String sError = strerror(errno); |
2273 |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
throw Exception("Could not open file \"" + path + "\" for writing: " + sError); |
2274 |
FILE_FLAG_RANDOM_ACCESS, NULL |
} |
2275 |
); |
#elif defined(WIN32) |
2276 |
if (hFileWrite == INVALID_HANDLE_VALUE) { |
io.hWrite = CreateFile( |
2277 |
hFileWrite = hFileRead; |
path.c_str(), GENERIC_WRITE, FILE_SHARE_READ, |
2278 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | |
2279 |
} |
FILE_FLAG_RANDOM_ACCESS, NULL |
2280 |
#else |
); |
2281 |
hFileWrite = fopen(path.c_str(), "w+b"); |
if (io.hWrite == INVALID_HANDLE_VALUE) { |
2282 |
if (!hFileWrite) { |
io.hWrite = io.hRead; |
2283 |
hFileWrite = hFileRead; |
throw Exception("Could not open file \"" + path + "\" for writing"); |
2284 |
throw Exception("Could not open file \"" + path + "\" for writing"); |
} |
2285 |
|
#else |
2286 |
|
io.hWrite = fopen(path.c_str(), "w+b"); |
2287 |
|
if (!io.hWrite) { |
2288 |
|
io.hWrite = io.hRead; |
2289 |
|
throw Exception("Could not open file \"" + path + "\" for writing"); |
2290 |
|
} |
2291 |
|
#endif // POSIX |
2292 |
|
io.Mode = stream_mode_read_write; |
2293 |
} |
} |
|
#endif // POSIX |
|
|
Mode = stream_mode_read_write; |
|
2294 |
|
|
2295 |
// get the overall file size required to save this file |
// get the overall file size required to save this file |
2296 |
const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference); |
const file_offset_t newFileSize = GetRequiredFileSize(FileOffsetPreference); |
2301 |
|
|
2302 |
// write complete RIFF tree to the other (new) file |
// write complete RIFF tree to the other (new) file |
2303 |
file_offset_t ullTotalSize; |
file_offset_t ullTotalSize; |
2304 |
{ |
if (pProgress) { |
2305 |
// divide progress into subprogress |
// divide progress into subprogress |
2306 |
progress_t subprogress; |
progress_t subprogress; |
2307 |
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress |
__divide_progress(pProgress, &subprogress, 2.f, 1.f); // arbitrarily subdivided into 1/2 of total progress |
2309 |
ullTotalSize = WriteChunk(0, 0, &subprogress); |
ullTotalSize = WriteChunk(0, 0, &subprogress); |
2310 |
// notify subprogress done |
// notify subprogress done |
2311 |
__notify_progress(&subprogress, 1.f); |
__notify_progress(&subprogress, 1.f); |
2312 |
} |
} else |
2313 |
file_offset_t ullActualSize = __GetFileSize(hFileWrite); |
ullTotalSize = WriteChunk(0, 0, NULL); |
2314 |
|
|
2315 |
|
const file_offset_t ullActualSize = __GetFileSize(FileWriteHandle()); |
2316 |
|
|
2317 |
// resize file to the final size (if the file was originally larger) |
// resize file to the final size (if the file was originally larger) |
2318 |
if (ullActualSize > ullTotalSize) ResizeFile(ullTotalSize); |
if (ullActualSize > ullTotalSize) ResizeFile(ullTotalSize); |
2319 |
|
|
2320 |
#if POSIX |
{ |
2321 |
if (hFileWrite) close(hFileWrite); |
std::lock_guard<std::mutex> lock(io.mutex); |
2322 |
#elif defined(WIN32) |
HandlePair& io = FileHandlePairUnsafeRef(); |
|
if (hFileWrite != INVALID_HANDLE_VALUE) CloseHandle(hFileWrite); |
|
|
#else |
|
|
if (hFileWrite) fclose(hFileWrite); |
|
|
#endif |
|
|
hFileWrite = hFileRead; |
|
2323 |
|
|
2324 |
// associate new file with this File object from now on |
if (_isValidHandle(io.hWrite)) _close(io.hWrite); |
2325 |
Filename = path; |
io.hWrite = io.hRead; |
2326 |
bIsNewFile = false; |
|
2327 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
// associate new file with this File object from now on |
2328 |
|
Filename = path; |
2329 |
|
bIsNewFile = false; |
2330 |
|
io.Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
2331 |
|
} |
2332 |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
2333 |
|
|
2334 |
__notify_progress(pProgress, 1.0); // notify done |
if (pProgress) |
2335 |
|
__notify_progress(pProgress, 1.0); // notify done |
2336 |
} |
} |
2337 |
|
|
2338 |
void File::ResizeFile(file_offset_t ullNewSize) { |
void File::ResizeFile(file_offset_t ullNewSize) { |
2339 |
|
const Handle hWrite = FileWriteHandle(); |
2340 |
#if POSIX |
#if POSIX |
2341 |
if (ftruncate(hFileWrite, ullNewSize) < 0) |
if (ftruncate(hWrite, ullNewSize) < 0) |
2342 |
throw Exception("Could not resize file \"" + Filename + "\""); |
throw Exception("Could not resize file \"" + Filename + "\""); |
2343 |
#elif defined(WIN32) |
#elif defined(WIN32) |
2344 |
LARGE_INTEGER liFilePos; |
LARGE_INTEGER liFilePos; |
2345 |
liFilePos.QuadPart = ullNewSize; |
liFilePos.QuadPart = ullNewSize; |
2346 |
if ( |
if ( |
2347 |
!SetFilePointerEx(hFileWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN) || |
!SetFilePointerEx(hWrite, liFilePos, NULL/*new pos pointer*/, FILE_BEGIN) || |
2348 |
!SetEndOfFile(hFileWrite) |
!SetEndOfFile(hWrite) |
2349 |
) throw Exception("Could not resize file \"" + Filename + "\""); |
) throw Exception("Could not resize file \"" + Filename + "\""); |
2350 |
#else |
#else |
2351 |
# error Sorry, this version of libgig only supports POSIX and Windows systems yet. |
# error Sorry, this version of libgig only supports POSIX and Windows systems yet. |
2354 |
} |
} |
2355 |
|
|
2356 |
File::~File() { |
File::~File() { |
2357 |
#if DEBUG |
#if DEBUG_RIFF |
2358 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
2359 |
#endif // DEBUG |
#endif // DEBUG_RIFF |
2360 |
Cleanup(); |
Cleanup(); |
2361 |
} |
} |
2362 |
|
|
2369 |
} |
} |
2370 |
|
|
2371 |
void File::Cleanup() { |
void File::Cleanup() { |
2372 |
#if POSIX |
const Handle hRead = FileHandle(); |
2373 |
if (hFileRead) close(hFileRead); |
if (_isValidHandle(hRead)) _close(hRead); |
|
#elif defined(WIN32) |
|
|
if (hFileRead != INVALID_HANDLE_VALUE) CloseHandle(hFileRead); |
|
|
#else |
|
|
if (hFileRead) fclose(hFileRead); |
|
|
#endif // POSIX |
|
2374 |
DeleteChunkList(); |
DeleteChunkList(); |
2375 |
pFile = NULL; |
pFile = NULL; |
2376 |
} |
} |
2383 |
*/ |
*/ |
2384 |
file_offset_t File::GetCurrentFileSize() const { |
file_offset_t File::GetCurrentFileSize() const { |
2385 |
file_offset_t size = 0; |
file_offset_t size = 0; |
2386 |
|
const Handle hRead = FileHandle(); |
2387 |
try { |
try { |
2388 |
size = __GetFileSize(hFileRead); |
size = __GetFileSize(hRead); |
2389 |
} catch (...) { |
} catch (...) { |
2390 |
size = 0; |
size = 0; |
2391 |
} |
} |
2493 |
return FileOffsetSizeFor(GetCurrentFileSize()); |
return FileOffsetSizeFor(GetCurrentFileSize()); |
2494 |
} |
} |
2495 |
|
|
2496 |
|
/** @brief Whether file streams are independent for each thread. |
2497 |
|
* |
2498 |
|
* All file I/O operations like reading from a RIFF chunk body (e.g. by |
2499 |
|
* calling Chunk::Read(), Chunk::ReadInt8()), writing to a RIFF chunk body |
2500 |
|
* (e.g. by calling Chunk::Write(), Chunk::WriteInt8()) or saving the |
2501 |
|
* current RIFF tree structure to some file (e.g. by calling Save()) |
2502 |
|
* operate on a file I/O stream state, i.e. there is a "current" file |
2503 |
|
* read/write position and reading/writing by a certain amount of bytes |
2504 |
|
* automatically advances that "current" file position. |
2505 |
|
* |
2506 |
|
* By default there is only one stream state for a RIFF::File object, which |
2507 |
|
* is not an issue as long as only one thread is using the RIFF::File |
2508 |
|
* object at a time (which might also be the case in a collaborative / |
2509 |
|
* coroutine multi-threaded scenario). |
2510 |
|
* |
2511 |
|
* If however a RIFF::File object is read/written @b simultaniously by |
2512 |
|
* multiple threads this can lead to undefined behaviour as the individual |
2513 |
|
* threads would concurrently alter the file stream position. For such a |
2514 |
|
* concurrent multithreaded file I/O scenario @c SetIOPerThread(true) might |
2515 |
|
* be enabled which causes each thread to automatically use its own file |
2516 |
|
* stream state. |
2517 |
|
* |
2518 |
|
* @returns true if each thread has its own file stream state |
2519 |
|
* (default: false) |
2520 |
|
* @see SetIOPerThread() |
2521 |
|
*/ |
2522 |
|
bool File::IsIOPerThread() const { |
2523 |
|
std::lock_guard<std::mutex> lock(io.mutex); |
2524 |
|
return !io.byThread.empty(); |
2525 |
|
} |
2526 |
|
|
2527 |
|
/** @brief Enable/disable file streams being independent for each thread. |
2528 |
|
* |
2529 |
|
* By enabling this feature (default off) each thread will automatically use |
2530 |
|
* its own file I/O stream state for allowing simultanious multi-threaded |
2531 |
|
* file read/write operations. |
2532 |
|
* |
2533 |
|
* @b NOTE: After having enabled this feature, the individual threads must |
2534 |
|
* at least once check GetState() and if their file I/O stream is yet closed |
2535 |
|
* they must call SetMode() (i.e. once) respectively to open their own file |
2536 |
|
* handles before being able to use any of the Read() or Write() methods. |
2537 |
|
* |
2538 |
|
* @param enable - @c true: one independent stream state per thread, |
2539 |
|
* @c false: only one stream in total shared by @b all threads |
2540 |
|
* @see IsIOPerThread() for more details about this feature |
2541 |
|
*/ |
2542 |
|
void File::SetIOPerThread(bool enable) { |
2543 |
|
std::lock_guard<std::mutex> lock(io.mutex); |
2544 |
|
if (!io.byThread.empty() == enable) return; |
2545 |
|
if (enable) { |
2546 |
|
const std::thread::id tid = std::this_thread::get_id(); |
2547 |
|
io.byThread[tid] = io; |
2548 |
|
} else { |
2549 |
|
// retain an arbitrary handle pair, close all other handle pairs |
2550 |
|
for (auto it = io.byThread.begin(); it != io.byThread.end(); ++it) { |
2551 |
|
if (it == io.byThread.begin()) { |
2552 |
|
io.hRead = it->second.hRead; |
2553 |
|
io.hWrite = it->second.hWrite; |
2554 |
|
} else { |
2555 |
|
_close(it->second.hRead); |
2556 |
|
_close(it->second.hWrite); |
2557 |
|
} |
2558 |
|
} |
2559 |
|
io.byThread.clear(); |
2560 |
|
} |
2561 |
|
} |
2562 |
|
|
2563 |
#if POSIX |
#if POSIX |
2564 |
file_offset_t File::__GetFileSize(int hFile) const { |
file_offset_t File::__GetFileSize(int hFile) const { |
2565 |
struct stat filestat; |
struct stat filestat; |
2589 |
// *************** Exception *************** |
// *************** Exception *************** |
2590 |
// * |
// * |
2591 |
|
|
2592 |
|
Exception::Exception() { |
2593 |
|
} |
2594 |
|
|
2595 |
|
Exception::Exception(String format, ...) { |
2596 |
|
va_list arg; |
2597 |
|
va_start(arg, format); |
2598 |
|
Message = assemble(format, arg); |
2599 |
|
va_end(arg); |
2600 |
|
} |
2601 |
|
|
2602 |
|
Exception::Exception(String format, va_list arg) { |
2603 |
|
Message = assemble(format, arg); |
2604 |
|
} |
2605 |
|
|
2606 |
void Exception::PrintMessage() { |
void Exception::PrintMessage() { |
2607 |
std::cout << "RIFF::Exception: " << Message << std::endl; |
std::cout << "RIFF::Exception: " << Message << std::endl; |
2608 |
} |
} |
2609 |
|
|
2610 |
|
String Exception::assemble(String format, va_list arg) { |
2611 |
|
char* buf = NULL; |
2612 |
|
vasprintf(&buf, format.c_str(), arg); |
2613 |
|
String s = buf; |
2614 |
|
free(buf); |
2615 |
|
return s; |
2616 |
|
} |
2617 |
|
|
2618 |
|
|
2619 |
// *************** functions *************** |
// *************** functions *************** |
2620 |
// * |
// * |