Advertisement
Guest User

pref.lua

a guest
Jun 14th, 2015
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.99 KB | None | 0 0
  1. --by 1Ridav
  2. ---http://computercraft.ru
  3. --PWD set full path /mnt/1a2/
  4. --local PWD = "" --uncomment this if use dofile()
  5. local PWD = os.getenv("PWD") --comment this if use dofile()
  6. local pref = {}
  7.  
  8. function pref.create(path)
  9.   local table = {}
  10.   table["_FullPreferencesPath"] = PWD..path
  11.   return table
  12. end
  13.  
  14. function pref.load(path)
  15.   path = PWD..path
  16.   local table = {}
  17.   local key, value, delimeter
  18.   local len = 0
  19.  
  20.   local f = io.open(path, "r")
  21.   if not f then return nil end
  22.  
  23.   for line in f:lines() do
  24.     len = len + 1
  25.     delimeter = string.find(line, "=")
  26.     key = string.sub(line, 1, delimeter-1)
  27.     value = string.sub(line, delimeter+1, #line)
  28.     table[key] = value
  29.   end
  30.  
  31.   f:close()
  32.  
  33.   if len == 0 then return nil end
  34.  
  35.   table["_FullPreferencesPath"] = path
  36.   return table
  37. end
  38.  
  39. function pref.save(table)
  40.   local f = io.open(table["_FullPreferencesPath"], "w")
  41.   if not f then return false end
  42.   for k, v in pairs(table) do
  43.     f:write(k.."="..v.."\n")
  44.   end
  45.   f:close()
  46.   return true
  47. end
  48.  
  49. return pref
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement