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(0, 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 TAG* tag = taglist_getTAGs2(self, tagmap, 1);
84 //----------------------------------------------------------------------------
85 TAG* taglist_getTAGs2(PyObject*self, PyObject*tagmap, int addDependencies)
87 if(!PY_CHECK_TYPE(self,&TagListClass)) {
88 PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
91 TagListObject*taglist = (TagListObject*)self;
93 /* TODO: the tags will be modified by this. We should set mutexes. */
95 int l = PyList_Size(taglist->taglist);
99 mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
101 PyObject*item = PyList_GetItem(taglist->taglist, t);
102 if(addDependencies) {
103 PyObject* deps = tag_getDependencies(item);
104 int l = PyList_Size(deps);
107 PyObject*item = PyList_GetItem(deps, t);
108 if(tagmap_obj2id(tagmap, item)<0) {
109 /*PyObject*_self = taglist_concat(self, item);
112 tag = tag_getTAG(item, tag, tagmap);
113 if(!tag) { return 0; }
120 tag = tag_getTAG(item, tag, tagmap);
121 if(!tag) { /* pass through errors */ return 0; }
128 //----------------------------------------------------------------------------
129 static PyObject * taglist_foldAll(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 PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
146 /* TagListObject*taglist = (TagListObject*)self;
147 if(!self || !PyArg_ParseTuple(args,""))
149 swf.firstTag = taglist->firstTag;
151 taglist->firstTag = swf.firstTag;
152 taglist->lastTag = 0; // FIXME
153 taglist->searchTag = 0;*/
156 //----------------------------------------------------------------------------
157 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
160 /* TagListObject*taglist = (TagListObject*)self;
161 if(!self || !PyArg_ParseTuple(args,""))
163 swf.firstTag = taglist->firstTag;
165 taglist->firstTag = swf.firstTag;
166 taglist->lastTag = 0; // FIXME
167 taglist->searchTag = 0;*/
170 //----------------------------------------------------------------------------
171 static PyMethodDef taglist_functions[] =
172 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
173 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
174 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
175 {NULL, NULL, 0, NULL}
178 static PyObject* taglist_getattr(PyObject * self, char* a)
180 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
181 mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
184 //----------------------------------------------------------------------------
185 static int taglist_length(PyObject * self)
187 TagListObject*tags = (TagListObject*)self;
188 mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
189 return PyList_GET_SIZE(tags->taglist);
191 //----------------------------------------------------------------------------
192 static int taglist_contains(PyObject * self, PyObject * tag)
194 /* TODO: optimize! */
195 TagListObject*taglist = (TagListObject*)self;
196 PyObject*list = taglist->taglist;
197 int l = PyList_Size(list);
200 PyObject*item = PyList_GetItem(list, t);
207 //----------------------------------------------------------------------------
208 static PyObject * taglist_concat(PyObject * self, PyObject* list)
211 PY_ASSERT_TYPE(self, &TagListClass);
212 TagListObject*taglist = (TagListObject*)self;
213 mylog(" %08x(%d) taglist_concat %08x(%d)", (int)self, self->ob_refcnt, list, list->ob_refcnt);
215 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
216 if(!taglist_contains(self, tag)) {
217 mylog(" %08x(%d) taglist_concat: Adding Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
218 PyList_Append(taglist->taglist, tag);
220 mylog(" %08x(%d) taglist_concat: Already contains Tag %08x(%d)", (int)self, self->ob_refcnt, tag, tag->ob_refcnt);
224 /* copy tag, so we don't have to do INCREF(tag) (and don't
225 get problems if the tag is appended to more than one
227 /* TODO: handle IDs */
229 TAG*t = tag_getTAG(tag);
231 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
233 nt = swf_InsertTag(0, t->id);
234 swf_SetBlock(nt,t->data,t->len);
235 PyObject*newtag = tag_new(taglist->swf, nt);
236 if(swf_isDefiningTag(t)) {
237 int id = swf_GetDefineID(t);
238 PyObject*id = PyLong_FromLong(id);
239 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
241 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
248 if (PyList_Check(list)) {
249 int l = PyList_Size(list);
252 PyObject*item = PyList_GetItem(list, t);
253 if(!PY_CHECK_TYPE(item, &TagClass)) {
254 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
257 PyObject*_self = taglist_concat(self, item);
267 if (PY_CHECK_TYPE(list, &TagListClass)) {
268 TagListObject*taglist2 = (TagListObject*)list;
269 return taglist_concat(self, taglist2->taglist);
271 /*TAG* tags = taglist_getTAGs(self);
272 TAG* tags2 = taglist_getTAGs(list);
274 tags3 = swf_Concatenate(tags,tags2);
275 PyObject* newtaglist = taglist_new(tags3);
277 Py_INCREF(newtaglist);*/
281 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
284 //----------------------------------------------------------------------------
285 static PyObject * taglist_item(PyObject * self, int index)
287 TagListObject*taglist = (TagListObject*)self;
289 tag = PyList_GetItem(taglist->taglist, index);
292 mylog(" %08x(%d) taglist_item(%d): %08x", (int)self, self->ob_refcnt, index, tag);
296 //----------------------------------------------------------------------------
297 static void taglist_dealloc(PyObject* self)
299 TagListObject*taglist = (TagListObject*)self;
300 mylog("-%08x(%d) taglist_dealloc list=%08x(%d)\n", (int)self, self->ob_refcnt, taglist->taglist, taglist->taglist->ob_refcnt);
301 Py_DECREF(taglist->taglist);
302 taglist->taglist = 0;
305 //----------------------------------------------------------------------------
306 static PySequenceMethods taglist_as_sequence =
308 sq_length: taglist_length, // len(obj)
309 sq_concat: taglist_concat, // obj += [...], obj1+obj2
310 sq_repeat: 0, // x*n, intargfunc
311 sq_item: taglist_item, // obj[3]
312 sq_slice: 0, // x[i:j] intintargfunc
313 sq_ass_item: 0, // x[i] = y intobjargproc
314 sq_ass_slice: 0, // x[i:j] = v intintobjargproc
315 sq_contains: taglist_contains, //???
317 PyTypeObject TagListClass =
319 PyObject_HEAD_INIT(NULL)
322 tp_basicsize: sizeof(TagListObject),
324 tp_dealloc: taglist_dealloc,
325 tp_print: 0, // print x
326 tp_getattr: taglist_getattr, // x.attr
327 tp_setattr: 0, // x.attr = v
328 tp_compare: 0, // x>y
329 tp_repr: 0, // `x`, print x
331 tp_as_sequence: &taglist_as_sequence,