cc/permset.lua

45 lines
918 B
Lua
Raw Permalink Normal View History

2024-05-31 23:12:12 +00:00
local function new(file)
local set = {}
local fh = fs.open(file, "r")
if fh == nil then
-- no such file, return empty set
return {inner=set, file=file}
end
local line = fh.readLine()
while line ~= nil do
if line then
set[line] = true
end
line = fh.readLine()
end
fh.close()
return {inner=set, file=file}
end
local function writeSet(set)
local fh = fs.open(set.file, "w")
for it, allowed in pairs(set.inner) do
if allowed then
fh.writeLine(it)
end
end
fh.flush()
fh.close()
end
local function add(set, item)
set.inner[item] = true
writeSet(set)
end
local function remove(set, item)
set.inner[item] = false
writeSet(set)
end
local function has(set, item)
return set.inner[item] == true
end
return {new=new, writeSet=writeSet, add=add, remove=remove, has=has}