lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2010/4/15 Tom N Harris <telliamed@whoopdedo.org>:
> And as I write this, I'm getting an idea for a different way to access
> packed data using tables. You compile a description of the data, which looks
> something like a C struct definition but with slightly more logic (a Pascal
> string could be "uint8_t len; char[len] name;") When attached to a string it
> can act like a table to unpack fields individually. But at the moment I
> don't want to be distracted from Larc. Maybe some other time or if anyone
> else wants to use the idea.

I've written such a high level library : http://lunary.luaforge.net/

You can describe structures with named fields with several methods.
Your Pascal string example though has its own built-in type
(sizedbuffer). Here is a short example for a structure with a few
fields :

local str = file:read("*a")
local position = serial.read.struct(serial.buffer(str), {
    {'x', 'float', 'le'},
    {'y', 'float', 'le'},
    {'angle', 'uint16', 'le'},
})

The call to serial.buffer is to embed the string into a "stream"
object (I guess I could make that embedding automatic in a future
version).