aboutsummaryrefslogtreecommitdiff
path: root/nerv.lua
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2015-05-24 20:35:40 +0800
committerDeterminant <[email protected]>2015-05-24 20:35:40 +0800
commit3ccca6ba1eb7b6732036f4128c977c0b02ef3836 (patch)
tree150ea05f65a2cc041433a601a83c4c887a7bcc86 /nerv.lua
parent5d1cb5619ea720968e01ec9a63422b2cc30295ef (diff)
add write functionality to ParamFile
Diffstat (limited to 'nerv.lua')
-rw-r--r--nerv.lua37
1 files changed, 37 insertions, 0 deletions
diff --git a/nerv.lua b/nerv.lua
index 33b1aff..1c4ba39 100644
--- a/nerv.lua
+++ b/nerv.lua
@@ -1,5 +1,6 @@
require 'libnerv'
require 'matrix.init'
+require 'io.init'
-- nerv.class = require 'pl.class'
nerv.utils = require 'pl.utils'
@@ -35,3 +36,39 @@ function nerv.class(tname, parenttname)
end
return mt, mpt
end
+
+function table.val_to_str(v)
+ if "string" == type(v) then
+ v = string.gsub(v, "\n", "\\n")
+ if string.match(string.gsub(v,"[^'\"]",""), '^"+$') then
+ return "'" .. v .. "'"
+ end
+ return '"' .. string.gsub(v,'"', '\\"') .. '"'
+ else
+ return "table" == type(v) and table.tostring(v) or
+ tostring(v)
+ end
+end
+
+function table.key_to_str (k)
+ if "string" == type(k) and string.match(k, "^[_%a][_%a%d]*$") then
+ return k
+ else
+ return "[" .. table.val_to_str(k) .. "]"
+ end
+end
+
+function table.tostring(tbl)
+ local result, done = {}, {}
+ for k, v in ipairs(tbl) do
+ table.insert(result, table.val_to_str(v))
+ done[k] = true
+ end
+ for k, v in pairs(tbl) do
+ if not done[k] then
+ table.insert(result,
+ table.key_to_str(k) .. "=" .. table.val_to_str(v))
+ end
+ end
+ return "{" .. table.concat(result, ",") .. "}"
+end