2021-12-25 22:36:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
enum class eStreamFileMode
|
|
|
|
{
|
|
|
|
NONE = 0,
|
|
|
|
READ,
|
|
|
|
WRITE
|
|
|
|
};
|
|
|
|
|
|
|
|
class CIOStream
|
|
|
|
{
|
2022-05-30 02:47:15 +02:00
|
|
|
ofstream writer; // Output file stream.
|
|
|
|
ifstream reader; // Input file stream.
|
|
|
|
string svFilePath; // Filepath.
|
|
|
|
eStreamFileMode eCurrentMode; // Current active mode.
|
2021-12-25 22:36:38 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
CIOStream();
|
2022-05-30 02:47:15 +02:00
|
|
|
CIOStream(const string& svFileFullPath, eStreamFileMode eMode);
|
2021-12-25 22:36:38 +01:00
|
|
|
~CIOStream();
|
|
|
|
|
2022-05-30 02:47:15 +02:00
|
|
|
bool Open(const string& svFileFullPath, eStreamFileMode eMode);
|
|
|
|
void Close();
|
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-05-30 02:47:15 +02:00
|
|
|
bool IsReadable();
|
|
|
|
bool IsWritable() const;
|
|
|
|
|
|
|
|
bool IsEof() const;
|
2021-12-25 22:36:38 +01:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: reads any value from the file (for strings use 'readString(...)' instead)
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
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())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-05-30 02:47:15 +02:00
|
|
|
reader.read(reinterpret_cast<char*>(&tValue), sizeof(tValue));
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: reads any value from the file and returns it (for strings use 'readString(...)' instead)
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
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-05-30 02:47:15 +02:00
|
|
|
IsReadable();
|
2021-12-25 22:36:38 +01:00
|
|
|
|
|
|
|
T value;
|
2022-05-30 02:47:15 +02:00
|
|
|
reader.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
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: writes any value to the file (for strings use 'writeString(...)' instead)
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
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-05-30 02:47:15 +02:00
|
|
|
writer.write(reinterpret_cast<const char*>(&tValue), sizeof(tValue));
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
2022-05-30 02:47:15 +02:00
|
|
|
void WriteString(string svInput);
|
2021-12-25 22:36:38 +01:00
|
|
|
};
|