Recent

Author Topic: A Question about ini files?  (Read 2287 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
A Question about ini files?
« on: January 26, 2018, 07:20:44 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms,
  9.   IniFiles, Controls, Graphics, Dialogs, StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Button2: TButton;
  18.     Edit1: TEdit;
  19.     Edit2: TEdit;
  20.     Edit3: TEdit;
  21.     Edit4: TEdit;
  22.     Edit5: TEdit;
  23.     Edit6: TEdit;
  24.     Label1: TLabel;
  25.     Label2: TLabel;
  26.     Label3: TLabel;
  27.     Label4: TLabel;
  28.     Label5: TLabel;
  29.     Label6: TLabel;
  30.     procedure Button1Click(Sender: TObject);
  31.     procedure Button2Click(Sender: TObject);
  32.     procedure FormCreate(Sender: TObject);
  33.     Procedure GetIni;
  34.   private
  35.  
  36.   public
  37.  
  38.   end;
  39.  
  40. const
  41.   C_DB_SECTION = 'DB-INFO';
  42.  
  43. var
  44.   Form1: TForm1;
  45.  
  46.   INI                  : TINIFile;
  47.   Author               : String;
  48.   Pass                 : String;
  49.   DBFile               : String;
  50.   MaxAttempts          : integer;
  51.   PassEnter            : String;
  52.   TryCount             : integer;
  53.   iniPath              : String;
  54.  
  55. implementation
  56.                                heaptrc
  57. {$R *.lfm}
  58.  
  59. { TForm1 }
  60.  
  61. procedure TForm1.Button1Click(Sender: TObject);
  62. begin
  63.   Close;
  64. end;
  65.  
  66. procedure TForm1.Button2Click(Sender: TObject);
  67. begin
  68.     Getini;
  69. end;
  70.  
  71. procedure TForm1.FormCreate(Sender: TObject);
  72.  
  73. begin
  74.   iniPath    := Application.Location;
  75.   Edit1.Text := iniPath;
  76. end;
  77.  
  78. Procedure TForm1.GetIni;
  79. Begin
  80.      INI := TINIFile.Create('DB.ini');
  81.  
  82.  // Put reading the INI file inside a try/finally block to prevent
  83.  // memory leaks
  84.       try
  85.         // Demonstrates reading values from the INI file.
  86.         Author       := INI.ReadString(C_DB_SECTION,'Author','');
  87.         Pass         := INI.ReadString(C_DB_SECTION,'Pass','');
  88.         DBFile       := INI.ReadString(C_DB_SECTION,'DBFile','');
  89.         MaxAttempts  := INI.ReadInteger(C_DB_SECTION,'MaxAttempts',1);
  90.  
  91.        // Show Results in Edit boxes on the screen
  92.  
  93.        Edit2.Text :=   Author;
  94.        Edit3.Text :=   Pass;
  95.        Edit4.Text :=   DBFile;
  96.        Edit5.Text :=   inttostr(MaxAttempts);
  97.        Edit6.Text :=   'Process End';
  98.       finally
  99.      INI.Free;
  100.  
  101.       end;
  102.  
  103. end;
  104.  
  105.  
  106.  
  107. end.
  108.                                      

Have written the above and it works.


Line 41 C_DB_SECTION = 'DB-INFO'; specifies the section in the ini file.
Line 80 I guess creates a file handle and Specifies the ini file name
to read.
Line 86 - 89 Does one read of the ini file.
Line 99 Closes the ini file.

Do you need a const defined for each section in an ini file.

I have an ini file set up with multi sections like so.

[Version]
Version=v3.0

[AIRAC]
Cycles=1801
PrevCycle=1713

[SimsDirPath]
X-Plane=F:\TestBed\X-Plane\
X-PlaneBackup=F:\TestBed\X-PlaneBackup\
Prepar3D=F:\TestBed\Prepar3D


[CheckBoxes]
CBX1=False
CBX2=False
CBX3=False

[Hangar]
.
.
.

[WWFleet]
.
.
.

Do I need to define a Const for each section. And how do I read multiple
tmes in a section.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

wp

  • Hero Member
  • *****
  • Posts: 11906
Re: A Question about ini files?
« Reply #1 on: January 26, 2018, 07:31:17 pm »
Quote
Do you need a const defined for each section in an ini file.
Using a const is good programming style because you need the value in the reading and writing code at many places. You can also apply the string directly ( Author := INI.ReadString('DB-INFO', 'Author','') but then there's a risk of hard-to-find bugs. Suppose the writing code is INI.WriteString('DB_INFO', 'Author',Author) - do you spot the difference? It will cause your program behave as is if nothing has been written to the ini file. Using a constant for the section name prevents this.

Quote
And how do I read multiple
tmes in a section.
Just by using multiple ReadXXXX commands, just as you do it between lines 86 and 89.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: A Question about ini files?
« Reply #2 on: January 26, 2018, 08:20:44 pm »
So do I understand this right?

For reading the ini file below I would need the following declaration:

Const
VersionSECTION = 'Version';
AIRACSection = 'AIRAC';
SimsDirPathSection = 'SimsDirPath';
CheckBoxesSection = 'CheckBoxes';
HangarSection = 'Hangar';   
WWFleetSection ='WWFleetSection';


The VersionSection only has one record so i would issue 1 read.
Change to AIRACSection and issue two reads so on and so on.

[Version]
Version=v3.0

[AIRAC]
Cycles=1801
PrevCycle=1713

[SimsDirPath]
X-Plane=F:\TestBed\X-Plane\
X-PlaneBackup=F:\TestBed\X-PlaneBackup\
Prepar3D=F:\TestBed\Prepar3D


[CheckBoxes]
CBX1=False
CBX2=False
CBX3=False

[Hangar]
.
.
.

[WWFleet]
.
.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: A Question about ini files?
« Reply #3 on: January 26, 2018, 10:57:17 pm »
You can try the attached Settings Dialog example.
Unzip the attachment into your project directory and add uIni to your main form's uses clause.
You call the dialog with GetPreferencesDlg (perhaps from a SetPreferences button click).
if GetPreferencesDlg is True it indicates the user saved preferences to an inifile. If it is False the user cancelled setting preferences, and no inifile was changed, but generally you can ignore the returned value.
You'll know how to adapt the dialog to do what you want.

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: A Question about ini files?
« Reply #4 on: January 27, 2018, 08:55:19 am »
Quote
Do I need to define a Const for each section. And how do I read multiple
tmes in a section.

You can get all the keys of a section in one go.  Use the TIniFile procedure ReadSection.
Pass it the section name and an initialised TstringList variable, then iterate through the StringList to read the key names.

You can read all the values in a section in one call using the TiniFile procedure ReadSectionValues.
Pass it the section name and an initialised TstringList variable, then iterate through the StringList to read the values.
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

 

TinyPortal © 2005-2018