summaryrefslogtreecommitdiff
path: root/lua2c.c
blob: b2ac181bdf9f8500625c5b53f0e88c5bec87236b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>

int main(int argc, char ** argv) {
    FILE * in, * out;
    size_t size, i;
    char * symbol;

    if (argc != 3) {
        fprintf(stderr, "Usage: lua2c file_in file_out\n");
        exit(-1);
    }

    if (!(in = fopen(argv[1], "rb"))) {
        fprintf(stderr, "Error opening input file %s.\n", argv[2]);
        perror("Opening input file");
        exit(-1);
    }

    if (!(out = fopen(argv[2], "w"))) {
        fprintf(stderr, "Error opening output file %s.\n", argv[3]);
        perror("Opening output file");
        exit(-1);
    }

    fseek(in, 0, SEEK_END);
    size = ftell(in);
    fseek(in, 0, SEEK_SET);

    fprintf(out, "{\nstatic const unsigned char B1[] = {");

    for (i = 0; i < size; i++) {
        if (i % 16 == 0)
            fprintf(out, "\n\t");
        fprintf(out, "0x%02x, ", fgetc(in));
    }

    fprintf(out, "\n};\n\n if (luaL_loadbuffer(L, (const char *)B1, sizeof(B1), \"%s\") == 0) lua_call(L, 0, 0);\n}\n", argv[1]);

    fclose(in);
    fclose(out);

    return 0;
}