core/smn_adt_array.cpp
changeset 1664 e054be79e28a
parent 1330 ab94316bb52b
child 1766 d30c252089be
     1.1 --- a/core/smn_adt_array.cpp	Fri Nov 09 18:08:09 2007 +0000
     1.2 +++ b/core/smn_adt_array.cpp	Fri Nov 09 23:46:05 2007 +0000
     1.3 @@ -484,6 +484,79 @@
     1.4  	return 1;
     1.5  }
     1.6  
     1.7 +static cell_t CloneArray(IPluginContext *pContext, const cell_t *params)
     1.8 +{
     1.9 +	CellArray *oldArray;
    1.10 +	HandleError err;
    1.11 +	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
    1.12 +
    1.13 +	if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&oldArray)) 
    1.14 +		!= HandleError_None)
    1.15 +	{
    1.16 +		return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
    1.17 +	}
    1.18 +
    1.19 +	CellArray *array = oldArray->clone();
    1.20 +	
    1.21 +	Handle_t hndl = g_HandleSys.CreateHandle(htCellArray, array, pContext->GetIdentity(), g_pCoreIdent, NULL);
    1.22 +	if (!hndl)
    1.23 +	{
    1.24 +		delete array;
    1.25 +	}
    1.26 +
    1.27 +	return hndl;
    1.28 +}
    1.29 +
    1.30 +static cell_t FindStringInArray(IPluginContext *pContext, const cell_t *params)
    1.31 +{
    1.32 +	CellArray *array;
    1.33 +	HandleError err;
    1.34 +	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
    1.35 +
    1.36 +	if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array)) 
    1.37 +		!= HandleError_None)
    1.38 +	{
    1.39 +		return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
    1.40 +	}
    1.41 +
    1.42 +	char *str;
    1.43 +	pContext->LocalToString(params[2], &str);
    1.44 +
    1.45 +	for (unsigned int i = 0; i < array->size(); i++)
    1.46 +	{
    1.47 +		const char *array_str = (const char *)array->at(i);
    1.48 +		if (strcmp(str, array_str) == 0)
    1.49 +		{
    1.50 +			return (cell_t) i;
    1.51 +		}
    1.52 +	}
    1.53 +
    1.54 +	return -1;
    1.55 +}
    1.56 +
    1.57 +static cell_t FindValueInArray(IPluginContext *pContext, const cell_t *params)
    1.58 +{
    1.59 +	CellArray *array;
    1.60 +	HandleError err;
    1.61 +	HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
    1.62 +
    1.63 +	if ((err = g_HandleSys.ReadHandle(params[1], htCellArray, &sec, (void **)&array)) 
    1.64 +		!= HandleError_None)
    1.65 +	{
    1.66 +		return pContext->ThrowNativeError("Invalid Handle %x (error: %d)", params[1], err);
    1.67 +	}
    1.68 +
    1.69 +	for (unsigned int i = 0; i < array->size(); i++)
    1.70 +	{
    1.71 +		if (params[2] == *array->at(i))
    1.72 +		{
    1.73 +			return (cell_t) i;
    1.74 +		}
    1.75 +	}
    1.76 +
    1.77 +	return -1;
    1.78 +}
    1.79 +
    1.80  REGISTER_NATIVES(cellArrayNatives)
    1.81  {
    1.82  	{"ClearArray",					ClearArray},
    1.83 @@ -502,5 +575,8 @@
    1.84  	{"SetArrayArray",				SetArrayArray},
    1.85  	{"ShiftArrayUp",				ShiftArrayUp},
    1.86  	{"SwapArrayItems",				SwapArrayItems},
    1.87 +	{"CloneArray",					CloneArray},
    1.88 +	{"FindStringInArray",			FindStringInArray},
    1.89 +	{"FindValueInArray",			FindValueInArray},
    1.90  	{NULL,							NULL},
    1.91  };