diff options
author | orwell96 <orwell@bleipb.de> | 2021-04-04 19:57:43 +0200 |
---|---|---|
committer | orwell96 <orwell@bleipb.de> | 2021-04-04 19:58:25 +0200 |
commit | 5ee8c0e39b6dffc5437e1b48b08cfbeb6ea414d1 (patch) | |
tree | c147099e46780125442232b18ca7396aa7a13059 /serialize_lib/serialize.lua | |
parent | 3dc6426e861b7c7e7fa14ab82d62a7d818ea0d2e (diff) | |
parent | edcc0d09178bfb64fbaa7620a7b0176d9b57b9d1 (diff) | |
download | advtrains-5ee8c0e39b6dffc5437e1b48b08cfbeb6ea414d1.tar.gz advtrains-5ee8c0e39b6dffc5437e1b48b08cfbeb6ea414d1.tar.bz2 advtrains-5ee8c0e39b6dffc5437e1b48b08cfbeb6ea414d1.zip |
Update serialize_lib
Diffstat (limited to 'serialize_lib/serialize.lua')
-rw-r--r-- | serialize_lib/serialize.lua | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/serialize_lib/serialize.lua b/serialize_lib/serialize.lua index 12a26c4..4b1ebb7 100644 --- a/serialize_lib/serialize.lua +++ b/serialize_lib/serialize.lua @@ -3,6 +3,9 @@ -- Contains the serialization and deserialization routines --[[ +Version history: +1 - initial +2 - also escaping CR character as &r Structure of entry: [keytype][key]:[valuetype][val] @@ -19,7 +22,7 @@ Table: E example: -LUA_SER v=1 { +LUA_SER v=2 { Skey:Svalue key = "value", N1:Seins [1] = "eins", B1:T [true] = { @@ -32,7 +35,7 @@ String representations: In strings the following characters are escaped by & '&' -> '&&' (line break) -> '&n' -(CR) -> '&r' --> required? +(CR) -> '&r' ':' -> '&:' All other characters are unchanged as they bear no special meaning. ]] @@ -97,6 +100,7 @@ end function escape_chars(str) local rstr = string.gsub(str, "&", "&&") rstr = string.gsub(rstr, ":", "&:") + rstr = string.gsub(rstr, "\r", "&r") rstr = string.gsub(rstr, "\n", "&n") return rstr end @@ -172,6 +176,7 @@ end function unescape_chars(str) --TODO local rstr = string.gsub(str, "&:", ":") rstr = string.gsub(rstr, "&n", "\n") + rstr = string.gsub(rstr, "&r", "\r") rstr = string.gsub(rstr, "&&", "&") return rstr end @@ -187,7 +192,7 @@ config = { -- Writes the passed table into the passed file descriptor, and closes the file local function write_to_fd(root_table, file, config) - file:write("LUA_SER v=1\n") + file:write("LUA_SER v=2\n") write_table(root_table, file, config) file:write("E\nEND_SER\n") file:close() @@ -197,14 +202,14 @@ end -- Throws errors when something is wrong. Closes the file. -- config: see above local function read_from_fd(file) - local first_line = file:read("*l") - if first_line ~= "LUA_SER v=1" then + local first_line = file:read("*line") + if not string.match(first_line, "LUA_SER v=[12]") then file:close() error("Expected header, got '"..first_line.."' instead!") end local t = {} read_table(t, file) - local last_line = file:read("*l") + local last_line = file:read("*line") file:close() if last_line ~= "END_SER" then error("Missing END_SER, got '"..last_line.."' instead!") @@ -216,7 +221,7 @@ end -- config: see above function write_to_file(root_table, filename, config) -- try opening the file - local file, err = io.open(filename, "w") + local file, err = io.open(filename, "wb") if not file then error("Failed opening file '"..filename.."' for write:\n"..err) end @@ -228,7 +233,7 @@ end -- Opens the passed filename, and returns its deserialized contents function read_from_file(filename) -- try opening the file - local file, err = io.open(filename, "r") + local file, err = io.open(filename, "rb") if not file then error("Failed opening file '"..filename.."' for read:\n"..err) end |