callback implementation details..

classic Classic list List threaded Threaded
25 messages Options
12
Reply | Threaded
Open this post in threaded view
|

RE: loadstring (...)

Benoit Germain
Hi,

-----Message d'origine-----
De : Luiz Henrique de Figueiredo [[hidden email]]
Envoyé : jeudi 23 octobre 2003 12:12
À : [hidden email]
Objet : Re: loadstring (...)

...
> Yes. It's in our plans for 5.1: chunks will be vararg functions.

Meaning that each time a chunk is executed, there will be the overhead of
taking the arguments on the stack and making a table of them ? Wouldn't it
be simpler to be able to name the parameters of the chunk, just like we name
the parameters of any function ? (i.e. have the very same possibilities for
'chunk-generated' functions than for 'regular' ones regarding parameter
handling) And then, if one wants to make a varag function from the chunk,
fine, but please give choice a chance :-)


Cheers,


Benoit.

Reply | Threaded
Open this post in threaded view
|

RE: loadstring (...)

Luiz Henrique de Figueiredo
>> Yes. It's in our plans for 5.1: chunks will be vararg functions.
>
>Meaning that each time a chunk is executed, there will be the overhead of
>taking the arguments on the stack and making a table of them ?

No, that's exactly the point: we plan to get rid of the automatic table
creation! We plan to let "..." stand for the list of arguments. If you need
a table, you'll be able to write {...}. More on that later...
--lhf

Reply | Threaded
Open this post in threaded view
|

Using binary files

jose marin2
In reply to this post by Reuben Thomas-5
Hi!

How do I deal with binary files in Lua?

I want to create a binary file using Lua code
to be used for a C++ program.

In lua:

file = fopen("file.dat", "wb")
fwritestring(file, "estutura1")
fwriteint(file, 12)
fwritefloat(file, 3.14)

fwritestring(file, "estutura2")
fwriteint(file, 33)
fwritefloat(file, 44.55)

fclose(file)

In C++

int n;
float f;
char str[STR_MAX];
fp = fopen("file.dat", "rb");

fread(str, STR_MAX, 1, fp);
fread(&, sizeof(int), 1, fp);
.
.
.

Is that possible?


Also, is possible to write to a file, in Lua, that a
C++ program could read as a struct?
something like this:

struct{

  char name[STR_MAX];
  int n;
  float f;
}struct_aux;


fread(&struct_aux, sizeof(struct_aux), 1, fp);

______________________________________________________________________

Yahoo! Mail: 6MB, anti-spam e antivírus gratuito! Crie sua conta agora:
http://mail.yahoo.com.br

Reply | Threaded
Open this post in threaded view
|

RE: Using binary files

jmckenn
> From: Jose Marin [[hidden email]] 

> How do I deal with binary files in Lua?

It's not hard to write a set of functions for reading and writing integers
in your platform's native format.  Here's mine (for a 32 bit little-endian
machine):

function readbyte( file )
	local str = file:read( 1 )
	return string.byte( str, 1 )
end

function writebyte( file, value )
	file:write( string.char(value) )
end

function readshort( file )
	local str = file:read( 2 )
	return string.byte( str, 1 ) + 256*string.byte( str, 2 )
end

function writeshort( file, value )
	local h = math.floor(value / 256)
	local l = value - 256*h
	writebyte( file, l )
	writebyte( file, h )
end

function readint( file )
	local str = file:read( 4 )
	return string.byte( str, 1 ) + 256*string.byte( str, 2 ) + 
		256*256*string.byte( str, 3 ) + 256*256*256*string.byte(
str, 4 )
end

function writeint( file, value )
	local h = math.floor(value / 65536)
	local l = value - 65536*h
	writeshort( file, l )
	writeshort( file, h )
end

floating point is a little trickier, but nothing impossible.  This one reads
a subset of IEEE single precision (no infinities or NANs, and I've never
tested denormalised):

function readfloat( file )
	local int = readint( file )
	local f = 1 + mod( int, 128*256*256 )/(128*256*256)
	local e = math.floor( int/(128*256*256) )
	local s
	if e >= 256 then
		e = e - 256
		s = -1
	else
		s = 1
	end
	e = e - 127
	return s * f * 2^e
end

I haven't written writefloat because so far I haven't needed it.

> Also, is possible to write to a file, in Lua, that a
> C++ program could read as a struct?

I do it all the time, but you have to be very careful.  In particular, you
need to know if and how the compiler adds alignment padding.  In your
example

> struct{
>   char name[STR_MAX];
>   int n;
>   float f;
> }struct_aux;

I'd explicitly add padding after the char array, if STR_MAX isn't a multiple
of 4 (the machine I build data files for has 32 bit int and float, and 8 bit
char).

Don't expect this sort of thing to be remotely portable.  I've had personal
experience of CPU/compiler combinations with 8 or 16 bit chars, 16 or 32 bit
ints, and 32 or 64 bit long ints.

Reply | Threaded
Open this post in threaded view
|

Re: Using binary files

Luiz Henrique de Figueiredo
In reply to this post by jose marin2
See my lpack library at http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/ .

--lhf

12