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