Advertisement
MarkFergus

/lib/fileHandler

Sep 1st, 2020 (edited)
1,575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.20 KB | None | 0 0
  1. function vn(val) return (val ~= nil) end
  2.  
  3. function update(file_path,input)
  4.  
  5.     local file_path = tostring(file_path)
  6.    
  7.     if (vn(file_path) and vn(input)) then
  8.         file = fs.open(file_path,"w")
  9.         file.flush()
  10.         file.write(textutils.serialise(input))
  11.         file.close()
  12.     else
  13.         print("Usage : fileHandler.update( <file_path>, <code> )")
  14.     end
  15.    
  16. end
  17. -- flush the data inside the file to replace it with the new input
  18.  
  19. function load(file_path,type,code)
  20.  
  21.     local file_path = tostring(file_path)
  22.    
  23.     local new_file_created = false
  24.    
  25.     if not fs.exists(file_path) then
  26.         if type == "pastebin" then
  27.             shell.run("/dpaste","1",code,file_path)
  28.             new_file_created = true
  29.         end
  30.         if type == "default" then
  31.             f1 = fs.open(file_path,"w")
  32.             f1.write(textutils.serialise(code))
  33.             f1.close()
  34.             new_file_created = true
  35.         end
  36.     end
  37.    
  38.     f1 = fs.open(file_path,"r")
  39.     output = textutils.unserialise(f1.readAll())
  40.     f1.close()
  41.     return output, new_file_created
  42.  
  43. end
  44. -- usage : table = fileHandler.load( <file_path> ,  <"default" or "pastebin">, code)
  45. -- "default" writes the sent table (code) in the file created with 'file_path'
  46. -- "pastebin" send a request to the paste (code) and writes the returned text in the file.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement