[fpc-pascal] TIniFile: how to save bool as string?

silvioprog silvioprog at gmail.com
Tue Oct 7 20:46:47 CEST 2014


On Sat, Oct 4, 2014 at 6:28 AM, Reinier Olislagers <
reinierolislagers at gmail.com> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20141007/a9f7447f/attachment.html>


More information about the fpc-pascal mailing list