Search code examples
c++configuration-filesallegro5

Allegro 5 loop through config file values with the same name


I have a config file that store a map

name=map1
width=5
height=5
[tiles]
   l=0,0,1,0,0
   l=0,1,1,1,0
   l=0,1,0,1,0
   l=0,1,0,1,0
   l=0,0,0,0,0
[/tiles]

How can i loop through the [tiles] section to store his lines(l) values into my vector?

Note: i put the allegro5 tag cause it have functions for load configuration files.


Solution

  • As you've discovered, Allegro will only take the last of a number of entries with the same key. While you could give each row a different key, you could instead take advantage of the fact that the = assignment is optional:

    [tiles]
       0,0,1,0,0
       0,1,1,1,0
       0,1,0,1,0
       0,1,0,1,0
       0,0,0,0,0
    [/tiles]
    

    Now the data for each row is stored in the 'key' itself, and the 'value' is ignored.

    int main() {
        ALLEGRO_CONFIG *cfg;
        ALLEGRO_CONFIG_ENTRY *entry;
        const char* row;
    
        al_init();
    
        cfg = al_load_config_file("config.cfg");
    
        row = al_get_first_config_entry(cfg, "tiles", &entry);
        while (entry) {
            printf("%s\n", row);
            row = al_get_next_config_entry(&entry);
        }
    
        return 0;
    }