forked from izaya/OC-PsychOS2
move libraries out of main dist and into PsychOSPackages
This commit is contained in:
parent
92084e8c90
commit
b82bb2a853
@ -1,69 +0,0 @@
|
||||
local preproc = {}
|
||||
preproc.directives = {}
|
||||
|
||||
function preproc.parsewords(line) -- string -- table -- Returns a table of words from the string *line*, parsing quotes and escapes.
|
||||
local rt = {""}
|
||||
local escaped, quoted = false, false
|
||||
for c in line:gmatch(".") do
|
||||
if escaped then
|
||||
rt[#rt] = rt[#rt]..c
|
||||
elseif c == '"' or c == "'" then
|
||||
quoted = not quoted
|
||||
elseif c == "\\" then
|
||||
escaped = true
|
||||
elseif c:match("%s") and not quoted and rt[#rt]:len() > 0 then
|
||||
rt[#rt+1] = ""
|
||||
else
|
||||
rt[#rt] = rt[#rt]..c
|
||||
end
|
||||
end
|
||||
return rt
|
||||
end
|
||||
|
||||
function preproc.line(line) -- string -- -- Returns either a function - which can be called to get lines until it returns nil - or a string from processing *line* using preprocessor directives.
|
||||
if line:match("^%-%-#") then
|
||||
local directive, args = line:match("^%-%-#(%S+)%s(.+)")
|
||||
print(directive,args)
|
||||
local args = preproc.parsewords(args)
|
||||
if preproc.directives[directive] then
|
||||
return preproc.directives[directive](table.unpack(args))
|
||||
else
|
||||
error("unknown preprocessor directive: "..directive)
|
||||
end
|
||||
else
|
||||
return line
|
||||
end
|
||||
end
|
||||
|
||||
function preproc.preproc(...) -- string -- string -- Returns the output from preprocessing the files listed in *...*.
|
||||
local tA = {...}
|
||||
local output = ""
|
||||
for _,fname in ipairs(tA) do
|
||||
local f,e = io.open(fname)
|
||||
if not f then error("unable to open file "..fname..": "..e) end
|
||||
for line in f:lines() do
|
||||
local r = preproc.line(line)
|
||||
if type(r) == "function" then
|
||||
while true do
|
||||
local rs = r()
|
||||
if not rs then break end
|
||||
output = output .. rs .. "\n"
|
||||
end
|
||||
else
|
||||
output = output .. r .. "\n"
|
||||
end
|
||||
end
|
||||
end
|
||||
return output
|
||||
end
|
||||
|
||||
preproc.directives.include = preproc.preproc
|
||||
|
||||
return setmetatable(preproc,{__call=function(_,...)
|
||||
local tA = {...}
|
||||
local out = table.remove(tA,#tA)
|
||||
local f,e = io.open(out,"wb")
|
||||
if not f then error("unable to open file "..out..": "..e) end
|
||||
f:write(preproc.preproc(table.unpack(tA)))
|
||||
f:close()
|
||||
end})
|
114
lib/unionfs.lua
114
lib/unionfs.lua
@ -1,114 +0,0 @@
|
||||
local unionfs = {}
|
||||
|
||||
local function normalise(path)
|
||||
return table.concat(fs.segments(path),"/")
|
||||
end
|
||||
|
||||
function unionfs.create(...) -- string -- table -- Returns a unionfs object of the directories specified in *...*.
|
||||
local paths,fids,fc = {...}, {}, 0
|
||||
for k,v in pairs(paths) do
|
||||
paths[k] = "/"..normalise(v)
|
||||
end
|
||||
local proxy = {}
|
||||
local function realpath(path)
|
||||
path = path or ""
|
||||
for k,v in pairs(paths) do
|
||||
if fs.exists(v.."/"..path) then
|
||||
return v.."/"..path
|
||||
end
|
||||
end
|
||||
return paths[1].."/"..path
|
||||
end
|
||||
|
||||
function proxy.setLabel()
|
||||
return false
|
||||
end
|
||||
|
||||
function proxy.spaceUsed()
|
||||
return fs.spaceUsed(paths[1])
|
||||
end
|
||||
function proxy.spaceTotal()
|
||||
return fs.spaceTotal(paths[1])
|
||||
end
|
||||
function proxy.isReadOnly()
|
||||
return fs.isReadOnly(paths[1])
|
||||
end
|
||||
function proxy.isDirectory(path)
|
||||
return fs.isDirectory(realpath(path))
|
||||
end
|
||||
function proxy.lastModified(path)
|
||||
return fs.lastModified(realpath(path))
|
||||
end
|
||||
function proxy.getLabel()
|
||||
return fs.getLabel(paths[1])
|
||||
end
|
||||
|
||||
function proxy.exists(path)
|
||||
return fs.exists(realpath(path))
|
||||
end
|
||||
function proxy.remove(path)
|
||||
return fs.remove(realpath(path))
|
||||
end
|
||||
function proxy.size(path)
|
||||
return fs.size(realpath(path))
|
||||
end
|
||||
|
||||
function proxy.list(path)
|
||||
local nt,rt = {},{}
|
||||
if #fs.segments(path) < 1 then
|
||||
for k,v in pairs(paths) do
|
||||
for l,m in ipairs(fs.list(v.."/"..path)) do
|
||||
nt[m] = true
|
||||
end
|
||||
end
|
||||
for k,v in pairs(nt) do
|
||||
rt[#rt+1] = k
|
||||
end
|
||||
table.sort(rt)
|
||||
return rt
|
||||
else
|
||||
return fs.list(realpath(path))
|
||||
end
|
||||
end
|
||||
|
||||
function proxy.open(path,mode)
|
||||
local fh, r = fs.open(realpath(path),mode)
|
||||
if not fh then return fh, r end
|
||||
fids[fc] = fh
|
||||
fc = fc + 1
|
||||
return fc - 1
|
||||
end
|
||||
|
||||
function proxy.close(fid)
|
||||
if not fids[fid] then
|
||||
return false, "file not open"
|
||||
end
|
||||
local rfh = fids[fid]
|
||||
fids[fid] = nil
|
||||
return rfh:close()
|
||||
end
|
||||
function proxy.write(fid,d)
|
||||
if not fids[fid] then
|
||||
return false, "file not open"
|
||||
end
|
||||
return fids[fid]:write(d)
|
||||
end
|
||||
function proxy.read(fid,d)
|
||||
if not fids[fid] then
|
||||
return false, "file not open"
|
||||
end
|
||||
local rb = fids[fid]:read(d)
|
||||
if rb == "" then rb = nil end
|
||||
return rb
|
||||
end
|
||||
function proxy.seek(fid,d)
|
||||
if not fids[fid] then
|
||||
return false, "file not open"
|
||||
end
|
||||
return fids[fid]:seek(d)
|
||||
end
|
||||
|
||||
return proxy
|
||||
end
|
||||
|
||||
return unionfs
|
Loading…
Reference in New Issue
Block a user