Raised This Month: $ Target: $400
 0% 

List of strings in KeyValues


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
statistician
Member
Join Date: Jan 2016
Location: Finland
Old 01-21-2016 , 06:34   List of strings in KeyValues
Reply With Quote #1

Hi, I'm trying to parse a list of strings inside a KeyValues file. This is a plugin config file, so SQL won't do.

Example file:

Code:
"Names"
{
    "name1"
    "name2"
    "name3"
}
I tried iterating the file essentially like this:

Code:
kv.GotoFirstSubKey();
do {
    char key[128];
    kv.GetSectionName(key, 128);
} while (kv.GotoNextKey());
but it didn't seem to work. Is there a way to solve this? I wouldn't want to just separate each key with a whitespace or something. Also in the real file the values are longer and in a more complicated KeyValues file, so simply reading the file line by line won't do, either.

I also don't want to separate the files, since these files are map-specific, so there could me many of them.

Thanks for your help!

Last edited by statistician; 01-21-2016 at 08:40.
statistician is offline
splewis
Veteran Member
Join Date: Feb 2014
Location: United States
Old 01-21-2016 , 14:33   Re: List of strings in KeyValues
Reply With Quote #2

You have to have an actual value still. iirc, empty string values don't save if set (but can be read), so you may want a dummy value that is ignored.
__________________
splewis is offline
Godis
Senior Member
Join Date: Jan 2014
Old 01-21-2016 , 15:25   Re: List of strings in KeyValues
Reply With Quote #3

I'm sorry if i stray away from the original question, but in your specific case, i would do something like this.

PHP Code:
File file OpenFile("path_to_file""r"); // Open "path_to_file" for reading.
if(file == null)
{
    
SetFailState("Could not open file: \"path_to_file\"");
}

char line[128], newline[128];
while(!
file.EndOfFile() && file.ReadLine(linesizeof(line)))
{
    if(
StrContains(line"//") != -1// The line contains a '//' comment, filter all preceding text on that line.
    
{
        
SplitString(line"//"newlinesizeof(newline));
    }
    else
    {
        
Format(newlinesizeof(newline), line);
    }
    
TrimString(newline);
    
    
// Do whatever you want with "newline".
}

delete file

Last edited by Godis; 01-21-2016 at 15:31.
Godis is offline
Disowned
Member
Join Date: Oct 2015
Old 01-21-2016 , 15:34   Re: List of strings in KeyValues
Reply With Quote #4

As far as I know, all VDF parsers depend upon a value existing with a key. I know that parsers in other languages, such as Python and the two existing parsers in PHP require it, this is because any Key without a string value is treated as the key to a new object value.

One of the many reasons Source should ditch VDF altogether, JSON already has it beat in every area of comparison.

Try this instead:

PHP Code:
"Names"
{
    
"name1" ""
    "name2" ""
    "name3" ""


Last edited by Disowned; 01-21-2016 at 15:37.
Disowned is offline
statistician
Member
Join Date: Jan 2016
Location: Finland
Old 01-21-2016 , 18:34   Re: List of strings in KeyValues
Reply With Quote #5

Intrestingly enough, my example case would be read as such:

Code:
"Models"
{
    "name1"    "name2"
    "name3"
}
The odd strings still make the KeyValues file erroneus, though. I think I'll just let this be, though, and work with empty values like Disowned suggested. I could make a temp file and manually append one empty string, but I'll just leave it. Thanks for the help.
statistician 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 05:08.


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