table-save.lua 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. do
  2. -- declare local variables
  3. -- // exportstring( string )
  4. -- // returns a "Lua" portable version of the string
  5. local function exportstring(s)
  6. return string.format("%q", s)
  7. end
  8. -- // The Save Function
  9. function table.save(tbl, filename)
  10. local charS, charE = " ", "\n"
  11. local file, err = io.open(filename, "wb")
  12. if err then
  13. return err
  14. end
  15. -- initiate variables for save procedure
  16. local tables, lookup = {tbl}, {
  17. [tbl] = 1
  18. }
  19. file:write("return {" .. charE)
  20. for idx, t in ipairs(tables) do
  21. file:write("-- Table: {" .. idx .. "}" .. charE)
  22. file:write("{" .. charE)
  23. local thandled = {}
  24. for i, v in ipairs(t) do
  25. thandled[i] = true
  26. local stype = type(v)
  27. -- only handle value
  28. if stype == "table" then
  29. if not lookup[v] then
  30. table.insert(tables, v)
  31. lookup[v] = #tables
  32. end
  33. file:write(charS .. "{" .. lookup[v] .. "}," .. charE)
  34. elseif stype == "string" then
  35. file:write(charS .. exportstring(v) .. "," .. charE)
  36. elseif stype == "number" then
  37. file:write(charS .. tostring(v) .. "," .. charE)
  38. end
  39. end
  40. for i, v in pairs(t) do
  41. -- escape handled values
  42. if (not thandled[i]) then
  43. local str = ""
  44. local stype = type(i)
  45. -- handle index
  46. if stype == "table" then
  47. if not lookup[i] then
  48. table.insert(tables, i)
  49. lookup[i] = #tables
  50. end
  51. str = charS .. "[{" .. lookup[i] .. "}]="
  52. elseif stype == "string" then
  53. str = charS .. "[" .. exportstring(i) .. "]="
  54. elseif stype == "number" then
  55. str = charS .. "[" .. tostring(i) .. "]="
  56. end
  57. if str ~= "" then
  58. stype = type(v)
  59. -- handle value
  60. if stype == "table" then
  61. if not lookup[v] then
  62. table.insert(tables, v)
  63. lookup[v] = #tables
  64. end
  65. file:write(str .. "{" .. lookup[v] .. "}," .. charE)
  66. elseif stype == "string" then
  67. file:write(str .. exportstring(v) .. "," .. charE)
  68. elseif stype == "number" then
  69. file:write(str .. tostring(v) .. "," .. charE)
  70. end
  71. end
  72. end
  73. end
  74. file:write("}," .. charE)
  75. end
  76. file:write("}")
  77. file:close()
  78. end
  79. -- // The Load Function
  80. function table.load(sfile)
  81. local ftables, err = loadfile(sfile)
  82. if err then
  83. return _, err
  84. end
  85. local tables = ftables()
  86. for idx = 1, #tables do
  87. local tolinki = {}
  88. for i, v in pairs(tables[idx]) do
  89. if type(v) == "table" then
  90. tables[idx][i] = tables[v[1]]
  91. end
  92. if type(i) == "table" and tables[i[1]] then
  93. table.insert(tolinki, {i, tables[i[1]]})
  94. end
  95. end
  96. -- link indices
  97. for _, v in ipairs(tolinki) do
  98. tables[idx][v[2]], tables[idx][v[1]] = tables[idx][v[1]], nil
  99. end
  100. end
  101. return tables[1]
  102. end
  103. -- close do
  104. function table.SaveTableContent(file, obj, isfirst)
  105. local szType = type(obj);
  106. print(szType);
  107. if szType == "number" then
  108. file:write(obj);
  109. elseif szType == "string" then
  110. file:write(string.format("\'%s\'", obj));
  111. elseif szType == "boolean" then
  112. local str = obj and "true" or "false"
  113. file:write(str);
  114. elseif szType == "table" then
  115. -- 把table的内容格式化写入文件
  116. isfirst = isfirst + 1
  117. if isfirst > 3 then
  118. file:write("{");
  119. for i, v in pairs(obj) do
  120. table.SaveTableContent(file, v, isfirst);
  121. file:write(",");
  122. end
  123. file:write("}");
  124. else
  125. local sortParams = {}
  126. local keyType = ""
  127. for kv in pairs(obj) do
  128. if kv ~= nil then
  129. table.insert(sortParams, kv)
  130. keyType = type(kv)
  131. end
  132. end
  133. if keyType == "number" then
  134. table.sort(sortParams, function(a, b)
  135. return a < b
  136. end)
  137. elseif keyType == "string" then
  138. table.sort(sortParams, function(a, b)
  139. error(string.find("1222", "Id"))
  140. if a == "Id" or (string.find(a, "Id") ~= nil and string.find(b,"Id") == nil)then
  141. return true
  142. else
  143. return a < b
  144. end
  145. end)
  146. end
  147. file:write("{\n");
  148. for _, v in pairs(sortParams) do
  149. file:write("[");
  150. table.SaveTableContent(file, v, isfirst);
  151. file:write("]=");
  152. table.SaveTableContent(file, obj[v], isfirst);
  153. file:write(", \n");
  154. end
  155. file:write("}");
  156. end
  157. else
  158. error("can't serialize a " .. szType);
  159. end
  160. end
  161. function table.SaveTable(luaTable, path, TableName)
  162. local file, err = io.open(path, "wb")
  163. if err then
  164. return err
  165. end
  166. assert(file);
  167. local oneLine = string.format("local %s=", TableName)
  168. file:write(oneLine);
  169. table.SaveTableContent(file, luaTable, 1);
  170. file:write(string.format("\n return %s", TableName));
  171. file:close();
  172. end
  173. end