memory management and logging fixes
[swftools.git] / lib / python / taglist.c
1 #include <Python.h>
2 #undef HAVE_STAT
3 #include "../rfxswf.h"
4 #include "../log.h"
5 #include "./pyutils.h"
6 #include "primitives.h"
7 #include "action.h"
8 #include "tag.h"
9 #include "tagmap.h"
10 #include "taglist.h"
11
12 //----------------------------------------------------------------------------
13 typedef struct {
14     PyObject_HEAD
15     PyObject* taglist;
16 } TagListObject;
17 //----------------------------------------------------------------------------
18 PyObject * taglist_new()
19 {
20     TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
21     mylog("+%08x(%d) taglist_new", (int)taglist, taglist->ob_refcnt);
22     taglist->taglist = PyList_New(0);
23     return (PyObject*)taglist;
24 }
25 //----------------------------------------------------------------------------
26 PyObject * taglist_new2(TAG*tag)
27 {
28     TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
29     mylog("+%08x(%d) taglist_new2 tag=%08x", (int)taglist, taglist->ob_refcnt, tag);
30     PyObject* tagmap = tagmap_new();
31
32     swf_FoldAllTags(tag);
33
34     int nr=0;
35     TAG*t = tag;
36     TAG*last = t;
37     while(t) {nr++;last=t;t=t->next;}
38
39     if(last && last->id==ST_END) {
40         swf_DeleteTag(last);
41         nr--;
42     }
43
44     taglist->taglist = PyList_New(nr);
45     
46     mylog("+%08x(%d) taglist_new2: %d items", (int)taglist, taglist->ob_refcnt, nr);
47
48     nr = 0;
49     t = tag;
50     while(t) {
51         PyObject*newtag = tag_new2(t, tagmap);
52         if(newtag==NULL) {
53             // pass through exception
54             Py_DECREF(tagmap);
55             return NULL;
56         }
57         PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
58         if(swf_isDefiningTag(t)) {
59             int id = swf_GetDefineID(t);
60             tagmap_addMapping(tagmap, id, newtag);
61         }
62         nr++;
63         t=t->next;
64     }
65     Py_DECREF(tagmap);
66     return (PyObject*)taglist;
67 }
68 //----------------------------------------------------------------------------
69 TAG* taglist_getTAGs(PyObject*self)
70 {
71     PyObject* tagmap = tagmap_new();
72     if(!PY_CHECK_TYPE(self,&TagListClass)) {
73         PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
74         return 0;
75     }
76     TagListObject*taglist = (TagListObject*)self;
77
78     /* TODO: the tags will be modified by this. We should set mutexes. */
79     
80     int l = PyList_Size(taglist->taglist);
81     int t;
82     TAG* tag = 0;
83     TAG* firstTag = 0;
84     mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
85     for(t=0;t<l;t++) {
86         PyObject*item = PyList_GetItem(taglist->taglist, t);
87         tag = tag_getTAG(item, tag, tagmap);
88         if(!tag) {
89             //pass through errors
90             Py_DECREF(tagmap);
91             return 0;
92         }
93         if(!firstTag)
94             firstTag = tag;
95     }
96     Py_DECREF(tagmap);
97     return firstTag;
98 }
99 //----------------------------------------------------------------------------
100 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
101 {
102 /*    SWF swf;
103     TagListObject*taglist = (TagListObject*)self;
104     if(!self || !PyArg_ParseTuple(args,"")) 
105         return NULL;
106     swf.firstTag = taglist->firstTag;
107     swf_FoldAll(&swf);
108     taglist->firstTag = swf.firstTag;
109     taglist->lastTag = 0; // FIXME
110     taglist->searchTag = 0;*/
111     return PY_NONE;
112 }
113 //----------------------------------------------------------------------------
114 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
115 {
116     SWF swf;
117 /*    TagListObject*taglist = (TagListObject*)self;
118     if(!self || !PyArg_ParseTuple(args,"")) 
119         return NULL;
120     swf.firstTag = taglist->firstTag;
121     swf_UnFoldAll(&swf);
122     taglist->firstTag = swf.firstTag;
123     taglist->lastTag = 0; // FIXME
124     taglist->searchTag = 0;*/
125     return PY_NONE;
126 }
127 //----------------------------------------------------------------------------
128 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
129 {
130     SWF swf;
131 /*    TagListObject*taglist = (TagListObject*)self;
132     if(!self || !PyArg_ParseTuple(args,"")) 
133         return NULL;
134     swf.firstTag = taglist->firstTag;
135     swf_UnFoldAll(&swf);
136     taglist->firstTag = swf.firstTag;
137     taglist->lastTag = 0; // FIXME
138     taglist->searchTag = 0;*/
139     return PY_NONE;
140 }
141 //----------------------------------------------------------------------------
142 static void taglist_dealloc(PyObject* self)
143 {
144     TagListObject*taglist = (TagListObject*)self;
145     mylog("-%08x(%d) taglist_dealloc list=%08x(%d)\n", (int)self, self->ob_refcnt, taglist->taglist, taglist->taglist->ob_refcnt);
146     Py_DECREF(taglist->taglist);
147     taglist->taglist = 0;
148     PyObject_Del(self);
149 }
150 //----------------------------------------------------------------------------
151 static PyMethodDef taglist_functions[] =
152 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
153  {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
154  {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
155  {NULL, NULL, 0, NULL}
156 };
157
158 static PyObject* taglist_getattr(PyObject * self, char* a)
159 {
160     PyObject* ret = Py_FindMethod(taglist_functions, self, a);
161     mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
162     return ret;
163 }
164 //----------------------------------------------------------------------------
165 static int taglist_length(PyObject * self)
166 {
167     TagListObject*tags = (TagListObject*)self;
168     mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
169     return PyList_GET_SIZE(tags->taglist);
170 }
171 //----------------------------------------------------------------------------
172 static int taglist_contains(PyObject * self, PyObject * tag)
173 {
174     /* TODO: optimize! */
175     TagListObject*taglist = (TagListObject*)self;
176     PyObject*list = taglist->taglist;
177     int l = PyList_Size(list);
178     int t;
179     for(t=0;t<l;t++) {
180         PyObject*item = PyList_GetItem(list, t);
181         if(item == tag) {
182             return 1;
183         }
184     }
185     return 0;
186 }
187 //----------------------------------------------------------------------------
188 static PyObject * taglist_concat(PyObject * self, PyObject* list)
189 {
190     PyObject*tag = 0;
191     PY_ASSERT_TYPE(self, &TagListClass);
192     TagListObject*taglist = (TagListObject*)self;
193     mylog(" %08x(%d) taglist_concat %08x(%d)", (int)self, self->ob_refcnt, list, list->ob_refcnt);
194
195     if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
196         list = tag_getDependencies(tag);
197         int l = PyList_Size(list);
198         int t;
199         for(t=0;t<l;t++) {
200             PyObject*item = PyList_GetItem(list, t);
201             PyObject*_self = taglist_concat(self, item);
202             Py_DECREF(self);
203             self = _self;
204         }
205         if(!taglist_contains(self, tag)) {
206             mylog(" %08x(%d) taglist_concat: Adding Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
207             PyList_Append(taglist->taglist, tag);
208         } else {
209             mylog(" %08x(%d) taglist_concat: Already contains Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
210         }
211         Py_INCREF(self);
212         return self;
213         /* copy tag, so we don't have to do INCREF(tag) (and don't
214            get problems if the tag is appended to more than one
215            taglist) */
216         /* TODO: handle IDs */
217         /*      
218         TAG*t = tag_getTAG(tag);
219         TAG*nt = 0;
220         mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
221         // copy tag
222         nt = swf_InsertTag(0, t->id);
223         swf_SetBlock(nt,t->data,t->len);
224         PyObject*newtag = tag_new(taglist->swf, nt);
225         if(swf_isDefiningTag(t)) {
226             int id = swf_GetDefineID(t);
227             PyObject*id = PyLong_FromLong(id);
228             PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
229             Py_INCREF(id);
230             PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
231             Py_INCREF(id);
232         }
233         Py_INCREF(self);
234         return self;*/
235     }
236     PyErr_Clear();
237     if (PyList_Check(list)) {
238         int l = PyList_Size(list);
239         int t;
240         for(t=0;t<l;t++) {
241             PyObject*item = PyList_GetItem(list, t);
242             if(!PY_CHECK_TYPE(item, &TagClass)) {
243                 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
244                 return 0;
245             }
246             PyObject*_self = taglist_concat(self, item);
247             Py_DECREF(self);
248             self = _self;
249             if(!self)
250                 return 0;
251         }
252         Py_INCREF(self);
253         return self;
254     }
255     PyErr_Clear();
256     if (PY_CHECK_TYPE(list, &TagListClass)) {
257         TagListObject*taglist2 = (TagListObject*)list;
258         return taglist_concat(self, taglist2->taglist);
259
260         /*TAG* tags = taglist_getTAGs(self);
261         TAG* tags2 = taglist_getTAGs(list);
262         TAG* tags3;
263         tags3 = swf_Concatenate(tags,tags2);
264         PyObject* newtaglist = taglist_new(tags3);
265         swf_FreeTags(tags3);
266         Py_INCREF(newtaglist);*/
267     }
268     PyErr_Clear();
269
270     PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
271     return 0;
272 }
273 //----------------------------------------------------------------------------
274 static PyObject * taglist_item(PyObject * self, int index)
275 {
276     TagListObject*taglist = (TagListObject*)self;
277     PyObject*tag;
278     mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
279     tag = PyList_GetItem(taglist->taglist, index);
280     Py_INCREF(tag);
281     return tag;
282 }
283 static PySequenceMethods taglist_as_sequence =
284 {
285     sq_length: taglist_length, // len(obj)
286     sq_concat: taglist_concat, // obj += [...], obj1+obj2
287     sq_repeat: 0,            // x*n, intargfunc
288     sq_item: taglist_item,  // obj[3]
289     sq_slice: 0,             // x[i:j] intintargfunc
290     sq_ass_item: 0,          // x[i] = y intobjargproc
291     sq_ass_slice: 0,         // x[i:j] = v intintobjargproc
292     sq_contains: taglist_contains,   //???
293 };
294 PyTypeObject TagListClass = 
295 {
296     PyObject_HEAD_INIT(NULL)
297     0,
298     tp_name: "TagList",
299     tp_basicsize: sizeof(TagListObject),
300     tp_itemsize: 0,
301     tp_dealloc: taglist_dealloc,
302     tp_print: 0,                 // print x
303     tp_getattr: taglist_getattr, // x.attr
304     tp_setattr: 0,               // x.attr = v
305     tp_compare: 0,               // x>y
306     tp_repr: 0,                  // `x`, print x
307     tp_as_number: 0,
308     tp_as_sequence: &taglist_as_sequence,
309 };