6 #include "primitives.h"
12 //----------------------------------------------------------------------------
18 //----------------------------------------------------------------------------
19 PyObject * taglist_new()
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;
27 //----------------------------------------------------------------------------
28 PyObject * taglist_new2(TAG*tag)
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();
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, taglist->tagmap);
53 // pass through exception
56 PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
57 if(swf_isDefiningTag(t)) {
58 int id = swf_GetDefineID(t);
59 tagmap_addMapping(taglist->tagmap, id, newtag);
64 return (PyObject*)taglist;
66 //----------------------------------------------------------------------------
67 TAG* taglist_getTAGs(PyObject*self)
69 if(!PY_CHECK_TYPE(self,&TagListClass)) {
70 PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
73 TagListObject*taglist = (TagListObject*)self;
75 /* TODO: the tags will be modified by this. We should set mutexes. */
77 int l = PyList_Size(taglist->taglist);
81 mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
83 PyObject*item = PyList_GetItem(taglist->taglist, t);
84 tag = tag_getTAG(item, tag, taglist->tagmap);
87 mylog(" %08x(%d) taglist_getTAGs: added tag %08x", (int)self, self->ob_refcnt, tag);
91 //----------------------------------------------------------------------------
92 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
95 TagListObject*taglist = (TagListObject*)self;
96 if(!self || !PyArg_ParseTuple(args,""))
98 swf.firstTag = taglist->firstTag;
100 taglist->firstTag = swf.firstTag;
101 taglist->lastTag = 0; // FIXME
102 taglist->searchTag = 0;*/
105 //----------------------------------------------------------------------------
106 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
109 /* TagListObject*taglist = (TagListObject*)self;
110 if(!self || !PyArg_ParseTuple(args,""))
112 swf.firstTag = taglist->firstTag;
114 taglist->firstTag = swf.firstTag;
115 taglist->lastTag = 0; // FIXME
116 taglist->searchTag = 0;*/
119 //----------------------------------------------------------------------------
120 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
123 /* TagListObject*taglist = (TagListObject*)self;
124 if(!self || !PyArg_ParseTuple(args,""))
126 swf.firstTag = taglist->firstTag;
128 taglist->firstTag = swf.firstTag;
129 taglist->lastTag = 0; // FIXME
130 taglist->searchTag = 0;*/
133 //----------------------------------------------------------------------------
134 static void taglist_dealloc(PyObject* self)
136 TagListObject*taglist = (TagListObject*)self;
137 mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt);
138 Py_DECREF(taglist->taglist);
139 taglist->taglist = 0;
140 Py_DECREF(taglist->tagmap);
144 //----------------------------------------------------------------------------
145 static PyMethodDef taglist_functions[] =
146 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
147 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
148 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
149 {NULL, NULL, 0, NULL}
152 static PyObject* taglist_getattr(PyObject * self, char* a)
154 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
155 mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
158 //----------------------------------------------------------------------------
159 static int taglist_length(PyObject * self)
161 TagListObject*tags = (TagListObject*)self;
162 mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
163 return PyList_GET_SIZE(tags->taglist);
165 //----------------------------------------------------------------------------
166 static int taglist_contains(PyObject * self, PyObject * tag)
168 /* TODO: optimize! */
169 TagListObject*taglist = (TagListObject*)self;
170 PyObject*list = taglist->taglist;
171 int l = PyList_Size(list);
174 PyObject*item = PyList_GetItem(list, t);
181 //----------------------------------------------------------------------------
182 static PyObject * taglist_concat(PyObject * self, PyObject* list)
185 PY_ASSERT_TYPE(self, &TagListClass);
186 TagListObject*taglist = (TagListObject*)self;
187 mylog(" %08x(%d) taglist_concat %08x", (int)self, self->ob_refcnt, list);
189 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
190 mylog(" %08x(%d) taglist_concat: Tag %08x", (int)self, self->ob_refcnt, tag);
191 list = tag_getDependencies(tag);
192 int l = PyList_Size(list);
194 mylog(" %08x(%d) taglist_concat: Tag: %d dependencies", (int)self, self->ob_refcnt, l);
196 PyObject*item = PyList_GetItem(list, t);
197 PyObject*_self = taglist_concat(self, item);
201 if(!taglist_contains(self, tag)) {
202 mylog(" %08x(%d) taglist_concat: Adding Tag %08x", (int)self, self->ob_refcnt, tag);
203 PyList_Append(taglist->taglist, tag);
205 mylog(" %08x(%d) taglist_concat: done", (int)self, self->ob_refcnt);
208 /* copy tag, so we don't have to do INCREF(tag) (and don't
209 get problems if the tag is appended to more than one
211 /* TODO: handle IDs */
213 TAG*t = tag_getTAG(tag);
215 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
217 nt = swf_InsertTag(0, t->id);
218 swf_SetBlock(nt,t->data,t->len);
219 PyObject*newtag = tag_new(taglist->swf, nt);
220 if(swf_isDefiningTag(t)) {
221 int id = swf_GetDefineID(t);
222 PyObject*id = PyLong_FromLong(id);
223 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
225 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
232 if (PyList_Check(list)) {
233 int l = PyList_Size(list);
235 mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt);
237 PyObject*item = PyList_GetItem(list, t);
238 if(!PY_CHECK_TYPE(item, &TagClass)) {
239 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
242 PyObject*_self = taglist_concat(self, item);
252 if (PY_CHECK_TYPE(list, &TagListClass)) {
253 mylog(" %08x(%d) taglist_concat: TagList", (int)self, self->ob_refcnt);
254 TagListObject*taglist2 = (TagListObject*)list;
255 return taglist_concat(self, taglist2->taglist);
257 /*TAG* tags = taglist_getTAGs(self);
258 TAG* tags2 = taglist_getTAGs(list);
260 tags3 = swf_Concatenate(tags,tags2);
261 PyObject* newtaglist = taglist_new(tags3);
263 Py_INCREF(newtaglist);*/
267 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
270 //----------------------------------------------------------------------------
271 static PyObject * taglist_item(PyObject * self, int index)
273 TagListObject*taglist = (TagListObject*)self;
275 mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
276 tag = PyList_GetItem(taglist->taglist, index);
280 static PySequenceMethods taglist_as_sequence =
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, //???
291 static PyTypeObject TagListClass =
293 PyObject_HEAD_INIT(NULL)
296 tp_basicsize: sizeof(TagListObject),
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
305 tp_as_sequence: &taglist_as_sequence,