From 2e4f03c94ed7af2e8d244383917d492885c65bfc Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Tue, 22 Dec 2020 11:32:35 +0100 Subject: [PATCH] Format file.lua --- file.lua | 142 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 73 insertions(+), 69 deletions(-) diff --git a/file.lua b/file.lua index 9ef979a..0588978 100644 --- a/file.lua +++ b/file.lua @@ -1,92 +1,96 @@ function read(filename) - local file=io.open(filename,"r") - if file==nil then - return nil - end - local content=file:read("*a") - file:close() - return content + local file = io.open(filename, "r") + if file == nil then return nil end + local content = file:read"*a" + file:close() + return content end + function write(filename, new_content) - local file=io.open(filename,"w") - if file==nil then - return false - end - file:write(new_content) - file:close() - return true + local file = io.open(filename, "w") + if file == nil then return false end + file:write(new_content) + file:close() + return true end + function append(filename, new_content) - local file=io.open(filename,"a") - if file==nil then - return false - end - file:write(new_content) - file:close() - return true + local file = io.open(filename, "a") + if file == nil then return false end + file:write(new_content) + file:close() + return true end + function exists(filename) - local file=io.open(filename, "r") - if file==nil then - return false - end - file:close() - return true + local file = io.open(filename, "r") + if file == nil then return false end + file:close() + return true end + function create_if_not_exists(filename, content) - if not exists(filename) then - write(filename, content or "") - return true - end - return false -end -function create_if_not_exists_from_file(filename, src_filename) - return create_if_not_exists(filename, read(src_filename)) + if not exists(filename) then + write(filename, content or "") + return true + end + return false end +function create_if_not_exists_from_file(filename, src_filename) return create_if_not_exists(filename, read(src_filename)) end + -- Process Bridge Helpers -process_bridges={} +process_bridges = {} function process_bridge_build(name, input, output, logs) - 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")} + 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") + } end + function process_bridge_listen(name, line_consumer, step) - 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) + 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) end + function process_bridge_serve(name, step) - 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) + 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) end + function process_bridge_write(name, message) - local bridge=process_bridges[name] - bridge.output_file:write(message.."\n") - --append(bridge.input, message) + local bridge = process_bridges[name] + bridge.output_file:write(message .. "\n") + -- append(bridge.input, message) end + function process_bridge_start(name, command, os_execute) - local bridge=process_bridges[name] - os_execute(string.format(command, bridge.output, bridge.input, bridge.logs)) + local bridge = process_bridges[name] + os_execute(string.format(command, bridge.output, bridge.input, bridge.logs)) end \ No newline at end of file