copy/paste caused some issues here
[swfdec.git] / libswfdec / compiler.c
blob7299dd6947a0119f68fc2328be5bb24a6cee9a35
1 //gcc -Wall -Werror `pkg-config --libs --cflags libming glib-2.0` compiler.c -o compiler
3 #include <glib.h>
4 #include <ming.h>
5 #include <string.h>
7 /* This is what is used to compile the Actionscript parts of the source to
8 * includable C data that can later be executed.
9 * Note that this is pretty much a hack until someone writes a proper
10 * Actionscript compiler for Swfdec.
11 * Also note that the creation of the include-scripts should probably not be
12 * autorun, as we don't want to depend on external bugs, only on internal ones.
14 static gboolean
15 write_data (guint8 *data, gsize len)
17 gsize i;
19 for (i = 0; i < len; i++) {
20 switch (i % 16) {
21 case 0:
22 if (i == 0)
23 g_print (" 0x%02X", data[i]);
24 else
25 g_print (",\n 0x%02X", data[i]);
26 break;
27 case 4:
28 case 8:
29 case 12:
30 g_print (", 0x%02X", data[i]);
31 break;
32 default:
33 g_print (", 0x%02X", data[i]);
34 break;
37 g_print ("\n");
38 return TRUE;
41 static char *
42 get_name (const char *filename)
44 char *end;
46 end = strrchr (filename, '/');
47 if (end)
48 filename = end + 1;
49 end = strchr (filename, '.');
50 if (end)
51 return g_strndup (filename, end - filename);
52 else
53 return g_strdup (filename);
56 int
57 main (int argc, char **argv)
59 SWFAction action;
60 char *contents;
61 GError *error = NULL;
62 guint i;
63 size_t len;
64 byte *data;
66 if (argc < 2) {
67 g_print ("usage: %s FILE ...\n\n", argv[0]);
68 return 1;
71 Ming_init ();
72 Ming_setSWFCompression (-1);
74 g_print ("/* This file is autogenerated, do not edit! */\n\n");
75 for (i = 1; i < argc; i++) {
76 if (!g_file_get_contents (argv[1], &contents, NULL, &error)) {
77 g_printerr ("%s\n", error->message);
78 g_error_free (error);
79 error = NULL;
80 return 1;
82 action = newSWFAction (contents);
83 data = SWFAction_getByteCode (action, &len);
84 contents = get_name (argv[i]);
85 g_print ("/* compiled from %s */\n", argv[i]);
86 g_print ("const unsigned char %s[] = {\n", contents);
87 g_free (contents);
88 write_data (data, len);
89 g_print ("};\n\n");
91 return 0;