SIEdit/lib/file.h

112 lines
2 KiB
C
Raw Normal View History

2022-07-18 14:25:00 -04:00
#ifndef FILE_H
#define FILE_H
#include "types.h"
namespace si {
class FileBase
{
public:
FileBase()
{
}
virtual ~FileBase()
{
}
enum Mode {
Read,
Write
};
2022-07-31 18:20:32 -04:00
typedef uint64_t pos_t;
2022-07-18 14:25:00 -04:00
uint8_t ReadU8();
uint16_t ReadU16();
uint32_t ReadU32();
std::string ReadString();
2022-07-31 18:20:32 -04:00
bytearray ReadBytes(pos_t size);
2022-07-18 14:25:00 -04:00
Vector3 ReadVector3();
2022-07-31 18:20:32 -04:00
virtual pos_t ReadData(void *data, pos_t size) = 0;
2022-07-18 14:25:00 -04:00
void WriteU8(uint8_t u);
void WriteU16(uint16_t u);
void WriteU32(uint32_t u);
void WriteString(const std::string &s);
void WriteBytes(const bytearray &b);
void WriteVector3(const Vector3 &b);
2022-07-31 18:20:32 -04:00
virtual pos_t WriteData(const void *data, pos_t size) = 0;
2022-07-18 14:25:00 -04:00
virtual void Close() {}
enum SeekMode
{
SeekStart,
SeekCurrent,
SeekEnd
};
2022-07-31 18:20:32 -04:00
virtual pos_t pos() = 0;
virtual pos_t size() = 0;
virtual void seek(pos_t p, SeekMode s = SeekStart) = 0;
2022-07-31 16:28:43 -04:00
LIBWEAVER_EXPORT bool atEnd() { return pos() == size(); }
2022-07-18 14:25:00 -04:00
};
class File : public FileBase
{
public:
2022-07-31 18:20:32 -04:00
File();
virtual ~File()
{
Close();
}
2022-07-18 14:25:00 -04:00
bool Open(const char *c, Mode mode);
#ifdef _WIN32
bool Open(const wchar_t *c, Mode mode);
#endif
2022-07-31 18:20:32 -04:00
virtual pos_t pos();
virtual pos_t size();
virtual void seek(pos_t p, SeekMode s = SeekStart);
2022-07-18 14:25:00 -04:00
virtual void Close();
2022-07-31 18:20:32 -04:00
virtual pos_t ReadData(void *data, pos_t size);
virtual pos_t WriteData(const void *data, pos_t size);
2022-07-18 14:25:00 -04:00
private:
2022-07-31 18:20:32 -04:00
void *m_Handle;
2022-07-18 14:25:00 -04:00
Mode m_Mode;
};
class MemoryBuffer : public FileBase
{
public:
2022-07-30 12:37:46 -04:00
LIBWEAVER_EXPORT MemoryBuffer();
LIBWEAVER_EXPORT MemoryBuffer(const bytearray &data);
2022-07-18 14:25:00 -04:00
2022-07-31 18:20:32 -04:00
LIBWEAVER_EXPORT virtual pos_t pos();
LIBWEAVER_EXPORT virtual pos_t size();
LIBWEAVER_EXPORT virtual void seek(pos_t p, SeekMode s = SeekStart);
2022-07-18 14:25:00 -04:00
const bytearray &data() const { return m_Internal; }
2022-07-31 18:20:32 -04:00
LIBWEAVER_EXPORT virtual pos_t ReadData(void *data, pos_t size);
LIBWEAVER_EXPORT virtual pos_t WriteData(const void *data, pos_t size);
2022-07-18 14:25:00 -04:00
private:
bytearray m_Internal;
2022-07-31 18:20:32 -04:00
pos_t m_Position;
2022-07-18 14:25:00 -04:00
};
}
#endif // FILE_H