serialize.lua 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. -- serialize.lua
  2. -- Lua-conformant library file that has no minetest dependencies
  3. -- Contains the serialization and deserialization routines
  4. --[[
  5. Version history:
  6. 1 - initial
  7. 2 - also escaping CR character as &r
  8. Structure of entry:
  9. [keytype][key]:[valuetype][val]
  10. Types:
  11. B - bool
  12. -> 0=false, 1=true
  13. S - string
  14. -> see below
  15. N - number
  16. -> thing compatible with tonumber()
  17. Table:
  18. [keytype][key]:T
  19. ... content is nested in table until the matching
  20. E
  21. example:
  22. LUA_SER v=2 {
  23. Skey:Svalue key = "value",
  24. N1:Seins [1] = "eins",
  25. B1:T [true] = {
  26. Sa:Sb a = "b",
  27. Sc:B0 c = false,
  28. E }
  29. E }
  30. String representations:
  31. In strings the following characters are escaped by &
  32. '&' -> '&&'
  33. (line break) -> '&n'
  34. (CR) -> '&r'
  35. ':' -> '&:'
  36. All other characters are unchanged as they bear no special meaning.
  37. ]]
  38. local write_table, literal_to_string, escape_chars, table_is_empty
  39. function table_is_empty(t)
  40. for _,_ in pairs(t) do
  41. return false
  42. end
  43. return true
  44. end
  45. function write_table(t, file, config)
  46. local ks, vs, writeit, istable
  47. for key, value in pairs(t) do
  48. ks = value_to_string(key, false)
  49. writeit = true
  50. istable = type(value)=="table"
  51. if istable then
  52. vs = "T"
  53. if config and config.skip_empty_tables then
  54. writeit = not table_is_empty(value)
  55. end
  56. else
  57. vs = value_to_string(value, true)
  58. end
  59. if writeit then
  60. file:write(ks..":"..vs.."\n")
  61. if istable then
  62. write_table(value, file, config)
  63. file:write("E\n")
  64. end
  65. end
  66. end
  67. end
  68. function value_to_string(t)
  69. if type(t)=="table" then
  70. file:close()
  71. error("Can not serialize a table in the key position!")
  72. elseif type(t)=="boolean" then
  73. if t then
  74. return "B1"
  75. else
  76. return "B0"
  77. end
  78. elseif type(t)=="number" then
  79. return "N"..t
  80. elseif type(t)=="string" then
  81. return "S"..escape_chars(t)
  82. else
  83. --error("Can not serialize '"..type(t).."' type!")
  84. return "S<function>"
  85. end
  86. return str
  87. end
  88. function escape_chars(str)
  89. local rstr = string.gsub(str, "&", "&&")
  90. rstr = string.gsub(rstr, ":", "&:")
  91. rstr = string.gsub(rstr, "\r", "&r")
  92. rstr = string.gsub(rstr, "\n", "&n")
  93. return rstr
  94. end
  95. ------
  96. local read_table, string_to_value, unescape_chars
  97. function read_table(t, file)
  98. local line, ks, vs, kv, vv, vt
  99. while true do
  100. line = file:read("*l")
  101. if not line then
  102. file:close()
  103. error("Unexpected EOF or read error!")
  104. end
  105. if line=="E" then
  106. -- done with this table
  107. return
  108. end
  109. ks, vs = string.match(line, "^(.*[^&]):(.+)$")
  110. if not ks or not vs then
  111. file:close()
  112. error("Unable to parse line: '"..line.."'!")
  113. end
  114. kv = string_to_value(ks)
  115. vv, vt = string_to_value(vs, true)
  116. if vt then
  117. read_table(vv, file)
  118. end
  119. -- put read value in table
  120. t[kv] = vv
  121. end
  122. end
  123. -- returns: value, is_table
  124. function string_to_value(str, table_allow)
  125. local first = string.sub(str, 1,1)
  126. local rest = string.sub(str, 2)
  127. if first=="T" then
  128. if table_allow then
  129. return {}, true
  130. else
  131. file:close()
  132. error("Table not allowed in key component!")
  133. end
  134. elseif first=="N" then
  135. local num = tonumber(rest)
  136. if num then
  137. return num
  138. else
  139. file:close()
  140. error("Unable to parse number: '"..rest.."'!")
  141. end
  142. elseif first=="B" then
  143. if rest=="0" then
  144. return false
  145. elseif rest=="1" then
  146. return true
  147. else
  148. file:close()
  149. error("Unable to parse boolean: '"..rest.."'!")
  150. end
  151. elseif first=="S" then
  152. return unescape_chars(rest)
  153. else
  154. file:close()
  155. error("Unknown literal type '"..first.."' for literal '"..str.."'!")
  156. end
  157. end
  158. function unescape_chars(str) --TODO
  159. local rstr = string.gsub(str, "&:", ":")
  160. rstr = string.gsub(rstr, "&n", "\n")
  161. rstr = string.gsub(rstr, "&r", "\r")
  162. rstr = string.gsub(rstr, "&&", "&")
  163. return rstr
  164. end
  165. ------
  166. --[[
  167. config = {
  168. skip_empty_tables = false -- if true, does not store empty tables
  169. -- On next read, keys that mapped to empty tables resolve to nil
  170. }
  171. ]]
  172. -- Writes the passed table into the passed file descriptor, and closes the file
  173. local function write_to_fd(root_table, file, config)
  174. file:write("LUA_SER v=2\n")
  175. write_table(root_table, file, config)
  176. file:write("E\nEND_SER\n")
  177. file:close()
  178. end
  179. -- Reads the file contents from the passed file descriptor and returns the table on success
  180. -- Throws errors when something is wrong. Closes the file.
  181. -- config: see above
  182. local function read_from_fd(file)
  183. local first_line = file:read("*line")
  184. if not string.match(first_line, "LUA_SER v=[12]") then
  185. file:close()
  186. error("Expected header, got '"..first_line.."' instead!")
  187. end
  188. local t = {}
  189. read_table(t, file)
  190. local last_line = file:read("*line")
  191. file:close()
  192. if last_line ~= "END_SER" then
  193. error("Missing END_SER, got '"..last_line.."' instead!")
  194. end
  195. return t
  196. end
  197. -- Opens the passed filename and serializes root_table into it
  198. -- config: see above
  199. function write_to_file(root_table, filename, config)
  200. -- try opening the file
  201. local file, err = io.open(filename, "wb")
  202. if not file then
  203. error("Failed opening file '"..filename.."' for write:\n"..err)
  204. end
  205. write_to_fd(root_table, file, config)
  206. return true
  207. end
  208. -- Opens the passed filename, and returns its deserialized contents
  209. function read_from_file(filename)
  210. -- try opening the file
  211. local file, err = io.open(filename, "rb")
  212. if not file then
  213. error("Failed opening file '"..filename.."' for read:\n"..err)
  214. end
  215. return read_from_fd(file)
  216. end
  217. --[[ simple unit test
  218. local testtable = {
  219. key = "value",
  220. [1] = "eins",
  221. [true] = {
  222. a = "b",
  223. c = false,
  224. },
  225. ["es:cape1"] = "foo:bar",
  226. ["es&ca\npe2"] = "baz&bam\nbim",
  227. ["es&&ca&\npe3"] = "baz&&bam&\nbim",
  228. ["es&:cape4"] = "foo\n:bar"
  229. }
  230. local config = {}
  231. --write_to_file(testtable, "test_out", config)
  232. local t = read_from_file("test_out")
  233. write_to_file(t, "test_out_2", config)
  234. local t2 = read_from_file("test_out_2")
  235. write_to_file(t2, "test_out_3", config)
  236. -- test_out_2 and test_out_3 should be equal
  237. --]]
  238. return {
  239. read_from_fd = read_from_fd,
  240. write_to_fd = write_to_fd,
  241. read_from_file = read_from_file,
  242. write_to_file = write_to_file,
  243. }