lua-users home
lua-l archive

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


Ignacio Castaño wrote:
Hi,

I just noticed there aren't any functions for reading/writting binary files
in the lua standard library, I would like to suggest the addition of them.
Something like:

fh:readb( "pattern", [num] )
fh:writeb( "pattern", ... )

where pattern would be something like: "ub", "sd", "df" for unsigned byte,
signed dword and double float.

I've had the experience of roughly implementing something like this for Lua.

Instead of directly reading and writing to files, you will want to be more general and operate on Lua strings, since they can hold binary data. There are several issues to complicate things. For example, the pattern syntax must be flexible enough to contain byte ordering and alignment info. See Perl's pack/unpack (http://www.perldoc.com/perl5.6/pod/func/pack.html).

Lua has limited data types, and so in the end is not so well suited for working with binary data. For example, with the standard double numbers, you cannot work with 64-bit integers. Furthermore some users set the number type to float, and then they can't even work with 32-bit integers.

If something like this is put into a library, obviously that library will have to offer binary operations also. Otherwise you have no way to look at flags and file headers, for example.

-John


--
http:// i      .   /