parrotcode: struct library | |
Contents | Language Implementations | Lua |
lib/struct.pir - struct library
See original on http://www.inf.puc-rio.br/~roberto/struct/
NOT UNDER TEST
This library offers basic facilities to convert Lua values to and from C structs.
Its main functions are struct.pack
,
which packs multiple Lua values into a struct-like string; and struct.unpack
,
which unpacks multiple Lua values from a given struct-like string.
The fist argument to both functions is a format string, which describes the layout of the structure. The format string has the following syntax:
* First,
it may contain a endianness flag,
which governs all conversions for that structure.
"<"
means little-endian; "
"> means big-endian.
When no endian flag is given,
the functions use the machine's native endianness.
* The next optional item is a alignment flag,
in the form "!n"
,
where n is the maximum required alignment (necessarily a power of 2).
An absent n means the machine's native alignment.
An absent alignment flag means no alignment at all (which is the same as !1
).
* After those optional flags, the format string contains a sequence of zero or more of the following conversion elements:
"x"
"b"
signed char
."B"
unsigned char
."h"
signed short
(native size)."H"
unsigned short
(native size)."l"
signed long
(native size)."L"
unsigned long
(native size)."in"
int
."In"
"in"
but unsigned."f"
float
(native size)."d"
double
(native size)."s"
"cn"
"c0"
"cn"
,
except that the n is given by other means.
When packing,
n is the length of the given string.
When unpacking,
n is the value of the previous unpacked value (which must be a number).
In that case,
this previous value is not returned.All functions are registered inside a table struct
.
struct.pack (fmt, d1, d2, ...)
d1
,
d2
,
ect.
packed according to the format string fmt
.struct.unpack (fmt, s, [i])
s
according to the format string fmt
.
An optional i
marks where in s
to start reading (default is 1).
After the read values,
this function also returns the index in s
where it stopped reading,
which is also where you should start to read the rest of the string.struct.size (fmt)
fmt
.
For obvious reasons,
the format string cannot contains neither the option s
nor the option c0
.Francois Perrad
|