Advertisement
nezd

TapeFS

Dec 10th, 2016
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.98 KB | None | 0 0
  1. print("TapeFS v0.1 by OctoNezd(a.k.a. Nezd)")
  2. tape = require("component").proxy(require("component").list("tape")())
  3. shell = require("shell")
  4. u = require("unicode")
  5. beep = require("computer").beep
  6. function verbprint(a)
  7.     if verbosity == true then
  8.         print("D:" .. a)
  9.     end
  10. end
  11. function read(count)
  12.     verbprint(string.format("Reading %s symbols", count))
  13.     a = ""
  14.     for i = 1, count do
  15.         print(string.format("I:Symbol num %s/%s", i, count))
  16.         b = tape.read()
  17.         if b == 0 then
  18.             verbprint("Found end symbol. Seeking to next position.")
  19.             tape.read(count - i)
  20.             return a
  21.         end
  22.         verbprint(string.format("Byte value %s. Unicode value:%s", b, u.char(b)))
  23.         a = a .. u.char(b)
  24.     end
  25.     return a
  26. end
  27. args, ops = shell.parse(...)
  28. if ops["h"] == true then
  29.     print(" -h Displays this message")
  30.     print(" --save=path Save file from path to tape")
  31.     print(" -l Loads file from tape")
  32.     print(" --dump=path Dumps file from tape to path")
  33.     print(" -v Enable verbosity")
  34.     print(" -f Disables questions")
  35. end
  36. if tape.isReady() == false then
  37.     print("E:No tape inserted")
  38.     os.exit()
  39. end
  40. print("I:Tape found.")
  41. if next(ops) == nil then
  42.     print("E:No arguments passed\nType 'tapefs -h' for help")
  43. end
  44. if ops["v"] == true then
  45.     verbosity = true
  46. end
  47. if ops["f"] == true then
  48.     force = true
  49. else
  50.     force = false
  51. end
  52. if verbosity == true then
  53.     print(ops["s"])
  54.     print(ops["l"])
  55. end
  56. if ops["l"] == true then
  57.     tape.seek(-999999999999999)
  58.     print("I:Checking tape...")
  59.     if read(6) == "TAPEFS" then
  60.         print("I:Checktape passed OK. Loading file...")
  61.     else
  62.         print("E:Checktape NOT passed. Did you inserted right tape?")
  63.         os.exit()
  64.     end
  65.     size = read(20)
  66.     print(string.format("I:File size is %s symbols", size))
  67.     tape.setSpeed(2.0)
  68.     code = read(tonumber(size))
  69.     beep(1500,0.5)
  70.     os.sleep(0.5)
  71.     beep(1500,0.5)
  72.     if force == false then
  73.         print("I:Code is loaded. Press enter to start it, or hit ^C to cancel loading")
  74.         io.read()
  75.     end
  76.     print("I:Loading code!")
  77.     verbprint("Rewiding tape.")
  78.     tape.seek(-999999999999999)
  79.     load(code)()
  80.  
  81.     os.exit()
  82. end
  83. if ops["save"] ~= nil then
  84.     if ops["save"] == true then
  85.         print("E:No file passed in arguments!")
  86.         os.exit()
  87.     else
  88.         tape.seek(-999999999999999)
  89.         f = io.open(ops["save"], "r")
  90.         if f == nil then
  91.             print("E:Non-existing file passed")
  92.         end
  93.         contents = f:read("*all*")
  94.         size = string.len(contents)
  95.         if string.len(size) > 20 then
  96.             print("E:File size is too big for TapeFS")
  97.         else
  98.             verbprint("Writing validator")
  99.             tape.write("TAPEFS")
  100.             verbprint("Writing size data")
  101.             tape.write(tostring(size))
  102.             --Seek space left
  103.             tape.seek(20 - string.len(size))
  104.             verbprint(string.format("Space left in TFS data:%s", 20-string.len(size)))
  105.             print("I:Writing...")
  106.             tape.write(contents)
  107.             print("I:Writed data. Validating")
  108.             tape.seek(-9999999999999)
  109.             if read(6) == "TAPEFS" then
  110.                 print("I:Succesfully validated! Setting label...")
  111.                 tape.setLabel("[TAPEFS]" .. ops["save"])
  112.                 print("I:Done writing.")
  113.                 os.exit()
  114.             else
  115.                 print("E:Cant validate tape! Looks like there is no enough space on it!")
  116.                 os.exit()
  117.             end
  118.         end
  119.     end
  120. end
  121. if ops["dump"] ~= nil then
  122.     if ops["dump"] == true then
  123.         print("E:No file passed in arguments!")
  124.     else
  125.         tape.seek(-9999999999999)
  126.         print("I:Checking tape")
  127.         if read(6) == "TAPEFS" then
  128.             print("I:Checktape passed OK. Loading file...")
  129.         else
  130.             print("E:Checktape NOT passed. Did you inserted right tape?")
  131.             os.exit()
  132.         end
  133.         size = read(20)
  134.         print(string.format("I:File size is %s symbols", size))
  135.         tape.setSpeed(2.0)
  136.         code = read(tonumber(size))
  137.         tape.seek(-9999999999999)
  138.         f = io.open(ops["dump"], "w")
  139.         io.output(f)
  140.         io.write(code)
  141.         io.close(f)
  142.         print("I:Dump:Success")
  143.     end
  144. end
  145. if ops["h"] == true then
  146.     print(" -h Displays this message")
  147.     print(" --save=path Save file from path to tape")
  148.     print(" -l Loads file from tape")
  149.     print(" --dump=path Dumps file from tape to path")
  150.     print(" -v Enable verbosity")
  151.     print(" -f Disables questions")
  152. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement