Raised This Month: $ Target: $400
 0% 

KeyValues with unknown Key's


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
HSFighter
Veteran Member
Join Date: Aug 2007
Location: Flensburg - Germany
Old 08-07-2014 , 05:51   KeyValues with unknown Key's
Reply With Quote #1

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.
__________________



Sorry for my very bad english
Greetings HSFighter

Last edited by HSFighter; 08-07-2014 at 05:55.
HSFighter is offline
Send a message via ICQ to HSFighter
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 08-07-2014 , 06:19   Re: KeyValues with unknown Key's
Reply With Quote #2

You may have to parse keys yourself manually, using readfileline
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 08-07-2014 , 09:18   Re: KeyValues with unknown Key's
Reply With Quote #3

Quote:
Originally Posted by friagram View Post
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.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 08-07-2014 at 09:23. Reason: Fixed KvGotoNextKey missing the second arg.
Powerlord is offline
HSFighter
Veteran Member
Join Date: Aug 2007
Location: Flensburg - Germany
Old 08-07-2014 , 11:10   Re: KeyValues with unknown Key's
Reply With Quote #4

Thank You Powerlord , 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?


-
__________________



Sorry for my very bad english
Greetings HSFighter

Last edited by HSFighter; 08-07-2014 at 11:12.
HSFighter is offline
Send a message via ICQ to HSFighter
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 08-07-2014 , 14:50   Re: KeyValues with unknown Key's
Reply With Quote #5

Whoops. Since you're storing Strings, your array needs a third dimension with the string length.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
HSFighter
Veteran Member
Join Date: Aug 2007
Location: Flensburg - Germany
Old 08-07-2014 , 18:03   Re: KeyValues with unknown Key's
Reply With Quote #6

That's it!

Thank you very much.
__________________



Sorry for my very bad english
Greetings HSFighter
HSFighter is offline
Send a message via ICQ to HSFighter
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 14:16.


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