reworked tag memory allocation
[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         mylog(" %08x(%d) taglist_getTAGs: added tag %08x", (int)self, self->ob_refcnt, tag);
96     }
97     Py_DECREF(tagmap);
98     return firstTag;
99 }
100 //----------------------------------------------------------------------------
101 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
102 {
103 /*    SWF swf;
104     TagListObject*taglist = (TagListObject*)self;
105     if(!self || !PyArg_ParseTuple(args,"")) 
106         return NULL;
107     swf.firstTag = taglist->firstTag;
108     swf_FoldAll(&swf);
109     taglist->firstTag = swf.firstTag;
110     taglist->lastTag = 0; // FIXME
111     taglist->searchTag = 0;*/
112     return PY_NONE;
113 }
114 //----------------------------------------------------------------------------
115 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
116 {
117     SWF swf;
118 /*    TagListObject*taglist = (TagListObject*)self;
119     if(!self || !PyArg_ParseTuple(args,"")) 
120         return NULL;
121     swf.firstTag = taglist->firstTag;
122     swf_UnFoldAll(&swf);
123     taglist->firstTag = swf.firstTag;
124     taglist->lastTag = 0; // FIXME
125     taglist->searchTag = 0;*/
126     return PY_NONE;
127 }
128 //----------------------------------------------------------------------------
129 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
130 {
131     SWF swf;
132 /*    TagListObject*taglist = (TagListObject*)self;
133     if(!self || !PyArg_ParseTuple(args,"")) 
134         return NULL;
135     swf.firstTag = taglist->firstTag;
136     swf_UnFoldAll(&swf);
137     taglist->firstTag = swf.firstTag;
138     taglist->lastTag = 0; // FIXME
139     taglist->searchTag = 0;*/
140     return PY_NONE;
141 }
142 //----------------------------------------------------------------------------
143 static void taglist_dealloc(PyObject* self)
144 {
145     TagListObject*taglist = (TagListObject*)self;
146     mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt);
147     Py_DECREF(taglist->taglist);
148     taglist->taglist = 0;
149     PyObject_Del(self);
150 }
151 //----------------------------------------------------------------------------
152 static PyMethodDef taglist_functions[] =
153 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
154  {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
155  {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
156  {NULL, NULL, 0, NULL}
157 };
158
159 static PyObject* taglist_getattr(PyObject * self, char* a)
160 {
161     PyObject* ret = Py_FindMethod(taglist_functions, self, a);
162     mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
163     return ret;
164 }
165 //----------------------------------------------------------------------------
166 static int taglist_length(PyObject * self)
167 {
168     TagListObject*tags = (TagListObject*)self;
169     mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
170     return PyList_GET_SIZE(tags->taglist);
171 }
172 //----------------------------------------------------------------------------
173 static int taglist_contains(PyObject * self, PyObject * tag)
174 {
175     /* TODO: optimize! */
176     TagListObject*taglist = (TagListObject*)self;
177     PyObject*list = taglist->taglist;
178     int l = PyList_Size(list);
179     int t;
180     for(t=0;t<l;t++) {
181         PyObject*item = PyList_GetItem(list, t);
182         if(item == tag) {
183             return 1;
184         }
185     }
186     return 0;
187 }
188 //----------------------------------------------------------------------------
189 static PyObject * taglist_concat(PyObject * self, PyObject* list)
190 {
191     PyObject*tag = 0;
192     PY_ASSERT_TYPE(self, &TagListClass);
193     TagListObject*taglist = (TagListObject*)self;
194     mylog(" %08x(%d) taglist_concat %08x", (int)self, self->ob_refcnt, list);
195
196     if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
197         mylog(" %08x(%d) taglist_concat: Tag %08x", (int)self, self->ob_refcnt, tag);
198         list = tag_getDependencies(tag);
199         int l = PyList_Size(list);
200         int t;
201         mylog(" %08x(%d) taglist_concat: Tag: %d dependencies", (int)self, self->ob_refcnt, l);
202         for(t=0;t<l;t++) {
203             PyObject*item = PyList_GetItem(list, t);
204             PyObject*_self = taglist_concat(self, item);
205             Py_DECREF(self);
206             self = _self;
207         }
208         if(!taglist_contains(self, tag)) {
209             mylog(" %08x(%d) taglist_concat: Adding Tag %08x", (int)self, self->ob_refcnt, tag);
210             PyList_Append(taglist->taglist, tag);
211         }
212         mylog(" %08x(%d) taglist_concat: done", (int)self, self->ob_refcnt);
213         Py_INCREF(self);
214         return self;
215         /* copy tag, so we don't have to do INCREF(tag) (and don't
216            get problems if the tag is appended to more than one
217            taglist) */
218         /* TODO: handle IDs */
219         /*      
220         TAG*t = tag_getTAG(tag);
221         TAG*nt = 0;
222         mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
223         // copy tag
224         nt = swf_InsertTag(0, t->id);
225         swf_SetBlock(nt,t->data,t->len);
226         PyObject*newtag = tag_new(taglist->swf, nt);
227         if(swf_isDefiningTag(t)) {
228             int id = swf_GetDefineID(t);
229             PyObject*id = PyLong_FromLong(id);
230             PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
231             Py_INCREF(id);
232             PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
233             Py_INCREF(id);
234         }
235         Py_INCREF(self);
236         return self;*/
237     }
238     PyErr_Clear();
239     if (PyList_Check(list)) {
240         int l = PyList_Size(list);
241         int t;
242         mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt);
243         for(t=0;t<l;t++) {
244             PyObject*item = PyList_GetItem(list, t);
245             if(!PY_CHECK_TYPE(item, &TagClass)) {
246                 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
247                 return 0;
248             }
249             PyObject*_self = taglist_concat(self, item);
250             Py_DECREF(self);
251             self = _self;
252             if(!self)
253                 return 0;
254         }
255         Py_INCREF(self);
256         return self;
257     }
258     PyErr_Clear();
259     if (PY_CHECK_TYPE(list, &TagListClass)) {
260         mylog(" %08x(%d) taglist_concat: TagList", (int)self, self->ob_refcnt);
261         TagListObject*taglist2 = (TagListObject*)list;
262         return taglist_concat(self, taglist2->taglist);
263
264         /*TAG* tags = taglist_getTAGs(self);
265         TAG* tags2 = taglist_getTAGs(list);
266         TAG* tags3;
267         tags3 = swf_Concatenate(tags,tags2);
268         PyObject* newtaglist = taglist_new(tags3);
269         swf_FreeTags(tags3);
270         Py_INCREF(newtaglist);*/
271     }
272     PyErr_Clear();
273
274     PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
275     return 0;
276 }
277 //----------------------------------------------------------------------------
278 static PyObject * taglist_item(PyObject * self, int index)
279 {
280     TagListObject*taglist = (TagListObject*)self;
281     PyObject*tag;
282     mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
283     tag = PyList_GetItem(taglist->taglist, index);
284     Py_INCREF(tag);
285     return tag;
286 }
287 static PySequenceMethods taglist_as_sequence =
288 {
289     sq_length: taglist_length, // len(obj)
290     sq_concat: taglist_concat, // obj += [...], obj1+obj2
291     sq_repeat: 0,            // x*n, intargfunc
292     sq_item: taglist_item,  // obj[3]
293     sq_slice: 0,             // x[i:j] intintargfunc
294     sq_ass_item: 0,          // x[i] = y intobjargproc
295     sq_ass_slice: 0,         // x[i:j] = v intintobjargproc
296     sq_contains: taglist_contains,   //???
297 };
298 static PyTypeObject TagListClass = 
299 {
300     PyObject_HEAD_INIT(NULL)
301     0,
302     tp_name: "TagList",
303     tp_basicsize: sizeof(TagListObject),
304     tp_itemsize: 0,
305     tp_dealloc: taglist_dealloc,
306     tp_print: 0,                 // print x
307     tp_getattr: taglist_getattr, // x.attr
308     tp_setattr: 0,               // x.attr = v
309     tp_compare: 0,               // x>y
310     tp_repr: 0,                  // `x`, print x
311     tp_as_number: 0,
312     tp_as_sequence: &taglist_as_sequence,
313 };