[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: opening a file in binary mode
- From: "Honglei Tu" <honglei@...>
- Date: Tue, 20 Mar 2001 17:42:42 -0800
Hello everyone,
Do you know how to call Lua functions recursively.
Such as I have a Lua function
function gettable(t)
local numEntries, e, name, flag, value
numEntries = GetNumEntriesForTable(t)
print("number of entries is ", numEntries)
for i=1, numEntries do
e = GetTableEntryByIndex(b, i)
name = GetEntryName(e)
flag = GetEntryFlag(e)
value = GetEntryValue(e)
print("name is ", name)
print("flag is ", flag)
print("value is ", value)
if (flag == 32)
then
gettable(value)
end
end
end
But I failed to recursively call gettable in gettable.
Anyone can tell me how to call lua functions recursively ?
Honglei
-----Original Message-----
From: owner-lua-l@tecgraf.puc-rio.br
[mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of Luiz Henrique de
Figueiredo
Sent: Tuesday, March 20, 2001 5:00 PM
To: Multiple recipients of list
Subject: Re: opening a file in binary mode
>In any case, how do I open a file in binary and write binary data to it? (I
don''t want my numbers converted to strings, as that's what seems to be
happening)
You open the file with openfile(name,"wb"), as you already know ("w+b" if
you
want to append instead of overwriting).
To write binary data, you could in principle use strchar(byte) as Pedro
Miller
suggested, but that assumes that you know how to break your binary data into
bytes. For integers, it's simple with a sequence of divisions by 256 (or
you could use "%08X" in format and then write strchar(byte) of each 2 hex
digits). For numbers with fractional part, it's harder, but you may get by
by using frexp. You might prefer to write a simple library that does this
for
you. Something like this (untested!):
static tobinary(lua_State* L)
{
if arg is number then
void* u=lua_newuserdata(L,sizeof(double));
double x=lua_tonumber(L,1));
memcpy(u,&x,sizeof(x));
else if arg is string
; /* do nothing */
return 1;
}
and export this to Lua. Then you'd simply do write(tobinary(x)) in Lua.
--lhf
<<attachment: winmail.dat>>