28.01.2014, 17:23
Here is my INI_ReadData function:
Usage:
It also works for strings, too (but you need to specify the string size).
Variables and specifiers can be in any order.
pawn Code:
stock INI_ReadData(filename[], specifiers[], {Float,_}:...)
{
static
identifiers[32],
argument,
key[32]
;
strcpy(identifiers, specifiers);
for (new i = 0, len = strlen(identifiers); i != len; i ++)
{
if (identifiers[i] != '[')
continue;
strdel(identifiers, i, strfind(identifiers, "]", false, i) + 1);
}
if ((numargs() - 2) / 2 != strlen(identifiers))
{
return printf("** Warning: %d arguments, expecting %d.", numargs() - 2, strlen(identifiers));
}
inline ReadData(name[], value[])
{
for (new i = 2, args = numargs(); i != args; i += 2)
{
va_getstring(key, i);
if (strcmp(name, key, true) != 0)
continue;
switch (identifiers[(argument = i / 2 - 1)])
{
case 'f':
setarg(i + 1, 0, _:floatstr(value));
case 's':
{
new size = strval(specifiers[strfind(specifiers, "[", true, argument) + 1]);
for (new j = 0, len = strlen(value); j < size && j < len; j ++) {
setarg(i + 1, j, value[j]);
}
}
default: {
setarg(i + 1, 0, strval(value));
}
}
break;
}
}
INI_ParseFile(filename, "ReadData");
return 1;
}
Code:
// test.ini Money = 10000 Health = 50.0 Score = 2176
pawn Code:
new
money,
score,
Float:health;
INI_ReadData("test.ini", "fdd", "Health", health, "Score", score, "Money", money);
pawn Code:
new
money,
score,
Float:health,
Float:armor,
str[32];
INI_ReadData("test.ini", "dds[32]ff", "Money", money, "Score", score, "Motto", str, "Health", health, "Armor", armor);