diff --git a/src/IniParser.Example/Program.cs b/src/IniParser.Example/Program.cs index 1cec3824..d2befa02 100644 --- a/src/IniParser.Example/Program.cs +++ b/src/IniParser.Example/Program.cs @@ -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(); @@ -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]); + } } } } diff --git a/src/IniParser/IniDataParser.cs b/src/IniParser/IniDataParser.cs index 63a0e22a..4644a465 100644 --- a/src/IniParser/IniDataParser.cs +++ b/src/IniParser/IniDataParser.cs @@ -58,6 +58,35 @@ public ReadOnlyCollection Errors } #endregion + /// + /// Try to Parse the string if its a valid ini data or not + /// + /// + /// String with ata in INI format + /// + /// + /// Output value of the parsed Data if its successful + /// + 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; + } + } + /// /// Parses a string containing valid ini data /// diff --git a/src/IniParser/Model/PropertyCollection.cs b/src/IniParser/Model/PropertyCollection.cs index d86622c7..e433547d 100644 --- a/src/IniParser/Model/PropertyCollection.cs +++ b/src/IniParser/Model/PropertyCollection.cs @@ -1,5 +1,7 @@ +using System; using System.Collections; using System.Collections.Generic; +using System.Linq; namespace IniParser.Model { @@ -91,10 +93,43 @@ public string this[string keyName] } } - /// - /// Return the number of keys in the collection - /// - public int Count + /// + /// Gets the value of a property. + /// + /// + /// Gets the key by int + /// + /// + /// key of the property + /// + 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; + } + } + + /// + /// Return the number of keys in the collection + /// + public int Count { get { return _properties.Count; } } @@ -313,4 +348,4 @@ internal Property GetLast() readonly IEqualityComparer _searchComparer; #endregion } -} \ No newline at end of file +}