Advertisement
ecoMeco

newLibCFG

Aug 31st, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.91 KB | None | 0 0
  1. -- newLibCFG 1.0.0 by Admicos.
  2. --
  3. -- table keys, [table comments, number line count] if not simple readConfigRaw(string configStr, bool simple)
  4. --     the heart of the API. Will parse the configStr.
  5. --
  6. -- table keys, [table comments, number line count] if not simple readConfig(string configFile, bool simple)
  7. --     file wrapper for readConfigRaw
  8. --
  9. -- nil writeConfig(file, keys, comments, len)
  10. --     writes keys/comments to config, must be used without simple
  11. --
  12. -- --
  13. --
  14. -- if you are NOT writing to a file, it's recommended to run the readConfig() or
  15. -- readConfigRaw() functions with the second argument "true", meaning "simple" as
  16. -- it will just return the table similar to the original libCFG, shown below
  17. --
  18. -- "simple" or original libCFG:
  19. --     table["key"] = value
  20. --
  21. -- newer approach, recommended for file modification
  22. --     table[linenumber][1] = key
  23. --     table[linenumber][2] = value
  24.  
  25. local function _split(str, splitter)
  26.     local t = {}
  27.     local function helper(line) table.insert(t, line) return "" end
  28.         helper((str:gsub("(.-)" .. splitter, helper)))
  29.     return t
  30. end
  31.  
  32. local function _trim(s)
  33.     return (s:gsub("^%s*(.-)%s*$", "%1"))
  34. end
  35.  
  36. function readConfigRaw(string, simple)
  37.     local comments  = {}
  38.     local keys      = {}
  39.  
  40.     local stringAsLines = _split(string, "\n")
  41.     local line          = ""
  42.  
  43.     for i = 1, #stringAsLines do
  44.         line = _trim(stringAsLines[i])
  45.  
  46.         if line:sub(1, 1) == "#" then --Line is a comment
  47.             if not simple then comments[i] = line:sub(2, #line) end
  48.         elseif line ~= "" then --Line is not a comment nor empty.
  49.             local l = _split(line, ":")
  50.  
  51.             if #l <= 1 then error("File format is incorrect") end
  52.  
  53.             local k = table.remove(l, 1)
  54.             local v = _trim(table.concat(l, ":"))
  55.  
  56.             if simple then keys[_trim(k)] = v
  57.             else           keys[i] = {_trim(k), v}
  58.             end
  59.         end
  60.     end
  61.  
  62.     line = nil
  63.  
  64.     if simple then return keys
  65.     else return keys, comments, #stringAsLines
  66.     end
  67. end
  68.  
  69. function readConfig(file, simple)
  70.     local file = fs.open(file, "r")
  71.         if file == nil then error("Couldn't open file") end
  72.  
  73.         local parsed = {readConfigRaw(file.readAll(), simple or false)}
  74.     file.close()
  75.  
  76.     return table.unpack(parsed)
  77. end
  78.  
  79. function writeConfig(file, keys, comments, len)
  80.     local w_file = fs.open(file, "w")
  81.         if w_file == nil then error("Couldn't open file") end
  82.         for i = 1, #file + #comments do
  83.             if comments[i] then w_file.writeLine("#" .. comments[i])
  84.             elseif keys[i] then w_file.writeLine(keys[i][1] .. ": " .. keys[i][2])
  85.             else w_file.writeLine()
  86.             end
  87.         end
  88.     w_file.close()
  89. end
  90.  
  91. local function simple_test()
  92.     local keys = readConfigRaw([[
  93.     #a
  94.     #a:b
  95.     hello: world
  96.     ]], true)
  97.  
  98.     for key, value in pairs(keys) do
  99.         stringStuff.printWithFormat("&8" .. key .. "&5 = &0" .. value)
  100.     end
  101. end
  102.  
  103. local function test()
  104.     local keys, comments, len = readConfigRaw([[
  105.     #newlibCFG Example config
  106.     #lines starting with hashes are comments
  107.  
  108.     #Here is an example config entry
  109.     key: value
  110.  
  111.     #Here is an example config that could be used in a program
  112.     saveDirectory: /program/saves
  113.     saveInterval: 10
  114.  
  115.     #Check the forum post for usage.
  116.     ]])
  117.  
  118.     for i = 1, len do
  119.         if comments[i] then stringStuff.printWithFormat("&d#" .. comments[i])
  120.         elseif keys[i] then stringStuff.printWithFormat("&8" .. keys[i][1] .. "&5 = &0" .. keys[i][2])
  121.         else print()
  122.         end
  123.     end
  124.  
  125.     print("Trying to write to 'test.cfg'")
  126.     writeConfig(shell.resolve("test.cfg"), keys, comments, len)
  127. end
  128.  
  129. if shell ~= nil then --not loaded as API, so run test.
  130.     local a = { ... }
  131.     if #a == 1 and a[1] == "simple" then simple_test()
  132.     else                                        test()
  133.     end
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement