mirror of
https://github.com/Mauler125/r5sdk.git
synced 2025-02-09 19:15:03 +01:00
Instead of seeking to the very start again when GetSize() is called, seek back to the original position. Also added dedicated methods for seeking put or get instead of using the mode since this is a fstream
112 lines
2.9 KiB
C++
112 lines
2.9 KiB
C++
#pragma once
|
|
|
|
class CIOStream
|
|
{
|
|
public:
|
|
enum Mode_t
|
|
{
|
|
NONE = 0,
|
|
READ = std::ios::in,
|
|
WRITE = std::ios::out,
|
|
BINARY = std::ios::binary,
|
|
};
|
|
|
|
CIOStream();
|
|
CIOStream(const fs::path& fsFileFullPath, int nFlags);
|
|
~CIOStream();
|
|
|
|
bool Open(const fs::path& fsFileFullPath, int nFlags);
|
|
void Close();
|
|
void Flush();
|
|
|
|
void ComputeFileSize();
|
|
|
|
std::streampos TellGet();
|
|
std::streampos TellPut();
|
|
|
|
void SeekGet(const std::streampos nOffset);
|
|
void SeekPut(const std::streampos nOffset);
|
|
void Seek(const std::streampos nOffset);
|
|
|
|
const std::filebuf* GetData() const;
|
|
const std::streampos GetSize() const;
|
|
|
|
bool IsReadable();
|
|
bool IsWritable() const;
|
|
|
|
bool IsEof() const;
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Purpose: reads any value from the file
|
|
//-----------------------------------------------------------------------------
|
|
template<typename T>
|
|
void Read(T& tValue)
|
|
{
|
|
if (IsReadable())
|
|
m_Stream.read(reinterpret_cast<char*>(&tValue), sizeof(tValue));
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Purpose: reads any value from the file with specified size
|
|
//-----------------------------------------------------------------------------
|
|
template<typename T>
|
|
void Read(T* tValue, const size_t nSize)
|
|
{
|
|
if (IsReadable())
|
|
m_Stream.read(reinterpret_cast<char*>(tValue), nSize);
|
|
}
|
|
template<typename T>
|
|
void Read(T& tValue, const size_t nSize)
|
|
{
|
|
if (IsReadable())
|
|
m_Stream.read(reinterpret_cast<char*>(&tValue), nSize);
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Purpose: reads any value from the file and returns it
|
|
//-----------------------------------------------------------------------------
|
|
template<typename T>
|
|
T Read()
|
|
{
|
|
T value{};
|
|
if (!IsReadable())
|
|
return value;
|
|
|
|
m_Stream.read(reinterpret_cast<char*>(&value), sizeof(value));
|
|
return value;
|
|
}
|
|
bool ReadString(std::string& svOut);
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Purpose: writes any value to the file
|
|
//-----------------------------------------------------------------------------
|
|
template<typename T>
|
|
void Write(T tValue)
|
|
{
|
|
if (!IsWritable())
|
|
return;
|
|
|
|
m_Stream.write(reinterpret_cast<const char*>(&tValue), sizeof(tValue));
|
|
m_nSize += sizeof(tValue);
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Purpose: writes any value to the file with specified size
|
|
//-----------------------------------------------------------------------------
|
|
template<typename T>
|
|
void Write(T* tValue, size_t nSize)
|
|
{
|
|
if (!IsWritable())
|
|
return;
|
|
|
|
m_Stream.write(reinterpret_cast<const char*>(tValue), nSize);
|
|
m_nSize += nSize;
|
|
}
|
|
bool WriteString(const std::string& svInput);
|
|
|
|
private:
|
|
std::streampos m_nSize; // File size.
|
|
int m_nFlags; // Stream flags.
|
|
std::fstream m_Stream; // I/O stream.
|
|
};
|