summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lua2c.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/lua2c.c b/lua2c.c
new file mode 100644
index 0000000..b2ac181
--- /dev/null
+++ b/lua2c.c
@@ -0,0 +1,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;
+}
+