Tier0: iostream light cleanup

- Take const char* directly instead of making extraneous copies
- Add overload for reading strings into fixed size buffers
This commit is contained in:
Kawe Mazidjatari 2024-02-03 23:34:14 +01:00
parent 2ed42296db
commit b89f72ac36
3 changed files with 66 additions and 37 deletions

View File

@ -12,15 +12,12 @@ public:
};
CIOStream();
CIOStream(const fs::path& fsFileFullPath, int nFlags);
~CIOStream();
bool Open(const fs::path& fsFileFullPath, int nFlags);
bool Open(const char* const pFilePath, const int nFlags);
void Close();
void Flush();
void ComputeFileSize();
std::streampos TellGet();
std::streampos TellPut();
@ -76,6 +73,7 @@ public:
return value;
}
bool ReadString(std::string& svOut);
bool ReadString(char* const pBuf, const size_t nLen);
//-----------------------------------------------------------------------------
// Purpose: writes any value to the file

View File

@ -4,6 +4,7 @@
//
//=============================================================================//
#include "tier0/binstream.h"
#include "tier1/fmtstr.h"
#include "basepanel.h"
#include "sdklauncher.h"
@ -228,16 +229,25 @@ bool CLauncher::CreateLaunchContext(eLaunchMode lMode, uint64_t nProcessorAffini
void CLauncher::SetupLaunchContext(const char* szConfig, const char* szGameDll, const char* szCommandLine)
{
CIOStream cfgFile;
string commandLine;
CFmtStrMax commandLine;
if (szConfig && szConfig[0])
{
if (cfgFile.Open(Format(GAME_CFG_PATH"%s", szConfig), CIOStream::READ))
commandLine.Format(GAME_CFG_PATH"%s", szConfig);
if (cfgFile.Open(commandLine.String(), CIOStream::READ))
{
if (!cfgFile.ReadString(commandLine))
// Reuse the stack string for the actual command line buffer.
commandLine.Clear();
if (!cfgFile.ReadString(commandLine.Access(), commandLine.GetMaxLength()))
{
AddLog(spdlog::level::level_enum::err, "Failed to read file '%s'!\n", szConfig);
}
else
{
commandLine.SetLength(strlen(commandLine.String()));
}
}
else // Failed to open config file.
{
@ -247,18 +257,18 @@ void CLauncher::SetupLaunchContext(const char* szConfig, const char* szGameDll,
if (szCommandLine && szCommandLine[0])
{
commandLine.append(szCommandLine);
commandLine.Append(szCommandLine);
}
m_svGameDll = Format("%s\\%s", m_svCurrentDir.c_str(), szGameDll);
m_svCmdLine = Format("%s\\%s %s", m_svCurrentDir.c_str(), szGameDll, commandLine.c_str());
m_svCmdLine = Format("%s\\%s %s", m_svCurrentDir.c_str(), szGameDll, commandLine.String());
///////////////////////////////////////////////////////////////////////////
// Print the file paths and arguments.
std::cout << "----------------------------------------------------------------------------------------------------------------------" << std::endl;
AddLog(spdlog::level::level_enum::debug, "- CWD: %s\n", m_svCurrentDir.c_str());
AddLog(spdlog::level::level_enum::debug, "- EXE: %s\n", m_svGameDll.c_str());
AddLog(spdlog::level::level_enum::debug, "- CLI: %s\n", commandLine.c_str());
AddLog(spdlog::level::level_enum::debug, "- CLI: %s\n", commandLine.String());
std::cout << "----------------------------------------------------------------------------------------------------------------------" << std::endl;
}

View File

@ -1,4 +1,5 @@
#include "tier0/binstream.h"
#include <sys/stat.h>
//-----------------------------------------------------------------------------
// Purpose: CIOStream constructors
@ -8,10 +9,6 @@ CIOStream::CIOStream()
m_nSize = 0;
m_nFlags = Mode_t::NONE;
}
CIOStream::CIOStream(const fs::path& svFileFullPath, int nFlags)
{
Open(svFileFullPath, nFlags);
}
//-----------------------------------------------------------------------------
// Purpose: CIOStream destructor
@ -26,11 +23,11 @@ CIOStream::~CIOStream()
//-----------------------------------------------------------------------------
// Purpose: opens the file in specified mode
// Input : &fsFilePath -
// Input : *pFilePath -
// nFlags -
// Output : true if operation is successful
//-----------------------------------------------------------------------------
bool CIOStream::Open(const fs::path& fsFilePath, int nFlags)
bool CIOStream::Open(const char* const pFilePath, const int nFlags)
{
m_nFlags = nFlags;
@ -38,7 +35,7 @@ bool CIOStream::Open(const fs::path& fsFilePath, int nFlags)
{
m_Stream.close();
}
m_Stream.open(fsFilePath, nFlags);
m_Stream.open(pFilePath, nFlags);
if (!m_Stream.is_open() || !m_Stream.good())
{
m_nFlags = Mode_t::NONE;
@ -47,7 +44,13 @@ bool CIOStream::Open(const fs::path& fsFilePath, int nFlags)
if (nFlags & Mode_t::READ)
{
ComputeFileSize();
struct _stat64 status;
if (_stat64(pFilePath, &status) != NULL)
{
return false;
}
m_nSize = status.st_size;
}
return true;
@ -70,22 +73,6 @@ void CIOStream::Flush()
m_Stream.flush();
}
//-----------------------------------------------------------------------------
// Purpose: computes the input file size
//-----------------------------------------------------------------------------
void CIOStream::ComputeFileSize()
{
const std::streampos currentPos = m_Stream.tellg();
m_nSize = currentPos;
m_Stream.seekg(0, std::ios::end);
m_nSize = m_Stream.tellg() - m_nSize;
// Restore to original position.
m_Stream.seekg(currentPos, std::ios::beg);
m_Stream.clear();
}
//-----------------------------------------------------------------------------
// Purpose: gets the position of the current character in the stream
// Input : mode -
@ -171,7 +158,7 @@ bool CIOStream::IsEof() const
}
//-----------------------------------------------------------------------------
// Purpose: reads a string from the file and returns it
// Purpose: reads a string from the file
// Input : &svOut -
// Output : true on success, false otherwise
//-----------------------------------------------------------------------------
@ -179,9 +166,43 @@ bool CIOStream::ReadString(std::string& svOut)
{
if (IsReadable())
{
char c;
while (!m_Stream.eof() && (c = Read<char>()) != '\0')
while (!m_Stream.eof())
{
const char c = Read<char>();
if (c == '\0')
break;
svOut += c;
}
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: reads a string from the file into a fixed size buffer
// Input : *pBuf -
// nLen -
// Output : true on success, false otherwise
//-----------------------------------------------------------------------------
bool CIOStream::ReadString(char* const pBuf, const size_t nLen)
{
if (IsReadable())
{
size_t i = 0;
while (i < nLen && !m_Stream.eof())
{
const char c = Read<char>();
if (c == '\0')
break;
pBuf[i++] = c;
}
return true;
}