Skip to content

Commit

Permalink
Merge pull request #72 from VoiDeD/keyvalue-from-string
Browse files Browse the repository at this point in the history
Implement StringToKeyValues.
  • Loading branch information
asherkin committed Jul 7, 2014
2 parents ff31d8d + fc54903 commit af791a3
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
29 changes: 28 additions & 1 deletion core/smn_keyvalues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,32 @@ static cell_t smn_FileToKeyValues(IPluginContext *pCtx, const cell_t *params)
return g_HL2.KVLoadFromFile(kv, basefilesystem, path);
}

static cell_t smn_StringToKeyValues(IPluginContext *pCtx, const cell_t *params)
{
Handle_t hndl = static_cast<Handle_t>(params[1]);
HandleError herr;
HandleSecurity sec;
KeyValueStack *pStk;
KeyValues *kv;

sec.pOwner = NULL;
sec.pIdentity = g_pCoreIdent;

if ((herr=handlesys->ReadHandle(hndl, g_KeyValueType, &sec, (void **)&pStk))
!= HandleError_None)
{
return pCtx->ThrowNativeError("Invalid key value handle %x (error %d)", hndl, herr);
}

char *buffer;
char *resourceName;
pCtx->LocalToString(params[2], &buffer);
pCtx->LocalToString(params[3], &resourceName);

kv = pStk->pCurRoot.front();
return kv->LoadFromBuffer(resourceName, buffer);
}

static cell_t smn_KvSetEscapeSequences(IPluginContext *pCtx, const cell_t *params)
{
Handle_t hndl = static_cast<Handle_t>(params[1]);
Expand Down Expand Up @@ -1101,6 +1127,7 @@ REGISTER_NATIVES(keyvaluenatives)
{"KvGetDataType", smn_KvGetDataType},
{"KeyValuesToFile", smn_KeyValuesToFile},
{"FileToKeyValues", smn_FileToKeyValues},
{"StringToKeyValues", smn_StringToKeyValues},
{"KvSetEscapeSequences", smn_KvSetEscapeSequences},
{"KvDeleteThis", smn_KvDeleteThis},
{"KvDeleteKey", smn_KvDeleteKey},
Expand All @@ -1113,4 +1140,4 @@ REGISTER_NATIVES(keyvaluenatives)
{"KvGetVector", smn_KvGetVector},
{"KvSetVector", smn_KvSetVector},
{NULL, NULL}
};
};
12 changes: 12 additions & 0 deletions plugins/include/keyvalues.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions plugins/testsuite/keyvalues.sp
Original file line number Diff line number Diff line change
@@ -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!");
}
1 change: 1 addition & 0 deletions tools/buildbot/PackageScript
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ CopyFiles('plugins/testsuite', 'addons/sourcemod/scripting/testsuite',
'structtest.sp',
'tf2-test.sp',
'tries.sp',
'keyvalues.sp',
]
)
CopyFiles('plugins/basecommands', 'addons/sourcemod/scripting/basecommands',
Expand Down

0 comments on commit af791a3

Please sign in to comment.