/Data/generator/engine.lua

http://awoe.googlecode.com/ · Lua · 198 lines · 167 code · 18 blank · 13 comment · 31 complexity · ba84743c8ef91689c0be925732c6ce67 MD5 · raw file

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