Merge pull request #72 from VoiDeD/keyvalue-from-string

Implement StringToKeyValues.
This commit is contained in:
Asher Baker
2014-07-07 13:49:24 +01:00
4 changed files with 99 additions and 1 deletions
+12
View File
@@ -360,6 +360,18 @@ native bool:KeyValuesToFile(Handle:kv, const String:file[]);
*/
native bool:FileToKeyValues(Handle:kv, const String:file[]);
/**
* Converts a given string to a KeyValues tree. The string is read into
* the current postion of the tree.
*
* @param kv KeyValues Handle.
* @param buffer String buffer to load into the KeyValues.
* @param resourceName The resource name of the KeyValues, used for error tracking purposes.
* @return True on success, false otherwise.
* @error Invalid Handle.
*/
native bool:StringToKeyValues(Handle:kv, const String:buffer[], const String:resourceName[]="StringToKeyValues");
/**
* Sets whether or not the KeyValues parser will read escape sequences.
* For example, \n would be read as a literal newline. This defaults
+58
View File
@@ -0,0 +1,58 @@
#include <sourcemod>
public Plugin:myinfo =
{
name = "KeyValues test",
author = "AlliedModders LLC",
description = "KeyValues test",
version = SOURCEMOD_VERSION,
url = "http://www.sourcemod.net/"
};
public OnPluginStart()
{
RegServerCmd("test_keyvalues", RunTests);
}
public Action:RunTests(argc)
{
new String:validKv[] =
"\"root\" \
{ \
\"child\" \"value\" \
\"subkey\" { \
subchild subvalue \
subfloat 1.0 \
} \
}";
new Handle:kv = CreateKeyValues("");
if (!StringToKeyValues(kv, validKv))
ThrowError("Valid kv not read correctly!");
decl String:value[128];
KvGetString(kv, "child", value, sizeof(value));
if (!StrEqual(value, "value"))
ThrowError("Child kv should have 'value' but has: '%s'", value);
if (!KvJumpToKey(kv, "subkey"))
ThrowError("No sub kv subkey exists!");
KvGetString(kv, "subchild", value, sizeof(value));
if (!StrEqual(value, "subvalue"))
ThrowError("Subkv subvalue should have 'subvalue' but has: '%s'", value);
new Float:subfloat = KvGetFloat(kv, "subfloat");
if (subfloat != 1.0)
ThrowError( "Subkv subfloat should have 1.0 but has: %f", subfloat)
CloseHandle(kv);
PrintToServer("KeyValue tests passed!");
}