Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- lsave v0.5
- -- (C) 2012 User 'Advert' at http://www.computercraft.info/forums2/
- -- X11/mit licence (use/modify at your will, but please, leave credit where due)
- -- This is from a project I'm working on, and I think it'd be useful for others, so I'm making it a snippet.
- -- All functions return success, result/error message.
- --
- -- Currently only saves the following data types:
- -- string, number, boolean
- --
- -- Functions:
- -- local sucess, error = lsave.save(filename, table with data) -- does not return anything other than if it succeeded
- -- WARNING: lsave.save WILL OVERWRITE YOUR FILE.
- -- local sucess, result = lsave.load(filename) -- if success is true, then result should be a table with your data.
- --
- -- HOW 2 USE 4 DUMMIES
- --
- -- save as "lsave" on your computer/floppy, keep note of where it's saved.
- --
- -- To include it in your program, do this:
- -- local loadFunc, err = loadfile("path/to/lsave")
- -- if type(loadFunc) == "function" then
- -- lsave = loadFunc()
- -- else
- -- -- We has error! file not exist or whatever.
- -- error(err)
- -- end
- --
- --
- local lsave = {} -- will carry the public functions; loadfile does not like globals :/
- do
- function lsave.load(sFile) -- This function will check that lsave saved the file in the future.
- if not fs.exists(sFile) then return false, "nofile" end -- We can't load a file that doesn't exist.
- local hHandle = io.open(sFile, "r")
- local err, res = pcall(function() return setfenv(loadfile(sFile),getfenv())() end)
- if not err then
- return false, res
- end
- return true, res
- end
- -- Saving helper format strings/functions
- lsave.types = {}
- lsave.types.boolean = "data[%q] = %s"
- lsave.types.string = "data[%q] = %q"
- lsave.types.number = "data[%q] = %d"
- -- Saving function
- function lsave.save(sFile, data)
- if type(sFile) ~= "string" or type(data) ~= "table" then
- return false, "Invalid arguments"
- end
- local sData = "local data = {} -- This file, generated by lsave 0.5. Edit at your own risk.\n\n"
- for k, v in pairs(data) do
- local sDataType = type(v)
- local saveHelper = lsave.types[sDataType]
- if not saveHelper then
- -- We have problem, let's just print it, and continue without saving it.
- print("Warning: (saveToFile) unknown data type (ignoring), in key: " .. k)
- else
- -- No errors, save it.
- if type(saveHelper) == "string" then
- sData = sData .. string.format(saveHelper, k, tostring(v)) .. "\n"
- elseif type(saveHelper) == "function" then -- For the future, and custom stuff.
- sData = sData .. saveHelper(k, v) .. "\n"
- end
- end
- end
- sData = sData .. "\nreturn data" -- We need to return the config in order to retrieve it.
- -- Delete and save.
- local hHandle, err = io.open(sFile, "w")
- if not hHandle then
- return false, err
- end
- hHandle:write(sData)
- hHandle:close()
- return true
- end
- end
- return lsave -- This, so you can do "lsave = loadfile("lsave")()"
- -- Remove the line if you want to just paste it in your program, instead.
- -- I think you should be able to convert this to an API, if you remove lsave. from function names (and lsave.types), and remove lsave = {}.
- -- Testing code (you will need to use interpreter to fill the fridge):
- -- in 'lua':
- -- lsave = loadfile("lsave")() -- not checking for errors since interpreter will spew
- -- fridge = {}
- -- fridge.stuff = 42
- -- fridge.otherstuff = false
- -- --etc.
- -- -- then, call
- -- lsave.save("fridge-data", fridge)
- -- -- you should then see "true" printed.
- -- exit() -- out of interpreter, then use fridge-test:
- -- then, load this program (call it fridge-test, or whatever.):
- --[[
- local loadFunc, err = loadfile("lsave")
- if type(loadFunc) == "function" then
- lsave = loadFunc()
- else
- error(err)
- end
- local empty = false
- local suc, fridge = lsave.load("fridge-data")
- if not suc then
- if fridge == "nofile" then
- print("Fridge is empty :(")
- empty = true
- else
- error("Error:" .. tostring(fridge))
- end
- end
- if not empty then
- print("Fridge Contents: ")
- for k, v in pairs(fridge) do
- print(k .. ": " .. tostring(v))
- end
- end
- --]]
Advertisement
Add Comment
Please, Sign In to add comment