aboutsummaryrefslogtreecommitdiff
path: root/serialize_lib/serialize.lua
diff options
context:
space:
mode:
authororwell96 <orwell@bleipb.de>2020-12-21 20:03:49 +0100
committerorwell96 <orwell@bleipb.de>2021-01-12 13:27:00 +0100
commitd0bd4ac30e8f14d40df01a91e346e7c2c589e0a9 (patch)
treee3d54c790d27a4ed4a686a26b65a8cb7777130a2 /serialize_lib/serialize.lua
parente6b656e93723ea2f0d36c8da39baba4fa099d711 (diff)
downloadadvtrains-d0bd4ac30e8f14d40df01a91e346e7c2c589e0a9.tar.gz
advtrains-d0bd4ac30e8f14d40df01a91e346e7c2c589e0a9.tar.bz2
advtrains-d0bd4ac30e8f14d40df01a91e346e7c2c589e0a9.zip
Serialize_lib: finish up and add atomic api
Diffstat (limited to 'serialize_lib/serialize.lua')
-rw-r--r--serialize_lib/serialize.lua20
1 files changed, 14 insertions, 6 deletions
diff --git a/serialize_lib/serialize.lua b/serialize_lib/serialize.lua
index 8ffd917..2d7c3a0 100644
--- a/serialize_lib/serialize.lua
+++ b/serialize_lib/serialize.lua
@@ -55,7 +55,7 @@ function write_table(t, file, config)
if istable then
vs = "T"
- if config.skip_empty_tables then
+ if config and config.skip_empty_tables then
writeit = not table_is_empty(value)
end
else
@@ -75,6 +75,7 @@ end
function value_to_string(t)
if type(t)=="table" then
+ file:close()
error("Can not serialize a table in the key position!")
elseif type(t)=="boolean" then
if t then
@@ -87,6 +88,7 @@ function value_to_string(t)
elseif type(t)=="string" then
return "S"..escape_chars(t)
else
+ file:close()
error("Can not serialize '"..type(t).."' type!")
end
return str
@@ -108,6 +110,7 @@ function read_table(t, file)
while true do
line = file:read("*l")
if not line then
+ file:close()
error("Unexpected EOF or read error!")
end
@@ -117,6 +120,7 @@ function read_table(t, file)
end
ks, vs = string.match(line, "^(.+[^&]):(.+)$")
if not ks or not vs then
+ file:close()
error("Unable to parse line: '"..line.."'!")
end
kv = string_to_value(ks)
@@ -137,6 +141,7 @@ function string_to_value(str, table_allow)
if table_allow then
return {}, true
else
+ file:close()
error("Table not allowed in key component!")
end
elseif first=="N" then
@@ -144,6 +149,7 @@ function string_to_value(str, table_allow)
if num then
return num
else
+ file:close()
error("Unable to parse number: '"..rest.."'!")
end
elseif first=="B" then
@@ -152,11 +158,13 @@ function string_to_value(str, table_allow)
elseif rest=="1" then
return true
else
+ file:close()
error("Unable to parse boolean: '"..rest.."'!")
end
elseif first=="S" then
return unescape_chars(rest)
else
+ file:close()
error("Unknown literal type '"..first.."' for literal '"..str.."'!")
end
end
@@ -177,20 +185,20 @@ config = {
}
]]
--- Writes the passed table into the passed file descriptor, and closes the file afterwards
+-- 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")
write_table(root_table, file, config)
file:write("E\nEND_SER\n")
- file:close()
end
-- Reads the file contents from the passed file descriptor and returns the table on success
--- Throws errors when something is wrong.
+-- 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
+ file:close()
error("Expected header, got '"..first_line.."' instead!")
end
local t = {}
@@ -209,7 +217,7 @@ function write_to_file(root_table, filename, config)
-- try opening the file
local file, err = io.open(filename, "w")
if not file then
- error("Failed opening file '"..filename.."' for write: "..err)
+ error("Failed opening file '"..filename.."' for write:\n"..err)
end
write_to_fd(root_table, file, config)
@@ -221,7 +229,7 @@ function read_from_file(filename)
-- try opening the file
local file, err = io.open(filename, "r")
if not file then
- error("Failed opening file '"..filename.."' for read: "..err)
+ error("Failed opening file '"..filename.."' for read:\n"..err)
end
return read_from_fd(file)