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);
46 // pass through exception
49 PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
50 if(swf_isDefiningTag(t)) {
51 tagmap_add(taglist->tagmap, newtag);
56 return (PyObject*)taglist;
58 //----------------------------------------------------------------------------
59 TAG* taglist_getTAGs(PyObject*self)
61 if(!PY_CHECK_TYPE(self,&TagListClass)) {
62 PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
65 TagListObject*taglist = (TagListObject*)self;
67 /* TODO: the tags will be modified by this. We should set mutexes. */
69 int l = PyList_Size(taglist->taglist);
73 mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
75 PyObject*item = PyList_GetItem(taglist->taglist, t);
76 tag = tag_getTAG(item, tag, taglist->tagmap);
79 mylog(" %08x(%d) taglist_getTAGs: added tag %08x", (int)self, self->ob_refcnt, tag);
83 //----------------------------------------------------------------------------
84 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
87 TagListObject*taglist = (TagListObject*)self;
88 if(!self || !PyArg_ParseTuple(args,""))
90 swf.firstTag = taglist->firstTag;
92 taglist->firstTag = swf.firstTag;
93 taglist->lastTag = 0; // FIXME
94 taglist->searchTag = 0;*/
97 //----------------------------------------------------------------------------
98 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
101 /* TagListObject*taglist = (TagListObject*)self;
102 if(!self || !PyArg_ParseTuple(args,""))
104 swf.firstTag = taglist->firstTag;
106 taglist->firstTag = swf.firstTag;
107 taglist->lastTag = 0; // FIXME
108 taglist->searchTag = 0;*/
111 //----------------------------------------------------------------------------
112 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
115 /* TagListObject*taglist = (TagListObject*)self;
116 if(!self || !PyArg_ParseTuple(args,""))
118 swf.firstTag = taglist->firstTag;
120 taglist->firstTag = swf.firstTag;
121 taglist->lastTag = 0; // FIXME
122 taglist->searchTag = 0;*/
125 //----------------------------------------------------------------------------
126 static void taglist_dealloc(PyObject* self)
128 TagListObject*taglist = (TagListObject*)self;
129 mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt);
130 Py_DECREF(taglist->taglist);
131 taglist->taglist = 0;
132 Py_DECREF(taglist->tagmap);
136 //----------------------------------------------------------------------------
137 static PyMethodDef taglist_functions[] =
138 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
139 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
140 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
141 {NULL, NULL, 0, NULL}
144 static PyObject* taglist_getattr(PyObject * self, char* a)
146 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
147 mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
150 //----------------------------------------------------------------------------
151 static int taglist_length(PyObject * self)
153 TagListObject*tags = (TagListObject*)self;
154 mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
155 return PyList_GET_SIZE(tags->taglist);
157 //----------------------------------------------------------------------------
158 static int taglist_contains(PyObject * self, PyObject * tag)
160 mylog(" %08x(%d) taglist_contains %08x", (int)self, self->ob_refcnt, tag);
161 TagListObject*taglist = (TagListObject*)self;
162 PyObject*list = taglist->taglist;
163 int l = PyList_Size(list);
166 PyObject*item = PyList_GetItem(list, t);
168 mylog(" %08x(%d) taglist_contains: yes", (int)self, self->ob_refcnt);
172 mylog(" %08x(%d) taglist_contains: no", (int)self, self->ob_refcnt);
175 //----------------------------------------------------------------------------
176 static PyObject * taglist_concat(PyObject * self, PyObject* list)
179 PY_ASSERT_TYPE(self, &TagListClass);
180 TagListObject*taglist = (TagListObject*)self;
181 mylog(" %08x(%d) taglist_concat %08x", (int)self, self->ob_refcnt, list);
183 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
184 mylog(" %08x(%d) taglist_concat: Tag %08x", (int)self, self->ob_refcnt, tag);
185 list = tag_getDependencies(tag);
186 int l = PyList_Size(list);
188 mylog(" %08x(%d) taglist_concat: Tag: %d dependencies", (int)self, self->ob_refcnt, l);
190 PyObject*item = PyList_GetItem(list, t);
191 PyObject*_self = taglist_concat(self, item);
195 if(!taglist_contains(self, tag)) {
196 mylog(" %08x(%d) taglist_concat: Adding Tag %08x", (int)self, self->ob_refcnt, tag);
197 PyList_Append(taglist->taglist, tag);
199 mylog(" %08x(%d) taglist_concat: done", (int)self, self->ob_refcnt);
202 /* copy tag, so we don't have to do INCREF(tag) (and don't
203 get problems if the tag is appended to more than one
205 /* TODO: handle IDs */
207 TAG*t = tag_getTAG(tag);
209 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
211 nt = swf_InsertTag(0, t->id);
212 swf_SetBlock(nt,t->data,t->len);
213 PyObject*newtag = tag_new(taglist->swf, nt);
214 if(swf_isDefiningTag(t)) {
215 int id = swf_GetDefineID(t);
216 PyObject*id = PyLong_FromLong(id);
217 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
219 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
226 if (PyList_Check(list)) {
227 int l = PyList_Size(list);
229 mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt);
231 PyObject*item = PyList_GetItem(list, t);
232 if(!PY_CHECK_TYPE(item, &TagClass)) {
233 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
236 PyObject*_self = taglist_concat(self, item);
246 if (PY_CHECK_TYPE(list, &TagListClass)) {
247 mylog(" %08x(%d) taglist_concat: TagList", (int)self, self->ob_refcnt);
248 TagListObject*taglist2 = (TagListObject*)list;
249 return taglist_concat(self, taglist2->taglist);
251 /*TAG* tags = taglist_getTAGs(self);
252 TAG* tags2 = taglist_getTAGs(list);
254 tags3 = swf_Concatenate(tags,tags2);
255 PyObject* newtaglist = taglist_new(tags3);
257 Py_INCREF(newtaglist);*/
261 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
264 //----------------------------------------------------------------------------
265 static PyObject * taglist_item(PyObject * self, int index)
267 TagListObject*taglist = (TagListObject*)self;
269 mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
270 tag = PyList_GetItem(taglist->taglist, index);
271 Py_INCREF(tag); //TODO-REF
274 static PySequenceMethods taglist_as_sequence =
276 sq_length: taglist_length, // len(obj)
277 sq_concat: taglist_concat, // obj += [...], obj1+obj2
278 sq_repeat: 0, // x*n, intargfunc
279 sq_item: taglist_item, // obj[3]
280 sq_slice: 0, // x[i:j] intintargfunc
281 sq_ass_item: 0, // x[i] = y intobjargproc
282 sq_ass_slice: 0, // x[i:j] = v intintobjargproc
283 sq_contains: taglist_contains, //???
285 static PyTypeObject TagListClass =
287 PyObject_HEAD_INIT(NULL)
290 tp_basicsize: sizeof(TagListObject),
292 tp_dealloc: taglist_dealloc,
293 tp_print: 0, // print x
294 tp_getattr: taglist_getattr, // x.attr
295 tp_setattr: 0, // x.attr = v
296 tp_compare: 0, // x>y
297 tp_repr: 0, // `x`, print x
299 tp_as_sequence: &taglist_as_sequence,