In the following code, g_strings is an array that houses enum arrays that point to arrays that contain characters. This is especially useful for plugins that may store the same string many times. Note that this code should not be used for plugins that utilize the same strings until the end of the map; use EngFunc_AllocString for that.
Code:
new Array:g_strings;
enum _:string_t
{
Array:s_aStr,
s_iRefCount
};
// Do this before using the below functions:
// g_strings = ArrayCreate(string_t);
// Call this to add the string to your string array
stock alloc_string(const str[], len)
{
new buf[256], elem[string_t];
for(new i=0;i<ArraySize(g_strings);i++)
{
ArrayGetArray(g_strings, i, elem);
if(ArraySize(elem[s_aStr])-1==len)
{
array_string(elem[s_aStr], buf, 255);
if(!strcmp(buf, str))
{
elem[s_iRefCount]++;
ArraySetArray(g_strings, i, elem);
return i;
}
}
}
elem[s_aStr] = ArrayCreate();
for(new i=0;i<=len;i++)
{
ArrayPushCell(elem[s_aStr], str[i]);
}
elem[s_iRefCount] = 1;
ArrayPushArray(g_strings, elem);
return ArraySize(g_strings) - 1;
}
// Call this to retrieve the string from its index
stock string(isz, buf[], buflen)
{
array_string(ArrayGetCell(g_strings, isz), buf, buflen);
}
// Do not use this:
stock array_string(Array:astr, buf[], buflen)
{
buflen = min(ArraySize(astr), buflen);
for(new i=0;i<buflen;i++)
{
buf[i] = ArrayGetCell(astr, i);
}
buf[buflen] = 0;
}
// Call this for every indexed string when you're destroying
// something that contains indexed strings
stock unreference_string(isz)
{
new elem[string_t];
ArrayGetArray(g_strings, isz, elem);
if(elem[s_iRefCount]==1)
{
ArrayDestroy(elem[s_aStr]);
return;
}
elem[s_iRefCount]--;
ArraySetArray(g_strings, isz, elem);
}
__________________