Results 1 to 3 of 3

Thread: INI writing/reading/deleteing

  1. #1
    Join Date
    Jul 2007
    Posts
    1,431
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default INI writing/reading/deleteing

    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

    SCAR Code:
    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:
    SCAR Code:
    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:

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

    Deleting:
    SCAR Code:
    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.

    SCAR Code:
    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.
    [CENTER][SIZE="4"]Inactive[/SIZE]I forgot my password[/CENTER]

  2. #2
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    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).
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  3. #3
    Join Date
    Jul 2007
    Posts
    1,431
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks...If I dont forget it tomorrow I'll make one(player form) and add it here.Bit late atm.

    Thanks
    [CENTER][SIZE="4"]Inactive[/SIZE]I forgot my password[/CENTER]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Do you like writing?
    By lakerzz8 in forum Discussions & Debates
    Replies: 3
    Last Post: 02-24-2008, 08:23 PM
  2. Reading and Writing to file
    By king vash in forum OSR Help
    Replies: 2
    Last Post: 01-22-2008, 03:20 PM
  3. C++: Writing and reading files
    By Repentinus in forum C/C++ Help and Tutorials
    Replies: 3
    Last Post: 11-11-2007, 04:55 PM
  4. Registry writing
    By rotflmfwao in forum OSR Help
    Replies: 4
    Last Post: 08-20-2007, 08:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •