// // NameValuePairGroup.cs - represents a grouped collection of NameValuePair items // // Author: // Michael Becker // // Copyright (c) 2011-2020 Mike Becker's Software // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . namespace UniversalEditor { using System; /// /// Represents a grouped collection of items. /// 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; } } } } }