· 6 years ago · Aug 30, 2019, 08:46 PM
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/
4local 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)
13end
14local 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)
25end
26function Trim(s)
27 return (s:gsub("^%s*(.-)%s*$", "%1"))
28end
29local 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
34end
35function openSettingsFile(path)
36 local settings = {}
37 local currentsection = {}
38 local currentsectionname = nil
39 local file = fs.open(path, "r")
40 local lines = true
41 settings["content"] = {}
42 while lines do
43 local currentline = file.readLine()
44 if currentline == nil then
45 lines = false
46 break
47 end
48 currentline = trimComments(currentline)
49 if Trim(currentline) ~= "" then
50 if string.byte(currentline, 1) == string.byte("[") then
51 if currentsectionname ~= nil then
52 settings["content"][currentsectionname] = currentsection
53 currentsection = {}
54 elseif currentsectionname == nil then
55 settings["content"][1] = currentsection
56 currentsection = {}
57 end
58 currentsectionname = string.sub(currentline, 2, -2)
59 else
60 local key, value = split(currentline)
61 if Trim(key) ~= nil and Trim(value) ~= nil then
62 local x = Trim(value)
63 if tonumber(x) then
64 x = tonumber(x)
65 else
66 x = RemoveQuotes(x)
67 end
68 if x ~= nil and tostring(Trim(key)) ~= nil then
69 currentsection[Trim(key)] = x
70 end
71
72 end
73 end
74 end
75 end
76 if currentsectionname ~= nil then
77 settings["content"][currentsectionname] = currentsection
78 currentsection = {}
79 elseif currentsectionname == nil then
80 settings["content"][1] = currentsection
81 currentsection = {}
82 end
83
84 function settings.addSection(name)
85 settings["content"][name] = {}
86 end
87
88 function settings.getValue(key)
89 local x = settings["content"][1]
90 return x[key]
91 end
92
93 function settings.getSectionedValue(section, key)
94 return settings["content"][section][key]
95 end
96
97 function settings.setValue(key, value)
98 settings["content"][1][key] = value
99 end
100
101 function settings.setSectionedValue(section, key, value)
102 settings["content"][section][key] = value
103 end
104
105 function settings.save(path)
106 local file = fs.open(path, "w")
107 local d = settings["content"][1]
108 if d ~= nil then
109 for k, v in pairs(d) do
110 local x = v
111 if string.byte(v, 1) == string.byte(" ") or string.byte(v, string.len(v)) == string.byte(" ") then
112 x = "\""..v.."\""
113 end
114 file.writeLine(k.." = "..x)
115 end
116 end
117 for k, v in pairs(settings["content"]) do
118 if k ~= 1 then
119 file.writeLine("")
120 file.writeLine("["..k.."]")
121 for j, l in pairs(v) do
122 local x = l
123 if string.byte(l, 1) == string.byte(" ") or string.byte(l, string.len(l)) == string.byte(" ") then
124 x = "\""..l.."\""
125 end
126 file.writeLine(j.." = "..x)
127 end
128 end
129 end
130 file.close()
131 end
132
133 return settings
134end