servicepoint/crates/servicepoint_binding_cs/ServicePoint/Cp437Grid.cs

135 lines
2.8 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-08-29 21:40:33 +02:00
public sealed class Cp437Grid : SpNativeInstance<BindGen.Cp437Grid>
2024-05-13 00:17:40 +02:00
{
2024-06-23 16:30:45 +02:00
public static Cp437Grid New(int width, int height)
2024-05-13 00:17:40 +02:00
{
unsafe
{
2024-06-23 16:30:45 +02:00
return new Cp437Grid(NativeMethods.sp_cp437_grid_new((nuint)width, (nuint)height));
2024-05-13 00:17:40 +02:00
}
}
2024-06-23 16:30:45 +02:00
public static Cp437Grid Load(int width, int height, Span<byte> bytes)
2024-05-13 00:17:40 +02:00
{
unsafe
{
fixed (byte* bytesPtr = bytes)
{
2024-06-23 16:30:45 +02:00
return new Cp437Grid(NativeMethods.sp_cp437_grid_load((nuint)width, (nuint)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-06-23 16:30:45 +02:00
return new Cp437Grid(NativeMethods.sp_cp437_grid_clone(Instance));
2024-05-13 00:17:40 +02:00
}
}
public byte this[int x, int y]
{
get
{
unsafe
{
2024-06-23 16:30:45 +02:00
return NativeMethods.sp_cp437_grid_get(Instance, (nuint)x, (nuint)y);
2024-05-13 00:17:40 +02:00
}
}
set
{
unsafe
{
2024-06-23 16:30:45 +02:00
NativeMethods.sp_cp437_grid_set(Instance, (nuint)x, (nuint)y, value);
2024-05-13 00:17:40 +02:00
}
}
}
public string this[int y]
{
set
{
var width = Width;
ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Length, width);
var x = 0;
for (; x < value.Length; x++)
this[x, y] = (byte)value[x];
for (; x < width; x++)
this[x, y] = 0;
}
get
{
var sb = new StringBuilder();
for (int 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-06-23 16:30:45 +02:00
NativeMethods.sp_cp437_grid_fill(Instance, value);
2024-05-13 00:17:40 +02:00
}
}
public int Width
{
get
{
unsafe
{
2024-06-23 16:30:45 +02:00
return (int)NativeMethods.sp_cp437_grid_width(Instance);
2024-05-13 00:17:40 +02:00
}
}
}
public int Height
{
get
{
unsafe
{
2024-06-23 16:30:45 +02:00
return (int)NativeMethods.sp_cp437_grid_height(Instance);
2024-05-13 00:17:40 +02:00
}
}
}
public Span<byte> Data
{
get
{
unsafe
{
2024-06-23 16:30:45 +02:00
var slice = NativeMethods.sp_cp437_grid_unsafe_data_ref(Instance);
return new Span<byte>(slice.start, (int)slice.length);
}
}
}
2024-08-29 21:40:33 +02:00
private unsafe Cp437Grid(BindGen.Cp437Grid* instance) : base(instance)
2024-05-13 00:17:40 +02:00
{
}
private protected override unsafe void Dealloc()
2024-05-13 00:17:40 +02:00
{
2024-06-23 16:30:45 +02:00
NativeMethods.sp_cp437_grid_dealloc(Instance);
2024-05-13 00:17:40 +02:00
}
}