Skip to content

Commit bcb9974

Browse files
committed
Added C# bindings
The C# binding of the function opk_extract_file is still missing.
1 parent cad2786 commit bcb9974

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

bindings/csharp/Opk.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace opk
5+
{
6+
public class Opk
7+
{
8+
private IntPtr opk;
9+
10+
[DllImport("libopk.dll", CallingConvention = CallingConvention.Cdecl)]
11+
private static extern IntPtr opk_open(
12+
[In()][MarshalAs(UnmanagedType.LPStr)] string filename);
13+
14+
[DllImport("libopk.dll", CallingConvention = CallingConvention.Cdecl)]
15+
private static extern void opk_close(IntPtr opk);
16+
17+
[DllImport("libopk.dll", CallingConvention = CallingConvention.Cdecl)]
18+
private static extern int opk_open_metadata(IntPtr opk, ref IntPtr filename);
19+
20+
[DllImport("libopk.dll", CallingConvention = CallingConvention.Cdecl)]
21+
private static extern int opk_read_pair(IntPtr opk,
22+
ref IntPtr key, ref ulong key_len,
23+
ref IntPtr val, ref ulong val_len);
24+
25+
public Opk(string filename)
26+
{
27+
this.opk = opk_open(filename);
28+
}
29+
30+
~Opk()
31+
{
32+
opk_close(this.opk);
33+
}
34+
35+
public string open_metadata()
36+
{
37+
IntPtr ptr = (IntPtr) 0;
38+
39+
int ret = opk_open_metadata(this.opk, ref ptr);
40+
if (ret < 0)
41+
throw new Exception("Unable to open metadata: err=" + ret);
42+
if (ret == 0)
43+
return null;
44+
45+
return Marshal.PtrToStringAnsi(ptr);
46+
}
47+
48+
public bool read_pair(out string key, out string value)
49+
{
50+
IntPtr kptr = (IntPtr) 0, vptr = (IntPtr) 0;
51+
ulong klen = 0, vlen = 0;
52+
53+
int ret = opk_read_pair(this.opk, ref kptr, ref klen, ref vptr, ref vlen);
54+
if (ret < 0)
55+
throw new Exception("Unable to read key/value pair: err=" + ret);
56+
if (ret == 0) {
57+
key = null;
58+
value = null;
59+
return false;
60+
}
61+
62+
value = Marshal.PtrToStringAnsi(vptr, (int) vlen);
63+
key = Marshal.PtrToStringAnsi(kptr, (int) klen);
64+
return true;
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)