New adt_array functions: CloneArray, FindStringInArray, FindCellInArray

Adjusted mapchooser/randomcycle/rockthevote and LoadMaps stock to use new array functions

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401670
This commit is contained in:
Michael McKoy
2007-11-09 23:46:05 +00:00
parent b3fa7316d5
commit daaf961c07
7 changed files with 165 additions and 66 deletions
+11
View File
@@ -138,6 +138,17 @@ public:
m_Size = count;
return true;
}
CellArray *clone()
{
CellArray *array = new CellArray(m_BlockSize);
array->m_AllocSize = m_AllocSize;
array->m_Size = m_Size;
array->m_Data = (cell_t *)malloc(sizeof(cell_t) * m_BlockSize * m_AllocSize);
memcpy(array->m_Data, m_Data, sizeof(cell_t) * m_BlockSize * m_Size);
return array;
}
private:
bool GrowIfNeeded(size_t count)
{
+76
View File
@@ -484,6 +484,79 @@ static cell_t SwapArrayItems(IPluginContext *pContext, const cell_t *params)
return 1;
}
static cell_t CloneArray(IPluginContext *pContext, const cell_t *params)
{
CellArray *oldArray;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&oldArray))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
CellArray *array = oldArray->clone();
Handle_t hndl = g_HandleSys.CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
if (!hndl)
{
delete array;
}
return hndl;
}
static cell_t FindStringInArray(IPluginContext *pContext, const cell_t *params)
{
CellArray *array;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
char *str;
pContext->LocalToString(params[2], &str);
for (unsigned int i = 0; i < array->size(); i++)
{
const char *array_str = (const char *)array->at(i);
if (strcmp(str, array_str) == 0)
{
return (cell_t) i;
}
}
return -1;
}
static cell_t FindValueInArray(IPluginContext *pContext, const cell_t *params)
{
CellArray *array;
HandleError err;
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
}
for (unsigned int i = 0; i < array->size(); i++)
{
if (params[2] == *array->at(i))
{
return (cell_t) i;
}
}
return -1;
}
REGISTER_NATIVES(cellArrayNatives)
{
{"ClearArray", ClearArray},
@@ -502,5 +575,8 @@ REGISTER_NATIVES(cellArrayNatives)
{"SetArrayArray", SetArrayArray},
{"ShiftArrayUp", ShiftArrayUp},
{"SwapArrayItems", SwapArrayItems},
{"CloneArray", CloneArray},
{"FindStringInArray", FindStringInArray},
{"FindValueInArray", FindValueInArray},
{NULL, NULL},
};