Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/lua-5.1.3/etc/bin2c.c @ 28

Last change on this file since 28 was 28, checked in by landauf, 16 years ago
File size: 1.5 KB
Line 
1/*
2* bin2c.c
3* convert files to byte arrays for automatic loading
4* Luiz Henrique de Figueiredo (lhf@tecgraf.puc-rio.br)
5* Fixed to Lua 5.1. Antonio Scuri (scuri@tecgraf.puc-rio.br)
6* Generated files will work also for Lua 5.0
7* 08 Dec 2005
8*/
9
10#include <ctype.h>
11#include <stdio.h>
12#include <stdlib.h>
13
14static void dump(FILE* f, int n)
15{
16  printf("static const unsigned char B%d[]={\n",n);
17  for (n=1;;n++)
18  {
19    int c=getc(f); 
20    if (c==EOF) break;
21    printf("%3u,",c);
22    if (n==20) { putchar('\n'); n=0; }
23  }
24  printf("\n};\n\n");
25}
26
27static void fdump(const char* fn, int n)
28{
29  FILE* f= fopen(fn,"rb");              /* must open in binary mode */
30  if (f==NULL)
31  {
32    fprintf(stderr,"bin2c: cannot open ");
33    perror(fn);
34    exit(1);
35  }
36  else
37  {
38    printf("/* %s */\n",fn);
39    dump(f,n);
40    fclose(f);
41  }
42}
43
44static void emit(const char* fn, int n)
45{
46  printf(" if (luaL_loadbuffer(L,(const char*)B%d,sizeof(B%d),\"%s\")==0) lua_pcall(L, 0, 0, 0);\n",n,n,fn);
47}
48
49int main(int argc, char* argv[])
50{
51  printf("/* code automatically generated by bin2c -- DO NOT EDIT */\n");
52  printf("{\n");
53  if (argc<2)
54  {
55    dump(stdin,0);
56    emit("=stdin",0);
57  }
58  else
59  {
60    int i;
61    printf("/* #include'ing this file in a C program is equivalent to calling\n");
62    for (i=1; i<argc; i++) printf("  if (luaL_loadfile(L,\"%s\")==0) lua_pcall(L, 0, 0, 0); \n",argv[i]);
63    printf("*/\n");
64    for (i=1; i<argc; i++) fdump(argv[i],i);
65    for (i=1; i<argc; i++) emit(argv[i],i);
66  }
67  printf("}\n");
68  return 0;
69}
Note: See TracBrowser for help on using the repository browser.