From 79ff33b800fcb99a4d3b86520e0b3fa9c39079a6 Mon Sep 17 00:00:00 2001 From: Ichimaki Date: Thu, 2 Mar 2023 05:04:17 +0800 Subject: [PATCH 1/6] Update PropertyCollection.cs --- src/IniParser/Model/PropertyCollection.cs | 34 +++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/IniParser/Model/PropertyCollection.cs b/src/IniParser/Model/PropertyCollection.cs index d86622c7..133e83d7 100644 --- a/src/IniParser/Model/PropertyCollection.cs +++ b/src/IniParser/Model/PropertyCollection.cs @@ -1,5 +1,6 @@ using System.Collections; using System.Collections.Generic; +using System.Linq; namespace IniParser.Model { @@ -91,10 +92,33 @@ 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 string this[int keyNumber] + { + get + { + foreach(var (v,i) in _properties.Select((v,i)=>(v,i))) + { + if (keyNumber == i) + return v.Key; + } + + return null; + } + } + + /// + /// Return the number of keys in the collection + /// + public int Count { get { return _properties.Count; } } @@ -313,4 +337,4 @@ internal Property GetLast() readonly IEqualityComparer _searchComparer; #endregion } -} \ No newline at end of file +} From 04c8438535bf616300aabc2a972188977c60b64d Mon Sep 17 00:00:00 2001 From: Ichimaki Date: Thu, 2 Mar 2023 05:14:23 +0800 Subject: [PATCH 2/6] Update Program.cs --- src/IniParser.Example/Program.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/IniParser.Example/Program.cs b/src/IniParser.Example/Program.cs index 1cec3824..fa1c2fe0 100644 --- a/src/IniParser.Example/Program.cs +++ b/src/IniParser.Example/Program.cs @@ -57,7 +57,17 @@ public static void Main() // Write down the contents of the modified ini file to the console Console.WriteLine("---- Printing contents of the new INI file ----"); Console.WriteLine(parsedData); - Console.WriteLine(); + Console.WriteLine(); + + // Test using Number on PropertyCollection + Console.WriteLine("User 1: {0}",parsedData["Users"][0]); + Console.WriteLine("User 2: {0}",parsedData["Users"][1]); + + // Test using ForLoop + for(int i = 0; i < parsedData["Users"].Count; i++) + { + Console.WriteLine("[ForLoop] User 1: {0}",parsedData["Users"][i]); + } } } } From 51e9d0fa5bf5a9d29c7910ea950a6647715100d5 Mon Sep 17 00:00:00 2001 From: Ichimaki Date: Fri, 3 Mar 2023 04:50:32 +0800 Subject: [PATCH 3/6] Update Program.cs --- src/IniParser.Example/Program.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/IniParser.Example/Program.cs b/src/IniParser.Example/Program.cs index fa1c2fe0..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(); @@ -57,11 +67,14 @@ public static void Main() // Write down the contents of the modified ini file to the console Console.WriteLine("---- Printing contents of the new INI file ----"); Console.WriteLine(parsedData); - Console.WriteLine(); - - // Test using Number on PropertyCollection + 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++) From ee23320fa45cb96a107c32c40eb34f125cb076ef Mon Sep 17 00:00:00 2001 From: Ichimaki Date: Fri, 3 Mar 2023 04:50:46 +0800 Subject: [PATCH 4/6] Update PropertyCollection.cs --- src/IniParser/Model/PropertyCollection.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/IniParser/Model/PropertyCollection.cs b/src/IniParser/Model/PropertyCollection.cs index 133e83d7..3696a3af 100644 --- a/src/IniParser/Model/PropertyCollection.cs +++ b/src/IniParser/Model/PropertyCollection.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using System.Linq; @@ -101,14 +102,23 @@ public string this[string keyName] /// /// key of the property /// - public string this[int keyNumber] + public dynamic this[int keyNumber] { get { - foreach(var (v,i) in _properties.Select((v,i)=>(v,i))) + bool _BooleanTest = false; + int _Interger = 0; + foreach (var (v, i) in _properties.Select((v, i) => (v, i))) { if (keyNumber == i) - return v.Key; + { + string Value = _properties[v.Key].Value; + + if (bool.TryParse(Value, out _BooleanTest)) return bool.Parse(Value); + if (int.TryParse(Value, out _Interger)) return int.Parse(Value); + return Value; + } + } return null; From ddd273da3f5f84c57caf84b2c7819b4b87336c85 Mon Sep 17 00:00:00 2001 From: Ichimaki Kasura Date: Sun, 2 Jul 2023 23:34:39 +0800 Subject: [PATCH 5/6] Added more parse on PropertyCollection Added TryParse on IniDataParser --- src/IniParser/IniDataParser.cs | 29 +++++++++++++++++++++++ src/IniParser/Model/PropertyCollection.cs | 12 ++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) 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 3696a3af..65c665d1 100644 --- a/src/IniParser/Model/PropertyCollection.cs +++ b/src/IniParser/Model/PropertyCollection.cs @@ -106,19 +106,21 @@ public dynamic this[int keyNumber] { get { - bool _BooleanTest = false; - int _Interger = 0; 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 _BooleanTest)) return bool.Parse(Value); - if (int.TryParse(Value, out _Interger)) return int.Parse(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 (float.TryParse(Value, out float _Float)) return _Float; + if (double.TryParse(Value, out double _Double)) return _Double; + return Value; } - } return null; From 28ea8be3b24b28ed6140a4eabdd3b1f6a42f6c3e Mon Sep 17 00:00:00 2001 From: Ichimaki Kasura Date: Mon, 3 Jul 2023 02:59:44 +0800 Subject: [PATCH 6/6] Removed float, double is superior. idk causes weird issue when reading single decimals. --- src/IniParser/Model/PropertyCollection.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/IniParser/Model/PropertyCollection.cs b/src/IniParser/Model/PropertyCollection.cs index 65c665d1..e433547d 100644 --- a/src/IniParser/Model/PropertyCollection.cs +++ b/src/IniParser/Model/PropertyCollection.cs @@ -116,7 +116,6 @@ public dynamic this[int keyNumber] 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 (float.TryParse(Value, out float _Float)) return _Float; if (double.TryParse(Value, out double _Double)) return _Double; return Value;