mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-22 07:13:45 +01:00
file: return errors for use in assertions
This commit is contained in:
parent
9bfefb9283
commit
170b0393e1
25
file.lua
25
file.lua
@ -25,16 +25,16 @@ end
|
|||||||
-- concat_path is set by init.lua to avoid code duplication
|
-- concat_path is set by init.lua to avoid code duplication
|
||||||
|
|
||||||
function read(filename)
|
function read(filename)
|
||||||
local file = io.open(filename, "r")
|
local file, err = io.open(filename, "r")
|
||||||
if file == nil then return nil end
|
if file == nil then return nil, err end
|
||||||
local content = file:read"*a"
|
local content = file:read"*a"
|
||||||
file:close()
|
file:close()
|
||||||
return content
|
return content
|
||||||
end
|
end
|
||||||
|
|
||||||
function write_unsafe(filename, new_content)
|
function write_unsafe(filename, new_content)
|
||||||
local file = io.open(filename, "w")
|
local file, err = io.open(filename, "w")
|
||||||
if file == nil then return false end
|
if file == nil then return false, err end
|
||||||
file:write(new_content)
|
file:write(new_content)
|
||||||
file:close()
|
file:close()
|
||||||
return true
|
return true
|
||||||
@ -42,9 +42,9 @@ end
|
|||||||
|
|
||||||
write = minetest and minetest.safe_file_write or write_unsafe
|
write = minetest and minetest.safe_file_write or write_unsafe
|
||||||
|
|
||||||
function write_binary(filename, new_content)
|
function write_binary_unsafe(filename, new_content)
|
||||||
local file = io.open(filename, "wb")
|
local file, err = io.open(filename, "wb")
|
||||||
if file == nil then return false end
|
if file == nil then return false, err end
|
||||||
file:write(new_content)
|
file:write(new_content)
|
||||||
file:close()
|
file:close()
|
||||||
return true
|
return true
|
||||||
@ -61,24 +61,23 @@ function ensure_content(filename, ensured_content)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function append(filename, new_content)
|
function append(filename, new_content)
|
||||||
local file = io.open(filename, "a")
|
local file, err = io.open(filename, "a")
|
||||||
if file == nil then return false end
|
if file == nil then return false, err 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, err = io.open(filename, "r")
|
||||||
if file == nil then return false end
|
if file == nil then return false, err 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 "")
|
return write(filename, content or "")
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user