6 #include "primitives.h"
12 //----------------------------------------------------------------------------
17 //----------------------------------------------------------------------------
18 static void taglist_showcontents(PyObject* self)
20 TagListObject*taglist = (TagListObject*)self;
21 int t, l = PyList_Size(taglist->taglist);
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);
27 //----------------------------------------------------------------------------
28 PyObject * taglist_new()
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;
35 //----------------------------------------------------------------------------
36 PyObject * taglist_new2(TAG*tag)
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();
45 while(t) {len++;last=t;t=t->next;}
47 if(last && last->id==ST_END) {
48 swf_DeleteTag(last); last = 0;
53 taglist->taglist = PyList_New(len);
58 PyObject*newtag = tag_new2(t, tagmap);
60 // pass through exception
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);
74 return (PyObject*)taglist;
76 //----------------------------------------------------------------------------
77 TAG* taglist_getTAGs(PyObject*self)
79 PyObject* tagmap = tagmap_new();
80 if(!PY_CHECK_TYPE(self,&TagListClass)) {
81 PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
84 TagListObject*taglist = (TagListObject*)self;
86 /* TODO: the tags will be modified by this. We should set mutexes. */
88 int l = PyList_Size(taglist->taglist);
92 mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
94 PyObject*item = PyList_GetItem(taglist->taglist, t);
95 tag = tag_getTAG(item, tag, tagmap);
107 //----------------------------------------------------------------------------
108 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
111 TagListObject*taglist = (TagListObject*)self;
112 if(!self || !PyArg_ParseTuple(args,""))
114 swf.firstTag = taglist->firstTag;
116 taglist->firstTag = swf.firstTag;
117 taglist->lastTag = 0; // FIXME
118 taglist->searchTag = 0;*/
121 //----------------------------------------------------------------------------
122 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
125 /* TagListObject*taglist = (TagListObject*)self;
126 if(!self || !PyArg_ParseTuple(args,""))
128 swf.firstTag = taglist->firstTag;
130 taglist->firstTag = swf.firstTag;
131 taglist->lastTag = 0; // FIXME
132 taglist->searchTag = 0;*/
135 //----------------------------------------------------------------------------
136 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
139 /* TagListObject*taglist = (TagListObject*)self;
140 if(!self || !PyArg_ParseTuple(args,""))
142 swf.firstTag = taglist->firstTag;
144 taglist->firstTag = swf.firstTag;
145 taglist->lastTag = 0; // FIXME
146 taglist->searchTag = 0;*/
149 //----------------------------------------------------------------------------
150 static PyMethodDef taglist_functions[] =
151 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
152 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
153 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
154 {NULL, NULL, 0, NULL}
157 static PyObject* taglist_getattr(PyObject * self, char* a)
159 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
160 mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
163 //----------------------------------------------------------------------------
164 static int taglist_length(PyObject * self)
166 TagListObject*tags = (TagListObject*)self;
167 mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
168 return PyList_GET_SIZE(tags->taglist);
170 //----------------------------------------------------------------------------
171 static int taglist_contains(PyObject * self, PyObject * tag)
173 /* TODO: optimize! */
174 TagListObject*taglist = (TagListObject*)self;
175 PyObject*list = taglist->taglist;
176 int l = PyList_Size(list);
179 PyObject*item = PyList_GetItem(list, t);
186 //----------------------------------------------------------------------------
187 static PyObject * taglist_concat(PyObject * self, PyObject* list)
190 PY_ASSERT_TYPE(self, &TagListClass);
191 TagListObject*taglist = (TagListObject*)self;
192 mylog(" %08x(%d) taglist_concat %08x(%d)", (int)self, self->ob_refcnt, list, list->ob_refcnt);
194 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
195 list = tag_getDependencies(tag);
196 int l = PyList_Size(list);
199 PyObject*item = PyList_GetItem(list, t);
200 PyObject*_self = taglist_concat(self, item);
204 if(!taglist_contains(self, tag)) {
205 mylog(" %08x(%d) taglist_concat: Adding Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
206 PyList_Append(taglist->taglist, tag);
208 mylog(" %08x(%d) taglist_concat: Already contains Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
212 /* copy tag, so we don't have to do INCREF(tag) (and don't
213 get problems if the tag is appended to more than one
215 /* TODO: handle IDs */
217 TAG*t = tag_getTAG(tag);
219 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
221 nt = swf_InsertTag(0, t->id);
222 swf_SetBlock(nt,t->data,t->len);
223 PyObject*newtag = tag_new(taglist->swf, nt);
224 if(swf_isDefiningTag(t)) {
225 int id = swf_GetDefineID(t);
226 PyObject*id = PyLong_FromLong(id);
227 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
229 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
236 if (PyList_Check(list)) {
237 int l = PyList_Size(list);
240 PyObject*item = PyList_GetItem(list, t);
241 if(!PY_CHECK_TYPE(item, &TagClass)) {
242 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
245 PyObject*_self = taglist_concat(self, item);
255 if (PY_CHECK_TYPE(list, &TagListClass)) {
256 TagListObject*taglist2 = (TagListObject*)list;
257 return taglist_concat(self, taglist2->taglist);
259 /*TAG* tags = taglist_getTAGs(self);
260 TAG* tags2 = taglist_getTAGs(list);
262 tags3 = swf_Concatenate(tags,tags2);
263 PyObject* newtaglist = taglist_new(tags3);
265 Py_INCREF(newtaglist);*/
269 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
272 //----------------------------------------------------------------------------
273 static PyObject * taglist_item(PyObject * self, int index)
275 TagListObject*taglist = (TagListObject*)self;
277 mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
278 tag = PyList_GetItem(taglist->taglist, index);
282 //----------------------------------------------------------------------------
283 static void taglist_dealloc(PyObject* self)
285 TagListObject*taglist = (TagListObject*)self;
286 mylog("-%08x(%d) taglist_dealloc list=%08x(%d)\n", (int)self, self->ob_refcnt, taglist->taglist, taglist->taglist->ob_refcnt);
287 Py_DECREF(taglist->taglist);
288 taglist->taglist = 0;
291 //----------------------------------------------------------------------------
292 static PySequenceMethods taglist_as_sequence =
294 sq_length: taglist_length, // len(obj)
295 sq_concat: taglist_concat, // obj += [...], obj1+obj2
296 sq_repeat: 0, // x*n, intargfunc
297 sq_item: taglist_item, // obj[3]
298 sq_slice: 0, // x[i:j] intintargfunc
299 sq_ass_item: 0, // x[i] = y intobjargproc
300 sq_ass_slice: 0, // x[i:j] = v intintobjargproc
301 sq_contains: taglist_contains, //???
303 PyTypeObject TagListClass =
305 PyObject_HEAD_INIT(NULL)
308 tp_basicsize: sizeof(TagListObject),
310 tp_dealloc: taglist_dealloc,
311 tp_print: 0, // print x
312 tp_getattr: taglist_getattr, // x.attr
313 tp_setattr: 0, // x.attr = v
314 tp_compare: 0, // x>y
315 tp_repr: 0, // `x`, print x
317 tp_as_sequence: &taglist_as_sequence,