mirror of
https://github.com/Mauler125/r5sdk.git
synced 2025-02-09 19:15:03 +01:00
Improve binary stream class
This commit is contained in:
parent
b3583cf005
commit
2ee9cce393
@ -2,12 +2,16 @@
|
||||
#include "public/include/binstream.h"
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: CIOStream constructor
|
||||
// Purpose: CIOStream constructors
|
||||
//-----------------------------------------------------------------------------
|
||||
CIOStream::CIOStream()
|
||||
{
|
||||
eCurrentMode = eStreamFileMode::NONE;
|
||||
}
|
||||
CIOStream::CIOStream(const string& svFileFullPath, eStreamFileMode eMode)
|
||||
{
|
||||
Open(svFileFullPath, eMode);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: CIOStream destructor
|
||||
@ -30,7 +34,7 @@ CIOStream::~CIOStream()
|
||||
// Input : fileFullPath - mode
|
||||
// Output : true if operation is successfull
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CIOStream::open(std::string svFileFullPath, eStreamFileMode eMode)
|
||||
bool CIOStream::Open(const string& svFileFullPath, eStreamFileMode eMode)
|
||||
{
|
||||
svFilePath = svFileFullPath;
|
||||
|
||||
@ -47,7 +51,7 @@ bool CIOStream::open(std::string svFileFullPath, eStreamFileMode eMode)
|
||||
writer.open(svFilePath.c_str(), std::ios::binary);
|
||||
if (!writer.is_open())
|
||||
{
|
||||
DevMsg(eDLL_T::FS, "Error opening file '%s' for write operation!\n", svFilePath.c_str());
|
||||
Error(eDLL_T::FS, "Error opening file '%s' for write operation!\n", svFilePath.c_str());
|
||||
eCurrentMode = eStreamFileMode::NONE;
|
||||
}
|
||||
}
|
||||
@ -65,7 +69,7 @@ bool CIOStream::open(std::string svFileFullPath, eStreamFileMode eMode)
|
||||
reader.open(svFilePath.c_str(), std::ios::binary);
|
||||
if (!reader.is_open())
|
||||
{
|
||||
DevMsg(eDLL_T::FS, "Error opening file '%s' for read operation!\n", svFilePath.c_str());
|
||||
Error(eDLL_T::FS, "Error opening file '%s' for read operation!\n", svFilePath.c_str());
|
||||
eCurrentMode = eStreamFileMode::NONE;
|
||||
}
|
||||
}
|
||||
@ -75,9 +79,9 @@ bool CIOStream::open(std::string svFileFullPath, eStreamFileMode eMode)
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: closes the file
|
||||
// Purpose: closes the stream
|
||||
//-----------------------------------------------------------------------------
|
||||
void CIOStream::close()
|
||||
void CIOStream::Close()
|
||||
{
|
||||
if (eCurrentMode == eStreamFileMode::WRITE)
|
||||
{
|
||||
@ -90,9 +94,47 @@ void CIOStream::close()
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: checks if we are able to read the file
|
||||
// Purpose: gets the possition of the current character in the stream
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CIOStream::checkReadabilityStatus()
|
||||
size_t CIOStream::GetPosition()
|
||||
{
|
||||
switch (eCurrentMode)
|
||||
{
|
||||
case eStreamFileMode::READ:
|
||||
return reader.tellg();
|
||||
break;
|
||||
case eStreamFileMode::WRITE:
|
||||
return writer.tellp();
|
||||
break;
|
||||
default:
|
||||
return 0i64;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: sets the possition of the current character in the stream
|
||||
// Input : nOffset -
|
||||
//-----------------------------------------------------------------------------
|
||||
void CIOStream::SetPosition(int64_t nOffset)
|
||||
{
|
||||
switch (eCurrentMode)
|
||||
{
|
||||
case eStreamFileMode::READ:
|
||||
reader.seekg(nOffset);
|
||||
break;
|
||||
case eStreamFileMode::WRITE:
|
||||
writer.seekp(nOffset);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: checks if we are able to read the file
|
||||
// Output : true on success, false otherwise
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CIOStream::IsReadable()
|
||||
{
|
||||
if (eCurrentMode != eStreamFileMode::READ)
|
||||
{
|
||||
@ -113,8 +155,9 @@ bool CIOStream::checkReadabilityStatus()
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: checks if we are able to write to file
|
||||
// Output : true on success, false otherwise
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CIOStream::checkWritabilityStatus()
|
||||
bool CIOStream::IsWritable() const
|
||||
{
|
||||
if (eCurrentMode != eStreamFileMode::WRITE)
|
||||
{
|
||||
@ -126,22 +169,24 @@ bool CIOStream::checkWritabilityStatus()
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: checks if we hit the end of file
|
||||
// Output : true on success, false otherwise
|
||||
//-----------------------------------------------------------------------------
|
||||
bool CIOStream::eof()
|
||||
bool CIOStream::IsEof() const
|
||||
{
|
||||
return reader.eof();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: reads a string from the file and returns it
|
||||
// Output : string
|
||||
//-----------------------------------------------------------------------------
|
||||
std::string CIOStream::readString()
|
||||
string CIOStream::ReadString()
|
||||
{
|
||||
if (checkReadabilityStatus())
|
||||
if (IsReadable())
|
||||
{
|
||||
char c;
|
||||
std::string result = "";
|
||||
while (!reader.eof() && (c = readR<char>()) != '\0')
|
||||
string result = "";
|
||||
while (!reader.eof() && (c = read<char>()) != '\0')
|
||||
{
|
||||
result += c;
|
||||
}
|
||||
@ -154,17 +199,17 @@ std::string CIOStream::readString()
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: writes a string to the file
|
||||
//-----------------------------------------------------------------------------
|
||||
void CIOStream::writeString(std::string str)
|
||||
void CIOStream::WriteString(string svInput)
|
||||
{
|
||||
if (!checkWritabilityStatus())
|
||||
if (!IsWritable())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
str += '\0'; // null-terminate the string.
|
||||
svInput += '\0'; // null-terminate the string.
|
||||
|
||||
char* text = (char*)(str.c_str());
|
||||
size_t size = str.size();
|
||||
char* szText = const_cast<char*>(svInput.c_str());
|
||||
size_t nSize = svInput.size();
|
||||
|
||||
writer.write((const char*)text, size);
|
||||
writer.write(reinterpret_cast<const char*>(szText), nSize);
|
||||
}
|
||||
|
@ -9,32 +9,36 @@ enum class eStreamFileMode
|
||||
|
||||
class CIOStream
|
||||
{
|
||||
std::ofstream writer; // Output file stream.
|
||||
std::ifstream reader; // Input file stream.
|
||||
std::string svFilePath = ""; // Filepath.
|
||||
eStreamFileMode eCurrentMode = eStreamFileMode::NONE; // Current active mode.
|
||||
ofstream writer; // Output file stream.
|
||||
ifstream reader; // Input file stream.
|
||||
string svFilePath; // Filepath.
|
||||
eStreamFileMode eCurrentMode; // Current active mode.
|
||||
|
||||
public:
|
||||
CIOStream();
|
||||
CIOStream(const string& svFileFullPath, eStreamFileMode eMode);
|
||||
~CIOStream();
|
||||
|
||||
bool open(std::string fileFullPath, eStreamFileMode mode);
|
||||
void close();
|
||||
bool Open(const string& svFileFullPath, eStreamFileMode eMode);
|
||||
void Close();
|
||||
|
||||
bool checkWritabilityStatus();
|
||||
bool checkReadabilityStatus();
|
||||
size_t GetPosition();
|
||||
void SetPosition(int64_t nOffset);
|
||||
|
||||
bool eof();
|
||||
bool IsReadable();
|
||||
bool IsWritable() const;
|
||||
|
||||
bool IsEof() const;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: reads any value from the file (for strings use 'readString(...)' instead)
|
||||
//-----------------------------------------------------------------------------
|
||||
template<typename T>
|
||||
void read(T& value) // Template functions have to be in the header!
|
||||
void Read(T& tValue) // Template functions have to be in the header!
|
||||
{
|
||||
if (checkReadabilityStatus())
|
||||
if (IsReadable())
|
||||
{
|
||||
reader.read((char*)&value, sizeof(value));
|
||||
reader.read(reinterpret_cast<char*>(&tValue), sizeof(tValue));
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,27 +46,27 @@ public:
|
||||
// Purpose: reads any value from the file and returns it (for strings use 'readString(...)' instead)
|
||||
//-----------------------------------------------------------------------------
|
||||
template<typename T>
|
||||
T readR() // Template functions have to be in the header!
|
||||
T Read() // Template functions have to be in the header!
|
||||
{
|
||||
checkReadabilityStatus();
|
||||
IsReadable();
|
||||
|
||||
T value;
|
||||
reader.read((char*)&value, sizeof(value));
|
||||
reader.read(reinterpret_cast<char*>(&value), sizeof(value));
|
||||
return value;
|
||||
}
|
||||
std::string readString();
|
||||
string ReadString();
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Purpose: writes any value to the file (for strings use 'writeString(...)' instead)
|
||||
//-----------------------------------------------------------------------------
|
||||
template<typename T>
|
||||
void write(T& value) // Template functions have to be in the header!
|
||||
void Write(T tValue) // Template functions have to be in the header!
|
||||
{
|
||||
if (!checkWritabilityStatus())
|
||||
if (!IsWritable())
|
||||
{
|
||||
return;
|
||||
}
|
||||
writer.write((const char*)&value, sizeof(value));
|
||||
writer.write(reinterpret_cast<const char*>(&tValue), sizeof(tValue));
|
||||
}
|
||||
void writeString(std::string str);
|
||||
void WriteString(string svInput);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user