40 private static readonly Dictionary<Backend.Types.peak_icv_archive_value_type,
ArchiveValueKind> _sValueKindMap =
43 { Backend.Types.peak_icv_archive_value_type.PEAK_ICV_ARCHIVE_VALUE_TYPE_BOOL, ArchiveValueKind.Bool },
45 Backend.Types.peak_icv_archive_value_type.PEAK_ICV_ARCHIVE_VALUE_TYPE_BOOL_ARRAY,
46 ArchiveValueKind.BoolArray
48 { Backend.Types.peak_icv_archive_value_type.PEAK_ICV_ARCHIVE_VALUE_TYPE_INT, ArchiveValueKind.Int64 },
50 Backend.Types.peak_icv_archive_value_type.PEAK_ICV_ARCHIVE_VALUE_TYPE_INT_ARRAY,
51 ArchiveValueKind.Int64Array
54 Backend.Types.peak_icv_archive_value_type.PEAK_ICV_ARCHIVE_VALUE_TYPE_DOUBLE,
55 ArchiveValueKind.Double
58 Backend.Types.peak_icv_archive_value_type.PEAK_ICV_ARCHIVE_VALUE_TYPE_DOUBLE_ARRAY,
59 ArchiveValueKind.DoubleArray
62 Backend.Types.peak_icv_archive_value_type.PEAK_ICV_ARCHIVE_VALUE_TYPE_STRING,
63 ArchiveValueKind.String
66 Backend.Types.peak_icv_archive_value_type.PEAK_ICV_ARCHIVE_VALUE_TYPE_STRING_ARRAY,
67 ArchiveValueKind.StringArray
70 Backend.Types.peak_icv_archive_value_type.PEAK_ICV_ARCHIVE_VALUE_TYPE_ARCHIVE,
71 ArchiveValueKind.Archive
74 Backend.Types.peak_icv_archive_value_type.PEAK_ICV_ARCHIVE_VALUE_TYPE_ARCHIVE_ARRAY,
75 ArchiveValueKind.ArchiveArray
79 private bool _disposed;
81 internal Handle _handle;
97 var handle =
new Handle();
98 DllHelper.ExecuteAndMapReturnCodes(() =>
99 CInterface.peak_icv_Archive_Create(ref handle));
116 GC.SuppressFinalize(
this);
119 SafeHandle IHasSafeHandle.Handle => _handle;
134 DllHelper.ExecuteAndMapReturnCodes(() =>
135 CInterface.peak_icv_Archive_HasKey(_handle, Utils.ToUtf8(key), ref hasKey));
152 DllHelper.ExecuteAndMapReturnCodes(() =>
153 CInterface.peak_icv_Archive_GetValueType(_handle, Utils.ToUtf8(key), ref type));
155 return _sValueKindMap[(Backend.Types.peak_icv_archive_value_type) type];
165 public override IReadOnlyList<string>
GetKeys()
168 DllHelper.ExecuteAndMapReturnCodes(() =>
169 CInterface.peak_icv_Archive_GetKeys_GetCount(_handle, ref keyCount));
171 var list =
new List<string>((
int) keyCount);
173 for (nuint i = 0; i < keyCount; i++)
175 nuint elementSize = 0;
177 DllHelper.ExecuteAndMapReturnCodes(() =>
178 CInterface.peak_icv_Archive_GetKeysElement_GetSizeInBytes(_handle, i1, ref elementSize));
180 var data =
new byte[elementSize];
181 DllHelper.ExecuteAndMapReturnCodes(() =>
182 CInterface.peak_icv_Archive_GetKeysElement(_handle, i1, data, elementSize));
184 list.Add(Utils.FromUtf8(data));
200 public override void SetBool(
string key,
bool value)
202 DllHelper.ExecuteAndMapReturnCodes(() =>
203 CInterface.peak_icv_Archive_SetBool(_handle, Utils.ToUtf8(key), value));
219 DllHelper.ExecuteAndMapReturnCodes(() =>
220 CInterface.peak_icv_Archive_GetBool(_handle, Utils.ToUtf8(key), ref value));
234 public override void SetInt64(
string key,
long value)
236 DllHelper.ExecuteAndMapReturnCodes(() =>
237 CInterface.peak_icv_Archive_SetInt(_handle, Utils.ToUtf8(key), value));
253 DllHelper.ExecuteAndMapReturnCodes(() =>
254 CInterface.peak_icv_Archive_GetInt(_handle, Utils.ToUtf8(key), ref value));
268 public override void SetDouble(
string key,
double value)
270 DllHelper.ExecuteAndMapReturnCodes(() =>
271 CInterface.peak_icv_Archive_SetDouble(_handle, Utils.ToUtf8(key), value));
287 DllHelper.ExecuteAndMapReturnCodes(() =>
288 CInterface.peak_icv_Archive_GetDouble(_handle, Utils.ToUtf8(key), ref value));
302 public override void SetString(
string key,
string value)
304 DllHelper.ExecuteAndMapReturnCodes(() =>
305 CInterface.peak_icv_Archive_SetString(_handle, Utils.ToUtf8(key), Utils.ToUtf8(value)));
321 var utf8Key = Utils.ToUtf8(key);
322 DllHelper.ExecuteAndMapReturnCodes(() =>
323 CInterface.peak_icv_Archive_GetString_GetSizeInBytes(_handle, utf8Key, ref size));
325 var data =
new byte[size];
326 DllHelper.ExecuteAndMapReturnCodes(() =>
327 CInterface.peak_icv_Archive_GetString(_handle, utf8Key, data, size));
329 return Utils.FromUtf8(data);
352 DllHelper.ExecuteAndMapReturnCodes(() =>
353 CInterface.peak_icv_Archive_SetArchive(_handle, Utils.ToUtf8(key), arch._handle));
368 var subArchiveHandle =
new Handle();
369 DllHelper.ExecuteAndMapReturnCodes(() =>
370 CInterface.peak_icv_Archive_GetArchive(_handle, Utils.ToUtf8(key), ref subArchiveHandle));
371 return new Archive(subArchiveHandle);
394 DllHelper.ExecuteAndMapReturnCodes(() =>
395 CInterface.peak_icv_Archive_GetArray_GetCount(_handle, key, ref count));
412 var byteArray = array.Select(x => (
byte) (x ? 1 : 0)).ToArray();
414 DllHelper.ExecuteAndMapReturnCodes(() =>
415 CInterface.peak_icv_Archive_SetBoolArray(_handle, Utils.ToUtf8(key), byteArray, (nuint) array.Length));
430 var utf8Key = Utils.ToUtf8(key);
432 var values =
new byte[arrayCount];
433 DllHelper.ExecuteAndMapReturnCodes(() =>
434 CInterface.peak_icv_Archive_GetBoolArray(_handle, utf8Key, values, (nuint) arrayCount));
435 return values.Select(x => x > 0).ToList();
451 DllHelper.ExecuteAndMapReturnCodes(() =>
452 CInterface.peak_icv_Archive_SetIntArray(_handle, Utils.ToUtf8(key), array, (nuint) array.Length));
467 var utf8Key = Utils.ToUtf8(key);
469 var values =
new long[arrayCount];
470 DllHelper.ExecuteAndMapReturnCodes(() =>
471 CInterface.peak_icv_Archive_GetIntArray(_handle, utf8Key, values, (nuint) arrayCount));
488 DllHelper.ExecuteAndMapReturnCodes(() =>
489 CInterface.peak_icv_Archive_SetDoubleArray(_handle, Utils.ToUtf8(key), array, (nuint) array.Length));
504 var utf8Key = Utils.ToUtf8(key);
506 var values =
new double[arrayCount];
507 DllHelper.ExecuteAndMapReturnCodes(() =>
508 CInterface.peak_icv_Archive_GetDoubleArray(_handle, utf8Key, values, (nuint) arrayCount));
526 var stringPointers =
new IntPtr[values.Count];
527 var allocatedStrings =
new List<IntPtr>();
531 for (var i = 0; i < values.Count; i++)
535 var bytes = Utils.ToUtf8(entry);
537 IntPtr strPtr = Marshal.AllocHGlobal(bytes.Length);
538 Marshal.Copy(bytes, 0, strPtr, bytes.Length);
540 allocatedStrings.Add(strPtr);
541 stringPointers[i] = strPtr;
544 IntPtr arrayPtr = Marshal.AllocHGlobal(IntPtr.Size * values.Count);
548 Marshal.Copy(stringPointers, 0, arrayPtr, values.Count);
550 DllHelper.ExecuteAndMapReturnCodes(() =>
551 CInterface.peak_icv_Archive_SetStringArray(_handle, Utils.ToUtf8(key), arrayPtr,
552 (nuint) values.Count));
556 Marshal.FreeHGlobal(arrayPtr);
561 foreach (IntPtr ptr
in allocatedStrings)
563 Marshal.FreeHGlobal(ptr);
580 var utf8Key = Utils.ToUtf8(key);
582 var values =
new List<string>(arrayCount);
584 for (var i = 0; i < arrayCount; i++)
587 var index = (nuint) i;
589 DllHelper.ExecuteAndMapReturnCodes(() => CInterface.peak_icv_Archive_GetStringArrayElement_GetSizeInBytes(
590 _handle, utf8Key, index,
593 var data =
new byte[length];
594 DllHelper.ExecuteAndMapReturnCodes(() =>
595 CInterface.peak_icv_Archive_GetStringArrayElement(_handle, utf8Key, index, data, length));
596 values.Add(Utils.FromUtf8(data));
622 IntPtr[] handles = SafeHandles.ExtractHandles(
630 var utf8Key = Utils.ToUtf8(key);
631 DllHelper.ExecuteAndMapReturnCodes(() =>
635 fixed (IntPtr* ptr = handles)
637 return (Backend.Types.peak_icv_status) CInterface.peak_icv_Archive_SetArchiveArray(_handle,
638 utf8Key, (IntPtr) ptr, (nuint) values.Count);
661 var utf8Key = Utils.ToUtf8(key);
669 var archivePointers =
new IntPtr[arrayCount];
670 DllHelper.ExecuteAndMapReturnCodes(() =>
674 fixed (IntPtr* ptr = archivePointers)
676 return (Backend.Types.peak_icv_status) CInterface.peak_icv_Archive_GetArchiveArray(_handle,
677 utf8Key, (IntPtr) ptr, (nuint) arrayCount);
682 return archivePointers.Select(x =>
new Archive(
new Handle(x))).ToList();
694 var handle =
new Handle();
695 DllHelper.ExecuteAndMapReturnCodes(() =>
696 CInterface.peak_icv_Archive_Create(ref handle));
700 internal sealed class Handle : SafeHandleZeroOrMinusOneIsInvalid
702 internal Handle() : base(true)
706 internal Handle(IntPtr handle) : base(true)
711 protected override bool ReleaseHandle()
713 Backend.Types.peak_icv_status ret = CInterface.peak_icv_Archive_Destroy(handle);
718 private static class CInterface
720 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
721 public static extern Backend.Types.peak_icv_status peak_icv_Archive_Create(ref Handle archiveHandle);
723 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
724 public static extern Backend.Types.peak_icv_status peak_icv_Archive_Destroy(IntPtr archiveHandle);
726 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
727 public static extern Backend.Types.peak_icv_status peak_icv_Archive_HasKey(Handle archiveHandle,
729 [MarshalAs(UnmanagedType.I1)] ref
bool hasKey);
731 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
732 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetKeys_GetCount(
733 Handle archiveHandle, ref nuint size);
735 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
736 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetKeysElement_GetSizeInBytes(
737 Handle archiveHandle, nuint index, ref nuint size);
739 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
740 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetKeysElement(
741 Handle archiveHandle, nuint index,
byte[] data, nuint size);
743 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
744 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetValueType(
745 Handle archiveHandle,
byte[] key, ref
int type);
747 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
748 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetArray_GetCount(
749 Handle archiveHandle,
byte[] key, ref nuint count);
751 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
752 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetInt(
753 Handle archiveHandle,
byte[] key, ref
long value);
755 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
756 public static extern Backend.Types.peak_icv_status peak_icv_Archive_SetInt(
757 Handle archiveHandle,
byte[] key,
long value);
759 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
760 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetIntArray(
761 Handle archiveHandle,
byte[] key,
long[] values, nuint size);
763 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
764 public static extern Backend.Types.peak_icv_status peak_icv_Archive_SetIntArray(
765 Handle archiveHandle,
byte[] key,
long[] values, nuint size);
767 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
768 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetDouble(
769 Handle archiveHandle,
byte[] key, ref
double value);
771 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
772 public static extern Backend.Types.peak_icv_status peak_icv_Archive_SetDouble(
773 Handle archiveHandle,
byte[] key,
double value);
775 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
776 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetDoubleArray(
777 Handle archiveHandle,
byte[] key,
double[] values, nuint size);
779 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
780 public static extern Backend.Types.peak_icv_status peak_icv_Archive_SetDoubleArray(
781 Handle archiveHandle,
byte[] key,
double[] values, nuint size);
783 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
784 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetBool(
785 Handle archiveHandle,
byte[] key, [MarshalAs(UnmanagedType.I1)] ref
bool value);
787 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
788 public static extern Backend.Types.peak_icv_status peak_icv_Archive_SetBool(
789 Handle archiveHandle,
byte[] key, [MarshalAs(UnmanagedType.I1)]
bool value);
791 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
792 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetBoolArray(
793 Handle archiveHandle,
byte[] key,
797 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
798 public static extern Backend.Types.peak_icv_status peak_icv_Archive_SetBoolArray(
799 Handle archiveHandle,
byte[] key,
803 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
804 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetString_GetSizeInBytes(
805 Handle archiveHandle,
byte[] key, ref nuint size);
807 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
808 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetString(
809 Handle archiveHandle,
byte[] key,
byte[] data, nuint size);
811 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
812 public static extern Backend.Types.peak_icv_status peak_icv_Archive_SetString(
813 Handle archiveHandle,
byte[] key,
byte[] data);
815 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
816 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetStringArrayElement_GetSizeInBytes(
817 Handle archiveHandle,
byte[] key, nuint index, ref nuint size);
819 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
820 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetStringArrayElement(
821 Handle archiveHandle,
byte[] key, nuint index,
byte[] data, nuint size);
823 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
824 public static extern Backend.Types.peak_icv_status peak_icv_Archive_SetStringArray(
825 Handle archiveHandle,
byte[] key, IntPtr data, nuint size);
827 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
828 public static extern Backend.Types.peak_icv_status peak_icv_Archive_GetArchive(
829 Handle archiveHandle,
byte[] key, ref Handle subArchive);
831 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
832 public static extern Backend.Types.peak_icv_status peak_icv_Archive_SetArchive(
833 Handle archiveHandle,
byte[] key, Handle subArchive);
835 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
836 public static extern int peak_icv_Archive_GetArchiveArray(
837 Handle archiveHandle,
839 IntPtr subArchiveHandles,
842 [DllImport(Global.LibraryName, CallingConvention = Global.CallingConvention)]
843 public static extern int peak_icv_Archive_SetArchiveArray(
844 Handle archiveHandle,
846 IntPtr subArchiveHandles,