Compute the file size properly

This commit is contained in:
Kawe Mazidjatari 2022-12-02 00:25:28 +01:00
parent 30e360e3b7
commit 053f284029
2 changed files with 16 additions and 4 deletions

View File

@ -40,16 +40,14 @@ bool CIOStream::Open(const fs::path& fsFilePath, Mode_t eMode)
{ {
m_iStream.close(); m_iStream.close();
} }
m_iStream.open(fsFilePath, std::ios::binary | std::ios::in || std::ios::ate); m_iStream.open(fsFilePath, std::ios::binary | std::ios::in);
if (!m_iStream.is_open() || !m_iStream.good()) if (!m_iStream.is_open() || !m_iStream.good())
{ {
m_eCurrentMode = Mode_t::NONE; m_eCurrentMode = Mode_t::NONE;
return false; return false;
} }
m_nSize = m_iStream.tellg(); ComputeFileSize();
m_iStream.seekg(0, std::ios::beg);
return true; return true;
case Mode_t::WRITE: case Mode_t::WRITE:
@ -96,6 +94,18 @@ void CIOStream::Flush()
m_oStream.flush(); m_oStream.flush();
} }
//-----------------------------------------------------------------------------
// Purpose: computes the input file size
//-----------------------------------------------------------------------------
void CIOStream::ComputeFileSize()
{
m_nSize = m_iStream.tellg();
m_iStream.seekg(0, std::ios::end);
m_nSize = m_iStream.tellg() - m_nSize;
m_iStream.seekg(0, std::ios::beg);
m_iStream.clear();
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Purpose: gets the position of the current character in the stream // Purpose: gets the position of the current character in the stream
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -18,6 +18,8 @@ public:
void Close(); void Close();
void Flush(); void Flush();
void ComputeFileSize();
std::streampos GetPosition(); std::streampos GetPosition();
void SetPosition(std::streampos nOffset); void SetPosition(std::streampos nOffset);