1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
using System;
using System.Runtime.InteropServices;
namespace Clearsilver {
// opaque types
public unsafe struct HDF {};
public unsafe struct NEOERR {};
public unsafe class Hdf {
[DllImport("libneo", EntryPoint="hdf_init")]
private static extern unsafe NEOERR* hdf_init(HDF **foo);
// NEOERR* hdf_set_value (HDF *hdf, char *name, char *value)
[DllImport("libneo")]
private static unsafe extern NEOERR* hdf_set_value(HDF *hdf,
[MarshalAs(UnmanagedType.LPStr)]
string name,
[MarshalAs(UnmanagedType.LPStr)]
string value);
// char* hdf_get_value (HDF *hdf, char *name, char *defval)
[DllImport("libneo")]
[return: MarshalAs(UnmanagedType.LPStr)]
private static unsafe extern string hdf_get_value(HDF *hdf,
[MarshalAs(UnmanagedType.LPStr)]
string name,
[MarshalAs(UnmanagedType.LPStr)]
string defval);
// NEOERR* hdf_dump (HDF *hdf, char *prefix);
[DllImport("libneo", EntryPoint="hdf_dump")]
private static extern void hdf_dump(
HDF *hdf,
[MarshalAs(UnmanagedType.LPStr)]
string prefix);
// HDF* hdf_get_obj (HDF *hdf, char *name)
[DllImport("libneo", EntryPoint="hdf_get_obj")]
private static extern HDF* hdf_get_obj(
HDF *hdf,
[MarshalAs(UnmanagedType.LPStr)]
string name);
// -----------------------------------------------------------
public HDF *hdf_root;
public Hdf() {
fixed (HDF **hdf_ptr = &hdf_root) {
hdf_init(hdf_ptr);
}
// Console.WriteLine((int)hdf_root);
}
public void setValue(string name,string value) {
hdf_set_value(hdf_root,name,value);
}
public string getValue(string name,string defvalue) {
return hdf_get_value(hdf_root,name,defvalue);
}
public void test() {
hdf_set_value(hdf_root,"b","1");
// hdf_read_file(hdf_root,"test.hdf");
// Console.WriteLine("b ", hdf_get_value(hdf_root,"b","5"));
hdf_dump(hdf_root,null);
// HDF *n = hdf_get_obj(hdf_root,"b");
// Console.WriteLine("object name {0}",
// Marshal.PtrToStringAnsi((IntPtr)n->name));
}
};
unsafe struct CSPARSE {};
public class CSTContext {
unsafe CSPARSE *csp;
unsafe public CSTContext(Hdf hdf) {
fixed (CSPARSE **csp_ptr = &csp) {
cs_init(csp_ptr, hdf.hdf_root);
}
}
[DllImport("libneo")]
extern static unsafe NEOERR *cs_init (CSPARSE **parse, HDF *hdf);
public unsafe void parseFile(string filename) {
cs_parse_file(csp,filename);
}
[DllImport("libneo")]
extern static unsafe NEOERR *cs_parse_file (CSPARSE *parse,
[MarshalAs(UnmanagedType.LPStr)]
string path);
// [DllImport("libneo")]
// extern static unsafe NEOERR *cs_parse_string (CSPARSE *parse,
// char *buf,
// size_t blen);
// NEOERR *cs_render (CSPARSE *parse, void *ctx, CSOUTFUNC cb);
// typedef NEOERR* (*CSOUTFUNC)(void *ctx, char *more_str_bytes);
[DllImport("libneo")]
extern static unsafe NEOERR *cs_render (CSPARSE *parse,
void *ctx,
[MarshalAs(UnmanagedType.FunctionPtr)]
CSOUTFUNC cb);
private unsafe delegate NEOERR* CSOUTFUNC(void* ctx, sbyte* more_bytes);
private class OutputBuilder {
private string output = "";
public unsafe NEOERR* handleOutput(void* ctx, sbyte* more_bytes) {
// add the more_bytes to the current string buffer
output += new String(more_bytes);
// Console.WriteLine("handleOutput called");
return null;
}
public string result() {
return output;
}
}
public unsafe string render() {
OutputBuilder ob = new OutputBuilder();
cs_render(csp,null,new CSOUTFUNC(ob.handleOutput));
return ob.result();
}
[DllImport("libneo")]
extern static unsafe void cs_destroy (CSPARSE **parse);
};
} // namespace Clearsilver
|