This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
NeverCast's profile picture

Metatables For File Access

Started by NeverCast, 24 January 2013 - 02:24 PM
NeverCast #1
Posted 24 January 2013 - 03:24 PM
Posted this code recently in a reply to someone asking about metatables.

Thought maybe someone would actually find it useful.
You can save this as an api but then it'll be apiName.files, up to you how you load the code. I would use dofile or something.

Anyway, here you go.


local ft_mt = {}
ft_mt.__index = function(t,k)
  local name = tostring(k)
  if not fs.exists(name) then return nil end
  local handle = fs.open(name, "r")
  if not handle then return nil end
  local content = handle.readAll()
  handle.close()
  return content
end
ft_mt.__newindex = function(t,k,v)
  local name = tostring(k)
  local handle = fs.open(name, "w")
  if not handle then return end
  handle.write(tostring(v))
  handle.close()
end
files = {}
setmetatable(files, ft_mt)

Basically lets you edit files using files["filename/or/path/here"]
So you can read the file using it's path as they key, or you can set the files content using
the key too.

For example, you might want to keep track of where the turtle is, so maybe you name the files table, database.

Then you can go, database.position = textutils.serialize({5,3,2})
Then you can get your position back!

position = textutils.unserialize(database.position)

It's beautiful right! Easy persistence, You can create as many keys as you like, and you know they'll be there on restart.. I see so much potential for mining programs!

I might eventually modify this to store the type information with the file and provide file stored tables. So you can use a table per usual as if it wasn't anything special.

table.number = 5
– Restart computer –
print(table.number).. prints 5!
KillaVanilla #2
Posted 24 January 2013 - 03:51 PM
I remember someone doing something exactly like this a while ago. It was called "MagicTables" or something similar…
NeverCast #3
Posted 24 January 2013 - 03:53 PM
I guess it could be considered Magic :P/>
Yeah it's probably been done before, this is pretty much the code I posted in a Ask a Pro topic because someone was learning Metatables. It's here if anyone wants it. :)/>
tesla1889 #4
Posted 31 January 2013 - 09:17 PM
metatables are definitely my favorite part of Lua
props to you, man!
KaoS #5
Posted 01 February 2013 - 02:54 AM
I love it, very simplistic. I made something vaguely similar to this but as usual over-complicated it :)/> go metatables
NeverCast #6
Posted 01 February 2013 - 08:53 AM
Thanks guys :D/>
I've not used this much myself yet, but I know when the time is right I'll copy paste this in and it shall be wondrous!
KaoS #7
Posted 01 February 2013 - 09:23 AM
just thought I would let you know that the setmetatable() command returns the main table so you could just say

files=setmetatable({}, ft_mt)
and you can return the filehandle closing as the second param thereby combining 3 lines

  local content = handle.readAll()
  handle.close()
  return content
could just be

  return handle.readAll(), handle.close()

just some useful things I have learned
NeverCast #8
Posted 01 February 2013 - 09:28 AM
And now I've learnt them also!
I like what you did with the file handle, would not have thought of that.

Also was not aware that setmetatable returned anything, Thanks KaoS!
Jan #9
Posted 02 February 2013 - 08:17 AM
I remember someone doing something exactly like this a while ago. It was called "MagicTables" or something similar…
Yep that was me :P/>
http://www.computercraft.info/forums2/index.php?/topic/5463-filemagic-demonstrating-the-power-of-metatables/
Turns out our ideas were not that unique :)/>
NeverCast #10
Posted 05 February 2013 - 12:06 PM
I give you +1 for coming up with such a great idea :P/>