program examples;
{$MODE OBJFPC}{$H+}
{$APPTYPE CONSOLE}
uses
classes, IniFiles;
// see also: http://wiki.freepascal.org/TStringList-TStrings_Tutorial
procedure TextFiles;
var
SomeTextLines: TStringList;
Line : String;
begin
// Add some lines and save to a file
SomeTextLines := TStringList.Create;
SomeTextLines.Append('Hello');
SomeTextLines.Append('world');
SomeTextLines.SaveToFile('testfile.dat');
SomeTextLines.Free;
// Load some lines from file and write
SomeTextLines := TStringList.Create;
SomeTextLines.LoadFromFile('testfile.dat');
for Line in SomeTextLines
do WriteLn(Line);
SomeTextLines.Free;
end;
// see also: http://wiki.freepascal.org/Using_INI_Files
procedure IniFiles;
var
MemIni: TMemIniFile;
String_Value: String;
Boolean_Value : Boolean;
Integer_Value : Integer;
begin
// Create ini file
MemIni := TMemIniFile.Create('testfile.ini');
MemIni.WriteString('Section_Name', 'Identifier', 'value');
MemIni.WriteBool('Another_Section', 'Boolean_Identifier', True);
MemIni.WriteInteger('Another_Section', 'Integer_Identifier', 1234);
MemIni.Free;
// read/parse ini
MemIni := TMemIniFile.Create('testfile.ini');
MemIni.ReadString('Section_Name', 'Identifier', 'default_value_in_case_not_present_inside_inifile');
Boolean_value := MemIni.ReadBool('Another_Section', 'Boolean_Identifier', false);
Integer_Value := MemIni.ReadInteger('Another_Section', 'Integer_Identifier', -1);
WriteLn('integer_value = ', Integer_Value);
if MemIni.ValueExists('Some_Section', 'Some_Identifier')
then WriteLn('Section "Some_Section" contains identifier "Some_identifier"')
else WriteLn('Section "Some_Section" and/or identifier "Some_identifier" does not exist');
// add new entry andcheck again
memIni.WriteString('Some_Section', 'Some_Identifier', 'now it does exist');
if MemIni.ValueExists('Some_Section', 'Some_Identifier') then
begin
WriteLn('Section "Some_Section" contains identifier "Some_identifier"');
WriteLn('The contents is :', memIni.ReadString('Some_Section', 'Some_Identifier', 'Oops somthing went wrong'));
end
else WriteLn('Section "Some_Section" and/or identifier "Some_identifier" does not exist');
MemIni.Free;
end;
begin
TextFiles;
IniFiles;
end.