mirror of
https://github.com/isledecomp/SIEdit.git
synced 2024-11-27 01:25:45 -05:00
file: added more basic functions to custom file abstraction classes
This commit is contained in:
parent
0443b9e69b
commit
91de5a7df4
2 changed files with 47 additions and 15 deletions
37
lib/file.cpp
37
lib/file.cpp
|
@ -53,6 +53,15 @@ size_t File::pos()
|
|||
}
|
||||
}
|
||||
|
||||
size_t File::size()
|
||||
{
|
||||
size_t before = pos();
|
||||
seek(0, SeekEnd);
|
||||
size_t sz = pos();
|
||||
seek(before, SeekStart);
|
||||
return sz;
|
||||
}
|
||||
|
||||
void File::seek(size_t p, SeekMode s)
|
||||
{
|
||||
std::ios::seekdir d;
|
||||
|
@ -81,19 +90,23 @@ bool File::atEnd()
|
|||
return !m_Stream.good();
|
||||
}
|
||||
|
||||
void File::CloseHandle()
|
||||
void File::Close()
|
||||
{
|
||||
m_Stream.close();
|
||||
}
|
||||
|
||||
void File::ReadData(char *data, size_t size)
|
||||
size_t File::ReadData(char *data, size_t size)
|
||||
{
|
||||
size_t before = this->pos();
|
||||
m_Stream.read(data, size);
|
||||
return this->pos() - before;
|
||||
}
|
||||
|
||||
void File::WriteData(const char *data, size_t size)
|
||||
size_t File::WriteData(const char *data, size_t size)
|
||||
{
|
||||
size_t before = this->pos();
|
||||
m_Stream.write(data, size);
|
||||
return this->pos() - before;
|
||||
}
|
||||
|
||||
uint8_t FileBase::ReadU8()
|
||||
|
@ -188,11 +201,22 @@ MemoryBuffer::MemoryBuffer()
|
|||
m_Position = 0;
|
||||
}
|
||||
|
||||
MemoryBuffer::MemoryBuffer(const bytearray &data)
|
||||
{
|
||||
m_Internal = data;
|
||||
m_Position = 0;
|
||||
}
|
||||
|
||||
size_t MemoryBuffer::pos()
|
||||
{
|
||||
return m_Position;
|
||||
}
|
||||
|
||||
size_t MemoryBuffer::size()
|
||||
{
|
||||
return m_Internal.size();
|
||||
}
|
||||
|
||||
void MemoryBuffer::seek(size_t p, SeekMode s)
|
||||
{
|
||||
switch (s) {
|
||||
|
@ -213,22 +237,23 @@ bool MemoryBuffer::atEnd()
|
|||
return m_Position == m_Internal.size();
|
||||
}
|
||||
|
||||
void MemoryBuffer::ReadData(char *data, size_t size)
|
||||
size_t MemoryBuffer::ReadData(char *data, size_t size)
|
||||
{
|
||||
size = std::min(size, m_Internal.size() - m_Position);
|
||||
memcpy(data, m_Internal.data() + m_Position, size);
|
||||
m_Position += size;
|
||||
return size;
|
||||
}
|
||||
|
||||
void MemoryBuffer::WriteData(const char *data, size_t size)
|
||||
size_t MemoryBuffer::WriteData(const char *data, size_t size)
|
||||
{
|
||||
LogDebug() << "writing " << size << " bytes to " << m_Position << std::endl;
|
||||
size_t end = m_Position + size;
|
||||
if (end > m_Internal.size()) {
|
||||
m_Internal.resize(end);
|
||||
}
|
||||
memcpy(m_Internal.data() + m_Position, data, size);
|
||||
m_Position += size;
|
||||
return size;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
25
lib/file.h
25
lib/file.h
|
@ -29,7 +29,7 @@ public:
|
|||
std::string ReadString();
|
||||
bytearray ReadBytes(size_t size);
|
||||
Vector3 ReadVector3();
|
||||
virtual void ReadData(char *data, size_t size) = 0;
|
||||
virtual size_t ReadData(char *data, size_t size) = 0;
|
||||
|
||||
void WriteU8(uint8_t u);
|
||||
void WriteU16(uint16_t u);
|
||||
|
@ -37,7 +37,7 @@ public:
|
|||
void WriteString(const std::string &s);
|
||||
void WriteBytes(const bytearray &b);
|
||||
void WriteVector3(const Vector3 &b);
|
||||
virtual void WriteData(const char *data, size_t size) = 0;
|
||||
virtual size_t WriteData(const char *data, size_t size) = 0;
|
||||
|
||||
virtual void Close() {}
|
||||
|
||||
|
@ -49,6 +49,7 @@ public:
|
|||
};
|
||||
|
||||
virtual size_t pos() = 0;
|
||||
virtual size_t size() = 0;
|
||||
virtual void seek(size_t p, SeekMode s = SeekStart) = 0;
|
||||
virtual bool atEnd() = 0;
|
||||
|
||||
|
@ -57,6 +58,11 @@ public:
|
|||
class File : public FileBase
|
||||
{
|
||||
public:
|
||||
virtual ~File()
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
bool Open(const char *c, Mode mode);
|
||||
|
||||
#ifdef _WIN32
|
||||
|
@ -64,13 +70,13 @@ public:
|
|||
#endif
|
||||
|
||||
virtual size_t pos();
|
||||
virtual size_t size();
|
||||
virtual void seek(size_t p, SeekMode s = SeekStart);
|
||||
virtual bool atEnd();
|
||||
|
||||
protected:
|
||||
virtual void CloseHandle();
|
||||
virtual void ReadData(char *data, size_t size);
|
||||
virtual void WriteData(const char *data, size_t size);
|
||||
virtual void Close();
|
||||
virtual size_t ReadData(char *data, size_t size);
|
||||
virtual size_t WriteData(const char *data, size_t size);
|
||||
|
||||
private:
|
||||
std::fstream m_Stream;
|
||||
|
@ -82,16 +88,17 @@ class MemoryBuffer : public FileBase
|
|||
{
|
||||
public:
|
||||
MemoryBuffer();
|
||||
MemoryBuffer(const bytearray &data);
|
||||
|
||||
virtual size_t pos();
|
||||
virtual size_t size();
|
||||
virtual void seek(size_t p, SeekMode s = SeekStart);
|
||||
virtual bool atEnd();
|
||||
|
||||
const bytearray &data() const { return m_Internal; }
|
||||
|
||||
protected:
|
||||
virtual void ReadData(char *data, size_t size);
|
||||
virtual void WriteData(const char *data, size_t size);
|
||||
virtual size_t ReadData(char *data, size_t size);
|
||||
virtual size_t WriteData(const char *data, size_t size);
|
||||
|
||||
private:
|
||||
bytearray m_Internal;
|
||||
|
|
Loading…
Reference in a new issue