al_get_next_config_entry with repeating values
Alianix

So I'm using al_get_next_config_entry to iterate through a config section however when it finds multiples of the same value it ends the section iteration:

#SelectExpand
1int cfg_section_values( ALLEGRO_CONFIG *cfg, const char *section, const char *value){ 2 3 const char *str = NULL; 4 ALLEGRO_CONFIG_ENTRY *iter = NULL; 5 int n = 0; 6 7 if( !(str = al_get_first_config_entry( cfg, section, &iter))){ 8 printf("ERROR: No such section [%s] while searcing for [%s]!\n", section, value); 9 return( -1); 10 } 11 12 if( !strcmp( str, value)) n++; 13 14 while( (str = al_get_next_config_entry( &iter))){ 15 16 printf("%s\n", str); 17 if( !strcmp( str, value)) n++; 18 19 } 20 21 printf("Found %d [%s] value in section [%s]\n", n, value, section); 22 23 return( n); 24}

I have this in my config file:

[GAME]
#--------------------------------------------------------------------------------
DATA-PATH = data
FONT-PATH = font
GFX-FLAGS = WINDOWED
#GFX-FLAGS = FULLSCREEN
GFX-OPTION = VSYNC
TIMER-FREQ = 24
#SYS-FONT = bgothl.ttf,12
SYS-FONT = pirulen.ttf,12
ANI-SET = Animations.ani
BMP-SET = snow.ani
FONT = bgothl.ttf,12
FONT = pirulen.ttf,12
#--------------------------------------------------------------------------------

However it only finds one FONT value instead of two. Is this normal?

J-Gamer

That is indeed expected behaviour, because you would otherwise have to find a way to make one key have multiple values. ie: if you look up the value of font, what should it be?
ie: what should al_get_config_value(cfg, "Game", "FONT") return? undefined if you have multiple entries for the same key.

Alianix

I disagree, it could return the first value found. There are good reasons to have multiple entries of the same name.

J-Gamer
Alianix said:

There are good reasons to have multiple entries of the same name.

Which are?

Anyway, it is impossible to specify which of the double entries you want to get with al_get_config_value, just like an std::map.

Append:
I haven't ever used the allegro config routines, so I don't know which element al_get_config_value will return. I figure it'll be either the first or the last entry, depending on how it the routines were written(overwriting the value at each read, or check if the element already exists,...).

SiegeLord

Keys do not need to be unique, but all but the last one are ignored.

Alianix

The fact that we are not sure which entry we get when multiples are found suggest that something is perhaps missing. Otherwise having multiple fonts is perfectly natural and would make sense to be able to load them all by iterating through the config entries. This makes sense to me from a programming point of view also. If you have a loading class it would need to find matching entries to know which loader to call for example. I guess I could use prefixes FONT_ and strstr instead but i don't see a good reason for this other than getting around the issue at hand.

Arthur Kalliokoski
Quote:

all but the last one are ignored.

Alianix

Ok got it, so how do i count them? Or get intermediate entries? I doesn't sound like this behavior is making much sense.

SiegeLord
Alianix said:

Ok got it, so how do i count them? Or get intermediate entries?

Impossible. You'll need to either use different keys (font0, font1), separate multiple fonts by delimeters, or use a different configuration file format. Not many configuration file formats support duplicate keys like that, incidentally... XML is the most popular one I can think ok. Everything else forces you to use lists (like JSON/YAML).

Elias

XML isn't a configuration file format though, just a markup language. If you use actual keys by attaching id attributes to elements they must be unique as well.

Alianix

Ok I'm not going to make a fuss out of this, probably just using FONT_XXX will do, then I will use strstr or strncmp...Thanks for your help!

SiegeLord
Elias said:

XML isn't a configuration file format though, just a markup language.

A markup language that's nearly never used for markup purposes :P.

Thread #610553. Printed from Allegro.cc