using System; using System.Diagnostics; using System.Collections.Generic; namespace MadMilkman.Ini { /// /// Represents a key item of the INI file with name and value content. /// public sealed class IniKey : IniItem { /// /// Gets and sets value. /// /// IniKey's Value Parsing /// IniKey's Value Binding public string Value { get; set; } /// /// Gets the to which this belongs to. /// public IniKeyCollection ParentCollection { get { return (IniKeyCollection)this.ParentCollectionCore; } } /// /// Gets the to which this belongs to. /// public IniSection ParentSection { get { return (IniSection)((this.ParentCollectionCore != null) ? this.ParentCollection.Owner : null); } } internal bool IsValueArray { get { return !string.IsNullOrEmpty(this.Value) && this.Value[0] == '{' && this.Value[this.Value.Length - 1] == '}'; } } internal string[] Values { get { var values = this.Value.Substring(1, this.Value.Length - 2).Split(','); for (int i = 0; i < values.Length; i++) values[i] = values[i].Trim(); return values; } set { this.Value = "{" + string.Join(",", value) + "}"; } } /// /// Initializes a new instance of the class. /// /// The owner file. /// The key's name. public IniKey(IniFile parentFile, string name) : this(parentFile, name, (string)null) { } /// /// Initializes a new instance of the class. /// /// The owner file. /// The key's name. /// The key's value. public IniKey(IniFile parentFile, string name, string value) : base(parentFile, name) { this.Value = value; } /// /// Initializes a new instance of the class. /// /// The owner file. /// The key's data, pair of key's name and key's value. public IniKey(IniFile parentFile, KeyValuePair nameValuePair) : base(parentFile, nameValuePair.Key) { this.Value = nameValuePair.Value; } // Constructor used by IniReader. internal IniKey(IniFile parentFile, string name, IniComment trailingComment) : base(parentFile, name, trailingComment) { } // Deep copy constructor. internal IniKey(IniFile destinationFile, IniKey sourceKey) : base(destinationFile, sourceKey) { this.Value = sourceKey.Value; } /// /// Copies this instance. /// /// Copied . /// IniItem's Copying public IniKey Copy() { return this.Copy(this.ParentFile); } /// /// Copies this instance and sets copied instance's ParentFile. /// /// Copied key's parent file. /// Copied that belongs to a specified . /// IniItem's Copying public IniKey Copy(IniFile destinationFile) { return new IniKey(destinationFile, this); } /// /// Indicates whether the can be converted to specified type. /// /// Type of the object to convert the to. /// if the specified type is supported. /// /// Currently supported types are: /// /// System.Boolean /// System.Byte /// System.SByte /// System.Int16 /// System.UInt16 /// System.Int32 /// System.UInt32 /// System.Int64 /// System.UInt64 /// System.Char /// System.Single /// System.Double /// System.Decimal /// System.DateTime /// System.TimeSpan /// System.Enum /// System.String /// /// Additionally both Array and List of the above types are supported. /// public static bool IsSupportedValueType(Type type) { if (type == null) throw new ArgumentNullException("type"); return type.IsPrimitive || type.IsEnum || type == typeof(String) || type == typeof(Decimal) || type == typeof(DateTime) || type == typeof(TimeSpan) || (type.IsArray && IsSupportedValueType(type.GetElementType())) || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>) && IsSupportedValueType(type.GetGenericArguments()[0])); } /// /// Converts the to an instance of the specified type. /// /// Uninitialized instance of a specific type which will hold the converted value if the conversion succeeds. /// Type of the object to convert the to. /// Value that indicates whether the conversion succeeded. /// public bool TryParseValue(out T result) { if (this.ParentFile.HasValueMappings && this.ParentFile.ValueMappings.TryGetResult(this.Value, out result)) return true; else return IniValueParser.TryParse(this.Value, out result); } /// /// Converts the to an array of the specified type. /// /// Uninitialized array of a specific type which will hold the converted values if the conversion succeeds. /// Type of the objects in array to convert the to. /// Value that indicates whether the conversion succeeded. /// public bool TryParseValue(out T[] results) { List listResults; if (this.TryParseValue(out listResults)) { results = listResults.ToArray(); return true; } results = null; return false; } /// /// Converts the to a list of the specified type. /// /// Uninitialized list of a specific type which will hold the converted values if the conversion succeeds. /// Type of the objects in list to convert the to. /// Value that indicates whether the conversion succeeded. /// public bool TryParseValue(out List results) { if (this.IsValueArray) { var listResults = new List(); foreach (var value in this.Values) { T result; if (this.ParentFile.HasValueMappings && this.ParentFile.ValueMappings.TryGetResult(value, out result)) listResults.Add(result); else if (IniValueParser.TryParse(value, out result)) listResults.Add(result); else { results = null; return false; } } results = listResults; return true; } results = null; return false; } } }