Raised This Month: $51 Target: $400
 12% 

KeyValuesToFile with saving keys with void values


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Kailo
Senior Member
Join Date: Sep 2014
Location: Moscow, Russia
Old 11-01-2015 , 08:48   KeyValuesToFile with saving keys with void values
Reply With Quote #1

Some files use KeyValues with void values (about server files, as example gamemodes.txt), but KeyValuesToFile don't save keys with void values.

As example:
PHP Code:
// This that you read from file
"kv"
{
    
"name"        "John Connor"
    "weapons"
    
{
        
"minigun"        ""
        "deagle"        ""
    
}
}

And 
then save this with KeyValuesToFile you got
"kv"
{
    
"name"        "John Connor"
    "weapons"
    
{
    }
}

or 
save with KeyValuesToFile2 you got
"kv"
{
    
"name"        "John Connor"
    "weapons"
    
{
        
"minigun"        ""
        "deagle"        ""
    
}

PHP Code:
stock bool KeyValuesToFile2(KeyValues kv, const char[] file)
{
    
File fh OpenFile(file"wb");
    
    if (
fh == null)
        return 
false;
    
    
IterateKeys(kvfh0);
    
    
delete fh;
    
    return 
true;
}

stock void IterateKeys(KeyValues kvFile fhint tab)
{
    
char buffer[512], buffer2[512];
    
    
kv.GetSectionName(buffersizeof(buffer));
    
    
WriteLine(fhtab"\"%s\""buffer);
    
    
WriteLine(fhtab"{");
    
    if (
kv.GotoFirstSubKey(false)) {
        do {
            if (
kv.GetDataType(NULL_STRING) == KvData_None)
                
IterateKeys(kvfhtab 1);
            else {
                
kv.GetSectionName(buffersizeof(buffer));
                
kv.GetString(NULL_STRINGbuffer2sizeof(buffer2));
                
WriteLine(fhtab 1"\"%s\"        \"%s\""bufferbuffer2);
            }
        } while (
kv.GotoNextKey(false));
        
        
kv.GoBack();
    }
    
    
WriteLine(fhtab"}");
}

stock void WriteLine(File fhint tab, const char[] stringany ...)
{
    
char buffer[512];
    
VFormat(buffersizeof(buffer), string4);
    
Format(buffersizeof(buffer), "%s\n"buffer);
    for (
int i 0tabi++)
        
Format(buffersizeof(buffer), "    %s"buffer);
    
fh.WriteString(bufferfalse);

Kailo is offline
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 13:59.


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