-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparsemain.lua
More file actions
181 lines (174 loc) · 3.87 KB
/
parsemain.lua
File metadata and controls
181 lines (174 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
local g_funclist = require "parsefunclist"
function CopyTable(obj)
if type(obj) ~= "table" then
return nil
end
local new_table = {}
for k,v in pairs(obj) do
if type(v) == "table" then
new_table[k] = CopyTable(v)
else
new_table[k] = v
end
end
return new_table
end
function PrintTable(obj)
for k, v in pairs(obj) do
if type(v) == "table" then
print("\t",k," = {")
PrintTable(v)
print("\t","}")
else
print("\t",k, " = ", v, ",")
end
end
end
function LogTable(name, tab)
name = name or "default"
print(name, " =\n{")
PrintTable(tab)
print("}")
end
local temp = 0
local space = "\t"
local endline = "\n"
function WriteTableToFile(ret,file)
local ttspace = space
for i=1,temp do
ttspace = ttspace .. space
end
if temp == 0 then
file:write(endline.."{"..endline)
else
file:write(endline..ttspace.."{"..endline)
end
temp = temp + 1
for k, v in pairs(ret) do
if type(v) == "table" then
if type(k) == "number" then
file:write(ttspace.."["..k.."]=")
else
file:write(ttspace..tostring(k).."=")
end
WriteTableToFile(v,file)
else
if type(v) == "string" then
if type(k) == "number" then
file:write(ttspace.."["..k.."]='"..tostring(v).."',"..endline)
else
file:write(ttspace..tostring(k).."='"..tostring(v).."',"..endline)
end
else
if type(k) == "number" then
file:write(ttspace.."["..k.."]="..tostring(v)..","..endline)
else
file:write(ttspace..tostring(k).."="..tostring(v)..","..endline)
end
end
end
end
temp = temp - 1
if temp == 0 then
file:write(ttspace..endline.."}"..endline)
else
file:write(ttspace..endline..ttspace.."},"..endline)
end
end
function SplitString(str,del)
local ret = {}
local pos = 0
local index = 1
while true do
pos = string.find(str,del,pos)
if pos == nil then
local ss = string.sub(str,index,#str)
table.insert(ret,ss)
break
end
local ss = string.sub(str,index,pos-1)
index = pos + 1
pos = pos + 1
table.insert(ret,ss)
end
return ret
end
function ParseCSVFile(filename)
local file, msg = io.open(filename,"r")
if not file then
print("not exist the file,msg = ",msg)
return nil
end
local index = 0
local attrname = {}
local attrtype = {}
local data = {}
for line in file:lines() do
line = string.gsub(line,"\r\n",",")
local rowData = SplitString(line,",")
if index == 1 then
attrname = CopyTable(rowData)
end
if index == 2 then
attrtype = CopyTable(rowData)
end
if index >= 3 then
data[#data+1] = rowData
end
index = index + 1
end
file:close()
return data,attrname,attrtype
end
function ParseCSVFileToLuaTable(filename,targetpath)
local tabname = string.sub(filename,1,string.find(filename,".csv")-1)
local data,attrname,attrtype = ParseCSVFile(filename)
if not data then
return
end
targetpath =targetpath..tabname..".lua"
local tempCfg = {}
for i=1, #data do
local tt = data[i]
local one = {}
for k,v in ipairs(tt) do
if attrtype[k] == "String" then
one[attrname[k]] = v
elseif attrtype[k] == "Number" then
if v ~= "" then
one[attrname[k]] = tonumber(v)
else
one[attrname[k]] = 0
end
end
end
table.insert(tempCfg,one)
end
local parsefunc = g_funclist[tabname]
local ret = nil
if parsefunc then
ret = parsefunc(tempCfg)
end
local file = io.open(targetpath..".lua","w")
file:write("local "..tabname.."=")
if ret then
WriteTableToFile(ret,file)
else
WriteTableToFile(tempCfg,file)
end
file:write("return "..tabname)
file:close()
end
function ParseAllCSVFile(filename, targetpath)
local data,attrname,attrtype = ParseCSVFile(filename)
for i=1, #data do
local tabname = data[i][2]
print("parse ["..tabname.."] begin...")
ParseCSVFileToLuaTable(tabname,targetpath)
print("parse ["..tabname.."] end...")
end
end
local cmd = {...}
local filename = cmd[1] or "readme.csv"
local targetpath = cmd[2] or "Config/"
ParseAllCSVFile(filename, targetpath)