Not always can your program know what sections or tags will be contained in INI files it uses. A game, for instance, might use an ini file to store properties of various game objects with their own INI sections without knowing how many objects will be present. In such cases, you need to access sections in a different way than using their names. MiniINI supports iteration over sections in a file and tags in a section to facilitate this.

Iterating over sections in an INIFile

Iteration over sections in an INIFile is done using the Next method. This method will increase the index used internally by INIFile to point to the current section. By default, this internal index points before the first section, so the first Next call will move to the first element. Next takes no parameters and returns a bool value that is true if there are any more sections left or false if we're already at the last section.

To get the current section, we use the same GetSection method we use to access sections by name, but instead of passing a section name we pass an empty string ("").

Example:

    //iterate over all sections in a file and print their names
    INIFile ini;
    ini.OpenFile("test.ini");
    while(ini.Next())
    {
        std::cout << ini.GetSection("")->GetName() << std::endl;
    }

Sections are always iterated in alphabetic order. To reset iteration back to start, use the Reset method. Reset takes no parameters and returns nothing.

Iterating over tags in an INISection

Iteration over tags in an INISection is done in the same way as over sections in an INIFile, using Next method to move to the next element and Reset to move back to start. Adittionally, you can use the CurrentTag method to get the name of the current tag as a const C string and CurrentTagSTL as a C++ std::string. These methods take no arguments. Just like over sections in an INIFile, iteration moves in alphabetic order.

Similarly to how you access iterated sections, you can access tags by passing an empty string to ReadXXX methods. Iteration also works with ReadMultiXXX methods and MultiValSize (used to work with tags with multiple values) but not with ReadXXXs or ArraySize (used to work with sequences of numbered tags) .

Example of iteration over tags in an INISection:

    //iterate over all tags in a section, print their names and values
    INIFile ini;
    ini.OpenFile("test.ini");
    INISection * section = ini.GetSection("test");
    const char * str; //STL: std::string str
    while(section->Next())
    {
        str = section->ReadString("", str);
        std::cout << section->CurrentTag() << "=" << str << std::endl; 
        //STL: std::cout << section->CurrentTagSTL() << "=" << str << std::endl; 
    }

Due to internal structure of MiniINI code, if you access tags this way, any warnings emitted by ReadXXX functions will be unable to print out names of tags concerned. This only really affects you if you're using MiniINI logging functionality to validate INI files (and you could probably find better tools for that job). MiniINI provides warnings where they might be useful for users modifying INI files, but not where it could negatively affect speed.