Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-10

TIniFile: how to save bool as string?

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

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!

Silvio Clécio My public projects - github.com/silvioprog
Graeme Geldenhuys
to silvioprog
silvioprog wrote
Is 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 -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/
Reinier Olislagers
to silvioprog: When I use the TIniFile to save my configurations, it saves the boolean values as 0 and 1.
silvioprog wrote 5 lines
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 on extending TInifiles.

Mark Morgan Lloyd
to Reinier Olislagers
Reinier Olislagers wrote
On 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.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
silvioprog
to Reinier Olislagers: 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 <

Show quoted text 12 lines
wrote: On 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 on extending TInifiles.

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 other

Just ideas hehe..

Silvio Clécio My public projects - github.com/silvioprog
JB
to silvioprog

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

silvioprog wrote 98 lines
On Sat, Oct 4, 2014 at 6:28 AM, Reinier Olislagers < > wrote: > On 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 on > extending TInifiles. 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 other Just ideas hehe.. -- Silvio Clécio My public projects - github.com/silvioprog _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
silvioprog
to JB
JB wrote
I 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. ;)

Silvio Clécio My public projects - github.com/silvioprog
Michael Van Canneyt
to silvioprog
silvioprog wrote 10 lines
On Tue, Oct 7, 2014 at 3:58 PM, JB wrote: I 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.

silvioprog
to Michael Van Canneyt

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...

Silvio Clécio My public projects - github.com/silvioprog