PageRenderTime 50ms CodeModel.GetById 25ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/Data/generator/engine.lua

http://awoe.googlecode.com/
Lua | 198 lines | 167 code | 18 blank | 13 comment | 19 complexity | ba84743c8ef91689c0be925732c6ce67 MD5 | raw file
  1require 'string'
  2require 'table'
  3
  4
  5
  6
  7function export_csv(file_path, sheet_cnt)
  8	local cwd = ""
  9
 10	os.execute("mkdir tmp")
 11	os.execute("cd > tmp\\_cwd.txt")
 12	local fcwd = io.open("tmp\\_cwd.txt")
 13	if fcwd then
 14		cwd = fcwd:read("*l")
 15		fcwd:close()
 16	end
 17	file_path = cwd .. "\\" .. file_path .. " "
 18	cwd = cwd .. "\\tmp\\ "
 19	os.execute("cscript export.vbs " .. file_path .. cwd ..  sheet_cnt)
 20end
 21
 22function clear_csv()
 23	os.execute("rmdir /S /Q tmp")
 24end
 25
 26
 27--
 28-- a customized compare function for sorting table that to be ouputed
 29--
 30function compare_func(a, b, w_tbl)
 31	w_tbl = w_tbl or {}
 32	w_tbl.id = w_tbl.id or -999
 33	w_tbl.name = w_tbl.name or -998
 34
 35	local va = a
 36	local vb = b
 37	if type(a) ~= "number" then
 38		va = w_tbl[a] or 0
 39	end
 40	if type(b) ~= "number" then
 41		vb = w_tbl[b] or 0
 42	end
 43
 44	if va == vb then
 45		return a<b
 46	else
 47		return va<vb
 48	end
 49end
 50
 51--
 52--	output a table to file in a cutomized format
 53--	talbe:t
 54--	file:f
 55--
 56function output_table(t, f, tabs, comma, w_tbl)
 57	if not (type(t)=="table") then
 58		print("Invalid table to output!")
 59		return
 60	end
 61
 62	tabs = tabs or ""
 63	local connector = ""
 64	local k_tbl = {}
 65	local has_tbl = false
 66	for k, v in pairs(t) do
 67		table.insert(k_tbl, k)
 68		if type(v)=="table" then
 69			has_tbl = true
 70		end
 71	end
 72
 73	table.sort(k_tbl, function (a,b) return compare_func(a, b, w_tbl) end )
 74
 75	--
 76	--	Note that:	getn is only for table with numberic index
 77	--			not available for table with string index
 78	--	so we use getn(k_tbl) instead of getn(t), as we know that
 79	--	k_tbl and t has the same number of k-v pairs
 80	if table.getn(k_tbl)<12 and not has_tbl then
 81		connector = ""
 82		tabs = ""
 83	else
 84		connector = "\n"
 85		tabs = tabs .. "	"
 86	end
 87
 88	f:write("{" .. connector)
 89	for _, k in ipairs(k_tbl) do
 90		local v = t[k]
 91
 92		if type(k)=="number" then
 93			k = "[" .. k .. "]"
 94		end
 95		local tv = type(v)
 96		if tv=="table" then
 97			f:write(tabs, k, "= ")
 98			output_table(v, f, tabs .. "	", true)
 99		elseif tv=="string" then
100			if v:sub(1,1)=="\"" and v:sub(-1)=="" then
101				f:write(tabs, k, "=", v, ","..connector)
102			else
103				f:write(tabs, k, "=", "\""..v.."\","..connector)
104			end
105		elseif tv=="boolean" then
106			f:write(tabs, k, "=", tostring(v), ","..connector)
107		else
108			f:write(tabs, k, "=", v or 0, ","..connector)
109		end
110	end
111	if not (tabs=="")then
112		tabs = tabs:sub(1,-2)
113	end
114	if comma then
115		f:write(tabs .. "},\n")
116	else
117		f:write(tabs .. "}\n")
118	end
119end
120
121
122function handle_line(obj, line, x)
123	obj[x] = {}
124	local y = 1
125	local pos_prev = 1
126	local mode = 0
127	for pos = 1, line:len() do
128		local c = line:sub(pos,pos)
129		if mode==1 then
130			if c=="\"" then
131				mode = 0
132			end
133		else
134			if c=="\"" then
135				mode = 1
136			elseif c=="," then
137				local val = line:sub(pos_prev, pos-1)
138				if val:sub(1,1)=="\"" and val:sub(-1)=="\"" then
139					obj[x][y] = val:sub(2,-2)
140				elseif val=="" then
141					obj[x][y] = 0
142				else
143					obj[x][y] = val
144				end
145				y = y+1
146				pos_prev = pos+1
147			end
148		end
149	end
150	obj[x][y] = line:sub(pos_prev)
151end
152
153function handle_file(fileName, _start, _step, do_func)
154	local f = io.open(fileName)
155	if not f then
156		print("Failed to open file:", fileName)
157		return
158	end
159	local cnt = 0
160	local obj = {}
161	local _end = _start + _step
162	local pre_line
163	for line in f:lines() do
164		if pre_line then
165			line = pre_line .. line
166		end
167		local flag = 0
168		for _ in line:gfind("\"") do
169			if flag == 0 then
170				flag = 1
171			else
172				flag = 0
173			end
174		end
175		if flag==1 then
176			pre_line = line
177		else
178			pre_line = nil
179
180			cnt = cnt + 1
181			if cnt>=_end then
182				do_func(obj)
183				obj = {}
184				_start = _start + _step
185				_end = _end + _step
186
187				handle_line(obj, line, cnt-_start+1)
188			elseif cnt>=_start then
189				handle_line(obj, line, cnt-_start+1)
190			end
191		end
192	end
193	if type(obj)=="table" then
194		do_func(obj)
195		obj = nil
196	end
197	f:close()
198end