Lazarus

Announcements => Third party => Topic started by: FlierMate on January 27, 2021, 05:23:29 pm

Title: An alternative INI files unit
Post by: FlierMate on January 27, 2021, 05:23:29 pm
These are supported commands for INI file access.

Code: Pascal  [Select][+][-]
  1. function ReadProfileString(FileID,SectionID,FieldID:string):string;
  2. function ReadProfileWord(FileID,SectionID,FieldID:string):word;
  3. function ReadProfileByte(FileID,SectionID,FieldID:string):byte;
  4. function ReadProfileBoolean(FileID,SectionID,FieldID:string):boolean;
  5. function ReadProfileInteger(FileID,SectionID,FieldID:string):integer;
  6. function ReadProfileReal(FileID,SectionID,FieldID:string):real;
  7.  
  8. function WriteProfileString(FileID,SectionID,FieldID,ValueStr:string):boolean;
  9. function WriteProfileWord(FileID,SectionID,FieldID:string;ValueWord:word):boolean;
  10. function WriteProfileByte(FileID,SectionID,FieldID:string;ValueByte:byte):boolean;
  11. function WriteProfileBoolean(FileID,SectionID,FieldID:string;ValueBool:boolean):boolean;
  12. function WriteProfileInteger(FileID,SectionID,FieldID:string;ValueInt:integer):boolean;
  13. function WriteProfileReal(FileID,SectionID,FieldID:string;ValueReal:real):boolean;

Example code in Pascal:

Code: Pascal  [Select][+][-]
  1. uses Profile;
  2.  
  3. const
  4.   FileID = 'MYAPP.ini';
  5.  
  6. var
  7.   W:Word;
  8.   B:Byte;
  9.   I:Integer;
  10.   R:Real;
  11.   L:Boolean;
  12.   S:String;
  13.  
  14. begin
  15.   WriteProfileString(FileID,'Player','Name','John');
  16.   W:=65500; WriteProfileWord(FileID,'Player','Key',W);
  17.   B:=127;   WriteProfileByte(FileID,'Settings','Interval',B);
  18.   I:=2244;  WriteProfileInteger(FileID,'Settings','Cycle',I);
  19.   L:=true;  WriteProfileBoolean(FileID,'Settings','AutoRun', L);
  20.   R:=3.1416;WriteProfileReal(FileID,'Score','John',R);
  21.  
  22.  
  23.   S:='';   S:=ReadProfileString(FileID,'Player','Name');
  24.   W:=0;    W:=ReadProfileWord(FileID,'Player','Key');
  25.   B:=0;    B:=ReadProfileByte(FileID,'Settings','Interval');
  26.   I:=0;    I:=ReadProfileInteger(FileID, 'Settings', 'Cycle');
  27.   L:=false;L:=ReadProfileBoolean(FileID,'Settings','AutoRun');
  28.   R:=0;    R:=ReadProfileReal(FileID,'Score',S);
  29.  
  30.   WriteLn('S = ', S);
  31.   WriteLn('W = ', W);
  32.   WriteLn('B = ', B);
  33.   WriteLn('I = ', I);
  34.   WriteLn('L = ', L);
  35.   WriteLn('R = ', R);
  36.  
  37.   Write('Press Enter to quit...'); ReadLn;
  38. end.

This PROFILE.pas unit was written in 1999. It is provided 'as is'.

The USEINI.pas is a newly written example to test PROFILE unit.

MYAPP.ini looks like this:
Quote
[Player]
Key=65500
Name=John

[Settings]
AutoRun=yes
Cycle=2244
Interval=127

[Score]
John= 3.1415999999999999E+000

Please let me know if you found bugs. Thanks!   :P
Title: Re: Add INI files R/W support to Free Pascal
Post by: wp on January 27, 2021, 05:46:36 pm
Why isn't unit inifiles suffcient? It comes with fpc (folder packages/fcl-base)
Title: Re: Add INI files R/W support to Free Pascal
Post by: FlierMate on January 27, 2021, 05:51:36 pm
Why isn't unit inifiles suffcient? It comes with fpc (folder packages/fcl-base)

I was not aware of that. I am going to change the title as 'alternative INI files support unit' but admins or mods might want to move it to less visible subsection.

Thank you for pointing it out.   :)
Title: Re: An alternative INI files unit
Post by: Edson on January 28, 2021, 10:12:28 pm
Just as information, I've written the library https://github.com/t-edson/MiConfig that can read/write variables from/to INI and XML files.

It can associate these variables to LCL controls too.
TinyPortal © 2005-2018