|
al_get_next_config_entry with repeating values |
Alianix
Member #10,518
December 2008
|
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: 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] However it only finds one FONT value instead of two. Is this normal?
|
J-Gamer
Member #12,491
January 2011
|
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? " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
Alianix
Member #10,518
December 2008
|
I disagree, it could return the first value found. There are good reasons to have multiple entries of the same name.
|
J-Gamer
Member #12,491
January 2011
|
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: " There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo |
SiegeLord
Member #7,827
October 2006
|
The Manual said: Keys do not need to be unique, but all but the last one are ignored.
"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Alianix
Member #10,518
December 2008
|
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
Second in Command
February 2005
|
Quote: all but the last one are ignored. They all watch too much MSNBC... they get ideas. |
Alianix
Member #10,518
December 2008
|
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
Member #7,827
October 2006
|
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). "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
Elias
Member #358
May 2000
|
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
Member #10,518
December 2008
|
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
Member #7,827
October 2006
|
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 . "For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18 |
|