bin2c

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

bin2c

Matt Campbell-2
Hello:

I noticed that bin2c was dropped from Lua 5.1, presumably because lua_dobuffer no longer exists in lauxlib. Will bin2c ever be re-written for Lua 5.1? If not, what's the recommended way to embed Lua chunks in a C program now? Thanks.

--
Matt Campbell
Lead Programmer
Serotek Corporation
www.freedombox.info
"The Accessibility Anywhere People"


Reply | Threaded
Open this post in threaded view
|

Re: bin2c

D Burgess-4
There is a bin2c.lua around (on the Wiki I think).  If not mail me and
I will send it to you.

lua_dobuffer becomes :-

int luaL_loadbuffer (lua_State *L, const char *buff,
                               size_t sz, const char *name);
followed by
lua_call or lua_pcall

David B.

PS. On Windows apps. I found it best to load lua code from resource
files.

On 2/26/06, Matt Campbell <[hidden email]> wrote:
> Hello:
>
> I noticed that bin2c was dropped from Lua 5.1, presumably because
> lua_dobuffer no longer exists in lauxlib.  Will bin2c ever be re-written
> for Lua 5.1?  If not, what's the recommended way to embed Lua chunks in
> a C program now?  Thanks.
>
> --
> Matt Campbell
> Lead Programmer
> Serotek Corporation
> www.freedombox.info
> "The Accessibility Anywhere People"
>
>


Reply | Threaded
Open this post in threaded view
|

Re: bin2c

Matt Campbell-2
D Burgess wrote:
PS. On Windows apps. I found it best to load lua code from resource
files.
I was wondering if that was the best option on Win32, which is currently my primary platform. Looks pretty easy to do, using FindResource, LoadResource, and LockResource. And that way, I can use named resources, with a custom resource type like "LUACHUNK".

--
Matt Campbell
Lead Programmer
Serotek Corporation
www.freedombox.info
"The Accessibility Anywhere People"


Reply | Threaded
Open this post in threaded view
|

Re: bin2c

John Labenski
In reply to this post by D Burgess-4
On 2/25/06, D Burgess <[hidden email]> wrote:
> There is a bin2c.lua around (on the Wiki I think).  If not mail me and
> I will send it to you.

There's a bin2c.lua that I wrote here
http://cvs.sourceforge.net/viewcvs.py/wxlua/wxLua/util/bin2c/bin2c.lua?view=markup

It can transform binary or text files to const char* strings. The text
file implementation maintains lines so that if you import it into CVS
the diffs aren't so ugly. If you set it to output to a file it only
updates it if there are differences.

Regards,
    John Labenski


Reply | Threaded
Open this post in threaded view
|

Re: bin2c

D Burgess-4
In reply to this post by Matt Campbell-2
Matt Campbell wrote:
> I was wondering if that was the best option on Win32, which is currently
> my primary platform.  Looks pretty easy to do, using FindResource,
> LoadResource, and LockResource.  And that way, I can use named
> resources, with a custom resource type like "LUACHUNK".

I dont know if its the best option but it works well for me and it is quick.

In lua code:
 chunk = loadrc(resource  [,dll])

We have used this scheme for "packaging" lua add-ons. For example, you
can place luasocket in one DLL by converting the lua code to resources.
This makes it a tidy way to distribute an application.

There is a slightly out of date bin2res.lua at
http:///www.spinifex.net/db/public/bin2res.zip
You should be able to adjust this to your needs.

David B.


Reply | Threaded
Open this post in threaded view
|

Re: bin2c

John D. Ramsdell-2
In reply to this post by Matt Campbell-2
The bin2c program I use is available here:

http://www.ccs.neu.edu/home/ramsdell/tools/bin2c.c

It's not Lua specific--it generates no calls to Lua library functions.
Instead, the generated C source file defines a string with the file
name of the source of data, and an unsigned character array containing
the binary data.

For example, if the source file is dl.lua, the generated file
contains:

static const char dl_lua_source[] = "dl.lua";

static const unsigned char dl_lua_bytes[] = {
...
};

For a Lua application, some C file would contain something like:

#include "dl.h"
static int
dl_lua(lua_State *L) {
  return lua_dobuffer(L, (const char*)dl_lua_bytes, 
		      sizeof(dl_lua_bytes), dl_lua_source);
}

The header file may be generated with the following GNUMakefile rule:

%.h:    %.lua
        luac -o $*.luo $<
        bin2c -o $@ -n $< $*.luo
        rm $*.luo

John

Reply | Threaded
Open this post in threaded view
|

Re: bin2c

Luiz Henrique de Figueiredo
In reply to this post by Matt Campbell-2
> I noticed that bin2c was dropped from Lua 5.1, presumably because 
> lua_dobuffer no longer exists in lauxlib.

No, it was dropped because it was easy to write it in Lua and we want
fewer things in the tarball, specially peripheral things like bin2c.

> Will bin2c ever be re-written for Lua 5.1?

Not officially, but it's easy to write your own, as previous posts in this
thread have shown.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: bin2c

Antonio Scuri

Just to notice, LuaBinaries distribution in LuaForge will contain the bin2c binary for Lua 5.1.

  The bin2c for Lua 5.1 beta is broken.

Best,
scuri

At 20:28 1/3/2006, Luiz Henrique de Figueiredo wrote:
> I noticed that bin2c was dropped from Lua 5.1, presumably because
> lua_dobuffer no longer exists in lauxlib.

No, it was dropped because it was easy to write it in Lua and we want
fewer things in the tarball, specially peripheral things like bin2c.

> Will bin2c ever be re-written for Lua 5.1?

Not officially, but it's easy to write your own, as previous posts in this
thread have shown.
--lhf