KerepWrapper project
This commit is contained in:
114
KerepWrapper/Dtsod/DtsodV24.cs
Normal file
114
KerepWrapper/Dtsod/DtsodV24.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using DTLib.Dtsod;
|
||||
using KerepWrapper.KerepTypes;
|
||||
using KerepWrapper.Autoarr;
|
||||
using Funcs=KerepWrapper.Dtsod.DtsodV24Functions;
|
||||
|
||||
namespace KerepWrapper.Dtsod;
|
||||
|
||||
public class DtsodV24 : IDtsod, IEnumerable<KVPair>, IDisposable
|
||||
{
|
||||
public DtsodVersion Version => DtsodVersion.V24;
|
||||
public readonly DtsodPtr UnmanagedPtr;
|
||||
//if true, destructor frees allocated unmanaged memory
|
||||
public bool AutoDispose = true;
|
||||
public ushort Height => Funcs.Height(UnmanagedPtr);
|
||||
|
||||
public DtsodV24(DtsodPtr ptr) => UnmanagedPtr = ptr;
|
||||
public DtsodV24(string text, bool autoDispose = true) : this(Funcs.Deserialize(text))
|
||||
=> AutoDispose = autoDispose;
|
||||
public DtsodV24(bool autoDispose=true) : this(" ", autoDispose) { }
|
||||
|
||||
public DtsodV24(IDictionary<string,dynamic> dict, bool autoDispose=true) : this(autoDispose)
|
||||
{
|
||||
foreach (KeyValuePair<string, dynamic> pair in dict)
|
||||
{
|
||||
if (pair.Value is not null) AddOrSet(pair.Key, pair.Value);
|
||||
//else Log("y", $"skiping key <{pair.Key}> with null value");
|
||||
}
|
||||
}
|
||||
|
||||
public IDictionary<string, dynamic> ToDictionary()
|
||||
{
|
||||
DtsodDict<string, dynamic> dict = new();
|
||||
foreach (var p in this)
|
||||
dict.Add(p.key,p.value.ToDynamic());
|
||||
return dict;
|
||||
}
|
||||
|
||||
|
||||
public bool TryGet(string key, out dynamic elem)
|
||||
{
|
||||
var g = Funcs.Get(UnmanagedPtr, key);
|
||||
elem = g.ToDynamic();
|
||||
return g.TypeCode == KerepTypeCode.Null;
|
||||
}
|
||||
|
||||
public void AddOrSet(string key, dynamic value) =>
|
||||
Funcs.AddOrSet(UnmanagedPtr, key, new Unitype(value));
|
||||
|
||||
public dynamic this[string key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!TryGet(key, out var v)) throw new KeyNotFoundException($"key <{key}> not found");
|
||||
return v;
|
||||
}
|
||||
set => AddOrSet(key, value);
|
||||
}
|
||||
|
||||
public bool TryRemove(string key) => Funcs.Remove(UnmanagedPtr,key);
|
||||
|
||||
[Obsolete("do you really need to use this? look at TryGet/TryRemove/AddOrSet")]
|
||||
public bool ContainsKey(string key) => Funcs.Contains(UnmanagedPtr, key);
|
||||
|
||||
public override string ToString() => Funcs.Serialize(UnmanagedPtr);
|
||||
|
||||
public void Dispose() => Funcs.Free(UnmanagedPtr);
|
||||
|
||||
~DtsodV24()
|
||||
{
|
||||
if(AutoDispose) Dispose();
|
||||
}
|
||||
|
||||
|
||||
public IEnumerator<KVPair> GetEnumerator() => new DtsodV24Enumerator(this);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
class DtsodV24Enumerator: IEnumerator<KVPair>
|
||||
{
|
||||
private readonly DtsodV24 d;
|
||||
private ushort h;
|
||||
private IEnumerator<KVPair> arEnumerator;
|
||||
|
||||
public DtsodV24Enumerator(DtsodV24 _d) => d = _d;
|
||||
|
||||
bool NextAr()
|
||||
{
|
||||
if (h >= Funcs.Height(d.UnmanagedPtr)) return false;
|
||||
var ar = new Autoarr<KVPair>(Funcs.GetRow(d.UnmanagedPtr, h), false);
|
||||
arEnumerator = ar.GetEnumerator();
|
||||
h++;
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if(arEnumerator==null)
|
||||
NextAr();
|
||||
while(!arEnumerator.MoveNext())
|
||||
if(!NextAr()) return false;
|
||||
Current = arEnumerator.Current;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset() => h = 0;
|
||||
public KVPair Current { get; private set; }
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
public void Dispose() { }
|
||||
}
|
||||
}
|
||||
110
KerepWrapper/Dtsod/DtsodV24Functions.cs
Normal file
110
KerepWrapper/Dtsod/DtsodV24Functions.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
global using DtsodPtr=System.IntPtr;
|
||||
global using AutoarrKVPairPtr=System.IntPtr;
|
||||
global using AutoarrUnitypePtr=System.IntPtr;
|
||||
global using CharPtr=System.IntPtr;
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using DTLib;
|
||||
using DTLib.Extensions;
|
||||
using KerepWrapper.KerepTypes;
|
||||
|
||||
namespace KerepWrapper.Dtsod;
|
||||
|
||||
internal static class DtsodV24Functions
|
||||
{
|
||||
private const string kereplib = "kerep";
|
||||
|
||||
static DtsodV24Functions()
|
||||
{
|
||||
DependencyResolver.CopyLibs();
|
||||
}
|
||||
|
||||
static void TryThrowErrmsg(CharPtr err)
|
||||
{
|
||||
if (err == IntPtr.Zero) return;
|
||||
string errmsg = Unmanaged.HGlobalUTF8ToString(err);
|
||||
Marshal.FreeHGlobal(err);
|
||||
throw new Exception(errmsg);
|
||||
}
|
||||
|
||||
|
||||
//parses text to binary values
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_deserialize(string text, out DtsodPtr output, out CharPtr errmsg);
|
||||
internal static DtsodPtr Deserialize(string text)
|
||||
{
|
||||
kerep_DtsodV24_deserialize(text, out var dtsod,out var err);
|
||||
TryThrowErrmsg(err);
|
||||
return dtsod;
|
||||
}
|
||||
|
||||
//creates text representation of dtsod
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_serialize(DtsodPtr dtsod, out CharPtr output, out CharPtr errmsg);
|
||||
internal static string Serialize(DtsodPtr dtsod)
|
||||
{
|
||||
kerep_DtsodV24_serialize(dtsod, out var text, out var err);
|
||||
TryThrowErrmsg(err);
|
||||
return Unmanaged.HGlobalUTF8ToString(text);
|
||||
}
|
||||
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_get(DtsodPtr dtsod, string key, out Unitype output);
|
||||
//returns value or UniNull if key not found
|
||||
internal static Unitype Get(DtsodPtr dtsod, string key)
|
||||
{
|
||||
kerep_DtsodV24_get(dtsod, key, out var output);
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport(kereplib,EntryPoint = "kerep_DtsodV24_addOrSet",CallingConvention = CallingConvention.Cdecl)]
|
||||
//adds or sets value
|
||||
static extern void kerep_DtsodV24_addOrSet(DtsodPtr dtsod, IntPtr key, Unitype value);
|
||||
|
||||
internal static void AddOrSet(DtsodPtr dtsod, string key, Unitype value)
|
||||
{
|
||||
IntPtr keyptr = key.StringToHGlobalUTF8();
|
||||
kerep_DtsodV24_addOrSet(dtsod, keyptr, value);
|
||||
}
|
||||
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
//checks for dtsod contains value or dont
|
||||
static extern void kerep_DtsodV24_contains(DtsodPtr dtsod, string key, [MarshalAs(UnmanagedType.I1)] out bool output);
|
||||
internal static bool Contains(DtsodPtr dtsod, string key)
|
||||
{
|
||||
kerep_DtsodV24_contains(dtsod, key, out var output);
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_remove(DtsodPtr dtsod, string key, [MarshalAs(UnmanagedType.I1)] out bool output);
|
||||
//replaces value with UniNull if key exists in dtsod
|
||||
internal static bool Remove(DtsodPtr dtsod, string key)
|
||||
{
|
||||
kerep_DtsodV24_remove(dtsod, key, out var output);
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport(kereplib,EntryPoint="kerep_DtsodV24_free", CallingConvention = CallingConvention.Cdecl)]
|
||||
//replaces value with UniNull if key exists in dtsod
|
||||
internal static extern void Free(DtsodPtr dtsod);
|
||||
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_height(DtsodPtr dtsod, out ushort heigth);
|
||||
//returns current amounts of rows (Autoarrs of KVPairs) in hashtable
|
||||
internal static ushort Height(DtsodPtr ptr)
|
||||
{
|
||||
kerep_DtsodV24_height(ptr, out var h);
|
||||
return h;
|
||||
}
|
||||
|
||||
[DllImport(kereplib, CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_getrow(DtsodPtr dtsod, ushort h, out AutoarrKVPairPtr row);
|
||||
//Returns row from hashtable.
|
||||
//check current hashtable height before calling this.
|
||||
internal static AutoarrKVPairPtr GetRow(DtsodPtr ptr, ushort height)
|
||||
{
|
||||
kerep_DtsodV24_getrow(ptr, height, out var rowptr);
|
||||
return rowptr;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user