/* Copyright (c) 2017 Ahmed Kh. Zamil Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using Esiur.Misc; using System.Reflection; namespace Esiur.Data { /// /// BinaryList holds a list of items to be converted to binary for storage and transmission /// public class BinaryList { private List held = new List(); /// /// Create an instance of BinaryList /// public BinaryList() { } /// /// Converts parameters to binary in same order /// /// Variables to convert public static byte[] ToBytes(params object[] values) { var held = new List(); foreach (var i in values) { if (i is byte) held.Add((byte)i); else { #if NETSTANDARD1_5 MethodInfo mi = typeof(DC).GetTypeInfo().GetMethod("ToBytes", new Type[] { i.GetType() }); #else MethodInfo mi = typeof(DC).GetMethod("ToBytes", new Type[] { i.GetType() }); #endif if (mi != null) { var b = (byte[])mi.Invoke(null, new object[] { i }); held.AddRange(b); } } } return held.ToArray(); } /// /// Create a new instance of BinaryList /// /// Populate the list items public BinaryList(params object[] values) { AddRange(values); } /// /// Add an array of items at the end of the list /// /// Array of items public void AddRange(object[] values) { foreach (var i in values) { if (i is byte) held.Add((byte)i); else { #if NETSTANDARD1_5 MethodInfo mi = typeof(DC).GetTypeInfo().GetMethod("ToBytes", new Type[] { i.GetType() }); #else MethodInfo mi = typeof(DC).GetMethod("ToBytes", new Type[] { i.GetType() }); #endif if (mi != null) { var b = (byte[])mi.Invoke(null, new object[] {i}); held.AddRange(b); } } } } /// /// Add multiple items at the end of the list /// /// Parameters of items public void Append(params object[] values) { AddRange(values); } /// /// Insert new items to the list at a specified index /// /// Position in the list /// Items to insert public void Insert(int offset, params object[] values) { foreach (var i in values) { if (i is byte) { held.Insert(offset++, (byte)i); } else { #if NETSTANDARD1_5 MethodInfo mi = typeof(DC).GetTypeInfo().GetMethod("ToBytes", new Type[] { i.GetType() }); #else MethodInfo mi = typeof(DC).GetMethod("ToBytes", new Type[] { i.GetType() }); #endif if (mi != null) { var b = (byte[])mi.Invoke(null, new object[] { i }); held.InsertRange(offset, b); offset += b.Length; } } } } /// /// Number of the items in the list /// public int Length { get { return held.Count; } } /* public void Append(byte data) { held.Add(data); } public void Append(byte[] data) { held.AddRange(data); } public void Append(int data) { held.AddRange(DC.ToBytes(data)); } public void Append(uint data) { held.AddRange(DC.ToBytes(data)); } public void Append(float data) { held.AddRange(DC.ToBytes(data)); } public void Append(short data) { held.AddRange(DC.ToBytes(data)); } public void Append(ushort data) { held.AddRange(DC.ToBytes(data)); } public void Append(double data) { held.AddRange(DC.ToBytes(data)); } public void Append(sbyte data) { held.Add((byte)data); } */ /// /// Convert the list to an array of bytes /// /// Bytes array public byte[] ToArray() { return held.ToArray(); } } }