117 lines
3.1 KiB
C++
117 lines
3.1 KiB
C++
#include "convar.hpp"
|
|
|
|
inline const char** CCommand::ArgV() const {
|
|
return ArgC() ? (const char**)m_Args.Base() : NULL;
|
|
}
|
|
|
|
inline const char* CCommand::ArgS() const {
|
|
return m_nArgv0Size ? (m_ArgSBuffer.Base() + m_nArgv0Size) : "";
|
|
}
|
|
|
|
inline const char* CCommand::operator[](int nIndex) const {
|
|
return Arg(nIndex);
|
|
}
|
|
static characterset_t s_BreakSet;
|
|
static bool s_bBuiltBreakSet = false;
|
|
void CharacterSetBuild(characterset_t* pSetBuffer, const char* pszSetString) {
|
|
int i = 0;
|
|
|
|
// Test our pointers
|
|
if (!pSetBuffer || !pszSetString) return;
|
|
|
|
memset(pSetBuffer->set, 0, sizeof(pSetBuffer->set));
|
|
|
|
while (pszSetString[i]) {
|
|
pSetBuffer->set[static_cast<size_t>(pszSetString[i])] = 1;
|
|
i++;
|
|
}
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Tokenizer class
|
|
//-----------------------------------------------------------------------------
|
|
CCommand::CCommand() {
|
|
if (!s_bBuiltBreakSet) {
|
|
s_bBuiltBreakSet = true;
|
|
CharacterSetBuild(&s_BreakSet, "{}()':");
|
|
}
|
|
|
|
Reset();
|
|
}
|
|
|
|
CCommand::CCommand(int nArgC, const char** ppArgV) {
|
|
Assert(nArgC > 0);
|
|
|
|
if (!s_bBuiltBreakSet) {
|
|
s_bBuiltBreakSet = true;
|
|
CharacterSetBuild(&s_BreakSet, "{}()':");
|
|
}
|
|
|
|
Reset();
|
|
|
|
char* pBuf = m_ArgvBuffer.Base();
|
|
char* pSBuf = m_ArgSBuffer.Base();
|
|
for (int i = 0; i < nArgC; ++i) {
|
|
m_Args.AddToTail(pBuf);
|
|
int nLen = strlen(ppArgV[i]);
|
|
memcpy(pBuf, ppArgV[i], nLen + 1);
|
|
if (i == 0) {
|
|
m_nArgv0Size = nLen;
|
|
}
|
|
pBuf += nLen + 1;
|
|
|
|
bool bContainsSpace = strchr(ppArgV[i], ' ') != NULL;
|
|
if (bContainsSpace) {
|
|
*pSBuf++ = '\"';
|
|
}
|
|
memcpy(pSBuf, ppArgV[i], nLen);
|
|
pSBuf += nLen;
|
|
if (bContainsSpace) {
|
|
*pSBuf++ = '\"';
|
|
}
|
|
|
|
if (i != nArgC - 1) {
|
|
*pSBuf++ = ' ';
|
|
}
|
|
}
|
|
}
|
|
bool CCommand::Tokenize(const char* pCommand, characterset_t* pBreakSet) {
|
|
__debugbreak();
|
|
return false;
|
|
}
|
|
void CCommand::Reset() {
|
|
m_nArgv0Size = 0;
|
|
m_ArgSBuffer.RemoveAll();
|
|
m_ArgvBuffer.RemoveAll();
|
|
m_Args.RemoveAll();
|
|
}
|
|
|
|
characterset_t* CCommand::DefaultBreakSet() { return &s_BreakSet; }
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Helper function to parse arguments to commands.
|
|
//-----------------------------------------------------------------------------
|
|
int CCommand::FindArg(const char* pName) const {
|
|
int nArgC = ArgC();
|
|
for (int i = 1; i < nArgC; i++) {
|
|
if (!_stricmp(Arg(i), pName)) return (i + 1) < nArgC ? i + 1 : -1;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int CCommand::FindArgInt(const char* pName, int nDefaultVal) const {
|
|
int idx = FindArg(pName);
|
|
if (idx != -1)
|
|
return atoi(m_Args[idx]);
|
|
else
|
|
return nDefaultVal;
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
// Default console command autocompletion function
|
|
//-----------------------------------------------------------------------------
|
|
int DefaultCompletionFunc(const char* partial,
|
|
CUtlVector<CUtlString>& commands) {
|
|
return 0;
|
|
}
|