2006-06-15 04:19:30 +00:00
|
|
|
#include "File.h"
|
|
|
|
|
|
|
|
using namespace ISO9660;
|
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
CFile::CFile(CISO9660* iso, uint64 start, uint64 size)
|
|
|
|
: m_iso(iso)
|
|
|
|
, m_start(start)
|
|
|
|
, m_end(start + size)
|
|
|
|
, m_position(0)
|
|
|
|
, m_blockPosition(static_cast<uint32>(start / CISO9660::BLOCKSIZE))
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-07-12 21:59:58 -04:00
|
|
|
iso->ReadBlock(m_blockPosition, m_block);
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CFile::~CFile()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
void CFile::Seek(int64 amount, Framework::STREAM_SEEK_DIRECTION whence)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-07-12 21:59:58 -04:00
|
|
|
switch(whence)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2008-04-05 03:38:53 +00:00
|
|
|
case Framework::STREAM_SEEK_SET:
|
2014-07-12 21:59:58 -04:00
|
|
|
m_position = amount;
|
2006-06-15 04:19:30 +00:00
|
|
|
break;
|
2008-04-05 03:38:53 +00:00
|
|
|
case Framework::STREAM_SEEK_CUR:
|
2014-07-12 21:59:58 -04:00
|
|
|
m_position += amount;
|
2006-06-15 04:19:30 +00:00
|
|
|
break;
|
2008-04-05 03:38:53 +00:00
|
|
|
case Framework::STREAM_SEEK_END:
|
2014-07-12 21:59:58 -04:00
|
|
|
m_position = m_end + 1;
|
2006-06-15 04:19:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64 CFile::Tell()
|
|
|
|
{
|
|
|
|
if(IsEOF())
|
|
|
|
{
|
2014-07-12 21:59:58 -04:00
|
|
|
return m_end - m_start;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
2014-07-12 21:59:58 -04:00
|
|
|
return m_position;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
uint64 CFile::Read(void* data, uint64 length)
|
2006-06-15 04:19:30 +00:00
|
|
|
{
|
2014-07-12 21:59:58 -04:00
|
|
|
if(length == 0) return 0;
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
uint64 total = length;
|
2006-06-15 04:19:30 +00:00
|
|
|
//Read what's remaining of this block
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
SyncBlock();
|
2014-07-12 21:59:58 -04:00
|
|
|
uint64 blockPosition = (m_start + m_position) % CISO9660::BLOCKSIZE;
|
|
|
|
uint64 blockRemain = CISO9660::BLOCKSIZE - blockPosition;
|
|
|
|
uint64 toRead = (length > blockRemain) ? (blockRemain) : (length);
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
memcpy(data, m_block + blockPosition, static_cast<uint32>(toRead));
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
m_position += toRead;
|
|
|
|
length -= toRead;
|
|
|
|
data = reinterpret_cast<uint8*>(data) + toRead;
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
if(length == 0) break;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
return total;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64 CFile::Write(const void* pData, uint64 nLength)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFile::IsEOF()
|
|
|
|
{
|
2014-07-12 21:59:58 -04:00
|
|
|
return ((m_start + m_position) > m_end);
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CFile::SyncBlock()
|
|
|
|
{
|
2014-07-12 21:59:58 -04:00
|
|
|
uint32 position = static_cast<uint32>((m_start + m_position) / CISO9660::BLOCKSIZE);
|
|
|
|
if(position == m_blockPosition) return;
|
2006-06-15 04:19:30 +00:00
|
|
|
|
2014-07-12 21:59:58 -04:00
|
|
|
m_iso->ReadBlock(position, m_block);
|
|
|
|
m_blockPosition = position;
|
2006-06-15 04:19:30 +00:00
|
|
|
}
|