does read() work with binary files?

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

does read() work with binary files?

David Jeske-2
Hey all,

I recently wrote a lua script which generates a C source file with a
binary symbol which includes the data of a given file.

I use it to roll a "predefined" lua script into my application at build
time. 

It works fine as long as I have it slurp up the ".lua" source file,
however, when I compile the lua file with luac, and then try to slurp it
up, it dosn't work. Not nearly enough characters are read out of the
binary file.

It looks to me like read("."); is having trouble... 

Is read() known to work with binary files?

Is read(".") the right way to get a single byte out of a binary file?

I've attached the bytecomp.lua script which turns a file into a C file
with a character string.


-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]
-- bytecomp.lua
--
-- this byte compiles a string of bytes into a C data structure
--

-- write the preamble

write("/*\n"..
      " * <filename>\n" ..
      " *\n" ..
      " * this is an automatically generated binary file, don't mess with it!\n" ..
      " */\n");

write("\n\n\n");


-- write the octals

local data;
local octals_per_line = 9;
local cur_octal = 0;
local octal_count = 0;
local line_indent = "    ";


write("unsigned char bin_data[] = {\n".. line_indent);

data = read(".");
while (data) do
     write(format("0x%02X, ",ascii(data)));
     cur_octal = cur_octal + 1;
     octal_count = octal_count + 1;
     if (cur_octal >= octals_per_line) then
         cur_octal = 0;
         write("\n" .. line_indent);
     end
     data = read(".");
end;

-- write the last octal

write("0x00\n");

-- write the postamble

write("};\n\n");

write(format("unsigned int bin_len = %d;\n",octal_count));
write("\n\n");

Reply | Threaded
Open this post in threaded view
|

Re: does read() work with binary files?

Roberto Ierusalimschy
> Is read() known to work with binary files?

Currently, "read()" is known *not* to work with binary files, because it
returns a string, and a string cannot have a binary value 0. Therefore,
whenever the binary file has a 0, we have a problem.

  The next version of Lua (3.1, not alpha) supports strings with arbitrary
binary values (that is, they may have 0's inside them), and therefore will be
able to read (and write) binary files.


> Is read(".") the right way to get a single byte out of a binary file?

It will be.


-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: does read() work with binary files?

David Jeske-2
On Thu, May 21, 1998 at 06:05:25PM -0300, Roberto Ierusalimschy wrote:
> > Is read() known to work with binary files?
> 
> Currently, "read()" is known *not* to work with binary files, because it
> returns a string, and a string cannot have a binary value 0. Therefore,
> whenever the binary file has a 0, we have a problem.
> 
>   The next version of Lua (3.1, not alpha) supports strings with arbitrary
> binary values (that is, they may have 0's inside them), and therefore will be
> able to read (and write) binary files.

Sounds great.

> > Is read(".") the right way to get a single byte out of a binary file?
> 
> It will be.

This is great to hear. I've reverted to just "C-bytedumping" the
_source_ form of the lua file instead of the compiled form. When the real
3.1 is released, it sounds like my bytecomp script will work unchanged,
I'll just need to re-change my build process to compile the lua file
before c-bytedumping.


-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]