2015-06-30 12:46:07 +03:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Globals initialization
|
|
|
|
*/
|
|
|
|
#ifndef HOOK_GAMEDLL
|
|
|
|
|
|
|
|
LessCtx_t g_LessCtx;
|
|
|
|
|
2016-02-23 02:13:52 +03:00
|
|
|
#endif
|
2015-06-30 12:46:07 +03:00
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
CUtlSymbolTable *CUtlSymbol::s_pSymbolTable = 0;
|
|
|
|
|
|
|
|
void CUtlSymbol::Initialize()
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
// necessary to allow us to create global symbols
|
2015-06-30 12:46:07 +03:00
|
|
|
static bool symbolsInitialized = false;
|
|
|
|
if (!symbolsInitialized)
|
|
|
|
{
|
|
|
|
s_pSymbolTable = new CUtlSymbolTable;
|
|
|
|
symbolsInitialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
CUtlSymbolTable *CUtlSymbol::CurrTable()
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
Initialize();
|
|
|
|
return s_pSymbolTable;
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
CUtlSymbol::CUtlSymbol(const char *pStr)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
m_Id = CurrTable()->AddString(pStr);
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
const char *CUtlSymbol::String() const
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
return CurrTable()->String(m_Id);
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
bool CUtlSymbol::operator==(const char *pStr) const
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
if (m_Id == UTL_INVAL_SYMBOL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return Q_strcmp(String(), pStr) == 0;
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
bool CUtlSymbolTable::SymLess(const unsigned int &i1, const unsigned int &i2)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
const char *str1 = g_LessCtx.m_pTable->String(i1);
|
|
|
|
const char *str2 = g_LessCtx.m_pTable->String(i2);
|
|
|
|
|
|
|
|
return Q_strcmp(str1, str2) < 0;
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
bool CUtlSymbolTable::SymLessi(const unsigned int &i1, const unsigned int &i2)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
const char *str1 = g_LessCtx.m_pTable->String(i1);
|
|
|
|
const char *str2 = g_LessCtx.m_pTable->String(i2);
|
|
|
|
|
|
|
|
return Q_stricmp(str1, str2) < 0;
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
CUtlSymbolTable::CUtlSymbolTable(int growSize, int initSize, bool caseInsensitive) :
|
|
|
|
m_Lookup(growSize, initSize, caseInsensitive ? SymLessi : SymLess),
|
|
|
|
m_Strings(256)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
;
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
CUtlSymbolTable::~CUtlSymbolTable()
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
// Release the stringpool string data
|
|
|
|
RemoveAll();
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
CUtlSymbol CUtlSymbolTable::Find(const char *pString) const
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
// Passing this special invalid symbol makes the comparison function
|
|
|
|
// use the string passed in the context
|
|
|
|
UtlSymId_t idx = m_Lookup.Find(UTL_INVAL_SYMBOL);
|
|
|
|
return CUtlSymbol(idx);
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
// Finds and/or creates a symbol based on the string
|
|
|
|
|
|
|
|
CUtlSymbol CUtlSymbolTable::AddString(const char *pString)
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
if (!pString)
|
|
|
|
return CUtlSymbol(UTL_INVAL_SYMBOL);
|
|
|
|
|
|
|
|
CUtlSymbol id = Find(pString);
|
|
|
|
|
|
|
|
if (id.IsValid())
|
|
|
|
return id;
|
|
|
|
|
|
|
|
int len = Q_strlen(pString) + 1;
|
|
|
|
int stridx = m_Strings.AddMultipleToTail(len);
|
|
|
|
|
|
|
|
UtlSymId_t idx = m_Lookup.Insert(stridx);
|
|
|
|
return CUtlSymbol(idx);
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
const char *CUtlSymbolTable::String(CUtlSymbol id) const
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
if (!id.IsValid())
|
|
|
|
return "";
|
|
|
|
|
|
|
|
assert(m_Lookup.IsValidIndex((UtlSymId_t)id));
|
|
|
|
return &m_Strings[id];
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-04 03:18:26 +03:00
|
|
|
void CUtlSymbolTable::RemoveAll()
|
2015-06-30 12:46:07 +03:00
|
|
|
{
|
2016-02-04 03:18:26 +03:00
|
|
|
m_Lookup.RemoveAll();
|
|
|
|
m_Strings.RemoveAll();
|
2015-06-30 12:46:07 +03:00
|
|
|
}
|