-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathio.lua
43 lines (33 loc) · 1.1 KB
/
io.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
-- The loader for individual IO formats for vstruct.
-- Returns the "io function", which is called with:
-- * an IO type, such as 'x' or 'c'
-- * an IO operation, such as 'write' or 'size'
-- * some (type x operation) specific other arguments
-- upon which it will attempt to load the handler for that operation from the
-- module vstruct.io.<type> and call it.
local defaults = require "vstruct.io.defaults"
local mt = { __index = defaults }
local function iorequire(format)
local r,v = pcall(require, "vstruct.io."..format)
if not r then
error("vstruct: no support for format '"..format.."':\n"..tostring(v))
end
setmetatable(v, mt)
return v
end
local controlnames = {
seekf = "+";
seekb = "-";
seekto = "@";
bigendian = ">";
littleendian= "<";
hostendian = "=";
}
for name,symbol in pairs(controlnames) do
package.preload["vstruct.io."..symbol] = function() return iorequire(name) end
end
return function(format, method, ...)
local fmt = iorequire(format)
assert(fmt[method], "No support for method '"..tostring(method).."' in IO module '"..format.."'")
return fmt[method](...)
end