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