CCommandLine improvements

* Make accessor inline.
* Add method 'AppendParametersFromFile' (will be used in a future init refactor).
This commit is contained in:
Kawe Mazidjatari 2023-06-30 21:59:16 +02:00
parent bb28e160cf
commit e9518d8ff1
2 changed files with 60 additions and 7 deletions

View File

@ -3,16 +3,60 @@
// Purpose: Command line utilities // Purpose: Command line utilities
// //
//=============================================================================// //=============================================================================//
#include "tier0/commandline.h" #include "tier0/commandline.h"
#include "tier1/cvar.h"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// Instance singleton and expose interface to rest of code // Purpose: parses a text file and appends its parameters/values
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
CCommandLine* CommandLine(void) void CCommandLine::AppendParametersFromFile(const char* const pszConfig)
{ {
return g_pCmdLine; FILE* const pFile = fopen(pszConfig, "r");
if (!pFile)
{
printf("%s: '%s' does not exist!\n",
__FUNCTION__, pszConfig);
return;
}
char szTextBuf[2048];
while (fgets(szTextBuf, sizeof(szTextBuf), pFile))
{
// Trim newlines...
size_t nLen = strlen(szTextBuf);
if (nLen > 0 && szTextBuf[nLen - 1] == '\n')
{
szTextBuf[--nLen] = '\0';
}
char* const pSpacePos = strchr(szTextBuf, ' ');
const char* pArgValue = "";
if (pSpacePos)
{
// Skip space and move to value.
*pSpacePos = '\0';
char* pArg = pSpacePos + 1;
// Trim the quotes around the value.
if (*pArg == '\"')
{
pArg++;
char* const pEndQuote = strchr(pArg, '\"');
if (pEndQuote)
{
*pEndQuote = '\0';
}
}
pArgValue = pArg;
}
CommandLine()->AppendParm(szTextBuf, pArgValue);
}
fclose(pFile);
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -3,6 +3,9 @@
class CCommandLine : public ICommandLine // VTABLE @0x141369C78 in R5pc_r5launch_N1094_CL456479_2019_10_30_05_20_PM class CCommandLine : public ICommandLine // VTABLE @0x141369C78 in R5pc_r5launch_N1094_CL456479_2019_10_30_05_20_PM
{ {
public:
void AppendParametersFromFile(const char* const pszConfig);
private: private:
enum enum
{ {
@ -17,7 +20,13 @@ private:
}; };
extern CCommandLine* g_pCmdLine; extern CCommandLine* g_pCmdLine;
CCommandLine* CommandLine(void); //-----------------------------------------------------------------------------
// Instance singleton and expose interface to rest of code
//-----------------------------------------------------------------------------
inline CCommandLine* CommandLine(void)
{
return g_pCmdLine;
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
class VCommandLine : public IDetour class VCommandLine : public IDetour