From e306842d6bf8115b6e0910ee09f3dc76df3c7bdf Mon Sep 17 00:00:00 2001 From: alcexhim Date: Mon, 11 May 2015 15:53:41 -0400 Subject: [PATCH] Added NameValuePair/NameValuePairGroup --- .../NameValuePair.cs | 120 +++++++++++ .../NameValuePairGroup.cs | 202 ++++++++++++++++++ .../UniversalEditor.Essential.csproj | 2 + 3 files changed, 324 insertions(+) create mode 100644 CSharp/Plugins/UniversalEditor.Essential/NameValuePair.cs create mode 100644 CSharp/Plugins/UniversalEditor.Essential/NameValuePairGroup.cs diff --git a/CSharp/Plugins/UniversalEditor.Essential/NameValuePair.cs b/CSharp/Plugins/UniversalEditor.Essential/NameValuePair.cs new file mode 100644 index 00000000..edf68955 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/NameValuePair.cs @@ -0,0 +1,120 @@ +namespace UniversalEditor +{ + using System; + + public class NameValuePair : NameValuePair + { + public NameValuePair(string Name) : base(Name) + { + } + + public NameValuePair(string Name, string Value) : base(Name, Value) + { + } + } + public class NameValuePair : ICloneable + { + private string mvarName; + private T mvarValue; + + public NameValuePair(string Name) + { + this.mvarName = ""; + this.mvarValue = default(T); + this.mvarName = Name; + this.mvarValue = default(T); + } + + public NameValuePair(string Name, T Value) + { + this.mvarName = ""; + this.mvarValue = default(T); + this.mvarName = Name; + this.mvarValue = Value; + } + + public string Name + { + get + { + return this.mvarName; + } + set + { + this.mvarName = value; + } + } + + public T Value + { + get + { + return this.mvarValue; + } + set + { + this.mvarValue = value; + } + } + + public class NameValuePairCollection : System.Collections.ObjectModel.Collection> + { + public NameValuePair Add(string Name) + { + return this.Add(Name, default(T)); + } + + public NameValuePair Add(string Name, T Value) + { + NameValuePair p = new NameValuePair(Name, Value); + base.Add(p); + return p; + } + + public bool Contains(string Name) + { + return (this[Name] != null); + } + + public bool Remove(string Name) + { + NameValuePair p = this[Name]; + if (p != null) + { + base.Remove(p); + return true; + } + return false; + } + + public NameValuePair this[string Name] + { + get + { + foreach (NameValuePair p in this) + { + if (p.Name == Name) + { + return p; + } + } + return null; + } + } + } + + public object Clone() + { + NameValuePair clone = new NameValuePair(mvarName.Clone() as string); + if (mvarValue is ICloneable) + { + clone.Value = (T)((mvarValue as ICloneable).Clone()); + } + else + { + clone.Value = mvarValue; + } + return clone; + } + } +} \ No newline at end of file diff --git a/CSharp/Plugins/UniversalEditor.Essential/NameValuePairGroup.cs b/CSharp/Plugins/UniversalEditor.Essential/NameValuePairGroup.cs new file mode 100644 index 00000000..b2b2e48a --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Essential/NameValuePairGroup.cs @@ -0,0 +1,202 @@ +namespace UniversalEditor +{ + using System; + + public class NameValuePairGroup + { + private NameValuePairGroupCollection mvarGroups; + private NameValuePair.NameValuePairCollection mvarItems; + private string mvarName; + private string mvarPathSeparator; + + public NameValuePairGroup(string Name) + { + mvarGroups = new NameValuePairGroupCollection(); + mvarItems = new NameValuePair.NameValuePairCollection(); + mvarPathSeparator = ""; + mvarName = Name; + } + + public NameValuePairGroup GetGroup(string GroupName) + { + string[] path = GroupName.Split(new string[] { this.mvarPathSeparator }, StringSplitOptions.None); + if (path.Length == 1) + { + return this.mvarGroups[path[0]]; + } + NameValuePairGroup pgParent = this.mvarGroups[path[0]]; + int i = 1; + while (pgParent != null) + { + if (pgParent.Groups[path[i]] != null) + { + pgParent = pgParent.Groups[path[i]]; + i++; + } + else + { + break; + } + } + return pgParent.Groups[path[path.Length - 1]]; + } + + public NameValuePair GetItem(string PropertyName) + { + string[] path = PropertyName.Split(new string[] { this.mvarPathSeparator }, StringSplitOptions.None); + if (path.Length == 1) + { + return mvarItems[path[0]]; + } + NameValuePairGroup pgParent = mvarGroups[path[0]]; + int i = 1; + while (pgParent != null) + { + if (pgParent.Groups[path[i]] != null) + { + pgParent = pgParent.Groups[path[i]]; + i++; + } + else + { + break; + } + } + return pgParent.Items[path[path.Length - 1]]; + } + + public bool GetItemValueAsBoolean(string ValueName) + { + return this.GetItemValueAsBoolean(ValueName, false); + } + + public bool GetItemValueAsBoolean(string ValueName, bool DefaultValue) + { + string val = this.GetItemValueAsString(ValueName); + if ((((val.ToLower() == "1") || (val.ToLower() == "true")) || (val.ToLower() == "yes")) || (val.ToLower() == "on")) + { + return true; + } + if ((((val.ToLower() == "0") || (val.ToLower() == "false")) || (val.ToLower() == "no")) || (val.ToLower() == "off")) + { + return false; + } + return DefaultValue; + } + + public int GetItemValueAsInteger(string ValueName) + { + return this.GetItemValueAsInteger(ValueName, -1); + } + + public int GetItemValueAsInteger(string ValueName, int DefaultValue) + { + try + { + return int.Parse(this.GetItemValueAsString(ValueName, DefaultValue.ToString())); + } + catch + { + return DefaultValue; + } + } + + public string GetItemValueAsString(string ValueName) + { + return this.GetItemValueAsString(ValueName, ""); + } + + public string GetItemValueAsString(string ValueName, string DefaultValue) + { + try + { + return this.GetItem(ValueName).Value.ToString(); + } + catch + { + return DefaultValue; + } + } + + public NameValuePairGroupCollection Groups + { + get + { + return this.mvarGroups; + } + } + + public NameValuePair.NameValuePairCollection Items + { + get + { + return this.mvarItems; + } + } + + public string Name + { + get + { + return this.mvarName; + } + set + { + this.mvarName = value; + } + } + + public string PathSeparator + { + get + { + return this.mvarPathSeparator; + } + set + { + this.mvarPathSeparator = value; + } + } + + public class NameValuePairGroupCollection : System.Collections.ObjectModel.Collection> + { + public NameValuePairGroup Add(string Name) + { + NameValuePairGroup pg = new NameValuePairGroup(Name); + base.Add(pg); + return pg; + } + + public bool Contains(string Name) + { + return (this[Name] != null); + } + + public bool Remove(string Name) + { + NameValuePairGroup pg = this[Name]; + if (pg != null) + { + base.Remove(pg); + return true; + } + return false; + } + + public NameValuePairGroup this[string Name] + { + get + { + foreach (NameValuePairGroup pg in this) + { + if (pg.Name == Name) + { + return pg; + } + } + return null; + } + } + } + } +} \ No newline at end of file diff --git a/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj b/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj index ea4b2d61..cddc69a6 100644 --- a/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj +++ b/CSharp/Plugins/UniversalEditor.Essential/UniversalEditor.Essential.csproj @@ -74,6 +74,8 @@ + +