diff options
author | Determinant <[email protected]> | 2015-05-24 20:35:40 +0800 |
---|---|---|
committer | Determinant <[email protected]> | 2015-05-24 20:35:40 +0800 |
commit | 3ccca6ba1eb7b6732036f4128c977c0b02ef3836 (patch) | |
tree | 150ea05f65a2cc041433a601a83c4c887a7bcc86 /nerv.lua | |
parent | 5d1cb5619ea720968e01ec9a63422b2cc30295ef (diff) |
add write functionality to ParamFile
Diffstat (limited to 'nerv.lua')
-rw-r--r-- | nerv.lua | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -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 |