0

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.

manlio
  • 18,345
  • 14
  • 76
  • 126

1 Answers1

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

mas.morozov
  • 2,666
  • 1
  • 22
  • 22
  • Thanks! Does that mean that i can use the Tstring for use in splitting a string in sections using the delimiter? I ask this because searching for a split string function gave me some complex results and using Tstrings would solve that to then. – user2914981 Oct 24 '13 at 09:52
  • Yes, you can. I did it and it worked for me. The only limitation (compared to good split function) is that you can split only in two parts and on single character only, but not on substring. – mas.morozov Oct 24 '13 at 09:55
  • Please, do not forget to tick-select the right answer. It will give you +2 [reputation points](http://stackoverflow.com/help/whats-reputation) too :) – mas.morozov Oct 24 '13 at 10:28
  • Ok cool, i forgot about that. Went straight on with the program :) – user2914981 Oct 24 '13 at 10:36