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();
36 while(t) {nr++;t=t->next;}
37 taglist->taglist = PyList_New(nr);
39 mylog("+%08x(%d) taglist_new2: %d items", (int)taglist, taglist->ob_refcnt, nr);
44 PyObject*newtag = tag_new2(t, taglist->tagmap);
45 PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
46 if(swf_isDefiningTag(t)) {
47 tagmap_add(taglist->tagmap, newtag);
52 return (PyObject*)taglist;
54 //----------------------------------------------------------------------------
55 TAG* taglist_getTAGs(PyObject*self)
57 if(!PY_CHECK_TYPE(self,&TagListClass)) {
58 PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
61 TagListObject*taglist = (TagListObject*)self;
63 /* TODO: the tags will be modified by this. We should set mutexes. */
65 int l = PyList_Size(taglist->taglist);
69 mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
71 PyObject*item = PyList_GetItem(taglist->taglist, t);
72 tag = tag_getTAG(item, tag, taglist->tagmap);
75 mylog(" %08x(%d) taglist_getTAGs: added tag %08x", (int)self, self->ob_refcnt, tag);
79 //----------------------------------------------------------------------------
80 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
83 TagListObject*taglist = (TagListObject*)self;
84 if(!self || !PyArg_ParseTuple(args,""))
86 swf.firstTag = taglist->firstTag;
88 taglist->firstTag = swf.firstTag;
89 taglist->lastTag = 0; // FIXME
90 taglist->searchTag = 0;*/
93 //----------------------------------------------------------------------------
94 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
97 /* TagListObject*taglist = (TagListObject*)self;
98 if(!self || !PyArg_ParseTuple(args,""))
100 swf.firstTag = taglist->firstTag;
102 taglist->firstTag = swf.firstTag;
103 taglist->lastTag = 0; // FIXME
104 taglist->searchTag = 0;*/
107 //----------------------------------------------------------------------------
108 static PyObject * taglist_optimizeOrder(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 void taglist_dealloc(PyObject* self)
124 TagListObject*taglist = (TagListObject*)self;
125 mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt);
126 Py_DECREF(taglist->taglist);
127 taglist->taglist = 0;
128 Py_DECREF(taglist->tagmap);
132 //----------------------------------------------------------------------------
133 static PyMethodDef taglist_functions[] =
134 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
135 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
136 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
137 {NULL, NULL, 0, NULL}
140 static PyObject* taglist_getattr(PyObject * self, char* a)
142 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
143 mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
146 //----------------------------------------------------------------------------
147 static int taglist_length(PyObject * self)
149 TagListObject*tags = (TagListObject*)self;
150 mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
151 return PyList_GET_SIZE(tags->taglist);
153 //----------------------------------------------------------------------------
154 static int taglist_contains(PyObject * self, PyObject * tag)
156 mylog(" %08x(%d) taglist_contains %08x", (int)self, self->ob_refcnt, tag);
157 TagListObject*taglist = (TagListObject*)self;
158 PyObject*list = taglist->taglist;
159 int l = PyList_Size(list);
162 PyObject*item = PyList_GetItem(list, t);
164 mylog(" %08x(%d) taglist_contains: yes", (int)self, self->ob_refcnt);
168 mylog(" %08x(%d) taglist_contains: no", (int)self, self->ob_refcnt);
171 //----------------------------------------------------------------------------
172 static PyObject * taglist_concat(PyObject * self, PyObject* list)
175 PY_ASSERT_TYPE(self, &TagListClass);
176 TagListObject*taglist = (TagListObject*)self;
177 mylog(" %08x(%d) taglist_concat %08x", (int)self, self->ob_refcnt, list);
179 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
180 mylog(" %08x(%d) taglist_concat: Tag %08x", (int)self, self->ob_refcnt, tag);
181 list = tag_getDependencies(tag);
182 int l = PyList_Size(list);
184 mylog(" %08x(%d) taglist_concat: Tag: %d dependencies", (int)self, self->ob_refcnt, l);
186 PyObject*item = PyList_GetItem(list, t);
187 PyObject*_self = taglist_concat(self, item);
191 if(!taglist_contains(self, tag)) {
192 mylog(" %08x(%d) taglist_concat: Adding Tag %08x", (int)self, self->ob_refcnt, tag);
193 PyList_Append(taglist->taglist, tag);
195 mylog(" %08x(%d) taglist_concat: done", (int)self, self->ob_refcnt);
198 /* copy tag, so we don't have to do INCREF(tag) (and don't
199 get problems if the tag is appended to more than one
201 /* TODO: handle IDs */
203 TAG*t = tag_getTAG(tag);
205 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
207 nt = swf_InsertTag(0, t->id);
208 swf_SetBlock(nt,t->data,t->len);
209 PyObject*newtag = tag_new(taglist->swf, nt);
210 if(swf_isDefiningTag(t)) {
211 int id = swf_GetDefineID(t);
212 PyObject*id = PyLong_FromLong(id);
213 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
215 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
222 if (PyList_Check(list)) {
223 int l = PyList_Size(list);
225 mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt);
227 PyObject*item = PyList_GetItem(list, t);
228 if(!PY_CHECK_TYPE(item, &TagClass)) {
229 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
232 PyObject*_self = taglist_concat(self, item);
242 if (PY_CHECK_TYPE(list, &TagListClass)) {
243 mylog(" %08x(%d) taglist_concat: TagList", (int)self, self->ob_refcnt);
244 TagListObject*taglist2 = (TagListObject*)list;
245 return taglist_concat(self, taglist2->taglist);
247 /*TAG* tags = taglist_getTAGs(self);
248 TAG* tags2 = taglist_getTAGs(list);
250 tags3 = swf_Concatenate(tags,tags2);
251 PyObject* newtaglist = taglist_new(tags3);
253 Py_INCREF(newtaglist);*/
257 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
260 //----------------------------------------------------------------------------
261 static PyObject * taglist_item(PyObject * self, int index)
263 TagListObject*taglist = (TagListObject*)self;
265 mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
266 tag = PyList_GetItem(taglist->taglist, index);
267 Py_INCREF(tag); //TODO-REF
270 static PySequenceMethods taglist_as_sequence =
272 sq_length: taglist_length, // len(obj)
273 sq_concat: taglist_concat, // obj += [...], obj1+obj2
274 sq_repeat: 0, // x*n, intargfunc
275 sq_item: taglist_item, // obj[3]
276 sq_slice: 0, // x[i:j] intintargfunc
277 sq_ass_item: 0, // x[i] = y intobjargproc
278 sq_ass_slice: 0, // x[i:j] = v intintobjargproc
279 sq_contains: taglist_contains, //???
281 static PyTypeObject TagListClass =
283 PyObject_HEAD_INIT(NULL)
286 tp_basicsize: sizeof(TagListObject),
288 tp_dealloc: taglist_dealloc,
289 tp_print: 0, // print x
290 tp_getattr: taglist_getattr, // x.attr
291 tp_setattr: 0, // x.attr = v
292 tp_compare: 0, // x>y
293 tp_repr: 0, // `x`, print x
295 tp_as_sequence: &taglist_as_sequence,