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