modlib/file.lua

104 lines
2.7 KiB
Lua
Raw Normal View History

2020-02-09 01:39:54 +01:00
function read(filename)
2020-12-22 11:32:35 +01:00
local file = io.open(filename, "r")
if file == nil then return nil end
local content = file:read"*a"
file:close()
return content
2020-02-09 01:39:54 +01:00
end
2020-12-22 11:32:35 +01:00
2020-02-09 01:39:54 +01:00
function write(filename, new_content)
2020-12-22 11:32:35 +01:00
local file = io.open(filename, "w")
if file == nil then return false end
file:write(new_content)
file:close()
return true
2020-02-09 01:39:54 +01:00
end
2020-12-22 11:32:35 +01:00
2020-12-22 11:36:58 +01:00
function ensure_content(filename, ensured_content)
local content = read(filename)
if content ~= ensured_content then
return write(filename, ensured_content)
end
return true
end
2020-02-09 01:39:54 +01:00
function append(filename, new_content)
2020-12-22 11:32:35 +01:00
local file = io.open(filename, "a")
if file == nil then return false end
file:write(new_content)
file:close()
return true
2020-02-09 01:39:54 +01:00
end
2020-12-22 11:32:35 +01:00
2020-02-09 01:39:54 +01:00
function exists(filename)
2020-12-22 11:32:35 +01:00
local file = io.open(filename, "r")
if file == nil then return false end
file:close()
return true
2020-02-09 01:39:54 +01:00
end
2020-12-22 11:32:35 +01:00
2020-02-09 01:39:54 +01:00
function create_if_not_exists(filename, content)
2020-12-22 11:32:35 +01:00
if not exists(filename) then
write(filename, content or "")
return true
end
return false
2020-02-09 01:39:54 +01:00
end
2020-12-22 11:32:35 +01:00
function create_if_not_exists_from_file(filename, src_filename) return create_if_not_exists(filename, read(src_filename)) end
2020-02-09 01:39:54 +01:00
-- Process Bridge Helpers
2020-12-22 11:32:35 +01:00
process_bridges = {}
2020-02-09 01:39:54 +01:00
function process_bridge_build(name, input, output, logs)
2020-12-22 11:32:35 +01:00
if not input or not output or not logs then
minetest.mkdir(minetest.get_worldpath() .. "/bridges/" .. name)
end
input = input or minetest.get_worldpath() .. "/bridges/" .. name .. "/input.txt"
output = output or minetest.get_worldpath() .. "/bridges/" .. name .. "/output.txt"
logs = logs or minetest.get_worldpath() .. "/bridges/" .. name .. "/logs.txt"
-- Clear input
write(input, "")
-- Clear output
write(output, "")
-- Create logs if not exists
create_if_not_exists(logs, "")
process_bridges[name] = {
input = input,
output = output,
logs = logs,
output_file = io.open(output, "a")
}
2020-02-09 01:39:54 +01:00
end
2020-12-22 11:32:35 +01:00
2020-02-09 01:39:54 +01:00
function process_bridge_listen(name, line_consumer, step)
2020-12-22 11:32:35 +01:00
local bridge = process_bridges[name]
modlib.minetest.register_globalstep(step or 0.1, function(dtime)
local content = io.open(bridge.input, "r")
local line = content:read()
while line do
line_consumer(line)
line = content:read()
end
write(bridge.input, "")
end)
2020-02-09 01:39:54 +01:00
end
2020-12-22 11:32:35 +01:00
2020-02-09 01:39:54 +01:00
function process_bridge_serve(name, step)
2020-12-22 11:32:35 +01:00
local bridge = process_bridges[name]
modlib.minetest.register_globalstep(step or 0.1, function(dtime)
bridge.output_file:close()
process_bridges[name].output_file = io.open(bridge.output, "a")
end)
2020-02-09 01:39:54 +01:00
end
2020-12-22 11:32:35 +01:00
2020-02-09 01:39:54 +01:00
function process_bridge_write(name, message)
2020-12-22 11:32:35 +01:00
local bridge = process_bridges[name]
bridge.output_file:write(message .. "\n")
-- append(bridge.input, message)
2020-02-09 01:39:54 +01:00
end
2020-12-22 11:32:35 +01:00
2020-02-09 01:39:54 +01:00
function process_bridge_start(name, command, os_execute)
2020-12-22 11:32:35 +01:00
local bridge = process_bridges[name]
os_execute(string.format(command, bridge.output, bridge.input, bridge.logs))
2020-02-09 01:39:54 +01:00
end