From f55f542bdfbae58c23be9ca1812d2d179fdf6ce2 Mon Sep 17 00:00:00 2001 From: cypherpotato Date: Fri, 9 Aug 2024 23:58:01 -0300 Subject: [PATCH] ini parser improvements --- .../Sisk.IniConfiguration/Parser/IniParser.cs | 20 +++++---- extensions/Sisk.IniConfiguration/README.md | 42 +++++++++++++++++++ 2 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 extensions/Sisk.IniConfiguration/README.md diff --git a/extensions/Sisk.IniConfiguration/Parser/IniParser.cs b/extensions/Sisk.IniConfiguration/Parser/IniParser.cs index 6ccc934..f7e3532 100644 --- a/extensions/Sisk.IniConfiguration/Parser/IniParser.cs +++ b/extensions/Sisk.IniConfiguration/Parser/IniParser.cs @@ -33,6 +33,9 @@ public IniParser(TextReader reader) const char STRING_QUOTE_2 = '\"'; const char PROPERTY_DELIMITER = '='; const char NEW_LINE = '\n'; + const char RETURN = '\r'; + const char TAB = '\t'; + const char SPACE = ' '; /// /// Reads the INI document from the input stream. @@ -51,7 +54,7 @@ public IniDocument Parse() { char c = (char)read; - if (c == SECTION_START) + if (c is SECTION_START) { reader.Read(); string? sectionName = ReadUntil(new char[] { SECTION_END })?.Trim(); @@ -65,9 +68,9 @@ public IniDocument Parse() items.Clear(); lastSectionName = sectionName; - SkipWhiteSpace(); + SkipUntilNewLine(); } - else if (c == COMMENT_1 || c == COMMENT_2) + else if (c is COMMENT_1 or COMMENT_2) { SkipUntilNewLine(); } @@ -122,19 +125,19 @@ void SkipWhiteSpace() int read = reader.Read(); if (read < 0) { - return ""; + return string.Empty; } else { char c = (char)read; - if (c == ' ' || c == '\t') + if (c is SPACE or TAB) { goto readNext; } - else if (c == '\r' || c == '\n') + else if (c is RETURN or NEW_LINE) { - return ""; + return string.Empty; } if (c == STRING_QUOTE_1) { @@ -163,7 +166,7 @@ void SkipWhiteSpace() { return sb.ToString(); } - else if (breakOnComment && (c == COMMENT_1 || c == COMMENT_2)) + else if (breakOnComment && (c is COMMENT_1 or COMMENT_2)) { var s = sb.ToString(); SkipUntilNewLine(); @@ -204,6 +207,7 @@ void Dispose(bool disposing) } } + /// public void Dispose() { Dispose(disposing: true); diff --git a/extensions/Sisk.IniConfiguration/README.md b/extensions/Sisk.IniConfiguration/README.md new file mode 100644 index 0000000..de051b5 --- /dev/null +++ b/extensions/Sisk.IniConfiguration/README.md @@ -0,0 +1,42 @@ +# Sisk.IniConfiguration + +A Sisk library for reading and writing INI files and using them as Sisk configurations. + +Current implementation flavor: + +- Properties and section names are **case-insensitive**. +- Properties names and values are **trimmed**. +- Values can be quoted with single or double quotes. Quotes can have line-breaks inside them. +- Comments are supported with `#` and `;`. Also, **trailing comments are allowed**. +- Properties can have multiple values. + +## Usage + +Using the following ini code as example: + +```ini +One = 1 +Value = this is an value +Another value = "this value + has an line break on it" + +; the code below has some colors +[some section] +Color = Red +Color = Blue +Color = Yellow ; do not use yellow +``` + +Parse it with: + +```csharp +// parse the ini text from the string +IniDocument doc = IniDocument.FromString(iniText); + +// get one value +string? one = doc.Global.GetOne("one"); +string? anotherValue = doc.Global.GetOne("another value"); + +// get multiple values +string[]? colors = doc.GetSection("some section")?.GetMany("color"); +``` \ No newline at end of file