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