Bug 22803 - Make header conform to implementation
[swfdec.git] / swfdec / compiler.c
blob5674bb99488f163bda2572902c9454813800bcb4
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, int len)
17 int 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 int len;
64 gsize ignored;
65 byte *data;
67 if (argc < 2) {
68 g_print ("usage: %s FILE ...\n\n", argv[0]);
69 return 1;
72 Ming_init ();
73 Ming_setSWFCompression (-1);
75 g_print ("/* This file is autogenerated, do not edit! */\n\n");
76 for (i = 1; i < argc; i++) {
77 if (!g_file_get_contents (argv[1], &contents, NULL, &error)) {
78 g_printerr ("Error: %s\n", error->message);
79 g_error_free (error);
80 error = NULL;
81 return 1;
83 action = newSWFAction (contents);
84 if (SWFAction_compile (action, 6, &len) != 0) {
85 g_printerr ("Error: Couldn't compile\n");
86 return -1;
88 data = SWFAction_getByteCode (action, &ignored);
89 contents = get_name (argv[i]);
90 g_print ("/* compiled from %s */\n", argv[i]);
91 g_print ("static const unsigned char %s[] = {\n", contents);
92 g_free (contents);
93 write_data (data, len);
94 g_print ("};\n\n");
96 return 0;