amxmodx/amxmodx/trie_natives.cpp

364 lines
7.2 KiB
C++
Raw Normal View History

2008-04-14 19:52:11 +00:00
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include "amxmodx.h"
#include "sm_trie_tpl.h"
#include "trie_natives.h"
2014-05-03 13:22:48 +02:00
using namespace SourceMod;
2008-04-14 19:52:11 +00:00
2014-05-03 13:22:48 +02:00
TrieHandles<CellTrie> g_TrieHandles;
2008-04-14 19:52:11 +00:00
// native Trie:TrieCreate();
static cell AMX_NATIVE_CALL TrieCreate(AMX *amx, cell *params)
{
return static_cast<cell>(g_TrieHandles.create());
}
// native Trie::TrieClear(Trie:handle);
static cell AMX_NATIVE_CALL TrieClear(AMX *amx, cell *params)
{
2014-05-03 13:22:48 +02:00
CellTrie *t = g_TrieHandles.lookup(params[1]);
2008-04-14 19:52:11 +00:00
if (t == NULL)
{
2014-05-03 13:22:48 +02:00
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
2008-04-14 19:52:11 +00:00
return 0;
}
2014-05-03 13:22:48 +02:00
t->map.clear();
2008-04-14 19:52:11 +00:00
return 1;
}
// native TrieSetCell(Trie:handle, const key[], any:value, bool:replace = true);
2008-04-14 19:52:11 +00:00
static cell AMX_NATIVE_CALL TrieSetCell(AMX *amx, cell *params)
{
2014-05-03 13:22:48 +02:00
CellTrie *t = g_TrieHandles.lookup(params[1]);
2008-04-14 19:52:11 +00:00
if (t == NULL)
{
2014-05-03 13:22:48 +02:00
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
2008-04-14 19:52:11 +00:00
return 0;
}
int len;
const char *key = get_amxstring(amx, params[2], 0, len);
2014-05-03 13:22:48 +02:00
StringHashMap<Entry>::Insert i = t->map.findForAdd(key);
if (!i.found())
2008-04-14 19:52:11 +00:00
{
2014-05-03 13:22:48 +02:00
if (!t->map.add(i, key))
2008-04-14 19:52:11 +00:00
{
return 0;
}
i->value.setCell(params[3]);
return 1;
}
// Old plugin doesn't have 'replace' parameter.
if (*params / sizeof(cell) == 4 && !params[4])
{
return 0;
2008-04-14 19:52:11 +00:00
}
2014-05-03 13:22:48 +02:00
i->value.setCell(params[3]);
2008-04-14 19:52:11 +00:00
return 1;
}
// native TrieSetString(Trie:handle, const key[], const data[], bool:replace = true);
2008-04-14 19:52:11 +00:00
static cell AMX_NATIVE_CALL TrieSetString(AMX *amx, cell *params)
{
2014-05-03 13:22:48 +02:00
CellTrie *t = g_TrieHandles.lookup(params[1]);
2008-04-14 19:52:11 +00:00
if (t == NULL)
{
2014-05-03 13:22:48 +02:00
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
2008-04-14 19:52:11 +00:00
return 0;
}
int len;
const char *key = get_amxstring(amx, params[2], 0, len);
2014-05-03 13:22:48 +02:00
const char *value = get_amxstring(amx, params[3], 1, len);
2008-04-14 19:52:11 +00:00
2014-05-03 13:22:48 +02:00
StringHashMap<Entry>::Insert i = t->map.findForAdd(key);
if (!i.found())
2008-04-14 19:52:11 +00:00
{
2014-05-03 13:22:48 +02:00
if (!t->map.add(i, key))
2008-04-14 19:52:11 +00:00
{
return 0;
}
i->value.setString(value);
return 1;
2008-04-14 19:52:11 +00:00
}
2014-05-03 13:22:48 +02:00
// Old plugin doesn't have 'replace' parameter.
if (*params / sizeof(cell) == 4 && !params[4])
{
return 0;
}
2014-05-03 13:22:48 +02:00
i->value.setString(value);
2008-04-14 19:52:11 +00:00
return 1;
}
// native TrieSetArray(Trie:handle, const key[], const any:buffer[], buffsize, bool:replace = true)
2008-04-14 19:52:11 +00:00
static cell AMX_NATIVE_CALL TrieSetArray(AMX *amx, cell *params)
{
2014-05-03 13:22:48 +02:00
CellTrie *t = g_TrieHandles.lookup(params[1]);
2008-04-14 19:52:11 +00:00
if (t == NULL)
{
2014-05-03 13:22:48 +02:00
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
2008-04-14 19:52:11 +00:00
return 0;
}
if (params[4] < 0)
{
LogError(amx, AMX_ERR_NATIVE, "Invalid array size (%d)", params[4]);
return 0;
}
2008-04-14 19:52:11 +00:00
int len;
const char *key = get_amxstring(amx, params[2], 0, len);
cell *ptr = get_amxaddr(amx, params[3]);
2008-04-14 19:52:11 +00:00
2014-05-03 13:22:48 +02:00
StringHashMap<Entry>::Insert i = t->map.findForAdd(key);
if (!i.found())
2008-04-14 19:52:11 +00:00
{
2014-05-03 13:22:48 +02:00
if (!t->map.add(i, key))
2008-04-14 19:52:11 +00:00
{
return 0;
}
i->key = key;
i->value.setArray(ptr, params[4]);
return 1;
2008-04-14 19:52:11 +00:00
}
// Old plugin doesn't have 'replace' parameter.
if (*params / sizeof(cell) == 4 && !params[5])
{
return 0;
2008-04-14 19:52:11 +00:00
}
i->value.setArray(ptr, params[4]);
2008-04-14 19:52:11 +00:00
return 1;
}
2008-04-14 19:52:11 +00:00
// native bool:TrieGetCell(Trie:handle, const key[], &any:value);
static cell AMX_NATIVE_CALL TrieGetCell(AMX *amx, cell *params)
{
2014-05-03 13:22:48 +02:00
CellTrie *t = g_TrieHandles.lookup(params[1]);
2008-04-14 19:52:11 +00:00
if (t == NULL)
{
2014-05-03 13:22:48 +02:00
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
2008-04-14 19:52:11 +00:00
return 0;
}
int len;
const char *key = get_amxstring(amx, params[2], 0, len);
2014-05-03 13:22:48 +02:00
StringHashMap<Entry>::Result r = t->map.find(key);
if (!r.found())
2008-04-14 19:52:11 +00:00
{
return 0;
}
2014-05-03 13:22:48 +02:00
2008-04-14 19:52:11 +00:00
cell *ptr = get_amxaddr(amx, params[3]);
2014-05-03 13:22:48 +02:00
if (r->value.isCell())
2008-04-14 19:52:11 +00:00
{
2014-05-03 13:22:48 +02:00
*ptr = r->value.cell_();
return 1;
2008-04-14 19:52:11 +00:00
}
2014-05-03 13:22:48 +02:00
return 0;
2008-04-14 19:52:11 +00:00
}
// native bool:TrieGetString(Trie:handle, const key[], buff[], len, &size = 0);
2008-04-14 19:52:11 +00:00
static cell AMX_NATIVE_CALL TrieGetString(AMX *amx, cell *params)
{
2014-05-03 13:22:48 +02:00
CellTrie *t = g_TrieHandles.lookup(params[1]);
2008-04-14 19:52:11 +00:00
if (t == NULL)
{
2014-05-03 13:22:48 +02:00
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
2008-04-14 19:52:11 +00:00
return 0;
}
if (params[4] < 0)
{
LogError(amx, AMX_ERR_NATIVE, "Invalid buffer size (%d)", params[4]);
return 0;
}
2008-04-14 19:52:11 +00:00
int len;
const char *key = get_amxstring(amx, params[2], 0, len);
cell *pSize = get_amxaddr(amx, params[5]);
2008-04-14 19:52:11 +00:00
2014-05-03 13:22:48 +02:00
StringHashMap<Entry>::Result r = t->map.find(key);
if (!r.found() || !r->value.isString())
2008-04-14 19:52:11 +00:00
{
return 0;
}
2014-05-03 13:22:48 +02:00
*pSize = (cell)set_amxstring_utf8(amx, params[3], r->value.chars(), strlen(r->value.chars()), params[4] + 1); // + EOS
2014-05-03 13:22:48 +02:00
2008-04-14 19:52:11 +00:00
return 1;
}
// native bool:TrieGetArray(Trie:handle, const key[], any:buff[], len, &size = 0);
2008-04-14 19:52:11 +00:00
static cell AMX_NATIVE_CALL TrieGetArray(AMX *amx, cell *params)
{
2014-05-03 13:22:48 +02:00
CellTrie *t = g_TrieHandles.lookup(params[1]);
2008-04-14 19:52:11 +00:00
if (t == NULL)
{
2014-05-03 13:22:48 +02:00
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
2008-04-14 19:52:11 +00:00
return 0;
}
if (params[4] < 0)
{
LogError(amx, AMX_ERR_NATIVE, "Invalid array size (%d)", params[4]);
return 0;
}
2008-04-14 19:52:11 +00:00
int len;
const char *key = get_amxstring(amx, params[2], 0, len);
cell *pValue = get_amxaddr(amx, params[3]);
cell *pSize = get_amxaddr(amx, params[5]);
2008-04-14 19:52:11 +00:00
2014-05-03 13:22:48 +02:00
StringHashMap<Entry>::Result r = t->map.find(key);
if (!r.found() || !r->value.isArray())
2008-04-14 19:52:11 +00:00
{
return 0;
}
2014-05-03 13:22:48 +02:00
if (!r->value.array())
{
*pSize = 0;
return 1;
}
2014-05-03 13:22:48 +02:00
if (!params[4])
2008-04-14 19:52:11 +00:00
{
return 0;
2008-04-14 19:52:11 +00:00
}
2014-05-03 13:22:48 +02:00
size_t length = r->value.arrayLength();
cell *base = r->value.array();
if (length > size_t(params[4]))
*pSize = params[4];
else
*pSize = length;
memcpy(pValue, base, sizeof(cell) * pSize[0]);
2008-04-14 19:52:11 +00:00
return 1;
}
2008-04-14 19:52:11 +00:00
// native bool:TrieKeyExists(Trie:handle, const key[]);
static cell AMX_NATIVE_CALL TrieKeyExists(AMX *amx, cell *params)
{
2014-05-03 13:22:48 +02:00
CellTrie *t = g_TrieHandles.lookup(params[1]);
2008-04-14 19:52:11 +00:00
if (t == NULL)
{
2014-05-03 13:22:48 +02:00
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
2008-04-14 19:52:11 +00:00
return 0;
}
int len;
const char *key = get_amxstring(amx, params[2], 0, len);
2014-05-03 13:22:48 +02:00
return static_cast<cell>(t->map.contains(key));
2008-04-14 19:52:11 +00:00
}
// native bool:TrieDeleteKey(Trie:handle, const key[]);
static cell AMX_NATIVE_CALL TrieDeleteKey(AMX *amx, cell *params)
{
2014-05-03 13:22:48 +02:00
CellTrie *t = g_TrieHandles.lookup(params[1]);
2008-04-14 19:52:11 +00:00
if (t == NULL)
{
2014-05-03 13:22:48 +02:00
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
2008-04-14 19:52:11 +00:00
return 0;
}
int len;
const char *key = get_amxstring(amx, params[2], 0, len);
2014-05-03 13:22:48 +02:00
StringHashMap<Entry>::Result r = t->map.find(key);
if (!r.found())
2008-04-14 19:52:11 +00:00
{
2014-05-03 13:22:48 +02:00
return 0;
2008-04-14 19:52:11 +00:00
}
2014-05-03 13:22:48 +02:00
t->map.remove(r);
return 1;
2008-04-14 19:52:11 +00:00
}
//native TrieDestroy(&Trie:handle)
static cell AMX_NATIVE_CALL TrieDestroy(AMX *amx, cell *params)
{
cell *ptr = get_amxaddr(amx, params[1]);
2014-05-03 13:22:48 +02:00
CellTrie *t = g_TrieHandles.lookup(*ptr);
2008-04-14 19:52:11 +00:00
if (t == NULL)
{
return 0;
}
2014-05-03 13:22:48 +02:00
2008-04-14 19:52:11 +00:00
if (g_TrieHandles.destroy(*ptr))
{
*ptr = 0;
return 1;
}
2014-05-03 13:22:48 +02:00
return 0;
2008-04-14 19:52:11 +00:00
}
2014-05-03 13:22:48 +02:00
2014-05-03 16:09:31 +02:00
// native TrieGetSize(Trie:handle);
static cell AMX_NATIVE_CALL TrieGetSize(AMX *amx, cell *params)
{
CellTrie *t = g_TrieHandles.lookup(params[1]);
if (t == NULL)
{
LogError(amx, AMX_ERR_NATIVE, "Invalid map handle provided (%d)", params[1]);
return 0;
}
return t->map.elements();
}
2008-04-14 19:52:11 +00:00
AMX_NATIVE_INFO trie_Natives[] =
{
{ "TrieCreate", TrieCreate },
{ "TrieClear", TrieClear },
{ "TrieSetCell", TrieSetCell },
{ "TrieSetString", TrieSetString },
{ "TrieSetArray", TrieSetArray },
{ "TrieGetCell", TrieGetCell },
{ "TrieGetString", TrieGetString },
{ "TrieGetArray", TrieGetArray },
{ "TrieDeleteKey", TrieDeleteKey },
{ "TrieKeyExists", TrieKeyExists },
{ "TrieDestroy", TrieDestroy },
2014-05-03 16:09:31 +02:00
{ "TrieGetSize", TrieGetSize },
2008-04-14 19:52:11 +00:00
{ NULL, NULL }
};