Menu

#8 inilib does not handle comments in ini files

open
nobody
None
5
2012-09-14
2003-01-30
No

My understanding is that ini files are allowed to have
comments in them -- comments start with a semicolon
(;). For long and complicated ini files, comments are
quite necessary.

inilib does not handle comments -- comment lines are
attached to the next key. For example:

; this line is a comment
foo = bar

ends up as

key := 'this line is a comment\nfoo
value := bar

which is clearly not as intended.

AFAIK, there are two types of comment in an ini file

1) ones that begin a new line, and

2) ones that come after a key value.

I haven't been able to find an ini-file specification that
says that the second type actually exists, but I'm fairly
sure I've seen them around...

The first type are easily handled by the patch attached
(linecomment.patch).

The second type requires a bit more stuffing around. I've
offered a suggestion that allows ; comment at the end of
a line, with a backslash escaping the comment if
needed. I leave it to you to decide on the usefulness and
correctness of this patch. i.e.

foo=bar ;this is a comment
quux=bar \;not a comment

FYI, both patches were made using:

       diff -Naur src/registry.cc src-old/registry.cc

>patchfile

and may be applied using:

       cd src; patch -Np1 <patchfile

Also, FYI inilib compiles successfully OOTB using the
cygwin tools with GCC 3.2. Well done for a great library!

Discussion

  • Stuart Prescott

    Stuart Prescott - 2003-01-30

    suggested patch to allow entire line to be a comment if it starts with a semicolon

     
  • Stuart Prescott

    Stuart Prescott - 2003-01-30

    suggested patch to allow last section of a line to be a comment from a semicolon onwards (unless \;)

     
  • Brian W. Barrett

    Logged In: YES
    user_id=44328

    Patches applied to CVS. Still have to figure out how to handle re-writing
    files with comments (which is why we left comments as a gray area in
    first release). Will be pushed into upcoming release (have to fix bug
    828881 as well).

     
  • Stuart Prescott

    Stuart Prescott - 2005-02-07

    Logged In: YES
    user_id=429635

    Great -- thanks.

    A suggestion for how to handle comments in rewriting files
    (you have probably already through along these lines) is to
    "attach" the comment to the key following it -- most people
    put comments on the line preceeding the entry so that would
    work fairly well.

    Another (not entirely unrelated) option is to use a
    diff/patch-like algorithm so that rather than clobbering the
    entire ini-file when writing it out (which I presume is what
    you do now... i don't know as I've only used inilib for
    reading ini flies!) you replace inidividual lines within it,
    inserting new keys at the end of the section (i.e. after the
    last key in it, not just before the new section heading!)
    and new sections at the end of the file.

    Deleting keys or sections would leave the question of what
    to do with comments associated with them as a vexed
    question, of course you could (optionally!) just comment out
    deleted keys rather than deleting them so that the key and
    comment are still in context :) Ouch!

    Rewriting plain-text files (with their subtle nuances of
    ordering and commenting) with GUI tools that make no use of
    such things is a fraught question facing the entire linux
    world and is (IMHO) not being handled very well at the
    moment. (e.g. if I use the www-interface to admin CUPS I get
    a completely different config file to what I get with the
    KDE interface... and then the Debian packager has a third
    completely different file... all of them have the same
    key/value pairs just sufficiently different than manually
    folding/finding changes in them is a right pain in neck.

    So good luck!

    (anyone feel like doing some serious work on
    libguiconfigfilemanager? first job is to come up with a
    shorter name!)

     

Log in to post a comment.