6 #include "primitives.h"
12 //----------------------------------------------------------------------------
18 //----------------------------------------------------------------------------
19 PyObject * taglist_new()
21 TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
22 mylog("+%08x(%d) taglist_new2", (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", (int)taglist, taglist->ob_refcnt);
32 taglist->tagmap = tagmap_new();
36 while(t) {nr++;t=t->next;}
37 taglist->taglist = PyList_New(nr);
42 PyObject*newtag = tag_new(tag);
43 PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
44 if(swf_isDefiningTag(t)) {
45 tagmap_add(taglist->tagmap, newtag);
50 return (PyObject*)taglist;
52 //----------------------------------------------------------------------------
53 TAG* taglist_getTAGs(PyObject*self)
55 if(!PY_CHECK_TYPE(self,&TagListClass)) {
56 PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
59 TagListObject*taglist = (TagListObject*)self;
61 /* TODO: the tags will be modified by this. We should set mutexes. */
63 int l = PyList_Size(taglist->taglist);
67 mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
69 PyObject*item = PyList_GetItem(taglist->taglist, t);
70 tag = tag_getTAG(item, tag, taglist->tagmap);
76 //----------------------------------------------------------------------------
77 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
80 TagListObject*taglist = (TagListObject*)self;
81 if(!self || !PyArg_ParseTuple(args,""))
83 swf.firstTag = taglist->firstTag;
85 taglist->firstTag = swf.firstTag;
86 taglist->lastTag = 0; // FIXME
87 taglist->searchTag = 0;*/
90 //----------------------------------------------------------------------------
91 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
94 /* TagListObject*taglist = (TagListObject*)self;
95 if(!self || !PyArg_ParseTuple(args,""))
97 swf.firstTag = taglist->firstTag;
99 taglist->firstTag = swf.firstTag;
100 taglist->lastTag = 0; // FIXME
101 taglist->searchTag = 0;*/
104 //----------------------------------------------------------------------------
105 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
108 /* TagListObject*taglist = (TagListObject*)self;
109 if(!self || !PyArg_ParseTuple(args,""))
111 swf.firstTag = taglist->firstTag;
113 taglist->firstTag = swf.firstTag;
114 taglist->lastTag = 0; // FIXME
115 taglist->searchTag = 0;*/
118 //----------------------------------------------------------------------------
119 static void taglist_dealloc(PyObject* self)
121 TagListObject*taglist = (TagListObject*)self;
122 mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt);
123 Py_DECREF(taglist->taglist);
124 taglist->taglist = 0;
125 Py_DECREF(taglist->tagmap);
129 //----------------------------------------------------------------------------
130 static PyMethodDef taglist_functions[] =
131 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
132 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
133 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
134 {NULL, NULL, 0, NULL}
137 static PyObject* taglist_getattr(PyObject * self, char* a)
139 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
140 mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
143 //----------------------------------------------------------------------------
144 static int taglist_length(PyObject * self)
146 TagListObject*tags = (TagListObject*)self;
147 mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
148 return PyList_GET_SIZE(tags->taglist);
150 //----------------------------------------------------------------------------
151 static int taglist_contains(PyObject * self, PyObject * tag)
153 mylog(" %08x(%d) taglist_contains %08x", (int)self, self->ob_refcnt, tag);
154 TagListObject*taglist = (TagListObject*)self;
155 PyObject*list = taglist->taglist;
156 int l = PyList_Size(list);
159 PyObject*item = PyList_GetItem(list, t);
161 mylog(" %08x(%d) taglist_contains: yes", (int)self, self->ob_refcnt);
165 mylog(" %08x(%d) taglist_contains: no", (int)self, self->ob_refcnt);
168 //----------------------------------------------------------------------------
169 static PyObject * taglist_concat(PyObject * self, PyObject* list)
172 PY_ASSERT_TYPE(self, &TagListClass);
173 TagListObject*taglist = (TagListObject*)self;
174 mylog(" %08x(%d) taglist_concat %08x", (int)self, self->ob_refcnt, list);
176 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
177 mylog(" %08x(%d) taglist_concat: Tag %08x", (int)self, self->ob_refcnt, tag);
178 list = tag_getDependencies(tag);
179 int l = PyList_Size(list);
181 mylog(" %08x(%d) taglist_concat: Tag: %d dependencies", (int)self, self->ob_refcnt, l);
183 PyObject*item = PyList_GetItem(list, t);
184 PyObject*_self = taglist_concat(self, item);
188 if(!taglist_contains(self, tag)) {
189 mylog(" %08x(%d) taglist_concat: Adding Tag %08x", (int)self, self->ob_refcnt, tag);
190 PyList_Append(taglist->taglist, tag);
192 mylog(" %08x(%d) taglist_concat: done", (int)self, self->ob_refcnt);
195 /* copy tag, so we don't have to do INCREF(tag) (and don't
196 get problems if the tag is appended to more than one
198 /* TODO: handle IDs */
200 TAG*t = tag_getTAG(tag);
202 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
204 nt = swf_InsertTag(0, t->id);
205 swf_SetBlock(nt,t->data,t->len);
206 PyObject*newtag = tag_new(taglist->swf, nt);
207 if(swf_isDefiningTag(t)) {
208 int id = swf_GetDefineID(t);
209 PyObject*id = PyLong_FromLong(id);
210 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
212 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
219 if (PyList_Check(list)) {
220 int l = PyList_Size(list);
222 mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt);
224 PyObject*item = PyList_GetItem(list, t);
225 if(!PY_CHECK_TYPE(item, &TagClass)) {
226 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
229 PyObject*_self = taglist_concat(self, item);
239 if (PY_CHECK_TYPE(list, &TagListClass)) {
240 mylog(" %08x(%d) taglist_concat: TagList", (int)self, self->ob_refcnt);
241 TagListObject*taglist2 = (TagListObject*)list;
242 return taglist_concat(self, taglist2->taglist);
244 /*TAG* tags = taglist_getTAGs(self);
245 TAG* tags2 = taglist_getTAGs(list);
247 tags3 = swf_Concatenate(tags,tags2);
248 PyObject* newtaglist = taglist_new(tags3);
250 Py_INCREF(newtaglist);*/
254 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
257 //----------------------------------------------------------------------------
258 static PyObject * taglist_item(PyObject * self, int index)
260 TagListObject*taglist = (TagListObject*)self;
262 mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
263 tag = PyList_GetItem(taglist->taglist, index);
264 Py_INCREF(tag); //TODO-REF
267 static PySequenceMethods taglist_as_sequence =
269 sq_length: taglist_length, // len(obj)
270 sq_concat: taglist_concat, // obj += [...], obj1+obj2
271 sq_repeat: 0, // x*n, intargfunc
272 sq_item: taglist_item, // obj[3]
273 sq_slice: 0, // x[i:j] intintargfunc
274 sq_ass_item: 0, // x[i] = y intobjargproc
275 sq_ass_slice: 0, // x[i:j] = v intintobjargproc
276 sq_contains: taglist_contains, //???
278 static PyTypeObject TagListClass =
280 PyObject_HEAD_INIT(NULL)
283 tp_basicsize: sizeof(TagListObject),
285 tp_dealloc: taglist_dealloc,
286 tp_print: 0, // print x
287 tp_getattr: taglist_getattr, // x.attr
288 tp_setattr: 0, // x.attr = v
289 tp_compare: 0, // x>y
290 tp_repr: 0, // `x`, print x
292 tp_as_sequence: &taglist_as_sequence,