2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2013 by Christian Schoenebeck * |
* Copyright (C) 2003-2014 by Christian Schoenebeck * |
6 |
* <cuse@users.sourceforge.net> * |
* <cuse@users.sourceforge.net> * |
7 |
* * |
* * |
8 |
* This library is free software; you can redistribute it and/or modify * |
* This library is free software; you can redistribute it and/or modify * |
281 |
#if DEBUG |
#if DEBUG |
282 |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
std::cout << "Chunk::Read(void*,ulong,ulong)" << std::endl; |
283 |
#endif // DEBUG |
#endif // DEBUG |
284 |
if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
//if (ulStartPos == 0) return 0; // is only 0 if this is a new chunk, so nothing to read (yet) |
285 |
if (ulPos >= CurrentChunkSize) return 0; |
if (ulPos >= CurrentChunkSize) return 0; |
286 |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
if (ulPos + WordCount * WordSize >= CurrentChunkSize) WordCount = (CurrentChunkSize - ulPos) / WordSize; |
287 |
#if POSIX |
#if POSIX |
751 |
* @see ReleaseChunkData() |
* @see ReleaseChunkData() |
752 |
*/ |
*/ |
753 |
void* Chunk::LoadChunkData() { |
void* Chunk::LoadChunkData() { |
754 |
if (!pChunkData && pFile->Filename != "" && ulStartPos != 0) { |
if (!pChunkData && pFile->Filename != "" /*&& ulStartPos != 0*/) { |
755 |
#if POSIX |
#if POSIX |
756 |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
if (lseek(pFile->hFileRead, ulStartPos, SEEK_SET) == -1) return NULL; |
757 |
#elif defined(WIN32) |
#elif defined(WIN32) |
1419 |
* @param FileType - four-byte identifier of the RIFF file type |
* @param FileType - four-byte identifier of the RIFF file type |
1420 |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
* @see AddSubChunk(), AddSubList(), SetByteOrder() |
1421 |
*/ |
*/ |
1422 |
File::File(uint32_t FileType) : List(this) { |
File::File(uint32_t FileType) |
1423 |
|
: List(this), bIsNewFile(true), Layout(layout_standard) |
1424 |
|
{ |
1425 |
//HACK: see _GET_RESIZED_CHUNKS() comment |
//HACK: see _GET_RESIZED_CHUNKS() comment |
1426 |
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1427 |
#if defined(WIN32) |
#if defined(WIN32) |
1443 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occured while trying to load the |
1444 |
* given RIFF file |
* given RIFF file |
1445 |
*/ |
*/ |
1446 |
File::File(const String& path) : List(this), Filename(path) { |
File::File(const String& path) |
1447 |
|
: List(this), Filename(path), bIsNewFile(false), Layout(layout_standard) |
1448 |
|
{ |
1449 |
#if DEBUG |
#if DEBUG |
1450 |
std::cout << "File::File("<<path<<")" << std::endl; |
std::cout << "File::File("<<path<<")" << std::endl; |
1451 |
#endif // DEBUG |
#endif // DEBUG |
1452 |
|
bEndianNative = true; |
1453 |
try { |
try { |
1454 |
bEndianNative = true; |
__openExistingFile(path); |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
|
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
|
|
#if POSIX |
|
|
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
|
|
if (hFileRead <= 0) { |
|
|
hFileRead = hFileWrite = 0; |
|
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
|
|
} |
|
|
#elif defined(WIN32) |
|
|
hFileRead = hFileWrite = CreateFile( |
|
|
path.c_str(), GENERIC_READ, |
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
|
|
NULL, OPEN_EXISTING, |
|
|
FILE_ATTRIBUTE_NORMAL | |
|
|
FILE_FLAG_RANDOM_ACCESS, NULL |
|
|
); |
|
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
|
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
|
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
|
|
} |
|
|
#else |
|
|
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
|
|
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
|
|
#endif // POSIX |
|
|
Mode = stream_mode_read; |
|
|
ulStartPos = RIFF_HEADER_SIZE; |
|
|
ReadHeader(0); |
|
1455 |
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1456 |
throw RIFF::Exception("Not a RIFF file"); |
throw RIFF::Exception("Not a RIFF file"); |
1457 |
} |
} |
1462 |
} |
} |
1463 |
} |
} |
1464 |
|
|
1465 |
|
/** @brief Load existing RIFF-like file. |
1466 |
|
* |
1467 |
|
* Loads an existing file, which is not a "real" RIFF file, but similar to |
1468 |
|
* an ordinary RIFF file. |
1469 |
|
* |
1470 |
|
* A "real" RIFF file contains at top level a List chunk either with chunk |
1471 |
|
* ID "RIFF" or "RIFX". The simple constructor above expects this to be |
1472 |
|
* case, and if it finds the toplevel List chunk to have another chunk ID |
1473 |
|
* than one of those two expected ones, it would throw an Exception and |
1474 |
|
* would refuse to load the file accordingly. |
1475 |
|
* |
1476 |
|
* Since there are however a lot of file formats which use the same simple |
1477 |
|
* principles of the RIFF format, with another toplevel List chunk ID |
1478 |
|
* though, you can use this alternative constructor here to be able to load |
1479 |
|
* and handle those files in the same way as you would do with "real" RIFF |
1480 |
|
* files. |
1481 |
|
* |
1482 |
|
* @param path - path and file name of the RIFF-alike file to be opened |
1483 |
|
* @param FileType - expected toplevel List chunk ID (this is the very |
1484 |
|
* first chunk found in the file) |
1485 |
|
* @param Endian - whether the file uses little endian or big endian layout |
1486 |
|
* @param layout - general file structure type |
1487 |
|
* @throws RIFF::Exception if error occured while trying to load the |
1488 |
|
* given RIFF-alike file |
1489 |
|
*/ |
1490 |
|
File::File(const String& path, uint32_t FileType, endian_t Endian, layout_t layout) |
1491 |
|
: List(this), Filename(path), bIsNewFile(false), Layout(layout) |
1492 |
|
{ |
1493 |
|
SetByteOrder(Endian); |
1494 |
|
try { |
1495 |
|
__openExistingFile(path, &FileType); |
1496 |
|
} |
1497 |
|
catch (...) { |
1498 |
|
Cleanup(); |
1499 |
|
throw; |
1500 |
|
} |
1501 |
|
} |
1502 |
|
|
1503 |
|
/** |
1504 |
|
* Opens an already existing RIFF file or RIFF-alike file. This method |
1505 |
|
* shall only be called once (in a File class constructor). |
1506 |
|
* |
1507 |
|
* @param path - path and file name of the RIFF file or RIFF-alike file to |
1508 |
|
* be opened |
1509 |
|
* @param FileType - (optional) expected chunk ID of first chunk in file |
1510 |
|
* @throws RIFF::Exception if error occured while trying to load the |
1511 |
|
* given RIFF file or RIFF-alike file |
1512 |
|
*/ |
1513 |
|
void File::__openExistingFile(const String& path, uint32_t* FileType) { |
1514 |
|
//HACK: see _GET_RESIZED_CHUNKS() comment |
1515 |
|
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1516 |
|
#if POSIX |
1517 |
|
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1518 |
|
if (hFileRead <= 0) { |
1519 |
|
hFileRead = hFileWrite = 0; |
1520 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1521 |
|
} |
1522 |
|
#elif defined(WIN32) |
1523 |
|
hFileRead = hFileWrite = CreateFile( |
1524 |
|
path.c_str(), GENERIC_READ, |
1525 |
|
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1526 |
|
NULL, OPEN_EXISTING, |
1527 |
|
FILE_ATTRIBUTE_NORMAL | |
1528 |
|
FILE_FLAG_RANDOM_ACCESS, NULL |
1529 |
|
); |
1530 |
|
if (hFileRead == INVALID_HANDLE_VALUE) { |
1531 |
|
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1532 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1533 |
|
} |
1534 |
|
#else |
1535 |
|
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1536 |
|
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1537 |
|
#endif // POSIX |
1538 |
|
Mode = stream_mode_read; |
1539 |
|
switch (Layout) { |
1540 |
|
case layout_standard: // this is a normal RIFF file |
1541 |
|
ulStartPos = RIFF_HEADER_SIZE; |
1542 |
|
ReadHeader(0); |
1543 |
|
if (FileType && ChunkID != *FileType) |
1544 |
|
throw RIFF::Exception("Invalid file container ID"); |
1545 |
|
break; |
1546 |
|
case layout_flat: // non-standard RIFF-alike file |
1547 |
|
ulStartPos = 0; |
1548 |
|
NewChunkSize = CurrentChunkSize = GetFileSize(); |
1549 |
|
if (FileType) { |
1550 |
|
uint32_t ckid; |
1551 |
|
if (Read(&ckid, 4, 1) != 4) { |
1552 |
|
throw RIFF::Exception("Invalid file header ID (premature end of header)"); |
1553 |
|
} else if (ckid != *FileType) { |
1554 |
|
String s = " (expected '" + convertToString(*FileType) + "' but got '" + convertToString(ckid) + "')"; |
1555 |
|
throw RIFF::Exception("Invalid file header ID" + s); |
1556 |
|
} |
1557 |
|
SetPos(0); // reset to first byte of file |
1558 |
|
} |
1559 |
|
LoadSubChunks(); |
1560 |
|
break; |
1561 |
|
} |
1562 |
|
} |
1563 |
|
|
1564 |
String File::GetFileName() { |
String File::GetFileName() { |
1565 |
return Filename; |
return Filename; |
1566 |
} |
} |
1567 |
|
|
1568 |
|
void File::SetFileName(const String& path) { |
1569 |
|
Filename = path; |
1570 |
|
} |
1571 |
|
|
1572 |
stream_mode_t File::GetMode() { |
stream_mode_t File::GetMode() { |
1573 |
return Mode; |
return Mode; |
1574 |
} |
} |
1575 |
|
|
1576 |
|
layout_t File::GetLayout() const { |
1577 |
|
return Layout; |
1578 |
|
} |
1579 |
|
|
1580 |
/** @brief Change file access mode. |
/** @brief Change file access mode. |
1581 |
* |
* |
1582 |
* Changes files access mode either to read-only mode or to read/write |
* Changes files access mode either to read-only mode or to read/write |
1709 |
* chunk or any kind of IO error occured |
* chunk or any kind of IO error occured |
1710 |
*/ |
*/ |
1711 |
void File::Save() { |
void File::Save() { |
1712 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1713 |
|
if (Layout == layout_flat) |
1714 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1715 |
|
|
1716 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1717 |
LoadSubChunksRecursively(); |
LoadSubChunksRecursively(); |
1718 |
|
|
1804 |
void File::Save(const String& path) { |
void File::Save(const String& path) { |
1805 |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
//TODO: we should make a check here if somebody tries to write to the same file and automatically call the other Save() method in that case |
1806 |
|
|
1807 |
|
//TODO: implementation for the case where first chunk is not a global container (List chunk) is not implemented yet (i.e. Korg files) |
1808 |
|
if (Layout == layout_flat) |
1809 |
|
throw Exception("Saving a RIFF file with layout_flat is not implemented yet"); |
1810 |
|
|
1811 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1812 |
LoadSubChunksRecursively(); |
LoadSubChunksRecursively(); |
1813 |
|
|
1814 |
if (Filename.length() > 0) SetMode(stream_mode_read); |
if (!bIsNewFile) SetMode(stream_mode_read); |
1815 |
// open the other (new) file for writing and truncate it to zero size |
// open the other (new) file for writing and truncate it to zero size |
1816 |
#if POSIX |
#if POSIX |
1817 |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
hFileWrite = open(path.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP); |
1859 |
|
|
1860 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1861 |
Filename = path; |
Filename = path; |
1862 |
|
bIsNewFile = false; |
1863 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1864 |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
SetMode(stream_mode_read_write); // ... so SetMode() has to reopen the file handles. |
1865 |
} |
} |
1885 |
#endif // DEBUG |
#endif // DEBUG |
1886 |
Cleanup(); |
Cleanup(); |
1887 |
} |
} |
1888 |
|
|
1889 |
|
/** |
1890 |
|
* Returns @c true if this file has been created new from scratch and |
1891 |
|
* has not been stored to disk yet. |
1892 |
|
*/ |
1893 |
|
bool File::IsNew() const { |
1894 |
|
return bIsNewFile; |
1895 |
|
} |
1896 |
|
|
1897 |
void File::Cleanup() { |
void File::Cleanup() { |
1898 |
#if POSIX |
#if POSIX |