From 0eae58fa828bd1b1651e3df94c2892517b72711f Mon Sep 17 00:00:00 2001 From: cypherpotato Date: Fri, 16 Aug 2024 19:23:33 -0300 Subject: [PATCH] syntax improvements --- .../IniConfigurationPipeline.cs | 6 +- .../Sisk.IniConfiguration/IniDocument.cs | 24 ++++---- .../Sisk.IniConfiguration/IniSection.cs | 8 +-- .../IniParser.cs => Serializer/IniReader.cs} | 59 ++++++++----------- .../Sisk.IniConfiguration/Serializer/Token.cs | 22 +++++++ .../Sisk.IniConfiguration.csproj | 2 +- 6 files changed, 68 insertions(+), 53 deletions(-) rename extensions/Sisk.IniConfiguration/{Parser/IniParser.cs => Serializer/IniReader.cs} (73%) create mode 100644 extensions/Sisk.IniConfiguration/Serializer/Token.cs diff --git a/extensions/Sisk.IniConfiguration/IniConfigurationPipeline.cs b/extensions/Sisk.IniConfiguration/IniConfigurationPipeline.cs index 06ecd57..b5e03dd 100644 --- a/extensions/Sisk.IniConfiguration/IniConfigurationPipeline.cs +++ b/extensions/Sisk.IniConfiguration/IniConfigurationPipeline.cs @@ -1,6 +1,6 @@ using Sisk.Core.Http; using Sisk.Core.Http.Hosting; -using Sisk.IniConfiguration.Parser; +using Sisk.IniConfiguration.Serializer; using System.Text; namespace Sisk.IniConfiguration; @@ -35,11 +35,11 @@ public void ReadConfiguration(ConfigurationContext context) parsingNode = "Server.IncludeRequestIdHeader"; if (serverSection.GetOne("IncludeRequestIdHeader") is { } IncludeRequestIdHeader) - context.Host.ServerConfiguration.IncludeRequestIdHeader = IniParser.IniNamingComparer.Compare(IncludeRequestIdHeader, "true") == 0; + context.Host.ServerConfiguration.IncludeRequestIdHeader = IniReader.IniNamingComparer.Compare(IncludeRequestIdHeader, "true") == 0; parsingNode = "Server.ThrowExceptions"; if (serverSection.GetOne("ThrowExceptions") is { } ThrowExceptions) - context.Host.ServerConfiguration.ThrowExceptions = IniParser.IniNamingComparer.Compare(ThrowExceptions, "true") == 0; + context.Host.ServerConfiguration.ThrowExceptions = IniReader.IniNamingComparer.Compare(ThrowExceptions, "true") == 0; parsingNode = "Server.AccessLogsStream"; if (serverSection.GetOne("AccessLogsStream") is { } AccessLogsStream) diff --git a/extensions/Sisk.IniConfiguration/IniDocument.cs b/extensions/Sisk.IniConfiguration/IniDocument.cs index bc8aef4..ce6d72c 100644 --- a/extensions/Sisk.IniConfiguration/IniDocument.cs +++ b/extensions/Sisk.IniConfiguration/IniDocument.cs @@ -1,4 +1,4 @@ -using Sisk.IniConfiguration.Parser; +using Sisk.IniConfiguration.Serializer; using System.Text; namespace Sisk.IniConfiguration; @@ -11,7 +11,7 @@ public sealed class IniDocument /// /// Gets all INI sections defined in this INI document. /// - public IniSection[] Sections { get; } + public IList Sections { get; } /// /// Gets the global INI section, which is the primary section in the document. @@ -26,8 +26,8 @@ public sealed class IniDocument public static IniDocument FromString(string iniConfiguration) { using TextReader reader = new StringReader(iniConfiguration); - using IniParser parser = new IniParser(reader); - return parser.Parse(); + using IniReader parser = new IniReader(reader); + return parser.Read(); } /// @@ -39,8 +39,8 @@ public static IniDocument FromString(string iniConfiguration) public static IniDocument FromFile(string filePath, Encoding? encoding = null) { using TextReader reader = new StreamReader(filePath, encoding ?? Encoding.UTF8); - using IniParser parser = new IniParser(reader); - return parser.Parse(); + using IniReader parser = new IniReader(reader); + return parser.Read(); } /// @@ -52,8 +52,8 @@ public static IniDocument FromFile(string filePath, Encoding? encoding = null) public static IniDocument FromStream(Stream stream, Encoding? encoding = null) { using TextReader reader = new StreamReader(stream, encoding ?? Encoding.UTF8); - using IniParser parser = new IniParser(reader); - return parser.Parse(); + using IniReader parser = new IniReader(reader); + return parser.Read(); } /// @@ -63,8 +63,8 @@ public static IniDocument FromStream(Stream stream, Encoding? encoding = null) /// The instance. public static IniDocument FromStream(TextReader reader) { - using IniParser parser = new IniParser(reader); - return parser.Parse(); + using IniReader parser = new IniReader(reader); + return parser.Read(); } internal IniDocument(IniSection[] sections) @@ -79,10 +79,10 @@ internal IniDocument(IniSection[] sections) /// The object if found, or null if not defined. public IniSection? GetSection(string sectionName) { - for (int i = 0; i < Sections.Length; i++) + for (int i = 0; i < Sections.Count; i++) { IniSection section = Sections[i]; - if (IniParser.IniNamingComparer.Compare(section.Name, sectionName) == 0) + if (IniReader.IniNamingComparer.Compare(section.Name, sectionName) == 0) return section; } return null; diff --git a/extensions/Sisk.IniConfiguration/IniSection.cs b/extensions/Sisk.IniConfiguration/IniSection.cs index a4069d5..cbfc347 100644 --- a/extensions/Sisk.IniConfiguration/IniSection.cs +++ b/extensions/Sisk.IniConfiguration/IniSection.cs @@ -1,4 +1,4 @@ -using Sisk.IniConfiguration.Parser; +using Sisk.IniConfiguration.Serializer; using System.Collections; using System.Diagnostics.CodeAnalysis; @@ -31,7 +31,7 @@ public string[] this[string key] get { return items - .Where(k => IniParser.IniNamingComparer.Compare(key, k.Item1) == 0) + .Where(k => IniReader.IniNamingComparer.Compare(key, k.Item1) == 0) .Select(k => k.Item2) .ToArray(); } @@ -78,7 +78,7 @@ public IEnumerable Values public string? GetOne(string key) { return items - .Where(k => IniParser.IniNamingComparer.Compare(key, k.Item1) == 0) + .Where(k => IniReader.IniNamingComparer.Compare(key, k.Item1) == 0) .Select(k => k.Item2) .LastOrDefault(); } @@ -105,7 +105,7 @@ public bool ContainsKey(string key) { (string, string?) item = items[i]; - if (IniParser.IniNamingComparer.Compare(item.Item1, key) == 0) + if (IniReader.IniNamingComparer.Compare(item.Item1, key) == 0) return true; } return false; diff --git a/extensions/Sisk.IniConfiguration/Parser/IniParser.cs b/extensions/Sisk.IniConfiguration/Serializer/IniReader.cs similarity index 73% rename from extensions/Sisk.IniConfiguration/Parser/IniParser.cs rename to extensions/Sisk.IniConfiguration/Serializer/IniReader.cs index f7e3532..db48317 100644 --- a/extensions/Sisk.IniConfiguration/Parser/IniParser.cs +++ b/extensions/Sisk.IniConfiguration/Serializer/IniReader.cs @@ -4,12 +4,12 @@ using System.Text; using System.Threading.Tasks; -namespace Sisk.IniConfiguration.Parser; +namespace Sisk.IniConfiguration.Serializer; /// -/// Provides an INI-document parser. +/// Provides an INI-document reader and parser. /// -public sealed class IniParser : IDisposable +public sealed class IniReader : IDisposable { internal static readonly StringComparer IniNamingComparer = StringComparer.InvariantCultureIgnoreCase; @@ -17,35 +17,28 @@ public sealed class IniParser : IDisposable private bool disposedValue; /// - /// Creates an new with the specified text reader. + /// Gets the which is providing data to this INI reader. + /// + public TextReader Reader { get => reader; } + + /// + /// Creates an new with the specified text reader. /// /// The instace to read the INI document. - public IniParser(TextReader reader) + public IniReader(TextReader reader) { this.reader = reader; } - const char SECTION_START = '['; - const char SECTION_END = ']'; - const char COMMENT_1 = '#'; - const char COMMENT_2 = ';'; - const char STRING_QUOTE_1 = '\''; - 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. /// /// An file containing all properties and data from the input stream. - public IniDocument Parse() + public IniDocument Read() { ThrowIfDisposed(); - string lastSectionName = "__GLOBAL__"; + string lastSectionName = "__SISKINIGLOBAL__"; List<(string, string)> items = new List<(string, string)>(); List creatingSections = new List(); @@ -54,10 +47,10 @@ public IniDocument Parse() { char c = (char)read; - if (c is SECTION_START) + if (c is Token.SECTION_START) { reader.Read(); - string? sectionName = ReadUntil(new char[] { SECTION_END })?.Trim(); + string? sectionName = ReadUntil(new char[] { Token.SECTION_END })?.Trim(); if (sectionName is null) break; @@ -70,13 +63,13 @@ public IniDocument Parse() SkipUntilNewLine(); } - else if (c is COMMENT_1 or COMMENT_2) + else if (c is Token.COMMENT_1 or Token.COMMENT_2) { SkipUntilNewLine(); } else { - string? propertyName = ReadUntil(new char[] { PROPERTY_DELIMITER }, true); + string? propertyName = ReadUntil(new char[] { Token.PROPERTY_DELIMITER, Token.NEW_LINE, Token.RETURN }, true); if (propertyName is null) break; @@ -102,7 +95,7 @@ public IniDocument Parse() void SkipUntilNewLine() { - ReadUntil(new char[] { NEW_LINE }, false); + ReadUntil(new char[] { Token.NEW_LINE }, false); } void SkipWhiteSpace() @@ -131,25 +124,25 @@ void SkipWhiteSpace() { char c = (char)read; - if (c is SPACE or TAB) + if (c is Token.SPACE or Token.TAB) { goto readNext; } - else if (c is RETURN or NEW_LINE) + else if (c is Token.RETURN or Token.NEW_LINE) { return string.Empty; } - if (c == STRING_QUOTE_1) + if (c == Token.STRING_QUOTE_1) { - return ReadUntil(new char[] { STRING_QUOTE_1 }, false); + return ReadUntil(new char[] { Token.STRING_QUOTE_1 }, false); } - else if (c == STRING_QUOTE_2) + else if (c == Token.STRING_QUOTE_2) { - return ReadUntil(new char[] { STRING_QUOTE_2 }, false); + return ReadUntil(new char[] { Token.STRING_QUOTE_2 }, false); } else { - return (c + ReadUntil(new char[] { NEW_LINE }, true, true)).Trim(); + return (c + ReadUntil(new char[] { Token.NEW_LINE }, true, true)).Trim(); } } } @@ -166,7 +159,7 @@ void SkipWhiteSpace() { return sb.ToString(); } - else if (breakOnComment && (c is COMMENT_1 or COMMENT_2)) + else if (breakOnComment && (c is Token.COMMENT_1 or Token.COMMENT_2)) { var s = sb.ToString(); SkipUntilNewLine(); @@ -191,7 +184,7 @@ void SkipWhiteSpace() void ThrowIfDisposed() { if (disposedValue) - throw new ObjectDisposedException(nameof(IniParser)); + throw new ObjectDisposedException(nameof(IniReader)); } void Dispose(bool disposing) diff --git a/extensions/Sisk.IniConfiguration/Serializer/Token.cs b/extensions/Sisk.IniConfiguration/Serializer/Token.cs new file mode 100644 index 0000000..7d62f67 --- /dev/null +++ b/extensions/Sisk.IniConfiguration/Serializer/Token.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Sisk.IniConfiguration.Serializer; + +static class Token +{ + public const char SECTION_START = '['; + public const char SECTION_END = ']'; + public const char COMMENT_1 = '#'; + public const char COMMENT_2 = ';'; + public const char STRING_QUOTE_1 = '\''; + public const char STRING_QUOTE_2 = '\"'; + public const char PROPERTY_DELIMITER = '='; + public const char NEW_LINE = '\n'; + public const char RETURN = '\r'; + public const char TAB = '\t'; + public const char SPACE = ' '; +} diff --git a/extensions/Sisk.IniConfiguration/Sisk.IniConfiguration.csproj b/extensions/Sisk.IniConfiguration/Sisk.IniConfiguration.csproj index 8565ffc..1e30610 100644 --- a/extensions/Sisk.IniConfiguration/Sisk.IniConfiguration.csproj +++ b/extensions/Sisk.IniConfiguration/Sisk.IniConfiguration.csproj @@ -21,7 +21,7 @@ http-server,http,web framework git - 1.0.0.0-beta + 1.0.0.0 1.0.0.0 1.0.0.0