Skip to content

Commit a72b822

Browse files
authored
feat: Add function newConfigFromText (#79)
* feat: add some comment Signed-off-by: 蒋振邦 <edomondja@gmail.com> * feat: Add function newConfigFromText Signed-off-by: 蒋振邦 <edomondja@gmail.com>
1 parent 2ad505f commit a72b822

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/config/Config.lua

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ end
4242
* @return the constructor of Config.
4343
]]
4444
function Config:newConfigFromText(text)
45-
45+
local c = {}
46+
setmetatable(c,self)
47+
self.__index = self
48+
c.data = {}
49+
local lines = {}
50+
string.gsub(text, '[^'.."\r\n"..']+', function(w) table.insert(lines, w) end )
51+
c:parseBuffer(lines)
52+
return c
4653
end
4754

4855
-- addConfig adds a new section->key:value to the configuration.
@@ -76,7 +83,7 @@ function Config:parseBuffer(lines)
7683
local lineNum = 0
7784
local canWrite = false
7885
local line = ""
79-
86+
8087
while true do
8188
if canWrite then
8289
if self:write(section, lineNum, buf) == true then
@@ -109,7 +116,7 @@ function Config:parseBuffer(lines)
109116
end
110117

111118
section = string.sub(line, 2, -2)
112-
else
119+
else
113120
local p = ""
114121

115122
if self.DEFAULT_MULTI_LINE_SEPARATOR == string.sub(line, -2, -1) then
@@ -216,12 +223,12 @@ function Config:get(key)
216223
else
217224
option = keys[1]
218225
end
219-
226+
220227
if self.data[section] == nil then
221228
return ""
222229
elseif self.data[section][option] == nil then
223230
return ""
224231
else
225232
return self.data[section][option]
226233
end
227-
end
234+
end

0 commit comments

Comments
 (0)