Git Product home page Git Product logo

Comments (9)

fluffynuts avatar fluffynuts commented on August 21, 2024

There's been a lot of discussion over commenting in INI files, some of it here: https://stackoverflow.com/questions/1378219/do-standard-windows-ini-files-allow-comments. The best spec for INI file comments is here: https://en.wikipedia.org/wiki/INI_file#Comments (and this doesn't even suggest lines with trailing comments, though I've seen the windows ini parser deal with this before).

These all suggest that the correct commenting character for INI is the semi-colon (;). However, semi-colons may be put into quoted strings: https://en.wikipedia.org/wiki/INI_file#Quoted_values

Is there any reason why your INI file can't look like the following?

[PATHS]
; The line below contains the SQL connection string to customer DB
DBPATH="Driver={SQL Server};Server=192.168.85.130\SQLEXPRESS02;DATABASE=OneSQL;UID=sa;PWD=pazzword;TRUSTED_CONECTION=NO;CONNECTION TIMEOUT=60;"

I've proven that this is already supported with

public void ShouldNotConsiderSemiColonsInQuotedValuesToBeCommentDelimiters()
{
// Arrange
using var tmp = new AutoDeletingTempFile();
var expected = "DRIVER={SQL Server};SERVER=192.168.1.2\\SQLEXPRESS02;DATABASE=MyDb;UID=sa;PWD=sneaky;TRUSTED_CONNECTION=NO;CONNECTION TIMEOUT=60;";
var src = new[]
{
"[PATHS]",
"; The line below contains the SQL connection string to customer DB",
$"DBPATH=\"{expected}\""
};
File.WriteAllBytes(
tmp.Path,
Encoding.UTF8.GetBytes(
string.Join(Environment.NewLine, src)
)
);
// Act
var ini = Create(tmp.Path);
// Assert
Expect(ini["PATHS"]["DBPATH"])
.To.Equal(expected);
}
}

This is not to tell you that the solution to your problem is simply to do it "my way" - part of the reason for ubiquitous formats like INI is that so many things understand how to read them. If you try to read the INI file as described in this issue with another tool, it will exhibit the same result - making your INI file no longer portable πŸ˜”

If there is no way around this behavior for you, I'm willing to expose a protected field on BestEffortLineParser, CommentDelimiter which you can use to set the comment delimiter in a derived class (much as you are doing now, and it will also cope with lines commented by whatever you please).
However, I would heartily discourage such usage as, at the expense of repeating myself, it means that your INI files wouldn't be portable - they would only be readable by your code.

from peanutbutter.

AxelTechnology avatar AxelTechnology commented on August 21, 2024

"Is there any reason why your INI file can't look like the following?"

It's about legacy code and legacy files, which I have to live with.
About portability, that's not really a requirement.

from peanutbutter.

fluffynuts avatar fluffynuts commented on August 21, 2024

Ah, fair enough - so I'll commit the changes I've made - you're already having to do custom work to parse these files, and hopefully this makes your custom work a little easier:

  • inherit from BestEffortLineParser
  • set the protected CommentDelimiter field in your derived class
    Changes are pushed to 7a6752c - I hope it helps (: I'll push packages now (should be version 2.0.62).

I'd also suggest, if possible, adding a "red bin" item somewhere in your worklog to fix up these ini files if it won't affect other parts of your system - you could use PeanutButter.INI to do so: load with your custom parser and write back out again (INIFile always writes out consistent formatting). Perhaps I'm being too pedantic about portability tho πŸ˜‰

from peanutbutter.

AxelTechnology avatar AxelTechnology commented on August 21, 2024

Is it possible that I cannot inherit from BestEffortLineParser because it is internal?
immagine

from peanutbutter.

fluffynuts avatar fluffynuts commented on August 21, 2024

It should be public in 2.0.62. It used to be internal to reduce the noise of exported types. Have you updated?

from peanutbutter.

fluffynuts avatar fluffynuts commented on August 21, 2024

I can confirm that, in a brand new project targeting 2.0.62, I can derive from BestEffortLineParser:

image

from peanutbutter.

AxelTechnology avatar AxelTechnology commented on August 21, 2024

Yes, it works, thank you!

Do you think that is a good idea that in case of
base.CommentDelimiter = "";
the comments are never stripped out of the line, so the whole line is returned?

Use cases:
[DESCRIPTION]
MESG-32=Industrie de l'emballage et fabrication d'autres matΓ©riaux
PWD-ENCRIPTED=xLvA'M3%^GKSV

from peanutbutter.

fluffynuts avatar fluffynuts commented on August 21, 2024

hm, wow, you have some really gnarly "ini" files to deal with! if you're trying to prevent any comment parsing whatsoever, I'd probably put some non-printable character in there, like char 0 or something like that? You'd want to not match on anything, so any character which isn't in your input file will do the trick - if you have a real null in your ini file, that would be Bad :D so yeah, perhaps

CommentDelimiter = "\0";

from peanutbutter.

fluffynuts avatar fluffynuts commented on August 21, 2024

again, I'd suggest processing these ini files to standard formats - perhaps your systems could (once-off) load up with the derivative line parser, with CommentDelimiter set to "\0" and rewrite the files, then open again? You'd want to record somewhere that you did this work so it's not done every time. I'm just thinking about the next poor person who has to deal with these files and doesn't have a PeanutButter to help πŸ˜‚

from peanutbutter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.