AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   String to KeyValues (https://forums.alliedmods.net/showthread.php?t=229811)

KissLick 11-15-2013 09:43

String to KeyValues
 
Ciao,

is there any possibility how to convert string to KeyValues tree? I need something like:

PHP Code:

// example inspired by https://wiki.alliedmods.net/KeyValues_(SourceMod_Scripting)#Basic_Lookup

new String:MyDataString[256] = "\"MyStructure\"{ \"STEAM_0:0:7\"{ \"name\" \"crab\"} }";
.
.
.
.
GetNameFromSteamIDPlayerSteamIDPlayerName32MyDataString ); // PlayerSteamID and PlayerName are just for example
.
.
.
.
bool:GetNameFromSteamID(const String:steamid[], String:name[], maxlengthString:DataString[])
{
    new 
Handle:kv CreateKeyValues("MyStructure");

    
StringToKeyValues(kvDataString); // need this function

    
if (!KvJumpToKey(kvsteamid))
    {
        return 
false;
    }
    
KvGetString(kv"name"namemaxlength);
    
CloseHandle(kv);
    return 
true;


I don't wanna make a new file with KeyValues structure and then convert this file to KeyValues. I wanna make string (e.g. pass throw function argument) ang this string convert to KeyValues.

GsiX 11-15-2013 10:27

Re: String to KeyValues
 
You were saying something like this, yes??

Code:

"MyStructure"
{
        "STEAM_0:0:0000"
        {
                "name"        "On_my_crab"
        }
       
        // or
       
        "LickMe_KickYou"
        {
                "SteamID"        "STEAM_0:0:0000"
                "name"                "Oh_my_crab"
        }
}


and this one should be..
PHP Code:

if (!KvJumpToKey(kvsteamid))
{
    
CloseHandle(kv);
    return 
false;



KissLick 11-15-2013 10:36

Re: String to KeyValues
 
No, that was just an example (with mistake copied from SM wiki :-D ). The main thing I need is function
PHP Code:

StringToKeyValues(kvDataString); 

Because I don't wanna make KeyValues from file but from string. Now I found json https://forums.alliedmods.net/showth...highlight=json do you think that use json is better solution than KeyValues?

GsiX 11-15-2013 10:49

Re: String to KeyValues
 
As far as i concern, keyValue is string.
Why you want "StringToString"?

EDIT: Pardon my poor english but i don't realy understand it.

KissLick 11-15-2013 11:04

Re: String to KeyValues
 
Well, I don't mean KeyValues like string but like KeyValues tree. I just wanna iterate throw the KeyValues tree via function KvGotoNextKey(kv) etc. and only function to get existed KeyValues structure is via FileToKeyValues(kv, "myfile.txt") but I wanna use string instead of file.

So what I want is

PHP Code:

new Handle:kv CreateKeyValues("MyStructure");
StringToKeyValues(kvDataString); 

and then

PHP Code:

do
{
// code
} while (KvGotoNextKey(kv)); 

I want this to pass KeyValues between functions. One function make a string and another function process this KeyValues string via function KvGotoNextKey(kv).

Now i think use json is better that KeyValues, but I am not sure..

GsiX 11-15-2013 12:16

Re: String to KeyValues
 
PHP Code:

StringToKeyValuesHandle:Kv, const String:SAVEPath[], const String:DataString[], const String:Buff[]  )
{
    
decl String:Value[32][128];
    
FileToKeyValuesKvSAVEPath );
    
KvGotoFirstSubKeyKvfalse );
    
    new 
i;
    do
    {
        
KvGetSectionNameKvValue[i], 15 );
        
i++;
    }
    while ( 
KvGotoNextKeyKv,false ));
    
KvRewindKv );
    
    
// retriv data from keyvalue. Maybe what you looking for
    
KvJumpToKeyKvDataStringfalse);
    
KvGetStringKvValue[0], BuffsizeofBuff ), "Null" );
    
KvRewindKv );


I fail.. :cry:

KissLick 11-15-2013 12:37

Re: String to KeyValues
 
Well.. I decided to do it via json.. it looks easier :-D

Anyway, thanks for help ;-)


All times are GMT -4. The time now is 21:12.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.