Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » al_get_next_config_entry with repeating values

This thread is locked; no one can reply to it. rss feed Print
al_get_next_config_entry with repeating values
Alianix
Member #10,518
December 2008
avatar

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
Member #12,491
January 2011
avatar

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.

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

Alianix
Member #10,518
December 2008
avatar

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
avatar

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,...).

" There are plenty of wonderful ideas in The Bible, but God isn't one of them." - Derezo
"If your body was a business, thought would be like micro-management and emotions would be like macro-management. If you primarily live your life with emotions, then you are prone to error on the details. If you over-think things all the time you tend to lose scope of priorities." - Mark Oates

SiegeLord
Member #7,827
October 2006
avatar

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
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Alianix
Member #10,518
December 2008
avatar

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
avatar

Quote:

all but the last one are ignored.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Alianix
Member #10,518
December 2008
avatar

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
avatar

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
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

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.

--
"Either help out or stop whining" - Evert

Alianix
Member #10,518
December 2008
avatar

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
avatar

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.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Go to: