Aadvert

lsave 0.5

Feb 10th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. -- lsave v0.5
  2. -- (C) 2012 User 'Advert' at http://www.computercraft.info/forums2/
  3. -- X11/mit licence (use/modify at your will, but please, leave credit where due)
  4.  
  5. -- 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.
  6. -- All functions return success, result/error message.
  7. --
  8. -- Currently only saves the following data types:
  9. -- string, number, boolean
  10. --
  11. -- Functions:
  12. -- local sucess, error = lsave.save(filename, table with data) -- does not return anything other than if it succeeded
  13. -- WARNING: lsave.save WILL OVERWRITE YOUR FILE.
  14. -- local sucess, result = lsave.load(filename) -- if success is true, then result should be a table with your data.
  15. --
  16. -- HOW 2 USE 4 DUMMIES
  17. --
  18. -- save as "lsave" on your computer/floppy, keep note of where it's saved.
  19. --
  20. -- To include it in your program, do this:
  21. -- local loadFunc, err = loadfile("path/to/lsave")
  22. -- if type(loadFunc) == "function" then
  23. --  lsave = loadFunc()
  24. -- else
  25. --  -- We has error! file not exist or whatever.
  26. --  error(err)
  27. -- end
  28. --
  29. --
  30.  
  31.  
  32. local lsave = {} -- will carry the public functions; loadfile does not like globals :/
  33. do
  34.  
  35.     function lsave.load(sFile) -- This function will check that lsave saved the file in the future.
  36.         if not fs.exists(sFile) then return false, "nofile" end -- We can't load a file that doesn't exist.
  37.         local hHandle = io.open(sFile, "r")
  38.         local err, res = pcall(function() return setfenv(loadfile(sFile),getfenv())() end)
  39.         if not err then
  40.             return false, res
  41.         end
  42.         return true, res
  43.     end
  44.  
  45.     -- Saving helper format strings/functions
  46.     lsave.types = {}
  47.     lsave.types.boolean = "data[%q] = %s"
  48.     lsave.types.string = "data[%q] = %q"
  49.     lsave.types.number = "data[%q] = %d"
  50.  
  51.     -- Saving function
  52.     function lsave.save(sFile, data)
  53.         if type(sFile) ~= "string" or type(data) ~= "table" then
  54.             return false, "Invalid arguments"
  55.         end
  56.         local sData = "local data = {} -- This file, generated by lsave 0.5. Edit at your own risk.\n\n"
  57.         for k, v in pairs(data) do
  58.             local sDataType = type(v)
  59.             local saveHelper = lsave.types[sDataType]
  60.             if not saveHelper then
  61.                 -- We have problem, let's just print it, and continue without saving it.
  62.                 print("Warning: (saveToFile) unknown data type (ignoring), in key: " .. k)
  63.             else
  64.                 -- No errors, save it.
  65.                 if type(saveHelper) == "string" then
  66.                     sData = sData .. string.format(saveHelper, k, tostring(v)) .. "\n"
  67.                 elseif type(saveHelper) == "function" then -- For the future, and custom stuff.
  68.                     sData = sData .. saveHelper(k, v) .. "\n"
  69.                 end
  70.             end
  71.         end
  72.         sData = sData .. "\nreturn data" -- We need to return the config in order to retrieve it.
  73.         -- Delete and save.
  74.         local hHandle, err = io.open(sFile, "w")
  75.         if not hHandle then
  76.             return false, err
  77.         end
  78.         hHandle:write(sData)
  79.         hHandle:close()
  80.         return true
  81.     end
  82. end
  83.  
  84. return lsave -- This, so you can do "lsave = loadfile("lsave")()"
  85. -- Remove the line if you want to just paste it in your program, instead.
  86. -- 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 = {}.
  87.  
  88.  
  89. -- Testing code (you will need to use interpreter to fill the fridge):
  90. -- in 'lua':
  91. -- lsave = loadfile("lsave")() -- not checking for errors since interpreter will spew
  92. -- fridge = {}
  93. -- fridge.stuff = 42
  94. -- fridge.otherstuff = false
  95. -- --etc.
  96. -- -- then, call
  97. -- lsave.save("fridge-data", fridge)
  98. -- -- you should then see "true" printed.
  99. -- exit() -- out of interpreter, then use fridge-test:
  100. -- then, load this program (call it fridge-test, or whatever.):
  101.  
  102. --[[
  103. local loadFunc, err = loadfile("lsave")
  104.  
  105. if type(loadFunc) == "function" then
  106.  lsave = loadFunc()
  107. else
  108.  error(err)
  109. end
  110.  
  111. local empty = false
  112. local suc, fridge = lsave.load("fridge-data")
  113. if not suc then
  114.     if fridge == "nofile" then
  115.         print("Fridge is empty :(")
  116.         empty = true
  117.     else
  118.         error("Error:" .. tostring(fridge))
  119.     end
  120. end
  121. if not empty then
  122.  
  123.     print("Fridge Contents: ")
  124.     for k, v in pairs(fridge) do
  125.         print(k .. ": " .. tostring(v))
  126.     end
  127. end
  128.  
  129.  
  130. --]]
Advertisement
Add Comment
Please, Sign In to add comment