From e48ebe91f02d5e589004b2196c4c1a304ef70f19 Mon Sep 17 00:00:00 2001 From: MrZ626 <1046101471@qq.com> Date: Sun, 20 Sep 2020 18:53:32 +0800 Subject: [PATCH] =?UTF-8?q?dumpTable=E5=92=8CaddToTable=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E5=85=A8=E5=B1=80=E6=94=BE=E5=85=A5toolfunc?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Zframework/file.lua | 58 ---------------------------------------- Zframework/toolfunc.lua | 59 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 58 deletions(-) diff --git a/Zframework/file.lua b/Zframework/file.lua index 71dacc8d..1356fa6f 100644 --- a/Zframework/file.lua +++ b/Zframework/file.lua @@ -4,64 +4,6 @@ local sub,find=string.sub,string.find local toN,toS=tonumber,tostring local concat=table.concat -local tabs={ - [0]="", - "\t", - "\t\t", - "\t\t\t", - "\t\t\t\t", - "\t\t\t\t\t", -} -local function dumpTable(L,t) - local s - if t then - s="{\n" - else - s="return{\n" - t=1 - end - local count=1 - for k,v in next,L do - local T=type(k) - if T=="number"then - if k==count then - k="" - count=count+1 - else - k="["..k.."]=" - end - elseif T=="string"then - if find(k,"[^0-9a-zA-Z_]")then - k="[\""..k.."\"]=" - else - k=k.."=" - end - elseif T=="boolean"then k="["..k.."]=" - else assert(false,"Error key type!") - end - T=type(v) - if T=="number"then v=tostring(v) - elseif T=="string"then v="\""..v.."\"" - elseif T=="table"then v=dumpTable(v,t+1) - elseif T=="boolean"then v=tostring(v) - else assert(false,"Error data type!") - end - s=s..tabs[t]..k..v..",\n" - end - return s..tabs[t-1].."}" -end -local function addToTable(G,base)--Refresh default base with G-values - for k,v in next,G do - if type(v)==type(base[k])then - if type(v)=="table"then - addToTable(v,base[k]) - else - base[k]=v - end - end - end -end - local files={ data= fs.newFile("data.dat"), setting=fs.newFile("settings.dat"), diff --git a/Zframework/toolfunc.lua b/Zframework/toolfunc.lua index 9aa6ba6c..cceeb8f5 100644 --- a/Zframework/toolfunc.lua +++ b/Zframework/toolfunc.lua @@ -39,6 +39,65 @@ do--setFont end end end +do--dumpTable + local tabs={ + [0]="", + "\t", + "\t\t", + "\t\t\t", + "\t\t\t\t", + "\t\t\t\t\t", + } + function dumpTable(L,t) + local s + if t then + s="{\n" + else + s="return{\n" + t=1 + end + local count=1 + for k,v in next,L do + local T=type(k) + if T=="number"then + if k==count then + k="" + count=count+1 + else + k="["..k.."]=" + end + elseif T=="string"then + if find(k,"[^0-9a-zA-Z_]")then + k="[\""..k.."\"]=" + else + k=k.."=" + end + elseif T=="boolean"then k="["..k.."]=" + else assert(false,"Error key type!") + end + T=type(v) + if T=="number"then v=tostring(v) + elseif T=="string"then v="\""..v.."\"" + elseif T=="table"then v=dumpTable(v,t+1) + elseif T=="boolean"then v=tostring(v) + else assert(false,"Error data type!") + end + s=s..tabs[t]..k..v..",\n" + end + return s..tabs[t-1].."}" + end +end +function addToTable(G,base)--Refresh default base with G-values + for k,v in next,G do + if type(v)==type(base[k])then + if type(v)=="table"then + addToTable(v,base[k]) + else + base[k]=v + end + end + end +end function splitStr(s,sep) local L={} local p1,p2=1--start,target