Advertisement
9551

data storage API

Jul 1st, 2021 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. local function getLine(files, line)
  2.     local l = 1
  3.     local out = {}
  4.     local file = fs.open(files, "r")
  5.     repeat
  6.         out[l] = file.readLine()
  7.         l = l + 1
  8.     until not out[l - 1]
  9.     file.close()
  10.     return out[line]
  11. end
  12. local function writeLine(files, line, data, overhead)
  13.     local l = 1
  14.     local alldata = {}
  15.     while getLine(files, l) do
  16.         alldata[l] = getLine(files, l)
  17.         l = l + 1
  18.     end
  19.     alldata[line] = data
  20.     local file = fs.open(files, "w")
  21.     for i = 1, #alldata + line + (overhead or 1) do
  22.         file.writeLine(alldata[i])
  23.     end
  24.     file.close()
  25. end
  26. local function clear(file)
  27.     fs.open(file,"w").close()
  28. end
  29. local function length(tbl)
  30.     local biggest = 0
  31.     for k in pairs(tbl) do
  32.         if type(k) == "number" then
  33.             biggest = math.max(biggest, k)
  34.         end
  35.     end
  36.     return biggest
  37. end
  38. local function ser(ins)
  39.     local out = {}
  40.     local start = 1
  41.     for i = 1, length(ins) do
  42.         if ins[i] ~= nil then
  43.             out[start] = ins[i]
  44.             start = start + 1
  45.         end
  46.     end
  47.     return out
  48. end
  49. return {
  50.     getLine = getLine,
  51.     writeLine = writeLine,
  52.     clear = clear,
  53.     length = length,
  54.     ser = ser
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement