From f6ad88d181967dc058162b2ee5061e9d261936b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=8B=E6=8C=AF=E9=82=A6?= Date: Wed, 7 Jul 2021 13:02:08 +0800 Subject: [PATCH] feat: Add function newConfigFromText MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 蒋振邦 --- src/config/Config.lua | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/config/Config.lua b/src/config/Config.lua index 8e256cd..fc5cb2f 100644 --- a/src/config/Config.lua +++ b/src/config/Config.lua @@ -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. @@ -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 @@ -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 @@ -216,7 +223,7 @@ function Config:get(key) else option = keys[1] end - + if self.data[section] == nil then return "" elseif self.data[section][option] == nil then @@ -224,4 +231,4 @@ function Config:get(key) else return self.data[section][option] end -end +end