Difference between revisions of "Using INI Files"
(→Basic Information: small language fixes; very briefly explain what an ini file) |
m (Fixed syntax highlighting; deleted category included in page template) |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{INI Files}} | {{INI Files}} | ||
− | |||
− | ===Basic Information== | + | =INI Files= |
− | INI | + | |
+ | ==Basic Information== | ||
+ | |||
+ | INI files are text files that store key/value pairs that are grouped in sections. | ||
+ | |||
+ | INI files can be used to save user settings easily. With the [http://www.freepascal.org/docs-html/fcl/inifiles/index.html IniFiles unit] and the [http://www.freepascal.org/docs-html/fcl/inifiles/tinifile.html TINIFile class] you can easily work with them. The IniFiles unit is part of the FCL. | ||
− | + | Currently only INI files compliant with the more restrictive Windows ini-file format are supported. I.e. using [http://en.wikipedia.org/wiki/INI_file#Sections sections] is mandatory and only the semicolon can be used for comments. | |
− | Currently only INI files compliant with the [http://en.wikipedia.org/wiki/INI_file | + | For more general information about ini-files read [http://en.wikipedia.org/wiki/INI_file this]. |
+ | |||
+ | ==INI Files== | ||
− | |||
INI Files use brackets to create and mark '''Sections''', which contain '''keys''' and key '''values'''. | INI Files use brackets to create and mark '''Sections''', which contain '''keys''' and key '''values'''. | ||
A Key and its corresponding Value are separated with an equals sign (Key=Value). | A Key and its corresponding Value are separated with an equals sign (Key=Value). | ||
Line 14: | Line 19: | ||
Section names are put inside square brackets ([Section]). | Section names are put inside square brackets ([Section]). | ||
− | Comments are | + | Comments are permitted and are marked with a semicolon (;) at the beginning of a line. |
− | |||
− | |||
An example ini file: | An example ini file: | ||
Line 24: | Line 27: | ||
; empty lines are ignored | ; empty lines are ignored | ||
− | |||
− | |||
− | |||
− | |||
− | |||
[General] | [General] | ||
Line 36: | Line 34: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ==Ini file reading example== | |
− | The console application below shows how to read ini files. To test it | + | The console application below shows how to read ini files. To test it, create an ini file with the name "DB.ini" and the contents below, and save it in the same folder as the executable program. |
<syntaxhighlight lang="ini"> | <syntaxhighlight lang="ini"> | ||
− | [ | + | [DB-INFO] |
− | |||
Author=Adam | Author=Adam | ||
− | Pass= | + | Pass=secret |
+ | MaxAttempts=5 | ||
DBFile=C:\Money.dat | DBFile=C:\Money.dat | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | The code to read this ini file: | |
− | <syntaxhighlight> | + | |
− | Program | + | <syntaxhighlight lang=pascal> |
+ | Program IniReadExample; | ||
{$mode objfpc}{$H+} | {$mode objfpc}{$H+} | ||
− | + | uses | |
− | + | classes, sysutils, IniFiles; | |
− | + | const | |
− | + | C_DB_SECTION = 'DB-INFO'; | |
− | |||
− | |||
+ | var | ||
+ | INI: TINIFile; | ||
+ | Author, Pass, DBFile: String; | ||
+ | MaxAttempts: integer; | ||
+ | PassEnter: String; | ||
+ | TryCount: integer; | ||
begin | begin | ||
− | // Create the object, specifying the | + | // Create the object, specifying the the ini file that contains the settings |
− | INI := TINIFile.Create(' | + | INI := TINIFile.Create('DB.ini'); |
− | // | + | |
− | // | + | // Put reading the INI file inside a try/finally block to prevent memory leaks |
− | + | try | |
− | + | // Demonstrates reading values from the INI file. | |
− | + | Author := INI.ReadString(C_DB_SECTION,'Author',''); | |
− | + | Pass := INI.ReadString(C_DB_SECTION,'Pass',''); | |
− | + | DBFile := INI.ReadString(C_DB_SECTION,'DBFile',''); | |
− | + | MaxAttempts := INI.ReadInteger(C_DB_SECTION,'MaxAttempts',1); | |
− | + | ||
− | + | // Do something with the values read; e.g. verify the password | |
− | + | if Pass <> '' then | |
− | + | begin | |
− | + | // Ask for the password, max attempts = MaxAttempts | |
+ | TryCount := MaxAttempts; | ||
+ | repeat | ||
+ | write('Please enter password; ', TryCount, ' attempt(s) left: '); | ||
+ | readln(PassEnter); | ||
+ | dec(TryCount); | ||
+ | if (PassEnter <> Pass) and (TryCount > 0) then | ||
+ | writeln('Wrong Password, please try again'); | ||
+ | until(PassEnter = Pass) or (TryCount = 0); | ||
+ | |||
+ | // Correct password given? | ||
+ | if PassEnter = Pass then | ||
+ | writeln('Correct Password.') | ||
+ | else | ||
+ | writeln('Invalid password, but maxiumm number of password attempts reached.'); | ||
+ | end; | ||
+ | |||
+ | writeln('Author : ', Author); | ||
+ | writeln('File : ', DBFile); | ||
+ | writeln('Password : ', Pass); | ||
+ | writeln('Max password attempts: ', MaxAttempts); | ||
+ | writeln; | ||
+ | write('Press Enter to close...'); | ||
+ | Readln; | ||
+ | |||
+ | finally | ||
+ | // After the ini file was used it must be freed to prevent memory leaks. | ||
+ | INI.Free; | ||
end; | end; | ||
− | + | end. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | end. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | ==Objects to know== | |
+ | |||
In the TINIFile class there are many different properties, procedures and functions that can be used. | In the TINIFile class there are many different properties, procedures and functions that can be used. | ||
Line 108: | Line 131: | ||
===Reference Documentation=== | ===Reference Documentation=== | ||
+ | |||
Here: [http://lazarus-ccr.sourceforge.net/docs/fcl/inifiles/index.html Free Pascal documentation on INI files] | Here: [http://lazarus-ccr.sourceforge.net/docs/fcl/inifiles/index.html Free Pascal documentation on INI files] | ||
− | + | = See also = | |
+ | |||
* [[HistoryFiles]] | * [[HistoryFiles]] | ||
* [[XML Tutorial]] | * [[XML Tutorial]] | ||
− | |||
− | |||
− |
Revision as of 09:04, 4 March 2020
│
العربية (ar) │
Deutsch (de) │
English (en) │
español (es) │
suomi (fi) │
français (fr) │
polski (pl) │
русский (ru) │
中文(中国大陆) (zh_CN) │
INI Files
Basic Information
INI files are text files that store key/value pairs that are grouped in sections.
INI files can be used to save user settings easily. With the IniFiles unit and the TINIFile class you can easily work with them. The IniFiles unit is part of the FCL.
Currently only INI files compliant with the more restrictive Windows ini-file format are supported. I.e. using sections is mandatory and only the semicolon can be used for comments. For more general information about ini-files read this.
INI Files
INI Files use brackets to create and mark Sections, which contain keys and key values. A Key and its corresponding Value are separated with an equals sign (Key=Value).
Section names are put inside square brackets ([Section]).
Comments are permitted and are marked with a semicolon (;) at the beginning of a line.
An example ini file:
; Comment. Beginning of INI file
; empty lines are ignored
[General]
; This starts a General section
Compiler=FreePascal
; Key Compiler and value FreePascal
Ini file reading example
The console application below shows how to read ini files. To test it, create an ini file with the name "DB.ini" and the contents below, and save it in the same folder as the executable program.
[DB-INFO]
Author=Adam
Pass=secret
MaxAttempts=5
DBFile=C:\Money.dat
The code to read this ini file:
Program IniReadExample;
{$mode objfpc}{$H+}
uses
classes, sysutils, IniFiles;
const
C_DB_SECTION = 'DB-INFO';
var
INI: TINIFile;
Author, Pass, DBFile: String;
MaxAttempts: integer;
PassEnter: String;
TryCount: integer;
begin
// Create the object, specifying the the ini file that contains the settings
INI := TINIFile.Create('DB.ini');
// Put reading the INI file inside a try/finally block to prevent memory leaks
try
// Demonstrates reading values from the INI file.
Author := INI.ReadString(C_DB_SECTION,'Author','');
Pass := INI.ReadString(C_DB_SECTION,'Pass','');
DBFile := INI.ReadString(C_DB_SECTION,'DBFile','');
MaxAttempts := INI.ReadInteger(C_DB_SECTION,'MaxAttempts',1);
// Do something with the values read; e.g. verify the password
if Pass <> '' then
begin
// Ask for the password, max attempts = MaxAttempts
TryCount := MaxAttempts;
repeat
write('Please enter password; ', TryCount, ' attempt(s) left: ');
readln(PassEnter);
dec(TryCount);
if (PassEnter <> Pass) and (TryCount > 0) then
writeln('Wrong Password, please try again');
until(PassEnter = Pass) or (TryCount = 0);
// Correct password given?
if PassEnter = Pass then
writeln('Correct Password.')
else
writeln('Invalid password, but maxiumm number of password attempts reached.');
end;
writeln('Author : ', Author);
writeln('File : ', DBFile);
writeln('Password : ', Pass);
writeln('Max password attempts: ', MaxAttempts);
writeln;
write('Press Enter to close...');
Readln;
finally
// After the ini file was used it must be freed to prevent memory leaks.
INI.Free;
end;
end.
Objects to know
In the TINIFile class there are many different properties, procedures and functions that can be used.
CaseSensitive - This property allows you to say if the keys and sections are case sensitive or not. By default they aren't.
ReadString - Has 3 constant statements. The first one is the section to search in. The second one is the key to look for. The third one is a default string in case the key and/or section searched for is not found.
WriteString - has three constant statements, too. The first is the section. The second is the key and the last is the value that you want to write. If the key and section exist already, the key will be overwritten with the new value..
ReadSections - Will allow you to to take the sections from the INI file and put them in a TStrings class (or TStringList with the AS operator).
DeleteKey - Remove an existing key from a specific section.
EraseSection - Remove a section and all its data.
There are more procedures and functions but this is enough to get you started.
Reference Documentation
Here: Free Pascal documentation on INI files