DtsodV24 wrapper almost done
This commit is contained in:
parent
1416b83dc5
commit
a72b00640f
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net6.0;net48</TargetFrameworks>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<LangVersion>10</LangVersion>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
@ -25,7 +25,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Dependencies\kerep.dll">
|
||||
<None Update="Dependencies\*">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
BIN
DTLib.Dtsod/Dependencies/kerep.dll
Normal file
BIN
DTLib.Dtsod/Dependencies/kerep.dll
Normal file
Binary file not shown.
BIN
DTLib.Dtsod/Dependencies/kerep.so
Normal file
BIN
DTLib.Dtsod/Dependencies/kerep.so
Normal file
Binary file not shown.
@ -1,4 +1,6 @@
|
||||
namespace DTLib.Dtsod;
|
||||
using DTLib.Dtsod.V24;
|
||||
|
||||
namespace DTLib.Dtsod;
|
||||
|
||||
public static class DtsodConverter
|
||||
{
|
||||
@ -8,6 +10,7 @@ public static class DtsodConverter
|
||||
DtsodVersion.V21 => new DtsodV21(src.ToDictionary()),
|
||||
DtsodVersion.V22 => throw new Exception("DtsodV22 is deprecated"),
|
||||
DtsodVersion.V23 => new DtsodV23(src.ToDictionary()),
|
||||
DtsodVersion.V24 => new DtsodV24(src.ToDictionary()),
|
||||
#if DEBUG
|
||||
DtsodVersion.V30 => new DtsodV30(src.ToDictionary()),
|
||||
#endif
|
||||
|
||||
@ -1,46 +1,52 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DTLib.Dtsod.V24.Autoarr;
|
||||
|
||||
public class Autoarr<T> : IEnumerable<T>, IDisposable where T : struct
|
||||
{
|
||||
private readonly IntPtr AutoarrHandler;
|
||||
private readonly AutoarrFunctions<T> Funcs;
|
||||
public readonly uint MaxLength;
|
||||
public readonly IntPtr UnmanagedPtr;
|
||||
//if true, destructor frees allocated unmanaged memory
|
||||
public bool AutoDispose;
|
||||
private readonly AutoarrFunctions<T> Funcs;
|
||||
public readonly uint MaxLength;
|
||||
public uint Length { get; private set; }
|
||||
|
||||
public Autoarr(IntPtr ptr, bool autoDispose=true)
|
||||
public Autoarr(IntPtr ptr, bool autoDispose)
|
||||
{
|
||||
AutoDispose = autoDispose;
|
||||
Funcs = AutoarrFunctions<T>.GetFunctions();
|
||||
MaxLength = Funcs.MaxLength(AutoarrHandler);
|
||||
Length = Funcs.Length(AutoarrHandler);
|
||||
AutoarrHandler = ptr;
|
||||
UnmanagedPtr = ptr;
|
||||
MaxLength = Funcs.MaxLength(UnmanagedPtr);
|
||||
Length = Funcs.Length(UnmanagedPtr);
|
||||
}
|
||||
|
||||
public Autoarr(ushort blockCount, ushort blockLength, bool autoDispose=true) : this(IntPtr.Zero,autoDispose)
|
||||
public Autoarr(ushort blockCount, ushort blockLength, bool autoDispose=true)
|
||||
{
|
||||
AutoarrHandler = Funcs.Create(blockCount, blockLength);
|
||||
AutoDispose = autoDispose;
|
||||
Funcs = AutoarrFunctions<T>.GetFunctions();
|
||||
UnmanagedPtr = Funcs.Create(blockCount, blockLength);
|
||||
MaxLength = Funcs.MaxLength(UnmanagedPtr);
|
||||
Length = Funcs.Length(UnmanagedPtr);
|
||||
}
|
||||
|
||||
public uint Length { get; private set; }
|
||||
|
||||
public T this[uint i]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (i < Length) return Funcs.Get(AutoarrHandler, i);
|
||||
if (i < Length) return Funcs.Get(UnmanagedPtr, i);
|
||||
throw new IndexOutOfRangeException($"index {i} >= Autoarr.Length {Length}");
|
||||
}
|
||||
set
|
||||
{
|
||||
if (i < Length) Funcs.Set(AutoarrHandler, i, value);
|
||||
if (i < Length) Funcs.Set(UnmanagedPtr, i, value);
|
||||
else throw new IndexOutOfRangeException($"index {i} >= Autoarr.Length {Length}");
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Funcs.Free(AutoarrHandler);
|
||||
Funcs.Free(UnmanagedPtr);
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
@ -55,7 +61,11 @@ public class Autoarr<T> : IEnumerable<T>, IDisposable where T : struct
|
||||
|
||||
public void Add(T value)
|
||||
{
|
||||
if (Length++ < MaxLength - 1) Funcs.Add(AutoarrHandler, value);
|
||||
if (Length < MaxLength)
|
||||
{
|
||||
Funcs.Add(UnmanagedPtr, value);
|
||||
Length++;
|
||||
}
|
||||
else throw new IndexOutOfRangeException($"Autoarr.Length == MaxLength ({MaxLength})");
|
||||
}
|
||||
|
||||
@ -74,7 +84,6 @@ public class Autoarr<T> : IEnumerable<T>, IDisposable where T : struct
|
||||
arr = ar;
|
||||
}
|
||||
|
||||
|
||||
public T Current { get; private set; }
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
@ -84,9 +93,10 @@ public class Autoarr<T> : IEnumerable<T>, IDisposable where T : struct
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
var r = ++index < arr.Length;
|
||||
if (r) Current = arr[index];
|
||||
return r;
|
||||
if (index >= arr.Length) return false;
|
||||
Current = arr[index];
|
||||
index++;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
|
||||
@ -1,10 +1,15 @@
|
||||
using System.Dynamic;
|
||||
using DTLib.Dtsod.V24.KerepTypes;
|
||||
|
||||
namespace DTLib.Dtsod.V24.Autoarr;
|
||||
using AutoarrPtr=System.IntPtr;
|
||||
using AutoarrPtr=IntPtr;
|
||||
|
||||
public abstract class AutoarrFunctions<T>
|
||||
{
|
||||
static AutoarrFunctions()
|
||||
{
|
||||
DependencyResolver.CopyLibs();
|
||||
}
|
||||
|
||||
internal abstract AutoarrPtr Create(ushort maxBlocksCount, ushort maxBlockLength);
|
||||
internal abstract void Free(AutoarrPtr ar);
|
||||
internal abstract T Get(AutoarrPtr ar, uint index);
|
||||
@ -17,10 +22,15 @@ public abstract class AutoarrFunctions<T>
|
||||
private static AutoarrFunctions<KVPair> f_kvp = new AutoarrKVPairFunctions();
|
||||
static internal AutoarrFunctions<T> GetFunctions()
|
||||
{
|
||||
if (typeof(T) == typeof(Unitype))
|
||||
if (f_kvp is AutoarrFunctions<T> f)
|
||||
return f;
|
||||
else if (f_uni is AutoarrFunctions<T> ff)
|
||||
return ff;
|
||||
else throw new Exception($"unsupported type: {typeof(T)}");
|
||||
/*if (typeof(T) == typeof(Unitype))
|
||||
return (AutoarrFunctions<T>)Convert.ChangeType(f_uni, typeof(AutoarrFunctions<T>));
|
||||
else if (typeof(T) == typeof(KVPair))
|
||||
return (AutoarrFunctions<T>) Convert.ChangeType(f_kvp, typeof(AutoarrFunctions<T>));
|
||||
else throw new Exception($"unsupported type: {typeof(T)}");
|
||||
else throw new Exception($"unsupported type: {typeof(T)}");*/
|
||||
}
|
||||
}
|
||||
@ -1,10 +1,11 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using DTLib.Dtsod.V24.KerepTypes;
|
||||
|
||||
namespace DTLib.Dtsod.V24.Autoarr;
|
||||
|
||||
internal class AutoarrKVPairFunctions : AutoarrFunctions<KVPair>
|
||||
{
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_create(ushort max_blocks_count, ushort max_block_length, out AutoarrKVPairPtr output);
|
||||
internal override AutoarrKVPairPtr Create(ushort maxBlocksCount, ushort maxBlockLength)
|
||||
{
|
||||
@ -12,11 +13,11 @@ internal class AutoarrKVPairFunctions : AutoarrFunctions<KVPair>
|
||||
return ar;
|
||||
}
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_KVPair_free(AutoarrKVPairPtr ar);
|
||||
internal override void Free(AutoarrKVPairPtr ar) => kerep_Autoarr_KVPair_free(ar);
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_get(AutoarrKVPairPtr ar, uint index, out KVPair output);
|
||||
internal override KVPair Get(AutoarrKVPairPtr ar, uint index)
|
||||
{
|
||||
@ -24,16 +25,16 @@ internal class AutoarrKVPairFunctions : AutoarrFunctions<KVPair>
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport("kerep.dll",CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep",CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_KVPair_add(AutoarrKVPairPtr ar, KVPair element);
|
||||
internal override void Add(AutoarrKVPairPtr ar, KVPair element) => kerep_Autoarr_KVPair_add(ar, element);
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_KVPair_set(AutoarrKVPairPtr ar, uint index, KVPair element);
|
||||
internal override void Set(AutoarrKVPairPtr ar, uint index, KVPair element) =>
|
||||
kerep_Autoarr_KVPair_set(ar, index, element);
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_length(AutoarrKVPairPtr ar, out uint output);
|
||||
internal override uint Length(AutoarrKVPairPtr ar)
|
||||
{
|
||||
@ -41,7 +42,7 @@ internal class AutoarrKVPairFunctions : AutoarrFunctions<KVPair>
|
||||
return l;
|
||||
}
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_max_length(AutoarrKVPairPtr ar, out uint output);
|
||||
internal override uint MaxLength(AutoarrKVPairPtr ar)
|
||||
{
|
||||
|
||||
@ -1,10 +1,11 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using DTLib.Dtsod.V24.KerepTypes;
|
||||
|
||||
namespace DTLib.Dtsod.V24.Autoarr;
|
||||
|
||||
internal class AutoarrUnitypeFunctions : AutoarrFunctions<Unitype>
|
||||
{
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_create(ushort max_blocks_count, ushort max_block_length, out AutoarrUnitypePtr output);
|
||||
internal override AutoarrUnitypePtr Create(ushort maxBlocksCount, ushort maxBlockLength)
|
||||
{
|
||||
@ -12,11 +13,11 @@ internal class AutoarrUnitypeFunctions : AutoarrFunctions<Unitype>
|
||||
return ar;
|
||||
}
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_Unitype_free(AutoarrUnitypePtr ar);
|
||||
internal override void Free(AutoarrUnitypePtr ar) => kerep_Autoarr_Unitype_free(ar);
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_get(AutoarrUnitypePtr ar, uint index, out Unitype output);
|
||||
internal override Unitype Get(AutoarrUnitypePtr ar, uint index)
|
||||
{
|
||||
@ -24,16 +25,16 @@ internal class AutoarrUnitypeFunctions : AutoarrFunctions<Unitype>
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport("kerep.dll",CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep",CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_Unitype_add(AutoarrUnitypePtr ar, Unitype element);
|
||||
internal override void Add(AutoarrUnitypePtr ar, Unitype element) => kerep_Autoarr_Unitype_add(ar, element);
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_Unitype_set(AutoarrUnitypePtr ar, uint index, Unitype element);
|
||||
internal override void Set(AutoarrUnitypePtr ar, uint index, Unitype element) =>
|
||||
kerep_Autoarr_Unitype_set(ar, index, element);
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_length(AutoarrUnitypePtr ar, out uint output);
|
||||
internal override uint Length(AutoarrUnitypePtr ar)
|
||||
{
|
||||
@ -41,7 +42,7 @@ internal class AutoarrUnitypeFunctions : AutoarrFunctions<Unitype>
|
||||
return l;
|
||||
}
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_max_length(AutoarrUnitypePtr ar, out uint output);
|
||||
internal override uint MaxLength(AutoarrUnitypePtr ar)
|
||||
{
|
||||
|
||||
20
DTLib.Dtsod/V24/DependencyResolver.cs
Normal file
20
DTLib.Dtsod/V24/DependencyResolver.cs
Normal file
@ -0,0 +1,20 @@
|
||||
using DTLib.Filesystem;
|
||||
|
||||
namespace DTLib.Dtsod.V24;
|
||||
|
||||
public static class DependencyResolver
|
||||
{
|
||||
private static bool KerepCopied=false;
|
||||
|
||||
public static void CopyLibs()
|
||||
{
|
||||
if(KerepCopied) return;
|
||||
|
||||
string kereplib = Environment.OSVersion.Platform == PlatformID.Win32NT
|
||||
? "kerep.dll"
|
||||
: "kerep.so";
|
||||
File.Copy($"Dependencies{Путь.Разд}{kereplib}",kereplib, true);
|
||||
KerepCopied = true;
|
||||
Log("g",$"{kereplib} copied");
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using DTLib.Dtsod.V24.Autoarr;
|
||||
using DTLib.Dtsod.V24.KerepTypes;
|
||||
using Funcs=DTLib.Dtsod.V24.DtsodV24Functions;
|
||||
|
||||
namespace DTLib.Dtsod.V24;
|
||||
@ -6,39 +7,41 @@ namespace DTLib.Dtsod.V24;
|
||||
public class DtsodV24 : IDtsod, IEnumerable<Autoarr<KVPair>>, IDisposable
|
||||
{
|
||||
public DtsodVersion Version => DtsodVersion.V24;
|
||||
private readonly DtsodPtr DtsodHandler;
|
||||
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) => DtsodHandler = ptr;
|
||||
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() => DtsodHandler = Funcs.Deserialize(" ");
|
||||
|
||||
public DtsodV24(string text) => DtsodHandler = Funcs.Deserialize(text);
|
||||
|
||||
public DtsodV24(IDictionary dict) : this()
|
||||
public DtsodV24(IDictionary<string,dynamic> dict, bool autoDispose=true) : this(autoDispose)
|
||||
{
|
||||
foreach (KeyValuePair<string, dynamic> pair in dict)
|
||||
AddOrSet(pair.Key, pair.Value);
|
||||
{
|
||||
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();
|
||||
|
||||
return dict;
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
|
||||
public bool TryGet(string key, out dynamic elem)
|
||||
{
|
||||
var g = Funcs.Get(DtsodHandler, key);
|
||||
var g = Funcs.Get(UnmanagedPtr, key);
|
||||
elem = g.ToDynamic();
|
||||
return g.type == my_type.Null;
|
||||
return g.TypeCode == KerepTypeCode.Null;
|
||||
}
|
||||
|
||||
public void AddOrSet(string key, dynamic value) =>
|
||||
Funcs.AddOrSet(DtsodHandler, key, new Unitype(value));
|
||||
Funcs.AddOrSet(UnmanagedPtr, key, new Unitype(value));
|
||||
|
||||
public dynamic this[string key]
|
||||
{
|
||||
@ -50,12 +53,14 @@ public class DtsodV24 : IDtsod, IEnumerable<Autoarr<KVPair>>, IDisposable
|
||||
set => AddOrSet(key, value);
|
||||
}
|
||||
|
||||
public bool TryRemove(string key) => Funcs.Remove(DtsodHandler,key);
|
||||
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(DtsodHandler, key);
|
||||
public bool ContainsKey(string key) => Funcs.Contains(UnmanagedPtr, key);
|
||||
|
||||
public void Dispose() => Funcs.Free(DtsodHandler);
|
||||
public override string ToString() => Funcs.Serialize(UnmanagedPtr);
|
||||
|
||||
public void Dispose() => Funcs.Free(UnmanagedPtr);
|
||||
|
||||
~DtsodV24()
|
||||
{
|
||||
@ -78,9 +83,10 @@ public class DtsodV24 : IDtsod, IEnumerable<Autoarr<KVPair>>, IDisposable
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
bool r = ++h < Funcs.Height(d.DtsodHandler);
|
||||
if(r) Current = new Autoarr<KVPair>(Funcs.GetRow(d.DtsodHandler,h), false);
|
||||
return r;
|
||||
if(h >= Funcs.Height(d.UnmanagedPtr)) return false;
|
||||
Current = new Autoarr<KVPair>(Funcs.GetRow(d.UnmanagedPtr,h), false);
|
||||
h++;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset() => h = 0;
|
||||
|
||||
@ -1,48 +1,49 @@
|
||||
global using DtsodPtr=System.IntPtr;
|
||||
global using AutoarrKVPairPtr=System.IntPtr;
|
||||
global using AutoarrUnitypePtr=System.IntPtr;
|
||||
global using CharPtr=System.IntPtr;
|
||||
using System.Runtime.InteropServices;
|
||||
using DTLib.Dtsod.V24.Autoarr;
|
||||
using DTLib.Dtsod.V24.KerepTypes;
|
||||
|
||||
namespace DTLib.Dtsod.V24;
|
||||
|
||||
public static unsafe class DtsodV24Functions
|
||||
internal static class DtsodV24Functions
|
||||
{
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void pinvoke_print(string msg);
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void test_marshalling(string text, out KVPair* k);
|
||||
public static void TestMarshalling()
|
||||
static DtsodV24Functions()
|
||||
{
|
||||
pinvoke_print("UwU");
|
||||
string msg = "hello!";
|
||||
test_marshalling(msg, out KVPair* kptr);
|
||||
Log("kptr: " + kptr->ToString());
|
||||
KVPair k = *kptr;
|
||||
Log("y",
|
||||
$"{{{Marshal.PtrToStringAnsi(k.key)}, {{{k.value.type.ToString()}, {Marshal.PtrToStringAnsi(k.value.VoidPtr)} }} }}");
|
||||
DependencyResolver.CopyLibs();
|
||||
}
|
||||
|
||||
static void TryThrowErrmsg(CharPtr err)
|
||||
{
|
||||
if (err == IntPtr.Zero) return;
|
||||
string errmsg = Marshal.PtrToStringUTF8(err);
|
||||
Marshal.FreeHGlobal(err);
|
||||
throw new Exception(errmsg);
|
||||
}
|
||||
|
||||
|
||||
//parses text to binary values
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_deserialize(string text, out DtsodPtr output);
|
||||
[DllImport("kerep", 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);
|
||||
kerep_DtsodV24_deserialize(text, out var dtsod,out var err);
|
||||
TryThrowErrmsg(err);
|
||||
return dtsod;
|
||||
}
|
||||
|
||||
//creates text representation of dtsod
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_DtsodV24_serialize(DtsodPtr dtsod, out string output);
|
||||
[DllImport("kerep", 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);
|
||||
return text;
|
||||
kerep_DtsodV24_serialize(dtsod, out var text, out var err);
|
||||
TryThrowErrmsg(err);
|
||||
return Marshal.PtrToStringUTF8(text);
|
||||
}
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", 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)
|
||||
@ -51,11 +52,17 @@ public static unsafe class DtsodV24Functions
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport("kerep.dll",EntryPoint = "kerep_DtsodV24_addOrSet",CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep",EntryPoint = "kerep_DtsodV24_addOrSet",CallingConvention = CallingConvention.Cdecl)]
|
||||
//adds or sets value
|
||||
internal static extern void AddOrSet(DtsodPtr dtsod, string key, Unitype value);
|
||||
static extern void kerep_DtsodV24_addOrSet(DtsodPtr dtsod, IntPtr key, Unitype value);
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static void AddOrSet(DtsodPtr dtsod, string key, Unitype value)
|
||||
{
|
||||
IntPtr keyptr = key.ToHGlobalUTF8();
|
||||
kerep_DtsodV24_addOrSet(dtsod, keyptr, value);
|
||||
}
|
||||
|
||||
[DllImport("kerep", 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)
|
||||
@ -64,7 +71,7 @@ public static unsafe class DtsodV24Functions
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", 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)
|
||||
@ -73,11 +80,11 @@ public static unsafe class DtsodV24Functions
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport("kerep.dll",EntryPoint="kerep_DtsodV24_free", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep",EntryPoint="kerep_DtsodV24_free", CallingConvention = CallingConvention.Cdecl)]
|
||||
//replaces value with UniNull if key exists in dtsod
|
||||
internal static extern void Free(DtsodPtr dtsod);
|
||||
|
||||
[DllImport("kerep.dll",EntryPoint="kerep_DtsodV24_free", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", 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)
|
||||
@ -86,7 +93,7 @@ public static unsafe class DtsodV24Functions
|
||||
return h;
|
||||
}
|
||||
|
||||
[DllImport("kerep.dll",EntryPoint="kerep_DtsodV24_free", CallingConvention = CallingConvention.Cdecl)]
|
||||
[DllImport("kerep", 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.
|
||||
|
||||
@ -1,59 +0,0 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DTLib.Dtsod.V24;
|
||||
|
||||
internal enum my_type : byte
|
||||
{
|
||||
Null, Float32, Float64, Char, Bool,
|
||||
UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64,
|
||||
UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr,
|
||||
CharPtr, STNodePtr, HashtablePtr,
|
||||
UniversalType,
|
||||
AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr,
|
||||
AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr,
|
||||
AutoarrUnitypePtr, AutoarrKVPairPtr
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
internal struct Unitype
|
||||
{
|
||||
[FieldOffset(0)] internal long Int64;
|
||||
[FieldOffset(0)] internal ulong UInt64;
|
||||
[FieldOffset(0)] internal double Float64;
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
[FieldOffset(0)] internal char Char;
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
[FieldOffset(0)] internal bool Bool;
|
||||
[FieldOffset(0)] internal IntPtr VoidPtr;
|
||||
[FieldOffset(8)] internal my_type type;
|
||||
|
||||
public Unitype(dynamic something)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public dynamic ToDynamic()
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case my_type.Null: return null;
|
||||
case my_type.Bool: return Bool;
|
||||
case my_type.Char: return Char;
|
||||
case my_type.Int64: return Int64;
|
||||
case my_type.UInt64: return UInt64;
|
||||
case my_type.Float64: return Float64;
|
||||
case my_type.CharPtr: return Marshal.PtrToStringAuto(VoidPtr);
|
||||
case my_type.AutoarrUnitypePtr: return new Autoarr.Autoarr<Unitype>(VoidPtr);
|
||||
case my_type.AutoarrKVPairPtr: return new Autoarr.Autoarr<KVPair>(VoidPtr);
|
||||
case my_type.HashtablePtr: return new DtsodV24(VoidPtr);
|
||||
default: throw new Exception($"can't unbox value of type {type}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct KVPair
|
||||
{
|
||||
internal IntPtr key;
|
||||
internal Unitype value;
|
||||
}
|
||||
26
DTLib.Dtsod/V24/KerepTypes/KVPair.cs
Normal file
26
DTLib.Dtsod/V24/KerepTypes/KVPair.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DTLib.Dtsod.V24.KerepTypes;
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public struct KVPair
|
||||
{
|
||||
public IntPtr key;
|
||||
public Unitype value;
|
||||
|
||||
public KVPair(IntPtr k, Unitype v)
|
||||
{
|
||||
key = k;
|
||||
value = v;
|
||||
}
|
||||
public KVPair(string k, Unitype v)
|
||||
{
|
||||
key = k.ToHGlobalUTF8();
|
||||
value = v;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{{{Marshal.PtrToStringUTF8(key)}, {value}}}";
|
||||
}
|
||||
}
|
||||
46
DTLib.Dtsod/V24/KerepTypes/KerepTypeCode.cs
Normal file
46
DTLib.Dtsod/V24/KerepTypes/KerepTypeCode.cs
Normal file
@ -0,0 +1,46 @@
|
||||
namespace DTLib.Dtsod.V24.KerepTypes;
|
||||
|
||||
public enum KerepTypeCode : byte
|
||||
{
|
||||
Null, Float32, Float64, Char, Bool,
|
||||
UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64,
|
||||
UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr,
|
||||
CharPtr, STNodePtr, HashtablePtr,
|
||||
UniversalType,
|
||||
AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr,
|
||||
AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr,
|
||||
AutoarrUnitypePtr, AutoarrKVPairPtr
|
||||
}
|
||||
|
||||
public static class KerepTypeHelper
|
||||
{
|
||||
|
||||
static readonly Dictionary<Type, KerepTypeCode> type_comparsion_dict = new()
|
||||
{
|
||||
{typeof(bool), KerepTypeCode.Bool},
|
||||
{typeof(byte), KerepTypeCode.UInt8},
|
||||
{typeof(ushort), KerepTypeCode.UInt16},
|
||||
{typeof(uint), KerepTypeCode.UInt32},
|
||||
{typeof(ulong), KerepTypeCode.UInt64},
|
||||
{typeof(sbyte), KerepTypeCode.Int8},
|
||||
{typeof(short), KerepTypeCode.Int16},
|
||||
{typeof(int), KerepTypeCode.Int32},
|
||||
{typeof(long), KerepTypeCode.Int64},
|
||||
{typeof(float), KerepTypeCode.Float32},
|
||||
{typeof(double), KerepTypeCode.Float64},
|
||||
{typeof(string), KerepTypeCode.CharPtr}
|
||||
};
|
||||
|
||||
public static KerepTypeCode GetKerepTypeCode(object something)
|
||||
{
|
||||
if (type_comparsion_dict.TryGetValue(something.GetType(), out var ktype))
|
||||
return ktype;
|
||||
|
||||
else return something switch
|
||||
{
|
||||
IList<object> => KerepTypeCode.AutoarrUnitypePtr,
|
||||
IDictionary<string, object> => KerepTypeCode.HashtablePtr,
|
||||
_ => throw new Exception($"can't get KerepTypeCode for type {something.GetType()}")
|
||||
};
|
||||
}
|
||||
}
|
||||
144
DTLib.Dtsod/V24/KerepTypes/Unitype.cs
Normal file
144
DTLib.Dtsod/V24/KerepTypes/Unitype.cs
Normal file
@ -0,0 +1,144 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using DTLib.Dtsod.V24.Autoarr;
|
||||
|
||||
namespace DTLib.Dtsod.V24.KerepTypes;
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public struct Unitype
|
||||
{
|
||||
[FieldOffset(0)] public long Int64;
|
||||
[FieldOffset(0)] public ulong UInt64;
|
||||
[FieldOffset(0)] public double Float64;
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
[FieldOffset(0)] public bool Bool;
|
||||
[FieldOffset(0)] public IntPtr VoidPtr;
|
||||
[FieldOffset(8)] public KerepTypeCode TypeCode;
|
||||
|
||||
public Unitype(object v) : this()
|
||||
{
|
||||
TypeCode = KerepTypeHelper.GetKerepTypeCode(v);
|
||||
switch (TypeCode)
|
||||
{
|
||||
case KerepTypeCode.Bool:
|
||||
Bool = (bool) v;
|
||||
break;
|
||||
case KerepTypeCode.UInt8:
|
||||
case KerepTypeCode.UInt16:
|
||||
case KerepTypeCode.UInt32:
|
||||
UInt64 = v.ToULong();
|
||||
TypeCode = KerepTypeCode.UInt64;
|
||||
break;
|
||||
case KerepTypeCode.UInt64:
|
||||
UInt64 = (ulong) v;
|
||||
break;
|
||||
case KerepTypeCode.Int8:
|
||||
case KerepTypeCode.Int16:
|
||||
case KerepTypeCode.Int32:
|
||||
Int64 = v.ToLong();
|
||||
TypeCode = KerepTypeCode.Int64;
|
||||
break;
|
||||
case KerepTypeCode.Int64:
|
||||
Int64 = (long) v;
|
||||
break;
|
||||
case KerepTypeCode.Float32:
|
||||
Float64 = v.ToDouble();
|
||||
TypeCode = KerepTypeCode.Float64;
|
||||
break;
|
||||
case KerepTypeCode.Float64:
|
||||
Float64 = (double) v;
|
||||
break;
|
||||
case KerepTypeCode.CharPtr:
|
||||
VoidPtr = ((string)v).ToHGlobalUTF8();
|
||||
break;
|
||||
case KerepTypeCode.AutoarrUnitypePtr:
|
||||
TypeCode = KerepTypeCode.AutoarrUnitypePtr;
|
||||
var ar = new Autoarr<Unitype>(64,1024,false);
|
||||
foreach (var sub in (List<object>)v)
|
||||
ar.Add(new Unitype(sub));
|
||||
VoidPtr = ar.UnmanagedPtr;
|
||||
break;
|
||||
case KerepTypeCode.HashtablePtr:
|
||||
TypeCode = KerepTypeCode.HashtablePtr;
|
||||
var ht = new DtsodV24((IDictionary<string,object>)v,false);
|
||||
VoidPtr = ht.UnmanagedPtr;
|
||||
break;
|
||||
default: throw new Exception($"can't box value of type {TypeCode}");
|
||||
}
|
||||
}
|
||||
|
||||
/*public Unitype(bool v) : this()
|
||||
{
|
||||
TypeCode = KerepTypeCode.Bool;
|
||||
Bool = v;
|
||||
}
|
||||
public Unitype(int v) : this()
|
||||
{
|
||||
TypeCode = KerepTypeCode.Int64;
|
||||
Int64 = v;
|
||||
}
|
||||
public Unitype(uint v) : this()
|
||||
{
|
||||
TypeCode = KerepTypeCode.UInt64;
|
||||
UInt64 = v;
|
||||
}
|
||||
public Unitype(double v) : this()
|
||||
{
|
||||
TypeCode = KerepTypeCode.Float64;
|
||||
Float64 = v;
|
||||
}
|
||||
public Unitype(string s) : this()
|
||||
{
|
||||
TypeCode = KerepTypeCode.CharPtr;
|
||||
VoidPtr = s.ToHGlobalUTF8();
|
||||
}
|
||||
|
||||
public Unitype(Autoarr<Unitype> v) : this()
|
||||
{
|
||||
TypeCode = KerepTypeCode.AutoarrUnitypePtr;
|
||||
VoidPtr = v.UnmanagedPtr;
|
||||
}
|
||||
public Unitype(Autoarr<KVPair> v) : this()
|
||||
{
|
||||
TypeCode = KerepTypeCode.AutoarrKVPairPtr;
|
||||
VoidPtr = v.UnmanagedPtr;
|
||||
}
|
||||
public Unitype(DtsodV24 v) : this()
|
||||
{
|
||||
TypeCode = KerepTypeCode.HashtablePtr;
|
||||
VoidPtr = v.UnmanagedPtr;
|
||||
}*/
|
||||
|
||||
public dynamic ToDynamic()
|
||||
{
|
||||
switch (TypeCode)
|
||||
{
|
||||
case KerepTypeCode.Null: return null;
|
||||
case KerepTypeCode.Bool: return Bool;
|
||||
case KerepTypeCode.Int64: return Int64;
|
||||
case KerepTypeCode.UInt64: return UInt64;
|
||||
case KerepTypeCode.Float64: return Float64;
|
||||
case KerepTypeCode.CharPtr: return Marshal.PtrToStringUTF8(VoidPtr);
|
||||
case KerepTypeCode.AutoarrUnitypePtr: return new Autoarr<Unitype>(VoidPtr, false);
|
||||
case KerepTypeCode.AutoarrKVPairPtr: return new Autoarr<KVPair>(VoidPtr, false);
|
||||
case KerepTypeCode.HashtablePtr: return new DtsodV24(VoidPtr);
|
||||
default: throw new Exception($"can't unbox value of type {TypeCode}");
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
switch (TypeCode)
|
||||
{
|
||||
case KerepTypeCode.Null: return "{Null}";
|
||||
case KerepTypeCode.Bool: return $"{{Bool:{Bool}}}";
|
||||
case KerepTypeCode.Int64: return $"{{Int64:{Int64}}}";
|
||||
case KerepTypeCode.UInt64: return $"{{UInt64:{UInt64}}}";
|
||||
case KerepTypeCode.Float64: return $"{{Float64:{Float64}}}";
|
||||
case KerepTypeCode.CharPtr: return $"{{CharPtr:{Marshal.PtrToStringUTF8(VoidPtr)}}}";
|
||||
case KerepTypeCode.AutoarrUnitypePtr: return $"{{AutoarrUnitypePtr:{VoidPtr.ToString()}}}";
|
||||
case KerepTypeCode.AutoarrKVPairPtr: return $"{{AutoarrKVPairPtr:{VoidPtr.ToString()}}}";
|
||||
case KerepTypeCode.HashtablePtr: return $"{{HashtablePtr:{VoidPtr.ToString()}}}";
|
||||
default: throw new Exception($"can't unbox value of type {TypeCode}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net6.0;net48</TargetFrameworks>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<LangVersion>10</LangVersion>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFrameworks>net6.0;net48</TargetFrameworks>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<LangVersion>10</LangVersion>
|
||||
<ImplicitUsings>false</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
@ -28,11 +28,23 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="DtsodV2X\*">
|
||||
<None Include="DtsodV23\*">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Include="DtsodV30\*">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="DtsodV24\base_types.dtsod">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="DtsodV24\complexes.dtsod">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="DtsodV24\lists.dtsod">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
<None Update="DtsodV24\messages.dtsod">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@ -1,22 +0,0 @@
|
||||
namespace DTLib.Tests;
|
||||
|
||||
static class DictTest
|
||||
{
|
||||
static void Fill(Dictionary<string,long> dict){
|
||||
for(long i=0;i<100000;i++)
|
||||
dict.Add($"key__{i}",i);
|
||||
}
|
||||
static long Gett(Dictionary<string,long> dict){
|
||||
long r=0;
|
||||
for(long i=0;i<100000;i++)
|
||||
r=dict[$"key__{i}"];
|
||||
return r;
|
||||
}
|
||||
|
||||
public static void Test(){
|
||||
Info.Log("c","--------------[DictTest]---------------");
|
||||
Dictionary<string,long> dict=new();
|
||||
LogOperationTime("fill",1,()=>Fill(dict));
|
||||
LogOperationTime("gett",1,()=>Gett(dict));
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,6 @@
|
||||
namespace DTLib.Tests;
|
||||
using System.Linq;
|
||||
|
||||
namespace DTLib.Tests;
|
||||
|
||||
public static class TestDtsodV23
|
||||
{
|
||||
@ -14,16 +16,16 @@ public static class TestDtsodV23
|
||||
|
||||
public static void TestBaseTypes()
|
||||
{
|
||||
Info.Log("c", "-----[TestDtsodV23/TestBaseTypes]------");
|
||||
DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Путь.Разд}base_types.dtsod"));
|
||||
Info.Log("c", "-----[TestDtsodV23/TestBaseTypes]-----");
|
||||
DtsodV23 dtsod = new(File.ReadAllText($"DtsodV23{Путь.Разд}base_types.dtsod"));
|
||||
foreach (var pair in dtsod)
|
||||
Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString());
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
public static void TestLists()
|
||||
{
|
||||
Info.Log("c", "-------[TestDtsodV23/TestLists]--------");
|
||||
DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Путь.Разд}lists.dtsod"));
|
||||
Info.Log("c", "-------[TestDtsodV23/TestLists]-------");
|
||||
DtsodV23 dtsod = new(File.ReadAllText($"DtsodV23{Путь.Разд}lists.dtsod"));
|
||||
foreach (var pair in dtsod)
|
||||
{
|
||||
Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c",
|
||||
@ -36,8 +38,8 @@ public static class TestDtsodV23
|
||||
|
||||
public static void TestComplexes()
|
||||
{
|
||||
Info.Log("c", "-----[TestDtsodV23/TestComplexes]------");
|
||||
DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Путь.Разд}complexes.dtsod"));
|
||||
Info.Log("c", "-----[TestDtsodV23/TestComplexes]-----");
|
||||
DtsodV23 dtsod = new(File.ReadAllText($"DtsodV23{Путь.Разд}complexes.dtsod"));
|
||||
foreach (var pair in dtsod)
|
||||
{
|
||||
Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key,
|
||||
@ -49,31 +51,31 @@ public static class TestDtsodV23
|
||||
|
||||
public static void TestReSerialization()
|
||||
{
|
||||
Info.Log("c", "---[TestDtsodV23/TestReSerialization]--");
|
||||
Info.Log("c", "--[TestDtsodV23/TestReSerialization]--");
|
||||
var dtsod = new DtsodV23(new DtsodV23(new DtsodV23(
|
||||
new DtsodV23(File.ReadAllText($"DtsodV2X{Путь.Разд}complexes.dtsod")).ToString()).ToString()).ToString());
|
||||
new DtsodV23(File.ReadAllText($"DtsodV23{Путь.Разд}complexes.dtsod")).ToString()).ToString()).ToString());
|
||||
Info.Log("y", dtsod.ToString());
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
|
||||
public static void TestSpeed()
|
||||
{
|
||||
Info.Log("c", "--------[TestDtsodV23/TestSpeed]-------");
|
||||
Info.Log("c", "-------[TestDtsodV23/TestSpeed]-------");
|
||||
IDtsod dtsod=null;
|
||||
string text = File.ReadAllText($"DtsodV2X{Путь.Разд}messages.dtsod");
|
||||
LogOperationTime("V21 deserialization",100,()=>dtsod=new DtsodV21(text));
|
||||
LogOperationTime("V21 serialization", 100, () => _=dtsod.ToString());
|
||||
LogOperationTime("V23 deserialization", 100, () => dtsod = new DtsodV23(text));
|
||||
LogOperationTime("V23 serialization", 100, () => _ = dtsod.ToString());
|
||||
string text = File.ReadAllText($"DtsodV23{Путь.Разд}messages.dtsod");
|
||||
LogOperationTime("V21 deserialization",64,()=>dtsod=new DtsodV21(text));
|
||||
LogOperationTime("V21 serialization", 64, () => _=dtsod.ToString());
|
||||
LogOperationTime("V23 deserialization", 64, () => dtsod = new DtsodV23(text));
|
||||
LogOperationTime("V23 serialization", 64, () => _ = dtsod.ToString());
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
|
||||
public static void TestMemoryConsumption()
|
||||
{
|
||||
Info.Log("c", "-----[TestDtsodV23/TestMemConsumpt]----");
|
||||
string text = File.ReadAllText($"DtsodV2X{Путь.Разд}messages.dtsod");
|
||||
Info.Log("c", "----[TestDtsodV23/TestMemConsumpt]----");
|
||||
string text = File.ReadAllText($"DtsodV23{Путь.Разд}messages.dtsod");
|
||||
var a = GC.GetTotalMemory(true);
|
||||
var dtsods = new DtsodV23[100];
|
||||
var dtsods = new DtsodV23[64];
|
||||
for (int i = 0; i < dtsods.Length; i++)
|
||||
dtsods[i] = new(text);
|
||||
var b = GC.GetTotalMemory(true);
|
||||
@ -11,4 +11,4 @@ ulong: 87659057946ul;
|
||||
float: 39.944f;
|
||||
double: 965.557;
|
||||
decimal: -84.20de;
|
||||
string: "_$\"\\\\'''\n\ta ûûû000;2;=:%d;```";
|
||||
string: "_$\"\\\\'''\n\ta ыыы000;2;=:%d;```";
|
||||
@ -2,3 +2,4 @@ chars: ['a','b','c'];
|
||||
uints: [10,20,30,0,0];
|
||||
floats: [8.2,5.225,-0.9993];
|
||||
strings:["aaa","bbb","ccc"];
|
||||
things:["aaa",'b',-122];
|
||||
15874
DTLib.Tests/DtsodV23/messages.dtsod
Normal file
15874
DTLib.Tests/DtsodV23/messages.dtsod
Normal file
File diff suppressed because it is too large
Load Diff
38
DTLib.Tests/DtsodV24/TestAutoarr.cs
Normal file
38
DTLib.Tests/DtsodV24/TestAutoarr.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using DTLib.Dtsod.V24.Autoarr;
|
||||
using DTLib.Dtsod.V24.KerepTypes;
|
||||
|
||||
namespace DTLib.Tests;
|
||||
|
||||
public static class TestAutoarr
|
||||
{
|
||||
public static void TestAll()
|
||||
{
|
||||
var ar = new Autoarr<KVPair>(4, 4, false);
|
||||
Fill(ar);
|
||||
Print(ar);
|
||||
Free(ar);
|
||||
}
|
||||
|
||||
public static void Fill(Autoarr<KVPair> ar)
|
||||
{
|
||||
Info.Log("c", "----------[TestAutoarr/Fill]----------");
|
||||
for(uint i=0;i<ar.MaxLength;i++)
|
||||
ar.Add(new KVPair($"key_{i}",new Unitype(i)));
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
|
||||
public static void Print(Autoarr<KVPair> ar)
|
||||
{
|
||||
Info.Log("c", "----------[TestAutoarr/Print]---------");
|
||||
foreach (KVPair pair in ar)
|
||||
Info.Log("h", pair.ToString());
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
|
||||
public static void Free(Autoarr<KVPair> ar)
|
||||
{
|
||||
Info.Log("c", "----------[TestAutoarr/Free]----------");
|
||||
ar.Dispose();
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
}
|
||||
75
DTLib.Tests/DtsodV24/TestDtsodV24.cs
Normal file
75
DTLib.Tests/DtsodV24/TestDtsodV24.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using DTLib.Dtsod.V24;
|
||||
using DTLib.Dtsod.V24.Autoarr;
|
||||
using DTLib.Dtsod.V24.KerepTypes;
|
||||
|
||||
namespace DTLib.Tests;
|
||||
|
||||
public static class TestDtsodV24
|
||||
{
|
||||
public static void TestAll()
|
||||
{
|
||||
TestBaseTypes();
|
||||
TestComplexes();
|
||||
TestLists();
|
||||
TestReSerialization();
|
||||
TestSpeed();
|
||||
}
|
||||
|
||||
public static void TestBaseTypes()
|
||||
{
|
||||
Info.Log("c", "-----[TestDtsodV24/TestBaseTypes]-----");
|
||||
DtsodV24 dtsod = new(File.ReadAllText($"DtsodV24{Путь.Разд}base_types.dtsod"));
|
||||
foreach (var autoarr in dtsod)
|
||||
{
|
||||
foreach (KVPair pair in autoarr)
|
||||
Info.LogNoTime("b", pair.ToString());
|
||||
}
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
|
||||
public static void TestComplexes()
|
||||
{
|
||||
Info.Log("c", "-----[TestDtsodV24/TestComplexes]-----");
|
||||
DtsodV24 dtsod = new(File.ReadAllText($"DtsodV24{Путь.Разд}complexes.dtsod"));
|
||||
Info.Log("h", dtsod.ToString());
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
|
||||
public static void TestLists()
|
||||
{
|
||||
Info.Log("c", "-------[TestDtsodV24/TestLists]-------");
|
||||
DtsodV24 dtsod = new(File.ReadAllText($"DtsodV24{Путь.Разд}lists.dtsod"));
|
||||
foreach (var autoarr in dtsod)
|
||||
foreach (KVPair pair in autoarr)
|
||||
{
|
||||
var list = new Autoarr<Unitype>(pair.value.VoidPtr, false);
|
||||
Info.LogNoTime("b", pair.key.ToStringUTF8(), "w", $" length: {list.Length}");
|
||||
foreach (var el in list)
|
||||
Info.LogNoTime("h", '\t' + el.ToString());
|
||||
}
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
|
||||
public static void TestReSerialization()
|
||||
{
|
||||
Info.Log("c", "--[TestDtsodV24/TestReSerialization]--");
|
||||
var dtsod = new DtsodV24(new DtsodV24(new DtsodV24(
|
||||
new DtsodV24(File.ReadAllText($"DtsodV24{Путь.Разд}complexes.dtsod")).ToString()).ToString()).ToString());
|
||||
Info.Log("h", dtsod.ToString());
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
|
||||
public static void TestSpeed()
|
||||
{
|
||||
Info.Log("c", "-------[TestDtsodV24/TestSpeed]-------");
|
||||
IDtsod dtsod=null;
|
||||
string _text = File.ReadAllText($"DtsodV23{Путь.Разд}messages.dtsod");
|
||||
string text = "";
|
||||
LogOperationTime( "V23 to V24 conversion", 32, ()=>
|
||||
text = DtsodConverter.ConvertVersion(new DtsodV23(_text), DtsodVersion.V24).ToString()
|
||||
);
|
||||
LogOperationTime("V24 deserialization", 64, () => dtsod = new DtsodV24(text));
|
||||
LogOperationTime("V24 serialization", 64, () => text = dtsod.ToString());
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
}
|
||||
39
DTLib.Tests/DtsodV24/TestPInvoke.cs
Normal file
39
DTLib.Tests/DtsodV24/TestPInvoke.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using DTLib.Dtsod.V24;
|
||||
using DTLib.Dtsod.V24.KerepTypes;
|
||||
|
||||
namespace DTLib.Tests;
|
||||
|
||||
public static class TestPInvoke
|
||||
{
|
||||
public static void TestAll()
|
||||
{
|
||||
DependencyResolver.CopyLibs();
|
||||
TestPrintf();
|
||||
TestMarshalling();
|
||||
}
|
||||
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void pinvoke_print([MarshalAs(UnmanagedType.LPUTF8Str)] string msg);
|
||||
|
||||
public static void TestPrintf()
|
||||
{
|
||||
Info.Log("c", "---------[TestPInvoke/Printf]---------");
|
||||
pinvoke_print("ъъ~ 中文");
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern unsafe void test_marshalling([MarshalAs(UnmanagedType.LPUTF8Str)] string text, out KVPair* k);
|
||||
|
||||
public static unsafe void TestMarshalling()
|
||||
{
|
||||
Info.Log("c", "---------[TestAutoarr/Print]----------");
|
||||
string msg = "ъъ~ 中文";
|
||||
test_marshalling(msg, out KVPair* kptr);
|
||||
KVPair k = *kptr;
|
||||
Info.Log("b", k.ToString());
|
||||
Info.Log("g", "test completed");
|
||||
}
|
||||
}
|
||||
7
DTLib.Tests/DtsodV24/base_types.dtsod
Normal file
7
DTLib.Tests/DtsodV24/base_types.dtsod
Normal file
@ -0,0 +1,7 @@
|
||||
bool: false ;
|
||||
int: -2515;
|
||||
uint: 0u;
|
||||
float: 39.944f;
|
||||
double: 965.557f;
|
||||
string: "_$\"\\\\'''\n\ta ыыы000;2;=:%d;```";
|
||||
|
||||
9
DTLib.Tests/DtsodV24/complexes.dtsod
Normal file
9
DTLib.Tests/DtsodV24/complexes.dtsod
Normal file
@ -0,0 +1,9 @@
|
||||
message:
|
||||
{
|
||||
type: "sent";
|
||||
time: "15.12.2021 20:51:24 +03:00";
|
||||
author_id: 293798876950036480;
|
||||
channel_id: 913088838761603212;
|
||||
message_id: 920734809096077353;
|
||||
text: "_$\"\\\\'''\n\ta ыыы000;2;=:%d;```";
|
||||
};
|
||||
15
DTLib.Tests/DtsodV24/lists.dtsod
Normal file
15
DTLib.Tests/DtsodV24/lists.dtsod
Normal file
@ -0,0 +1,15 @@
|
||||
uints: [10,20,30,0,0 ];
|
||||
floats: [8.2,5.225,-0.9993];
|
||||
strings: ["aaa","bbb","ccc"];
|
||||
things: ["aaa",true,-122];
|
||||
$complex: {
|
||||
a:141;
|
||||
b:00f;
|
||||
c:false;
|
||||
};
|
||||
$complex: {
|
||||
d:-1u;
|
||||
e:005;
|
||||
f:-1f;
|
||||
};
|
||||
list_of_lists: [ ["sss"]];
|
||||
15877
DTLib.Tests/DtsodV24/messages.dtsod
Normal file
15877
DTLib.Tests/DtsodV24/messages.dtsod
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1 +0,0 @@
|
||||
nullable:null;
|
||||
@ -1,15 +1,14 @@
|
||||
global using System;
|
||||
global using System.Collections;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Linq;
|
||||
global using System.Text;
|
||||
global using System.Threading;
|
||||
global using System.Threading.Tasks;
|
||||
global using DTLib;
|
||||
global using DTLib.Extensions;
|
||||
global using static DTLib.Loggers.LogFunctions;
|
||||
global using DTLib.Filesystem;
|
||||
global using DTLib.Dtsod;
|
||||
global using static DTLib.Loggers.LogFunctions;
|
||||
global using static DTLib.Tests.Program;
|
||||
using DTLib.Loggers;
|
||||
|
||||
@ -29,14 +28,10 @@ public static class Program
|
||||
Console.Title="tester";
|
||||
try
|
||||
{
|
||||
Info.Log("c", "-------------[DTLib.Tests]-------------");
|
||||
//TestDtsodV23.TestAll();
|
||||
//DictTest.Test();
|
||||
for (uint i = 0; i < 10; i++)
|
||||
{
|
||||
Dtsod.V24.DtsodV24Functions.TestMarshalling();
|
||||
Info.Log($"{i}");
|
||||
}
|
||||
TestPInvoke.TestAll();
|
||||
TestAutoarr.TestAll();
|
||||
TestDtsodV23.TestAll();
|
||||
TestDtsodV24.TestAll();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{ Info.Log("r", ex.ToString()); }
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net6.0;net48</TargetFrameworks>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<LangVersion>preview</LangVersion>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
|
||||
19
DTLib/Extensions/Unmanaged.cs
Normal file
19
DTLib/Extensions/Unmanaged.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace DTLib.Extensions;
|
||||
|
||||
public static class Unmanaged
|
||||
{
|
||||
public static unsafe IntPtr ToHGlobalUTF8(this string s)
|
||||
{
|
||||
byte[] buf = s.ToBytes();
|
||||
int bl = buf.Length;
|
||||
byte* ptr=(byte*)Marshal.AllocHGlobal(bl + 1);
|
||||
for (int i = 0; i < bl; i++)
|
||||
ptr[i] = buf[i];
|
||||
ptr[bl] = (byte)'\0';
|
||||
return (IntPtr) ptr;
|
||||
}
|
||||
|
||||
public static string ToStringUTF8(this IntPtr p) => Marshal.PtrToStringUTF8(p);
|
||||
}
|
||||
@ -7,10 +7,10 @@ public static class Directory
|
||||
// создает папку, если её не существует
|
||||
public static void Create(string dir)
|
||||
{
|
||||
if (!Directory.Exists(dir))
|
||||
if (!Exists(dir))
|
||||
{
|
||||
// проверяет существование папки, в которой нужно создать dir
|
||||
if (dir.Contains(Путь.Разд) && !Directory.Exists(dir.Remove(dir.LastIndexOf(Путь.Разд))))
|
||||
if (dir.Contains(Путь.Разд) && !Exists(dir.Remove(dir.LastIndexOf(Путь.Разд))))
|
||||
Create(dir.Remove(dir.LastIndexOf(Путь.Разд)));
|
||||
System.IO.Directory.CreateDirectory(dir);
|
||||
}
|
||||
@ -54,12 +54,12 @@ public static class Directory
|
||||
File.Delete(files[i]);
|
||||
for (int i = subdirs.Count - 1; i >= 0; i--)
|
||||
{
|
||||
PublicLog.Log($"deleting {subdirs[i]}");
|
||||
if (Directory.Exists(subdirs[i]))
|
||||
Log($"deleting {subdirs[i]}");
|
||||
if (Exists(subdirs[i]))
|
||||
System.IO.Directory.Delete(subdirs[i], true);
|
||||
}
|
||||
PublicLog.Log($"deleting {dir}");
|
||||
if (Directory.Exists(dir))
|
||||
Log($"deleting {dir}");
|
||||
if (Exists(dir))
|
||||
System.IO.Directory.Delete(dir, true);
|
||||
}
|
||||
|
||||
@ -71,10 +71,10 @@ public static class Directory
|
||||
public static List<string> GetAllFiles(string dir)
|
||||
{
|
||||
var all_files = new List<string>();
|
||||
string[] cur_files = Directory.GetFiles(dir);
|
||||
string[] cur_files = GetFiles(dir);
|
||||
for (int i = 0; i < cur_files.Length; i++)
|
||||
all_files.Add(cur_files[i]);
|
||||
string[] cur_subdirs = Directory.GetDirectories(dir);
|
||||
string[] cur_subdirs = GetDirectories(dir);
|
||||
for (int i = 0; i < cur_subdirs.Length; i++)
|
||||
all_files.AddRange(GetAllFiles(cur_subdirs[i]));
|
||||
return all_files;
|
||||
@ -84,10 +84,10 @@ public static class Directory
|
||||
public static List<string> GetAllFiles(string dir, ref List<string> all_subdirs)
|
||||
{
|
||||
var all_files = new List<string>();
|
||||
string[] cur_files = Directory.GetFiles(dir);
|
||||
string[] cur_files = GetFiles(dir);
|
||||
for (int i = 0; i < cur_files.Length; i++)
|
||||
all_files.Add(cur_files[i]);
|
||||
string[] cur_subdirs = Directory.GetDirectories(dir);
|
||||
string[] cur_subdirs = GetDirectories(dir);
|
||||
for (int i = 0; i < cur_subdirs.Length; i++)
|
||||
{
|
||||
all_subdirs.Add(cur_subdirs[i]);
|
||||
@ -101,7 +101,7 @@ public static class Directory
|
||||
public static void CreateSymlink(string sourceName, string symlinkName)
|
||||
{
|
||||
if (symlinkName.Contains(Путь.Разд))
|
||||
Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд)));
|
||||
Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд)));
|
||||
if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory))
|
||||
throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})");
|
||||
}
|
||||
@ -109,7 +109,7 @@ public static class Directory
|
||||
// copies directory with symlinks instead of files
|
||||
public static int SymCopy(string srcdir, string newdir)
|
||||
{
|
||||
List<string> files = Directory.GetAllFiles(srcdir);
|
||||
List<string> files = GetAllFiles(srcdir);
|
||||
if (!srcdir.EndsWith(Путь.Разд)) srcdir += Путь.Разд;
|
||||
if (!newdir.EndsWith(Путь.Разд)) newdir += Путь.Разд;
|
||||
int i = 0;
|
||||
|
||||
@ -9,7 +9,7 @@ public static class File
|
||||
// если файл не существует, создаёт файл, создаёт папки из его пути
|
||||
public static void Create(string file)
|
||||
{
|
||||
if (!File.Exists(file))
|
||||
if (!Exists(file))
|
||||
{
|
||||
if (file.Contains(Путь.Разд))
|
||||
Directory.Create(file.Remove(file.LastIndexOf(Путь.Разд)));
|
||||
@ -30,7 +30,7 @@ public static class File
|
||||
|
||||
public static byte[] ReadAllBytes(string file)
|
||||
{
|
||||
using System.IO.FileStream stream = File.OpenRead(file);
|
||||
using System.IO.FileStream stream = OpenRead(file);
|
||||
int size = GetSize(file);
|
||||
byte[] output = new byte[size];
|
||||
stream.Read(output, 0, size);
|
||||
@ -42,7 +42,7 @@ public static class File
|
||||
|
||||
public static void WriteAllBytes(string file, byte[] content)
|
||||
{
|
||||
using System.IO.FileStream stream = File.OpenWrite(file);
|
||||
using System.IO.FileStream stream = OpenWrite(file);
|
||||
stream.Write(content, 0, content.Length);
|
||||
stream.Close();
|
||||
}
|
||||
@ -51,7 +51,7 @@ public static class File
|
||||
|
||||
public static void AppendAllBytes(string file, byte[] content)
|
||||
{
|
||||
using System.IO.FileStream stream = File.OpenAppend(file);
|
||||
using System.IO.FileStream stream = OpenAppend(file);
|
||||
stream.Write(content, 0, content.Length);
|
||||
stream.Close();
|
||||
}
|
||||
@ -62,14 +62,14 @@ public static class File
|
||||
Exists(file) ? System.IO.File.OpenRead(file) : throw new Exception($"file not found: <{file}>");
|
||||
public static System.IO.FileStream OpenWrite(string file)
|
||||
{
|
||||
if (File.Exists(file))
|
||||
File.Delete(file);
|
||||
File.Create(file);
|
||||
if (Exists(file))
|
||||
Delete(file);
|
||||
Create(file);
|
||||
return System.IO.File.Open(file, System.IO.FileMode.OpenOrCreate);
|
||||
}
|
||||
public static System.IO.FileStream OpenAppend(string file)
|
||||
{
|
||||
File.Create(file);
|
||||
Create(file);
|
||||
return System.IO.File.Open(file, System.IO.FileMode.Append);
|
||||
}
|
||||
|
||||
|
||||
1
kerep
1
kerep
@ -1 +0,0 @@
|
||||
Subproject commit 3940eeb2a7c9147a5b91e1e2519530a3b1e8b9ca
|
||||
Loading…
Reference in New Issue
Block a user