Advertisement
Wyvern67

Untitled

Jul 22nd, 2015
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.37 KB | None | 0 0
  1. --Settings File API used to parse and interpret and save settings files.
  2. --Entirely created by bwhodle
  3. --Forum post: http://www.computercraft.info/forums2/index.php?/topic/14311-preferences-settings-configuration-store-them-all-settings-file-api/
  4. local function trimComments(line)
  5.     local commentstart = string.len(line)
  6.     for i = 1, string.len(line) do
  7.         if string.byte(line, i) == string.byte(";") then
  8.             commentstart = i - 1
  9.             break
  10.         end
  11.     end
  12.     return string.sub(line, 0, commentstart)
  13. end
  14. local function split(line)
  15.     local equalssign = nil
  16.     for i = 1, string.len(line) do
  17.         if string.byte(line, i) == string.byte("=") or string.byte(line, i) == string.byte(":") then
  18.             equalssign = i - 1
  19.         end
  20.     end
  21.     if equalssign == nil then
  22.         return nil, nil
  23.     end
  24.     return string.sub(line, 1, equalssign - 1), string.sub(line, equalssign + 2)
  25. end
  26. function Trim(s)
  27.     return (s:gsub("^%s*(.-)%s*$", "%1"))
  28. end
  29. local function RemoveQuotes(s)
  30.     if string.byte(s, 1) == string.byte("\"") and string.byte(s, string.len(s)) == string.byte("\"") then
  31.         return string.sub(s, 2, -2)
  32.     end
  33.     return s
  34. end
  35.  
  36. function openSettingsFile(path)
  37.     if not fs.exists(path) then
  38.         f = fs.open(path, "w")
  39.         f.close()
  40.     end
  41.  
  42.     local settings = {}
  43.     local currentsection = {}
  44.     local currentsectionname = nil
  45.     local file = fs.open(path, "r")
  46.     local lines = true
  47.     settings["content"] = {}
  48.     while lines do
  49.         local currentline = file.readLine()
  50.         if currentline == nil then
  51.             lines = false
  52.             break
  53.         end
  54.         currentline = trimComments(currentline)
  55.         if Trim(currentline) ~= "" then
  56.             if string.byte(currentline, 1) == string.byte("[") then
  57.                 if currentsectionname ~= nil then
  58.                     settings["content"][currentsectionname] = currentsection
  59.                     currentsection = {}
  60.                 elseif currentsectionname == nil then
  61.                     settings["content"][1] = currentsection
  62.                     currentsection = {}
  63.                 end
  64.                 currentsectionname = string.sub(currentline, 2, -2)
  65.             else
  66.                 local key, value = split(currentline)
  67.                 if Trim(key) ~= nil and Trim(value) ~= nil then
  68.                     local x = Trim(value)
  69.                     if tonumber(x) then
  70.                         x = tonumber(x)
  71.                     else
  72.                         x = RemoveQuotes(x)
  73.                     end
  74.                     if x ~= nil and tostring(Trim(key)) ~= nil then
  75.                         currentsection[Trim(key)] = x
  76.                     end
  77.                    
  78.                 end
  79.             end
  80.         end
  81.     end
  82.    
  83.     if currentsectionname ~= nil then
  84.         settings["content"][currentsectionname] = currentsection
  85.         currentsection = {}
  86.     elseif currentsectionname == nil then
  87.         settings["content"][1] = currentsection
  88.         currentsection = {}
  89.     end
  90.    
  91.     function settings.addSection(name)
  92.         settings["content"][name] = {}
  93.     end
  94.    
  95.     function settings.getSectionedValue(section, key)
  96.         if type(settings["content"][section]) == "table" then
  97.             return settings["content"][section][key]
  98.         else
  99.             return false
  100.         end
  101.     end
  102.    
  103.     function settings.getValue(section, key)
  104.         if key == nil then
  105.             key = section
  106.             if type(settings["content"]) == "table" then
  107.                 local x = settings["content"][1]
  108.                 return x[key]
  109.             else
  110.                 return false
  111.             end
  112.         else
  113.             return settings.getSectionedValue(section, key)
  114.         end
  115.     end
  116.    
  117.     function settings.setSectionedValue(section, key, value)
  118.         if type(settings["content"][section]) ~= "table" then
  119.             settings.addSection(section)
  120.         end
  121.         settings["content"][section][key] = value
  122.     end
  123.    
  124.     function settings.setValue(section, key, value)
  125.         if value == nil then
  126.             value = key
  127.             key = section
  128.             settings["content"][1][key] = value
  129.         else
  130.             settings.setSectionedValue(section, key, value)
  131.         end
  132.     end
  133.    
  134.     function settings.save(path)
  135.         local file = fs.open(path, "w")
  136.         local d = settings["content"][1]
  137.         if d ~= nil then
  138.             for k, v in pairs(d) do
  139.                 local x = v
  140.                 if string.byte(v, 1) == string.byte(" ") or string.byte(v, string.len(v)) == string.byte(" ") then
  141.                     x = "\""..v.."\""
  142.                 end
  143.                 file.writeLine(k.." = "..x)
  144.             end
  145.         end
  146.         for k, v in pairs(settings["content"]) do
  147.             if k ~= 1 then
  148.                 file.writeLine("")
  149.                 file.writeLine("["..k.."]")
  150.                 for j, l in pairs(v) do
  151.                     local x = l
  152.                     if string.byte(l, 1) == string.byte(" ") or string.byte(l, string.len(l)) == string.byte(" ") then
  153.                         x = "\""..l.."\""
  154.                     end
  155.                     file.writeLine(j.." = "..x)
  156.                 end
  157.             end
  158.         end
  159.         file.close()
  160.     end
  161.    
  162.     settings.read = settings.getValue
  163.     settings.write = settings.setValue
  164.     settings.close = settings.save
  165.     return settings
  166. end
  167. open = openSettingsFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement