SergOmarov

Card Worker

Aug 4th, 2015
248
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --идея - Programist135
  2. --реализация - SergOmarov,Programist135
  3. local cmp = require("component")
  4. local srl = require("serialization")
  5. local fs = require("filesystem")
  6. local sh = require("shell")
  7. local unicode = require("unicode")
  8. local term = require("term")
  9. local event = require("event")
  10.  
  11. card = {}
  12.  
  13. function card.writeData(data, cardname)
  14.     if not cmp.isAvailable("OSCardWriter") then
  15.         return "Writer not found"
  16.     end
  17.     local writer = cmp.OSCardWriter
  18.     local dataRAW = srl.serialize(data)
  19.     if unicode.len(dataRAW) > 128 then
  20.         return "Data too big"
  21.     end
  22.     writer.write(dataRAW, cardname)
  23.     return true
  24. end
  25.  
  26. function card.read(saveto)
  27.     local data = {}
  28.     if(saveto==nil)then
  29.         local _, _, _, dataRAW = event.pull("magData")
  30.         return srl.unserialize(dataRAW)
  31.     else
  32.         local save = io.open(saveto, "w")
  33.         if not cmp.isAvailable("OSMagReader") then
  34.             return "Reader not found"
  35.         end
  36.         local _, _, _, dataRAW = event.pull("magData")
  37.         data = srl.unserialize(dataRAW)
  38.         for _, dataFresh in pairs(data) do
  39.             save:write(dataFresh.."\n")    
  40.         end
  41.         save:flush()
  42.         return true
  43.         end
  44. end
  45.  
  46. function card.write(filename, cardname)
  47.     if not cmp.isAvailable("OSCardWriter") then
  48.         return "Writer not found"
  49.     end
  50.     local writer = cmp.OSCardWriter
  51.     local dataRAW = {}
  52.     for line in io.lines(filename) do
  53.         table.insert(dataRAW, line)
  54.     end
  55.     local data = srl.serialize(dataRAW)
  56.     if unicode.len(data) > 128 then
  57.         return "File too big"
  58.     end
  59.     writer.write(data, cardname)
  60.     return true
  61. end
  62.  
  63. return card
RAW Paste Data