c++c++builderinitstringlisttinifile

C++ TStringsList parse explanation


I'm trying to read a ini file in a value listbox. Example below works, but i don't know why.

ReadSectionValues contains a string list of ini lines. How does Rad Studio parse the lines with:

ListValues->Names[i] is first part of the line and ListValues->Values[ListValues->Names[i]] is the second part?

int i;
try

{

    //ShowMessage( ListBox1->Items->Strings[ListBox1->ItemIndex] );

    TStringList *ListValues = new TStringList;

    TIniFile* SettingsFile = new TIniFile(ExtractFilePath(Application->ExeName) + "settings.ini");

    String s;

    s = ListBox1->Items->Strings[ListBox1->ItemIndex];

    SettingsFile->ReadSectionValues( s , ListValues);

    for (i = 0; i < (ListValues->Count); i++) {

        //ShowMessage(ListValues->Names[i]);

        //ShowMessage(ListValues->Values[ListValues->Names[i]]);

        vList1->InsertRow(ListValues->Names[i] , ListValues->Values[ListValues->Names[i]],True);

    }

    delete SettingsFile;

    delete ListValues;

}

catch(Exception* e)
{
    ShowMessage(e->Message);
}

Please explain, Rad stuido help found no explanation.


Solution

  • void __fastcall ReadSectionValues(
      const System::UnicodeString Section, 
      System::Classes::TStrings* Strings
    )
    

    is a method, which gets all lines of ini-file section with name Section and stores them in TStrings-object Strings. Note that these strings have format name=value.

    TStrings class has two access properties Names and Values. Their parse algorithm is very simple. If you get stringsObject->Values[1] it takes second line from stringsObject and splits it into two strings on = (or other value of NameValueSeparator property of stringsObject). The string to the left of = (separator) is returned as name (by property Name) and the string to the right of = is returned as value (by property Value).