Skip to content

Commit

Permalink
ini parser improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
CypherPotato committed Aug 10, 2024
1 parent fbbe34a commit f55f542
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
20 changes: 12 additions & 8 deletions extensions/Sisk.IniConfiguration/Parser/IniParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ' ';

/// <summary>
/// Reads the INI document from the input stream.
Expand All @@ -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();
Expand All @@ -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();
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -204,6 +207,7 @@ void Dispose(bool disposing)
}
}

/// <inheritdoc/>
public void Dispose()
{
Dispose(disposing: true);
Expand Down
42 changes: 42 additions & 0 deletions extensions/Sisk.IniConfiguration/README.md
Original file line number Diff line number Diff line change
@@ -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");
```

0 comments on commit f55f542

Please sign in to comment.