From 89ff792dd39ee31fdd9c1a73735b222aa52110db Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Mon, 29 Jan 2024 00:36:26 +0100 Subject: [PATCH] IOStream: improve seeking logic Instead of seeking to the very start again when GetSize() is called, seek back to the original position. Also added dedicated methods for seeking put or get instead of using the mode since this is a fstream --- src/public/tier0/binstream.h | 14 +++++---- src/tier0/binstream.cpp | 57 +++++++++++++++++------------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/public/tier0/binstream.h b/src/public/tier0/binstream.h index 7d92e5d6..4ab81cc8 100644 --- a/src/public/tier0/binstream.h +++ b/src/public/tier0/binstream.h @@ -21,8 +21,12 @@ public: void ComputeFileSize(); - std::streampos GetPosition(Mode_t mode); - void SetPosition(std::streampos nOffset, Mode_t mode); + std::streampos TellGet(); + std::streampos TellPut(); + + void SeekGet(const std::streampos nOffset); + void SeekPut(const std::streampos nOffset); + void Seek(const std::streampos nOffset); const std::filebuf* GetData() const; const std::streampos GetSize() const; @@ -71,7 +75,7 @@ public: m_Stream.read(reinterpret_cast(&value), sizeof(value)); return value; } - bool ReadString(string& svOut); + bool ReadString(std::string& svOut); //----------------------------------------------------------------------------- // Purpose: writes any value to the file @@ -98,10 +102,10 @@ public: m_Stream.write(reinterpret_cast(tValue), nSize); m_nSize += nSize; } - bool WriteString(const string& svInput); + bool WriteString(const std::string& svInput); private: std::streampos m_nSize; // File size. int m_nFlags; // Stream flags. - fstream m_Stream; // I/O stream. + std::fstream m_Stream; // I/O stream. }; diff --git a/src/tier0/binstream.cpp b/src/tier0/binstream.cpp index 405a5c96..e0f41f37 100644 --- a/src/tier0/binstream.cpp +++ b/src/tier0/binstream.cpp @@ -75,10 +75,14 @@ void CIOStream::Flush() //----------------------------------------------------------------------------- void CIOStream::ComputeFileSize() { - m_nSize = m_Stream.tellg(); + const std::streampos currentPos = m_Stream.tellg(); + m_nSize = currentPos; + m_Stream.seekg(0, std::ios::end); m_nSize = m_Stream.tellg() - m_nSize; - m_Stream.seekg(0, std::ios::beg); + + // Restore to original position. + m_Stream.seekg(currentPos, std::ios::beg); m_Stream.clear(); } @@ -87,19 +91,13 @@ void CIOStream::ComputeFileSize() // Input : mode - // Output : std::streampos //----------------------------------------------------------------------------- -std::streampos CIOStream::GetPosition(Mode_t mode) +std::streampos CIOStream::TellGet() { - switch (mode) - { - case Mode_t::READ: - return m_Stream.tellg(); - break; - case Mode_t::WRITE: - return m_Stream.tellp(); - break; - default: - return static_cast(NULL); - } + return m_Stream.tellg(); +} +std::streampos CIOStream::TellPut() +{ + return m_Stream.tellp(); } //----------------------------------------------------------------------------- @@ -107,19 +105,18 @@ std::streampos CIOStream::GetPosition(Mode_t mode) // Input : nOffset - // mode - //----------------------------------------------------------------------------- -void CIOStream::SetPosition(std::streampos nOffset, Mode_t mode) +void CIOStream::SeekGet(const std::streampos nOffset) { - switch (mode) - { - case Mode_t::READ: - m_Stream.seekg(nOffset); - break; - case Mode_t::WRITE: - m_Stream.seekp(nOffset); - break; - default: - break; - } + m_Stream.seekg(nOffset, std::ios::beg); +} +void CIOStream::SeekPut(const std::streampos nOffset) +{ + m_Stream.seekp(nOffset, std::ios::beg); +} +void CIOStream::Seek(const std::streampos nOffset) +{ + SeekGet(nOffset); + SeekPut(nOffset); } //----------------------------------------------------------------------------- @@ -178,7 +175,7 @@ bool CIOStream::IsEof() const // Input : &svOut - // Output : true on success, false otherwise //----------------------------------------------------------------------------- -bool CIOStream::ReadString(string& svOut) +bool CIOStream::ReadString(std::string& svOut) { if (IsReadable()) { @@ -197,13 +194,13 @@ bool CIOStream::ReadString(string& svOut) // Input : &svInput - // Output : true on success, false otherwise //----------------------------------------------------------------------------- -bool CIOStream::WriteString(const string& svInput) +bool CIOStream::WriteString(const std::string& svInput) { if (!IsWritable()) return false; - const char* szText = svInput.c_str(); - size_t nSize = svInput.size(); + const char* const szText = svInput.c_str(); + const size_t nSize = svInput.size(); m_Stream.write(szText, nSize); m_nSize += nSize;