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-12-02 00:25:28 +01:00
|
|
|
void ComputeFileSize();
|
|
|
|
|
2022-12-01 22:47:39 +01:00
|
|
|
std::streampos GetPosition();
|
|
|
|
void SetPosition(std::streampos nOffset);
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-12-01 22:47:39 +01:00
|
|
|
const std::filebuf* GetData() const;
|
|
|
|
const std::streampos GetSize() const;
|
2022-06-02 01:52:49 +02:00
|
|
|
|
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-12-01 22:47:39 +01:00
|
|
|
void Read(T& tValue)
|
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>
|
2022-12-01 22:47:39 +01:00
|
|
|
void Read(T& tValue, size_t nSize)
|
2022-06-02 01:52:49 +02:00
|
|
|
{
|
|
|
|
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-12-01 22:47:39 +01:00
|
|
|
T Read()
|
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-12-01 22:47:39 +01:00
|
|
|
void Write(T tValue)
|
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>
|
2022-12-01 22:47:39 +01:00
|
|
|
void Write(T tValue, size_t nSize)
|
2022-06-02 01:52:49 +02:00
|
|
|
{
|
|
|
|
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:
|
|
|
|
|
2022-12-01 22:47:39 +01:00
|
|
|
std::streampos m_nSize; // Size of ifstream.
|
|
|
|
Mode_t m_eCurrentMode; // Current active mode.
|
2022-06-02 01:52:49 +02:00
|
|
|
ofstream m_oStream; // Output file stream.
|
|
|
|
ifstream m_iStream; // Input file stream.
|
2021-12-25 22:36:38 +01:00
|
|
|
};
|