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 |
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) { |
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 |
|
|
1789 |
#if DEBUG |
#if DEBUG |
1790 |
std::cout << "File::~File()" << std::endl; |
std::cout << "File::~File()" << std::endl; |
1791 |
#endif // DEBUG |
#endif // DEBUG |
1792 |
|
Cleanup(); |
1793 |
|
} |
1794 |
|
|
1795 |
|
void File::Cleanup() { |
1796 |
#if POSIX |
#if POSIX |
1797 |
if (hFileRead) close(hFileRead); |
if (hFileRead) close(hFileRead); |
1798 |
#elif defined(WIN32) |
#elif defined(WIN32) |