mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-29 10:43:43 +01:00
Format file.lua
This commit is contained in:
parent
f39596efb6
commit
2e4f03c94e
38
file.lua
38
file.lua
@ -1,38 +1,34 @@
|
||||
function read(filename)
|
||||
local file = io.open(filename, "r")
|
||||
if file==nil then
|
||||
return nil
|
||||
end
|
||||
local content=file:read("*a")
|
||||
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
|
||||
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
|
||||
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
|
||||
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 "")
|
||||
@ -40,9 +36,8 @@ function create_if_not_exists(filename, content)
|
||||
end
|
||||
return false
|
||||
end
|
||||
function create_if_not_exists_from_file(filename, src_filename)
|
||||
return create_if_not_exists(filename, read(src_filename))
|
||||
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 = {}
|
||||
@ -60,8 +55,14 @@ function process_bridge_build(name, input, output, logs)
|
||||
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")}
|
||||
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)
|
||||
@ -74,6 +75,7 @@ function process_bridge_listen(name, line_consumer, step)
|
||||
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)
|
||||
@ -81,11 +83,13 @@ function process_bridge_serve(name, step)
|
||||
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)
|
||||
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))
|
||||
|
Loading…
Reference in New Issue
Block a user