fixed a security bug in logging, added basic xml support to as3 compiler
[swftools.git] / lib / python / pyutils.c
1 #include <Python.h>
2 #include <stdarg.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6
7 char* setError(char*format, ...)
8 {
9     char buf[1024];
10     int l;
11     va_list arglist;
12     va_start(arglist, format);
13     vsnprintf(buf, sizeof(buf)-1, format, arglist);
14     va_end(arglist);
15     l = strlen(buf);
16     while(l && buf[l-1]=='\n') {
17         buf[l-1] = 0;
18         l--;
19     }
20     return strdup(buf);
21 }
22
23 static int verbose = 0;
24 void mylog(char*format, ...)
25 {
26     char buf[1024];
27     int l;
28     va_list arglist;
29     if(!verbose)
30         return;
31     va_start(arglist, format);
32     vsnprintf(buf, sizeof(buf)-1, format, arglist);
33     va_end(arglist);
34     l = strlen(buf);
35     while(l && buf[l-1]=='\n') {
36         buf[l-1] = 0;
37         l--;
38     }
39     fprintf(stderr, "[SWF] %s\n", buf);
40     fflush(stderr);
41 }
42
43 #define PY_NONE Py_BuildValue("s", 0)
44
45 PyObject* FindMethodMore(PyObject*ret, PyMethodDef f[], PyObject*self, char* a)
46 {
47     if(ret==NULL) {
48         ret = Py_FindMethod(f, self, a);
49     } else {
50         if(!strcmp(a, "__methods__")) {
51             /* we are being dir()ed. Complete the function table */
52             PyObject* add = Py_FindMethod(f, self, a);
53             int t;
54             mylog("taglist_getattr: append common funtions %08x %08x\n", ret, add);
55             for(t=0;t<PyList_Size(add);t++)
56                 PyList_Append(ret, PyList_GetItem(add, t));
57         }
58     }
59     return ret;
60 }
61
62 void dummy_dealloc(PyObject* self)
63 {
64     PyObject_Del(self);
65 }
66
67 PyMethodDef* addMethods(PyMethodDef*obj1, PyMethodDef*obj2) 
68 {
69     int num1=0,num2=0;
70     if(obj1) for(num1=0;obj1[num1].ml_name;num1++);
71     if(obj2) for(num2=0;obj2[num2].ml_name;num2++);
72     PyMethodDef* result = malloc(sizeof(PyMethodDef)*(num1+num2+1));
73     if(obj1)
74         memcpy(result, obj1, num1*sizeof(PyMethodDef));
75     if(obj2)
76         memcpy(&result[num1], obj2, (num2+1)*sizeof(PyMethodDef));
77     if(obj1)
78         free(obj1);
79     return result;
80 }
81 void setVerbosity(int _verbose)
82 {
83     verbose = _verbose;
84     mylog("setting verbosity to %d", verbose);
85 }