diff --git a/ICD.Common.Utils/Attributes/Properties/SettingsProperty.cs b/ICD.Common.Utils/Attributes/Properties/SettingsProperty.cs deleted file mode 100644 index f50520e..0000000 --- a/ICD.Common.Utils/Attributes/Properties/SettingsProperty.cs +++ /dev/null @@ -1,39 +0,0 @@ -using System; -using ICD.Common.Properties; - -namespace ICD.Common.Attributes.Properties -{ - /// - /// Provides information on a settings property. - /// - [PublicAPI] - public sealed class SettingsProperty : Attribute - { - public enum ePropertyType - { - [PublicAPI] Default, - [PublicAPI] Hidden, - [PublicAPI] PortId, - [PublicAPI] DeviceId, - [PublicAPI] Ipid, - [PublicAPI] Enum - } - - private readonly ePropertyType m_PropertyType; - - /// - /// Gets the property type. - /// - [PublicAPI] - public ePropertyType PropertyType { get { return m_PropertyType; } } - - /// - /// Constructor. - /// - /// - public SettingsProperty(ePropertyType propertyType) - { - m_PropertyType = propertyType; - } - } -} diff --git a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs index 80e23df..66cc2bd 100644 --- a/ICD.Common.Utils/Extensions/EnumerableExtensions.cs +++ b/ICD.Common.Utils/Extensions/EnumerableExtensions.cs @@ -53,12 +53,12 @@ namespace ICD.Common.Utils.Extensions /// /// Outputs the first item in the sequence. /// - public static bool TryFirstOrDefault(this IEnumerable extends, out T item) + public static bool TryFirst(this IEnumerable extends, out T item) { if (extends == null) throw new ArgumentNullException("extends"); - return extends.TryFirstOrDefault(i => true, out item); + return extends.TryFirst(i => true, out item); } /// @@ -69,7 +69,7 @@ namespace ICD.Common.Utils.Extensions /// /// Outputs the first item in the sequence. /// - public static bool TryFirstOrDefault(this IEnumerable extends, Func predicate, out T item) + public static bool TryFirst(this IEnumerable extends, Func predicate, out T item) { if (extends == null) throw new ArgumentNullException("extends"); diff --git a/ICD.Common.Utils/Extensions/ListExtensions.cs b/ICD.Common.Utils/Extensions/ListExtensions.cs new file mode 100644 index 0000000..2d842ba --- /dev/null +++ b/ICD.Common.Utils/Extensions/ListExtensions.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using ICD.Common.Properties; + +namespace ICD.Common.Utils.Extensions +{ + /// + /// Extension methods for working with Lists. + /// + public static class ListExtensions + { + /// + /// Adds the item into a sorted list. + /// + /// + /// + /// + [PublicAPI] + public static void AddSorted(this List extends, T item) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + extends.AddSorted(item, Comparer.Default); + } + + /// + /// Adds the item into a sorted list. + /// + /// + /// + /// + /// + [PublicAPI] + public static void AddSorted(this List extends, T item, IComparer comparer) + { + if (extends == null) + throw new ArgumentNullException("extends"); + + if (comparer == null) + throw new ArgumentNullException("comparer"); + + if (extends.Count == 0) + { + extends.Add(item); + return; + } + + if (comparer.Compare(extends[extends.Count - 1], item) <= 0) + { + extends.Add(item); + return; + } + + if (comparer.Compare(extends[0], item) >= 0) + { + extends.Insert(0, item); + return; + } + + int index = extends.BinarySearch(item, comparer); + if (index < 0) + index = ~index; + + extends.Insert(index, item); + } + } +} diff --git a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj index c39b3ae..5c10690 100644 --- a/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj +++ b/ICD.Common.Utils/ICD.Common.Utils_SimplSharp.csproj @@ -71,7 +71,6 @@ - @@ -85,6 +84,7 @@ + diff --git a/ICD.Common.Utils/IcdConsole.cs b/ICD.Common.Utils/IcdConsole.cs index dc065ce..5505eb6 100644 --- a/ICD.Common.Utils/IcdConsole.cs +++ b/ICD.Common.Utils/IcdConsole.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using ICD.Common.Properties; #if SIMPLSHARP using Crestron.SimplSharp; @@ -34,7 +35,8 @@ namespace ICD.Common.Utils [PublicAPI] public static void ConsoleCommandResponse(string message, params object[] args) { - message = string.Format(message, args); + if(args != null && args.Any()) + message = string.Format(message, args); #if SIMPLSHARP try diff --git a/ICD.Common.Utils/ProgramUtils.cs b/ICD.Common.Utils/ProgramUtils.cs index b065bd5..2d17aca 100644 --- a/ICD.Common.Utils/ProgramUtils.cs +++ b/ICD.Common.Utils/ProgramUtils.cs @@ -147,29 +147,29 @@ namespace ICD.Common.Utils { Dictionary output = new Dictionary(); - try + string progInfo = string.Empty; + string command = string.Format("progcomments:{0}", ProgramNumber); + + if (!IcdConsole.SendControlSystemCommand(command, ref progInfo)) { - string progInfo = string.Empty; - string command = string.Format("progcomments:{0}", ProgramNumber); - - if (!IcdConsole.SendControlSystemCommand(command, ref progInfo)) - { - ServiceProvider.TryGetService().AddEntry(eSeverity.Warning, "Failed to parse prog comments"); - return output; - } - - foreach (string line in progInfo.Split(new[] {"\n\r", "\r\n", "\n", "\r"})) - { - string[] pair = line.Split(':', 2).ToArray(); - string key = pair[0].Trim(); - string value = pair[1].Trim(); - output[key] = value; - } + ServiceProvider.GetService().AddEntry(eSeverity.Warning, "Failed to parse prog comments"); + return output; } - catch (Exception e) + + foreach (string line in progInfo.Split(new[] {"\n\r", "\r\n", "\n", "\r"})) { - ServiceProvider.TryGetService() - .AddEntry(eSeverity.Error, e, "Failed to parse prog comments - {0}", e.Message); + string[] pair = line.Split(':', 2).ToArray(); + + if (pair.Length < 2) + { + ServiceProvider.GetService() + .AddEntry(eSeverity.Warning, "Failed to parse prog comments line - {0}", line); + continue; + } + + string key = pair[0].Trim(); + string value = pair[1].Trim(); + output[key] = value; } return output; diff --git a/ICD.Common.Utils/StringUtils.cs b/ICD.Common.Utils/StringUtils.cs index 8a27161..325a55e 100644 --- a/ICD.Common.Utils/StringUtils.cs +++ b/ICD.Common.Utils/StringUtils.cs @@ -516,5 +516,17 @@ namespace ICD.Common.Utils return builder.ToString(); } + + /// + /// Shim of value.ToString() + /// Returns null if input value is null. + /// + /// + /// + [PublicAPI, CanBeNull] + public static string Trim(string value) + { + return value == null ? null : value.ToUpper(); + } } } diff --git a/ICD.Common.Utils/Xml/IcdXmlDocument.cs b/ICD.Common.Utils/Xml/IcdXmlDocument.cs index cd2973b..09a50f1 100644 --- a/ICD.Common.Utils/Xml/IcdXmlDocument.cs +++ b/ICD.Common.Utils/Xml/IcdXmlDocument.cs @@ -28,7 +28,7 @@ namespace ICD.Common.Utils.Xml } catch (XmlException e) { - throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition); + throw new IcdXmlException(e); } } diff --git a/ICD.Common.Utils/Xml/IcdXmlException.cs b/ICD.Common.Utils/Xml/IcdXmlException.cs index 708228f..984fbb4 100644 --- a/ICD.Common.Utils/Xml/IcdXmlException.cs +++ b/ICD.Common.Utils/Xml/IcdXmlException.cs @@ -1,4 +1,9 @@ using System; +#if SIMPLSHARP +using Crestron.SimplSharp; +#else +using System.Xml; +#endif namespace ICD.Common.Utils.Xml { @@ -20,5 +25,14 @@ namespace ICD.Common.Utils.Xml m_LineNumber = lineNumber; m_LinePosition = linePosition; } + + /// + /// Constructor. + /// + /// + public IcdXmlException(XmlException inner) + : this(inner.Message, inner, inner.LineNumber, inner.LinePosition) + { + } } } diff --git a/ICD.Common.Utils/Xml/IcdXmlReader.cs b/ICD.Common.Utils/Xml/IcdXmlReader.cs index 4103949..109c9db 100644 --- a/ICD.Common.Utils/Xml/IcdXmlReader.cs +++ b/ICD.Common.Utils/Xml/IcdXmlReader.cs @@ -50,26 +50,54 @@ namespace ICD.Common.Utils.Xml public bool MoveToNextAttribute() { - return m_Reader.MoveToNextAttribute(); + try + { + return m_Reader.MoveToNextAttribute(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public void MoveToElement() { - m_Reader.MoveToElement(); + try + { + m_Reader.MoveToElement(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public string GetAttribute(string name) { - return m_Reader.GetAttribute(name); + try + { + return m_Reader.GetAttribute(name); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public string ReadString() { + try + { #if SIMPLSHARP - return m_Reader.ReadString(); + return m_Reader.ReadString(); #else - return m_Reader.ReadElementContentAsString(); + return m_Reader.ReadElementContentAsString(); #endif + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public bool Read() @@ -80,47 +108,96 @@ namespace ICD.Common.Utils.Xml } catch (XmlException e) { - throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition); + throw new IcdXmlException(e); } } public void Dispose() { + try + { #if SIMPLSHARP - m_Reader.Dispose(true); + m_Reader.Dispose(true); #else - m_Reader.Dispose(); + m_Reader.Dispose(); #endif + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public void Skip() { - m_Reader.Skip(); + try + { + m_Reader.Skip(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public string ReadElementContentAsString() { - return m_Reader.ReadElementContentAsString(); + try + { + return m_Reader.ReadElementContentAsString(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public string ReadOuterXml() { - return m_Reader.ReadOuterXml(); + try + { + return m_Reader.ReadOuterXml(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public string ReadInnerXml() { - return m_Reader.ReadInnerXml(); + try + { + return m_Reader.ReadInnerXml(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public long ReadElementContentAsLong() { - return m_Reader.ReadElementContentAsLong(); + try + { + return m_Reader.ReadElementContentAsLong(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } public float ReadElementContentAsFloat() { - return m_Reader.ReadElementContentAsFloat(); + try + { + return m_Reader.ReadElementContentAsFloat(); + } + catch (XmlException e) + { + throw new IcdXmlException(e); + } } #endregion diff --git a/ICD.Common.Utils/Xml/XmlUtils.cs b/ICD.Common.Utils/Xml/XmlUtils.cs index 5196045..ee07697 100644 --- a/ICD.Common.Utils/Xml/XmlUtils.cs +++ b/ICD.Common.Utils/Xml/XmlUtils.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using ICD.Common.EventArguments; using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; using ICD.Common.Utils.IO; namespace ICD.Common.Utils.Xml @@ -67,7 +68,11 @@ namespace ICD.Common.Utils.Xml [PublicAPI] public static IcdXmlAttribute GetAttribute(string xml, string name) { - return GetAttributes(xml).First(a => a.Name == name); + IcdXmlAttribute output; + if (GetAttributes(xml).TryFirst(a => a.Name == name, out output)) + return output; + + throw new KeyNotFoundException(string.Format("No attribute with name {0}", name)); } /// @@ -130,6 +135,9 @@ namespace ICD.Common.Utils.Xml [PublicAPI] public static void Recurse(string xml, Action callback) { + if (callback == null) + throw new ArgumentNullException("callback"); + Recurse(xml, new Stack(), callback); } @@ -141,6 +149,12 @@ namespace ICD.Common.Utils.Xml /// private static void Recurse(string xml, Stack path, Action callback) { + if (path == null) + throw new ArgumentNullException("path"); + + if (callback == null) + throw new ArgumentNullException("callback"); + IcdXmlReader childReader; try