2021-12-25 22:36:38 +01:00
|
|
|
#include "core/stdafx.h"
|
2022-08-09 17:18:07 +02:00
|
|
|
#include "public/utility/binstream.h"
|
2021-12-25 22:36:38 +01:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-05-30 02:47:15 +02:00
|
|
|
// Purpose: CIOStream constructors
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
CIOStream::CIOStream()
|
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
m_eCurrentMode = Mode_t::NONE;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
2022-06-24 16:56:28 +02:00
|
|
|
CIOStream::CIOStream(const fs::path& svFileFullPath, Mode_t eMode)
|
2022-05-30 02:47:15 +02:00
|
|
|
{
|
|
|
|
Open(svFileFullPath, eMode);
|
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: CIOStream destructor
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
CIOStream::~CIOStream()
|
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
if (m_oStream.is_open())
|
|
|
|
m_oStream.close();
|
|
|
|
if (m_iStream.is_open())
|
|
|
|
m_iStream.close();
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: opens the file in specified mode
|
|
|
|
// Input : fileFullPath - mode
|
2022-09-09 19:47:31 +02:00
|
|
|
// Output : true if operation is successful
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-06-24 16:56:28 +02:00
|
|
|
bool CIOStream::Open(const fs::path& fsFilePath, Mode_t eMode)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
m_eCurrentMode = eMode;
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-06-02 01:52:49 +02:00
|
|
|
switch (m_eCurrentMode)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
case Mode_t::READ:
|
|
|
|
if (m_iStream.is_open())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
m_iStream.close();
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
2022-12-02 00:25:28 +01:00
|
|
|
m_iStream.open(fsFilePath, std::ios::binary | std::ios::in);
|
2022-06-05 17:28:39 +02:00
|
|
|
if (!m_iStream.is_open() || !m_iStream.good())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
m_eCurrentMode = Mode_t::NONE;
|
|
|
|
return false;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
2022-12-01 22:47:39 +01:00
|
|
|
|
2022-12-02 00:25:28 +01:00
|
|
|
ComputeFileSize();
|
2022-06-02 01:52:49 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
case Mode_t::WRITE:
|
|
|
|
if (m_oStream.is_open())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
m_oStream.close();
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
2022-06-24 16:56:28 +02:00
|
|
|
m_oStream.open(fsFilePath, std::ios::binary | std::ios::out);
|
2022-06-05 17:28:39 +02:00
|
|
|
if (!m_oStream.is_open() || !m_oStream.good())
|
2022-06-02 01:52:49 +02:00
|
|
|
{
|
|
|
|
m_eCurrentMode = Mode_t::NONE;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-06-02 01:52:49 +02:00
|
|
|
default:
|
|
|
|
m_eCurrentMode = Mode_t::NONE;
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-05-30 02:47:15 +02:00
|
|
|
// Purpose: closes the stream
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-05-30 02:47:15 +02:00
|
|
|
void CIOStream::Close()
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
switch (m_eCurrentMode)
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
case Mode_t::READ:
|
|
|
|
m_iStream.close();
|
|
|
|
return;
|
|
|
|
case Mode_t::WRITE:
|
|
|
|
m_oStream.close();
|
|
|
|
return;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-04 13:55:24 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: flushes the ofstream
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void CIOStream::Flush()
|
|
|
|
{
|
|
|
|
if (IsWritable())
|
|
|
|
m_oStream.flush();
|
|
|
|
}
|
|
|
|
|
2022-12-02 00:25:28 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2022-05-30 02:47:15 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-09-09 19:47:31 +02:00
|
|
|
// Purpose: gets the position of the current character in the stream
|
2022-05-30 02:47:15 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-12-01 22:47:39 +01:00
|
|
|
std::streampos CIOStream::GetPosition()
|
2022-05-30 02:47:15 +02:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
switch (m_eCurrentMode)
|
2022-05-30 02:47:15 +02:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
case Mode_t::READ:
|
|
|
|
return m_iStream.tellg();
|
2022-05-30 02:47:15 +02:00
|
|
|
break;
|
2022-06-02 01:52:49 +02:00
|
|
|
case Mode_t::WRITE:
|
|
|
|
return m_oStream.tellp();
|
2022-05-30 02:47:15 +02:00
|
|
|
break;
|
|
|
|
default:
|
2022-12-01 22:47:39 +01:00
|
|
|
return static_cast<std::streampos>(NULL);
|
2022-05-30 02:47:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-09-09 19:47:31 +02:00
|
|
|
// Purpose: sets the position of the current character in the stream
|
2022-05-30 02:47:15 +02:00
|
|
|
// Input : nOffset -
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-12-01 22:47:39 +01:00
|
|
|
void CIOStream::SetPosition(std::streampos nOffset)
|
2022-05-30 02:47:15 +02:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
switch (m_eCurrentMode)
|
2022-05-30 02:47:15 +02:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
case Mode_t::READ:
|
|
|
|
m_iStream.seekg(nOffset);
|
2022-05-30 02:47:15 +02:00
|
|
|
break;
|
2022-06-02 01:52:49 +02:00
|
|
|
case Mode_t::WRITE:
|
|
|
|
m_oStream.seekp(nOffset);
|
2022-05-30 02:47:15 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 01:52:49 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: returns the data (ifstream only)
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-12-01 22:47:39 +01:00
|
|
|
const std::filebuf* CIOStream::GetData() const
|
2022-06-02 01:52:49 +02:00
|
|
|
{
|
2022-12-01 22:47:39 +01:00
|
|
|
return m_iStream.rdbuf();
|
2022-06-02 01:52:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: returns the data size (ifstream only)
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-12-01 22:47:39 +01:00
|
|
|
const std::streampos CIOStream::GetSize() const
|
2022-06-02 01:52:49 +02:00
|
|
|
{
|
2022-12-01 22:47:39 +01:00
|
|
|
return m_nSize;
|
2022-06-02 01:52:49 +02:00
|
|
|
}
|
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: checks if we are able to read the file
|
2022-05-30 02:47:15 +02:00
|
|
|
// Output : true on success, false otherwise
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-05-30 02:47:15 +02:00
|
|
|
bool CIOStream::IsReadable()
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
if (m_eCurrentMode != Mode_t::READ)
|
2021-12-25 22:36:38 +01:00
|
|
|
return false;
|
|
|
|
|
2022-06-06 23:08:53 +02:00
|
|
|
if (!m_iStream)
|
|
|
|
return false;
|
|
|
|
|
2022-06-02 01:52:49 +02:00
|
|
|
if (m_iStream.eof())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
m_iStream.close();
|
|
|
|
m_eCurrentMode = Mode_t::NONE;
|
2021-12-25 22:36:38 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-06 23:08:53 +02:00
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: checks if we are able to write to file
|
2022-05-30 02:47:15 +02:00
|
|
|
// Output : true on success, false otherwise
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-05-30 02:47:15 +02:00
|
|
|
bool CIOStream::IsWritable() const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
if (m_eCurrentMode != Mode_t::WRITE)
|
2021-12-25 22:36:38 +01:00
|
|
|
return false;
|
2022-06-02 01:52:49 +02:00
|
|
|
|
2022-06-06 23:08:53 +02:00
|
|
|
if (!m_oStream)
|
|
|
|
return false;
|
|
|
|
|
2021-12-25 22:36:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: checks if we hit the end of file
|
2022-05-30 02:47:15 +02:00
|
|
|
// Output : true on success, false otherwise
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-05-30 02:47:15 +02:00
|
|
|
bool CIOStream::IsEof() const
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-06-02 01:52:49 +02:00
|
|
|
return m_iStream.eof();
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: reads a string from the file and returns it
|
2022-05-30 02:47:15 +02:00
|
|
|
// Output : string
|
2021-12-25 22:36:38 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-05-30 02:47:15 +02:00
|
|
|
string CIOStream::ReadString()
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
2022-12-01 22:47:39 +01:00
|
|
|
string result;
|
|
|
|
|
2022-05-30 02:47:15 +02:00
|
|
|
if (IsReadable())
|
2021-12-25 22:36:38 +01:00
|
|
|
{
|
|
|
|
char c;
|
2022-06-02 01:52:49 +02:00
|
|
|
while (!m_iStream.eof() && (c = Read<char>()) != '\0')
|
2021-12-25 22:36:38 +01:00
|
|
|
result += c;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2022-12-01 22:47:39 +01:00
|
|
|
|
|
|
|
return result;
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Purpose: writes a string to the file
|
|
|
|
//-----------------------------------------------------------------------------
|
2022-05-30 02:47:15 +02:00
|
|
|
void CIOStream::WriteString(string svInput)
|
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
|
|
|
svInput += '\0'; // null-terminate the string.
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-12-01 22:47:39 +01:00
|
|
|
const char* szText = svInput.c_str();
|
2022-05-30 02:47:15 +02:00
|
|
|
size_t nSize = svInput.size();
|
2021-12-25 22:36:38 +01:00
|
|
|
|
2022-12-01 22:47:39 +01:00
|
|
|
m_oStream.write(szText, nSize);
|
2021-12-25 22:36:38 +01:00
|
|
|
}
|