/[svn]/libgig/trunk/src/RIFF.cpp
ViewVC logotype

Annotation of /libgig/trunk/src/RIFF.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4092 - (hide annotations) (download)
Sun Feb 11 21:54:18 2024 UTC (2 months ago) by schoenebeck
File size: 113522 byte(s)
* src/RIFF.cpp: Fixed compilation error with some compilers, caused by
  using designated initializers, which is a C++20 feature.

* Bumped version (4.4.0.svn1),

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

  ViewVC Help
Powered by ViewVC