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