Recent

Author Topic: TiniFile alters inifile comments  (Read 4195 times)

klyxmaster

  • New Member
  • *
  • Posts: 25
TiniFile alters inifile comments
« on: August 17, 2014, 08:15:49 am »
Hi, hope this is in the correct thread :-)

I am using Tinifile, love it.
problem: it is altering two things:
1. removing the whitespace
2. altering the comment lines

example

##################
# this is a comment line
#
# more comment lines
#############
---whitespace here (blank line) -----
key=some data
--whitespace here (blank line) -----
#################
#
# more comments
#
##############

what fpc does to it:

=#################
=# this is a comment line
=#
=# more comment lines
=#############
key=some data (notice whitespace above and below are now gone)
=#################
=#
=# more comments
=#
=##############

this is when i use the line ini.writestring([newdata here])
 
Fortuneately, the app that uses the ini file, seems to be unaffected by these comment changes, and white space, however, I prefer it not to do that.
is there osmething I can do to prevent these changes?


typo

  • Hero Member
  • *****
  • Posts: 3051
Re: TiniFile alters inifile comments
« Reply #1 on: August 17, 2014, 08:52:29 am »
AFAICS, if you include in a line an equal sign, nothing is altered on that line, no matter it has text or not. Blank lines are removed.

I don't know whether this behavior is by design or can be considered a bug.

You could make your comments so, for instance:

Code: [Select]
[section]
==================
=====COMMENT======
==================
key=value
key with spaces=value with spaces

Spaces after the equal sign and spaces which begin lines are also removed.
« Last Edit: August 17, 2014, 09:38:54 am by typo »

howardpc

  • Hero Member
  • *****
  • Posts: 3097
Re: TiniFile alters inifile comments
« Reply #2 on: August 17, 2014, 10:39:10 am »
Blank lines are removed.
I don't know whether this behavior is by design or can be considered a bug.

I'm sure this is by design, not a bug, because some legacy INI implementations do not allow blank lines.
The INI format is a rather informal standard, which since it was deprecated by M$ in favour of the Registry has no international company or organisation to encourage/enforce compatibility with a specific implementation standard (unlike JSON or XML).

nsl

  • New member
  • *
  • Posts: 9
Re: TiniFile alters inifile comments
« Reply #3 on: August 17, 2014, 12:08:57 pm »
In INI files if the first character on a line is a semi-colon (;) then that line is taken to be a comment.
If looks like lines starting with a '#' are taken to be values with an empty key so that when they are written back out the key-value separator (the '=') is added to the start of the string   
Lazarus 2.0 on Windows 10 / MacOS High Sierra

klyxmaster

  • New Member
  • *
  • Posts: 25
Re: TiniFile alters inifile comments
« Reply #4 on: August 17, 2014, 02:06:22 pm »
Good replies and all true. I remember the good ole days LOL
Yes it is true ini,cfg etc files used to start with ";" as a "standard" in comments
whitespace was still iffy, whether it was allowed.

@typo, unfortunately, I am working with existing projects - and the ini files have their own standard for the last several years. They are just large and I wrote a simple editor that allows a user to pick one of the ini files, and edit. Just easier than opening notepad over and over and scanning through long lines of config settings.

so far it doesn't seem to affect the app.

anyone have a trick to prevent the "=#" scenario? I can probably live without the whitespace, as to this day, no config file uses it.



howardpc

  • Hero Member
  • *****
  • Posts: 3097
Re: TiniFile alters inifile comments
« Reply #5 on: August 17, 2014, 03:48:42 pm »
anyone have a trick to prevent the "=#" scenario?

I have no trick, but you can alter the behaviour to what you want by changing the IsComment function of the Inifiles unit. Unfortunately it is a stand-alone function, not a virtual method of TIniFile which could be overridden without needing to tinker with the source. Simply change it to the following:

Code: [Select]

function IsComment(const AString: string): boolean;
const
  Comments: set of Char =[';','#'];
var
  c: char;
begin
  Result := False;
  if (AString > '') then
    Result:=(AString[1] in Comments);
end;

Rather than alter the sources, I would just copy the source to your project directory, renaming to something other than inifile, and put the altered file in your uses clause, rather than inifile.