2021-12-25 22:36:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
class CIOStream
|
|
|
|
{
|
|
|
|
public:
|
2022-06-02 01:52:49 +02:00
|
|
|
enum class Mode_t
|
|
|
|
{
|
|
|
|
NONE = 0,
|
|
|
|
READ,
|
|
|
|
WRITE
|
|
|
|
};
|
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
CIOStream();
|
2022-06-24 16:56:28 +02:00
|
|
|
CIOStream(const fs::path& fsFileFullPath, Mode_t eMode);
|
2021-12-25 22:36:38 +01:00
|
|
|
~CIOStream();
|
|
|
|
|
2022-06-24 16:56:28 +02:00
|
|
|
bool Open(const fs::path& fsFileFullPath, Mode_t eMode);
|
2022-05-30 02:47:15 +02:00
|
|
|
void Close();
|
2022-06-04 13:55:24 +02:00
|
|
|
void Flush();
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-05-30 02:47:15 +02:00
|
|
|
size_t GetPosition();
|
|
|
|
void SetPosition(int64_t nOffset);
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-06-02 01:52:49 +02:00
|
|
|
const vector<uint8_t>& GetVector() const;
|
|
|
|
const uint8_t* GetData() const;
|
|
|
|
const size_t GetSize() const;
|
|
|
|
|
2022-05-30 02:47:15 +02:00
|
|
|
bool IsReadable();
|
|
|
|
bool IsWritable() const;
|
|
|
|
|
|
|
|
bool IsEof() const;
|
2021-12-25 22:36:38 +01:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-06-02 01:52:49 +02:00
|
|
|
// Purpose: reads any value from the file
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
2022-05-30 02:47:15 +02:00
|
|
|
void Read(T& tValue) // Template functions have to be in the header!
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-05-30 02:47:15 +02:00
|
|
|
if (IsReadable())
|
2022-06-02 01:52:49 +02:00
|
|
|
m_iStream.read(reinterpret_cast<char*>(&tValue), sizeof(tValue));
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-06-02 01:52:49 +02:00
|
|
|
// Purpose: reads any value from the file with specified size
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
|
|
void Read(T& tValue, size_t nSize) // Template functions have to be in the header!
|
|
|
|
{
|
|
|
|
if (IsReadable())
|
|
|
|
m_iStream.read(reinterpret_cast<char*>(&tValue), nSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: reads any value from the file and returns it
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
2022-05-30 02:47:15 +02:00
|
|
|
T Read() // Template functions have to be in the header!
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
T value{};
|
|
|
|
if (!IsReadable())
|
|
|
|
return value;
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-06-02 01:52:49 +02:00
|
|
|
m_iStream.read(reinterpret_cast<char*>(&value), sizeof(value));
|
2021-12-25 22:36:38 +01:00
|
|
|
return value;
|
|
|
|
}
|
2022-05-30 02:47:15 +02:00
|
|
|
string ReadString();
|
2021-12-25 22:36:38 +01:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-06-02 01:52:49 +02:00
|
|
|
// Purpose: writes any value to the file
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
2022-05-30 02:47:15 +02:00
|
|
|
void Write(T tValue) // Template functions have to be in the header!
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-05-30 02:47:15 +02:00
|
|
|
if (!IsWritable())
|
2021-12-25 22:36:38 +01:00
|
|
|
return;
|
2022-06-02 01:52:49 +02:00
|
|
|
|
|
|
|
m_oStream.write(reinterpret_cast<const char*>(&tValue), sizeof(tValue));
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: writes any value to the file with specified size
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
|
|
void Write(T tValue, size_t nSize) // Template functions have to be in the header!
|
|
|
|
{
|
|
|
|
if (!IsWritable())
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_oStream.write(reinterpret_cast<const char*>(tValue), nSize);
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
2022-05-30 02:47:15 +02:00
|
|
|
void WriteString(string svInput);
|
2022-06-02 01:52:49 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
ofstream m_oStream; // Output file stream.
|
|
|
|
ifstream m_iStream; // Input file stream.
|
|
|
|
vector<uint8_t> m_vData; // Data vector
|
|
|
|
Mode_t m_eCurrentMode; // Current active mode.
|
2021-12-25 22:36:38 +01:00
|
|
|
};
|