local cmp = require("component")
local srl = require("serialization")
local fs = require("filesystem")
local sh = require("shell")
local unicode = require("unicode")
local term = require("term")
local event = require("event")
local function write(quickfilename, cardname)
if not cmp.isAvailable("OSCardWriter") then
io.stderr:write("Отсуствует записывающее устройство.")
return
end
if quickfilename then
filename = quickfilename
end
local fileRAW = {}
local writer = cmp.OSCardWriter
local function askReadFrom()
io.write("Введите имя файла: ")
filename = term.read()
filename = unicode.sub(filename, 1, -2)
end
if not quickfilename then
askReadFrom()
end
for line in io.lines(filename) do
table.insert(fileRAW, line)
end
file = srl.serialize(fileRAW)
if unicode.len(file) > 128 then
io.stderr:write("Превышено ограничение по размеру файла.")
return
end
if cardname then
writer.write(file, cardname)
else
writer.write(file)
end
io.write("Карта записана успешно!\n")
end
local function read(quickfilename)
if not cmp.isAvailable("OSMagReader") then
io.stderr:write("Отсуствует картридер.")
return
end
if quickfilename then
saveto=quickfilename
end
io.write("Проведите картой...")
_, _, _, dataRAW = event.pull("magData")
io.write(" ОК!\n")
local data = {}
data = srl.unserialize(dataRAW)
local function askSaveTo()
io.write("Куда сохранить файл? ")
saveto = term.read()
saveto = unicode.sub(saveto, 1, -2)
if fs.exists(saveto) then
io.stderr:write("Файл существует. Введите другое имя.\n")
askSaveTo()
end
end
if not quickfilename then
askSaveTo()
end
local save = io.open(saveto, "w")
for _, dataFresh in pairs(data) do
save:write(dataFresh.."\n")
end
save:flush()
print("Файл сохранён!")
end
local function printUsage()
io.write("Использование: card <-rw> [файл] [имя]\n")
io.write(" r: расшифровать данные с карты\n")
io.write(" w: зашифровать данные на карту\n")
end
local args, options = sh.parse(...)
if options.r then
if args[1] then
read(args[1])
else
read()
end
elseif options.w then
if args[1] and args[2] then
write(args[1], args[2])
elseif args[1] and not args[2] then
write(args[1])
else
write()
end
else
printUsage()
end