servicepoint/crates/servicepoint_binding_cs/ServicePoint/Cp437Grid.cs

131 lines
2.5 KiB
C#
Raw Normal View History

using System.Text;
using ServicePoint.BindGen;
2024-05-13 00:17:40 +02:00
namespace ServicePoint;
2024-05-13 00:17:40 +02:00
2024-10-16 22:46:34 +02:00
public sealed class Cp437Grid : SpNativeInstance<BindGen.Cp437Grid>
2024-05-13 00:17:40 +02:00
{
2024-10-16 22:46:34 +02:00
public static Cp437Grid New(nuint width, nuint height)
2024-05-13 00:17:40 +02:00
{
unsafe
{
2024-10-16 22:46:34 +02:00
return new Cp437Grid(Cp437GridNative.sp_cp437_grid_new(width, height));
2024-05-13 00:17:40 +02:00
}
}
2024-10-16 22:46:34 +02:00
public static Cp437Grid Load(nuint width, nuint height, Span<byte> bytes)
2024-05-13 00:17:40 +02:00
{
unsafe
{
fixed (byte* bytesPtr = bytes)
{
2024-10-16 22:46:34 +02:00
return new Cp437Grid(Cp437GridNative.sp_cp437_grid_load(width, height, bytesPtr,
2024-05-13 00:17:40 +02:00
(nuint)bytes.Length));
}
}
}
2024-06-23 16:30:45 +02:00
public Cp437Grid Clone()
2024-05-13 00:17:40 +02:00
{
unsafe
{
2024-10-16 22:46:34 +02:00
return new Cp437Grid(Instance->Clone());
2024-05-13 00:17:40 +02:00
}
}
2024-10-16 22:46:34 +02:00
public byte this[nuint x, nuint y]
2024-05-13 00:17:40 +02:00
{
get
{
unsafe
{
2024-10-16 22:46:34 +02:00
return Instance->Get(x, y);
2024-05-13 00:17:40 +02:00
}
}
set
{
unsafe
{
2024-10-16 22:46:34 +02:00
Instance->Set(x, y, value);
2024-05-13 00:17:40 +02:00
}
}
}
2024-10-16 22:46:34 +02:00
public string this[nuint y]
{
set
{
var width = Width;
2024-10-16 22:46:34 +02:00
ArgumentOutOfRangeException.ThrowIfGreaterThan((nuint)value.Length, width);
2024-10-16 22:46:34 +02:00
nuint x = 0;
for (; x < (nuint)value.Length; x++)
this[x, y] = (byte)value[(int)x];
for (; x < width; x++)
this[x, y] = 0;
}
get
{
var sb = new StringBuilder();
2024-10-16 22:46:34 +02:00
for (nuint x = 0; x < Width; x++)
{
var val = this[x, y];
if (val == 0)
break;
sb.Append((char)val);
}
return sb.ToString();
}
}
2024-05-13 00:17:40 +02:00
public void Fill(byte value)
{
unsafe
{
2024-10-16 22:46:34 +02:00
Instance->Fill(value);
2024-05-13 00:17:40 +02:00
}
}
2024-10-16 22:46:34 +02:00
public nuint Width
2024-05-13 00:17:40 +02:00
{
get
{
unsafe
{
2024-10-16 22:46:34 +02:00
return Instance->Width();
2024-05-13 00:17:40 +02:00
}
}
}
2024-10-16 22:46:34 +02:00
public nuint Height
2024-05-13 00:17:40 +02:00
{
get
{
unsafe
{
2024-10-16 22:46:34 +02:00
return Instance->Height();
2024-05-13 00:17:40 +02:00
}
}
}
public Span<byte> Data
{
get
{
unsafe
{
2024-10-16 22:46:34 +02:00
return Instance->UnsafeDataRef().AsSpan();
}
}
}
2024-10-16 22:46:34 +02:00
private unsafe Cp437Grid(BindGen.Cp437Grid* instance) : base(instance)
2024-05-13 00:17:40 +02:00
{
}
2024-10-16 22:46:34 +02:00
private protected override unsafe void Free() => Instance->Free();
2024-05-13 00:17:40 +02:00
}