mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-14 20:25:01 +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="item">Outputs the first item in the sequence.</param>
|
||||
/// <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)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.TryFirstOrDefault(i => true, out item);
|
||||
return extends.TryFirst(i => true, out item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -69,7 +69,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="item">Outputs the first item in the sequence.</param>
|
||||
/// <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)
|
||||
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>
|
||||
<Compile Include="Attributes\AbstractIcdAttribute.cs" />
|
||||
<Compile Include="Attributes\KrangPluginAttribute.cs" />
|
||||
<Compile Include="Attributes\Properties\SettingsProperty.cs" />
|
||||
<Compile Include="Attributes\Rpc\RpcAttribute.cs" />
|
||||
<Compile Include="EventArguments\BoolEventArgs.cs" />
|
||||
<Compile Include="EventArguments\CharEventArgs.cs" />
|
||||
@@ -85,6 +84,7 @@
|
||||
<Compile Include="EventArguments\XmlRecursionEventArgs.cs" />
|
||||
<None Include="ObfuscationSettings.cs" />
|
||||
<Compile Include="Extensions\ByteExtensions.cs" />
|
||||
<Compile Include="Extensions\ListExtensions.cs" />
|
||||
<Compile Include="Properties\Annotations.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Services\Logging\ILoggerService.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
|
||||
|
||||
@@ -147,29 +147,29 @@ namespace ICD.Common.Utils
|
||||
{
|
||||
Dictionary<string, string> output = new Dictionary<string, string>();
|
||||
|
||||
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<ILoggerService>().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<ILoggerService>().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<ILoggerService>()
|
||||
.AddEntry(eSeverity.Error, e, "Failed to parse prog comments - {0}", e.Message);
|
||||
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 value = pair[1].Trim();
|
||||
output[key] = value;
|
||||
}
|
||||
|
||||
return output;
|
||||
|
||||
@@ -516,5 +516,17 @@ namespace ICD.Common.Utils
|
||||
|
||||
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)
|
||||
{
|
||||
throw new IcdXmlException(e.Message, e, e.LineNumber, e.LinePosition);
|
||||
throw new IcdXmlException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="inner"></param>
|
||||
public IcdXmlException(XmlException inner)
|
||||
: this(inner.Message, inner, inner.LineNumber, inner.LinePosition)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -130,6 +135,9 @@ namespace ICD.Common.Utils.Xml
|
||||
[PublicAPI]
|
||||
public static void Recurse(string xml, Action<XmlRecursionEventArgs> callback)
|
||||
{
|
||||
if (callback == null)
|
||||
throw new ArgumentNullException("callback");
|
||||
|
||||
Recurse(xml, new Stack<string>(), callback);
|
||||
}
|
||||
|
||||
@@ -141,6 +149,12 @@ namespace ICD.Common.Utils.Xml
|
||||
/// <param name="callback"></param>
|
||||
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;
|
||||
|
||||
try
|
||||
|
||||
Reference in New Issue
Block a user