Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function table.save(t, filename)
- local dir = getprofilepath()
- local file = io.open(dir .. "\\data\\" .. filename, "w")
- local spaces = 0
- local function tab()
- local str = ""
- for i = 1,spaces do
- str = str .. " "
- end
- return str
- end
- local function format(t)
- spaces = spaces + 4
- local str = "{ "
- for k,v in pairs(t) do
- -- Key datatypes
- if type(k) == "string" then
- k = string.format("%q", k)
- end
- -- Value datatypes
- if type(v) == "string" then
- v = string.format("%q", v)
- elseif v == math.inf then
- v = "1 / 0"
- end
- if type(v) == "table" then
- if table.len(v) > 0 then
- str = str .. "\n" .. tab() .. "[" .. k .. "] = " .. format(v) .. ","
- else
- str = str .. "\n" .. tab() .. "[" .. k .. "] = {},"
- end
- else
- str = str .. "\n" .. tab() .. "[" .. k .. "] = " .. tostring(v) .. ","
- end
- end
- spaces = spaces - 4
- return string.sub(str, 1, string.len(str) - 1) .. "\n" .. tab() .. "}"
- end
- file:write("return " .. format(t))
- file:close()
- end
- function table.load(filename)
- local dir = getprofilepath()
- local file = loadfile(dir .. "\\data\\" .. filename)
- if file then
- return file()
- end
- return {}
- end
- function table.len(t)
- local count = 0
- for k,v in pairs(t) do
- count = count + 1
- end
- return count
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement