2021-12-25 22:36:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
class CIOStream
|
|
|
|
{
|
|
|
|
public:
|
2023-02-04 00:36:05 +01:00
|
|
|
enum Mode_t
|
2022-06-02 01:52:49 +02:00
|
|
|
{
|
|
|
|
NONE = 0,
|
2023-02-04 00:36:05 +01:00
|
|
|
READ = std::ios::in,
|
|
|
|
WRITE = std::ios::out,
|
|
|
|
BINARY = std::ios::binary,
|
2022-06-02 01:52:49 +02:00
|
|
|
};
|
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
CIOStream();
|
|
|
|
~CIOStream();
|
|
|
|
|
2024-02-03 23:34:14 +01:00
|
|
|
bool Open(const char* const pFilePath, const int nFlags);
|
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
|
|
|
|
2024-01-29 00:36:26 +01:00
|
|
|
std::streampos TellGet();
|
|
|
|
std::streampos TellPut();
|
|
|
|
|
|
|
|
void SeekGet(const std::streampos nOffset);
|
|
|
|
void SeekPut(const std::streampos nOffset);
|
|
|
|
void Seek(const 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-12-02 22:31:42 +01:00
|
|
|
m_Stream.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>
|
2024-01-27 01:41:34 +01:00
|
|
|
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)
|
2022-06-02 01:52:49 +02:00
|
|
|
{
|
|
|
|
if (IsReadable())
|
2022-12-02 22:31:42 +01:00
|
|
|
m_Stream.read(reinterpret_cast<char*>(&tValue), nSize);
|
2022-06-02 01:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// 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-12-02 22:31:42 +01:00
|
|
|
m_Stream.read(reinterpret_cast<char*>(&value), sizeof(value));
|
2021-12-25 22:36:38 +01:00
|
|
|
return value;
|
|
|
|
}
|
2024-01-29 00:36:26 +01:00
|
|
|
bool ReadString(std::string& svOut);
|
2024-02-03 23:34:14 +01:00
|
|
|
bool ReadString(char* const pBuf, const size_t nLen);
|
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
|
|
|
|
2022-12-02 22:31:42 +01:00
|
|
|
m_Stream.write(reinterpret_cast<const char*>(&tValue), sizeof(tValue));
|
2023-02-04 00:36:05 +01:00
|
|
|
m_nSize += sizeof(tValue);
|
2022-06-02 01:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: writes any value to the file with specified size
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
2024-01-27 01:41:34 +01:00
|
|
|
void Write(T* tValue, size_t nSize)
|
2022-06-02 01:52:49 +02:00
|
|
|
{
|
|
|
|
if (!IsWritable())
|
|
|
|
return;
|
|
|
|
|
2022-12-02 22:31:42 +01:00
|
|
|
m_Stream.write(reinterpret_cast<const char*>(tValue), nSize);
|
2023-02-04 00:36:05 +01:00
|
|
|
m_nSize += nSize;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
2024-01-29 00:36:26 +01:00
|
|
|
bool WriteString(const std::string& svInput);
|
2022-06-02 01:52:49 +02:00
|
|
|
|
|
|
|
private:
|
2023-02-04 00:36:05 +01:00
|
|
|
std::streampos m_nSize; // File size.
|
|
|
|
int m_nFlags; // Stream flags.
|
2024-01-29 00:36:26 +01:00
|
|
|
std::fstream m_Stream; // I/O stream.
|
2021-12-25 22:36:38 +01:00
|
|
|
};
|