Skip to content

Commit

Permalink
feat: Add function newConfigFromText
Browse files Browse the repository at this point in the history
Signed-off-by: 蒋振邦 <edomondja@gmail.com>
  • Loading branch information
Edmond-J-A committed Jul 7, 2021
1 parent 2ef2138 commit f6ad88d
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/config/Config.lua
Expand Up @@ -42,7 +42,14 @@ end
* @return the constructor of Config.
]]
function Config:newConfigFromText(text)

local c = {}
setmetatable(c,self)
self.__index = self
c.data = {}
local lines = {}
string.gsub(text, '[^'.."\r\n"..']+', function(w) table.insert(lines, w) end )
c:parseBuffer(lines)
return c
end

-- addConfig adds a new section->key:value to the configuration.
Expand Down Expand Up @@ -76,7 +83,7 @@ function Config:parseBuffer(lines)
local lineNum = 0
local canWrite = false
local line = ""

while true do
if canWrite then
if self:write(section, lineNum, buf) == true then
Expand Down Expand Up @@ -109,7 +116,7 @@ function Config:parseBuffer(lines)
end

section = string.sub(line, 2, -2)
else
else
local p = ""

if self.DEFAULT_MULTI_LINE_SEPARATOR == string.sub(line, -2, -1) then
Expand Down Expand Up @@ -216,12 +223,12 @@ function Config:get(key)
else
option = keys[1]
end

if self.data[section] == nil then
return ""
elseif self.data[section][option] == nil then
return ""
else
return self.data[section][option]
end
end
end

0 comments on commit f6ad88d

Please sign in to comment.