2 |
* * |
* * |
3 |
* libgig - C++ cross-platform Gigasampler format file access library * |
* libgig - C++ cross-platform Gigasampler format file access library * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003-2009 by Christian Schoenebeck * |
* Copyright (C) 2003-2013 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 * |
595 |
} |
} |
596 |
|
|
597 |
/** |
/** |
598 |
|
* Reads a null-padded string of size characters and copies it |
599 |
|
* into the string \a s. The position within the chunk will |
600 |
|
* automatically be incremented. |
601 |
|
* |
602 |
|
* @param s destination string |
603 |
|
* @param size number of characters to read |
604 |
|
* @throws RIFF::Exception if an error occured or less than |
605 |
|
* \a size characters could be read! |
606 |
|
*/ |
607 |
|
void Chunk::ReadString(String& s, int size) { |
608 |
|
char* buf = new char[size]; |
609 |
|
ReadSceptical(buf, 1, size); |
610 |
|
s.assign(buf, std::find(buf, buf + size, '\0')); |
611 |
|
delete[] buf; |
612 |
|
} |
613 |
|
|
614 |
|
/** |
615 |
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
* Writes \a WordCount number of 32 Bit unsigned integer words from the |
616 |
* buffer pointed by \a pData to the chunk's body, directly to the |
* buffer pointed by \a pData to the chunk's body, directly to the |
617 |
* actual "physical" file. The position within the chunk will |
* actual "physical" file. The position within the chunk will |
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) : List(this), bIsNewFile(true) { |
1423 |
//HACK: see _GET_RESIZED_CHUNKS() comment |
//HACK: see _GET_RESIZED_CHUNKS() comment |
1424 |
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1425 |
#if defined(WIN32) |
#if defined(WIN32) |
1441 |
* @throws RIFF::Exception if error occured while trying to load the |
* @throws RIFF::Exception if error occured while trying to load the |
1442 |
* given RIFF file |
* given RIFF file |
1443 |
*/ |
*/ |
1444 |
File::File(const String& path) : List(this), Filename(path) { |
File::File(const String& path) : List(this), Filename(path), bIsNewFile(false) { |
1445 |
#if DEBUG |
#if DEBUG |
1446 |
std::cout << "File::File("<<path<<")" << std::endl; |
std::cout << "File::File("<<path<<")" << std::endl; |
1447 |
#endif // DEBUG |
#endif // DEBUG |
1448 |
bEndianNative = true; |
try { |
1449 |
//HACK: see _GET_RESIZED_CHUNKS() comment |
bEndianNative = true; |
1450 |
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
//HACK: see _GET_RESIZED_CHUNKS() comment |
1451 |
#if POSIX |
ResizedChunks.push_back(reinterpret_cast<Chunk*>(new std::set<Chunk*>)); |
1452 |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
#if POSIX |
1453 |
if (hFileRead <= 0) { |
hFileRead = hFileWrite = open(path.c_str(), O_RDONLY | O_NONBLOCK); |
1454 |
hFileRead = hFileWrite = 0; |
if (hFileRead <= 0) { |
1455 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
hFileRead = hFileWrite = 0; |
1456 |
} |
throw RIFF::Exception("Can't open \"" + path + "\""); |
1457 |
#elif defined(WIN32) |
} |
1458 |
hFileRead = hFileWrite = CreateFile( |
#elif defined(WIN32) |
1459 |
path.c_str(), GENERIC_READ, |
hFileRead = hFileWrite = CreateFile( |
1460 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
path.c_str(), GENERIC_READ, |
1461 |
NULL, OPEN_EXISTING, |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
1462 |
FILE_ATTRIBUTE_NORMAL | |
NULL, OPEN_EXISTING, |
1463 |
FILE_FLAG_RANDOM_ACCESS, NULL |
FILE_ATTRIBUTE_NORMAL | |
1464 |
); |
FILE_FLAG_RANDOM_ACCESS, NULL |
1465 |
if (hFileRead == INVALID_HANDLE_VALUE) { |
); |
1466 |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
if (hFileRead == INVALID_HANDLE_VALUE) { |
1467 |
throw RIFF::Exception("Can't open \"" + path + "\""); |
hFileRead = hFileWrite = INVALID_HANDLE_VALUE; |
1468 |
|
throw RIFF::Exception("Can't open \"" + path + "\""); |
1469 |
|
} |
1470 |
|
#else |
1471 |
|
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
1472 |
|
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
1473 |
|
#endif // POSIX |
1474 |
|
Mode = stream_mode_read; |
1475 |
|
ulStartPos = RIFF_HEADER_SIZE; |
1476 |
|
ReadHeader(0); |
1477 |
|
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
1478 |
|
throw RIFF::Exception("Not a RIFF file"); |
1479 |
|
} |
1480 |
} |
} |
1481 |
#else |
catch (...) { |
1482 |
hFileRead = hFileWrite = fopen(path.c_str(), "rb"); |
Cleanup(); |
1483 |
if (!hFileRead) throw RIFF::Exception("Can't open \"" + path + "\""); |
throw; |
|
#endif // POSIX |
|
|
Mode = stream_mode_read; |
|
|
ulStartPos = RIFF_HEADER_SIZE; |
|
|
ReadHeader(0); |
|
|
if (ChunkID != CHUNK_ID_RIFF && ChunkID != CHUNK_ID_RIFX) { |
|
|
throw RIFF::Exception("Not a RIFF file"); |
|
1484 |
} |
} |
1485 |
} |
} |
1486 |
|
|
1487 |
String File::GetFileName() { |
String File::GetFileName() { |
1488 |
return Filename; |
return Filename; |
1489 |
} |
} |
1490 |
|
|
1491 |
|
void File::SetFileName(const String& path) { |
1492 |
|
Filename = path; |
1493 |
|
} |
1494 |
|
|
1495 |
stream_mode_t File::GetMode() { |
stream_mode_t File::GetMode() { |
1496 |
return Mode; |
return Mode; |
1722 |
// make sure the RIFF tree is built (from the original file) |
// make sure the RIFF tree is built (from the original file) |
1723 |
LoadSubChunksRecursively(); |
LoadSubChunksRecursively(); |
1724 |
|
|
1725 |
if (Filename.length() > 0) SetMode(stream_mode_read); |
if (!bIsNewFile) SetMode(stream_mode_read); |
1726 |
// 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 |
1727 |
#if POSIX |
#if POSIX |
1728 |
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); |
1770 |
|
|
1771 |
// associate new file with this File object from now on |
// associate new file with this File object from now on |
1772 |
Filename = path; |
Filename = path; |
1773 |
|
bIsNewFile = false; |
1774 |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
Mode = (stream_mode_t) -1; // Just set it to an undefined mode ... |
1775 |
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. |
1776 |
} |
} |
1794 |
#if DEBUG |
#if DEBUG |
1795 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
1796 |
#endif // DEBUG |
#endif // DEBUG |
1797 |
|
Cleanup(); |
1798 |
|
} |
1799 |
|
|
1800 |
|
/** |
1801 |
|
* Returns @c true if this file has been created new from scratch and |
1802 |
|
* has not been stored to disk yet. |
1803 |
|
*/ |
1804 |
|
bool File::IsNew() const { |
1805 |
|
return bIsNewFile; |
1806 |
|
} |
1807 |
|
|
1808 |
|
void File::Cleanup() { |
1809 |
#if POSIX |
#if POSIX |
1810 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1811 |
#elif defined(WIN32) |
#elif defined(WIN32) |