Advertisement
willwac

File API

Oct 3rd, 2013
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. function get( code, file )
  2.     if not http then
  3.         return false, "no_http"
  4.     end
  5.     resp = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode( code ))
  6.     if resp then
  7.         handler = io.open(file,"w")
  8.         handler:write(resp.readAll())
  9.         handler:close()
  10.         return true, "success"
  11.     else
  12.         return false, "fail"
  13.     end
  14. end
  15.  
  16. function getRaw( url, file )
  17.     if not http then
  18.         return false, "no_http"
  19.     end
  20.     resp = http.get(textutils.urlEncode( url ))
  21.     if resp then
  22.         handler = io.open(file,"w")
  23.         handler:write(resp.readAll())
  24.         handler:close()
  25.         return true, "success"
  26.     else
  27.         return false, "fail"
  28.     end
  29. end
  30.  
  31. function append( file, text, w )
  32.     -- w = true if you don't want to add an end of line character
  33.     local handler = fs.open( file, "a" )
  34.     if not w then
  35.         handler.writeLine( text )
  36.         handler.close()
  37.         return true, "append_line"
  38.     else
  39.         handler.write( text )
  40.         handler.close()
  41.         return true,"append"
  42.     end
  43. end
  44.  
  45. function read( file, all )
  46.     -- all = true if you want to load all of the file's content.
  47.     local handler = fs.open( file, "r")
  48.     if not all then
  49.         local line = handler.readLine()
  50.         handler.close()
  51.     else
  52.         local line = handler.readAll()
  53.         handler.close()
  54.     end
  55.     return true, line
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement