From 2ee9cce3937c83ebcbd01681bc3c53faca45b890 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 30 May 2022 02:47:15 +0200 Subject: [PATCH] Improve binary stream class --- r5dev/public/binstream.cpp | 85 ++++++++++++++++++++++++-------- r5dev/public/include/binstream.h | 44 +++++++++-------- 2 files changed, 89 insertions(+), 40 deletions(-) diff --git a/r5dev/public/binstream.cpp b/r5dev/public/binstream.cpp index 88e2734f..b0b3cf20 100644 --- a/r5dev/public/binstream.cpp +++ b/r5dev/public/binstream.cpp @@ -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()) != '\0') + string result = ""; + while (!reader.eof() && (c = read()) != '\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(svInput.c_str()); + size_t nSize = svInput.size(); - writer.write((const char*)text, size); + writer.write(reinterpret_cast(szText), nSize); } diff --git a/r5dev/public/include/binstream.h b/r5dev/public/include/binstream.h index 1607d97f..cf46d872 100644 --- a/r5dev/public/include/binstream.h +++ b/r5dev/public/include/binstream.h @@ -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 - 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(&tValue), sizeof(tValue)); } } @@ -42,27 +46,27 @@ public: // Purpose: reads any value from the file and returns it (for strings use 'readString(...)' instead) //----------------------------------------------------------------------------- template - 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(&value), sizeof(value)); return value; } - std::string readString(); + string ReadString(); //----------------------------------------------------------------------------- // Purpose: writes any value to the file (for strings use 'writeString(...)' instead) //----------------------------------------------------------------------------- template - 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(&tValue), sizeof(tValue)); } - void writeString(std::string str); + void WriteString(string svInput); };