gemwriter/util.lua

48 lines
968 B
Lua

local util = {}
function util.split_str(str, sep)
local tbl = {}
local n = 0
if sep == nil then sep = "%S+" end
for sub in string.gmatch(str, sep) do
n = n + 1
tbl[n] = sub
end
return tbl
end
function util.extract_str(full_str, find_str, end_str)
fi1, fi2 = string.find(full_str, find_str, 1)
ei1, ei2 = string.find(full_str, end_str, fi2)
return string.sub(full_str, fi2 + 1, ei1 - 1)
end
function util.extract_file_date(dir, file)
-- Atom feed date format: 2022-01-15T00:00:00+00:00
local ls_cmd = io.popen("ls -l --time-style=\"+%Y-%m-%dT%H:%M:%S%z\" " ..
dir .. " | grep " .. file .. " | awk '{print $6}'")
return ls_cmd:read("*a")
end
function util.read_file(file)
local fh = io.open(file, "r")
local text = ""
io.input(fh)
text = io.read("*a")
io.close(fh)
return text
end
function util.write_file(file, str)
local fh = io.open(file, "w")
io.output(fh)
io.write(str)
io.close(fh)
end
return util