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 int id = swf_GetDefineID(t);
52 tagmap_addMapping(taglist->tagmap, id, newtag);
57 return (PyObject*)taglist;
59 //----------------------------------------------------------------------------
60 TAG* taglist_getTAGs(PyObject*self)
62 if(!PY_CHECK_TYPE(self,&TagListClass)) {
63 PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
66 TagListObject*taglist = (TagListObject*)self;
68 /* TODO: the tags will be modified by this. We should set mutexes. */
70 int l = PyList_Size(taglist->taglist);
74 mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
76 PyObject*item = PyList_GetItem(taglist->taglist, t);
77 tag = tag_getTAG(item, tag, taglist->tagmap);
80 mylog(" %08x(%d) taglist_getTAGs: added tag %08x", (int)self, self->ob_refcnt, tag);
84 //----------------------------------------------------------------------------
85 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
88 TagListObject*taglist = (TagListObject*)self;
89 if(!self || !PyArg_ParseTuple(args,""))
91 swf.firstTag = taglist->firstTag;
93 taglist->firstTag = swf.firstTag;
94 taglist->lastTag = 0; // FIXME
95 taglist->searchTag = 0;*/
98 //----------------------------------------------------------------------------
99 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
102 /* TagListObject*taglist = (TagListObject*)self;
103 if(!self || !PyArg_ParseTuple(args,""))
105 swf.firstTag = taglist->firstTag;
107 taglist->firstTag = swf.firstTag;
108 taglist->lastTag = 0; // FIXME
109 taglist->searchTag = 0;*/
112 //----------------------------------------------------------------------------
113 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
116 /* TagListObject*taglist = (TagListObject*)self;
117 if(!self || !PyArg_ParseTuple(args,""))
119 swf.firstTag = taglist->firstTag;
121 taglist->firstTag = swf.firstTag;
122 taglist->lastTag = 0; // FIXME
123 taglist->searchTag = 0;*/
126 //----------------------------------------------------------------------------
127 static void taglist_dealloc(PyObject* self)
129 TagListObject*taglist = (TagListObject*)self;
130 mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt);
131 Py_DECREF(taglist->taglist);
132 taglist->taglist = 0;
133 Py_DECREF(taglist->tagmap);
137 //----------------------------------------------------------------------------
138 static PyMethodDef taglist_functions[] =
139 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
140 {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
141 {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
142 {NULL, NULL, 0, NULL}
145 static PyObject* taglist_getattr(PyObject * self, char* a)
147 PyObject* ret = Py_FindMethod(taglist_functions, self, a);
148 mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
151 //----------------------------------------------------------------------------
152 static int taglist_length(PyObject * self)
154 TagListObject*tags = (TagListObject*)self;
155 mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
156 return PyList_GET_SIZE(tags->taglist);
158 //----------------------------------------------------------------------------
159 static int taglist_contains(PyObject * self, PyObject * tag)
161 mylog(" %08x(%d) taglist_contains %08x", (int)self, self->ob_refcnt, tag);
162 TagListObject*taglist = (TagListObject*)self;
163 PyObject*list = taglist->taglist;
164 int l = PyList_Size(list);
167 PyObject*item = PyList_GetItem(list, t);
169 mylog(" %08x(%d) taglist_contains: yes", (int)self, self->ob_refcnt);
173 mylog(" %08x(%d) taglist_contains: no", (int)self, self->ob_refcnt);
176 //----------------------------------------------------------------------------
177 static PyObject * taglist_concat(PyObject * self, PyObject* list)
180 PY_ASSERT_TYPE(self, &TagListClass);
181 TagListObject*taglist = (TagListObject*)self;
182 mylog(" %08x(%d) taglist_concat %08x", (int)self, self->ob_refcnt, list);
184 if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
185 mylog(" %08x(%d) taglist_concat: Tag %08x", (int)self, self->ob_refcnt, tag);
186 list = tag_getDependencies(tag);
187 int l = PyList_Size(list);
189 mylog(" %08x(%d) taglist_concat: Tag: %d dependencies", (int)self, self->ob_refcnt, l);
191 PyObject*item = PyList_GetItem(list, t);
192 PyObject*_self = taglist_concat(self, item);
196 if(!taglist_contains(self, tag)) {
197 mylog(" %08x(%d) taglist_concat: Adding Tag %08x", (int)self, self->ob_refcnt, tag);
198 PyList_Append(taglist->taglist, tag);
200 mylog(" %08x(%d) taglist_concat: done", (int)self, self->ob_refcnt);
203 /* copy tag, so we don't have to do INCREF(tag) (and don't
204 get problems if the tag is appended to more than one
206 /* TODO: handle IDs */
208 TAG*t = tag_getTAG(tag);
210 mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
212 nt = swf_InsertTag(0, t->id);
213 swf_SetBlock(nt,t->data,t->len);
214 PyObject*newtag = tag_new(taglist->swf, nt);
215 if(swf_isDefiningTag(t)) {
216 int id = swf_GetDefineID(t);
217 PyObject*id = PyLong_FromLong(id);
218 PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
220 PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
227 if (PyList_Check(list)) {
228 int l = PyList_Size(list);
230 mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt);
232 PyObject*item = PyList_GetItem(list, t);
233 if(!PY_CHECK_TYPE(item, &TagClass)) {
234 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
237 PyObject*_self = taglist_concat(self, item);
247 if (PY_CHECK_TYPE(list, &TagListClass)) {
248 mylog(" %08x(%d) taglist_concat: TagList", (int)self, self->ob_refcnt);
249 TagListObject*taglist2 = (TagListObject*)list;
250 return taglist_concat(self, taglist2->taglist);
252 /*TAG* tags = taglist_getTAGs(self);
253 TAG* tags2 = taglist_getTAGs(list);
255 tags3 = swf_Concatenate(tags,tags2);
256 PyObject* newtaglist = taglist_new(tags3);
258 Py_INCREF(newtaglist);*/
262 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
265 //----------------------------------------------------------------------------
266 static PyObject * taglist_item(PyObject * self, int index)
268 TagListObject*taglist = (TagListObject*)self;
270 mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
271 tag = PyList_GetItem(taglist->taglist, index);
272 Py_INCREF(tag); //TODO-REF
275 static PySequenceMethods taglist_as_sequence =
277 sq_length: taglist_length, // len(obj)
278 sq_concat: taglist_concat, // obj += [...], obj1+obj2
279 sq_repeat: 0, // x*n, intargfunc
280 sq_item: taglist_item, // obj[3]
281 sq_slice: 0, // x[i:j] intintargfunc
282 sq_ass_item: 0, // x[i] = y intobjargproc
283 sq_ass_slice: 0, // x[i:j] = v intintobjargproc
284 sq_contains: taglist_contains, //???
286 static PyTypeObject TagListClass =
288 PyObject_HEAD_INIT(NULL)
291 tp_basicsize: sizeof(TagListObject),
293 tp_dealloc: taglist_dealloc,
294 tp_print: 0, // print x
295 tp_getattr: taglist_getattr, // x.attr
296 tp_setattr: 0, // x.attr = v
297 tp_compare: 0, // x>y
298 tp_repr: 0, // `x`, print x
300 tp_as_sequence: &taglist_as_sequence,