|
|
||
|
On 4/15/2010 10:44 AM, Jerome Vuarand wrote:
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).
That looks good. Though it's still stream oriented and I was thinking of late parsing where the data object keeps the string intact and only unpacks the necessary bytes when indexed. At least for structs with known sizes. Throw in a variable-length field and you're back to stream parsing.
You have 64-bit integers on the to-do list. Feel free to copy the implementation I used in larc. (And if you think something like "64bit integer" or "integer.64" would be better, I'm willing to change.)
Anyway, I made a list of the struct-like libraries I knew of and added it to http://lua-users.org/wiki/ReadWriteFormat including the sample code from the recent questions about reading floats. Any other packing/unpacking tips can go there.
-- - tom telliamed@whoopdedo.org