512mb-bot/libraries/file.lua

114 lines
3.2 KiB
Lua
Raw Permalink Normal View History

2021-11-26 16:38:17 +00:00
--This bot is heavily dependent on file operations, therefore this library exists.
local file = {}
local json
if pcall(import,"json") then
2022-05-20 18:20:49 +00:00
json = import("json")
2021-11-26 16:38:17 +00:00
elseif pcall(require,"json") then
2022-05-20 18:20:49 +00:00
json = require("json")
2021-11-26 16:38:17 +00:00
end
file.safe = true
file.read = function(filename,mode)
2022-05-20 18:20:49 +00:00
assert(type(filename) == "string","string expected, got "..type(filename))
local mode = mode or "*a"
local temp_file,err = io.open(filename,r)
if err then
if not file.safe then error(err) else
ret_string = ""
end
else
ret_string = temp_file:read(mode)
temp_file:close()
2021-11-26 16:38:17 +00:00
end
2022-05-20 18:20:49 +00:00
return ret_string,err
2021-11-26 16:38:17 +00:00
end
file.write = function(filename,write)
2022-05-20 18:20:49 +00:00
assert(type(filename) == "string", "string expected, got "..type(filename))
assert(type(write) == "string", "string expected for argument #2, got "..type(write))
local temp_file,err = io.open(filename,"w+")
local status = false
if err then
if not file.safe then error(err) else
status = false
end
else
temp_file:write(write)
temp_file:close()
status = true
2021-11-26 16:38:17 +00:00
end
2022-05-20 18:20:49 +00:00
return status,err
2021-11-26 16:38:17 +00:00
end
file.exists = function(filename)
2022-05-20 18:20:49 +00:00
local file = io.open(filename,"r")
if file then
file:close()
return true
else
return false
end
2021-11-26 16:38:17 +00:00
end
file.existsDir = function(filename)
2022-05-20 18:20:49 +00:00
local file = io.open(filename.."/stuff","w")
if file then
file:close()
os.remove(filename.."/stuff")
return true
else
return false
end
2021-11-26 16:38:17 +00:00
end
file.ls = function(path)
2022-05-20 18:20:49 +00:00
if file.existsDir(path) then
local ls_handle = io.popen("ls -1 "..path,"r")
local ls_data = ls_handle:read("*a")
ls_handle:close()
return ls_data
else
return false, "No such file or directory"
end
2021-11-26 16:38:17 +00:00
end
if json then
2022-05-20 18:20:49 +00:00
file.readJSON = function(filename,default)
assert(type(filename) == "string","string expected, got "..type(filename))
local json_data,err = file.read(filename,"*a")
local table_data, status
if err then
if not file.safe then error(err) else
status = err
table_data = default or {}
end
else
table_data,_,err = json.decode(json_data)
if not table_data then
if not file.safe then error(err) else
status = err
table_data = default or {}
end
end
2021-11-26 16:38:17 +00:00
end
2022-05-20 18:20:49 +00:00
return table_data, status
2021-11-26 16:38:17 +00:00
end
2022-05-20 18:20:49 +00:00
file.writeJSON = function(filename,table_data)
assert(type(filename) == "string","string expected, got "..type(filename))
assert(type(table_data) == "table","table expected, got "..type(table_data))
local status = false
local status,json_object,_,err = pcall(function() return json.encode(table_data) end)
if not status then
if not file.safe then error(err) else
status = false
err = json_object
end
else
if json_object then
status,err = file.write(filename,json_object)
end
end
return status, err
2021-11-26 16:38:17 +00:00
end
end
return file