Greygraphics

Lua file packer

May 12th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.23 KB | None | 0 0
  1. --Setting everything up
  2. local args = {...} --   Read arguments
  3. local target --         Create reference to targte file
  4. local contents --       Create array for new lines to be written
  5.  
  6. local function rFile(file) --   Return an array with the contents of the file
  7.     local f = fs.open(file,"r")
  8.     local ln = ""
  9.     local ret = {}
  10.     repeat
  11.         ln = f.readLine()
  12.         ret[#ret+1] = ln
  13.     until ln == nil
  14.     f.close()
  15.     return ret
  16. end
  17.  
  18. -- Execute --
  19. contents = {"--[["} --  The contents of each file will be written into a comment
  20. target = fs.open(args[1],((fs.exists(args[1]) and "a")) or ("w")) --    Open target file in either write (not existing) or append (existing) mode
  21.  
  22. for i=2,#args,1 do
  23.     local file = rFile(args[i]) --      Read the file into an array
  24.     contents[#contents+1] = "NEWFILE" --Create header for the file
  25.     contents[#contents+1] = args[i] --  Write file name
  26.    
  27.     for j=1,#file,1 do --   Write file contents into array of added lines
  28.         contents[#contents+1] = file[j]
  29.     end
  30.     contents[#contents+1] = "EOF" --    Create marker for eof
  31.     print("Added "..args[i])
  32. end
  33.  
  34. contents[#contents+1] = "--]]" --       Close comment in added lines
  35.  
  36. for i=1,#contents,1 do --   Write actual data to the target file
  37.     target.writeLine(contents[i])
  38. end
  39.  
  40. target.close()
  41. print("Done")
Add Comment
Please, Sign In to add comment