93 lines
3.0 KiB
C#
93 lines
3.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace Sarand.Net.Standard.Base.Data
|
|
{
|
|
//=======================================================================
|
|
public class SacsData
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
/// <summary>Construction - Empty data.</summary>
|
|
public SacsData () { }
|
|
/// <summary>Construction - Data from byte array.</summary>
|
|
public SacsData (byte[] data)
|
|
{
|
|
DataBuffer = new byte[data?.Length ?? 0];
|
|
DataLength = data?.Length ?? 0;
|
|
if (DataLength > 0)
|
|
Array.Copy(data, 0, DataBuffer, 0, DataLength);
|
|
CheckForNull();
|
|
}
|
|
/// <summary>
|
|
/// Construction - Data from string.
|
|
/// <para>Encoding: ASCII</para>
|
|
/// </summary>
|
|
public SacsData (string data) :
|
|
this(data, Encoding.ASCII)
|
|
{ }
|
|
/// <summary>Construction - Data from string with specific encoding.</summary>
|
|
public SacsData (string data, Encoding encoding) :
|
|
this(encoding.GetBytes(data))
|
|
{
|
|
StringEncoding = encoding;
|
|
}
|
|
/// <summary>Copy construction.</summary>
|
|
public SacsData (SacsData data) :
|
|
this(data.DataBuffer)
|
|
{
|
|
StringEncoding = data.StringEncoding;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
/// <summary>Data buffer - cannot be replaced but individual entries can be amended.</summary>
|
|
public byte[] DataBuffer { get; protected set; } = new byte[0];
|
|
/// <summary>Length of data may be less than the buffer length.</summary>
|
|
public int DataLength { get; protected set; }
|
|
/// <summary>Encoding used to convert data to String.</summary>
|
|
public Encoding StringEncoding { get; set; } = Encoding.ASCII;
|
|
/// <summary>Does the data contain null (0x00) values?<para>In which case conversion to String will be invalid.</para></summary>
|
|
public bool ContainsNull { get; protected set; }
|
|
|
|
//-------------------------------------------------------------------
|
|
// Utilities.
|
|
private void CheckForNull()
|
|
{
|
|
ContainsNull = Array.Exists(DataBuffer, item => item == 0);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Conversion to String.
|
|
public override String ToString ()
|
|
{
|
|
return String.Format(GetType().Name + " [Length: {0}]", DataLength);
|
|
}
|
|
public static implicit operator String (SacsData data)
|
|
{
|
|
return data.StringEncoding.GetString(data.DataBuffer);
|
|
}
|
|
public String NullTerminatedString
|
|
{
|
|
get
|
|
{
|
|
int nullIdx = Array.IndexOf(DataBuffer,(byte)0);
|
|
if (nullIdx >= 0)
|
|
return StringEncoding.GetString(DataBuffer.Take(nullIdx).ToArray());
|
|
return StringEncoding.GetString(DataBuffer);
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Append data.
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|