FoldAll is now done in SWF.c
[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     int nr=0, len=0;
33     TAG*t = tag;
34     TAG*last = t;
35     while(t) {len++;last=t;t=t->next;}
36
37     if(last && last->id==ST_END) {
38         swf_DeleteTag(last); last = 0;
39         len--;
40         if(len==0) tag = 0;
41     }
42
43     taglist->taglist = PyList_New(len);
44     
45     nr = 0;
46     t = tag;
47     while(t) {
48         PyObject*newtag = tag_new2(t, tagmap);
49         if(newtag==NULL) {
50             // pass through exception
51             Py_DECREF(tagmap);
52             return NULL;
53         }
54         PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
55         if(swf_isDefiningTag(t)) {
56             int id = swf_GetDefineID(t);
57             tagmap_addMapping(tagmap, id, newtag);
58         }
59         nr++;
60         t=t->next;
61     }
62     Py_DECREF(tagmap);
63     return (PyObject*)taglist;
64 }
65 //----------------------------------------------------------------------------
66 TAG* taglist_getTAGs(PyObject*self)
67 {
68     PyObject* tagmap = tagmap_new();
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, tagmap);
85         if(!tag) {
86             //pass through errors
87             Py_DECREF(tagmap);
88             return 0;
89         }
90         if(!firstTag)
91             firstTag = tag;
92     }
93     Py_DECREF(tagmap);
94     return firstTag;
95 }
96 //----------------------------------------------------------------------------
97 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
98 {
99 /*    SWF swf;
100     TagListObject*taglist = (TagListObject*)self;
101     if(!self || !PyArg_ParseTuple(args,"")) 
102         return NULL;
103     swf.firstTag = taglist->firstTag;
104     swf_FoldAll(&swf);
105     taglist->firstTag = swf.firstTag;
106     taglist->lastTag = 0; // FIXME
107     taglist->searchTag = 0;*/
108     return PY_NONE;
109 }
110 //----------------------------------------------------------------------------
111 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
112 {
113     SWF swf;
114 /*    TagListObject*taglist = (TagListObject*)self;
115     if(!self || !PyArg_ParseTuple(args,"")) 
116         return NULL;
117     swf.firstTag = taglist->firstTag;
118     swf_UnFoldAll(&swf);
119     taglist->firstTag = swf.firstTag;
120     taglist->lastTag = 0; // FIXME
121     taglist->searchTag = 0;*/
122     return PY_NONE;
123 }
124 //----------------------------------------------------------------------------
125 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
126 {
127     SWF swf;
128 /*    TagListObject*taglist = (TagListObject*)self;
129     if(!self || !PyArg_ParseTuple(args,"")) 
130         return NULL;
131     swf.firstTag = taglist->firstTag;
132     swf_UnFoldAll(&swf);
133     taglist->firstTag = swf.firstTag;
134     taglist->lastTag = 0; // FIXME
135     taglist->searchTag = 0;*/
136     return PY_NONE;
137 }
138 //----------------------------------------------------------------------------
139 static void taglist_dealloc(PyObject* self)
140 {
141     TagListObject*taglist = (TagListObject*)self;
142     mylog("-%08x(%d) taglist_dealloc list=%08x(%d)\n", (int)self, self->ob_refcnt, taglist->taglist, taglist->taglist->ob_refcnt);
143     Py_DECREF(taglist->taglist);
144     taglist->taglist = 0;
145     PyObject_Del(self);
146 }
147 //----------------------------------------------------------------------------
148 static PyMethodDef taglist_functions[] =
149 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
150  {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
151  {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
152  {NULL, NULL, 0, NULL}
153 };
154
155 static PyObject* taglist_getattr(PyObject * self, char* a)
156 {
157     PyObject* ret = Py_FindMethod(taglist_functions, self, a);
158     mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
159     return ret;
160 }
161 //----------------------------------------------------------------------------
162 static int taglist_length(PyObject * self)
163 {
164     TagListObject*tags = (TagListObject*)self;
165     mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
166     return PyList_GET_SIZE(tags->taglist);
167 }
168 //----------------------------------------------------------------------------
169 static int taglist_contains(PyObject * self, PyObject * tag)
170 {
171     /* TODO: optimize! */
172     TagListObject*taglist = (TagListObject*)self;
173     PyObject*list = taglist->taglist;
174     int l = PyList_Size(list);
175     int t;
176     for(t=0;t<l;t++) {
177         PyObject*item = PyList_GetItem(list, t);
178         if(item == tag) {
179             return 1;
180         }
181     }
182     return 0;
183 }
184 //----------------------------------------------------------------------------
185 static PyObject * taglist_concat(PyObject * self, PyObject* list)
186 {
187     PyObject*tag = 0;
188     PY_ASSERT_TYPE(self, &TagListClass);
189     TagListObject*taglist = (TagListObject*)self;
190     mylog(" %08x(%d) taglist_concat %08x(%d)", (int)self, self->ob_refcnt, list, list->ob_refcnt);
191
192     if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
193         list = tag_getDependencies(tag);
194         int l = PyList_Size(list);
195         int t;
196         for(t=0;t<l;t++) {
197             PyObject*item = PyList_GetItem(list, t);
198             PyObject*_self = taglist_concat(self, item);
199             Py_DECREF(self);
200             self = _self;
201         }
202         if(!taglist_contains(self, tag)) {
203             mylog(" %08x(%d) taglist_concat: Adding Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
204             PyList_Append(taglist->taglist, tag);
205         } else {
206             mylog(" %08x(%d) taglist_concat: Already contains Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
207         }
208         Py_INCREF(self);
209         return self;
210         /* copy tag, so we don't have to do INCREF(tag) (and don't
211            get problems if the tag is appended to more than one
212            taglist) */
213         /* TODO: handle IDs */
214         /*      
215         TAG*t = tag_getTAG(tag);
216         TAG*nt = 0;
217         mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
218         // copy tag
219         nt = swf_InsertTag(0, t->id);
220         swf_SetBlock(nt,t->data,t->len);
221         PyObject*newtag = tag_new(taglist->swf, nt);
222         if(swf_isDefiningTag(t)) {
223             int id = swf_GetDefineID(t);
224             PyObject*id = PyLong_FromLong(id);
225             PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
226             Py_INCREF(id);
227             PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
228             Py_INCREF(id);
229         }
230         Py_INCREF(self);
231         return self;*/
232     }
233     PyErr_Clear();
234     if (PyList_Check(list)) {
235         int l = PyList_Size(list);
236         int t;
237         for(t=0;t<l;t++) {
238             PyObject*item = PyList_GetItem(list, t);
239             if(!PY_CHECK_TYPE(item, &TagClass)) {
240                 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
241                 return 0;
242             }
243             PyObject*_self = taglist_concat(self, item);
244             Py_DECREF(self);
245             self = _self;
246             if(!self)
247                 return 0;
248         }
249         Py_INCREF(self);
250         return self;
251     }
252     PyErr_Clear();
253     if (PY_CHECK_TYPE(list, &TagListClass)) {
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 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 };