Format file.lua

This commit is contained in:
Lars Mueller 2020-12-22 11:32:35 +01:00
parent f39596efb6
commit 2e4f03c94e

@ -1,38 +1,34 @@
function read(filename) function read(filename)
local file = io.open(filename, "r") local file = io.open(filename, "r")
if file==nil then if file == nil then return nil end
return nil local content = file:read"*a"
end
local content=file:read("*a")
file:close() file:close()
return content return content
end end
function write(filename, new_content) function write(filename, new_content)
local file = io.open(filename, "w") local file = io.open(filename, "w")
if file==nil then if file == nil then return false end
return false
end
file:write(new_content) file:write(new_content)
file:close() file:close()
return true return true
end end
function append(filename, new_content) function append(filename, new_content)
local file = io.open(filename, "a") local file = io.open(filename, "a")
if file==nil then if file == nil then return false end
return false
end
file:write(new_content) file:write(new_content)
file:close() file:close()
return true return true
end end
function exists(filename) function exists(filename)
local file = io.open(filename, "r") local file = io.open(filename, "r")
if file==nil then if file == nil then return false end
return false
end
file:close() file:close()
return true return true
end end
function create_if_not_exists(filename, content) function create_if_not_exists(filename, content)
if not exists(filename) then if not exists(filename) then
write(filename, content or "") write(filename, content or "")
@ -40,9 +36,8 @@ function create_if_not_exists(filename, content)
end end
return false return false
end end
function create_if_not_exists_from_file(filename, src_filename)
return create_if_not_exists(filename, read(src_filename)) function create_if_not_exists_from_file(filename, src_filename) return create_if_not_exists(filename, read(src_filename)) end
end
-- Process Bridge Helpers -- Process Bridge Helpers
process_bridges = {} process_bridges = {}
@ -60,8 +55,14 @@ function process_bridge_build(name, input, output, logs)
write(output, "") write(output, "")
-- Create logs if not exists -- Create logs if not exists
create_if_not_exists(logs, "") create_if_not_exists(logs, "")
process_bridges[name]={input=input, output=output, logs=logs, output_file=io.open(output,"a")} process_bridges[name] = {
input = input,
output = output,
logs = logs,
output_file = io.open(output, "a")
}
end end
function process_bridge_listen(name, line_consumer, step) function process_bridge_listen(name, line_consumer, step)
local bridge = process_bridges[name] local bridge = process_bridges[name]
modlib.minetest.register_globalstep(step or 0.1, function(dtime) modlib.minetest.register_globalstep(step or 0.1, function(dtime)
@ -74,6 +75,7 @@ function process_bridge_listen(name, line_consumer, step)
write(bridge.input, "") write(bridge.input, "")
end) end)
end end
function process_bridge_serve(name, step) function process_bridge_serve(name, step)
local bridge = process_bridges[name] local bridge = process_bridges[name]
modlib.minetest.register_globalstep(step or 0.1, function(dtime) modlib.minetest.register_globalstep(step or 0.1, function(dtime)
@ -81,11 +83,13 @@ function process_bridge_serve(name, step)
process_bridges[name].output_file = io.open(bridge.output, "a") process_bridges[name].output_file = io.open(bridge.output, "a")
end) end)
end end
function process_bridge_write(name, message) function process_bridge_write(name, message)
local bridge = process_bridges[name] local bridge = process_bridges[name]
bridge.output_file:write(message .. "\n") bridge.output_file:write(message .. "\n")
-- append(bridge.input, message) -- append(bridge.input, message)
end end
function process_bridge_start(name, command, os_execute) function process_bridge_start(name, command, os_execute)
local bridge = process_bridges[name] local bridge = process_bridges[name]
os_execute(string.format(command, bridge.output, bridge.input, bridge.logs)) os_execute(string.format(command, bridge.output, bridge.input, bridge.logs))