2017-01-29 15:18:49 -08:00
|
|
|
-- digicompute/builtin.lua
|
|
|
|
|
|
|
|
digicompute.builtin = {}
|
|
|
|
local builtin = digicompute.builtin
|
|
|
|
|
|
|
|
-- [function] check if file exists
|
|
|
|
function builtin.exists(path)
|
2018-05-31 11:00:44 -07:00
|
|
|
if io.open(path, "r") then return true end
|
2017-01-29 15:18:49 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] list contents
|
|
|
|
function builtin.list(path)
|
2018-05-31 11:00:44 -07:00
|
|
|
local files = minetest.get_dir_list(path, false)
|
|
|
|
local subdirs = minetest.get_dir_list(path, true)
|
2017-01-29 15:18:49 -08:00
|
|
|
|
2018-05-31 11:00:44 -07:00
|
|
|
local retval = {
|
|
|
|
files = files,
|
|
|
|
subdirs = subdirs,
|
|
|
|
}
|
2017-02-06 21:02:54 -08:00
|
|
|
|
2018-05-31 11:00:44 -07:00
|
|
|
if not files and not subdirs then
|
|
|
|
retval = nil
|
|
|
|
end
|
2017-02-06 21:02:54 -08:00
|
|
|
|
2018-05-31 11:00:44 -07:00
|
|
|
return retval
|
2017-01-29 15:18:49 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] create file
|
|
|
|
function builtin.create(path)
|
2018-05-31 11:00:44 -07:00
|
|
|
local f = io.open(path, "w") -- create file
|
|
|
|
f:close() -- close file
|
|
|
|
return true
|
2017-01-29 15:18:49 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] write to file
|
|
|
|
function builtin.write(path, data, mode)
|
2018-05-31 11:00:44 -07:00
|
|
|
if mode ~= "w" and mode ~= "a" then
|
|
|
|
mode = "w"
|
|
|
|
end
|
|
|
|
local f = io.open(path, mode) -- open file for writing
|
|
|
|
f:write(data) -- write data
|
|
|
|
f:close() -- close file
|
|
|
|
return true
|
2017-01-29 15:18:49 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] read file
|
|
|
|
function builtin.read(path)
|
2018-05-31 11:00:44 -07:00
|
|
|
local f = io.open(path, "r") -- open file for reading
|
|
|
|
if f then
|
|
|
|
local data = f:read("*all") -- read and store all data
|
|
|
|
f:close() -- Close file
|
|
|
|
return data -- return file contents
|
|
|
|
end
|
2017-01-29 15:18:49 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] copy file
|
|
|
|
function builtin.copy(original, new)
|
2018-05-31 11:00:44 -07:00
|
|
|
original = builtin.read(original) -- read
|
|
|
|
if original then
|
|
|
|
builtin.write(new, original) -- write
|
|
|
|
return true
|
|
|
|
end
|
2017-01-29 15:18:49 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] create directory
|
|
|
|
function builtin.mkdir(path)
|
2018-05-31 11:00:44 -07:00
|
|
|
if not io.open(path) then
|
|
|
|
if minetest.mkdir then
|
|
|
|
minetest.mkdir(path) -- create directory if minetest.mkdir is available
|
|
|
|
elseif os.execute then
|
|
|
|
os.execute('mkdir "'..path..'"') -- create directory with os mkdir command
|
2017-06-27 13:59:08 -07:00
|
|
|
else
|
|
|
|
return false
|
2018-05-31 11:00:44 -07:00
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2017-01-29 15:18:49 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] remove directory
|
|
|
|
function builtin.rmdir(path)
|
2018-05-31 11:00:44 -07:00
|
|
|
if builtin.list(path) then
|
|
|
|
-- [local function] remove files
|
|
|
|
local function rm_files(ppath, files)
|
|
|
|
for _, f in ipairs(files) do
|
|
|
|
os.remove(ppath.."/"..f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- [local function] check and rm dir
|
|
|
|
local function rm_dir(dpath)
|
|
|
|
local files = minetest.get_dir_list(dpath, false)
|
|
|
|
local subdirs = minetest.get_dir_list(dpath, true)
|
|
|
|
rm_files(dpath, files)
|
|
|
|
if subdirs then
|
|
|
|
for _, d in ipairs(subdirs) do
|
|
|
|
rm_dir(dpath.."/"..d)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local ok = os.remove(dpath) -- TODO: TEST
|
|
|
|
if not ok then
|
2017-06-27 13:59:08 -07:00
|
|
|
if os.execute then
|
2018-05-31 11:00:44 -07:00
|
|
|
os.execute("rmdir "..dpath)
|
2017-06-27 13:59:08 -07:00
|
|
|
end
|
2018-05-31 11:00:44 -07:00
|
|
|
end
|
|
|
|
end
|
2017-02-06 21:02:54 -08:00
|
|
|
|
2018-05-31 11:00:44 -07:00
|
|
|
local len = path:len()
|
2017-02-06 21:02:54 -08:00
|
|
|
|
2018-05-31 11:00:44 -07:00
|
|
|
if path:sub(len, len) == "/" then
|
|
|
|
path = path:sub(1, -2)
|
|
|
|
end
|
2017-01-29 15:18:49 -08:00
|
|
|
|
2018-05-31 11:00:44 -07:00
|
|
|
rm_dir(path)
|
|
|
|
return true
|
|
|
|
end
|
2017-01-29 15:18:49 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
-- [function] copy directory
|
|
|
|
function builtin.cpdir(original, new)
|
2018-05-31 11:00:44 -07:00
|
|
|
if builtin.list(original) then
|
|
|
|
-- [local function] copy files
|
|
|
|
local function copy_files(opath, npath, files)
|
|
|
|
for _, f in ipairs(files) do
|
|
|
|
builtin.copy(opath.."/"..f, npath.."/"..f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- [local function] check and copy dir
|
|
|
|
local function copy_dir(opath, npath)
|
|
|
|
builtin.mkdir(npath)
|
|
|
|
local files = minetest.get_dir_list(opath, false)
|
|
|
|
local subdirs = minetest.get_dir_list(opath, true)
|
|
|
|
copy_files(opath, npath, files)
|
|
|
|
for _, d in ipairs(subdirs) do
|
|
|
|
copy_dir(opath.."/"..d, npath.."/"..d)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
copy_dir(original, new)
|
|
|
|
return true
|
|
|
|
end
|
2017-01-29 15:18:49 -08:00
|
|
|
end
|