2021-07-21 09:35:11 -04:00
|
|
|
#include "ChdStreamSupport.h"
|
2022-09-30 11:55:14 -04:00
|
|
|
#include <cassert>
|
2021-07-21 09:35:11 -04:00
|
|
|
#include <libchdr/chd.h>
|
2021-07-16 10:54:45 -04:00
|
|
|
#include "Stream.h"
|
|
|
|
|
2022-09-28 09:31:49 -04:00
|
|
|
static size_t stream_core_fread(void* buffer, size_t elemSize, size_t elemCount, core_file* file)
|
2021-07-16 10:54:45 -04:00
|
|
|
{
|
2022-09-28 09:31:49 -04:00
|
|
|
assert(elemSize == 1);
|
|
|
|
auto stream = reinterpret_cast<Framework::CStream*>(file->argp);
|
|
|
|
return stream->Read(buffer, elemSize * elemCount);
|
2021-07-16 10:54:45 -04:00
|
|
|
}
|
|
|
|
|
2022-10-31 09:54:10 -04:00
|
|
|
static int stream_core_fseek(core_file* file, INT64 position, int whence)
|
2021-07-16 10:54:45 -04:00
|
|
|
{
|
2022-09-28 09:31:49 -04:00
|
|
|
auto stream = reinterpret_cast<Framework::CStream*>(file->argp);
|
2021-07-16 10:54:45 -04:00
|
|
|
stream->Seek(position, static_cast<Framework::STREAM_SEEK_DIRECTION>(whence));
|
2022-09-28 09:31:49 -04:00
|
|
|
return 0;
|
2021-07-16 10:54:45 -04:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:31:49 -04:00
|
|
|
static UINT64 stream_core_fsize(core_file* file)
|
2021-07-16 10:54:45 -04:00
|
|
|
{
|
2022-09-28 09:31:49 -04:00
|
|
|
auto stream = reinterpret_cast<Framework::CStream*>(file->argp);
|
|
|
|
auto currPos = stream->Tell();
|
|
|
|
auto size = stream->GetLength();
|
|
|
|
stream->Seek(currPos, Framework::STREAM_SEEK_SET);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int stream_core_fclose(core_file* file)
|
|
|
|
{
|
|
|
|
delete file;
|
|
|
|
return 0;
|
2021-07-16 10:54:45 -04:00
|
|
|
}
|
2021-07-21 09:35:11 -04:00
|
|
|
|
|
|
|
core_file* ChdStreamSupport::CreateFileFromStream(Framework::CStream* stream)
|
|
|
|
{
|
2022-09-28 09:31:49 -04:00
|
|
|
auto file = new core_file;
|
|
|
|
file->argp = stream;
|
|
|
|
file->fread = &stream_core_fread;
|
|
|
|
file->fseek = &stream_core_fseek;
|
|
|
|
file->fsize = &stream_core_fsize;
|
|
|
|
file->fclose = &stream_core_fclose;
|
|
|
|
return file;
|
2021-07-21 09:35:11 -04:00
|
|
|
}
|