Skip to content

Add support for int on PropertyCollection #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/IniParser.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ public static void Main()
#format: user = pass
[Users]
ricky = rickypass
patty = pattypass ";
patty = pattypass

[String]
a = abc

[Integer]
a = 123

[bool]
a = true";


//Create an instance of a ini file parser
var parser = new IniDataParser();
Expand Down Expand Up @@ -58,6 +68,19 @@ public static void Main()
Console.WriteLine("---- Printing contents of the new INI file ----");
Console.WriteLine(parsedData);
Console.WriteLine();

// Test using Number on PropertyCollection
Console.WriteLine("User 1: {0}",parsedData["Users"][0]);
Console.WriteLine("User 2: {0}",parsedData["Users"][1]);
Console.WriteLine("String: {0} Type: {1}",parsedData["String"][0],parsedData["String"][0].GetType());
Console.WriteLine("Integer: {0} Type: {1}",parsedData["Integer"][0],parsedData["Integer"][0].GetType());
Console.WriteLine("Boolean: {0} Type: {1}",parsedData["bool"][0],parsedData["bool"][0].GetType());

// Test using ForLoop
for(int i = 0; i < parsedData["Users"].Count; i++)
{
Console.WriteLine("[ForLoop] User 1: {0}",parsedData["Users"][i]);
}
}
}
}
29 changes: 29 additions & 0 deletions src/IniParser/IniDataParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,35 @@ public ReadOnlyCollection<Exception> Errors
}
#endregion

/// <summary>
/// Try to Parse the string if its a valid ini data or not
/// </summary>
/// <param name="iniString">
/// String with ata in INI format
/// </param>
/// <param name="result">
/// Output value of the parsed Data if its successful
/// </param>
public static bool TryParse(string iniString, out IniData result)
{
if(string.IsNullOrEmpty(iniString))
{
result = null;
return false;
}

try
{
result = new IniDataParser().Parse(iniString);
return true;
}
catch
{
result = null;
return false;
}
}

/// <summary>
/// Parses a string containing valid ini data
/// </summary>
Expand Down
45 changes: 40 additions & 5 deletions src/IniParser/Model/PropertyCollection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace IniParser.Model
{
Expand Down Expand Up @@ -91,10 +93,43 @@ public string this[string keyName]
}
}

/// <summary>
/// Return the number of keys in the collection
/// </summary>
public int Count
/// <summary>
/// Gets the value of a property.
/// </summary>
/// <remarks>
/// Gets the key by int
/// </remarks>
/// <param name="keyNumber">
/// key of the property
/// </param>
public dynamic this[int keyNumber]
{
get
{
foreach (var (v, i) in _properties.Select((v, i) => (v, i)))
{
if (keyNumber == i)
{
string Value = _properties[v.Key].Value;

if (bool.TryParse(Value, out bool _BooleanTest)) return _BooleanTest;
if (short.TryParse(Value, out short _Short)) return _Short;
if (int.TryParse(Value, out int _Interger)) return _Interger;
if (long.TryParse(Value, out long _Long)) return _Long;
if (double.TryParse(Value, out double _Double)) return _Double;

return Value;
}
}

return null;
}
}

/// <summary>
/// Return the number of keys in the collection
/// </summary>
public int Count
{
get { return _properties.Count; }
}
Expand Down Expand Up @@ -313,4 +348,4 @@ internal Property GetLast()
readonly IEqualityComparer<string> _searchComparer;
#endregion
}
}
}