TombEngine/TR5Main/Specific/IO/Streams.h

226 lines
3.8 KiB
C
Raw Normal View History

#pragma once
#include <istream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <stdlib.h>
using namespace std;
enum SEEK_ORIGIN {
BEGIN,
CURRENT
};
class BaseStream {
public:
2019-12-02 09:11:21 +01:00
virtual bool Read(char* buffer, int length) = 0;
virtual bool Write(char* buffer, int length) = 0;
virtual int GetCurrentPosition() = 0;
virtual bool Seek(int seek, SEEK_ORIGIN origin) = 0;
virtual bool IsEOF() = 0;
virtual bool Close() = 0;
2019-12-02 09:11:21 +01:00
bool ReadBytes(byte* value, int length)
{
return Read(reinterpret_cast<char*>(value), length);
}
bool ReadByte(byte* value)
{
return Read(reinterpret_cast<char*>(value), 1);
}
bool ReadBool(bool* value)
{
return Read(reinterpret_cast<char*>(value), 1);
}
2019-12-02 09:11:21 +01:00
bool ReadInt16(short* value)
{
return Read(reinterpret_cast<char*>(value), 2);
}
2019-12-02 09:11:21 +01:00
bool ReadInt32(int* value)
{
return Read(reinterpret_cast<char*>(value), 4);
}
bool ReadFloat(float* value)
{
return Read(reinterpret_cast<char*>(value), 4);
}
bool ReadString(char** value)
{
2019-12-02 09:11:21 +01:00
int length;
ReadInt32(&length);
*value = (char*)malloc(length + 1);
Read(*value, length);
(*value)[length] = NULL;
return true;
}
2019-12-02 09:11:21 +01:00
bool WriteBytes(byte* value, int length)
{
return Write(reinterpret_cast<char*>(value), length);
}
bool WriteByte(byte value)
{
return Write(reinterpret_cast<char*>(&value), 1);
}
2019-12-02 09:11:21 +01:00
bool WriteInt16(short value)
{
return Write(reinterpret_cast<char*>(&value), 2);
}
2019-12-02 09:11:21 +01:00
bool WriteInt32(int value)
{
return Write(reinterpret_cast<char*>(&value), 4);
}
bool WriteBool(bool value)
2018-10-09 00:02:14 +02:00
{
return Write(reinterpret_cast<char*>(&value), 1);
2018-10-09 00:02:14 +02:00
}
bool WriteFloat(float value)
2018-10-09 00:02:14 +02:00
{
return Write(reinterpret_cast<char*>(&value), 4);
2018-10-09 00:02:14 +02:00
}
bool WriteString(char* str)
{
2019-12-02 09:11:21 +01:00
int length = (int)strlen(str);
WriteInt32(length);
Write(str, length);
return true;
}
};
class MemoryStream : public BaseStream {
private:
char* m_startBuffer;
char* m_buffer;
2019-12-02 09:11:21 +01:00
int m_size;
public:
2019-12-02 09:11:21 +01:00
MemoryStream(char* buffer, int size)
{
m_buffer = (char*)malloc(size);
m_startBuffer = m_buffer;
memcpy(m_buffer, buffer, size);
m_size = size;
}
2019-12-02 09:11:21 +01:00
MemoryStream(int size)
{
m_buffer = (char*)malloc(size);
m_startBuffer = m_buffer;
m_size = size;
}
~MemoryStream()
{
free(m_startBuffer);
}
2019-12-02 09:11:21 +01:00
bool Read(char* buffer, int length)
{
memcpy(buffer, m_buffer, length);
m_buffer += length;
return true;
}
2019-12-02 09:11:21 +01:00
bool Write(char* buffer, int length)
{
memcpy(m_buffer, buffer, length);
m_buffer += length;
return true;
}
2019-12-02 09:11:21 +01:00
int GetCurrentPosition()
{
return (m_buffer - m_startBuffer);
}
2019-12-02 09:11:21 +01:00
bool Seek(int seek, SEEK_ORIGIN origin)
{
if (origin == SEEK_ORIGIN::BEGIN)
m_buffer = m_startBuffer + seek;
else
m_buffer += seek;
return true;
}
bool IsEOF()
{
return (GetCurrentPosition() > m_size);
}
bool Close()
{
return true;
}
};
class FileStream : public BaseStream {
private:
fstream m_stream;
public:
2018-10-24 23:32:22 +02:00
FileStream(char* fileName, bool read, bool write)
{
2019-12-02 09:11:21 +01:00
int mode = 0;
2018-10-24 23:32:22 +02:00
if (read)
mode |= ifstream::binary | fstream::in;
if (write)
mode |= ofstream::binary | fstream::out | fstream::trunc;
m_stream.open(fileName, mode);
}
~FileStream()
{
m_stream.close();
}
2019-12-02 09:11:21 +01:00
bool Read(char* buffer, int length)
{
m_stream.read(buffer, length);
return true;
}
2019-12-02 09:11:21 +01:00
bool Write(char* buffer, int length)
{
m_stream.write(buffer, length);
return true;
}
2019-12-02 09:11:21 +01:00
int GetCurrentPosition()
{
2019-12-02 09:11:21 +01:00
return (int)(m_stream.tellg());
}
2019-12-02 09:11:21 +01:00
bool Seek(int seek, SEEK_ORIGIN origin)
{
m_stream.seekg(seek, (origin == SEEK_ORIGIN::BEGIN ? m_stream.beg : m_stream.cur));
return true;
}
bool IsEOF()
{
return (m_stream.eof());
}
bool Close()
{
m_stream.flush();
m_stream.close();
return true;
}
};