Skip to content

Commit

Permalink
syntax improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed Aug 16, 2024
1 parent 8d7c2d2 commit 0eae58f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 53 deletions.
6 changes: 3 additions & 3 deletions extensions/Sisk.IniConfiguration/IniConfigurationPipeline.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions extensions/Sisk.IniConfiguration/IniDocument.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Sisk.IniConfiguration.Parser;
using Sisk.IniConfiguration.Serializer;
using System.Text;

namespace Sisk.IniConfiguration;
Expand All @@ -11,7 +11,7 @@ public sealed class IniDocument
/// <summary>
/// Gets all INI sections defined in this INI document.
/// </summary>
public IniSection[] Sections { get; }
public IList<IniSection> Sections { get; }

/// <summary>
/// Gets the global INI section, which is the primary section in the document.
Expand All @@ -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();
}

/// <summary>
Expand All @@ -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();
}

/// <summary>
Expand All @@ -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();
}

/// <summary>
Expand All @@ -63,8 +63,8 @@ public static IniDocument FromStream(Stream stream, Encoding? encoding = null)
/// <param name="reader">The <see cref="TextReader"/> instance.</param>
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)
Expand All @@ -79,10 +79,10 @@ internal IniDocument(IniSection[] sections)
/// <returns>The <see cref="IniSection"/> object if found, or null if not defined.</returns>
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;
Expand Down
8 changes: 4 additions & 4 deletions extensions/Sisk.IniConfiguration/IniSection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Sisk.IniConfiguration.Parser;
using Sisk.IniConfiguration.Serializer;
using System.Collections;
using System.Diagnostics.CodeAnalysis;

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -78,7 +78,7 @@ public IEnumerable<string[]> 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();
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,41 @@
using System.Text;
using System.Threading.Tasks;

namespace Sisk.IniConfiguration.Parser;
namespace Sisk.IniConfiguration.Serializer;

/// <summary>
/// Provides an INI-document parser.
/// Provides an INI-document reader and parser.
/// </summary>
public sealed class IniParser : IDisposable
public sealed class IniReader : IDisposable
{
internal static readonly StringComparer IniNamingComparer = StringComparer.InvariantCultureIgnoreCase;

private TextReader reader;
private bool disposedValue;

/// <summary>
/// Creates an new <see cref="IniParser"/> with the specified text reader.
/// Gets the <see cref="TextReader"/> which is providing data to this INI reader.
/// </summary>
public TextReader Reader { get => reader; }

/// <summary>
/// Creates an new <see cref="IniReader"/> with the specified text reader.
/// </summary>
/// <param name="reader">The <see cref="TextReader"/> instace to read the INI document.</param>
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 = ' ';

/// <summary>
/// Reads the INI document from the input stream.
/// </summary>
/// <returns>An <see cref="IniDocument"/> file containing all properties and data from the input stream.</returns>
public IniDocument Parse()
public IniDocument Read()
{
ThrowIfDisposed();

string lastSectionName = "__GLOBAL__";
string lastSectionName = "__SISKINIGLOBAL__";
List<(string, string)> items = new List<(string, string)>();
List<IniSection> creatingSections = new List<IniSection>();

Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -102,7 +95,7 @@ public IniDocument Parse()

void SkipUntilNewLine()
{
ReadUntil(new char[] { NEW_LINE }, false);
ReadUntil(new char[] { Token.NEW_LINE }, false);
}

void SkipWhiteSpace()
Expand Down Expand Up @@ -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();
}
}
}
Expand All @@ -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();
Expand All @@ -191,7 +184,7 @@ void SkipWhiteSpace()
void ThrowIfDisposed()
{
if (disposedValue)
throw new ObjectDisposedException(nameof(IniParser));
throw new ObjectDisposedException(nameof(IniReader));
}

void Dispose(bool disposing)
Expand Down
22 changes: 22 additions & 0 deletions extensions/Sisk.IniConfiguration/Serializer/Token.cs
Original file line number Diff line number Diff line change
@@ -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 = ' ';
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageTags>http-server,http,web framework</PackageTags>
<RepositoryType>git</RepositoryType>

<Version>1.0.0.0-beta</Version>
<Version>1.0.0.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>

Expand Down

0 comments on commit 0eae58f

Please sign in to comment.