Sign in Join

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

Mailing lists / fpc-pascal / 2017-08

TInifile does not handle " " (space) as a value.

10 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Torsten Bonde Christiansen

Dear List.

I was trying to use the ini file for our program to store a single space " " as a value, but this does not seem to be posible.

From the Wikipedia definition of INI file
(https://en.wikipedia.org/wiki/INI_file#Whitespace) whitespace in values does not seem to be well defined, so it may just be a design choice.

The following illustrates the issue:

program Project1;
{$mode objfpc}{$H+}
uses
   Classes, IniFiles;
var
   Ini: TIniFile;
   S: String;
begin
   Ini := TIniFile.Create('/tmp/test.ini');
   Ini.WriteString('sec1', 'S', ' ');
   Ini.Free;
   Ini := TIniFile.Create('/tmp/test.ini');
   S := Ini.ReadString('sec1', 'S', 'ZZ'); // S is '' and not ' ' as
expected
   Ini.Free;
end.

Is this considered a bug or "by design"?

Kind regards
Torsten Bonde Christiansen

Reply by email

Michael Van Canneyt
to Torsten Bonde Christiansen
Torsten Bonde Christiansen wrote 34 lines
Dear List. I was trying to use the ini file for our program to store a single space " " as a value, but this does not seem to be posible. From the Wikipedia definition of INI file (https://en.wikipedia.org/wiki/INI_file#Whitespace) whitespace in values does not seem to be well defined, so it may just be a design choice. The following illustrates the issue: program Project1; {$mode objfpc}{$H+} uses Classes, IniFiles; var Ini: TIniFile; S: String; begin Ini := TIniFile.Create('/tmp/test.ini'); Ini.WriteString('sec1', 'S', ' '); Ini.Free; Ini := TIniFile.Create('/tmp/test.ini'); S := Ini.ReadString('sec1', 'S', 'ZZ'); // S is '' and not ' ' as expected Ini.Free; end. Is this considered a bug or "by design"?

This is by design.
As you said, whitespace is not so well defined.
Probably an option can be added not to trim whitespace, but I fear this will lead to more inconsistencies.

As a workaround, try quoting your spaces, something like " "

Michael.

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt
Michael Van Canneyt wrote
> Is this considered a bug or "by design"? This is by design. As you said, whitespace is not so well defined.

Torsten, if you can, maybe switch to JSON files for config setting instead. I have had fantastic success with JSON files in such a way. If it doesn't yet exist (it probably does), one could write a API wrapper (think Adapter design pattern) giving JSON files the same or very similar interface to TINIFile - it could be handy for a transition period.

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 11 lines
On 2017-08-04 08:47, Michael Van Canneyt wrote: >> Is this considered a bug or "by design"? > This is by design. > As you said, whitespace is not so well defined. Torsten, if you can, maybe switch to JSON files for config setting instead. I have had fantastic success with JSON files in such a way. If it doesn't yet exist (it probably does), one could write a API wrapper (think Adapter design pattern) giving JSON files the same or very similar interface to TINIFile - it could be handy for a transition period.

Actually, I have that class on my todo list...

Michael.

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys: Torsten, if you can, maybe switch to JSON files for config setting instead. I have had…
Graeme Geldenhuys wrote 11 lines
On 2017-08-04 08:47, Michael Van Canneyt wrote: >> Is this considered a bug or "by design"? > This is by design. > As you said, whitespace is not so well defined. Torsten, if you can, maybe switch to JSON files for config setting instead. I have had fantastic success with JSON files in such a way. If it doesn't yet exist (it probably does), one could write a API wrapper (think Adapter design pattern) giving JSON files the same or very similar interface to TINIFile - it could be handy for a transition period.

You ask, we deliver. Revision 36913, package fcl-json.

Unit jsonini.pp contains TJSONIniFile. It is a descendent of TCustomIniFile.

Class method TJSONIniFile.ConvertIni will convert an .ini file to a .json file. It attempts to use native types when possible.

ini2json example program included.

One item scratched off my long TODO list ;)

Michael.

Reply by email

Sven Barth
to Michael Van Canneyt
"Michael Van Canneyt" wrote 15 lines
On Mon, 14 Aug 2017, Graeme Geldenhuys wrote: > On 2017-08-04 08:47, Michael Van Canneyt wrote: >>> >>> Is this considered a bug or "by design"? >> >> This is by design. >> As you said, whitespace is not so well defined. > > > > Torsten, if you can, maybe switch to JSON files for config setting

instead. I have had fantastic success with JSON files in such a way. If it doesn't yet exist (it probably does), one could write a API wrapper (think Adapter design pattern) giving JSON files the same or very similar interface to TINIFile - it could be handy for a transition period.

Show quoted text 5 lines
You ask, we deliver. Revision 36913, package fcl-json. Unit jsonini.pp contains TJSONIniFile. It is a descendent of

TCustomIniFile.

Class method TJSONIniFile.ConvertIni will convert an .ini file to a .json

file.

Show quoted text 5 lines
It attempts to use native types when possible. ini2json example program included. One item scratched off my long TODO list ;)

Not entirely: "make install" fails. It complains about missing jsonini.rst file :P

Regards,
Sven

Reply by email

Michael Van Canneyt
to Sven Barth
Sven Barth wrote 37 lines
Am 15.08.2017 11:17 schrieb "Michael Van Canneyt" : > > > > On Mon, 14 Aug 2017, Graeme Geldenhuys wrote: > >> On 2017-08-04 08:47, Michael Van Canneyt wrote: >>>> >>>> Is this considered a bug or "by design"? >>> >>> This is by design. >>> As you said, whitespace is not so well defined. >> >> >> >> Torsten, if you can, maybe switch to JSON files for config setting instead. I have had fantastic success with JSON files in such a way. If it doesn't yet exist (it probably does), one could write a API wrapper (think Adapter design pattern) giving JSON files the same or very similar interface to TINIFile - it could be handy for a transition period. > > > You ask, we deliver. Revision 36913, package fcl-json. > > Unit jsonini.pp contains TJSONIniFile. It is a descendent of TCustomIniFile. > > Class method TJSONIniFile.ConvertIni will convert an .ini file to a .json file. > It attempts to use native types when possible. > > ini2json example program included. > > One item scratched off my long TODO list ;) Not entirely: "make install" fails. It complains about missing jsonini.rst file :P

Already fixed too :)

Michael.

Reply by email

Graeme Geldenhuys
to Michael Van Canneyt: You ask, we deliver. Revision 36913, package fcl-json. Unit jsonini.pp contains…
Michael Van Canneyt wrote
You ask, we deliver. Revision 36913, package fcl-json. Unit jsonini.pp contains TJSONIniFile. It is a descendent of TCustomIniFile.

What a show off!!! :-P

hehehe.... Nicely done Michael, that was very fast. :)

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 10 lines
On 2017-08-15 10:17, Michael Van Canneyt wrote: > You ask, we deliver. Revision 36913, package fcl-json. > > Unit jsonini.pp contains TJSONIniFile. It is a descendent of TCustomIniFile. What a show off!!! :-P hehehe.... Nicely done Michael, that was very fast. :)

Well, it was on my TODO list anyway.
I was glad to be able to tick it off, it was fun to do.

Michael.

Reply by email

Sven Barth
to Michael Van Canneyt: Already fixed too :) Michael.
"Michael Van Canneyt" wrote 28 lines
On Tue, 15 Aug 2017, Sven Barth via fpc-pascal wrote: > Am 15.08.2017 11:17 schrieb "Michael Van Canneyt" < : >> >> >> >> >> On Mon, 14 Aug 2017, Graeme Geldenhuys wrote: >> >>> On 2017-08-04 08:47, Michael Van Canneyt wrote: >>>>> >>>>> >>>>> Is this considered a bug or "by design"? >>>> >>>> >>>> This is by design. >>>> As you said, whitespace is not so well defined. >>> >>> >>> >>> >>> Torsten, if you can, maybe switch to JSON files for config setting > > instead. I have had fantastic success with JSON files in such a way. If

it

> doesn't yet exist (it probably does), one could write a API wrapper

(think

Show quoted text 13 lines
> Adapter design pattern) giving JSON files the same or very similar > interface to TINIFile - it could be handy for a transition period. >> >> >> >> You ask, we deliver. Revision 36913, package fcl-json. >> >> Unit jsonini.pp contains TJSONIniFile. It is a descendent of > > TCustomIniFile. >> >> >> Class method TJSONIniFile.ConvertIni will convert an .ini file to a

.json

Show quoted text 11 lines
> > file. >> >> It attempts to use native types when possible. >> >> ini2json example program included. >> >> One item scratched off my long TODO list ;) > > > Not entirely: "make install" fails. It complains about missing

jsonini.rst

> file :P Already fixed too :)

Thanks!

Regards,
Sven

Reply by email