Skip to content

TCustomIniFile (IniFiles) Bug in ReadBool, documentation error ifoWriteStringBoolean

Summary

When one sets the ifoWriteStringBoolean option (btw: missing in the documentation), "true" values are returned as false by the ReadBool function.

System Information

OS: Windows 11x Architecture: x86-64 Compiler version: 3.2.2 Device: Computer

Steps to reproduce

Execute the example project

Example Project

program project1;

uses IniFiles, SysUtils;

const
  TEST_SECTION = 'TestSection';
  TEST_VALUE = 'TestValue';

var
  IniFile:TIniFile;

begin
  IniFile := TIniFile.Create('Testfile.ini');
  writeln(Format('Setting [%s]%s to true',[TEST_SECTION,TEST_VALUE]));
  iniFile.Options := iniFile.Options + [ifoWriteStringBoolean];
  IniFile.WriteBool(TEST_SECTION,TEST_VALUE,true);
  writeln('Test read (boolean): ',BoolToStr(iniFile.ReadBool(TEST_SECTION,TEST_VALUE,false),'true','false'));
  writeln('Test read (string): ',iniFile.ReadString(TEST_SECTION,TEST_VALUE,'?'));
  FreeAndnil(iniFile);
  Write('Hit [Enter] to exit');
  Readln;
end.

What is the current bug behavior?

IniFile.ReadBool returns false

What is the expected (correct) behavior?

IniFile.ReadBool returns true

Relevant logs and/or screenshots

The output of the above code is:

Setting [TestSection]TestValue to true
Test read (boolean): false
Test read (string): true
Hit [Enter] to exit

Possible fixes

Problem is caused by TCustomIniFile.WriteBool providing default strings for 'true' and 'false', but TCustomIniFile.ReadBool does not. It looks at the first returned character and compares "t" with "1", thus "true" in the ini file becomes false in the code.

A workaround is to specify the BoolTrueStrings array in addition to ifoWriteStringBoolean

  iniFile.Options := iniFile.Options + [ifoWriteStringBoolean];
  iniFile.BoolTrueStrings := ['true']; 
Edited by arminlinder
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information