john9912

fs Alternative

Jan 24th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.80 KB | None | 0 0
  1. open = function(name)
  2.     local r
  3.     if name then
  4.         r = fs.open(name,"r")
  5.     end
  6.     local a = {}
  7.     local i = 1
  8.     for line in r.readLine do
  9.         a[i] = line
  10.         i = i + 1
  11.     end
  12.     a.readHandle = r
  13.     a.fileName = name
  14.     function a:close()
  15.         for i = 1,#self do
  16.             self[i] = nil
  17.         end
  18.         self["close"] = nil
  19.         self["save"] = nil
  20.         self["fileName"] = nil
  21.         self["readHandle"] = nil
  22.     end
  23.     function a:update()
  24.         if self.fileName then
  25.             local r = fs.open(self.fileName,"r")
  26.             local i = 1
  27.             for line in r.readLine do
  28.                 self[i] = line
  29.                 i = i + 1
  30.             end
  31.         end
  32.     end
  33.     function a:save()
  34.         if not self.fileName then
  35.             return false
  36.         end
  37.         local w = fs.open(self.fileName,"w")
  38.         for i=1,#self do
  39.             if self[i] then
  40.                 w.writeLine(self[i])
  41.             end
  42.         end
  43.         w.close()
  44.     end
  45.     if r then
  46.         return a
  47.     end
  48. end
Add Comment
Please, Sign In to add comment