|
|
|
@ -28,6 +28,7 @@ FileStream::FileStream(const std::string& name, PHYSFS_File *fileHandle)
|
|
|
|
|
{
|
|
|
|
|
m_name = name;
|
|
|
|
|
m_fileHandle = fileHandle;
|
|
|
|
|
m_cacheReadPos = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileStream::~FileStream()
|
|
|
|
@ -35,6 +36,24 @@ FileStream::~FileStream()
|
|
|
|
|
close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FileStream::cache()
|
|
|
|
|
{
|
|
|
|
|
if(!m_fileHandle)
|
|
|
|
|
logTraceError("no file handle to cache");
|
|
|
|
|
|
|
|
|
|
// cache entire file into cache buffer
|
|
|
|
|
m_cacheReadPos = PHYSFS_tell(m_fileHandle);
|
|
|
|
|
PHYSFS_seek(m_fileHandle, 0);
|
|
|
|
|
int size = PHYSFS_fileLength(m_fileHandle);
|
|
|
|
|
m_cacheBuffer.resize(size);
|
|
|
|
|
int res = PHYSFS_read(m_fileHandle, &m_cacheBuffer[0], size, 1);
|
|
|
|
|
if(res == -1)
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
|
|
|
|
|
PHYSFS_close(m_fileHandle);
|
|
|
|
|
m_fileHandle = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileStream::close()
|
|
|
|
|
{
|
|
|
|
|
if(m_fileHandle) {
|
|
|
|
@ -43,12 +62,18 @@ bool FileStream::close()
|
|
|
|
|
|
|
|
|
|
m_fileHandle = nullptr;
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
m_cacheBuffer.clear();
|
|
|
|
|
m_cacheReadPos = 0;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileStream::flush()
|
|
|
|
|
{
|
|
|
|
|
if(!m_fileHandle)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if(PHYSFS_flush(m_fileHandle) == 0) {
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
return false;
|
|
|
|
@ -58,71 +83,143 @@ bool FileStream::flush()
|
|
|
|
|
|
|
|
|
|
int FileStream::read(void *buffer, int size, int nmemb)
|
|
|
|
|
{
|
|
|
|
|
int res = PHYSFS_read(m_fileHandle, buffer, size, nmemb);
|
|
|
|
|
if(res == -1) {
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
return 0;
|
|
|
|
|
if(m_fileHandle) {
|
|
|
|
|
int res = PHYSFS_read(m_fileHandle, buffer, size, nmemb);
|
|
|
|
|
if(res == -1) {
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
} else {
|
|
|
|
|
uint maxReadPos = m_cacheBuffer.size()-1;
|
|
|
|
|
int writePos = 0;
|
|
|
|
|
uint8 *outBuffer = (uint8*)buffer;
|
|
|
|
|
for(int i=0;i<nmemb;++i) {
|
|
|
|
|
if(m_cacheReadPos+size > maxReadPos)
|
|
|
|
|
return i;
|
|
|
|
|
|
|
|
|
|
for(int j=0;j<size;++j)
|
|
|
|
|
outBuffer[writePos++] = m_cacheBuffer[m_cacheReadPos++];
|
|
|
|
|
}
|
|
|
|
|
return nmemb;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileStream::write(void *buffer, int count)
|
|
|
|
|
{
|
|
|
|
|
if(!m_fileHandle)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if(PHYSFS_write(m_fileHandle, buffer, 1, count) != count) {
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileStream::seek(int pos)
|
|
|
|
|
{
|
|
|
|
|
if(PHYSFS_seek(m_fileHandle, pos) == 0) {
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
return false;
|
|
|
|
|
if(m_fileHandle) {
|
|
|
|
|
if(PHYSFS_seek(m_fileHandle, pos) == 0) {
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if(pos > (int)m_cacheBuffer.size() || pos < 0) {
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': seek pos cannot be greater than file length");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
m_cacheReadPos = pos;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FileStream::size()
|
|
|
|
|
{
|
|
|
|
|
return PHYSFS_fileLength(m_fileHandle);
|
|
|
|
|
if(m_fileHandle)
|
|
|
|
|
return PHYSFS_fileLength(m_fileHandle);
|
|
|
|
|
else
|
|
|
|
|
return m_cacheBuffer.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FileStream::tell()
|
|
|
|
|
{
|
|
|
|
|
return PHYSFS_tell(m_fileHandle);
|
|
|
|
|
if(m_fileHandle)
|
|
|
|
|
return PHYSFS_tell(m_fileHandle);
|
|
|
|
|
else
|
|
|
|
|
return m_cacheReadPos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8 FileStream::getU8()
|
|
|
|
|
{
|
|
|
|
|
uint8 v = 0;
|
|
|
|
|
if(PHYSFS_read(m_fileHandle, &v, 1, 1) != 1)
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
if(m_fileHandle) {
|
|
|
|
|
if(PHYSFS_read(m_fileHandle, &v, 1, 1) != 1)
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
} else {
|
|
|
|
|
if(m_cacheReadPos+1 >= m_cacheBuffer.size()) {
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': reached file eof");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v = m_cacheBuffer[m_cacheReadPos];
|
|
|
|
|
m_cacheReadPos += 1;
|
|
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16 FileStream::getU16()
|
|
|
|
|
{
|
|
|
|
|
uint16 v = 0;
|
|
|
|
|
if(PHYSFS_readULE16(m_fileHandle, &v) == 0)
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
if(m_fileHandle) {
|
|
|
|
|
if(PHYSFS_readULE16(m_fileHandle, &v) == 0)
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
} else {
|
|
|
|
|
if(m_cacheReadPos+2 >= m_cacheBuffer.size()) {
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': reached file eof");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v = Fw::readLE16(&m_cacheBuffer[m_cacheReadPos]);
|
|
|
|
|
m_cacheReadPos += 2;
|
|
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32 FileStream::getU32()
|
|
|
|
|
{
|
|
|
|
|
uint32 v = 0;
|
|
|
|
|
if(PHYSFS_readULE32(m_fileHandle, &v) == 0)
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
if(m_fileHandle) {
|
|
|
|
|
if(PHYSFS_readULE32(m_fileHandle, &v) == 0)
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
} else {
|
|
|
|
|
if(m_cacheReadPos+4 >= m_cacheBuffer.size()) {
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': reached file eof");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v = Fw::readLE32(&m_cacheBuffer[m_cacheReadPos]);
|
|
|
|
|
m_cacheReadPos += 4;
|
|
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint64 FileStream::getU64()
|
|
|
|
|
{
|
|
|
|
|
uint64 v = 0;
|
|
|
|
|
if(PHYSFS_readULE64(m_fileHandle, (PHYSFS_uint64*)&v) == 0)
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
if(m_fileHandle) {
|
|
|
|
|
if(PHYSFS_readULE64(m_fileHandle, (PHYSFS_uint64*)&v) == 0)
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': ", PHYSFS_getLastError());
|
|
|
|
|
} else {
|
|
|
|
|
if(m_cacheReadPos+8 >= m_cacheBuffer.size()) {
|
|
|
|
|
logTraceError("operation failed on '", m_name, "': reached file eof");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
v = Fw::readLE64(&m_cacheBuffer[m_cacheReadPos]);
|
|
|
|
|
m_cacheReadPos += 8;
|
|
|
|
|
}
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|