table.save/table.load with error reporting?

Post Reply
SilverDragon
Posts: 23
Joined: Tue Mar 01, 2011 10:00 pm

table.save/table.load with error reporting?

Post by SilverDragon »

So I have a couple functions to save/load tables that overlap the Mudlet functions of the same usage.

Code: Select all

function tableSave(name,tablename)
	if string.char(getMudletHomeDir():byte()) == "/" then _sep = "/" else  _sep = "\\" end
	local filename = getMudletHomeDir() .. _sep .. name .. ".lua"
	table.save(filename, tablename)
end -- func

function tableLoad(name,tablename)
	loaded = false
	if string.char(getMudletHomeDir():byte()) == "/" then _sep = "/" else  _sep = "\\" end
	local filename = getMudletHomeDir() .. _sep .. name .. ".lua"
	if (io.exists(filename)) then
		table.load(filename, tablename)
		loaded = true
	else
		loaded = false
	end -- if
return loaded
end -- function

I use these quite extensively because I have a lot of tables that contain data I want to survive between sessions.

while these seem to work fine, I've experienced -some- frustration with the limited abilities these functions impart. For instance, I have to make a whole separate function if I wanted to actually see if it's loaded or not

Code: Select all

function loadSystemTable()
loadTable("systemVariables", sys.var)
if loaded then
    cecho("system variables loaded fine.")
else
    cecho("Error loading system variables.")
end -- if
end -- function
Now on to my actual request. Is there a more efficient way I can make error reporting for these tableSave, tableLoad functions, and is it possible to return more data? Like if the file simply didn't exist, or if the file got corrupted, etc, etc.

And for the second part of my request, anyone mind giving me a nudge in the direction on how to script a compare function, for whenever I exit mudlet, it will compare the tables saved in mudlet, and the tables saved on my computer, and if there are differences, overwrite the ones on the computer with the new data? I ask this, because I currently have the unefficient method of just saving the table after -every- change, and that can bog down my decade old laptop.

Thanks in advance!

phasma
Posts: 191
Joined: Sat Aug 03, 2013 7:00 pm
Discord: phasma#4694

Re: table.save/table.load with error reporting?

Post by phasma »

assert()?

SilverDragon
Posts: 23
Joined: Tue Mar 01, 2011 10:00 pm

Re: table.save/table.load with error reporting?

Post by SilverDragon »

How ironic that a single word answers my question, mostly. i never though of assert() because i had no clue what it did. But thanks to your suggestion, i did some research and i believe that is exactly what i need....now to just figure out the proper way of querying. :)

Post Reply