-
Notifications
You must be signed in to change notification settings - Fork 5
/
udf_template.lua
87 lines (75 loc) · 2.57 KB
/
udf_template.lua
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
--
-- HDF5-UDF: User-Defined Functions for HDF5
--
-- File: udf_template.lua
--
-- HDF5 filter callbacks and main interface with the Lua API.
--
local lib = {}
function init(libpath)
local ffi = require("ffi")
local udflib = ffi.load(libpath)
ffi.cdef[[
const char *luaGetFilePath(void);
void *luaGetData(const char *);
const char *luaGetType(const char *);
const char *luaGetCast(const char *);
const char *luaGetDims(const char *);
int luaGetElementSize(const char *);
// compound_declarations_placeholder
]]
lib.getFilePath = function()
return ffi.string(udflib.luaGetFilePath())
end
lib.string = function(name)
local type = tostring(ffi.typeof(name)):gsub("ctype<", ""):gsub("( ?)[&>?]", "")
if type:find("^struct") ~= nil then
return ffi.string(name.value)
elseif type:find("^char ") ~= nil then
return ffi.string(ffi.cast("char *", name))
end
return ffi.string(name)
end
lib.setString = function(name, s)
local t = tostring(ffi.typeof(name)):gsub("ctype<", ""):gsub("( ?)[&>?]", "")
if t:find("^struct") ~= nil then
local n = #s
if n > ffi.sizeof(name.value) then
n = ffi.sizeof(name.value)
end
ffi.copy(name.value, s, n)
else
local n = #s
if n > ffi.sizeof(name) then
n = ffi.sizeof(name)
end
ffi.copy(name, s, n)
end
end
lib.getData = function(name)
local cast = udflib.luaGetCast(name)
local data = ffi.cast("char*", udflib.luaGetData(name))
-- To allow 1-based indexing of HDF5 datasets we return a shifted data
-- container to the Lua application. In case of compounds or structures
-- the shift size is determined by ffi.sizeof().
local elementsize = udflib.luaGetElementSize(name)
if elementsize == -1 then
local datatype = ffi.string(cast):gsub("*", "")
elementsize = ffi.sizeof(ffi.typeof(datatype))
end
return ffi.cast(ffi.string(cast), data - elementsize)
end
lib.getType = function(name)
return ffi.string(udflib.luaGetType(name))
end
lib.getDims = function(name)
local dims = ffi.string(udflib.luaGetDims(name))
local t = {}
for dim in string.gmatch(dims, "([^x]+)") do
table.insert(t, tonumber(dim))
end
return t
end
end
-- User-Defined Function
-- user_callback_placeholder