Advertisement
Stiepen

config API

Sep 15th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. --[[
  2.     This code is licensed with CreativeCommons BY-NC-SA:
  3.     http://creativecommons.org/licenses/by-nc-sa/3.0/
  4.     You are free to change and redistribut this as long as you give credit to me, the author,
  5.     Stiepen/Kilobyte.
  6.     You also need to distribute your code but you have to use the same license
  7.     Exceptions from this rule are parts of the code that are taken from the regular CC files, since i don't own the rights For them
  8. --]]
  9.  
  10. local function loadImpl(tLines, pos)
  11.   local ret = {}
  12.   while tLines[pos] do
  13.     local l = tLines[pos]
  14.     pos = pos + 1
  15.     if string.sub(l, 1, 1) == "#" then
  16.       --the line is a comment
  17.     elseif string.find(l, "=") then
  18.       local eqpos = string.find(l, "=")
  19.       local key, val
  20.       key = string.sub(l, 1, eqpos-1)
  21.       val = string.sub(l, eqpos + 3)
  22.       sType = string.sub(l, eqpos + 1, eqpos + 1)
  23.       if sType == "s" then
  24.         ret[key] = val
  25.       elseif sType == "n" then
  26.         ret[key] = tonumber(val)
  27.       elseif sType == "b" then
  28.         ret[key] = (val == "true")
  29.       end
  30.     elseif string.sub(l, -2, -1) == "[[" then
  31.       ret[string.sub(l, 1, -3)], pos = loadImpl(tLines, pos)
  32.     elseif l == "]]" then
  33.       break
  34.     elseif l == "" then
  35.     else
  36.       error("Invalid Config File")
  37.     end
  38.   end
  39.   return ret, pos
  40. end
  41.  
  42. local function readAsTable(_sFile, _fs)
  43.   local f = _fs.open(_sFile, "r")
  44.   local ret = {}
  45.   while true do
  46.     local line = f.readLine()
  47.     if not line then break end
  48.     table.insert(ret, line)
  49.   end
  50.   f.close()
  51.   return ret
  52. end
  53. --[[
  54.     Loads a config file
  55.    
  56.     @param _sFile The name of the file where the config should be loaded from.
  57.     Optional @param _fs a file system to use. leave empty or set to nil if you don't know what this means
  58.    
  59.     @returns a table containing the config info
  60. --]]
  61. function load(_sFile, _fs)
  62.   if not _fs then _fs = fs end
  63.   if not _fs.exists(_sFile) and not _fs.isReadOnly(_sFile) then local f = _fs.open(_sFile, "w") print(f) f.close() end
  64.   if not _fs.isDir(_sFile) then
  65.     local lines = readAsTable(_sFile, _fs)
  66.     local ret, pos = loadImpl(lines, 1)
  67.     return ret
  68.   end
  69.   print("Found you!")
  70.   return nil, "couldn't load config"
  71. end
  72.  
  73. local function saveImpl(tIn)
  74.   local ret = ""
  75.   for i, v in pairs(tIn) do
  76.     if string.find(i, "=") then
  77.       error("config option key must not contain '='")
  78.     end
  79.     if string.sub(i, 1, 1) == "#" then
  80.       error("key must not start with '#'")
  81.     end
  82.     sType = type(v)
  83.     if sType == "string" then
  84.       ret = ret..i.."=s:"..v.."\n"
  85.     elseif sType == "number" then
  86.       ret = ret..i.."=n:"..v.."\n"
  87.     elseif sType == "boolean" then
  88.       local v_ = "false" if v then v_ = "true" end
  89.       ret = ret..i.."=b:"..v_.."\n"
  90.     elseif sType == "table" then
  91.       ret = ret..i.."[[\n"
  92.       ret = ret..saveImpl(v).."]]\n"
  93.     end
  94.   end
  95.   return ret
  96. end
  97.  
  98. --[[
  99.     Saves config options from a table to a file
  100.    
  101.     @param _sFile The filename/path
  102.     @param _tCfg The config option table to write
  103.     @_tComments no effect yet, set to nil or leave out
  104.     Optional @param _fs The filesystem to use. see above
  105. --]]
  106. function save(_sFile, _tCfg, _tComments, _fs)
  107.   if not _fs then _fs = fs end
  108.   local data = saveImpl(_tCfg)
  109.   local f = _fs.open(_sFile, "w")
  110.   f.write(data)
  111.   f.close()
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement