lua-users home
lua-l archive

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


2011/10/14 Shaun savage <savages@mozapps.com>:
> I have looked at http://lua-users.org/wiki/StructurePacking
> Ihave tried vstruct ( works, but only read into a table(array), no
> dictionary)
> lunary it looks like it read into table but can not get it to work,
> stream problem I think
>
> It would be easier for me to write a C function and return a dictionary(
> C background)
>
> The documentation for vstruct could be much better, start with a very
> simple working example
>
>
> #!/usr/bin/env lua
>
> require "vstruct"
> require "serial"
>
>
> local f = assert(io.open("SAVE10.GAM","rb"))
>
> local sz = f:seek("end")
> f:seek("set")
> local stream = serial.filestream(f)
> local block = 100
>
> local data = f:read( block)
>
> local fmt = "i4 s27 i4 i1 i1 i1 i1 i1 i1 i1 i1 i1 i1 i1 i1 i1 i1 i1 i1
> i1 i1 i1 i1 i1 i1 i1 i1 i1"
> local lfmt = {
>     { "version", uint32},
>     { "desc", cstring},
>     { "gamtyp", uint8}
> }
> print ( type( lfmt), type(stream), stream, lfmt)
> local out = vstruct.unpack(fmt,data)
> f:seek("set")
> local out2 = serial.read.struct(stream,lfmt) *****
>
> for k,v in pairs(out2) do
>     print( k,v)
> end
>
> ------
> lua: /usr/share/lua/5.1/serial.lua:1147: no function to read field of
> type nil
> stack traceback:
>     [C]: in function 'assert'
>     /usr/share/lua/5.1/serial.lua:1147: in function '_struct'
>     /usr/share/lua/5.1/serial.lua:1162: in function 'struct'
>     struct.lua:25: in main chunk
>     [C]: ?

In the Lunary version, your format is not properly written. It should
look like this:

local lfmt = {
    { "version", "uint32", "le"},
    { "desc", "cstring"},
    { "gamtyp", "uint8"}
}

Note the quotes around the type name, and the endianness for the
multi-byte integer (make sure little-endian is what you need). The
error message is not clear, but it's quite hard to point precisely at
the problem within the complex data definition that Lunary takes as
input.

Since I don't have the binary file you want to load I cannot check it,
so please provide one if what I wrote above doesn't work for you.