mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-17 21:54:58 +00:00
Merge remote-tracking branch 'origin/dev' into dev
This commit is contained in:
@@ -1,39 +0,0 @@
|
|||||||
using System;
|
|
||||||
using ICD.Common.Properties;
|
|
||||||
|
|
||||||
namespace ICD.Common.Attributes.Properties
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Provides information on a settings property.
|
|
||||||
/// </summary>
|
|
||||||
[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;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the property type.
|
|
||||||
/// </summary>
|
|
||||||
[PublicAPI]
|
|
||||||
public ePropertyType PropertyType { get { return m_PropertyType; } }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Constructor.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="propertyType"></param>
|
|
||||||
public SettingsProperty(ePropertyType propertyType)
|
|
||||||
{
|
|
||||||
m_PropertyType = propertyType;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -53,12 +53,12 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="extends"></param>
|
/// <param name="extends"></param>
|
||||||
/// <param name="item">Outputs the first item in the sequence.</param>
|
/// <param name="item">Outputs the first item in the sequence.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool TryFirstOrDefault<T>(this IEnumerable<T> extends, out T item)
|
public static bool TryFirst<T>(this IEnumerable<T> extends, out T item)
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
return extends.TryFirstOrDefault(i => true, out item);
|
return extends.TryFirst(i => true, out item);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -69,7 +69,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
/// <param name="predicate"></param>
|
/// <param name="predicate"></param>
|
||||||
/// <param name="item">Outputs the first item in the sequence.</param>
|
/// <param name="item">Outputs the first item in the sequence.</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static bool TryFirstOrDefault<T>(this IEnumerable<T> extends, Func<T, bool> predicate, out T item)
|
public static bool TryFirst<T>(this IEnumerable<T> extends, Func<T, bool> predicate, out T item)
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException("extends");
|
throw new ArgumentNullException("extends");
|
||||||
|
|||||||
68
ICD.Common.Utils/Extensions/ListExtensions.cs
Normal file
68
ICD.Common.Utils/Extensions/ListExtensions.cs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using ICD.Common.Properties;
|
||||||
|
|
||||||
|
namespace ICD.Common.Utils.Extensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Extension methods for working with Lists.
|
||||||
|
/// </summary>
|
||||||
|
public static class ListExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the item into a sorted list.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
[PublicAPI]
|
||||||
|
public static void AddSorted<T>(this List<T> extends, T item)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
extends.AddSorted(item, Comparer<T>.Default);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds the item into a sorted list.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="comparer"></param>
|
||||||
|
[PublicAPI]
|
||||||
|
public static void AddSorted<T>(this List<T> extends, T item, IComparer<T> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -71,7 +71,6 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
||||||
<Compile Include="Attributes\KrangPluginAttribute.cs" />
|
<Compile Include="Attributes\KrangPluginAttribute.cs" />
|
||||||
<Compile Include="Attributes\Properties\SettingsProperty.cs" />
|
|
||||||
<Compile Include="Attributes\Rpc\RpcAttribute.cs" />
|
<Compile Include="Attributes\Rpc\RpcAttribute.cs" />
|
||||||
<Compile Include="EventArguments\BoolEventArgs.cs" />
|
<Compile Include="EventArguments\BoolEventArgs.cs" />
|
||||||
<Compile Include="EventArguments\CharEventArgs.cs" />
|
<Compile Include="EventArguments\CharEventArgs.cs" />
|
||||||
@@ -85,6 +84,7 @@
|
|||||||
<Compile Include="EventArguments\XmlRecursionEventArgs.cs" />
|
<Compile Include="EventArguments\XmlRecursionEventArgs.cs" />
|
||||||
<None Include="ObfuscationSettings.cs" />
|
<None Include="ObfuscationSettings.cs" />
|
||||||
<Compile Include="Extensions\ByteExtensions.cs" />
|
<Compile Include="Extensions\ByteExtensions.cs" />
|
||||||
|
<Compile Include="Extensions\ListExtensions.cs" />
|
||||||
<Compile Include="Properties\Annotations.cs" />
|
<Compile Include="Properties\Annotations.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Services\Logging\ILoggerService.cs" />
|
<Compile Include="Services\Logging\ILoggerService.cs" />
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
using Crestron.SimplSharp;
|
using Crestron.SimplSharp;
|
||||||
@@ -34,6 +35,7 @@ namespace ICD.Common.Utils
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static void ConsoleCommandResponse(string message, params object[] args)
|
public static void ConsoleCommandResponse(string message, params object[] args)
|
||||||
{
|
{
|
||||||
|
if(args != null && args.Any())
|
||||||
message = string.Format(message, args);
|
message = string.Format(message, args);
|
||||||
|
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
|
|||||||
@@ -147,30 +147,30 @@ namespace ICD.Common.Utils
|
|||||||
{
|
{
|
||||||
Dictionary<string, string> output = new Dictionary<string, string>();
|
Dictionary<string, string> output = new Dictionary<string, string>();
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
string progInfo = string.Empty;
|
string progInfo = string.Empty;
|
||||||
string command = string.Format("progcomments:{0}", ProgramNumber);
|
string command = string.Format("progcomments:{0}", ProgramNumber);
|
||||||
|
|
||||||
if (!IcdConsole.SendControlSystemCommand(command, ref progInfo))
|
if (!IcdConsole.SendControlSystemCommand(command, ref progInfo))
|
||||||
{
|
{
|
||||||
ServiceProvider.TryGetService<ILoggerService>().AddEntry(eSeverity.Warning, "Failed to parse prog comments");
|
ServiceProvider.GetService<ILoggerService>().AddEntry(eSeverity.Warning, "Failed to parse prog comments");
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (string line in progInfo.Split(new[] {"\n\r", "\r\n", "\n", "\r"}))
|
foreach (string line in progInfo.Split(new[] {"\n\r", "\r\n", "\n", "\r"}))
|
||||||
{
|
{
|
||||||
string[] pair = line.Split(':', 2).ToArray();
|
string[] pair = line.Split(':', 2).ToArray();
|
||||||
|
|
||||||
|
if (pair.Length < 2)
|
||||||
|
{
|
||||||
|
ServiceProvider.GetService<ILoggerService>()
|
||||||
|
.AddEntry(eSeverity.Warning, "Failed to parse prog comments line - {0}", line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
string key = pair[0].Trim();
|
string key = pair[0].Trim();
|
||||||
string value = pair[1].Trim();
|
string value = pair[1].Trim();
|
||||||
output[key] = value;
|
output[key] = value;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
ServiceProvider.TryGetService<ILoggerService>()
|
|
||||||
.AddEntry(eSeverity.Error, e, "Failed to parse prog comments - {0}", e.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -516,5 +516,17 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
return builder.ToString();
|
return builder.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shim of value.ToString()
|
||||||
|
/// Returns null if input value is null.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
[PublicAPI, CanBeNull]
|
||||||
|
public static string Trim(string value)
|
||||||
|
{
|
||||||
|
return value == null ? null : value.ToUpper();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ namespace ICD.Common.Utils.Xml
|
|||||||
}
|
}
|
||||||
catch (XmlException e)
|
catch (XmlException e)
|
||||||
{
|
{
|
||||||
throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition);
|
throw new IcdXmlException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
|
#if SIMPLSHARP
|
||||||
|
using Crestron.SimplSharp;
|
||||||
|
#else
|
||||||
|
using System.Xml;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Xml
|
namespace ICD.Common.Utils.Xml
|
||||||
{
|
{
|
||||||
@@ -20,5 +25,14 @@ namespace ICD.Common.Utils.Xml
|
|||||||
m_LineNumber = lineNumber;
|
m_LineNumber = lineNumber;
|
||||||
m_LinePosition = linePosition;
|
m_LinePosition = linePosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructor.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="inner"></param>
|
||||||
|
public IcdXmlException(XmlException inner)
|
||||||
|
: this(inner.Message, inner, inner.LineNumber, inner.LinePosition)
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,28 +49,56 @@ namespace ICD.Common.Utils.Xml
|
|||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
public bool MoveToNextAttribute()
|
public bool MoveToNextAttribute()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return m_Reader.MoveToNextAttribute();
|
return m_Reader.MoveToNextAttribute();
|
||||||
}
|
}
|
||||||
|
catch (XmlException e)
|
||||||
|
{
|
||||||
|
throw new IcdXmlException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void MoveToElement()
|
public void MoveToElement()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
m_Reader.MoveToElement();
|
m_Reader.MoveToElement();
|
||||||
}
|
}
|
||||||
|
catch (XmlException e)
|
||||||
|
{
|
||||||
|
throw new IcdXmlException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string GetAttribute(string name)
|
public string GetAttribute(string name)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return m_Reader.GetAttribute(name);
|
return m_Reader.GetAttribute(name);
|
||||||
}
|
}
|
||||||
|
catch (XmlException e)
|
||||||
|
{
|
||||||
|
throw new IcdXmlException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string ReadString()
|
public string ReadString()
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
return m_Reader.ReadString();
|
return m_Reader.ReadString();
|
||||||
#else
|
#else
|
||||||
return m_Reader.ReadElementContentAsString();
|
return m_Reader.ReadElementContentAsString();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
catch (XmlException e)
|
||||||
|
{
|
||||||
|
throw new IcdXmlException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public bool Read()
|
public bool Read()
|
||||||
{
|
{
|
||||||
@@ -80,48 +108,97 @@ namespace ICD.Common.Utils.Xml
|
|||||||
}
|
}
|
||||||
catch (XmlException e)
|
catch (XmlException e)
|
||||||
{
|
{
|
||||||
throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition);
|
throw new IcdXmlException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
m_Reader.Dispose(true);
|
m_Reader.Dispose(true);
|
||||||
#else
|
#else
|
||||||
m_Reader.Dispose();
|
m_Reader.Dispose();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
catch (XmlException e)
|
||||||
|
{
|
||||||
|
throw new IcdXmlException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void Skip()
|
public void Skip()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
m_Reader.Skip();
|
m_Reader.Skip();
|
||||||
}
|
}
|
||||||
|
catch (XmlException e)
|
||||||
|
{
|
||||||
|
throw new IcdXmlException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string ReadElementContentAsString()
|
public string ReadElementContentAsString()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return m_Reader.ReadElementContentAsString();
|
return m_Reader.ReadElementContentAsString();
|
||||||
}
|
}
|
||||||
|
catch (XmlException e)
|
||||||
|
{
|
||||||
|
throw new IcdXmlException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string ReadOuterXml()
|
public string ReadOuterXml()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return m_Reader.ReadOuterXml();
|
return m_Reader.ReadOuterXml();
|
||||||
}
|
}
|
||||||
|
catch (XmlException e)
|
||||||
|
{
|
||||||
|
throw new IcdXmlException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public string ReadInnerXml()
|
public string ReadInnerXml()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return m_Reader.ReadInnerXml();
|
return m_Reader.ReadInnerXml();
|
||||||
}
|
}
|
||||||
|
catch (XmlException e)
|
||||||
|
{
|
||||||
|
throw new IcdXmlException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public long ReadElementContentAsLong()
|
public long ReadElementContentAsLong()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return m_Reader.ReadElementContentAsLong();
|
return m_Reader.ReadElementContentAsLong();
|
||||||
}
|
}
|
||||||
|
catch (XmlException e)
|
||||||
|
{
|
||||||
|
throw new IcdXmlException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public float ReadElementContentAsFloat()
|
public float ReadElementContentAsFloat()
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
return m_Reader.ReadElementContentAsFloat();
|
return m_Reader.ReadElementContentAsFloat();
|
||||||
}
|
}
|
||||||
|
catch (XmlException e)
|
||||||
|
{
|
||||||
|
throw new IcdXmlException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using ICD.Common.EventArguments;
|
using ICD.Common.EventArguments;
|
||||||
using ICD.Common.Properties;
|
using ICD.Common.Properties;
|
||||||
|
using ICD.Common.Utils.Extensions;
|
||||||
using ICD.Common.Utils.IO;
|
using ICD.Common.Utils.IO;
|
||||||
|
|
||||||
namespace ICD.Common.Utils.Xml
|
namespace ICD.Common.Utils.Xml
|
||||||
@@ -67,7 +68,11 @@ namespace ICD.Common.Utils.Xml
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static IcdXmlAttribute GetAttribute(string xml, string name)
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -130,6 +135,9 @@ namespace ICD.Common.Utils.Xml
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public static void Recurse(string xml, Action<XmlRecursionEventArgs> callback)
|
public static void Recurse(string xml, Action<XmlRecursionEventArgs> callback)
|
||||||
{
|
{
|
||||||
|
if (callback == null)
|
||||||
|
throw new ArgumentNullException("callback");
|
||||||
|
|
||||||
Recurse(xml, new Stack<string>(), callback);
|
Recurse(xml, new Stack<string>(), callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,6 +149,12 @@ namespace ICD.Common.Utils.Xml
|
|||||||
/// <param name="callback"></param>
|
/// <param name="callback"></param>
|
||||||
private static void Recurse(string xml, Stack<string> path, Action<XmlRecursionEventArgs> callback)
|
private static void Recurse(string xml, Stack<string> path, Action<XmlRecursionEventArgs> callback)
|
||||||
{
|
{
|
||||||
|
if (path == null)
|
||||||
|
throw new ArgumentNullException("path");
|
||||||
|
|
||||||
|
if (callback == null)
|
||||||
|
throw new ArgumentNullException("callback");
|
||||||
|
|
||||||
IcdXmlReader childReader;
|
IcdXmlReader childReader;
|
||||||
|
|
||||||
try
|
try
|
||||||
|
|||||||
Reference in New Issue
Block a user