--Copyright (C) Paul O'Reilly, [email protected]
--[[
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
The software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. in no event shall the author be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
]]
os.loadAPI("/strutils/StrUtils")
os.loadAPI("/immibis/serialize")
persistent_path = "/var"
if fs.exists( persistent_path ) == false then fs.makeDir( persistent_path ) end
function _doNothing( value ) return value end
persistent_encryption = _doNothing
persistent_decryption = _doNothing
function _getpersistentVariablePath( path, name )
-- will create path if required
local complete_path = persistent_path..'/'..path
local path_fragments = StrUtils.seperate(complete_path, "/")
local current_path = ""
for k,v in pairs( path_fragments ) do
current_path = current_path.."/"..v
if not fs.exists( current_path ) then fs.makeDir( current_path ) end
end
return complete_path.."/"..name..".var"
end
function exists( path, name )
local path = _getpersistentVariablePath( path, name )
return fs.exists( path )
end
function delete( path, name )
local variable_path = _getpersistentVariablePath( path, name )
if exists( path, name ) then fs.delete( variable_path )
else return "Variable doesn't exist"
end
end
function update( path, name, value )
local variable_path = _getpersistentVariablePath( path, name )
local as_string = serialize.serialize( value )
if not ( persistent_encryption == nil ) then
as_string = persistent_encryption( as_string )
end
local persistent_file = io.open( variable_path, "w" )
if persistent_file then
persistent_file:write( as_string )
persistent_file:close()
else return "Error opening file" end
end
function read( path, name )
local variable_path = _getpersistentVariablePath( path, name )
if fs.exists( variable_path ) then
local persistent_file = io.open( variable_path, "r" )
if persistent_file then
content = persistent_file:read("*a")
persistent_file:close()
if not ( persistent_decryption == nil ) then
content = persistent_decryption( content )
end
return serialize.deserialize( content )
else return "Error opening file" end
else return "Variable doesn't exist"
end
end