--[[ deck loading and saving functions (working with Recfiles) ]] local Card = dofile(SRC_DIR .. "card.lua") local recrw = dofile(SRC_DIR .. "recrw.lua") --[[ load a deck (a table of `Card`s) from a Recfile ]] local function loaddeck(fname, conf) local t0 = os.clock() local fh = io.open(fname) if not fh then print("no fh") return nil end local rt = fh:read "a" if not rt then print("no read") return nil end fh:close() local t1 = os.clock() local pr = recrw.read(rt) if not pr then print("no recs") return nil end local t2 = os.clock() local ep = Card:fromrecs(pr, conf) local t3 = os.clock() print(string.format("times (ms): %.1f load, %.1f recparse, %.1f cardparse", 1000 * (t1 - t0), 1000 * (t2 - t1), 1000 * (t3 - t2))) return ep end --[[ save a deck (a table of `Card`s) to a Recfile ]] local function savedeck(fname, deck) local rrf = {} local t0 = os.clock() for i, cg in ipairs(deck) do rrf[i] = cg:torec() end local t1 = os.clock() local wot = recrw.write(rrf) local t2 = os.clock() local fh = io.open(fname, "w") if not fh then io.write("couldn't open output\n") end local wr, err = fh:write(wot) if not wr then io.write("couldn't write to output\n") end fh:close() local t3 = os.clock() print(string.format("times (ms): %.1f recify, %.1f rectext, %.1f write", 1000 * (t1 - t0), 1000 * (t2 - t1), 1000 * (t3 - t2))) end return { load = loaddeck, save = savedeck }