Hello,
When I use the TIniFile to save my configurations, it saves the boolean values as 0 and 1.
Is there any way to save these values as string (true/false) instead of integer (0/1)? If not, can I send a patch to implement that?!
Thank you!

Mailing lists / fpc-pascal / 2014-10
9 messages · 6 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hello,
When I use the TIniFile to save my configurations, it saves the boolean values as 0 and 1.
Is there any way to save these values as string (true/false) instead of integer (0/1)? If not, can I send a patch to implement that?!
Thank you!
silvioprog wroteIs there any way to save these values as string (true/false) instead of integer (0/1)?
Yes, simply create a descendant of TINIFile, override the WriteBool() virtual method and use BoolToStr() instead of BoolToChar() in its implementation.
The same then needs to be done for ReadBool() as well.
Regards,
- Graeme -
When I use the TIniFile to save my configurations, it saves the boolean values as 0 and 1.
1. Write your own wrapper?
2. AFAIR, there already is a bug report+patch in the bug tracker on extending TInifiles.
Reinier Olislagers wroteOn 03/10/2014 21:16, silvioprog wrote:> When I use the TIniFile to save my configurations, it saves the boolean> values as 0 and 1.> > Is there any way to save these values as string (true/false) instead> of integer (0/1)? If not, can I send a patch to implement that?! 1. Write your own wrapper?2. AFAIR, there already is a bug report+patch in the bug tracker onextending TInifiles.
At least if it saves (and presumably expects) 0 and 1 it's protected from internationalisation issues e.g. if a template .ini is supplied with an app.
I hacked a wrapper some while ago for my own use, which tries to preserve the form of the variable (i.e. if it's currently "True" then "False" is written, but if it's "yes" then "no" is written"). That's obviously massively problematic if multiple languages have to be supported.
1. Write your own wrapper? 2. AFAIR, there already is a bug report+patch in the bug…
On Sat, Oct 4, 2014 at 6:28 AM, Reinier Olislagers <
Thanks.
Internally it uses this functions:
...
function CharToBool(AChar: char): boolean;
begin
Result := (Achar = '1');
end;
function BoolToChar(ABool: boolean): char;
begin
if ABool then
Result := '1'
else
Result := '0';
end;
...This functions could be replaced by functions from SysUtils functions, allowing compatibility with words like 0/1 (as is now), true/false, yes/no (USA), sim/não (Brazil), foo/bar (any) etc. Then:
Instead of:
function TCustomIniFile.ReadBool(const Section, Ident: string; Default:
Boolean): Boolean;
var
s: string;
begin
Result := Default;
s := ReadString(Section, Ident, '');
if s > '' then
Result := CharToBool(s[1]);
end;Could be:
function TCustomIniFile.ReadBool(const Section, Ident: string; Default:
Boolean): Boolean;
var
s: string;
begin
Result := Default;
s := ReadString(Section, Ident, '');
if s > '' then
Result := SysUtils.StrToBoolDef(s[1], False);
end;And, instead of:
procedure TCustomIniFile.WriteBool(const Section, Ident: string; Value:
Boolean);
begin
WriteString(Section, Ident, BoolToChar(Value));
end;Could be:
procedure TCustomIniFile.WriteBool(const Section, Ident: string; Value:
Boolean);
begin
WriteString(Section, Ident, SysUtils.BoolToStr(Value, True));
end;So I could use with:
initialization
SetLength(SysUtils.TrueBoolStrs, 1);
SetLength(SysUtils.FalseBoolStrs, 1);
SysUtils.TrueBoolStrs[0] := 'true'; // or other
SysUtils.FalseBoolStrs[0] := 'false'; // or otherJust ideas hehe..
I usually use integer cast to boolean values, sample:
var
cond: boolean;
i: integer;
begin
cond := true;
writeln(inttostr(integer(cond)));
i := 0;
if boolean(i) then
writeln('true')
else
writeln('false');
end;best regards
José Benedito
JBS Soluções
Consulting Systems Development
c
www.jbsolucoes.net
skype:
+55 22 30251654
+55 22 996064279
JB wroteI usually use integer cast to boolean values, sample:
[...]
But the main ideia to change it is allow any type supported by SysUtils, like integer (-1/0 or 0/1), strings (true/false, yes/no, y/n, sim/não or other) etc.
My patch keeps the current behavior of the INI class, but allows the programmer to internationalize that to the your favorite way. You prefer 0/1, I prefer true/false, other prefer yin/yang etc., my patch allow to use in any way. ;)
Unfortunately, this is not quite good enough.
The read part is OK, but we need to make sure that the write part is compatible with other code out there. It is OK when all code accessing the ini file uses the same conventions but this condition is not always guaranteed.
If your code writes 'true' and my code doesn't handle 'true' because it uses an older version of TIniFile, then this is a problem.
So, we'll need a property that tells TIniFile to write using the new properties (by default false), and check the value of this property when writing new boolean values.
Michael.
On Wed, Oct 8, 2014 at 8:40 AM, Michael Van Canneyt wrote:
[...]
So, we'll need a property that tells TIniFile to write using the new properties (by default false), and check the value of this property when writing new boolean values.
Very nice idea Michael! I'll send a new patch with this tip...