dmtools/lib/dmbmx/ZiffInputStream.cpp

86 lines
2.5 KiB
C++

#include <cstdio>
#include <cstring>
#include <dmbmx/ZiffInputStream.hpp>
namespace dmtools::dmbmx {
ZiffInputStream::ZiffInputStream(io::Stream& stream, FourCCT expectedFormCkId)
: stream(stream), expectedFormCkId(expectedFormCkId) {
}
void ZiffInputStream::SetChunkCallback(ChunkCallback&& chunkCallback) {
this->callback = std::move(chunkCallback);
}
void ZiffInputStream::Process() {
//if(!NextChunkImpl(true))
// return;
while(!stream.Eof()) {
if(!NextChunkImpl(false))
return;
}
}
void ZiffInputStream::Rewind() {
stream.Seek(0, io::StreamSeekDirection::Beg);
}
bool ZiffInputStream::NextChunkImpl(bool isForm) {
#if 0
if(!stream.ReadObject(currentChunk.chunkHeader))
return false;
printf("FUCK: %08x %d\n", currentChunk.chunkHeader.ckId, currentChunk.chunkHeader.ckSz);
if(isForm) {
if(currentChunk.chunkHeader.ckId != expectedFormCkId) {
auto* expectedBytes = std::bit_cast<u8*>(&expectedFormCkId);
auto* gotBytes = std::bit_cast<u8*>(&currentChunk.chunkHeader.ckId);
std::printf("WARNING: Expected Form ckId '%c%c%c%c', got '%c%c%c%c'\n",
expectedBytes[0], expectedBytes[1], expectedBytes[2], expectedBytes[3],
gotBytes[0], gotBytes[1], gotBytes[2], gotBytes[3]);
}
} else {
currentChunk.chunkData.resize(sizeof(currentChunk.chunkHeader) + currentChunk.chunkHeader.ckSz);
memcpy(currentChunk.chunkData.data(), &currentChunk.chunkHeader, sizeof(currentChunk.chunkHeader));
if(stream.ReadSome(currentChunk.chunkData.data() + sizeof(currentChunk.chunkHeader), currentChunk.chunkHeader.ckSz) != currentChunk.chunkHeader.ckSz)
return false;
}
if(callback)
callback(currentChunk);
return true;
#endif
if(!stream.ReadObject(currentChunk.chunkHeader))
return false;
printf("FUCK: %08x %d\n", currentChunk.chunkHeader.ckId, currentChunk.chunkHeader.ckSz);
if(isForm) {
if(currentChunk.chunkHeader.ckId != expectedFormCkId) {
auto* expectedBytes = std::bit_cast<u8*>(&expectedFormCkId);
auto* gotBytes = std::bit_cast<u8*>(&currentChunk.chunkHeader.ckId);
std::printf("WARNING: Expected Form ckId '%c%c%c%c', got '%c%c%c%c'\n",
expectedBytes[0], expectedBytes[1], expectedBytes[2], expectedBytes[3],
gotBytes[0], gotBytes[1], gotBytes[2], gotBytes[3]);
}
}// else {
currentChunk.chunkData.resize(currentChunk.chunkHeader.ckSz);
if(stream.ReadSome(currentChunk.chunkData) != currentChunk.chunkHeader.ckSz)
return false;
//}
if(callback)
callback(currentChunk);
return true;
}
} // namespace dmtools::dmbmx