PDA

View Full Version : INI writing/reading/deleteing



Negaal
03-31-2008, 07:31 PM
Ok, this is basically anwser to EvilChicken's request
to make tutorial about ini commands and how to use them on forms...

First, let's define ini:

INI File - An initialization file, which is used by an application
to store configuration data that is read as the program is starting.

Actually we can use ini's whenever we want, not only at starting.
INI files are text files, easy to open with notepad and look into.

...here, these are 3 function for writing, deleting and reading ini's


procedure WriteINI(Section, KeyName, NewString, FileName: string);
{Writes data to an ini-structured file.
Structure:
[Section]
KeyName=NewString}

function ReadINI(Section, KeyName, FileName: string): string;
{Reads data from an ini-structured file.
Structure:
[Section]
KeyName=SomeString}

procedure DeleteINI(Section, KeyName, FileName: string);
{Deletes a KeyName from an INI file and if you pass an
empty string as KeyName it will delete then entire Section. }


Now using them without form, to understand how they work:

program Hallo;
begin
WriteINI('Writing section', 'Keyname Example', 'New string example', ScriptPath+'\ini writing');
end.


Run it.
Instead of explaining it, go to same folder where script was saved, open the ini with any text editor,
and see the result, it will be like this:



[Writing section]
Keyname Example=New string example


Simple enough? mhm.

Reading:


program Hallo;
var
s : string;
begin
s := ReadINI('Writing section', 'Keyname Example', ScriptPath+'\ini writing');
writeln(s);
end.


Deleting:

program Hallo;
begin
DeleteINI('Writing section', 'Keyname Example', ScriptPath+'\ini writing');
end.


After that the ini file will look like this:



[Writing section]


Replacing? Just overwrite the keyname with new value.

Now, how should I use them on forms?
(I think you need to know open and save dialogs before,
they are well explained in jhildy's "Open and save" tutorial)

It writes string writed in editbox to ini, you need to specify the sections and keyname.
This is temporary to show you how it works.
(you can use other keynames and section names created by yourself.)

Loading part does the opposite to saving, opens ini,
you need to specify the keyname and section again.


program New;

var
frmDesign : TForm;
edit2 : TEdit;
Edit1 : TEdit;
Button1 : TButton;
Button2 : TButton;
OpenDialog1 : TOpenDialog;
SaveDialog1 : TSaveDialog;

Procedure Open(Sender: TObject);
begin
opendialog1.Execute;
Edit2.text := OpenDialog1.Filename;
Edit1.text := ReadINI(ReadLn('Enter section to open'), ReadLn('Enter keyname to open'), OpenDialog1.Filename);
end;

Procedure Save(Sender: TObject);
begin
Savedialog1.Execute;
Edit2.text := SaveDialog1.Filename;
WriteIni(ReadLn('Enter section to save'), ReadLn('Enter keyname to save'), Edit1.Text, SaveDialog1.Filename);
end;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 263;
frmDesign.Top := 148;
frmDesign.Width := 331;
frmDesign.Height := 125;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := false;
frmDesign.PixelsPerInch := 96;
edit2 := TEdit.Create(frmDesign);
edit2.Parent := frmDesign;
edit2.Left := 8;
edit2.Top := 20;
edit2.Width := 200;
edit2.Height := 13;
edit2.ReadOnly := true;
Edit1 := TEdit.Create(frmDesign);
Edit1.Parent := frmDesign;
Edit1.Left := 8;
Edit1.Top := 56;
Edit1.Width := 305;
Edit1.Height := 177;
Edit1.TabOrder := 8;
Button1 := TButton.Create(frmDesign);
Button1.Parent := frmDesign;
Button1.Left := 250;
Button1.Top := 0;
Button1.Width := 65;
Button1.Height := 25;
Button1.Caption := 'Open';
Button1.TabOrder := 9;
Button2 := TButton.Create(frmDesign);
Button2.Parent := frmDesign;
Button2.Left := 250;
Button2.Top := 24;
Button2.Width := 65;
Button2.Height := 25;
Button2.Caption := 'Save';
Button2.TabOrder := 10;
OpenDialog1 := TOpenDialog.Create(frmDesign);
Button1.OnClick := @Open;
SaveDialog1 := TSaveDialog.Create(frmDesign);
Button2.OnClick := @Save;
end;

procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('InitForm', v);
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('ShowFormModal', v);
end;

begin
SafeInitForm;
SafeShowFormModal;
end.


Instead of ReadLn, you can use your own key and section names. Or generate theme while script runs.

Using these it is very easy to create declareplayers form to save players
usernames, passwords and nicknames and just load them next time.

Congratz, you reached to end.
Very possible that you got 2 cents smarter in scar ini writing/reading/deleting.

mixster
03-31-2008, 07:59 PM
Add an example of using with DeclarePlayer's and it's a rep++ from me :)
(I already know how, but it's one of the main reasons for using ini's - well there's script settings, but DecPlay's is more important).

Negaal
03-31-2008, 08:12 PM
Thanks...If I dont forget it tomorrow I'll make one(player form) and add it here.Bit late atm.

Thanks:)