Advertisement
guitarplayer616

Convert

Apr 21st, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.01 KB | None | 0 0
  1. local tArgs = { ... }
  2.  
  3. function printUsage()
  4.   print("Usage: convert toByteFile <Input file name> <Output file>")
  5.   print("Usage: convert toStringFile <gunzipped schematic file | Input file name> <Output file>")
  6. end
  7.  
  8. function convertToStringFile(inputFileName, outputFileName)
  9.   if not fs.exists(inputFileName) then
  10.     print("InputFile: " .. inputFileName .. " does not exist.")
  11.     printUsage()
  12.     return
  13.   end
  14.   if fs.exists(outputFileName) then
  15.     print("OutputFile: " .. outputFileName .. " does exist, please delete it before or choose another filename.")
  16.     printUsage()
  17.     return
  18.   end
  19.  
  20.   local inFile = fs.open(inputFileName, "rb")
  21.  
  22.   local byteArray = {}
  23.   local byte = 0
  24.   while byte ~= nil do
  25.     byte = inFile.read()
  26.     table.insert(byteArray, byte)
  27.   end
  28.  
  29.   outFile = fs.open(outputFileName, "w")
  30.   outFile.write(textutils.serialize(byteArray))
  31.   outFile.close()
  32. end
  33.  
  34. function convertToByteFile(inputFileName, outputFileName)
  35.  
  36.   if not fs.exists(inputFileName) then
  37.     print("InputFile: " .. inputFileName .. " does not exist.")
  38.     printUsage()
  39.     return
  40.   end
  41.   if fs.exists(outputFileName) then
  42.     print("OutputFile: " .. outputFileName .. " does exist, please delete it before or choose another filename.")
  43.     printUsage()
  44.     return
  45.   end
  46.  
  47.   local inFile = fs.open(inputFileName, "r")
  48.   local charArray = {}
  49.   charArray = textutils.unserialize(inFile.readAll())
  50.   inFile.close()
  51.  
  52.  
  53.   local outFile = fs.open(outputFileName, "wb")
  54.  
  55.   for _, byte in ipairs(charArray) do
  56.     outFile.write(byte)
  57.   end
  58.   outFile.close()
  59. end
  60.  
  61. function main()
  62.  
  63.   if #tArgs ~= 3 then
  64.     printUsage()
  65.     return
  66.   end
  67.  
  68.   if tArgs[2] == tArgs[3] then
  69.     print("inputFileName and outputFileName are the same, please correct that")
  70.     printUsage()
  71.     return
  72.   end
  73.  
  74.   if tArgs[1] == "toByteFile" then
  75.     convertToByteFile(tArgs[2], tArgs[3])
  76.   elseif tArgs[1] == "toStringFile" then
  77.     convertToStringFile(tArgs[2], tArgs[3])
  78.   else
  79.     printUsage()
  80.   end
  81. end
  82.  
  83.  
  84. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement