[fpc-devel] TIniFile with option ifoWriteStringBoolean

Armin Linder arminlinder at arminlinder.de
Wed Mar 16 13:15:56 CET 2022


Already filed incident #39625

My way to express the issue was that if you set the 
ifoWriteStringBoolean option but do not assign the string array(s), 
WriteBool will write a string to the ini file which ReadBool cannot 
correctly read back. Furthermore, since the ifoWriteStringBoolean  
option is missing in the documentation, one cannot now.

IMHO, such a behavior is not intentional.

Armin (aka Nimral)

Am 16/03/2022 um 13:04 schrieb Bart via fpc-devel:
> Hi,
>
> User Nimral in the wiki noted this:
> If you specify ifoWriteStringBoolean in Options for TIniFile _and_ you
> forget to assign values to BoolTrueStrings/BoolFalseStrings, then
> writing and reading boolean values is not symmetrical.
> Writing a boolean will write either 'true' or 'false', but reading
> calls CharToBool on the found string.
>
> Consider this piece of code:
>    Ini := TIniFile.Create('test.ini');
>    Ini.Options := [ifoWriteStringBoolean]; //[ifoWriteStringBoolean]
>    Ini.WriteBool('Section','True',True);
>    Ini.WriteBool('Section','False',False);
>    Ini.Free;
>
> Creates an inifile like:
> [Section]
> True=true
> False=false
>
> Now, with the same options for TIniFile, read the booleans:
>    Ini := TIniFile.Create('test.ini');
>    Ini.Options := [ifoWriteStringBoolean];
>    B := Ini.ReadBool('Section','True',False);
>    Check(B, True); //Found FALSE, Expected TRUE: FAIL
>    B := Ini.ReadBool('Section','False',True);
>    Check(B, False); //Found FALSE, Expected FALSE: OK
>    Ini.Free;
>
> I would have expected that, with exactly the same options for
> TIniFile, if you read from the ini what you wrote to that ini, you
> would get the same value back.
>
> To me, it would have made more sense to either:
> - let WriteBool write '1' or '0' if BoolTrueStrings/BoolFalseStrings are empty
> or
> - let ReadBool compare to 'true' / 'false' if
> BoolTrueStrings/BoolFalseStrings are empty
> (
> or
> - initilaze BoolTrueStrings to ['1'] and BoolFalseStrings to ['0']
> )
>
> This may very well be Delphi compatible (I cannot test that nor can
> find info in Delphi's DocWiki), but if it is not:
> Is this by design or is it a bug?
>
> Bart
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20220316/eb75352c/attachment.htm>


More information about the fpc-devel mailing list