seed random from ruby interface
[swftools.git] / lib / action / actioncompiler.c
1 /*
2     Ming, an SWF output library
3     Copyright (C) 2002  Opaque Industries - http://www.opaque.net/
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stdarg.h>
23 #include <string.h>
24
25 #include "libming.h"
26 #include "compile.h"
27 #include "action.h"
28
29 #include "actioncompiler.h"
30
31 static void print_error(const char*format,...)
32 {
33     char buf[1024];
34     int len;
35     va_list arglist;
36     va_start(arglist, format);
37     vsnprintf(buf, sizeof(buf)-1, format, arglist);
38     va_end(arglist);
39
40     len = strlen(buf);
41     while(len>0 && buf[len-1]=='\n') len--;
42     buf[len] = '\n';
43     buf[len+1] = 0;
44     fprintf(stderr, "error: %s", buf);
45 }
46
47 static void print_warn(const char*format,...)
48 {
49     char buf[1024];
50     int len;
51     va_list arglist;
52     va_start(arglist, format);
53     vsnprintf(buf, sizeof(buf)-1, format, arglist);
54     va_end(arglist);
55
56     len = strlen(buf);
57     while(len>0 && buf[len-1]=='\n') len--;
58     buf[len] = '\n';
59     buf[len+1] = 0;
60     printf("%s", buf);
61     fprintf(stderr, "warning: %s", buf);
62 }
63
64 int compileSWFActionCode(const char *script, int version, void**data, int*len)
65 {
66         Buffer b;
67
68         *data = 0;
69         *len = 0;
70
71         SWF_versionNum = version;
72         if(!SWF_error) 
73             SWF_error = print_error;
74         if(!SWF_warn) 
75             SWF_warn = print_warn;
76         
77         /* yydebug = 1; */
78
79         if(version == 4)
80         {
81                 swf4ParseInit(script, 0);
82
83                 if(swf4parse((void *)&b) != 0)
84                         return 0;
85         }
86         else
87         {
88                 swf5ParseInit(script, 0);
89
90                 if(swf5parse((void *)&b) != 0)
91                         return 0;
92         }
93             
94         *data = b->buffer;
95         *len = bufferLength(b);
96
97         //destroyBuffer(b);
98         free(b);
99         
100         return 1;
101 }
102