Advertisement
promitheus_sal

conf.lua

Dec 5th, 2022 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.80 KB | None | 0 0
  1. --[[
  2. (C) PROMETHEUS TECHNLOGIES 2022
  3.  
  4. conf.lua v1.0
  5. Lua configuration file parser
  6. ]]
  7.  
  8. conf = {}
  9. local function loadStr(str)
  10.     local r = {}
  11.     for k,v in string.gmatch(str,"([%w_]+) *= *([;^]+);") do
  12.        r[k] = v
  13.     end
  14.     return r
  15. end
  16.  
  17. local function load(filename)
  18.     local f = io.open(filename,"r")
  19.     local s = f.read("*a")
  20.     f:close()
  21.     return loadStr(s)
  22. end
  23.  
  24. local function saveStr(l)
  25.     local r = ""
  26.     for k,v in pairs(l) do
  27.        r=r..string.format("%s=%s;\n",tostring(k),tostring(v))
  28.     end
  29.     return r
  30. end
  31.  
  32. local function save(filename,l)
  33.     local f = io.open(filename,"w")
  34.     f:write(saveStr(l))
  35.     f:flush()
  36.     f:close()
  37. end
  38.  
  39. conf.load = load
  40. conf.loadStr = loadStr
  41. conf.save = save
  42. conf.saveStr = saveStr
  43. conf.version = "1.0"
  44. return conf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement