AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   KeyValues with unknown Key's (https://forums.alliedmods.net/showthread.php?t=245797)

HSFighter 08-07-2014 05:51

KeyValues with unknown Key's
 
Hi all,

i want to read all values from a config file via "KeyValues".
But the Key's are unknow.

It's no problem when i know the name of the "key" but i didn't.

Example:

Code:

"MyFile"
{
    "de_dust"
    {
        "Key_fvj"          "Value_arhu"
        "Key_fgaz"          "Value_mpai"
        "Key_sae"          "Value_uusj"
        "Key_diugx"        "Value_yjko"

    }
    "de_dust2"
    {
        "Key_abc"          "Value_afdu"
        "Key_fguz"          "Value_msai"
        "Key_sqae"          "Value_ddsj"
        "Key_diegx"        "Value_ejko"
    }
}

PHP Code:

public bool:ReadFile(const String:value[], maxlength
{    
    
decl String:map[64];
    
GetCurrentMap(mapsizeof(map));

    
kv CreateKeyValues("Myfile");
    
FileToKeyValues(kvpath);
    
    if (!
KvGotoFirstSubKey(kv)) 
    {
        
LogMessage("CFG File not found");
        
        
CloseHandle(kv);
        return 
false;
    }
    do 
    {
        
KvGetSectionName(kvbuffersizeof(buffer));
        if (
StrEqual(buffermap))
        {            
            
// Here i dount know the "Key"
            
KvGetString(kv"Key_???"Valuemaxlength);
            
CloseHandle(kv);
            return 
true;            
        }
    }while (
KvGotoNextKey(kv));
    
CloseHandle(kv);
    return 
true;  


I want to save all key's and values to an Array.

Code:

E.x. de_dust:

Array[0][0] = Key_fvj
Array[0][1] = Value_arhu

Array[1][0] = Key_fgaz
Array[1][1] = Value_mpai

...

Can someone tell me how i can realize it?

Thanks for the effort.

friagram 08-07-2014 06:19

Re: KeyValues with unknown Key's
 
You may have to parse keys yourself manually, using readfileline

Powerlord 08-07-2014 09:18

Re: KeyValues with unknown Key's
 
Quote:

Originally Posted by friagram (Post 2180237)
You may have to parse keys yourself manually, using readfileline

That's just silly.

HSFighter:

It is possible to iterate through those. Valve refers to KV items that have values (and thus no children) as Values, which is what might have thrown you off from finding the solution on your own.

Both KvGotoFirstSubKey and KvGotoNextKey have an optional second value named keysOnly. When this is set to true (the default), it will only go through the Key items. When this is set to false, it will only go through the Value items.

One more important thing: To retrieve the value of the current Value item, you have to pass NULL_STRING as the key name. This is mentioned as a valid argument for the key name, but the documentation doesn't mention what it does.

So, to fetch them all is something like this:

PHP Code:

        if (StrEqual(buffermap))
        {
            if (
KvGotoFirstSubKey(kvfalse))
            {
                new 
counter 0;
                do
                {
                    
KvGetSectionName(kv, Array[counter][0], sizeof(Array[])); // This is the current key name
                    
KvGetString(kvNULL_STRING, Array[counter][1], sizeof(Array[])); // and this is its value
                    
counter++;
                }while (
KvGotoNextKey(kvfalse));
            }
        } 

Incidentally, if you want to know if this works, MapChooser Extended Sounds uses it.

HSFighter 08-07-2014 11:10

Re: KeyValues with unknown Key's
 
Thank You Powerlord :P:bacon!:, very interesting.

I get only the first Char back.
Then i set the Size of Array for the time being to 128.
After this i get some funny Results.

Result from the First Key / Value:
Code:

Debug: ArrayKey: KValKValue_mpai
Debug: ArrayValue: ValKValue_mpai

Here My Code:


PHP Code:

public bool:ReadHelpFile(const String:Array[9][2]) 
{

    
decl String:buffer[PLATFORM_MAX_PATH];
    
decl String:map[64];
    
    
GetCurrentMap(mapsizeof(map));
    
    new 
Handlekv CreateKeyValues("Myfile");
    
FileToKeyValues(kvpath);
    
    if (!
KvGotoFirstSubKey(kv)) 
    {
        
LogMessage("CFG File not found");
        
        
CloseHandle(kv);
        return 
false;
    }
    do
    {
        
KvGetSectionName(kvbuffersizeof(buffer));
        if (
StrEqual(buffermap))
        {                        
            if (
KvGotoFirstSubKey(kvfalse))
            {
                new 
counter 0;
                do
                {
                    
KvGetSectionName(kv, Array[counter][0], 128); // This is the current key name
                    
KvGetString(kvNULL_STRING, Array[counter][1], 128); // and this is its value
                    
counter++;
                }while (
KvGotoNextKey(kvfalse));
            }
        }
    }while (
KvGotoNextKey(kv));
    
CloseHandle(kv);
    
    return 
true;  


PHP Code:

public Action:Test(clientargs )
{
    
decl Stringg_adtArray[9][2];
    if (
ReadHelpFile(g_adtArray))
    {
        
PrintToChatAll("Debug: ArrayKey: %s"g_adtArray[0][0]);            
        
PrintToChatAll("Debug: ArrayValue: %s"g_adtArray[0][1]);            
    }
    return 
Plugin_Handled;
    


Any idea what i doing wrong?


-

Powerlord 08-07-2014 14:50

Re: KeyValues with unknown Key's
 
Whoops. Since you're storing Strings, your array needs a third dimension with the string length.

HSFighter 08-07-2014 18:03

Re: KeyValues with unknown Key's
 
That's it!

Thank you very much. :bacon!:


All times are GMT -4. The time now is 10:43.

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