6 #include "primitives.h"
12 //----------------------------------------------------------------------------
17 //----------------------------------------------------------------------------
18 PyObject * taglist_new()
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;
25 //----------------------------------------------------------------------------
26 PyObject * taglist_new2(TAG*tag)
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();
37 while(t) {nr++;last=t;t=t->next;}
39 if(last && last->id==ST_END) {
44 taglist->taglist = PyList_New(nr);
46 mylog("+%08x(%d) taglist_new2: %d items", (int)taglist, taglist->ob_refcnt, nr);
51 PyObject*newtag = tag_new2(t, tagmap);
53 // pass through exception
57 PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
58 if(swf_isDefiningTag(t)) {
59 int id = swf_GetDefineID(t);
60 tagmap_addMapping(tagmap, id, newtag);
66 return (PyObject*)taglist;
68 //----------------------------------------------------------------------------
69 TAG* taglist_getTAGs(PyObject*self)
71 PyObject* tagmap = tagmap_new();
72 if(!PY_CHECK_TYPE(self,&TagListClass)) {
73 PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
76 TagListObject*taglist = (TagListObject*)self;
78 /* TODO: the tags will be modified by this. We should set mutexes. */
80 int l = PyList_Size(taglist->taglist);
84 mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
86 PyObject*item = PyList_GetItem(taglist->taglist, t);
87 tag = tag_getTAG(item, tag, tagmap);
95 mylog(" %08x(%d) taglist_getTAGs: added tag %08x", (int)self, self->ob_refcnt, tag);
100 //----------------------------------------------------------------------------
101 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
104 TagListObject*taglist = (TagListObject*)self;
105 if(!self || !PyArg_ParseTuple(args,""))
107 swf.firstTag = taglist->firstTag;
109 taglist->firstTag = swf.firstTag;
110 taglist->lastTag = 0; // FIXME
111 taglist->searchTag = 0;*/
114 //----------------------------------------------------------------------------
115 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
118 /* TagListObject*taglist = (TagListObject*)self;
119 if(!self || !PyArg_ParseTuple(args,""))
121 swf.firstTag = taglist->firstTag;
123 taglist->firstTag = swf.firstTag;
124 taglist->lastTag = 0; // FIXME
125 taglist->searchTag = 0;*/
128 //----------------------------------------------------------------------------
129 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
132 /* TagListObject*taglist = (TagListObject*)self;
133 if(!self || !PyArg_ParseTuple(args,""))
135 swf.firstTag = taglist->firstTag;
137 taglist->firstTag = swf.firstTag;
138 taglist->lastTag = 0; // FIXME
139 taglist->searchTag = 0;*/
142 //----------------------------------------------------------------------------
143 static void taglist_dealloc(PyObject* self)
145 TagListObject*taglist = (TagListObject*)self;
146 mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt);
147 Py_DECREF(taglist->taglist);
148 taglist->taglist = 0;
151 //----------------------------------------------------------------------------
152 static PyMethodDef taglist_functions[] =
153 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
154 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
155 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
156 {NULL, NULL, 0, NULL}
159 static PyObject* taglist_getattr(PyObject * self, char* a)
161 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
162 mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
165 //----------------------------------------------------------------------------
166 static int taglist_length(PyObject * self)
168 TagListObject*tags = (TagListObject*)self;
169 mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
170 return PyList_GET_SIZE(tags->taglist);
172 //----------------------------------------------------------------------------
173 static int taglist_contains(PyObject * self, PyObject * tag)
175 /* TODO: optimize! */
176 TagListObject*taglist = (TagListObject*)self;
177 PyObject*list = taglist->taglist;
178 int l = PyList_Size(list);
181 PyObject*item = PyList_GetItem(list, t);
188 //----------------------------------------------------------------------------
189 static PyObject * taglist_concat(PyObject * self, PyObject* list)
192 PY_ASSERT_TYPE(self, &TagListClass);
193 TagListObject*taglist = (TagListObject*)self;
194 mylog(" %08x(%d) taglist_concat %08x", (int)self, self->ob_refcnt, list);
196 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
197 mylog(" %08x(%d) taglist_concat: Tag %08x", (int)self, self->ob_refcnt, tag);
198 list = tag_getDependencies(tag);
199 int l = PyList_Size(list);
201 mylog(" %08x(%d) taglist_concat: Tag: %d dependencies", (int)self, self->ob_refcnt, l);
203 PyObject*item = PyList_GetItem(list, t);
204 PyObject*_self = taglist_concat(self, item);
208 if(!taglist_contains(self, tag)) {
209 mylog(" %08x(%d) taglist_concat: Adding Tag %08x", (int)self, self->ob_refcnt, tag);
210 PyList_Append(taglist->taglist, tag);
212 mylog(" %08x(%d) taglist_concat: done", (int)self, self->ob_refcnt);
215 /* copy tag, so we don't have to do INCREF(tag) (and don't
216 get problems if the tag is appended to more than one
218 /* TODO: handle IDs */
220 TAG*t = tag_getTAG(tag);
222 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
224 nt = swf_InsertTag(0, t->id);
225 swf_SetBlock(nt,t->data,t->len);
226 PyObject*newtag = tag_new(taglist->swf, nt);
227 if(swf_isDefiningTag(t)) {
228 int id = swf_GetDefineID(t);
229 PyObject*id = PyLong_FromLong(id);
230 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
232 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
239 if (PyList_Check(list)) {
240 int l = PyList_Size(list);
242 mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt);
244 PyObject*item = PyList_GetItem(list, t);
245 if(!PY_CHECK_TYPE(item, &TagClass)) {
246 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
249 PyObject*_self = taglist_concat(self, item);
259 if (PY_CHECK_TYPE(list, &TagListClass)) {
260 mylog(" %08x(%d) taglist_concat: TagList", (int)self, self->ob_refcnt);
261 TagListObject*taglist2 = (TagListObject*)list;
262 return taglist_concat(self, taglist2->taglist);
264 /*TAG* tags = taglist_getTAGs(self);
265 TAG* tags2 = taglist_getTAGs(list);
267 tags3 = swf_Concatenate(tags,tags2);
268 PyObject* newtaglist = taglist_new(tags3);
270 Py_INCREF(newtaglist);*/
274 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
277 //----------------------------------------------------------------------------
278 static PyObject * taglist_item(PyObject * self, int index)
280 TagListObject*taglist = (TagListObject*)self;
282 mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
283 tag = PyList_GetItem(taglist->taglist, index);
287 static PySequenceMethods taglist_as_sequence =
289 sq_length: taglist_length, // len(obj)
290 sq_concat: taglist_concat, // obj += [...], obj1+obj2
291 sq_repeat: 0, // x*n, intargfunc
292 sq_item: taglist_item, // obj[3]
293 sq_slice: 0, // x[i:j] intintargfunc
294 sq_ass_item: 0, // x[i] = y intobjargproc
295 sq_ass_slice: 0, // x[i:j] = v intintobjargproc
296 sq_contains: taglist_contains, //???
298 static PyTypeObject TagListClass =
300 PyObject_HEAD_INIT(NULL)
303 tp_basicsize: sizeof(TagListObject),
305 tp_dealloc: taglist_dealloc,
306 tp_print: 0, // print x
307 tp_getattr: taglist_getattr, // x.attr
308 tp_setattr: 0, // x.attr = v
309 tp_compare: 0, // x>y
310 tp_repr: 0, // `x`, print x
312 tp_as_sequence: &taglist_as_sequence,